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: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 347 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 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 ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 375 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 376 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 377 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
| 378 | return( PSA_ERROR_HARDWARE_FAILURE ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 379 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 380 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 381 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 382 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 383 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 384 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 388 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 389 | |
| 390 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 391 | /****************************************************************/ |
| 392 | /* Key management */ |
| 393 | /****************************************************************/ |
| 394 | |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 395 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 396 | static inline int psa_key_slot_is_external( const psa_key_slot_t *slot ) |
| 397 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 398 | return( psa_key_lifetime_is_external( slot->attr.lifetime ) ); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 399 | } |
| 400 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 401 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 402 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 403 | * current test driver in key_management.c is using this function |
| 404 | * when accelerators are used for ECC key pair and public key. |
| 405 | * Once that dependency is resolved these guards can be removed. |
| 406 | */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 407 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 408 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 409 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 410 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 411 | 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] | 412 | size_t byte_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 413 | { |
| 414 | switch( curve ) |
| 415 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 416 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 417 | switch( byte_length ) |
| 418 | { |
| 419 | case PSA_BITS_TO_BYTES( 192 ): |
| 420 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
| 421 | case PSA_BITS_TO_BYTES( 224 ): |
| 422 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
| 423 | case PSA_BITS_TO_BYTES( 256 ): |
| 424 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
| 425 | case PSA_BITS_TO_BYTES( 384 ): |
| 426 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
| 427 | case PSA_BITS_TO_BYTES( 521 ): |
| 428 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 429 | default: |
| 430 | return( MBEDTLS_ECP_DP_NONE ); |
| 431 | } |
| 432 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 433 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 434 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 435 | switch( byte_length ) |
| 436 | { |
| 437 | case PSA_BITS_TO_BYTES( 256 ): |
| 438 | return( MBEDTLS_ECP_DP_BP256R1 ); |
| 439 | case PSA_BITS_TO_BYTES( 384 ): |
| 440 | return( MBEDTLS_ECP_DP_BP384R1 ); |
| 441 | case PSA_BITS_TO_BYTES( 512 ): |
| 442 | return( MBEDTLS_ECP_DP_BP512R1 ); |
| 443 | default: |
| 444 | return( MBEDTLS_ECP_DP_NONE ); |
| 445 | } |
| 446 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 447 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 448 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 449 | switch( byte_length ) |
| 450 | { |
| 451 | case PSA_BITS_TO_BYTES( 255 ): |
| 452 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 453 | case PSA_BITS_TO_BYTES( 448 ): |
| 454 | return( MBEDTLS_ECP_DP_CURVE448 ); |
| 455 | default: |
| 456 | return( MBEDTLS_ECP_DP_NONE ); |
| 457 | } |
| 458 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 459 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 460 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 461 | switch( byte_length ) |
| 462 | { |
| 463 | case PSA_BITS_TO_BYTES( 192 ): |
| 464 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
| 465 | case PSA_BITS_TO_BYTES( 224 ): |
| 466 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
| 467 | case PSA_BITS_TO_BYTES( 256 ): |
| 468 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
| 469 | default: |
| 470 | return( MBEDTLS_ECP_DP_NONE ); |
| 471 | } |
| 472 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 473 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 474 | default: |
| 475 | return( MBEDTLS_ECP_DP_NONE ); |
| 476 | } |
| 477 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 478 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 479 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 480 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || |
| 481 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 482 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 483 | static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type, |
| 484 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 485 | { |
| 486 | /* Check that the bit size is acceptable for the key type */ |
| 487 | switch( type ) |
| 488 | { |
| 489 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 490 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 491 | case PSA_KEY_TYPE_DERIVE: |
| 492 | break; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 493 | #if defined(MBEDTLS_AES_C) |
| 494 | case PSA_KEY_TYPE_AES: |
| 495 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 496 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 497 | break; |
| 498 | #endif |
| 499 | #if defined(MBEDTLS_CAMELLIA_C) |
| 500 | case PSA_KEY_TYPE_CAMELLIA: |
| 501 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 502 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 503 | break; |
| 504 | #endif |
| 505 | #if defined(MBEDTLS_DES_C) |
| 506 | case PSA_KEY_TYPE_DES: |
| 507 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 508 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 509 | break; |
| 510 | #endif |
| 511 | #if defined(MBEDTLS_ARC4_C) |
| 512 | case PSA_KEY_TYPE_ARC4: |
| 513 | if( bits < 8 || bits > 2048 ) |
| 514 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 515 | break; |
| 516 | #endif |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 517 | #if defined(MBEDTLS_CHACHA20_C) |
| 518 | case PSA_KEY_TYPE_CHACHA20: |
| 519 | if( bits != 256 ) |
| 520 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 521 | break; |
| 522 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 523 | default: |
| 524 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 525 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 526 | if( bits % 8 != 0 ) |
| 527 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 528 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 529 | return( PSA_SUCCESS ); |
| 530 | } |
| 531 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 532 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 533 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 534 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 535 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
| 536 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 537 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 538 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 539 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 540 | * that are not a multiple of 8) well. For example, there is only |
| 541 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 542 | * way to return the exact bit size of a key. |
| 543 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 544 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 545 | const mbedtls_rsa_context *rsa ) |
| 546 | { |
| 547 | mbedtls_mpi n; |
| 548 | psa_status_t status; |
| 549 | mbedtls_mpi_init( &n ); |
| 550 | status = mbedtls_to_psa_error( |
| 551 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 552 | if( status == PSA_SUCCESS ) |
| 553 | { |
| 554 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 555 | status = PSA_ERROR_NOT_SUPPORTED; |
| 556 | } |
| 557 | mbedtls_mpi_free( &n ); |
| 558 | return( status ); |
| 559 | } |
| 560 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 561 | /** Load the contents of a key buffer into an internal RSA representation |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 562 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 563 | * \param[in] type The type of key contained in \p data. |
| 564 | * \param[in] data The buffer from which to load the representation. |
| 565 | * \param[in] data_length The size in bytes of \p data. |
| 566 | * \param[out] p_rsa Returns a pointer to an RSA context on success. |
| 567 | * The caller is responsible for freeing both the |
| 568 | * contents of the context and the context itself |
| 569 | * when done. |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 570 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 571 | static psa_status_t psa_load_rsa_representation( psa_key_type_t type, |
| 572 | const uint8_t *data, |
| 573 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 574 | mbedtls_rsa_context **p_rsa ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 575 | { |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 576 | psa_status_t status; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 577 | mbedtls_pk_context ctx; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 578 | size_t bits; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 579 | mbedtls_pk_init( &ctx ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 580 | |
| 581 | /* Parse the data. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 582 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
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_key( &ctx, data, data_length, NULL, 0 ) ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 585 | else |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 586 | status = mbedtls_to_psa_error( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 587 | mbedtls_pk_parse_public_key( &ctx, data, data_length ) ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 588 | if( status != PSA_SUCCESS ) |
| 589 | goto exit; |
| 590 | |
| 591 | /* We have something that the pkparse module recognizes. If it is a |
| 592 | * valid RSA key, store it. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 593 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
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 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 596 | goto exit; |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 597 | } |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 598 | |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 599 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 600 | * supports non-byte-aligned key sizes, but not well. For example, |
| 601 | * 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] | 602 | 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] | 603 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 604 | { |
| 605 | status = PSA_ERROR_NOT_SUPPORTED; |
| 606 | goto exit; |
| 607 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 608 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 609 | if( status != PSA_SUCCESS ) |
| 610 | goto exit; |
| 611 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 612 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 613 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 614 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 615 | ctx.pk_info = NULL; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 616 | |
| 617 | exit: |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 618 | mbedtls_pk_free( &ctx ); |
| 619 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 620 | } |
| 621 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 622 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 623 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 624 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 625 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 626 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 627 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 628 | |
| 629 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 630 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 631 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 632 | /** Export an RSA key to export representation |
| 633 | * |
| 634 | * \param[in] type The type of key (public/private) to export |
| 635 | * \param[in] rsa The internal RSA representation from which to export |
| 636 | * \param[out] data The buffer to export to |
| 637 | * \param[in] data_size The length of the buffer to export to |
| 638 | * \param[out] data_length The amount of bytes written to \p data |
| 639 | */ |
| 640 | static psa_status_t psa_export_rsa_key( psa_key_type_t type, |
| 641 | mbedtls_rsa_context *rsa, |
| 642 | uint8_t *data, |
| 643 | size_t data_size, |
| 644 | size_t *data_length ) |
| 645 | { |
| 646 | #if defined(MBEDTLS_PK_WRITE_C) |
| 647 | int ret; |
| 648 | mbedtls_pk_context pk; |
| 649 | uint8_t *pos = data + data_size; |
| 650 | |
| 651 | mbedtls_pk_init( &pk ); |
| 652 | pk.pk_info = &mbedtls_rsa_info; |
| 653 | pk.pk_ctx = rsa; |
| 654 | |
| 655 | /* 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] | 656 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 657 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 658 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 659 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 660 | else |
| 661 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 662 | |
| 663 | if( ret < 0 ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 664 | { |
| 665 | /* Clean up in case pk_write failed halfway through. */ |
| 666 | memset( data, 0, data_size ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 667 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 668 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 669 | |
| 670 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 671 | * Move the data to the beginning and erase remaining data |
| 672 | * at the original location. */ |
| 673 | if( 2 * (size_t) ret <= data_size ) |
| 674 | { |
| 675 | memcpy( data, data + data_size - ret, ret ); |
| 676 | memset( data + data_size - ret, 0, ret ); |
| 677 | } |
| 678 | else if( (size_t) ret < data_size ) |
| 679 | { |
| 680 | memmove( data, data + data_size - ret, ret ); |
| 681 | memset( data + ret, 0, data_size - ret ); |
| 682 | } |
| 683 | |
| 684 | *data_length = ret; |
| 685 | return( PSA_SUCCESS ); |
| 686 | #else |
| 687 | (void) type; |
| 688 | (void) rsa; |
| 689 | (void) data; |
| 690 | (void) data_size; |
| 691 | (void) data_length; |
| 692 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 693 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 694 | } |
| 695 | |
| 696 | /** Import an RSA key from import representation to a slot |
| 697 | * |
| 698 | * \param[in,out] slot The slot where to store the export representation to |
| 699 | * \param[in] data The buffer containing the import representation |
| 700 | * \param[in] data_length The amount of bytes in \p data |
| 701 | */ |
| 702 | static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot, |
| 703 | const uint8_t *data, |
| 704 | size_t data_length ) |
| 705 | { |
| 706 | psa_status_t status; |
| 707 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 708 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 709 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 710 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 711 | status = psa_load_rsa_representation( slot->attr.type, |
| 712 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 713 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 714 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 715 | if( status != PSA_SUCCESS ) |
| 716 | goto exit; |
| 717 | |
| 718 | slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 719 | mbedtls_rsa_get_len( rsa ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 720 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 721 | /* Re-export the data to PSA export format, such that we can store export |
| 722 | * representation in the key slot. Export representation in case of RSA is |
| 723 | * the smallest representation that's allowed as input, so a straight-up |
| 724 | * 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] | 725 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 726 | if( output == NULL ) |
| 727 | { |
| 728 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 729 | goto exit; |
| 730 | } |
| 731 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 732 | status = psa_export_rsa_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 733 | rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 734 | output, |
| 735 | data_length, |
| 736 | &data_length); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 737 | exit: |
| 738 | /* Always free the RSA object */ |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 739 | mbedtls_rsa_free( rsa ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 740 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 741 | |
| 742 | /* Free the allocated buffer only on error. */ |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 743 | if( status != PSA_SUCCESS ) |
| 744 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 745 | mbedtls_free( output ); |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 746 | return( status ); |
| 747 | } |
| 748 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 749 | /* On success, store the allocated export-formatted key. */ |
| 750 | slot->data.key.data = output; |
| 751 | slot->data.key.bytes = data_length; |
Jaeden Amero | cd09d8c | 2019-01-11 17:53:05 +0000 | [diff] [blame] | 752 | |
| 753 | return( PSA_SUCCESS ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 754 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 755 | |
| 756 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 757 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 758 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 759 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 760 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 761 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 762 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \ |
| 763 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 764 | /** Load the contents of a key buffer into an internal ECP representation |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 765 | * |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 766 | * \param[in] type The type of key contained in \p data. |
| 767 | * \param[in] data The buffer from which to load the representation. |
| 768 | * \param[in] data_length The size in bytes of \p data. |
| 769 | * \param[out] p_ecp Returns a pointer to an ECP context on success. |
| 770 | * The caller is responsible for freeing both the |
| 771 | * contents of the context and the context itself |
| 772 | * when done. |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 773 | */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 774 | static psa_status_t psa_load_ecp_representation( psa_key_type_t type, |
| 775 | const uint8_t *data, |
| 776 | size_t data_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 777 | mbedtls_ecp_keypair **p_ecp ) |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 778 | { |
| 779 | mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 780 | psa_status_t status; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 781 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 782 | size_t curve_size = data_length; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 783 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 784 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) && |
| 785 | PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY ) |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 786 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 787 | /* A Weierstrass public key is represented as: |
| 788 | * - The byte 0x04; |
| 789 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 790 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 791 | * 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] | 792 | */ |
| 793 | if( ( data_length & 1 ) == 0 ) |
| 794 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 795 | curve_size = data_length / 2; |
| 796 | |
| 797 | /* Montgomery public keys are represented in compressed format, meaning |
| 798 | * their curve_size is equal to the amount of input. */ |
| 799 | |
| 800 | /* Private keys are represented in uncompressed private random integer |
| 801 | * format, meaning their curve_size is equal to the amount of input. */ |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 802 | } |
| 803 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 804 | /* Allocate and initialize a key representation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 805 | ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 806 | if( ecp == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 807 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 808 | mbedtls_ecp_keypair_init( ecp ); |
| 809 | |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 810 | /* Load the group. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 811 | grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ), |
| 812 | curve_size ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 813 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 814 | { |
| 815 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 816 | goto exit; |
| 817 | } |
| 818 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 819 | status = mbedtls_to_psa_error( |
| 820 | mbedtls_ecp_group_load( &ecp->grp, grp_id ) ); |
| 821 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 822 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 823 | |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 824 | /* Load the key material. */ |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 825 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 826 | { |
| 827 | /* Load the public value. */ |
| 828 | status = mbedtls_to_psa_error( |
| 829 | mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 830 | data, |
| 831 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 832 | if( status != PSA_SUCCESS ) |
| 833 | goto exit; |
| 834 | |
| 835 | /* Check that the point is on the curve. */ |
| 836 | status = mbedtls_to_psa_error( |
| 837 | mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) ); |
| 838 | if( status != PSA_SUCCESS ) |
| 839 | goto exit; |
| 840 | } |
| 841 | else |
| 842 | { |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 843 | /* Load and validate the secret value. */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 844 | status = mbedtls_to_psa_error( |
| 845 | mbedtls_ecp_read_key( ecp->grp.id, |
| 846 | ecp, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 847 | data, |
| 848 | data_length ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 849 | if( status != PSA_SUCCESS ) |
| 850 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 851 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 852 | |
| 853 | *p_ecp = ecp; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 854 | exit: |
| 855 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 856 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 857 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 858 | mbedtls_free( ecp ); |
| 859 | } |
| 860 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 861 | return( status ); |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 862 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 863 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 864 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 865 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 866 | * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || |
| 867 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 868 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 869 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 870 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 871 | /** Export an ECP key to export representation |
| 872 | * |
| 873 | * \param[in] type The type of key (public/private) to export |
| 874 | * \param[in] ecp The internal ECP representation from which to export |
| 875 | * \param[out] data The buffer to export to |
| 876 | * \param[in] data_size The length of the buffer to export to |
| 877 | * \param[out] data_length The amount of bytes written to \p data |
| 878 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 879 | static psa_status_t psa_export_ecp_key( psa_key_type_t type, |
| 880 | mbedtls_ecp_keypair *ecp, |
| 881 | uint8_t *data, |
| 882 | size_t data_size, |
| 883 | size_t *data_length ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 884 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 885 | psa_status_t status; |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 886 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 887 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 888 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 889 | /* Check whether the public part is loaded */ |
| 890 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 891 | { |
| 892 | /* Calculate the public key */ |
| 893 | status = mbedtls_to_psa_error( |
| 894 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 895 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 896 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 897 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 900 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 901 | mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q, |
| 902 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 903 | data_length, |
| 904 | data, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 905 | data_size ) ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 906 | if( status != PSA_SUCCESS ) |
| 907 | memset( data, 0, data_size ); |
| 908 | |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 909 | return( status ); |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 910 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 911 | else |
| 912 | { |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 913 | if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 914 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 915 | |
| 916 | status = mbedtls_to_psa_error( |
| 917 | mbedtls_ecp_write_key( ecp, |
| 918 | data, |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 919 | PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 920 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 921 | *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 922 | else |
| 923 | memset( data, 0, data_size ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 924 | |
| 925 | return( status ); |
| 926 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 927 | } |
Gilles Peskine | af89fd7 | 2018-06-29 19:52:37 +0200 | [diff] [blame] | 928 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 929 | /** Import an ECP key from import representation to a slot |
| 930 | * |
| 931 | * \param[in,out] slot The slot where to store the export representation to |
| 932 | * \param[in] data The buffer containing the import representation |
| 933 | * \param[in] data_length The amount of bytes in \p data |
| 934 | */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 935 | static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot, |
| 936 | const uint8_t *data, |
| 937 | size_t data_length ) |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 938 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 939 | psa_status_t status; |
| 940 | uint8_t* output = NULL; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 941 | mbedtls_ecp_keypair *ecp = NULL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 942 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 943 | /* Parse input */ |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 944 | status = psa_load_ecp_representation( slot->attr.type, |
| 945 | data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 946 | data_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 947 | &ecp ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 948 | if( status != PSA_SUCCESS ) |
| 949 | goto exit; |
Gilles Peskine | 4cd3277 | 2019-12-02 20:49:42 +0100 | [diff] [blame] | 950 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 951 | 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] | 952 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 953 | else |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 954 | slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits; |
Steven Cooreman | e3fd392 | 2020-06-11 16:50:36 +0200 | [diff] [blame] | 955 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 956 | /* Re-export the data to PSA export format. There is currently no support |
| 957 | * for other input formats then the export format, so this is a 1-1 |
| 958 | * copy operation. */ |
| 959 | output = mbedtls_calloc( 1, data_length ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 960 | if( output == NULL ) |
| 961 | { |
| 962 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 963 | goto exit; |
| 964 | } |
| 965 | |
| 966 | status = psa_export_ecp_key( slot->attr.type, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 967 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 968 | output, |
| 969 | data_length, |
| 970 | &data_length); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 971 | exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 972 | /* Always free the PK object (will also free contained ECP context) */ |
| 973 | mbedtls_ecp_keypair_free( ecp ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 974 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 975 | |
| 976 | /* Free the allocated buffer only on error. */ |
| 977 | if( status != PSA_SUCCESS ) |
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 | mbedtls_free( output ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 980 | return( status ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 981 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 982 | |
| 983 | /* On success, store the allocated export-formatted key. */ |
| 984 | slot->data.key.data = output; |
| 985 | slot->data.key.bytes = data_length; |
| 986 | |
| 987 | return( PSA_SUCCESS ); |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 988 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 989 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 990 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 991 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 992 | /** Return the size of the key in the given slot, in bits. |
| 993 | * |
| 994 | * \param[in] slot A key slot. |
| 995 | * |
| 996 | * \return The key size in bits, read from the metadata in the slot. |
| 997 | */ |
| 998 | static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot ) |
| 999 | { |
| 1000 | return( slot->attr.bits ); |
| 1001 | } |
| 1002 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1003 | /** Try to allocate a buffer to an empty key slot. |
| 1004 | * |
| 1005 | * \param[in,out] slot Key slot to attach buffer to. |
| 1006 | * \param[in] buffer_length Requested size of the buffer. |
| 1007 | * |
| 1008 | * \retval #PSA_SUCCESS |
| 1009 | * The buffer has been successfully allocated. |
| 1010 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1011 | * Not enough memory was available for allocation. |
| 1012 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1013 | * Trying to allocate a buffer to a non-empty key slot. |
| 1014 | */ |
| 1015 | static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 1016 | size_t buffer_length ) |
| 1017 | { |
| 1018 | if( slot->data.key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1019 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1020 | |
| 1021 | slot->data.key.data = mbedtls_calloc( 1, buffer_length ); |
| 1022 | if( slot->data.key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1023 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1024 | |
| 1025 | slot->data.key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1026 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1029 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 1030 | const uint8_t* data, |
| 1031 | size_t data_length ) |
| 1032 | { |
| 1033 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 1034 | data_length ); |
| 1035 | if( status != PSA_SUCCESS ) |
| 1036 | return( status ); |
| 1037 | |
| 1038 | memcpy( slot->data.key.data, data, data_length ); |
| 1039 | return( PSA_SUCCESS ); |
| 1040 | } |
| 1041 | |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1042 | /** Import key data into a slot. |
| 1043 | * |
| 1044 | * `slot->type` must have been set previously. |
| 1045 | * This function assumes that the slot does not contain any key material yet. |
| 1046 | * On failure, the slot content is unchanged. |
| 1047 | * |
| 1048 | * Persistent storage is not affected. |
| 1049 | * |
| 1050 | * \param[in,out] slot The key slot to import data into. |
| 1051 | * Its `type` field must have previously been set to |
| 1052 | * the desired key type. |
| 1053 | * It must not contain any key material yet. |
| 1054 | * \param[in] data Buffer containing the key material to parse and import. |
| 1055 | * \param data_length Size of \p data in bytes. |
| 1056 | * |
| 1057 | * \retval #PSA_SUCCESS |
| 1058 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1059 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1060 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1061 | */ |
| 1062 | static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot, |
| 1063 | const uint8_t *data, |
| 1064 | size_t data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1065 | { |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1066 | psa_status_t status = PSA_SUCCESS; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1067 | size_t bit_size; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1068 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1069 | /* zero-length keys are never supported. */ |
| 1070 | if( data_length == 0 ) |
| 1071 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1072 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1073 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1074 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 1075 | bit_size = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1076 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1077 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
| 1078 | if( data_length > SIZE_MAX / 8 ) |
| 1079 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1080 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 1081 | /* Enforce a size limit, and in particular ensure that the bit |
| 1082 | * size fits in its representation type. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1083 | if( bit_size > PSA_MAX_KEY_BITS ) |
| 1084 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1085 | |
| 1086 | status = validate_unstructured_key_bit_size( slot->attr.type, bit_size ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 1087 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1088 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1089 | |
| 1090 | /* Allocate memory for the key */ |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 1091 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1092 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1093 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1094 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 1095 | /* Write the actual key size to the slot. |
| 1096 | * psa_start_key_creation() wrote the size declared by the |
| 1097 | * caller, which may be 0 (meaning unspecified) or wrong. */ |
| 1098 | slot->attr.bits = (psa_key_bits_t) bit_size; |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1099 | |
| 1100 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1101 | } |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1102 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1103 | { |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1104 | /* Try validation through accelerators first. */ |
| 1105 | bit_size = slot->attr.bits; |
| 1106 | psa_key_attributes_t attributes = { |
| 1107 | .core = slot->attr |
| 1108 | }; |
| 1109 | status = psa_driver_wrapper_validate_key( &attributes, |
| 1110 | data, |
| 1111 | data_length, |
| 1112 | &bit_size ); |
| 1113 | if( status == PSA_SUCCESS ) |
| 1114 | { |
| 1115 | /* Key has been validated successfully by an accelerator. |
| 1116 | * Copy key material into slot. */ |
| 1117 | status = psa_copy_key_material_into_slot( slot, data, data_length ); |
| 1118 | if( status != PSA_SUCCESS ) |
| 1119 | return( status ); |
| 1120 | |
| 1121 | slot->attr.bits = (psa_key_bits_t) bit_size; |
| 1122 | return( PSA_SUCCESS ); |
| 1123 | } |
| 1124 | else if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1125 | return( status ); |
| 1126 | |
| 1127 | /* Key format is not supported by any accelerator, try software fallback |
| 1128 | * if present. */ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1129 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1130 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1131 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
| 1132 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1133 | return( psa_import_ecp_key( slot, data, data_length ) ); |
| 1134 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1135 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1136 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1137 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1138 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1139 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1140 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1141 | return( psa_import_rsa_key( slot, data, data_length ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 1142 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1143 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1144 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1145 | |
| 1146 | /* Fell through the fallback as well, so have nothing else to try. */ |
| 1147 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1148 | } |
| 1149 | else |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 1150 | { |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 1151 | /* Unknown key type */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1152 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1153 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1154 | } |
| 1155 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1156 | /** Calculate the intersection of two algorithm usage policies. |
| 1157 | * |
| 1158 | * Return 0 (which allows no operation) on incompatibility. |
| 1159 | */ |
| 1160 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 1161 | psa_algorithm_t alg1, |
| 1162 | psa_algorithm_t alg2 ) |
| 1163 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1164 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1165 | if( alg1 == alg2 ) |
| 1166 | return( alg1 ); |
| 1167 | /* If the policies are from the same hash-and-sign family, check |
| 1168 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 1169 | if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) && |
| 1170 | PSA_ALG_IS_HASH_AND_SIGN( alg2 ) && |
| 1171 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
| 1172 | { |
| 1173 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 1174 | return( alg2 ); |
| 1175 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 1176 | return( alg1 ); |
| 1177 | } |
| 1178 | /* If the policies are incompatible, allow nothing. */ |
| 1179 | return( 0 ); |
| 1180 | } |
| 1181 | |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1182 | static int psa_key_algorithm_permits( psa_algorithm_t policy_alg, |
| 1183 | psa_algorithm_t requested_alg ) |
| 1184 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 1185 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1186 | if( requested_alg == policy_alg ) |
| 1187 | return( 1 ); |
| 1188 | /* 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] | 1189 | * and requested_alg is the same hash-and-sign family with any hash, |
| 1190 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1191 | if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) && |
| 1192 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
| 1193 | { |
| 1194 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 1195 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 1196 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1197 | /* 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] | 1198 | * a key derivation with that key agreement should also be allowed. This |
| 1199 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1200 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 1201 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 1202 | { |
| 1203 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 1204 | policy_alg ); |
| 1205 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1206 | /* If it isn't permitted, it's forbidden. */ |
| 1207 | return( 0 ); |
| 1208 | } |
| 1209 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1210 | /** Test whether a policy permits an algorithm. |
| 1211 | * |
| 1212 | * The caller must test usage flags separately. |
| 1213 | */ |
| 1214 | static int psa_key_policy_permits( const psa_key_policy_t *policy, |
| 1215 | psa_algorithm_t alg ) |
| 1216 | { |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1217 | return( psa_key_algorithm_permits( policy->alg, alg ) || |
| 1218 | psa_key_algorithm_permits( policy->alg2, alg ) ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1221 | /** Restrict a key policy based on a constraint. |
| 1222 | * |
| 1223 | * \param[in,out] policy The policy to restrict. |
| 1224 | * \param[in] constraint The policy constraint to apply. |
| 1225 | * |
| 1226 | * \retval #PSA_SUCCESS |
| 1227 | * \c *policy contains the intersection of the original value of |
| 1228 | * \c *policy and \c *constraint. |
| 1229 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1230 | * \c *policy and \c *constraint are incompatible. |
| 1231 | * \c *policy is unchanged. |
| 1232 | */ |
| 1233 | static psa_status_t psa_restrict_key_policy( |
| 1234 | psa_key_policy_t *policy, |
| 1235 | const psa_key_policy_t *constraint ) |
| 1236 | { |
| 1237 | psa_algorithm_t intersection_alg = |
| 1238 | psa_key_policy_algorithm_intersection( policy->alg, constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1239 | psa_algorithm_t intersection_alg2 = |
| 1240 | psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1241 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 1242 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1243 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 1244 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1245 | policy->usage &= constraint->usage; |
| 1246 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1247 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1248 | return( PSA_SUCCESS ); |
| 1249 | } |
| 1250 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1251 | /** Get the description of a key given its identifier and policy constraints |
| 1252 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1253 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1254 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
| 1255 | * nonzero, the key must allow operations with this algorithm. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 1256 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1257 | * In case of a persistent key, the function loads the description of the key |
| 1258 | * into a key slot if not already done. |
| 1259 | * |
| 1260 | * On success, the returned key slot is locked. It is the responsibility of |
| 1261 | * 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] | 1262 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1263 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 1264 | mbedtls_svc_key_id_t key, |
| 1265 | psa_key_slot_t **p_slot, |
| 1266 | psa_key_usage_t usage, |
| 1267 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1268 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1269 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1270 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1271 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1272 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1273 | if( status != PSA_SUCCESS ) |
| 1274 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1275 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1276 | |
| 1277 | /* Enforce that usage policy for the key slot contains all the flags |
| 1278 | * required by the usage parameter. There is one exception: public |
| 1279 | * keys can always be exported, so we treat public key objects as |
| 1280 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1281 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1282 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1283 | |
| 1284 | status = PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1285 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1286 | goto error; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1287 | |
| 1288 | /* Enforce that the usage policy permits the requested algortihm. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1289 | if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1290 | goto error; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1291 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1292 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1293 | |
| 1294 | error: |
| 1295 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1296 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1297 | |
| 1298 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1299 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1300 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1301 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1302 | * |
| 1303 | * A transparent key is a key for which the key material is directly |
| 1304 | * available, as opposed to a key in a secure element. |
| 1305 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1306 | * This is a temporary function to use instead of |
| 1307 | * psa_get_and_lock_key_slot_with_policy() until secure element support is |
| 1308 | * fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1309 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1310 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1311 | * 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] | 1312 | */ |
| 1313 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1314 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1315 | mbedtls_svc_key_id_t key, |
| 1316 | psa_key_slot_t **p_slot, |
| 1317 | psa_key_usage_t usage, |
| 1318 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1319 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1320 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 1321 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1322 | if( status != PSA_SUCCESS ) |
| 1323 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1324 | |
Gilles Peskine | adad813 | 2019-07-25 11:31:23 +0200 | [diff] [blame] | 1325 | if( psa_key_slot_is_external( *p_slot ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1326 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1327 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1328 | *p_slot = NULL; |
| 1329 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1330 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1331 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1332 | return( PSA_SUCCESS ); |
| 1333 | } |
| 1334 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1335 | /* With no secure element support, all keys are transparent. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1336 | #define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \ |
| 1337 | 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] | 1338 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1339 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1340 | /** Wipe key data from a slot. Preserve metadata such as the policy. */ |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1341 | 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] | 1342 | { |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1343 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Fredrik Strupe | 462aa57 | 2020-12-17 10:44:38 +0100 | [diff] [blame] | 1344 | if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) && |
| 1345 | psa_key_slot_is_external( slot ) ) |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1346 | { |
| 1347 | /* No key material to clean. */ |
| 1348 | } |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 1349 | else |
| 1350 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1351 | { |
Steven Cooreman | fd4d69a | 2020-08-05 15:46:33 +0200 | [diff] [blame] | 1352 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1353 | * initialized slot, so we can just free it. */ |
| 1354 | if( slot->data.key.data != NULL ) |
| 1355 | mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1356 | mbedtls_free( slot->data.key.data ); |
| 1357 | slot->data.key.data = NULL; |
| 1358 | slot->data.key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1359 | } |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1360 | |
| 1361 | return( PSA_SUCCESS ); |
| 1362 | } |
| 1363 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1364 | /** Completely wipe a slot in memory, including its policy. |
| 1365 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 1366 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1367 | { |
| 1368 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1369 | |
| 1370 | /* |
| 1371 | * 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] | 1372 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1373 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1374 | * 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] | 1375 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1376 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1377 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1378 | { |
| 1379 | #ifdef MBEDTLS_CHECK_PARAMS |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1380 | MBEDTLS_PARAM_FAILED( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1381 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1382 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1383 | } |
| 1384 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1385 | /* Multipart operations may still be using the key. This is safe |
| 1386 | * because all multipart operation objects are independent from |
| 1387 | * the key slot: if they need to access the key after the setup |
| 1388 | * phase, they have a copy of the key. Note that this means that |
| 1389 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1390 | /* At this point, key material and other type-specific content has |
| 1391 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1392 | * zeroize because the metadata is not particularly sensitive. */ |
| 1393 | memset( slot, 0, sizeof( *slot ) ); |
| 1394 | return( status ); |
| 1395 | } |
| 1396 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1397 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1398 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1399 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1400 | psa_status_t status; /* status of the last operation */ |
| 1401 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1402 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1403 | psa_se_drv_table_entry_t *driver; |
| 1404 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1405 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1406 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1407 | return( PSA_SUCCESS ); |
| 1408 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1409 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1410 | * 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] | 1411 | * key, this will load the key description from persistent memory if not |
| 1412 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1413 | * the key is operated by an SE or not and this information is needed by |
| 1414 | * the current implementation. |
| 1415 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1416 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1417 | if( status != PSA_SUCCESS ) |
| 1418 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1419 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1420 | /* |
| 1421 | * If the key slot containing the key description is under access by the |
| 1422 | * library (apart from the present access), the key cannot be destroyed |
| 1423 | * yet. For the time being, just return in error. Eventually (to be |
| 1424 | * implemented), the key should be destroyed when all accesses have |
| 1425 | * stopped. |
| 1426 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1427 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1428 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1429 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1430 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1431 | } |
| 1432 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1433 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1434 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1435 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1436 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1437 | /* For a key in a secure element, we need to do three things: |
| 1438 | * remove the key file in internal storage, destroy the |
| 1439 | * key inside the secure element, and update the driver's |
| 1440 | * persistent data. Start a transaction that will encompass these |
| 1441 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1442 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1443 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1444 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1445 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1446 | status = psa_crypto_save_transaction( ); |
| 1447 | if( status != PSA_SUCCESS ) |
| 1448 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1449 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1450 | /* We should still try to destroy the key in the secure |
| 1451 | * element and the key metadata in storage. This is especially |
| 1452 | * important if the error is that the storage is full. |
| 1453 | * But how to do it exactly without risking an inconsistent |
| 1454 | * state after a reset? |
| 1455 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1456 | */ |
| 1457 | overall_status = status; |
| 1458 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1459 | } |
| 1460 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1461 | status = psa_destroy_se_key( driver, slot->data.se.slot_number ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1462 | if( overall_status == PSA_SUCCESS ) |
| 1463 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1464 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1465 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1466 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1467 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1468 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1469 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1470 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1471 | if( overall_status == PSA_SUCCESS ) |
| 1472 | overall_status = status; |
| 1473 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1474 | /* TODO: other slots may have a copy of the same key. We should |
| 1475 | * invalidate them. |
| 1476 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1477 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1478 | } |
| 1479 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1480 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1481 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1482 | if( driver != NULL ) |
| 1483 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1484 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1485 | if( overall_status == PSA_SUCCESS ) |
| 1486 | overall_status = status; |
| 1487 | status = psa_crypto_stop_transaction( ); |
| 1488 | if( overall_status == PSA_SUCCESS ) |
| 1489 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1490 | } |
| 1491 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1492 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1493 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1494 | exit: |
| 1495 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1496 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1497 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
| 1498 | if( overall_status == PSA_SUCCESS ) |
| 1499 | overall_status = status; |
| 1500 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1501 | } |
| 1502 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1503 | void psa_reset_key_attributes( psa_key_attributes_t *attributes ) |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1504 | { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1505 | mbedtls_free( attributes->domain_parameters ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1506 | memset( attributes, 0, sizeof( *attributes ) ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1509 | psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes, |
| 1510 | psa_key_type_t type, |
| 1511 | const uint8_t *data, |
| 1512 | size_t data_length ) |
| 1513 | { |
| 1514 | uint8_t *copy = NULL; |
| 1515 | |
| 1516 | if( data_length != 0 ) |
| 1517 | { |
| 1518 | copy = mbedtls_calloc( 1, data_length ); |
| 1519 | if( copy == NULL ) |
| 1520 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 1521 | memcpy( copy, data, data_length ); |
| 1522 | } |
| 1523 | /* After this point, this function is guaranteed to succeed, so it |
| 1524 | * can start modifying `*attributes`. */ |
| 1525 | |
| 1526 | if( attributes->domain_parameters != NULL ) |
| 1527 | { |
| 1528 | mbedtls_free( attributes->domain_parameters ); |
| 1529 | attributes->domain_parameters = NULL; |
| 1530 | attributes->domain_parameters_size = 0; |
| 1531 | } |
| 1532 | |
| 1533 | attributes->domain_parameters = copy; |
| 1534 | attributes->domain_parameters_size = data_length; |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1535 | attributes->core.type = type; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1536 | return( PSA_SUCCESS ); |
| 1537 | } |
| 1538 | |
| 1539 | psa_status_t psa_get_key_domain_parameters( |
| 1540 | const psa_key_attributes_t *attributes, |
| 1541 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 1542 | { |
| 1543 | if( attributes->domain_parameters_size > data_size ) |
| 1544 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1545 | *data_length = attributes->domain_parameters_size; |
| 1546 | if( attributes->domain_parameters_size != 0 ) |
| 1547 | memcpy( data, attributes->domain_parameters, |
| 1548 | attributes->domain_parameters_size ); |
| 1549 | return( PSA_SUCCESS ); |
| 1550 | } |
| 1551 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1552 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1553 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1554 | static psa_status_t psa_get_rsa_public_exponent( |
| 1555 | const mbedtls_rsa_context *rsa, |
| 1556 | psa_key_attributes_t *attributes ) |
| 1557 | { |
| 1558 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1559 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1560 | uint8_t *buffer = NULL; |
| 1561 | size_t buflen; |
| 1562 | mbedtls_mpi_init( &mpi ); |
| 1563 | |
| 1564 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1565 | if( ret != 0 ) |
| 1566 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1567 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1568 | { |
| 1569 | /* It's the default value, which is reported as an empty string, |
| 1570 | * so there's nothing to do. */ |
| 1571 | goto exit; |
| 1572 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1573 | |
| 1574 | buflen = mbedtls_mpi_size( &mpi ); |
| 1575 | buffer = mbedtls_calloc( 1, buflen ); |
| 1576 | if( buffer == NULL ) |
| 1577 | { |
| 1578 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1579 | goto exit; |
| 1580 | } |
| 1581 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1582 | if( ret != 0 ) |
| 1583 | goto exit; |
| 1584 | attributes->domain_parameters = buffer; |
| 1585 | attributes->domain_parameters_size = buflen; |
| 1586 | |
| 1587 | exit: |
| 1588 | mbedtls_mpi_free( &mpi ); |
| 1589 | if( ret != 0 ) |
| 1590 | mbedtls_free( buffer ); |
| 1591 | return( mbedtls_to_psa_error( ret ) ); |
| 1592 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1593 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1594 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1595 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1596 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1597 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1598 | 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] | 1599 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1600 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1601 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1602 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1603 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1604 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1605 | psa_reset_key_attributes( attributes ); |
| 1606 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1607 | 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] | 1608 | if( status != PSA_SUCCESS ) |
| 1609 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1610 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1611 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1612 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1613 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1614 | |
| 1615 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1616 | if( psa_key_slot_is_external( slot ) ) |
| 1617 | psa_set_key_slot_number( attributes, slot->data.se.slot_number ); |
| 1618 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1619 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1620 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1621 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1622 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1623 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1624 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1625 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1626 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1627 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1628 | * is not yet implemented. |
| 1629 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1630 | */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1631 | if( psa_key_slot_is_external( slot ) ) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1632 | break; |
| 1633 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1634 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1635 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1636 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1637 | status = psa_load_rsa_representation( slot->attr.type, |
| 1638 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1639 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1640 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1641 | if( status != PSA_SUCCESS ) |
| 1642 | break; |
| 1643 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1644 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1645 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1646 | mbedtls_rsa_free( rsa ); |
| 1647 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1648 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1649 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1650 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1651 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1652 | default: |
| 1653 | /* Nothing else to do. */ |
| 1654 | break; |
| 1655 | } |
| 1656 | |
| 1657 | if( status != PSA_SUCCESS ) |
| 1658 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1659 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1660 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1661 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1662 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1665 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1666 | psa_status_t psa_get_key_slot_number( |
| 1667 | const psa_key_attributes_t *attributes, |
| 1668 | psa_key_slot_number_t *slot_number ) |
| 1669 | { |
| 1670 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1671 | { |
| 1672 | *slot_number = attributes->slot_number; |
| 1673 | return( PSA_SUCCESS ); |
| 1674 | } |
| 1675 | else |
| 1676 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1677 | } |
| 1678 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1679 | |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1680 | static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot, |
| 1681 | uint8_t *data, |
| 1682 | size_t data_size, |
| 1683 | size_t *data_length ) |
| 1684 | { |
| 1685 | if( slot->data.key.bytes > data_size ) |
| 1686 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1687 | memcpy( data, slot->data.key.data, slot->data.key.bytes ); |
| 1688 | memset( data + slot->data.key.bytes, 0, |
| 1689 | data_size - slot->data.key.bytes ); |
| 1690 | *data_length = slot->data.key.bytes; |
| 1691 | return( PSA_SUCCESS ); |
| 1692 | } |
| 1693 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1694 | static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot, |
| 1695 | uint8_t *data, |
| 1696 | size_t data_size, |
| 1697 | size_t *data_length, |
| 1698 | int export_public_key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1699 | { |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1700 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1701 | const psa_drv_se_t *drv; |
| 1702 | psa_drv_se_context_t *drv_context; |
| 1703 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1704 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1705 | *data_length = 0; |
| 1706 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1707 | if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1708 | return( PSA_ERROR_INVALID_ARGUMENT ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1709 | |
Gilles Peskine | f916894 | 2019-09-12 19:20:29 +0200 | [diff] [blame] | 1710 | /* Reject a zero-length output buffer now, since this can never be a |
| 1711 | * valid key representation. This way we know that data must be a valid |
| 1712 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1713 | if( data_size == 0 ) |
| 1714 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1715 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1716 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1717 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1718 | { |
| 1719 | psa_drv_se_export_key_t method; |
| 1720 | if( drv->key_management == NULL ) |
| 1721 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1722 | method = ( export_public_key ? |
| 1723 | drv->key_management->p_export_public : |
| 1724 | drv->key_management->p_export ); |
| 1725 | if( method == NULL ) |
| 1726 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 2e0f388 | 2019-07-25 11:34:33 +0200 | [diff] [blame] | 1727 | return( method( drv_context, |
| 1728 | slot->data.se.slot_number, |
| 1729 | data, data_size, data_length ) ); |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1730 | } |
| 1731 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1732 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1733 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1734 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 1735 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
Gilles Peskine | 188c71e | 2018-10-29 19:26:02 +0100 | [diff] [blame] | 1736 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1737 | else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) || |
| 1738 | PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1739 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1740 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1741 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1742 | /* Exporting public -> public */ |
| 1743 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1744 | } |
| 1745 | else if( !export_public_key ) |
| 1746 | { |
| 1747 | /* Exporting private -> private */ |
| 1748 | return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) ); |
| 1749 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1750 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1751 | /* Need to export the public part of a private key, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1752 | * so conversion is needed. Try the accelerators first. */ |
| 1753 | psa_status_t status = psa_driver_wrapper_export_public_key( slot, |
| 1754 | data, |
| 1755 | data_size, |
| 1756 | data_length ); |
| 1757 | |
| 1758 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 1759 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 1760 | return( status ); |
| 1761 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1762 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
| 1763 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1764 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1765 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1766 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1767 | status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1768 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1769 | slot->data.key.data, |
| 1770 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1771 | &rsa ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1772 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1773 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1774 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1775 | status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1776 | rsa, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1777 | data, |
| 1778 | data_size, |
| 1779 | data_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1780 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1781 | mbedtls_rsa_free( rsa ); |
| 1782 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1783 | |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1784 | return( status ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1785 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1786 | /* We don't know how to convert a private RSA key to public. */ |
| 1787 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1788 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1789 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1790 | } |
| 1791 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1792 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1793 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1794 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1795 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1796 | status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 1797 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1798 | slot->data.key.data, |
| 1799 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 1800 | &ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1801 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1802 | return( status ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1803 | |
| 1804 | status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY( |
| 1805 | PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 1806 | slot->attr.type ) ), |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1807 | ecp, |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1808 | data, |
| 1809 | data_size, |
| 1810 | data_length ); |
| 1811 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1812 | mbedtls_ecp_keypair_free( ecp ); |
| 1813 | mbedtls_free( ecp ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1814 | return( status ); |
| 1815 | #else |
| 1816 | /* 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] | 1817 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1818 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1819 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1820 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1821 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1822 | else |
| 1823 | { |
| 1824 | /* This shouldn't happen in the reference implementation, but |
| 1825 | it is valid for a special-purpose implementation to omit |
| 1826 | support for exporting certain key types. */ |
| 1827 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1828 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1829 | } |
| 1830 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1831 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1832 | uint8_t *data, |
| 1833 | size_t data_size, |
| 1834 | size_t *data_length ) |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1835 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1836 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1837 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1838 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1839 | |
| 1840 | /* Set the key to empty now, so that even when there are errors, we always |
| 1841 | * set data_length to a value between 0 and data_size. On error, setting |
| 1842 | * the key to empty is a good choice because an empty key representation is |
| 1843 | * unlikely to be accepted anywhere. */ |
| 1844 | *data_length = 0; |
| 1845 | |
| 1846 | /* Export requires the EXPORT flag. There is an exception for public keys, |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1847 | * which don't require any flag, but |
| 1848 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1849 | */ |
| 1850 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1851 | PSA_KEY_USAGE_EXPORT, 0 ); |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1852 | if( status != PSA_SUCCESS ) |
| 1853 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1854 | |
| 1855 | status = psa_internal_export_key( slot, data, data_size, data_length, 0 ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1856 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1857 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1858 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1859 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1860 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1861 | 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] | 1862 | uint8_t *data, |
| 1863 | size_t data_size, |
| 1864 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1865 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1866 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1867 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1868 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1869 | |
| 1870 | /* Set the key to empty now, so that even when there are errors, we always |
| 1871 | * set data_length to a value between 0 and data_size. On error, setting |
| 1872 | * the key to empty is a good choice because an empty key representation is |
| 1873 | * unlikely to be accepted anywhere. */ |
| 1874 | *data_length = 0; |
| 1875 | |
| 1876 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1877 | 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] | 1878 | if( status != PSA_SUCCESS ) |
| 1879 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1880 | |
| 1881 | status = psa_internal_export_key( slot, data, data_size, data_length, 1 ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1882 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1883 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1884 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1885 | } |
| 1886 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1887 | #if defined(static_assert) |
| 1888 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1889 | "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] | 1890 | 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] | 1891 | "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] | 1892 | 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] | 1893 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1894 | #endif |
| 1895 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1896 | /** Validate that a key policy is internally well-formed. |
| 1897 | * |
| 1898 | * This function only rejects invalid policies. It does not validate the |
| 1899 | * consistency of the policy with respect to other attributes of the key |
| 1900 | * such as the key type. |
| 1901 | */ |
| 1902 | 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] | 1903 | { |
| 1904 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1905 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1906 | PSA_KEY_USAGE_ENCRYPT | |
| 1907 | PSA_KEY_USAGE_DECRYPT | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1908 | PSA_KEY_USAGE_SIGN_HASH | |
| 1909 | PSA_KEY_USAGE_VERIFY_HASH | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1910 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1911 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1912 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1913 | return( PSA_SUCCESS ); |
| 1914 | } |
| 1915 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1916 | /** Validate the internal consistency of key attributes. |
| 1917 | * |
| 1918 | * This function only rejects invalid attribute values. If does not |
| 1919 | * validate the consistency of the attributes with any key data that may |
| 1920 | * be involved in the creation of the key. |
| 1921 | * |
| 1922 | * Call this function early in the key creation process. |
| 1923 | * |
| 1924 | * \param[in] attributes Key attributes for the new key. |
| 1925 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1926 | * NULL for a transparent key. |
| 1927 | * |
| 1928 | */ |
| 1929 | static psa_status_t psa_validate_key_attributes( |
| 1930 | const psa_key_attributes_t *attributes, |
| 1931 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1932 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1933 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1934 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1935 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1936 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1937 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1938 | if( status != PSA_SUCCESS ) |
| 1939 | return( status ); |
| 1940 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1941 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1942 | if( status != PSA_SUCCESS ) |
| 1943 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1944 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1945 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1946 | { |
| 1947 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1948 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1949 | } |
| 1950 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1951 | { |
Ronald Cron | cbd7bea | 2020-11-11 14:57:44 +0100 | [diff] [blame] | 1952 | status = psa_validate_key_id( psa_get_key_id( attributes ), 0 ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1953 | if( status != PSA_SUCCESS ) |
| 1954 | return( status ); |
| 1955 | } |
| 1956 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1957 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1958 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1959 | return( status ); |
| 1960 | |
| 1961 | /* Refuse to create overly large keys. |
| 1962 | * Note that this doesn't trigger on import if the attributes don't |
| 1963 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1964 | * psa_import_key() needs its own checks. */ |
| 1965 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1966 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1967 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1968 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1969 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1970 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1971 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1972 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1973 | return( PSA_SUCCESS ); |
| 1974 | } |
| 1975 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1976 | /** Prepare a key slot to receive key material. |
| 1977 | * |
| 1978 | * This function allocates a key slot and sets its metadata. |
| 1979 | * |
| 1980 | * If this function fails, call psa_fail_key_creation(). |
| 1981 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1982 | * This function is intended to be used as follows: |
| 1983 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1984 | * it with the specified attributes, and in case of a volatile key assign it |
| 1985 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1986 | * -# Populate the slot with the key material. |
| 1987 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1988 | * In case of failure at any step, stop the sequence and call |
| 1989 | * psa_fail_key_creation(). |
| 1990 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1991 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1992 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1993 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1994 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1995 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1996 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1997 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1998 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1999 | * |
| 2000 | * \retval #PSA_SUCCESS |
| 2001 | * The key slot is ready to receive key material. |
| 2002 | * \return If this function fails, the key slot is an invalid state. |
| 2003 | * 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] | 2004 | */ |
| 2005 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2006 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2007 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2008 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2009 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2010 | { |
| 2011 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 2012 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2013 | psa_key_slot_t *slot; |
| 2014 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2015 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2016 | *p_drv = NULL; |
| 2017 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2018 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 2019 | if( status != PSA_SUCCESS ) |
| 2020 | return( status ); |
| 2021 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2022 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2023 | if( status != PSA_SUCCESS ) |
| 2024 | return( status ); |
| 2025 | slot = *p_slot; |
| 2026 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2027 | /* We're storing the declared bit-size of the key. It's up to each |
| 2028 | * creation mechanism to verify that this information is correct. |
| 2029 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2030 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2031 | * is optional (import, copy). In case of a volatile key, assign it the |
| 2032 | * volatile key identifier associated to the slot returned to contain its |
| 2033 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2034 | |
| 2035 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2036 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 2037 | { |
| 2038 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 2039 | slot->attr.id = volatile_key_id; |
| 2040 | #else |
| 2041 | slot->attr.id.key_id = volatile_key_id; |
| 2042 | #endif |
| 2043 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 2044 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 2045 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 2046 | * external-only flags, query `attributes`. Thanks to the check |
| 2047 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 2048 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 2049 | * may have set. */ |
| 2050 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 2051 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2052 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2053 | /* 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] | 2054 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 2055 | * create the key file in internal storage, create the |
| 2056 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2057 | * persistent data. This is done by starting a transaction that will |
| 2058 | * encompass these three actions. |
| 2059 | * For registering a volatile key, we just need to find an appropriate |
| 2060 | * slot number inside the SE. Since the key is designated volatile, creating |
| 2061 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 2062 | /* The first thing to do is to find a slot number for the new key. |
| 2063 | * We save the slot number in persistent storage as part of the |
| 2064 | * transaction data. It will be needed to recover if the power |
| 2065 | * fails during the key creation process, to clean up on the secure |
| 2066 | * element side after restarting. Obtaining a slot number from the |
| 2067 | * secure element driver updates its persistent state, but we do not yet |
| 2068 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2069 | * 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] | 2070 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2071 | { |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 2072 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2073 | &slot->data.se.slot_number ); |
| 2074 | if( status != PSA_SUCCESS ) |
| 2075 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2076 | |
| 2077 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2078 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2079 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 2080 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 2081 | psa_crypto_transaction.key.slot = slot->data.se.slot_number; |
| 2082 | psa_crypto_transaction.key.id = slot->attr.id; |
| 2083 | status = psa_crypto_save_transaction( ); |
| 2084 | if( status != PSA_SUCCESS ) |
| 2085 | { |
| 2086 | (void) psa_crypto_stop_transaction( ); |
| 2087 | return( status ); |
| 2088 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 2089 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2090 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 2091 | |
| 2092 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 2093 | { |
| 2094 | /* Key registration only makes sense with a secure element. */ |
| 2095 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2096 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2097 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2098 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 2099 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2100 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2101 | |
| 2102 | /** Finalize the creation of a key once its key material has been set. |
| 2103 | * |
| 2104 | * This entails writing the key to persistent storage. |
| 2105 | * |
| 2106 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2107 | * See the documentation of psa_start_key_creation() for the intended use |
| 2108 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2109 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2110 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 2111 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 2112 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2113 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2114 | * \param[in,out] slot Pointer to the slot with key material. |
| 2115 | * \param[in] driver The secure element driver for the key, |
| 2116 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2117 | * \param[out] key On success, identifier of the key. Note that the |
| 2118 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2119 | * |
| 2120 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2121 | * The key was successfully created. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2122 | * \return If this function fails, the key slot is an invalid state. |
| 2123 | * 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] | 2124 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2125 | static psa_status_t psa_finish_key_creation( |
| 2126 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2127 | psa_se_drv_table_entry_t *driver, |
| 2128 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2129 | { |
| 2130 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 2131 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2132 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2133 | |
| 2134 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 2135 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2136 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2137 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2138 | if( driver != NULL ) |
| 2139 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2140 | psa_se_key_data_storage_t data; |
| 2141 | #if defined(static_assert) |
| 2142 | static_assert( sizeof( slot->data.se.slot_number ) == |
| 2143 | sizeof( data.slot_number ), |
| 2144 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2145 | #endif |
| 2146 | memcpy( &data.slot_number, &slot->data.se.slot_number, |
| 2147 | sizeof( slot->data.se.slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 2148 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2149 | (uint8_t*) &data, |
| 2150 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2151 | } |
| 2152 | else |
| 2153 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2154 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 2155 | /* Key material is saved in export representation in the slot, so |
| 2156 | * just pass the slot buffer for storage. */ |
| 2157 | status = psa_save_persistent_key( &slot->attr, |
| 2158 | slot->data.key.data, |
| 2159 | slot->data.key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 2160 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2161 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 2162 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 2163 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2164 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2165 | /* Finish the transaction for a key creation. This does not |
| 2166 | * happen when registering an existing key. Detect this case |
| 2167 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 2168 | * creation of a persistent key in a secure element requires a transaction, |
| 2169 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2170 | if( driver != NULL && |
| 2171 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2172 | { |
| 2173 | status = psa_save_se_persistent_data( driver ); |
| 2174 | if( status != PSA_SUCCESS ) |
| 2175 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2176 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2177 | return( status ); |
| 2178 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2179 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 2180 | } |
| 2181 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2182 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2183 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2184 | { |
| 2185 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2186 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2187 | if( status != PSA_SUCCESS ) |
| 2188 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2189 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 2190 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2191 | return( status ); |
| 2192 | } |
| 2193 | |
| 2194 | /** Abort the creation of a key. |
| 2195 | * |
| 2196 | * You may call this function after calling psa_start_key_creation(), |
| 2197 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 2198 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 2199 | * See the documentation of psa_start_key_creation() for the intended use |
| 2200 | * of this function. |
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 | * \param[in,out] slot Pointer to the slot with key material. |
| 2203 | * \param[in] driver The secure element driver for the key, |
| 2204 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2205 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2206 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2207 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2208 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2209 | (void) driver; |
| 2210 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2211 | if( slot == NULL ) |
| 2212 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2213 | |
| 2214 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 2215 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2216 | * element, and the failure happened later (when saving metadata |
| 2217 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 2218 | * element. |
| 2219 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 2220 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2221 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2222 | /* Abort the ongoing transaction if any (there may not be one if |
| 2223 | * the creation process failed before starting one, or if the |
| 2224 | * key creation is a registration of a key in a secure element). |
| 2225 | * Earlier functions must already have done what it takes to undo any |
| 2226 | * partial creation. All that's left is to update the transaction data |
| 2227 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 2228 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2229 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2230 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2231 | psa_wipe_key_slot( slot ); |
| 2232 | } |
| 2233 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2234 | /** Validate optional attributes during key creation. |
| 2235 | * |
| 2236 | * Some key attributes are optional during key creation. If they are |
| 2237 | * specified in the attributes structure, check that they are consistent |
| 2238 | * with the data in the slot. |
| 2239 | * |
| 2240 | * This function should be called near the end of key creation, after |
| 2241 | * the slot in memory is fully populated but before saving persistent data. |
| 2242 | */ |
| 2243 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2244 | const psa_key_slot_t *slot, |
| 2245 | const psa_key_attributes_t *attributes ) |
| 2246 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2247 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2248 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2249 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2250 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2251 | } |
| 2252 | |
| 2253 | if( attributes->domain_parameters_size != 0 ) |
| 2254 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2255 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 2256 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2257 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2258 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2259 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2260 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 2261 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2262 | |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2263 | psa_status_t status = psa_load_rsa_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2264 | slot->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2265 | slot->data.key.data, |
| 2266 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 2267 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2268 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 2269 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 2270 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2271 | mbedtls_mpi_init( &actual ); |
| 2272 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2273 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2274 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2275 | mbedtls_rsa_free( rsa ); |
| 2276 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2277 | if( ret != 0 ) |
| 2278 | goto rsa_exit; |
| 2279 | ret = mbedtls_mpi_read_binary( &required, |
| 2280 | attributes->domain_parameters, |
| 2281 | attributes->domain_parameters_size ); |
| 2282 | if( ret != 0 ) |
| 2283 | goto rsa_exit; |
| 2284 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 2285 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 2286 | rsa_exit: |
| 2287 | mbedtls_mpi_free( &actual ); |
| 2288 | mbedtls_mpi_free( &required ); |
| 2289 | if( ret != 0) |
| 2290 | return( mbedtls_to_psa_error( ret ) ); |
| 2291 | } |
| 2292 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2293 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 2294 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2295 | { |
| 2296 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2297 | } |
| 2298 | } |
| 2299 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2300 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2301 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2302 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2303 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2304 | } |
| 2305 | |
| 2306 | return( PSA_SUCCESS ); |
| 2307 | } |
| 2308 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2309 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2310 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 2311 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2312 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2313 | { |
| 2314 | psa_status_t status; |
| 2315 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2316 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2317 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2318 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2319 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 2320 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2321 | * This also rejects any key which might be encoded as an empty string, |
| 2322 | * which is never valid. */ |
| 2323 | if( data_length == 0 ) |
| 2324 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2325 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 2326 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2327 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2328 | if( status != PSA_SUCCESS ) |
| 2329 | goto exit; |
| 2330 | |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2331 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2332 | if( driver != NULL ) |
| 2333 | { |
| 2334 | const psa_drv_se_t *drv = psa_get_se_driver_methods( driver ); |
Darryl Green | 0892d0f | 2019-08-20 09:50:14 +0100 | [diff] [blame] | 2335 | /* The driver should set the number of key bits, however in |
| 2336 | * case it doesn't, we initialize bits to an invalid value. */ |
| 2337 | size_t bits = PSA_MAX_KEY_BITS + 1; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2338 | if( drv->key_management == NULL || |
| 2339 | drv->key_management->p_import == NULL ) |
| 2340 | { |
| 2341 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2342 | goto exit; |
| 2343 | } |
| 2344 | status = drv->key_management->p_import( |
| 2345 | psa_get_se_driver_context( driver ), |
Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 2346 | slot->data.se.slot_number, attributes, data, data_length, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 2347 | &bits ); |
| 2348 | if( status != PSA_SUCCESS ) |
| 2349 | goto exit; |
| 2350 | if( bits > PSA_MAX_KEY_BITS ) |
| 2351 | { |
| 2352 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2353 | goto exit; |
| 2354 | } |
| 2355 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2356 | } |
| 2357 | else |
| 2358 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2359 | { |
| 2360 | status = psa_import_key_into_slot( slot, data, data_length ); |
| 2361 | if( status != PSA_SUCCESS ) |
| 2362 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 2363 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2364 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2365 | if( status != PSA_SUCCESS ) |
| 2366 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2367 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2368 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2369 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2370 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2371 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2372 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2373 | return( status ); |
| 2374 | } |
| 2375 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2376 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2377 | psa_status_t mbedtls_psa_register_se_key( |
| 2378 | const psa_key_attributes_t *attributes ) |
| 2379 | { |
| 2380 | psa_status_t status; |
| 2381 | psa_key_slot_t *slot = NULL; |
| 2382 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2383 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2384 | |
| 2385 | /* Leaving attributes unspecified is not currently supported. |
| 2386 | * It could make sense to query the key type and size from the |
| 2387 | * secure element, but not all secure elements support this |
| 2388 | * and the driver HAL doesn't currently support it. */ |
| 2389 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 2390 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2391 | if( psa_get_key_bits( attributes ) == 0 ) |
| 2392 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2393 | |
| 2394 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2395 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2396 | if( status != PSA_SUCCESS ) |
| 2397 | goto exit; |
| 2398 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2399 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2400 | |
| 2401 | exit: |
| 2402 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2403 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2404 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2405 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2406 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2407 | return( status ); |
| 2408 | } |
| 2409 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2410 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2411 | 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] | 2412 | psa_key_slot_t *target ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2413 | { |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 2414 | psa_status_t status = psa_copy_key_material_into_slot( target, |
| 2415 | source->data.key.data, |
| 2416 | source->data.key.bytes ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2417 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2418 | return( status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2419 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2420 | target->attr.type = source->attr.type; |
| 2421 | target->attr.bits = source->attr.bits; |
| 2422 | |
| 2423 | return( PSA_SUCCESS ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2424 | } |
| 2425 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2426 | 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] | 2427 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2428 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2429 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2430 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2431 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2432 | psa_key_slot_t *source_slot = NULL; |
| 2433 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2434 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2435 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2436 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2437 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2438 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2439 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 2440 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2441 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2442 | goto exit; |
| 2443 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2444 | status = psa_validate_optional_attributes( source_slot, |
| 2445 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2446 | if( status != PSA_SUCCESS ) |
| 2447 | goto exit; |
| 2448 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 2449 | status = psa_restrict_key_policy( &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2450 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2451 | if( status != PSA_SUCCESS ) |
| 2452 | goto exit; |
| 2453 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2454 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2455 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2456 | if( status != PSA_SUCCESS ) |
| 2457 | goto exit; |
| 2458 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2459 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2460 | if( driver != NULL ) |
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 | /* Copying to a secure element is not implemented yet. */ |
| 2463 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2464 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2465 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2466 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2467 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2468 | status = psa_copy_key_material( source_slot, target_slot ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2469 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2470 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2471 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2472 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2473 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2474 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2475 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2476 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2477 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2478 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2479 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2480 | } |
| 2481 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2482 | |
| 2483 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2484 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2485 | /* Message digests */ |
| 2486 | /****************************************************************/ |
| 2487 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2488 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 2489 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 2490 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2491 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2492 | 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] | 2493 | { |
| 2494 | switch( alg ) |
| 2495 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2496 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2497 | case PSA_ALG_MD2: |
| 2498 | return( &mbedtls_md2_info ); |
| 2499 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2500 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2501 | case PSA_ALG_MD4: |
| 2502 | return( &mbedtls_md4_info ); |
| 2503 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2504 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2505 | case PSA_ALG_MD5: |
| 2506 | return( &mbedtls_md5_info ); |
| 2507 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2508 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2509 | case PSA_ALG_RIPEMD160: |
| 2510 | return( &mbedtls_ripemd160_info ); |
| 2511 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2512 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2513 | case PSA_ALG_SHA_1: |
| 2514 | return( &mbedtls_sha1_info ); |
| 2515 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2516 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2517 | case PSA_ALG_SHA_224: |
| 2518 | return( &mbedtls_sha224_info ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2519 | #endif |
| 2520 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2521 | case PSA_ALG_SHA_256: |
| 2522 | return( &mbedtls_sha256_info ); |
| 2523 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2524 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2525 | case PSA_ALG_SHA_384: |
| 2526 | return( &mbedtls_sha384_info ); |
Manuel Pégourié-Gonnard | d602084 | 2019-07-17 16:28:21 +0200 | [diff] [blame] | 2527 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2528 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2529 | case PSA_ALG_SHA_512: |
| 2530 | return( &mbedtls_sha512_info ); |
| 2531 | #endif |
| 2532 | default: |
| 2533 | return( NULL ); |
| 2534 | } |
| 2535 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 2536 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 2537 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 2538 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 2539 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2540 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2541 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2542 | { |
| 2543 | switch( operation->alg ) |
| 2544 | { |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 2545 | case 0: |
| 2546 | /* The object has (apparently) been initialized but it is not |
| 2547 | * in use. It's ok to call abort on such an object, and there's |
| 2548 | * nothing to do. */ |
| 2549 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2550 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2551 | case PSA_ALG_MD2: |
| 2552 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 2553 | break; |
| 2554 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2555 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2556 | case PSA_ALG_MD4: |
| 2557 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 2558 | break; |
| 2559 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2560 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2561 | case PSA_ALG_MD5: |
| 2562 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 2563 | break; |
| 2564 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2565 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2566 | case PSA_ALG_RIPEMD160: |
| 2567 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 2568 | break; |
| 2569 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2570 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2571 | case PSA_ALG_SHA_1: |
| 2572 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 2573 | break; |
| 2574 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2575 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2576 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2577 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2578 | break; |
| 2579 | #endif |
| 2580 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2581 | case PSA_ALG_SHA_256: |
| 2582 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 2583 | break; |
| 2584 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2585 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2586 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2587 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2588 | break; |
| 2589 | #endif |
| 2590 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2591 | case PSA_ALG_SHA_512: |
| 2592 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 2593 | break; |
| 2594 | #endif |
| 2595 | default: |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 2596 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2597 | } |
| 2598 | operation->alg = 0; |
| 2599 | return( PSA_SUCCESS ); |
| 2600 | } |
| 2601 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2602 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2603 | psa_algorithm_t alg ) |
| 2604 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2605 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2606 | |
| 2607 | /* A context must be freshly initialized before it can be set up. */ |
| 2608 | if( operation->alg != 0 ) |
| 2609 | { |
| 2610 | return( PSA_ERROR_BAD_STATE ); |
| 2611 | } |
| 2612 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2613 | switch( alg ) |
| 2614 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2615 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2616 | case PSA_ALG_MD2: |
| 2617 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 2618 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 2619 | break; |
| 2620 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2621 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2622 | case PSA_ALG_MD4: |
| 2623 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 2624 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 2625 | break; |
| 2626 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2627 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2628 | case PSA_ALG_MD5: |
| 2629 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 2630 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 2631 | break; |
| 2632 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2633 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2634 | case PSA_ALG_RIPEMD160: |
| 2635 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 2636 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 2637 | break; |
| 2638 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2639 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2640 | case PSA_ALG_SHA_1: |
| 2641 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 2642 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 2643 | break; |
| 2644 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2645 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2646 | case PSA_ALG_SHA_224: |
| 2647 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2648 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 2649 | break; |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2650 | #endif |
| 2651 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2652 | case PSA_ALG_SHA_256: |
| 2653 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 2654 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 2655 | break; |
| 2656 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2657 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2658 | case PSA_ALG_SHA_384: |
| 2659 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2660 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 2661 | break; |
Manuel Pégourié-Gonnard | 792b16d | 2020-01-07 10:13:18 +0100 | [diff] [blame] | 2662 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2663 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2664 | case PSA_ALG_SHA_512: |
| 2665 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 2666 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 2667 | break; |
| 2668 | #endif |
| 2669 | default: |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 2670 | return( PSA_ALG_IS_HASH( alg ) ? |
| 2671 | PSA_ERROR_NOT_SUPPORTED : |
| 2672 | PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2673 | } |
| 2674 | if( ret == 0 ) |
| 2675 | operation->alg = alg; |
| 2676 | else |
| 2677 | psa_hash_abort( operation ); |
| 2678 | return( mbedtls_to_psa_error( ret ) ); |
| 2679 | } |
| 2680 | |
| 2681 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2682 | const uint8_t *input, |
| 2683 | size_t input_length ) |
| 2684 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2685 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2686 | |
| 2687 | /* Don't require hash implementations to behave correctly on a |
| 2688 | * zero-length input, which may have an invalid pointer. */ |
| 2689 | if( input_length == 0 ) |
| 2690 | return( PSA_SUCCESS ); |
| 2691 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2692 | switch( operation->alg ) |
| 2693 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2694 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2695 | case PSA_ALG_MD2: |
| 2696 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 2697 | input, input_length ); |
| 2698 | break; |
| 2699 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2700 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2701 | case PSA_ALG_MD4: |
| 2702 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 2703 | input, input_length ); |
| 2704 | break; |
| 2705 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2706 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2707 | case PSA_ALG_MD5: |
| 2708 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 2709 | input, input_length ); |
| 2710 | break; |
| 2711 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2712 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2713 | case PSA_ALG_RIPEMD160: |
| 2714 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 2715 | input, input_length ); |
| 2716 | break; |
| 2717 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2718 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2719 | case PSA_ALG_SHA_1: |
| 2720 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 2721 | input, input_length ); |
| 2722 | break; |
| 2723 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2724 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2725 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2726 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2727 | input, input_length ); |
| 2728 | break; |
| 2729 | #endif |
| 2730 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2731 | case PSA_ALG_SHA_256: |
| 2732 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 2733 | input, input_length ); |
| 2734 | break; |
| 2735 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2736 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2737 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2738 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2739 | input, input_length ); |
| 2740 | break; |
| 2741 | #endif |
| 2742 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2743 | case PSA_ALG_SHA_512: |
| 2744 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 2745 | input, input_length ); |
| 2746 | break; |
| 2747 | #endif |
| 2748 | default: |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2749 | (void)input; |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2750 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2751 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2752 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2753 | if( ret != 0 ) |
| 2754 | psa_hash_abort( operation ); |
| 2755 | return( mbedtls_to_psa_error( ret ) ); |
| 2756 | } |
| 2757 | |
| 2758 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2759 | uint8_t *hash, |
| 2760 | size_t hash_size, |
| 2761 | size_t *hash_length ) |
| 2762 | { |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2763 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2764 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 2765 | size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2766 | |
| 2767 | /* Fill the output buffer with something that isn't a valid hash |
| 2768 | * (barring an attack on the hash and deliberately-crafted input), |
| 2769 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2770 | *hash_length = hash_size; |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 2771 | /* If hash_size is 0 then hash may be NULL and then the |
| 2772 | * call to memset would have undefined behavior. */ |
| 2773 | if( hash_size != 0 ) |
| 2774 | memset( hash, '!', hash_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2775 | |
| 2776 | if( hash_size < actual_hash_length ) |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2777 | { |
| 2778 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2779 | goto exit; |
| 2780 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2781 | |
| 2782 | switch( operation->alg ) |
| 2783 | { |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2784 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2785 | case PSA_ALG_MD2: |
| 2786 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 2787 | break; |
| 2788 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2789 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2790 | case PSA_ALG_MD4: |
| 2791 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 2792 | break; |
| 2793 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2794 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2795 | case PSA_ALG_MD5: |
| 2796 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 2797 | break; |
| 2798 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2799 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2800 | case PSA_ALG_RIPEMD160: |
| 2801 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 2802 | break; |
| 2803 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2804 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2805 | case PSA_ALG_SHA_1: |
| 2806 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 2807 | break; |
| 2808 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2809 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2810 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2811 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2812 | break; |
| 2813 | #endif |
| 2814 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2815 | case PSA_ALG_SHA_256: |
| 2816 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 2817 | break; |
| 2818 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2819 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2820 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2821 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2822 | break; |
| 2823 | #endif |
| 2824 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2825 | case PSA_ALG_SHA_512: |
| 2826 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 2827 | break; |
| 2828 | #endif |
| 2829 | default: |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2830 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2831 | } |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2832 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2833 | |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2834 | exit: |
| 2835 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2836 | { |
Gilles Peskine | aee1333 | 2018-07-02 12:15:28 +0200 | [diff] [blame] | 2837 | *hash_length = actual_hash_length; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2838 | return( psa_hash_abort( operation ) ); |
| 2839 | } |
| 2840 | else |
| 2841 | { |
| 2842 | psa_hash_abort( operation ); |
itayzafrir | 40835d4 | 2018-08-02 13:14:17 +0300 | [diff] [blame] | 2843 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2847 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2848 | const uint8_t *hash, |
| 2849 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2850 | { |
| 2851 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2852 | size_t actual_hash_length; |
| 2853 | psa_status_t status = psa_hash_finish( operation, |
| 2854 | actual_hash, sizeof( actual_hash ), |
| 2855 | &actual_hash_length ); |
| 2856 | if( status != PSA_SUCCESS ) |
| 2857 | return( status ); |
| 2858 | if( actual_hash_length != hash_length ) |
| 2859 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2860 | if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
| 2861 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 2862 | return( PSA_SUCCESS ); |
| 2863 | } |
| 2864 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2865 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2866 | const uint8_t *input, size_t input_length, |
| 2867 | uint8_t *hash, size_t hash_size, |
| 2868 | size_t *hash_length ) |
| 2869 | { |
| 2870 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2871 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2872 | |
| 2873 | *hash_length = hash_size; |
| 2874 | status = psa_hash_setup( &operation, alg ); |
| 2875 | if( status != PSA_SUCCESS ) |
| 2876 | goto exit; |
| 2877 | status = psa_hash_update( &operation, input, input_length ); |
| 2878 | if( status != PSA_SUCCESS ) |
| 2879 | goto exit; |
| 2880 | status = psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 2881 | if( status != PSA_SUCCESS ) |
| 2882 | goto exit; |
| 2883 | |
| 2884 | exit: |
| 2885 | if( status == PSA_SUCCESS ) |
| 2886 | status = psa_hash_abort( &operation ); |
| 2887 | else |
| 2888 | psa_hash_abort( &operation ); |
| 2889 | return( status ); |
| 2890 | } |
| 2891 | |
| 2892 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2893 | const uint8_t *input, size_t input_length, |
| 2894 | const uint8_t *hash, size_t hash_length ) |
| 2895 | { |
| 2896 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 2897 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2898 | |
| 2899 | status = psa_hash_setup( &operation, alg ); |
| 2900 | if( status != PSA_SUCCESS ) |
| 2901 | goto exit; |
| 2902 | status = psa_hash_update( &operation, input, input_length ); |
| 2903 | if( status != PSA_SUCCESS ) |
| 2904 | goto exit; |
| 2905 | status = psa_hash_verify( &operation, hash, hash_length ); |
| 2906 | if( status != PSA_SUCCESS ) |
| 2907 | goto exit; |
| 2908 | |
| 2909 | exit: |
| 2910 | if( status == PSA_SUCCESS ) |
| 2911 | status = psa_hash_abort( &operation ); |
| 2912 | else |
| 2913 | psa_hash_abort( &operation ); |
| 2914 | return( status ); |
| 2915 | } |
| 2916 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2917 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2918 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2919 | { |
| 2920 | if( target_operation->alg != 0 ) |
| 2921 | return( PSA_ERROR_BAD_STATE ); |
| 2922 | |
| 2923 | switch( source_operation->alg ) |
| 2924 | { |
| 2925 | case 0: |
| 2926 | return( PSA_ERROR_BAD_STATE ); |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2927 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2928 | case PSA_ALG_MD2: |
| 2929 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 2930 | &source_operation->ctx.md2 ); |
| 2931 | break; |
| 2932 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2933 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2934 | case PSA_ALG_MD4: |
| 2935 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 2936 | &source_operation->ctx.md4 ); |
| 2937 | break; |
| 2938 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2939 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2940 | case PSA_ALG_MD5: |
| 2941 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 2942 | &source_operation->ctx.md5 ); |
| 2943 | break; |
| 2944 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2945 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2946 | case PSA_ALG_RIPEMD160: |
| 2947 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 2948 | &source_operation->ctx.ripemd160 ); |
| 2949 | break; |
| 2950 | #endif |
John Durkop | ee4e660 | 2020-11-27 08:48:46 -0800 | [diff] [blame] | 2951 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2952 | case PSA_ALG_SHA_1: |
| 2953 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 2954 | &source_operation->ctx.sha1 ); |
| 2955 | break; |
| 2956 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2957 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2958 | case PSA_ALG_SHA_224: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2959 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2960 | &source_operation->ctx.sha256 ); |
| 2961 | break; |
| 2962 | #endif |
| 2963 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2964 | case PSA_ALG_SHA_256: |
| 2965 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 2966 | &source_operation->ctx.sha256 ); |
| 2967 | break; |
| 2968 | #endif |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2969 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2970 | case PSA_ALG_SHA_384: |
John Durkop | 6ca2327 | 2020-12-03 06:01:32 -0800 | [diff] [blame] | 2971 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2972 | &source_operation->ctx.sha512 ); |
| 2973 | break; |
| 2974 | #endif |
| 2975 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2976 | case PSA_ALG_SHA_512: |
| 2977 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 2978 | &source_operation->ctx.sha512 ); |
| 2979 | break; |
| 2980 | #endif |
| 2981 | default: |
| 2982 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2983 | } |
| 2984 | |
| 2985 | target_operation->alg = source_operation->alg; |
| 2986 | return( PSA_SUCCESS ); |
| 2987 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2988 | |
| 2989 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2990 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2991 | /* MAC */ |
| 2992 | /****************************************************************/ |
| 2993 | |
Gilles Peskine | dc2fc84 | 2018-03-07 16:42:59 +0100 | [diff] [blame] | 2994 | static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2995 | psa_algorithm_t alg, |
| 2996 | psa_key_type_t key_type, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2997 | size_t key_bits, |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 2998 | mbedtls_cipher_id_t* cipher_id ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2999 | { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3000 | mbedtls_cipher_mode_t mode; |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3001 | mbedtls_cipher_id_t cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3002 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3003 | if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3004 | alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 3005 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3006 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 3007 | { |
Nir Sonnenschein | e9664c3 | 2018-06-17 14:41:30 +0300 | [diff] [blame] | 3008 | switch( alg ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3009 | { |
Bence Szépkúti | 1de907d | 2020-12-07 18:20:28 +0100 | [diff] [blame] | 3010 | case PSA_ALG_STREAM_CIPHER: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3011 | mode = MBEDTLS_MODE_STREAM; |
| 3012 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3013 | case PSA_ALG_CTR: |
| 3014 | mode = MBEDTLS_MODE_CTR; |
| 3015 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3016 | case PSA_ALG_CFB: |
| 3017 | mode = MBEDTLS_MODE_CFB; |
| 3018 | break; |
| 3019 | case PSA_ALG_OFB: |
| 3020 | mode = MBEDTLS_MODE_OFB; |
| 3021 | break; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3022 | case PSA_ALG_ECB_NO_PADDING: |
| 3023 | mode = MBEDTLS_MODE_ECB; |
| 3024 | break; |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 3025 | case PSA_ALG_CBC_NO_PADDING: |
| 3026 | mode = MBEDTLS_MODE_CBC; |
| 3027 | break; |
| 3028 | case PSA_ALG_CBC_PKCS7: |
| 3029 | mode = MBEDTLS_MODE_CBC; |
| 3030 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3031 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3032 | mode = MBEDTLS_MODE_CCM; |
| 3033 | break; |
Gilles Peskine | 57fbdb1 | 2018-10-17 18:29:17 +0200 | [diff] [blame] | 3034 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3035 | mode = MBEDTLS_MODE_GCM; |
| 3036 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3037 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3038 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 3039 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3040 | default: |
| 3041 | return( NULL ); |
| 3042 | } |
| 3043 | } |
| 3044 | else if( alg == PSA_ALG_CMAC ) |
| 3045 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3046 | else |
| 3047 | return( NULL ); |
| 3048 | |
| 3049 | switch( key_type ) |
| 3050 | { |
| 3051 | case PSA_KEY_TYPE_AES: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3052 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3053 | break; |
| 3054 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3055 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 3056 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3057 | if( key_bits == 64 ) |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3058 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3059 | else |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3060 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 3061 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 3062 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 3063 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 3064 | if( key_bits == 128 ) |
| 3065 | key_bits = 192; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3066 | break; |
| 3067 | case PSA_KEY_TYPE_CAMELLIA: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3068 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3069 | break; |
| 3070 | case PSA_KEY_TYPE_ARC4: |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3071 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3072 | break; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 3073 | case PSA_KEY_TYPE_CHACHA20: |
| 3074 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 3075 | break; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3076 | default: |
| 3077 | return( NULL ); |
| 3078 | } |
mohammad1603 | f4f0d61 | 2018-06-03 15:04:51 +0300 | [diff] [blame] | 3079 | if( cipher_id != NULL ) |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3080 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3081 | |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 3082 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 3083 | (int) key_bits, mode ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3084 | } |
| 3085 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3086 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3087 | static size_t psa_get_hash_block_size( psa_algorithm_t alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3088 | { |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3089 | switch( alg ) |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3090 | { |
| 3091 | case PSA_ALG_MD2: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3092 | return( 16 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3093 | case PSA_ALG_MD4: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3094 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3095 | case PSA_ALG_MD5: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3096 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3097 | case PSA_ALG_RIPEMD160: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3098 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3099 | case PSA_ALG_SHA_1: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3100 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3101 | case PSA_ALG_SHA_224: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3102 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3103 | case PSA_ALG_SHA_256: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3104 | return( 64 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3105 | case PSA_ALG_SHA_384: |
Nir Sonnenschein | aa5aea0 | 2018-06-18 12:24:33 +0300 | [diff] [blame] | 3106 | return( 128 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3107 | case PSA_ALG_SHA_512: |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3108 | return( 128 ); |
| 3109 | default: |
| 3110 | return( 0 ); |
Nir Sonnenschein | 9627241 | 2018-06-17 14:41:10 +0300 | [diff] [blame] | 3111 | } |
| 3112 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3113 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */ |
Nir Sonnenschein | 0c9ec53 | 2018-06-07 13:27:47 +0300 | [diff] [blame] | 3114 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3115 | /* Initialize the MAC operation structure. Once this function has been |
| 3116 | * called, psa_mac_abort can run and will do the right thing. */ |
| 3117 | static psa_status_t psa_mac_init( psa_mac_operation_t *operation, |
| 3118 | psa_algorithm_t alg ) |
| 3119 | { |
| 3120 | psa_status_t status = PSA_ERROR_NOT_SUPPORTED; |
| 3121 | |
| 3122 | operation->alg = alg; |
| 3123 | operation->key_set = 0; |
| 3124 | operation->iv_set = 0; |
| 3125 | operation->iv_required = 0; |
| 3126 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3127 | operation->is_sign = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3128 | |
| 3129 | #if defined(MBEDTLS_CMAC_C) |
| 3130 | if( alg == PSA_ALG_CMAC ) |
| 3131 | { |
| 3132 | operation->iv_required = 0; |
| 3133 | mbedtls_cipher_init( &operation->ctx.cmac ); |
| 3134 | status = PSA_SUCCESS; |
| 3135 | } |
| 3136 | else |
| 3137 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3138 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3139 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3140 | { |
Gilles Peskine | ff94abd | 2018-07-12 17:07:52 +0200 | [diff] [blame] | 3141 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 3142 | operation->ctx.hmac.hash_ctx.alg = 0; |
| 3143 | status = PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3144 | } |
| 3145 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3146 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3147 | { |
Gilles Peskine | c06e071 | 2018-06-20 16:21:04 +0200 | [diff] [blame] | 3148 | if( ! PSA_ALG_IS_MAC( alg ) ) |
| 3149 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | if( status != PSA_SUCCESS ) |
| 3153 | memset( operation, 0, sizeof( *operation ) ); |
| 3154 | return( status ); |
| 3155 | } |
| 3156 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3157 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3158 | static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac ) |
| 3159 | { |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3160 | mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3161 | return( psa_hash_abort( &hmac->hash_ctx ) ); |
| 3162 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3163 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3164 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3165 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
| 3166 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3167 | if( operation->alg == 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3168 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3169 | /* The object has (apparently) been initialized but it is not |
| 3170 | * in use. It's ok to call abort on such an object, and there's |
| 3171 | * nothing to do. */ |
| 3172 | return( PSA_SUCCESS ); |
| 3173 | } |
| 3174 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3175 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3176 | if( operation->alg == PSA_ALG_CMAC ) |
| 3177 | { |
| 3178 | mbedtls_cipher_free( &operation->ctx.cmac ); |
| 3179 | } |
| 3180 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3181 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3182 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3183 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3184 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3185 | psa_hmac_abort_internal( &operation->ctx.hmac ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3186 | } |
| 3187 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3188 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3189 | { |
| 3190 | /* Sanity check (shouldn't happen: operation->alg should |
| 3191 | * always have been initialized to a valid value). */ |
| 3192 | goto bad_state; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3193 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3194 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3195 | operation->alg = 0; |
| 3196 | operation->key_set = 0; |
| 3197 | operation->iv_set = 0; |
| 3198 | operation->iv_required = 0; |
| 3199 | operation->has_input = 0; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3200 | operation->is_sign = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3201 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3202 | return( PSA_SUCCESS ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3203 | |
| 3204 | bad_state: |
| 3205 | /* If abort is called on an uninitialized object, we can't trust |
| 3206 | * anything. Wipe the object in case it contains confidential data. |
| 3207 | * This may result in a memory leak if a pointer gets overwritten, |
| 3208 | * but it's too late to do anything about this. */ |
| 3209 | memset( operation, 0, sizeof( *operation ) ); |
| 3210 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3211 | } |
| 3212 | |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3213 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3214 | static int psa_cmac_setup( psa_mac_operation_t *operation, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3215 | size_t key_bits, |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3216 | psa_key_slot_t *slot, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3217 | const mbedtls_cipher_info_t *cipher_info ) |
| 3218 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3219 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3220 | |
| 3221 | operation->mac_size = cipher_info->block_size; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3222 | |
| 3223 | ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info ); |
| 3224 | if( ret != 0 ) |
| 3225 | return( ret ); |
| 3226 | |
| 3227 | ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3228 | slot->data.key.data, |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3229 | key_bits ); |
| 3230 | return( ret ); |
| 3231 | } |
Gilles Peskine | e3b07d8 | 2018-06-19 11:57:35 +0200 | [diff] [blame] | 3232 | #endif /* MBEDTLS_CMAC_C */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3233 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3234 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3235 | static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac, |
| 3236 | const uint8_t *key, |
| 3237 | size_t key_length, |
| 3238 | psa_algorithm_t hash_alg ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3239 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3240 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3241 | size_t i; |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3242 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3243 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3244 | psa_status_t status; |
| 3245 | |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3246 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 3247 | * overflow below. This should never trigger if the hash algorithm |
| 3248 | * is implemented correctly. */ |
| 3249 | /* The size checks against the ipad and opad buffers cannot be written |
| 3250 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 3251 | * because that triggers -Wlogical-op on GCC 7.3. */ |
| 3252 | if( block_size > sizeof( ipad ) ) |
| 3253 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3254 | if( block_size > sizeof( hmac->opad ) ) |
| 3255 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3256 | if( block_size < hash_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3257 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3258 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3259 | if( key_length > block_size ) |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3260 | { |
Gilles Peskine | 84b8fc8 | 2019-11-28 20:07:20 +0100 | [diff] [blame] | 3261 | status = psa_hash_compute( hash_alg, key, key_length, |
| 3262 | ipad, sizeof( ipad ), &key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3263 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b8be288 | 2018-07-17 16:24:34 +0200 | [diff] [blame] | 3264 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3265 | } |
Gilles Peskine | 9688997 | 2018-07-12 17:07:03 +0200 | [diff] [blame] | 3266 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 3267 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 3268 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 3269 | * an invalid pointer which would make the behavior undefined. */ |
| 3270 | else if( key_length != 0 ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3271 | memcpy( ipad, key, key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3272 | |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3273 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 3274 | * to create the ipad value. */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3275 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | d223b52 | 2018-06-11 18:12:58 +0200 | [diff] [blame] | 3276 | ipad[i] ^= 0x36; |
| 3277 | memset( ipad + key_length, 0x36, block_size - key_length ); |
| 3278 | |
| 3279 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 3280 | * and filling the rest of opad with the requisite constant. */ |
| 3281 | for( i = 0; i < key_length; i++ ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3282 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
| 3283 | memset( hmac->opad + key_length, 0x5C, block_size - key_length ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3284 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3285 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3286 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3287 | goto cleanup; |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3288 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3289 | status = psa_hash_update( &hmac->hash_ctx, ipad, block_size ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3290 | |
| 3291 | cleanup: |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 3292 | mbedtls_platform_zeroize( ipad, sizeof( ipad ) ); |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3293 | |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3294 | return( status ); |
| 3295 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3296 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3297 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3298 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3299 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3300 | psa_algorithm_t alg, |
| 3301 | int is_sign ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3302 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3303 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3304 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3305 | psa_key_slot_t *slot; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3306 | size_t key_bits; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3307 | psa_key_usage_t usage = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3308 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH; |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3309 | uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 3310 | psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3311 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3312 | /* A context must be freshly initialized before it can be set up. */ |
| 3313 | if( operation->alg != 0 ) |
| 3314 | { |
| 3315 | return( PSA_ERROR_BAD_STATE ); |
| 3316 | } |
| 3317 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3318 | status = psa_mac_init( operation, full_length_alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3319 | if( status != PSA_SUCCESS ) |
| 3320 | return( status ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3321 | if( is_sign ) |
| 3322 | operation->is_sign = 1; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3323 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3324 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3325 | key, &slot, usage, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3326 | if( status != PSA_SUCCESS ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3327 | goto exit; |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 3328 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 3329 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3330 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3331 | if( full_length_alg == PSA_ALG_CMAC ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3332 | { |
| 3333 | const mbedtls_cipher_info_t *cipher_info = |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3334 | mbedtls_cipher_info_from_psa( full_length_alg, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3335 | slot->attr.type, key_bits, NULL ); |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3336 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3337 | if( cipher_info == NULL ) |
| 3338 | { |
| 3339 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3340 | goto exit; |
| 3341 | } |
| 3342 | operation->mac_size = cipher_info->block_size; |
| 3343 | ret = psa_cmac_setup( operation, key_bits, slot, cipher_info ); |
| 3344 | status = mbedtls_to_psa_error( ret ); |
| 3345 | } |
| 3346 | else |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3347 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3348 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3349 | if( PSA_ALG_IS_HMAC( full_length_alg ) ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3350 | { |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 3351 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3352 | if( hash_alg == 0 ) |
| 3353 | { |
| 3354 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3355 | goto exit; |
| 3356 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3357 | |
| 3358 | operation->mac_size = PSA_HASH_SIZE( hash_alg ); |
| 3359 | /* Sanity check. This shouldn't fail on a valid configuration. */ |
| 3360 | if( operation->mac_size == 0 || |
| 3361 | operation->mac_size > sizeof( operation->ctx.hmac.opad ) ) |
| 3362 | { |
| 3363 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3364 | goto exit; |
| 3365 | } |
| 3366 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3367 | if( slot->attr.type != PSA_KEY_TYPE_HMAC ) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3368 | { |
| 3369 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3370 | goto exit; |
| 3371 | } |
Gilles Peskine | 9aa369e | 2018-07-16 00:36:29 +0200 | [diff] [blame] | 3372 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3373 | status = psa_hmac_setup_internal( &operation->ctx.hmac, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 3374 | slot->data.key.data, |
| 3375 | slot->data.key.bytes, |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3376 | hash_alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3377 | } |
| 3378 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3379 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3380 | { |
Jaeden Amero | 82df32e | 2018-11-23 15:11:20 +0000 | [diff] [blame] | 3381 | (void) key_bits; |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3382 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3383 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3384 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3385 | if( truncated == 0 ) |
| 3386 | { |
| 3387 | /* The "normal" case: untruncated algorithm. Nothing to do. */ |
| 3388 | } |
| 3389 | else if( truncated < 4 ) |
| 3390 | { |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 3391 | /* A very short MAC is too short for security since it can be |
| 3392 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 3393 | * so we make this our minimum, even though 32 bits is still |
| 3394 | * too small for security. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3395 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3396 | } |
| 3397 | else if( truncated > operation->mac_size ) |
| 3398 | { |
| 3399 | /* It's impossible to "truncate" to a larger length. */ |
| 3400 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3401 | } |
| 3402 | else |
| 3403 | operation->mac_size = truncated; |
| 3404 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3405 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3406 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3407 | { |
Gilles Peskine | 6a0a44e | 2018-06-11 17:42:48 +0200 | [diff] [blame] | 3408 | psa_mac_abort( operation ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3409 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3410 | else |
| 3411 | { |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 3412 | operation->key_set = 1; |
| 3413 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3414 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3415 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3416 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3417 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3418 | } |
| 3419 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3420 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3421 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3422 | psa_algorithm_t alg ) |
| 3423 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3424 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3428 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3429 | psa_algorithm_t alg ) |
| 3430 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3431 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3432 | } |
| 3433 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3434 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
| 3435 | const uint8_t *input, |
| 3436 | size_t input_length ) |
| 3437 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3438 | psa_status_t status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3439 | if( ! operation->key_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3440 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3441 | if( operation->iv_required && ! operation->iv_set ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3442 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3443 | operation->has_input = 1; |
| 3444 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3445 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3446 | if( operation->alg == PSA_ALG_CMAC ) |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3447 | { |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3448 | int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac, |
| 3449 | input, input_length ); |
| 3450 | status = mbedtls_to_psa_error( ret ); |
| 3451 | } |
| 3452 | else |
| 3453 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3454 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3455 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3456 | { |
| 3457 | status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input, |
| 3458 | input_length ); |
| 3459 | } |
| 3460 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3461 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3462 | { |
| 3463 | /* This shouldn't happen if `operation` was initialized by |
| 3464 | * a setup function. */ |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3465 | return( PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 3466 | } |
| 3467 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3468 | if( status != PSA_SUCCESS ) |
| 3469 | psa_mac_abort( operation ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3470 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3471 | } |
| 3472 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3473 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3474 | static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac, |
| 3475 | uint8_t *mac, |
| 3476 | size_t mac_size ) |
| 3477 | { |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 3478 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3479 | psa_algorithm_t hash_alg = hmac->hash_ctx.alg; |
| 3480 | size_t hash_size = 0; |
| 3481 | size_t block_size = psa_get_hash_block_size( hash_alg ); |
| 3482 | psa_status_t status; |
| 3483 | |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3484 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3485 | if( status != PSA_SUCCESS ) |
| 3486 | return( status ); |
| 3487 | /* From here on, tmp needs to be wiped. */ |
| 3488 | |
| 3489 | status = psa_hash_setup( &hmac->hash_ctx, hash_alg ); |
| 3490 | if( status != PSA_SUCCESS ) |
| 3491 | goto exit; |
| 3492 | |
| 3493 | status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size ); |
| 3494 | if( status != PSA_SUCCESS ) |
| 3495 | goto exit; |
| 3496 | |
| 3497 | status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size ); |
| 3498 | if( status != PSA_SUCCESS ) |
| 3499 | goto exit; |
| 3500 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3501 | status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size ); |
| 3502 | if( status != PSA_SUCCESS ) |
| 3503 | goto exit; |
| 3504 | |
| 3505 | memcpy( mac, tmp, mac_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3506 | |
| 3507 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3508 | mbedtls_platform_zeroize( tmp, hash_size ); |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3509 | return( status ); |
| 3510 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3511 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3512 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3513 | 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] | 3514 | uint8_t *mac, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3515 | size_t mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3516 | { |
Gilles Peskine | 1d96fff | 2018-07-02 12:15:39 +0200 | [diff] [blame] | 3517 | if( ! operation->key_set ) |
| 3518 | return( PSA_ERROR_BAD_STATE ); |
| 3519 | if( operation->iv_required && ! operation->iv_set ) |
| 3520 | return( PSA_ERROR_BAD_STATE ); |
| 3521 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3522 | if( mac_size < operation->mac_size ) |
| 3523 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3524 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3525 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3526 | if( operation->alg == PSA_ALG_CMAC ) |
| 3527 | { |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3528 | uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE]; |
| 3529 | int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp ); |
| 3530 | if( ret == 0 ) |
Gilles Peskine | 87b0ac4 | 2018-08-21 14:55:49 +0200 | [diff] [blame] | 3531 | memcpy( mac, tmp, operation->mac_size ); |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3532 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3533 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3534 | } |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3535 | else |
| 3536 | #endif /* MBEDTLS_CMAC_C */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3537 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3538 | if( PSA_ALG_IS_HMAC( operation->alg ) ) |
| 3539 | { |
Gilles Peskine | 01126fa | 2018-07-12 17:04:55 +0200 | [diff] [blame] | 3540 | return( psa_hmac_finish_internal( &operation->ctx.hmac, |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3541 | mac, operation->mac_size ) ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3542 | } |
| 3543 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3544 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 3545 | { |
| 3546 | /* This shouldn't happen if `operation` was initialized by |
| 3547 | * a setup function. */ |
| 3548 | return( PSA_ERROR_BAD_STATE ); |
| 3549 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3550 | } |
| 3551 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3552 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
| 3553 | uint8_t *mac, |
| 3554 | size_t mac_size, |
| 3555 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3556 | { |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3557 | psa_status_t status; |
| 3558 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3559 | if( operation->alg == 0 ) |
| 3560 | { |
| 3561 | return( PSA_ERROR_BAD_STATE ); |
| 3562 | } |
| 3563 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3564 | /* Fill the output buffer with something that isn't a valid mac |
| 3565 | * (barring an attack on the mac and deliberately-crafted input), |
| 3566 | * in case the caller doesn't check the return status properly. */ |
| 3567 | *mac_length = mac_size; |
| 3568 | /* If mac_size is 0 then mac may be NULL and then the |
| 3569 | * call to memset would have undefined behavior. */ |
| 3570 | if( mac_size != 0 ) |
| 3571 | memset( mac, '!', mac_size ); |
| 3572 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3573 | if( ! operation->is_sign ) |
| 3574 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3575 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3576 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3577 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3578 | status = psa_mac_finish_internal( operation, mac, mac_size ); |
| 3579 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3580 | if( status == PSA_SUCCESS ) |
| 3581 | { |
| 3582 | status = psa_mac_abort( operation ); |
| 3583 | if( status == PSA_SUCCESS ) |
| 3584 | *mac_length = operation->mac_size; |
| 3585 | else |
| 3586 | memset( mac, '!', mac_size ); |
| 3587 | } |
| 3588 | else |
| 3589 | psa_mac_abort( operation ); |
| 3590 | return( status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 3593 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
| 3594 | const uint8_t *mac, |
| 3595 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3596 | { |
Gilles Peskine | 828ed14 | 2018-06-18 23:25:51 +0200 | [diff] [blame] | 3597 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3598 | psa_status_t status; |
| 3599 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3600 | if( operation->alg == 0 ) |
| 3601 | { |
| 3602 | return( PSA_ERROR_BAD_STATE ); |
| 3603 | } |
| 3604 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3605 | if( operation->is_sign ) |
| 3606 | { |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 3607 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3608 | } |
| 3609 | if( operation->mac_size != mac_length ) |
| 3610 | { |
| 3611 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3612 | goto cleanup; |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 3613 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3614 | |
| 3615 | status = psa_mac_finish_internal( operation, |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3616 | actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | 28cd416 | 2020-01-20 16:31:06 +0100 | [diff] [blame] | 3617 | if( status != PSA_SUCCESS ) |
| 3618 | goto cleanup; |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3619 | |
| 3620 | if( safer_memcmp( mac, actual_mac, mac_length ) != 0 ) |
| 3621 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3622 | |
| 3623 | cleanup: |
| 3624 | if( status == PSA_SUCCESS ) |
| 3625 | status = psa_mac_abort( operation ); |
| 3626 | else |
| 3627 | psa_mac_abort( operation ); |
| 3628 | |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 3629 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 3630 | |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 3631 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3635 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3636 | /****************************************************************/ |
| 3637 | /* Asymmetric cryptography */ |
| 3638 | /****************************************************************/ |
| 3639 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3640 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3641 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3642 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3643 | * md_alg. Verify that the hash length is acceptable. */ |
Gilles Peskine | 8b18a4f | 2018-06-08 16:34:46 +0200 | [diff] [blame] | 3644 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 3645 | size_t hash_length, |
| 3646 | mbedtls_md_type_t *md_alg ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3647 | { |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 3648 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3649 | 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] | 3650 | *md_alg = mbedtls_md_get_type( md_info ); |
| 3651 | |
| 3652 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 3653 | * parameters. Validate that it fits so that we don't risk an |
| 3654 | * overflow later. */ |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3655 | #if SIZE_MAX > UINT_MAX |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3656 | if( hash_length > UINT_MAX ) |
| 3657 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3658 | #endif |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3659 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3660 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3661 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 3662 | * must be correct. */ |
| 3663 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 3664 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3665 | { |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3666 | if( md_info == NULL ) |
| 3667 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3668 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 3669 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3670 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3671 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3672 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3673 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3674 | /* PSS requires a hash internally. */ |
| 3675 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3676 | { |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3677 | if( md_info == NULL ) |
| 3678 | return( PSA_ERROR_NOT_SUPPORTED ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3679 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3680 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3681 | |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3682 | return( PSA_SUCCESS ); |
Nir Sonnenschein | 4db79eb | 2018-06-04 16:40:31 +0300 | [diff] [blame] | 3683 | } |
| 3684 | |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3685 | static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa, |
| 3686 | psa_algorithm_t alg, |
| 3687 | const uint8_t *hash, |
| 3688 | size_t hash_length, |
| 3689 | uint8_t *signature, |
| 3690 | size_t signature_size, |
| 3691 | size_t *signature_length ) |
| 3692 | { |
| 3693 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3694 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3695 | mbedtls_md_type_t md_alg; |
| 3696 | |
| 3697 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3698 | if( status != PSA_SUCCESS ) |
| 3699 | return( status ); |
| 3700 | |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3701 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3702 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 3703 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3704 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3705 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3706 | { |
| 3707 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3708 | MBEDTLS_MD_NONE ); |
| 3709 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3710 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3711 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3712 | MBEDTLS_RSA_PRIVATE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3713 | md_alg, |
| 3714 | (unsigned int) hash_length, |
| 3715 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3716 | signature ); |
| 3717 | } |
| 3718 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3719 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3720 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3721 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3722 | { |
| 3723 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3724 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3725 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3726 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3727 | MBEDTLS_RSA_PRIVATE, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3728 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3729 | (unsigned int) hash_length, |
| 3730 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3731 | signature ); |
| 3732 | } |
| 3733 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3734 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3735 | { |
| 3736 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3737 | } |
| 3738 | |
| 3739 | if( ret == 0 ) |
Gilles Peskine | 630a18a | 2018-06-29 17:49:35 +0200 | [diff] [blame] | 3740 | *signature_length = mbedtls_rsa_get_len( rsa ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3741 | return( mbedtls_to_psa_error( ret ) ); |
| 3742 | } |
| 3743 | |
| 3744 | static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa, |
| 3745 | psa_algorithm_t alg, |
| 3746 | const uint8_t *hash, |
| 3747 | size_t hash_length, |
| 3748 | const uint8_t *signature, |
| 3749 | size_t signature_length ) |
| 3750 | { |
| 3751 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3752 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3753 | mbedtls_md_type_t md_alg; |
| 3754 | |
| 3755 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 3756 | if( status != PSA_SUCCESS ) |
| 3757 | return( status ); |
| 3758 | |
Gilles Peskine | 89cc74f | 2019-09-12 22:08:23 +0200 | [diff] [blame] | 3759 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 3760 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3761 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3762 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3763 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 3764 | { |
| 3765 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 3766 | MBEDTLS_MD_NONE ); |
| 3767 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3768 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3769 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3770 | MBEDTLS_RSA_PUBLIC, |
| 3771 | md_alg, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3772 | (unsigned int) hash_length, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3773 | hash, |
| 3774 | signature ); |
| 3775 | } |
| 3776 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3777 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 3778 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3779 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 3780 | { |
| 3781 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 3782 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3783 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3784 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3785 | MBEDTLS_RSA_PUBLIC, |
Gilles Peskine | 71ac7b1 | 2018-06-29 23:36:35 +0200 | [diff] [blame] | 3786 | MBEDTLS_MD_NONE, |
Jaeden Amero | bbf97e3 | 2018-06-26 14:20:51 +0100 | [diff] [blame] | 3787 | (unsigned int) hash_length, |
| 3788 | hash, |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3789 | signature ); |
| 3790 | } |
| 3791 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3792 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3793 | { |
| 3794 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3795 | } |
Gilles Peskine | ef12c63 | 2018-09-13 20:37:48 +0200 | [diff] [blame] | 3796 | |
| 3797 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 3798 | * the rest of the signature is invalid". This has little use in |
| 3799 | * practice and PSA doesn't report this distinction. */ |
| 3800 | if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) |
| 3801 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3802 | return( mbedtls_to_psa_error( ret ) ); |
| 3803 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3804 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3805 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 3806 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3807 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3808 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3809 | /* `ecp` cannot be const because `ecp->grp` needs to be non-const |
| 3810 | * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det() |
| 3811 | * (even though these functions don't modify it). */ |
| 3812 | static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp, |
| 3813 | psa_algorithm_t alg, |
| 3814 | const uint8_t *hash, |
| 3815 | size_t hash_length, |
| 3816 | uint8_t *signature, |
| 3817 | size_t signature_size, |
| 3818 | size_t *signature_length ) |
| 3819 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3820 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3821 | mbedtls_mpi r, s; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3822 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3823 | mbedtls_mpi_init( &r ); |
| 3824 | mbedtls_mpi_init( &s ); |
| 3825 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3826 | if( signature_size < 2 * curve_bytes ) |
| 3827 | { |
| 3828 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 3829 | goto cleanup; |
| 3830 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3831 | |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3832 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3833 | if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) ) |
| 3834 | { |
| 3835 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 3836 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 3837 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
Darryl Green | 5e843fa | 2019-09-05 14:06:34 +0100 | [diff] [blame] | 3838 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s, |
| 3839 | &ecp->d, hash, |
| 3840 | hash_length, md_alg, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3841 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3842 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3843 | } |
| 3844 | else |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3845 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3846 | { |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3847 | (void) alg; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3848 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d, |
| 3849 | hash, hash_length, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 3850 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3851 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3852 | } |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3853 | |
| 3854 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r, |
| 3855 | signature, |
| 3856 | curve_bytes ) ); |
| 3857 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s, |
| 3858 | signature + curve_bytes, |
| 3859 | curve_bytes ) ); |
| 3860 | |
| 3861 | cleanup: |
| 3862 | mbedtls_mpi_free( &r ); |
| 3863 | mbedtls_mpi_free( &s ); |
| 3864 | if( ret == 0 ) |
| 3865 | *signature_length = 2 * curve_bytes; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3866 | return( mbedtls_to_psa_error( ret ) ); |
| 3867 | } |
| 3868 | |
| 3869 | static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp, |
| 3870 | const uint8_t *hash, |
| 3871 | size_t hash_length, |
| 3872 | const uint8_t *signature, |
| 3873 | size_t signature_length ) |
| 3874 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3875 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3876 | mbedtls_mpi r, s; |
| 3877 | size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits ); |
| 3878 | mbedtls_mpi_init( &r ); |
| 3879 | mbedtls_mpi_init( &s ); |
| 3880 | |
| 3881 | if( signature_length != 2 * curve_bytes ) |
| 3882 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 3883 | |
| 3884 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, |
| 3885 | signature, |
| 3886 | curve_bytes ) ); |
| 3887 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s, |
| 3888 | signature + curve_bytes, |
| 3889 | curve_bytes ) ); |
| 3890 | |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3891 | /* Check whether the public part is loaded. If not, load it. */ |
| 3892 | if( mbedtls_ecp_is_zero( &ecp->Q ) ) |
| 3893 | { |
| 3894 | MBEDTLS_MPI_CHK( |
| 3895 | mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 3896 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3897 | } |
| 3898 | |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3899 | ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length, |
| 3900 | &ecp->Q, &r, &s ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3901 | |
| 3902 | cleanup: |
| 3903 | mbedtls_mpi_free( &r ); |
| 3904 | mbedtls_mpi_free( &s ); |
| 3905 | return( mbedtls_to_psa_error( ret ) ); |
| 3906 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3907 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3908 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3909 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3910 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3911 | psa_algorithm_t alg, |
| 3912 | const uint8_t *hash, |
| 3913 | size_t hash_length, |
| 3914 | uint8_t *signature, |
| 3915 | size_t signature_size, |
| 3916 | size_t *signature_length ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3917 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3918 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3919 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3920 | psa_key_slot_t *slot; |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3921 | |
| 3922 | *signature_length = signature_size; |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 3923 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3924 | * that signature must be a valid pointer. (On the other hand, the hash |
| 3925 | * buffer can in principle be empty since it doesn't actually have |
| 3926 | * to be a hash.) */ |
| 3927 | if( signature_size == 0 ) |
| 3928 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3929 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3930 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3931 | PSA_KEY_USAGE_SIGN_HASH, |
| 3932 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3933 | if( status != PSA_SUCCESS ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3934 | goto exit; |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3935 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3936 | { |
| 3937 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3938 | goto exit; |
| 3939 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3940 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3941 | /* Try any of the available accelerators first */ |
| 3942 | status = psa_driver_wrapper_sign_hash( slot, |
| 3943 | alg, |
| 3944 | hash, |
| 3945 | hash_length, |
| 3946 | signature, |
| 3947 | signature_size, |
| 3948 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 3949 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 3950 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 3951 | goto exit; |
| 3952 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 3953 | /* If the operation was not supported by any accelerator, try fallback. */ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3954 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3955 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3956 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3957 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3958 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3959 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3960 | status = psa_load_rsa_representation( slot->attr.type, |
| 3961 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3962 | slot->data.key.bytes, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3963 | &rsa ); |
| 3964 | if( status != PSA_SUCCESS ) |
| 3965 | goto exit; |
| 3966 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3967 | status = psa_rsa_sign( rsa, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3968 | alg, |
| 3969 | hash, hash_length, |
| 3970 | signature, signature_size, |
| 3971 | signature_length ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3972 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3973 | mbedtls_rsa_free( rsa ); |
| 3974 | mbedtls_free( rsa ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3975 | } |
| 3976 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3977 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3978 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3979 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3980 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3981 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3982 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3983 | if( |
John Durkop | 0ea39e0 | 2020-10-13 19:58:20 -0700 | [diff] [blame] | 3984 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a05219c | 2018-11-16 16:02:56 +0100 | [diff] [blame] | 3985 | PSA_ALG_IS_ECDSA( alg ) |
| 3986 | #else |
| 3987 | PSA_ALG_IS_RANDOMIZED_ECDSA( alg ) |
| 3988 | #endif |
| 3989 | ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3990 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3991 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3992 | status = psa_load_ecp_representation( slot->attr.type, |
| 3993 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3994 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 3995 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3996 | if( status != PSA_SUCCESS ) |
| 3997 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3998 | status = psa_ecdsa_sign( ecp, |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 3999 | alg, |
| 4000 | hash, hash_length, |
| 4001 | signature, signature_size, |
| 4002 | signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4003 | mbedtls_ecp_keypair_free( ecp ); |
| 4004 | mbedtls_free( ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4005 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4006 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4007 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4008 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4009 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4010 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4011 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4012 | } |
| 4013 | else |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4014 | { |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4015 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4016 | } |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4017 | |
| 4018 | exit: |
| 4019 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 4020 | * the trailing part on success) with something that isn't a valid mac |
| 4021 | * (barring an attack on the mac and deliberately-crafted input), |
| 4022 | * in case the caller doesn't check the return status properly. */ |
| 4023 | if( status == PSA_SUCCESS ) |
| 4024 | memset( signature + *signature_length, '!', |
| 4025 | signature_size - *signature_length ); |
Gilles Peskine | 4019f0e | 2019-09-12 22:05:59 +0200 | [diff] [blame] | 4026 | else |
Gilles Peskine | a26ff6a | 2018-06-28 12:21:19 +0200 | [diff] [blame] | 4027 | memset( signature, '!', signature_size ); |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4028 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 4029 | * memset because signature may be NULL in this case. */ |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4030 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4031 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4032 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4033 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4034 | } |
| 4035 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4036 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4037 | psa_algorithm_t alg, |
| 4038 | const uint8_t *hash, |
| 4039 | size_t hash_length, |
| 4040 | const uint8_t *signature, |
| 4041 | size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4042 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4043 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4044 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4045 | psa_key_slot_t *slot; |
Gilles Peskine | 2b450e3 | 2018-06-27 15:42:46 +0200 | [diff] [blame] | 4046 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4047 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 4048 | PSA_KEY_USAGE_VERIFY_HASH, |
| 4049 | alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4050 | if( status != PSA_SUCCESS ) |
| 4051 | return( status ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4052 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 4053 | /* Try any of the available accelerators first */ |
| 4054 | status = psa_driver_wrapper_verify_hash( slot, |
| 4055 | alg, |
| 4056 | hash, |
| 4057 | hash_length, |
| 4058 | signature, |
| 4059 | signature_length ); |
Steven Cooreman | 8d2bde7 | 2020-09-04 13:06:39 +0200 | [diff] [blame] | 4060 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4061 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4062 | goto exit; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 4063 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4064 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 4065 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4066 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4067 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4068 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4069 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4070 | status = psa_load_rsa_representation( slot->attr.type, |
| 4071 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4072 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4073 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4074 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4075 | goto exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4076 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4077 | status = psa_rsa_verify( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4078 | alg, |
| 4079 | hash, hash_length, |
| 4080 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4081 | mbedtls_rsa_free( rsa ); |
| 4082 | mbedtls_free( rsa ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4083 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4084 | } |
| 4085 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4086 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 4087 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4088 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4089 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4090 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4091 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4092 | if( PSA_ALG_IS_ECDSA( alg ) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4093 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4094 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4095 | status = psa_load_ecp_representation( slot->attr.type, |
| 4096 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4097 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4098 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4099 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4100 | goto exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4101 | status = psa_ecdsa_verify( ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4102 | hash, hash_length, |
| 4103 | signature, signature_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4104 | mbedtls_ecp_keypair_free( ecp ); |
| 4105 | mbedtls_free( ecp ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4106 | goto exit; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 4107 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4108 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4109 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4110 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4111 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4112 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4113 | goto exit; |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 4114 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4115 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4116 | else |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4117 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4118 | status = PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4119 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4120 | |
| 4121 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4122 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4123 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4124 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4125 | } |
| 4126 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4127 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4128 | static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg, |
| 4129 | mbedtls_rsa_context *rsa ) |
| 4130 | { |
| 4131 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg ); |
| 4132 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 4133 | mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info ); |
| 4134 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 4135 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4136 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 4137 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4138 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4139 | psa_algorithm_t alg, |
| 4140 | const uint8_t *input, |
| 4141 | size_t input_length, |
| 4142 | const uint8_t *salt, |
| 4143 | size_t salt_length, |
| 4144 | uint8_t *output, |
| 4145 | size_t output_size, |
| 4146 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4147 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4148 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4149 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4150 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4151 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4152 | (void) input; |
| 4153 | (void) input_length; |
| 4154 | (void) salt; |
| 4155 | (void) output; |
| 4156 | (void) output_size; |
| 4157 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4158 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4159 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4160 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4161 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4162 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4163 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4164 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4165 | if( status != PSA_SUCCESS ) |
| 4166 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4167 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 4168 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4169 | { |
| 4170 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4171 | goto exit; |
| 4172 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4173 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4174 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4175 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4176 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4177 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4178 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4179 | status = psa_load_rsa_representation( slot->attr.type, |
| 4180 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4181 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4182 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4183 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4184 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4185 | |
| 4186 | if( output_size < mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4187 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4188 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4189 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4190 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4191 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4192 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4193 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4194 | status = mbedtls_to_psa_error( |
| 4195 | mbedtls_rsa_pkcs1_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4196 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4197 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4198 | MBEDTLS_RSA_PUBLIC, |
| 4199 | input_length, |
| 4200 | input, |
| 4201 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4202 | } |
| 4203 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4204 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4205 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4206 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4207 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4208 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4209 | status = mbedtls_to_psa_error( |
| 4210 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4211 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4212 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4213 | MBEDTLS_RSA_PUBLIC, |
| 4214 | salt, salt_length, |
| 4215 | input_length, |
| 4216 | input, |
| 4217 | output ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4218 | } |
| 4219 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4220 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4221 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4222 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4223 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4224 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4225 | rsa_exit: |
| 4226 | if( status == PSA_SUCCESS ) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4227 | *output_length = mbedtls_rsa_get_len( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4228 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4229 | mbedtls_rsa_free( rsa ); |
| 4230 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4231 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4232 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 4233 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4234 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4235 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4236 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4237 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4238 | |
| 4239 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4240 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4241 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4242 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4243 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4244 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4245 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 4246 | psa_algorithm_t alg, |
| 4247 | const uint8_t *input, |
| 4248 | size_t input_length, |
| 4249 | const uint8_t *salt, |
| 4250 | size_t salt_length, |
| 4251 | uint8_t *output, |
| 4252 | size_t output_size, |
| 4253 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4254 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4255 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4256 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4257 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4258 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 4259 | (void) input; |
| 4260 | (void) input_length; |
| 4261 | (void) salt; |
| 4262 | (void) output; |
| 4263 | (void) output_size; |
| 4264 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 4265 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4266 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 4267 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 4268 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4269 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4270 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4271 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4272 | if( status != PSA_SUCCESS ) |
| 4273 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4274 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4275 | { |
| 4276 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4277 | goto exit; |
| 4278 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4279 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4280 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 4281 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4282 | if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4283 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4284 | mbedtls_rsa_context *rsa = NULL; |
| 4285 | status = psa_load_rsa_representation( slot->attr.type, |
| 4286 | slot->data.key.data, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4287 | slot->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 4288 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4289 | if( status != PSA_SUCCESS ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4290 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4291 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4292 | if( input_length != mbedtls_rsa_get_len( rsa ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4293 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4294 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4295 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 4296 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4297 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4298 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Gilles Peskine | 6afe789 | 2018-05-31 13:16:08 +0200 | [diff] [blame] | 4299 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4300 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4301 | status = mbedtls_to_psa_error( |
| 4302 | mbedtls_rsa_pkcs1_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4303 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4304 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4305 | MBEDTLS_RSA_PRIVATE, |
| 4306 | output_length, |
| 4307 | input, |
| 4308 | output, |
| 4309 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4310 | } |
| 4311 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4312 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 4313 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 4314 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4315 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4316 | psa_rsa_oaep_set_padding_mode( alg, rsa ); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4317 | status = mbedtls_to_psa_error( |
| 4318 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4319 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4320 | MBEDTLS_PSA_RANDOM_STATE, |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4321 | MBEDTLS_RSA_PRIVATE, |
| 4322 | salt, salt_length, |
| 4323 | output_length, |
| 4324 | input, |
| 4325 | output, |
| 4326 | output_size ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4327 | } |
| 4328 | else |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 4329 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4330 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4331 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4332 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 4333 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 4334 | rsa_exit: |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4335 | mbedtls_rsa_free( rsa ); |
| 4336 | mbedtls_free( rsa ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4337 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4338 | else |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4339 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 4340 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4341 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4342 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4343 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4344 | |
| 4345 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4346 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4347 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4348 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4349 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4350 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4351 | |
| 4352 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4353 | /****************************************************************/ |
| 4354 | /* Symmetric cryptography */ |
| 4355 | /****************************************************************/ |
| 4356 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4357 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4358 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 7e92885 | 2018-06-04 16:23:10 +0200 | [diff] [blame] | 4359 | psa_algorithm_t alg, |
| 4360 | mbedtls_operation_t cipher_operation ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4361 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4362 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4363 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4364 | int ret = 0; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4365 | psa_key_slot_t *slot; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4366 | size_t key_bits; |
| 4367 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4368 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 4369 | PSA_KEY_USAGE_ENCRYPT : |
| 4370 | PSA_KEY_USAGE_DECRYPT ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4371 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4372 | /* A context must be freshly initialized before it can be set up. */ |
| 4373 | if( operation->alg != 0 ) |
| 4374 | return( PSA_ERROR_BAD_STATE ); |
| 4375 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4376 | /* The requested algorithm must be one that can be processed by cipher. */ |
| 4377 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4378 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4379 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4380 | /* Fetch key material from key storage. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4381 | 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] | 4382 | if( status != PSA_SUCCESS ) |
| 4383 | goto exit; |
| 4384 | |
| 4385 | /* Initialize the operation struct members, except for alg. The alg member |
| 4386 | * is used to indicate to psa_cipher_abort that there are resources to free, |
| 4387 | * so we only set it after resources have been allocated/initialized. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4388 | operation->key_set = 0; |
| 4389 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4390 | operation->mbedtls_in_use = 0; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4391 | operation->iv_size = 0; |
| 4392 | operation->block_size = 0; |
| 4393 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 4394 | operation->iv_required = 0; |
| 4395 | else |
| 4396 | operation->iv_required = 1; |
| 4397 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4398 | /* Try doing the operation through a driver before using software fallback. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4399 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4400 | status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4401 | slot, |
| 4402 | alg ); |
| 4403 | else |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4404 | status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 4405 | slot, |
| 4406 | alg ); |
| 4407 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4408 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4409 | { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4410 | /* Once the driver context is initialised, it needs to be freed using |
| 4411 | * psa_cipher_abort. Indicate this through setting alg. */ |
| 4412 | operation->alg = alg; |
Steven Cooreman | 6d81f7e | 2020-09-14 13:14:31 +0200 | [diff] [blame] | 4413 | } |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4414 | |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4415 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 4416 | psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
| 4417 | goto exit; |
| 4418 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4419 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4420 | * available for the given algorithm & key. */ |
| 4421 | mbedtls_cipher_init( &operation->ctx.cipher ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4422 | |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4423 | /* Once the cipher context is initialised, it needs to be freed using |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4424 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 4425 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 4426 | * setting mbedtls_in_use. */ |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4427 | operation->alg = alg; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4428 | operation->mbedtls_in_use = 1; |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4429 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4430 | key_bits = psa_get_key_slot_bits( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4431 | 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] | 4432 | if( cipher_info == NULL ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4433 | { |
| 4434 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4435 | goto exit; |
| 4436 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4437 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4438 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4439 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4440 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4441 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4442 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4443 | if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4444 | { |
| 4445 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
Gilles Peskine | c11c4dc | 2019-07-15 11:06:38 +0200 | [diff] [blame] | 4446 | uint8_t keys[24]; |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4447 | memcpy( keys, slot->data.key.data, 16 ); |
| 4448 | memcpy( keys + 16, slot->data.key.data, 8 ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4449 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 4450 | keys, |
| 4451 | 192, cipher_operation ); |
| 4452 | } |
| 4453 | else |
| 4454 | #endif |
| 4455 | { |
| 4456 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4457 | slot->data.key.data, |
Jaeden Amero | 23bbb75 | 2018-06-26 14:16:54 +0100 | [diff] [blame] | 4458 | (int) key_bits, cipher_operation ); |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 4459 | } |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4460 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4461 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4462 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4463 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4464 | switch( alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4465 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4466 | case PSA_ALG_CBC_NO_PADDING: |
| 4467 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4468 | MBEDTLS_PADDING_NONE ); |
| 4469 | break; |
| 4470 | case PSA_ALG_CBC_PKCS7: |
| 4471 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 4472 | MBEDTLS_PADDING_PKCS7 ); |
| 4473 | break; |
| 4474 | default: |
| 4475 | /* The algorithm doesn't involve padding. */ |
| 4476 | ret = 0; |
| 4477 | break; |
| 4478 | } |
| 4479 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4480 | goto exit; |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4481 | #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING |
| 4482 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4483 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4484 | PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) ); |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4485 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 4486 | alg != PSA_ALG_ECB_NO_PADDING ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4487 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4488 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4489 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4490 | #if defined(MBEDTLS_CHACHA20_C) |
| 4491 | else |
Bence Szépkúti | cbe3953 | 2020-12-08 00:01:31 +0100 | [diff] [blame] | 4492 | if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 ) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4493 | operation->iv_size = 12; |
| 4494 | #endif |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4495 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4496 | status = PSA_SUCCESS; |
| 4497 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4498 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4499 | if( ret != 0 ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4500 | status = mbedtls_to_psa_error( ret ); |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4501 | if( status == PSA_SUCCESS ) |
| 4502 | { |
| 4503 | /* Update operation flags for both driver and software implementations */ |
| 4504 | operation->key_set = 1; |
| 4505 | } |
| 4506 | else |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 4507 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4508 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4509 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4510 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4511 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4512 | } |
| 4513 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4514 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4515 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4516 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4517 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4518 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4519 | } |
| 4520 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4521 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4522 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4523 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4524 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4525 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4526 | } |
| 4527 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4528 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4529 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4530 | size_t iv_size, |
| 4531 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4532 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4533 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4534 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4535 | if( operation->iv_set || ! operation->iv_required ) |
| 4536 | { |
| 4537 | return( PSA_ERROR_BAD_STATE ); |
| 4538 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4539 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4540 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4541 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4542 | status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4543 | iv, |
| 4544 | iv_size, |
| 4545 | iv_length ); |
| 4546 | goto exit; |
| 4547 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4548 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4549 | if( iv_size < operation->iv_size ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4550 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4551 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4552 | goto exit; |
| 4553 | } |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 4554 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4555 | iv, operation->iv_size ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4556 | if( ret != 0 ) |
| 4557 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4558 | status = mbedtls_to_psa_error( ret ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4559 | goto exit; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4560 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4561 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4562 | *iv_length = operation->iv_size; |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4563 | status = psa_cipher_set_iv( operation, iv, *iv_length ); |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4564 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4565 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4566 | if( status == PSA_SUCCESS ) |
| 4567 | operation->iv_set = 1; |
| 4568 | else |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4569 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4570 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4571 | } |
| 4572 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4573 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4574 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 4575 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4576 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4577 | psa_status_t status; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 4578 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4579 | if( operation->iv_set || ! operation->iv_required ) |
| 4580 | { |
| 4581 | return( PSA_ERROR_BAD_STATE ); |
| 4582 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4583 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4584 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4585 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4586 | status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4587 | iv, |
| 4588 | iv_length ); |
| 4589 | goto exit; |
| 4590 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4591 | |
Moran Peker | a28258c | 2018-05-29 16:25:04 +0300 | [diff] [blame] | 4592 | if( iv_length != operation->iv_size ) |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4593 | { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4594 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4595 | goto exit; |
Moran Peker | 71f19ae | 2018-04-22 20:23:16 +0300 | [diff] [blame] | 4596 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4597 | ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); |
| 4598 | status = mbedtls_to_psa_error( ret ); |
| 4599 | exit: |
| 4600 | if( status == PSA_SUCCESS ) |
| 4601 | operation->iv_set = 1; |
| 4602 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4603 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4604 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4605 | } |
| 4606 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4607 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 4608 | * manual processing, since the PSA API is defined as being able to process |
| 4609 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 4610 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 4611 | static psa_status_t psa_cipher_update_ecb_internal( |
| 4612 | mbedtls_cipher_context_t *ctx, |
| 4613 | const uint8_t *input, |
| 4614 | size_t input_length, |
| 4615 | uint8_t *output, |
| 4616 | size_t output_size, |
| 4617 | size_t *output_length ) |
| 4618 | { |
| 4619 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4620 | size_t block_size = ctx->cipher_info->block_size; |
| 4621 | size_t internal_output_length = 0; |
| 4622 | *output_length = 0; |
| 4623 | |
| 4624 | if( input_length == 0 ) |
| 4625 | { |
| 4626 | status = PSA_SUCCESS; |
| 4627 | goto exit; |
| 4628 | } |
| 4629 | |
| 4630 | if( ctx->unprocessed_len > 0 ) |
| 4631 | { |
| 4632 | /* Fill up to block size, and run the block if there's a full one. */ |
| 4633 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 4634 | |
| 4635 | if( input_length < bytes_to_copy ) |
| 4636 | bytes_to_copy = input_length; |
| 4637 | |
| 4638 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4639 | input, bytes_to_copy ); |
| 4640 | input_length -= bytes_to_copy; |
| 4641 | input += bytes_to_copy; |
| 4642 | ctx->unprocessed_len += bytes_to_copy; |
| 4643 | |
| 4644 | if( ctx->unprocessed_len == block_size ) |
| 4645 | { |
| 4646 | status = mbedtls_to_psa_error( |
| 4647 | mbedtls_cipher_update( ctx, |
| 4648 | ctx->unprocessed_data, |
| 4649 | block_size, |
| 4650 | output, &internal_output_length ) ); |
| 4651 | |
| 4652 | if( status != PSA_SUCCESS ) |
| 4653 | goto exit; |
| 4654 | |
| 4655 | output += internal_output_length; |
| 4656 | output_size -= internal_output_length; |
| 4657 | *output_length += internal_output_length; |
| 4658 | ctx->unprocessed_len = 0; |
| 4659 | } |
| 4660 | } |
| 4661 | |
| 4662 | while( input_length >= block_size ) |
| 4663 | { |
| 4664 | /* Run all full blocks we have, one by one */ |
| 4665 | status = mbedtls_to_psa_error( |
| 4666 | mbedtls_cipher_update( ctx, input, |
| 4667 | block_size, |
| 4668 | output, &internal_output_length ) ); |
| 4669 | |
| 4670 | if( status != PSA_SUCCESS ) |
| 4671 | goto exit; |
| 4672 | |
| 4673 | input_length -= block_size; |
| 4674 | input += block_size; |
| 4675 | |
| 4676 | output += internal_output_length; |
| 4677 | output_size -= internal_output_length; |
| 4678 | *output_length += internal_output_length; |
| 4679 | } |
| 4680 | |
| 4681 | if( input_length > 0 ) |
| 4682 | { |
| 4683 | /* Save unprocessed bytes for later processing */ |
| 4684 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 4685 | input, input_length ); |
| 4686 | ctx->unprocessed_len += input_length; |
| 4687 | } |
| 4688 | |
| 4689 | status = PSA_SUCCESS; |
| 4690 | |
| 4691 | exit: |
| 4692 | return( status ); |
| 4693 | } |
| 4694 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4695 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 4696 | const uint8_t *input, |
| 4697 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 4698 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4699 | size_t output_size, |
| 4700 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4701 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4702 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4703 | size_t expected_output_size; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4704 | if( operation->alg == 0 ) |
| 4705 | { |
| 4706 | return( PSA_ERROR_BAD_STATE ); |
| 4707 | } |
| 4708 | if( operation->iv_required && ! operation->iv_set ) |
| 4709 | { |
| 4710 | return( PSA_ERROR_BAD_STATE ); |
| 4711 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4712 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4713 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4714 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4715 | status = psa_driver_wrapper_cipher_update( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4716 | input, |
| 4717 | input_length, |
| 4718 | output, |
| 4719 | output_size, |
| 4720 | output_length ); |
| 4721 | goto exit; |
| 4722 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4723 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4724 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4725 | { |
| 4726 | /* Take the unprocessed partial block left over from previous |
| 4727 | * update calls, if any, plus the input to this call. Remove |
| 4728 | * the last partial block, if any. You get the data that will be |
| 4729 | * output in this call. */ |
| 4730 | expected_output_size = |
| 4731 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
| 4732 | / operation->block_size * operation->block_size; |
| 4733 | } |
| 4734 | else |
| 4735 | { |
| 4736 | expected_output_size = input_length; |
| 4737 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4738 | |
Gilles Peskine | 89d789c | 2018-06-04 17:17:16 +0200 | [diff] [blame] | 4739 | if( output_size < expected_output_size ) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4740 | { |
| 4741 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4742 | goto exit; |
| 4743 | } |
mohammad1603 | 8275961 | 2018-03-12 18:16:40 +0200 | [diff] [blame] | 4744 | |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4745 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
| 4746 | { |
| 4747 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 4748 | * process a single block at a time in ECB mode. Abstract away that |
| 4749 | * inconsistency here to match the PSA API behaviour. */ |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4750 | status = psa_cipher_update_ecb_internal( &operation->ctx.cipher, |
| 4751 | input, |
| 4752 | input_length, |
| 4753 | output, |
| 4754 | output_size, |
| 4755 | output_length ); |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4756 | } |
| 4757 | else |
| 4758 | { |
| 4759 | status = mbedtls_to_psa_error( |
| 4760 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
| 4761 | input_length, output, output_length ) ); |
| 4762 | } |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4763 | exit: |
| 4764 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4765 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4766 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4767 | } |
| 4768 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4769 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 4770 | uint8_t *output, |
| 4771 | size_t output_size, |
| 4772 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4773 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4774 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4775 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4776 | if( operation->alg == 0 ) |
| 4777 | { |
| 4778 | return( PSA_ERROR_BAD_STATE ); |
| 4779 | } |
| 4780 | if( operation->iv_required && ! operation->iv_set ) |
| 4781 | { |
| 4782 | return( PSA_ERROR_BAD_STATE ); |
| 4783 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4784 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4785 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4786 | { |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4787 | status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver, |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4788 | output, |
| 4789 | output_size, |
| 4790 | output_length ); |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4791 | goto exit; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 4792 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4793 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4794 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Moran Peker | 7cb22b8 | 2018-06-05 11:40:02 +0300 | [diff] [blame] | 4795 | { |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4796 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 4797 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4798 | { |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 4799 | status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4800 | goto exit; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4801 | } |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4802 | } |
| 4803 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4804 | status = mbedtls_to_psa_error( |
| 4805 | mbedtls_cipher_finish( &operation->ctx.cipher, |
| 4806 | temp_output_buffer, |
| 4807 | output_length ) ); |
| 4808 | if( status != PSA_SUCCESS ) |
| 4809 | goto exit; |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4810 | |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4811 | if( *output_length == 0 ) |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4812 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 46f1fd7 | 2018-06-28 19:31:31 +0200 | [diff] [blame] | 4813 | else if( output_size >= *output_length ) |
Moran Peker | dc38ebc | 2018-04-30 15:45:34 +0300 | [diff] [blame] | 4814 | memcpy( output, temp_output_buffer, *output_length ); |
| 4815 | else |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4816 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4817 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4818 | exit: |
| 4819 | if( operation->mbedtls_in_use == 1 ) |
| 4820 | mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4821 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4822 | if( status == PSA_SUCCESS ) |
| 4823 | return( psa_cipher_abort( operation ) ); |
| 4824 | else |
| 4825 | { |
| 4826 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4827 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4828 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4829 | return( status ); |
| 4830 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4831 | } |
| 4832 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4833 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 4834 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4835 | if( operation->alg == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4836 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4837 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4838 | * in use. It's ok to call abort on such an object, and there's |
| 4839 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4840 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4841 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4842 | |
Gilles Peskine | f9c2c09 | 2018-06-21 16:57:07 +0200 | [diff] [blame] | 4843 | /* Sanity check (shouldn't happen: operation->alg should |
| 4844 | * always have been initialized to a valid value). */ |
| 4845 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
| 4846 | return( PSA_ERROR_BAD_STATE ); |
| 4847 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4848 | if( operation->mbedtls_in_use == 0 ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 4849 | psa_driver_wrapper_cipher_abort( &operation->ctx.driver ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4850 | else |
| 4851 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4852 | |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4853 | operation->alg = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4854 | operation->key_set = 0; |
| 4855 | operation->iv_set = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4856 | operation->mbedtls_in_use = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4857 | operation->iv_size = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4858 | operation->block_size = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4859 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4860 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 4861 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4862 | } |
| 4863 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 4864 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4865 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4866 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4867 | /****************************************************************/ |
| 4868 | /* AEAD */ |
| 4869 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4870 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4871 | typedef struct |
| 4872 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4873 | psa_key_slot_t *slot; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4874 | const mbedtls_cipher_info_t *cipher_info; |
| 4875 | union |
| 4876 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4877 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4878 | #if defined(MBEDTLS_CCM_C) |
| 4879 | mbedtls_ccm_context ccm; |
| 4880 | #endif /* MBEDTLS_CCM_C */ |
| 4881 | #if defined(MBEDTLS_GCM_C) |
| 4882 | mbedtls_gcm_context gcm; |
| 4883 | #endif /* MBEDTLS_GCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4884 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4885 | mbedtls_chachapoly_context chachapoly; |
| 4886 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4887 | } ctx; |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4888 | psa_algorithm_t core_alg; |
| 4889 | uint8_t full_tag_length; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4890 | uint8_t tag_length; |
| 4891 | } aead_operation_t; |
| 4892 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4893 | #define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0} |
| 4894 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 4895 | static void psa_aead_abort_internal( aead_operation_t *operation ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4896 | { |
Gilles Peskine | f8a8fe6 | 2018-08-21 16:38:05 +0200 | [diff] [blame] | 4897 | switch( operation->core_alg ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4898 | { |
| 4899 | #if defined(MBEDTLS_CCM_C) |
| 4900 | case PSA_ALG_CCM: |
| 4901 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 4902 | break; |
| 4903 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 4904 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4905 | case PSA_ALG_GCM: |
| 4906 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 4907 | break; |
| 4908 | #endif /* MBEDTLS_GCM_C */ |
| 4909 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4910 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4911 | psa_unlock_key_slot( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | static psa_status_t psa_aead_setup( aead_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4915 | mbedtls_svc_key_id_t key, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4916 | psa_key_usage_t usage, |
| 4917 | psa_algorithm_t alg ) |
| 4918 | { |
| 4919 | psa_status_t status; |
| 4920 | size_t key_bits; |
| 4921 | mbedtls_cipher_id_t cipher_id; |
| 4922 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4923 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 4924 | key, &operation->slot, usage, alg ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4925 | if( status != PSA_SUCCESS ) |
| 4926 | return( status ); |
| 4927 | |
Gilles Peskine | db4b3ab | 2019-04-18 12:53:01 +0200 | [diff] [blame] | 4928 | key_bits = psa_get_key_slot_bits( operation->slot ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4929 | |
| 4930 | operation->cipher_info = |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4931 | mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4932 | &cipher_id ); |
| 4933 | if( operation->cipher_info == NULL ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4934 | { |
| 4935 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4936 | goto cleanup; |
| 4937 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4938 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4939 | switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ) |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4940 | { |
| 4941 | #if defined(MBEDTLS_CCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4942 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ): |
| 4943 | operation->core_alg = PSA_ALG_CCM; |
| 4944 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4945 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 4946 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 4947 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4948 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4949 | { |
| 4950 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4951 | goto cleanup; |
| 4952 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4953 | mbedtls_ccm_init( &operation->ctx.ccm ); |
| 4954 | status = mbedtls_to_psa_error( |
| 4955 | mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4956 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4957 | (unsigned int) key_bits ) ); |
| 4958 | if( status != 0 ) |
| 4959 | goto cleanup; |
| 4960 | break; |
| 4961 | #endif /* MBEDTLS_CCM_C */ |
| 4962 | |
| 4963 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 4964 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ): |
| 4965 | operation->core_alg = PSA_ALG_GCM; |
| 4966 | operation->full_tag_length = 16; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4967 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 4968 | * The call to mbedtls_gcm_crypt_and_tag or |
| 4969 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 4970 | if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4971 | { |
| 4972 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4973 | goto cleanup; |
| 4974 | } |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4975 | mbedtls_gcm_init( &operation->ctx.gcm ); |
| 4976 | status = mbedtls_to_psa_error( |
| 4977 | mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4978 | operation->slot->data.key.data, |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4979 | (unsigned int) key_bits ) ); |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4980 | if( status != 0 ) |
| 4981 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 4982 | break; |
| 4983 | #endif /* MBEDTLS_GCM_C */ |
| 4984 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4985 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 4986 | case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 4987 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
| 4988 | operation->full_tag_length = 16; |
| 4989 | /* We only support the default tag length. */ |
| 4990 | if( alg != PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4991 | { |
| 4992 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4993 | goto cleanup; |
| 4994 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4995 | mbedtls_chachapoly_init( &operation->ctx.chachapoly ); |
| 4996 | status = mbedtls_to_psa_error( |
| 4997 | mbedtls_chachapoly_setkey( &operation->ctx.chachapoly, |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 4998 | operation->slot->data.key.data ) ); |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 4999 | if( status != 0 ) |
| 5000 | goto cleanup; |
| 5001 | break; |
| 5002 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
| 5003 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5004 | default: |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5005 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5006 | goto cleanup; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5007 | } |
| 5008 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5009 | if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length ) |
| 5010 | { |
| 5011 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5012 | goto cleanup; |
| 5013 | } |
| 5014 | operation->tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5015 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5016 | return( PSA_SUCCESS ); |
| 5017 | |
| 5018 | cleanup: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5019 | psa_aead_abort_internal( operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5020 | return( status ); |
| 5021 | } |
| 5022 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5023 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5024 | psa_algorithm_t alg, |
| 5025 | const uint8_t *nonce, |
| 5026 | size_t nonce_length, |
| 5027 | const uint8_t *additional_data, |
| 5028 | size_t additional_data_length, |
| 5029 | const uint8_t *plaintext, |
| 5030 | size_t plaintext_length, |
| 5031 | uint8_t *ciphertext, |
| 5032 | size_t ciphertext_size, |
| 5033 | size_t *ciphertext_length ) |
| 5034 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5035 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5036 | aead_operation_t operation = AEAD_OPERATION_INIT; |
mohammad1603 | 15223a8 | 2018-06-03 17:19:55 +0300 | [diff] [blame] | 5037 | uint8_t *tag; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5038 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 5039 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 5040 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5041 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5042 | if( status != PSA_SUCCESS ) |
| 5043 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5044 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5045 | /* For all currently supported modes, the tag is at the end of the |
| 5046 | * ciphertext. */ |
| 5047 | if( ciphertext_size < ( plaintext_length + operation.tag_length ) ) |
| 5048 | { |
| 5049 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5050 | goto exit; |
| 5051 | } |
| 5052 | tag = ciphertext + plaintext_length; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5053 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5054 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5055 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5056 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5057 | status = mbedtls_to_psa_error( |
| 5058 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 5059 | MBEDTLS_GCM_ENCRYPT, |
| 5060 | plaintext_length, |
| 5061 | nonce, nonce_length, |
| 5062 | additional_data, additional_data_length, |
| 5063 | plaintext, ciphertext, |
| 5064 | operation.tag_length, tag ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5065 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5066 | else |
| 5067 | #endif /* MBEDTLS_GCM_C */ |
| 5068 | #if defined(MBEDTLS_CCM_C) |
| 5069 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5070 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5071 | status = mbedtls_to_psa_error( |
| 5072 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 5073 | plaintext_length, |
| 5074 | nonce, nonce_length, |
| 5075 | additional_data, |
| 5076 | additional_data_length, |
| 5077 | plaintext, ciphertext, |
| 5078 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5079 | } |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5080 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5081 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5082 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5083 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5084 | { |
| 5085 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5086 | { |
| 5087 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5088 | goto exit; |
| 5089 | } |
| 5090 | status = mbedtls_to_psa_error( |
| 5091 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 5092 | plaintext_length, |
| 5093 | nonce, |
| 5094 | additional_data, |
| 5095 | additional_data_length, |
| 5096 | plaintext, |
| 5097 | ciphertext, |
| 5098 | tag ) ); |
| 5099 | } |
| 5100 | else |
| 5101 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5102 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5103 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 5c8845f | 2018-05-09 05:40:09 -0700 | [diff] [blame] | 5104 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5105 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5106 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 5107 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5108 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5109 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5110 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5111 | if( status == PSA_SUCCESS ) |
| 5112 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 5113 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5114 | } |
| 5115 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5116 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 5117 | * followed by the tag. Return the length of the part preceding the tag in |
| 5118 | * *plaintext_length. This is the size of the plaintext in modes where |
| 5119 | * the encrypted data has the same size as the plaintext, such as |
| 5120 | * CCM and GCM. */ |
| 5121 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 5122 | const uint8_t *ciphertext, |
| 5123 | size_t ciphertext_length, |
| 5124 | size_t plaintext_size, |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5125 | const uint8_t **p_tag ) |
| 5126 | { |
| 5127 | size_t payload_length; |
| 5128 | if( tag_length > ciphertext_length ) |
| 5129 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5130 | payload_length = ciphertext_length - tag_length; |
| 5131 | if( payload_length > plaintext_size ) |
| 5132 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 5133 | *p_tag = ciphertext + payload_length; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5134 | return( PSA_SUCCESS ); |
| 5135 | } |
| 5136 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5137 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5138 | psa_algorithm_t alg, |
| 5139 | const uint8_t *nonce, |
| 5140 | size_t nonce_length, |
| 5141 | const uint8_t *additional_data, |
| 5142 | size_t additional_data_length, |
| 5143 | const uint8_t *ciphertext, |
| 5144 | size_t ciphertext_length, |
| 5145 | uint8_t *plaintext, |
| 5146 | size_t plaintext_size, |
| 5147 | size_t *plaintext_length ) |
| 5148 | { |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5149 | psa_status_t status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5150 | aead_operation_t operation = AEAD_OPERATION_INIT; |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5151 | const uint8_t *tag = NULL; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5152 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 5153 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 5154 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5155 | status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5156 | if( status != PSA_SUCCESS ) |
| 5157 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5158 | |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 5159 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 5160 | ciphertext, ciphertext_length, |
| 5161 | plaintext_size, &tag ); |
| 5162 | if( status != PSA_SUCCESS ) |
| 5163 | goto exit; |
| 5164 | |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5165 | #if defined(MBEDTLS_GCM_C) |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 5166 | if( operation.core_alg == PSA_ALG_GCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5167 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5168 | status = mbedtls_to_psa_error( |
| 5169 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 5170 | ciphertext_length - operation.tag_length, |
| 5171 | nonce, nonce_length, |
| 5172 | additional_data, |
| 5173 | additional_data_length, |
| 5174 | tag, operation.tag_length, |
| 5175 | ciphertext, plaintext ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5176 | } |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5177 | else |
| 5178 | #endif /* MBEDTLS_GCM_C */ |
| 5179 | #if defined(MBEDTLS_CCM_C) |
| 5180 | if( operation.core_alg == PSA_ALG_CCM ) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5181 | { |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5182 | status = mbedtls_to_psa_error( |
| 5183 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 5184 | ciphertext_length - operation.tag_length, |
| 5185 | nonce, nonce_length, |
| 5186 | additional_data, |
| 5187 | additional_data_length, |
| 5188 | ciphertext, plaintext, |
| 5189 | tag, operation.tag_length ) ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5190 | } |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5191 | else |
Gilles Peskine | b0b189f | 2018-11-28 17:30:58 +0100 | [diff] [blame] | 5192 | #endif /* MBEDTLS_CCM_C */ |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 5193 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 5194 | if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 5195 | { |
| 5196 | if( nonce_length != 12 || operation.tag_length != 16 ) |
| 5197 | { |
| 5198 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5199 | goto exit; |
| 5200 | } |
| 5201 | status = mbedtls_to_psa_error( |
| 5202 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 5203 | ciphertext_length - operation.tag_length, |
| 5204 | nonce, |
| 5205 | additional_data, |
| 5206 | additional_data_length, |
| 5207 | tag, |
| 5208 | ciphertext, |
| 5209 | plaintext ) ); |
| 5210 | } |
| 5211 | else |
| 5212 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5213 | { |
mohammad1603 | 554faad | 2018-06-03 15:07:38 +0300 | [diff] [blame] | 5214 | return( PSA_ERROR_NOT_SUPPORTED ); |
mohammad1603 | 3957465 | 2018-06-01 04:39:53 -0700 | [diff] [blame] | 5215 | } |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 5216 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5217 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 5218 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 5219 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5220 | exit: |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 5221 | psa_aead_abort_internal( &operation ); |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 5222 | if( status == PSA_SUCCESS ) |
| 5223 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 5224 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 5225 | } |
| 5226 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 5227 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 5228 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5229 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5230 | /* Generators */ |
| 5231 | /****************************************************************/ |
| 5232 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5233 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 5234 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5235 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5236 | #define AT_LEAST_ONE_BUILTIN_KDF |
| 5237 | #endif |
| 5238 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5239 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5240 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5241 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5242 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5243 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5244 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5245 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5246 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5247 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 5248 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5249 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5250 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5251 | } |
| 5252 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5253 | 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] | 5254 | { |
| 5255 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5256 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5257 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5258 | { |
| 5259 | /* The object has (apparently) been initialized but it is not |
| 5260 | * in use. It's ok to call abort on such an object, and there's |
| 5261 | * nothing to do. */ |
| 5262 | } |
| 5263 | else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5264 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5265 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5266 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5267 | mbedtls_free( operation->ctx.hkdf.info ); |
| 5268 | status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5269 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5270 | else |
| 5271 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5272 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5273 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5274 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5275 | /* 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] | 5276 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5277 | { |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5278 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 5279 | { |
| 5280 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 5281 | operation->ctx.tls12_prf.seed_length ); |
| 5282 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 5283 | } |
| 5284 | |
| 5285 | if( operation->ctx.tls12_prf.label != NULL ) |
| 5286 | { |
| 5287 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 5288 | operation->ctx.tls12_prf.label_length ); |
| 5289 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 5290 | } |
| 5291 | |
| 5292 | status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac ); |
| 5293 | |
| 5294 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5295 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5296 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5297 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5298 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5299 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5300 | { |
| 5301 | status = PSA_ERROR_BAD_STATE; |
| 5302 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 5303 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5304 | return( status ); |
| 5305 | } |
| 5306 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5307 | 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] | 5308 | size_t *capacity) |
| 5309 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5310 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5311 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5312 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5313 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5316 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5317 | return( PSA_SUCCESS ); |
| 5318 | } |
| 5319 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5320 | 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] | 5321 | size_t capacity ) |
| 5322 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5323 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5324 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5325 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5326 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5327 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5328 | return( PSA_SUCCESS ); |
| 5329 | } |
| 5330 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5331 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5332 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5333 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5334 | 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] | 5335 | psa_algorithm_t hash_alg, |
| 5336 | uint8_t *output, |
| 5337 | size_t output_length ) |
| 5338 | { |
| 5339 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5340 | psa_status_t status; |
| 5341 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5342 | if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set ) |
| 5343 | return( PSA_ERROR_BAD_STATE ); |
| 5344 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5345 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5346 | while( output_length != 0 ) |
| 5347 | { |
| 5348 | /* Copy what remains of the current block */ |
| 5349 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 5350 | if( n > output_length ) |
| 5351 | n = (uint8_t) output_length; |
| 5352 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 5353 | output += n; |
| 5354 | output_length -= n; |
| 5355 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5356 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5357 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5358 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5359 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5360 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5361 | * object was corrupted or if this function is called directly |
| 5362 | * inside the library. */ |
| 5363 | if( hkdf->block_number == 0xff ) |
| 5364 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5365 | |
| 5366 | /* We need a new block */ |
| 5367 | ++hkdf->block_number; |
| 5368 | hkdf->offset_in_block = 0; |
| 5369 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5370 | hkdf->prk, hash_length, |
| 5371 | hash_alg ); |
| 5372 | if( status != PSA_SUCCESS ) |
| 5373 | return( status ); |
| 5374 | if( hkdf->block_number != 1 ) |
| 5375 | { |
| 5376 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5377 | hkdf->output_block, |
| 5378 | hash_length ); |
| 5379 | if( status != PSA_SUCCESS ) |
| 5380 | return( status ); |
| 5381 | } |
| 5382 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5383 | hkdf->info, |
| 5384 | hkdf->info_length ); |
| 5385 | if( status != PSA_SUCCESS ) |
| 5386 | return( status ); |
| 5387 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5388 | &hkdf->block_number, 1 ); |
| 5389 | if( status != PSA_SUCCESS ) |
| 5390 | return( status ); |
| 5391 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5392 | hkdf->output_block, |
| 5393 | sizeof( hkdf->output_block ) ); |
| 5394 | if( status != PSA_SUCCESS ) |
| 5395 | return( status ); |
| 5396 | } |
| 5397 | |
| 5398 | return( PSA_SUCCESS ); |
| 5399 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5400 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5401 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5402 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5403 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5404 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5405 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5406 | psa_algorithm_t alg ) |
| 5407 | { |
| 5408 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
| 5409 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5410 | psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT; |
| 5411 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5412 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5413 | /* We can't be wanting more output after block 0xff, otherwise |
| 5414 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5415 | * prevented this call. It could happen only if the operation |
| 5416 | * object was corrupted or if this function is called directly |
| 5417 | * inside the library. */ |
| 5418 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 5419 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5420 | |
| 5421 | /* We need a new block */ |
| 5422 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5423 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5424 | |
| 5425 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5426 | * |
| 5427 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5428 | * |
| 5429 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5430 | * HMAC_hash(secret, A(2) + seed) + |
| 5431 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5432 | * |
| 5433 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5434 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5435 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5436 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5437 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5438 | * is currently extracted as `output_block` and where i is |
| 5439 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5440 | */ |
| 5441 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5442 | /* Save the hash context before using it, to preserve the hash state with |
| 5443 | * only the inner padding in it. We need this, because inner padding depends |
| 5444 | * on the key (secret in the RFC's terminology). */ |
| 5445 | status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup ); |
| 5446 | if( status != PSA_SUCCESS ) |
| 5447 | goto cleanup; |
| 5448 | |
| 5449 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 5450 | if( tls12_prf->block_number == 1 ) |
| 5451 | { |
| 5452 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5453 | * the variable seed and in this instance means it in the context of the |
| 5454 | * P_hash function, where seed = label + seed.) */ |
| 5455 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5456 | tls12_prf->label, tls12_prf->label_length ); |
| 5457 | if( status != PSA_SUCCESS ) |
| 5458 | goto cleanup; |
| 5459 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5460 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5461 | if( status != PSA_SUCCESS ) |
| 5462 | goto cleanup; |
| 5463 | } |
| 5464 | else |
| 5465 | { |
| 5466 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
| 5467 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5468 | tls12_prf->Ai, hash_length ); |
| 5469 | if( status != PSA_SUCCESS ) |
| 5470 | goto cleanup; |
| 5471 | } |
| 5472 | |
| 5473 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5474 | tls12_prf->Ai, hash_length ); |
| 5475 | if( status != PSA_SUCCESS ) |
| 5476 | goto cleanup; |
| 5477 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5478 | if( status != PSA_SUCCESS ) |
| 5479 | goto cleanup; |
| 5480 | |
| 5481 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
| 5482 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5483 | tls12_prf->Ai, hash_length ); |
| 5484 | if( status != PSA_SUCCESS ) |
| 5485 | goto cleanup; |
| 5486 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5487 | tls12_prf->label, tls12_prf->label_length ); |
| 5488 | if( status != PSA_SUCCESS ) |
| 5489 | goto cleanup; |
| 5490 | status = psa_hash_update( &tls12_prf->hmac.hash_ctx, |
| 5491 | tls12_prf->seed, tls12_prf->seed_length ); |
| 5492 | if( status != PSA_SUCCESS ) |
| 5493 | goto cleanup; |
| 5494 | status = psa_hmac_finish_internal( &tls12_prf->hmac, |
| 5495 | tls12_prf->output_block, hash_length ); |
| 5496 | if( status != PSA_SUCCESS ) |
| 5497 | goto cleanup; |
| 5498 | status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx ); |
| 5499 | if( status != PSA_SUCCESS ) |
| 5500 | goto cleanup; |
| 5501 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5502 | |
| 5503 | cleanup: |
| 5504 | |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5505 | cleanup_status = psa_hash_abort( &backup ); |
| 5506 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 5507 | status = cleanup_status; |
| 5508 | |
| 5509 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5510 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5511 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5512 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5513 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5514 | psa_algorithm_t alg, |
| 5515 | uint8_t *output, |
| 5516 | size_t output_length ) |
| 5517 | { |
| 5518 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
| 5519 | uint8_t hash_length = PSA_HASH_SIZE( hash_alg ); |
| 5520 | psa_status_t status; |
| 5521 | uint8_t offset, length; |
| 5522 | |
| 5523 | while( output_length != 0 ) |
| 5524 | { |
| 5525 | /* Check if we have fully processed the current block. */ |
| 5526 | if( tls12_prf->left_in_block == 0 ) |
| 5527 | { |
| 5528 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 5529 | alg ); |
| 5530 | if( status != PSA_SUCCESS ) |
| 5531 | return( status ); |
| 5532 | |
| 5533 | continue; |
| 5534 | } |
| 5535 | |
| 5536 | if( tls12_prf->left_in_block > output_length ) |
| 5537 | length = (uint8_t) output_length; |
| 5538 | else |
| 5539 | length = tls12_prf->left_in_block; |
| 5540 | |
| 5541 | offset = hash_length - tls12_prf->left_in_block; |
| 5542 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 5543 | output += length; |
| 5544 | output_length -= length; |
| 5545 | tls12_prf->left_in_block -= length; |
| 5546 | } |
| 5547 | |
| 5548 | return( PSA_SUCCESS ); |
| 5549 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5550 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5551 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5552 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5553 | psa_status_t psa_key_derivation_output_bytes( |
| 5554 | psa_key_derivation_operation_t *operation, |
| 5555 | uint8_t *output, |
| 5556 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5557 | { |
| 5558 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5559 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5560 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5561 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5562 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5563 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5564 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5567 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5568 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5569 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5570 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5571 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5572 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5573 | goto exit; |
| 5574 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5575 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5576 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5577 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5578 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5579 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5580 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5581 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5582 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5583 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5584 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5585 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5586 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5587 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5588 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5589 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5590 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5591 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5592 | output, output_length ); |
| 5593 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5594 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5595 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 5596 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5597 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 5598 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5599 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5600 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5601 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5602 | kdf_alg, output, |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5603 | output_length ); |
| 5604 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5605 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5606 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5607 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5608 | { |
| 5609 | return( PSA_ERROR_BAD_STATE ); |
| 5610 | } |
| 5611 | |
| 5612 | exit: |
| 5613 | if( status != PSA_SUCCESS ) |
| 5614 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5615 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5616 | * This allows us to differentiate between exhausted operations and |
| 5617 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5618 | * operations. */ |
| 5619 | psa_algorithm_t alg = operation->alg; |
| 5620 | psa_key_derivation_abort( operation ); |
| 5621 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5622 | memset( output, '!', output_length ); |
| 5623 | } |
| 5624 | return( status ); |
| 5625 | } |
| 5626 | |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5627 | #if defined(MBEDTLS_DES_C) |
| 5628 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 5629 | { |
| 5630 | if( data_size >= 8 ) |
| 5631 | mbedtls_des_key_set_parity( data ); |
| 5632 | if( data_size >= 16 ) |
| 5633 | mbedtls_des_key_set_parity( data + 8 ); |
| 5634 | if( data_size >= 24 ) |
| 5635 | mbedtls_des_key_set_parity( data + 16 ); |
| 5636 | } |
| 5637 | #endif /* MBEDTLS_DES_C */ |
| 5638 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5639 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5640 | psa_key_slot_t *slot, |
| 5641 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5642 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5643 | { |
| 5644 | uint8_t *data = NULL; |
| 5645 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
| 5646 | psa_status_t status; |
| 5647 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5648 | if( ! key_type_is_raw_bytes( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5649 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5650 | if( bits % 8 != 0 ) |
| 5651 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5652 | data = mbedtls_calloc( 1, bytes ); |
| 5653 | if( data == NULL ) |
| 5654 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5655 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5656 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5657 | if( status != PSA_SUCCESS ) |
| 5658 | goto exit; |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5659 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5660 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5661 | psa_des_set_key_parity( data, bytes ); |
| 5662 | #endif /* MBEDTLS_DES_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5663 | status = psa_import_key_into_slot( slot, data, bytes ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5664 | |
| 5665 | exit: |
| 5666 | mbedtls_free( data ); |
| 5667 | return( status ); |
| 5668 | } |
| 5669 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5670 | 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] | 5671 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5672 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5673 | { |
| 5674 | psa_status_t status; |
| 5675 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5676 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5677 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5678 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5679 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5680 | /* Reject any attempt to create a zero-length key so that we don't |
| 5681 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5682 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5683 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5684 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5685 | if( ! operation->can_output_key ) |
| 5686 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5687 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5688 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5689 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5690 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5691 | if( driver != NULL ) |
| 5692 | { |
| 5693 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5694 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5695 | } |
| 5696 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5697 | if( status == PSA_SUCCESS ) |
| 5698 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5699 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5700 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5701 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5702 | } |
| 5703 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5704 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5705 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5706 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5707 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5708 | return( status ); |
| 5709 | } |
| 5710 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5711 | |
| 5712 | |
| 5713 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5714 | /* Key derivation */ |
| 5715 | /****************************************************************/ |
| 5716 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5717 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5718 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5719 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5720 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5721 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5722 | int is_kdf_alg_supported; |
| 5723 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5724 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5725 | * initialisers for this union leave some bytes unspecified.) */ |
| 5726 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5727 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5728 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5729 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5730 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5731 | is_kdf_alg_supported = 1; |
| 5732 | else |
| 5733 | #endif |
| 5734 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5735 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5736 | is_kdf_alg_supported = 1; |
| 5737 | else |
| 5738 | #endif |
| 5739 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5740 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5741 | is_kdf_alg_supported = 1; |
| 5742 | else |
| 5743 | #endif |
| 5744 | is_kdf_alg_supported = 0; |
| 5745 | |
| 5746 | if( is_kdf_alg_supported ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5747 | { |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5748 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5749 | size_t hash_size = PSA_HASH_SIZE( hash_alg ); |
| 5750 | if( hash_size == 0 ) |
| 5751 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5752 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5753 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
Gilles Peskine | ab4b201 | 2019-04-12 15:06:27 +0200 | [diff] [blame] | 5754 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5755 | { |
| 5756 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5757 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5758 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5759 | return( PSA_SUCCESS ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5760 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5761 | |
| 5762 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5763 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5764 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5765 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5766 | 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] | 5767 | psa_algorithm_t alg ) |
| 5768 | { |
| 5769 | psa_status_t status; |
| 5770 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5771 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5772 | return( PSA_ERROR_BAD_STATE ); |
| 5773 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5774 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5775 | return( PSA_ERROR_INVALID_ARGUMENT ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5776 | #ifdef AT_LEAST_ONE_BUILTIN_KDF |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5777 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5778 | { |
| 5779 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5780 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5781 | } |
| 5782 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5783 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5784 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5785 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5786 | #endif |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5787 | else |
| 5788 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5789 | |
| 5790 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5791 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5792 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5793 | } |
| 5794 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 5795 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5796 | 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] | 5797 | psa_algorithm_t hash_alg, |
| 5798 | psa_key_derivation_step_t step, |
| 5799 | const uint8_t *data, |
| 5800 | size_t data_length ) |
| 5801 | { |
| 5802 | psa_status_t status; |
| 5803 | switch( step ) |
| 5804 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5805 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5806 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5807 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5808 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5809 | data, data_length, |
| 5810 | hash_alg ); |
| 5811 | if( status != PSA_SUCCESS ) |
| 5812 | return( status ); |
| 5813 | hkdf->state = HKDF_STATE_STARTED; |
| 5814 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5815 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5816 | /* If no salt was provided, use an empty salt. */ |
| 5817 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5818 | { |
| 5819 | status = psa_hmac_setup_internal( &hkdf->hmac, |
| 5820 | NULL, 0, |
Gilles Peskine | f9ee633 | 2019-04-11 21:22:52 +0200 | [diff] [blame] | 5821 | hash_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5822 | if( status != PSA_SUCCESS ) |
| 5823 | return( status ); |
| 5824 | hkdf->state = HKDF_STATE_STARTED; |
| 5825 | } |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5826 | if( hkdf->state != HKDF_STATE_STARTED ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5827 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5828 | status = psa_hash_update( &hkdf->hmac.hash_ctx, |
| 5829 | data, data_length ); |
| 5830 | if( status != PSA_SUCCESS ) |
| 5831 | return( status ); |
| 5832 | status = psa_hmac_finish_internal( &hkdf->hmac, |
| 5833 | hkdf->prk, |
| 5834 | sizeof( hkdf->prk ) ); |
| 5835 | if( status != PSA_SUCCESS ) |
| 5836 | return( status ); |
| 5837 | hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg ); |
| 5838 | hkdf->block_number = 0; |
| 5839 | hkdf->state = HKDF_STATE_KEYED; |
| 5840 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5841 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5842 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5843 | return( PSA_ERROR_BAD_STATE ); |
| 5844 | if( hkdf->info_set ) |
| 5845 | return( PSA_ERROR_BAD_STATE ); |
| 5846 | hkdf->info_length = data_length; |
| 5847 | if( data_length != 0 ) |
| 5848 | { |
| 5849 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5850 | if( hkdf->info == NULL ) |
| 5851 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5852 | memcpy( hkdf->info, data, data_length ); |
| 5853 | } |
| 5854 | hkdf->info_set = 1; |
| 5855 | return( PSA_SUCCESS ); |
| 5856 | default: |
| 5857 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5858 | } |
| 5859 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5860 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5861 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5862 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5863 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5864 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5865 | const uint8_t *data, |
| 5866 | size_t data_length ) |
| 5867 | { |
| 5868 | if( prf->state != TLS12_PRF_STATE_INIT ) |
| 5869 | return( PSA_ERROR_BAD_STATE ); |
| 5870 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5871 | if( data_length != 0 ) |
| 5872 | { |
| 5873 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5874 | if( prf->seed == NULL ) |
| 5875 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5876 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5877 | memcpy( prf->seed, data, data_length ); |
| 5878 | prf->seed_length = data_length; |
| 5879 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5880 | |
| 5881 | prf->state = TLS12_PRF_STATE_SEED_SET; |
| 5882 | |
| 5883 | return( PSA_SUCCESS ); |
| 5884 | } |
| 5885 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5886 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
| 5887 | psa_algorithm_t hash_alg, |
| 5888 | const uint8_t *data, |
| 5889 | size_t data_length ) |
| 5890 | { |
| 5891 | psa_status_t status; |
| 5892 | if( prf->state != TLS12_PRF_STATE_SEED_SET ) |
| 5893 | return( PSA_ERROR_BAD_STATE ); |
| 5894 | |
| 5895 | status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg ); |
| 5896 | if( status != PSA_SUCCESS ) |
| 5897 | return( status ); |
| 5898 | |
| 5899 | prf->state = TLS12_PRF_STATE_KEY_SET; |
| 5900 | |
| 5901 | return( PSA_SUCCESS ); |
| 5902 | } |
| 5903 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5904 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5905 | const uint8_t *data, |
| 5906 | size_t data_length ) |
| 5907 | { |
| 5908 | if( prf->state != TLS12_PRF_STATE_KEY_SET ) |
| 5909 | return( PSA_ERROR_BAD_STATE ); |
| 5910 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5911 | if( data_length != 0 ) |
| 5912 | { |
| 5913 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5914 | if( prf->label == NULL ) |
| 5915 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5916 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5917 | memcpy( prf->label, data, data_length ); |
| 5918 | prf->label_length = data_length; |
| 5919 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5920 | |
| 5921 | prf->state = TLS12_PRF_STATE_LABEL_SET; |
| 5922 | |
| 5923 | return( PSA_SUCCESS ); |
| 5924 | } |
| 5925 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5926 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
| 5927 | psa_algorithm_t hash_alg, |
| 5928 | psa_key_derivation_step_t step, |
| 5929 | const uint8_t *data, |
| 5930 | size_t data_length ) |
| 5931 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5932 | switch( step ) |
| 5933 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5934 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5935 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5936 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5937 | return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5938 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5939 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5940 | default: |
| 5941 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5942 | } |
| 5943 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5944 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5945 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5946 | |
| 5947 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5948 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5949 | psa_tls12_prf_key_derivation_t *prf, |
| 5950 | psa_algorithm_t hash_alg, |
| 5951 | const uint8_t *data, |
| 5952 | size_t data_length ) |
| 5953 | { |
| 5954 | psa_status_t status; |
| 5955 | uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ]; |
| 5956 | uint8_t *cur = pms; |
| 5957 | |
| 5958 | if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ) |
| 5959 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5960 | |
| 5961 | /* Quoting RFC 4279, Section 2: |
| 5962 | * |
| 5963 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5964 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5965 | * uint16 with the value N, and the PSK itself. |
| 5966 | */ |
| 5967 | |
| 5968 | *cur++ = ( data_length >> 8 ) & 0xff; |
| 5969 | *cur++ = ( data_length >> 0 ) & 0xff; |
| 5970 | memset( cur, 0, data_length ); |
| 5971 | cur += data_length; |
| 5972 | *cur++ = pms[0]; |
| 5973 | *cur++ = pms[1]; |
| 5974 | memcpy( cur, data, data_length ); |
| 5975 | cur += data_length; |
| 5976 | |
| 5977 | status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms ); |
| 5978 | |
| 5979 | mbedtls_platform_zeroize( pms, sizeof( pms ) ); |
| 5980 | return( status ); |
| 5981 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5982 | |
| 5983 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5984 | psa_tls12_prf_key_derivation_t *prf, |
| 5985 | psa_algorithm_t hash_alg, |
| 5986 | psa_key_derivation_step_t step, |
| 5987 | const uint8_t *data, |
| 5988 | size_t data_length ) |
| 5989 | { |
| 5990 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5991 | { |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5992 | return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg, |
| 5993 | data, data_length ) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5994 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5995 | |
| 5996 | return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) ); |
| 5997 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5998 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5999 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6000 | /** Check whether the given key type is acceptable for the given |
| 6001 | * input step of a key derivation. |
| 6002 | * |
| 6003 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 6004 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 6005 | * Both secret and non-secret inputs can alternatively have the type |
| 6006 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 6007 | * that the input was passed as a buffer rather than via a key object. |
| 6008 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6009 | static int psa_key_derivation_check_input_type( |
| 6010 | psa_key_derivation_step_t step, |
| 6011 | psa_key_type_t key_type ) |
| 6012 | { |
| 6013 | switch( step ) |
| 6014 | { |
| 6015 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6016 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 6017 | return( PSA_SUCCESS ); |
| 6018 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6019 | return( PSA_SUCCESS ); |
| 6020 | break; |
| 6021 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 6022 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 6023 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 6024 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6025 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 6026 | return( PSA_SUCCESS ); |
| 6027 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6028 | return( PSA_SUCCESS ); |
| 6029 | break; |
| 6030 | } |
| 6031 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6032 | } |
| 6033 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6034 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6035 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6036 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6037 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6038 | const uint8_t *data, |
| 6039 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6040 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6041 | psa_status_t status; |
| 6042 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 6043 | |
| 6044 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6045 | if( status != PSA_SUCCESS ) |
| 6046 | goto exit; |
| 6047 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 6048 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6049 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6050 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6051 | status = psa_hkdf_input( &operation->ctx.hkdf, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6052 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6053 | step, data, data_length ); |
| 6054 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6055 | else |
| 6056 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 6057 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 6058 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6059 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6060 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
| 6061 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 6062 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6063 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6064 | else |
| 6065 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 6066 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6067 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6068 | { |
| 6069 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
| 6070 | PSA_ALG_HKDF_GET_HASH( kdf_alg ), |
| 6071 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6072 | } |
| 6073 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6074 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6075 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6076 | /* This can't happen unless the operation object was not initialized */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6077 | return( PSA_ERROR_BAD_STATE ); |
| 6078 | } |
| 6079 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6080 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6081 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6082 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6083 | return( status ); |
| 6084 | } |
| 6085 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6086 | psa_status_t psa_key_derivation_input_bytes( |
| 6087 | psa_key_derivation_operation_t *operation, |
| 6088 | psa_key_derivation_step_t step, |
| 6089 | const uint8_t *data, |
| 6090 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6091 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6092 | return( psa_key_derivation_input_internal( operation, step, |
| 6093 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 6094 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6095 | } |
| 6096 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6097 | psa_status_t psa_key_derivation_input_key( |
| 6098 | psa_key_derivation_operation_t *operation, |
| 6099 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6100 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6101 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6102 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6103 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6104 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6105 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6106 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6107 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6108 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6109 | { |
| 6110 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6111 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6112 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6113 | |
| 6114 | /* Passing a key object as a SECRET input unlocks the permission |
| 6115 | * to output to a key object. */ |
| 6116 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6117 | operation->can_output_key = 1; |
| 6118 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6119 | status = psa_key_derivation_input_internal( operation, |
| 6120 | step, slot->attr.type, |
| 6121 | slot->data.key.data, |
| 6122 | slot->data.key.bytes ); |
| 6123 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6124 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6125 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6126 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6127 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6128 | |
| 6129 | |
| 6130 | |
| 6131 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6132 | /* Key agreement */ |
| 6133 | /****************************************************************/ |
| 6134 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6135 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6136 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 6137 | size_t peer_key_length, |
| 6138 | const mbedtls_ecp_keypair *our_key, |
| 6139 | uint8_t *shared_secret, |
| 6140 | size_t shared_secret_size, |
| 6141 | size_t *shared_secret_length ) |
| 6142 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 6143 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6144 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6145 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6146 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6147 | 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] | 6148 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6149 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6150 | status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 6151 | peer_key, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6152 | peer_key_length, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6153 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6154 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6155 | goto exit; |
| 6156 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6157 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6158 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6159 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6160 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6161 | status = mbedtls_to_psa_error( |
| 6162 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 6163 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6164 | goto exit; |
| 6165 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6166 | status = mbedtls_to_psa_error( |
| 6167 | mbedtls_ecdh_calc_secret( &ecdh, |
| 6168 | shared_secret_length, |
| 6169 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6170 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6171 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 6172 | if( status != PSA_SUCCESS ) |
| 6173 | goto exit; |
| 6174 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 6175 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6176 | |
| 6177 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 6178 | if( status != PSA_SUCCESS ) |
| 6179 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6180 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6181 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 6182 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6183 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 6184 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6185 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6186 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6187 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6188 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 6189 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6190 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 6191 | psa_key_slot_t *private_key, |
| 6192 | const uint8_t *peer_key, |
| 6193 | size_t peer_key_length, |
| 6194 | uint8_t *shared_secret, |
| 6195 | size_t shared_secret_size, |
| 6196 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6197 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6198 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6199 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6200 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6201 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6202 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6203 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6204 | mbedtls_ecp_keypair *ecp = NULL; |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6205 | psa_status_t status = psa_load_ecp_representation( |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 6206 | private_key->attr.type, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6207 | private_key->data.key.data, |
| 6208 | private_key->data.key.bytes, |
Steven Cooreman | 7f39187 | 2020-07-30 14:57:44 +0200 | [diff] [blame] | 6209 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6210 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6211 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6212 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6213 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6214 | shared_secret, shared_secret_size, |
| 6215 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 6216 | mbedtls_ecp_keypair_free( ecp ); |
| 6217 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6218 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6219 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6220 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 6221 | (void) private_key; |
| 6222 | (void) peer_key; |
| 6223 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6224 | (void) shared_secret; |
| 6225 | (void) shared_secret_size; |
| 6226 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6227 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6228 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6229 | } |
| 6230 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6231 | /* 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] | 6232 | * to potentially free embedded data structures and wipe confidential data. |
| 6233 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6234 | 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] | 6235 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6236 | psa_key_slot_t *private_key, |
| 6237 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6238 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6239 | { |
| 6240 | psa_status_t status; |
| 6241 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 6242 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6243 | 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] | 6244 | |
| 6245 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 6246 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6247 | status = psa_key_agreement_raw_internal( ka_alg, |
| 6248 | private_key, |
| 6249 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6250 | shared_secret, |
| 6251 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6252 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6253 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6254 | goto exit; |
| 6255 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 6256 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6257 | * the shared secret. A shared secret is permitted wherever a key |
| 6258 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6259 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6260 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6261 | shared_secret, |
| 6262 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6263 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6264 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6265 | return( status ); |
| 6266 | } |
| 6267 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6268 | 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] | 6269 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6270 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6271 | const uint8_t *peer_key, |
| 6272 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +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; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 6276 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6277 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6278 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6279 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6280 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6281 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6282 | if( status != PSA_SUCCESS ) |
| 6283 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6284 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6285 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 6286 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 6287 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6288 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6289 | else |
| 6290 | { |
| 6291 | /* If a private key has been added as SECRET, we allow the derived |
| 6292 | * key material to be used as a key in PSA Crypto. */ |
| 6293 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6294 | operation->can_output_key = 1; |
| 6295 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6296 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6297 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6298 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6299 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6300 | } |
| 6301 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6302 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6303 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6304 | const uint8_t *peer_key, |
| 6305 | size_t peer_key_length, |
| 6306 | uint8_t *output, |
| 6307 | size_t output_size, |
| 6308 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6309 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6310 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6311 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6312 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6313 | |
| 6314 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 6315 | { |
| 6316 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6317 | goto exit; |
| 6318 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6319 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 6320 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6321 | if( status != PSA_SUCCESS ) |
| 6322 | goto exit; |
| 6323 | |
| 6324 | status = psa_key_agreement_raw_internal( alg, slot, |
| 6325 | peer_key, peer_key_length, |
| 6326 | output, output_size, |
| 6327 | output_length ); |
| 6328 | |
| 6329 | exit: |
| 6330 | if( status != PSA_SUCCESS ) |
| 6331 | { |
| 6332 | /* If an error happens and is not handled properly, the output |
| 6333 | * may be used as a key to protect sensitive data. Arrange for such |
| 6334 | * a key to be random, which is likely to result in decryption or |
| 6335 | * verification errors. This is better than filling the buffer with |
| 6336 | * some constant data such as zeros, which would result in the data |
| 6337 | * being protected with a reproducible, easily knowable key. |
| 6338 | */ |
| 6339 | psa_generate_random( output, output_size ); |
| 6340 | *output_length = output_size; |
| 6341 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6342 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6343 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6344 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6345 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6346 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6347 | |
| 6348 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6349 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6350 | /****************************************************************/ |
| 6351 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6352 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6353 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6354 | /** Initialize the PSA random generator. |
| 6355 | */ |
| 6356 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 6357 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6358 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6359 | memset( rng, 0, sizeof( *rng ) ); |
| 6360 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6361 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6362 | /* Set default configuration if |
| 6363 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6364 | if( rng->entropy_init == NULL ) |
| 6365 | rng->entropy_init = mbedtls_entropy_init; |
| 6366 | if( rng->entropy_free == NULL ) |
| 6367 | rng->entropy_free = mbedtls_entropy_free; |
| 6368 | |
| 6369 | rng->entropy_init( &rng->entropy ); |
| 6370 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6371 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6372 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6373 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6374 | mbedtls_entropy_add_source( &rng->entropy, |
| 6375 | mbedtls_nv_seed_poll, NULL, |
| 6376 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6377 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6378 | #endif |
| 6379 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6380 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6381 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6382 | } |
| 6383 | |
| 6384 | /** Deinitialize the PSA random generator. |
| 6385 | */ |
| 6386 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 6387 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6388 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6389 | memset( rng, 0, sizeof( *rng ) ); |
| 6390 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6391 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6392 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6393 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6394 | } |
| 6395 | |
| 6396 | /** Seed the PSA random generator. |
| 6397 | */ |
| 6398 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 6399 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6400 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6401 | /* Do nothing: the external RNG seeds itself. */ |
| 6402 | (void) rng; |
| 6403 | return( PSA_SUCCESS ); |
| 6404 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6405 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6406 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 6407 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6408 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6409 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6410 | } |
| 6411 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6412 | psa_status_t psa_generate_random( uint8_t *output, |
| 6413 | size_t output_size ) |
| 6414 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6415 | GUARD_MODULE_INITIALIZED; |
| 6416 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6417 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6418 | |
| 6419 | size_t output_length = 0; |
| 6420 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 6421 | output, output_size, |
| 6422 | &output_length ); |
| 6423 | if( status != PSA_SUCCESS ) |
| 6424 | return( status ); |
| 6425 | /* Breaking up a request into smaller chunks is currently not supported |
| 6426 | * for the extrernal RNG interface. */ |
| 6427 | if( output_length != output_size ) |
| 6428 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6429 | return( PSA_SUCCESS ); |
| 6430 | |
| 6431 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6432 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6433 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6434 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6435 | size_t request_size = |
| 6436 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6437 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6438 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6439 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 6440 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6441 | if( ret != 0 ) |
| 6442 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6443 | output_size -= request_size; |
| 6444 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6445 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6446 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6447 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6448 | } |
| 6449 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6450 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6451 | * |
| 6452 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6453 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6454 | * PSA API doesn't support passing an explicit state. |
| 6455 | * |
| 6456 | * In the non-external case, psa_generate_random() calls an |
| 6457 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6458 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6459 | * instead of doing this back-and-forth between the PSA API and the |
| 6460 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6461 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6462 | */ |
| 6463 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6464 | int mbedtls_psa_get_random( void *p_rng, |
| 6465 | unsigned char *output, |
| 6466 | size_t output_size ) |
| 6467 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6468 | /* This function takes a pointer to the RNG state because that's what |
| 6469 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6470 | * its own state internally and doesn't let the caller access that state. |
| 6471 | * So we just ignore the state parameter, and in practice we'll pass |
| 6472 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6473 | (void) p_rng; |
| 6474 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6475 | if( status == PSA_SUCCESS ) |
| 6476 | return( 0 ); |
| 6477 | else |
| 6478 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6479 | } |
| 6480 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6481 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6482 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 6483 | #include "mbedtls/entropy_poll.h" |
| 6484 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6485 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6486 | size_t seed_size ) |
| 6487 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6488 | if( global_data.initialized ) |
| 6489 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6490 | |
| 6491 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6492 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6493 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6494 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6495 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6496 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6497 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6498 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6499 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6500 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6501 | static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters, |
| 6502 | size_t domain_parameters_size, |
| 6503 | int *exponent ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6504 | { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6505 | size_t i; |
| 6506 | uint32_t acc = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6507 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6508 | if( domain_parameters_size == 0 ) |
| 6509 | { |
| 6510 | *exponent = 65537; |
| 6511 | return( PSA_SUCCESS ); |
| 6512 | } |
| 6513 | |
| 6514 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 6515 | * support values that fit in a 32-bit integer, which is larger than |
| 6516 | * int on just about every platform anyway. */ |
| 6517 | if( domain_parameters_size > sizeof( acc ) ) |
| 6518 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6519 | for( i = 0; i < domain_parameters_size; i++ ) |
| 6520 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 6521 | if( acc > INT_MAX ) |
| 6522 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6523 | *exponent = acc; |
| 6524 | return( PSA_SUCCESS ); |
| 6525 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6526 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6527 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6528 | static psa_status_t psa_generate_key_internal( |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6529 | psa_key_slot_t *slot, size_t bits, |
| 6530 | const uint8_t *domain_parameters, size_t domain_parameters_size ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6531 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 6532 | psa_key_type_t type = slot->attr.type; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6533 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6534 | if( domain_parameters == NULL && domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6535 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6536 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6537 | if( key_type_is_raw_bytes( type ) ) |
| 6538 | { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6539 | psa_status_t status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6540 | |
| 6541 | status = validate_unstructured_key_bit_size( slot->attr.type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6542 | if( status != PSA_SUCCESS ) |
| 6543 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6544 | |
| 6545 | /* Allocate memory for the key */ |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6546 | status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) ); |
| 6547 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6548 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6549 | |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 6550 | status = psa_generate_random( slot->data.key.data, |
| 6551 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6552 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6553 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6554 | |
| 6555 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6556 | #if defined(MBEDTLS_DES_C) |
| 6557 | if( type == PSA_KEY_TYPE_DES ) |
Steven Cooreman | 71fd80d | 2020-07-07 21:12:27 +0200 | [diff] [blame] | 6558 | psa_des_set_key_parity( slot->data.key.data, |
| 6559 | slot->data.key.bytes ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6560 | #endif /* MBEDTLS_DES_C */ |
| 6561 | } |
| 6562 | else |
| 6563 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6564 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6565 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6566 | { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6567 | mbedtls_rsa_context rsa; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6568 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6569 | int exponent; |
| 6570 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6571 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6572 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6573 | /* Accept only byte-aligned keys, for the same reasons as |
| 6574 | * in psa_import_rsa_key(). */ |
| 6575 | if( bits % 8 != 0 ) |
| 6576 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6577 | status = psa_read_rsa_exponent( domain_parameters, |
| 6578 | domain_parameters_size, |
| 6579 | &exponent ); |
| 6580 | if( status != PSA_SUCCESS ) |
| 6581 | return( status ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6582 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 6583 | ret = mbedtls_rsa_gen_key( &rsa, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6584 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6585 | MBEDTLS_PSA_RANDOM_STATE, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6586 | (unsigned int) bits, |
| 6587 | exponent ); |
| 6588 | if( ret != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6589 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6590 | |
| 6591 | /* Make sure to always have an export representation available */ |
| 6592 | size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits ); |
| 6593 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6594 | status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6595 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6596 | { |
| 6597 | mbedtls_rsa_free( &rsa ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6598 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6599 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6600 | |
| 6601 | status = psa_export_rsa_key( type, |
| 6602 | &rsa, |
| 6603 | slot->data.key.data, |
| 6604 | bytes, |
| 6605 | &slot->data.key.bytes ); |
| 6606 | mbedtls_rsa_free( &rsa ); |
| 6607 | if( status != PSA_SUCCESS ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 6608 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6609 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6610 | } |
| 6611 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6612 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6613 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6614 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6615 | 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] | 6616 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 6617 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type ); |
Gilles Peskine | 4295e8b | 2019-12-02 21:39:10 +0100 | [diff] [blame] | 6618 | mbedtls_ecp_group_id grp_id = |
| 6619 | mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6620 | const mbedtls_ecp_curve_info *curve_info = |
| 6621 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6622 | mbedtls_ecp_keypair ecp; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 6623 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6624 | if( domain_parameters_size != 0 ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6625 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6626 | if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL ) |
| 6627 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6628 | mbedtls_ecp_keypair_init( &ecp ); |
| 6629 | ret = mbedtls_ecp_gen_key( grp_id, &ecp, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6630 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6631 | MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6632 | if( ret != 0 ) |
| 6633 | { |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6634 | mbedtls_ecp_keypair_free( &ecp ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6635 | return( mbedtls_to_psa_error( ret ) ); |
| 6636 | } |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6637 | |
| 6638 | |
| 6639 | /* Make sure to always have an export representation available */ |
| 6640 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6641 | psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes ); |
| 6642 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6643 | { |
| 6644 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6645 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6646 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 6647 | |
| 6648 | status = mbedtls_to_psa_error( |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6649 | mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) ); |
| 6650 | |
| 6651 | mbedtls_ecp_keypair_free( &ecp ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6652 | if( status != PSA_SUCCESS ) { |
| 6653 | memset( slot->data.key.data, 0, bytes ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 6654 | psa_remove_key_data_from_memory( slot ); |
Steven Cooreman | b7f6dea | 2020-08-05 16:07:20 +0200 | [diff] [blame] | 6655 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 6656 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6657 | } |
| 6658 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6659 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6660 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6661 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6662 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6663 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6664 | return( PSA_SUCCESS ); |
| 6665 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6666 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6667 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6668 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6669 | { |
| 6670 | psa_status_t status; |
| 6671 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6672 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6673 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6674 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6675 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6676 | /* Reject any attempt to create a zero-length key so that we don't |
| 6677 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6678 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6679 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6680 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6681 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6682 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6683 | if( status != PSA_SUCCESS ) |
| 6684 | goto exit; |
| 6685 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6686 | status = psa_driver_wrapper_generate_key( attributes, |
| 6687 | slot ); |
| 6688 | if( status != PSA_ERROR_NOT_SUPPORTED || |
| 6689 | psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 6690 | goto exit; |
| 6691 | |
| 6692 | status = psa_generate_key_internal( |
| 6693 | slot, attributes->core.bits, |
| 6694 | attributes->domain_parameters, attributes->domain_parameters_size ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6695 | |
| 6696 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6697 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6698 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6699 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6700 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6701 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6702 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6703 | } |
| 6704 | |
| 6705 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6706 | |
| 6707 | /****************************************************************/ |
| 6708 | /* Module setup */ |
| 6709 | /****************************************************************/ |
| 6710 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6711 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6712 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6713 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6714 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6715 | { |
| 6716 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6717 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6718 | global_data.rng.entropy_init = entropy_init; |
| 6719 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6720 | return( PSA_SUCCESS ); |
| 6721 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6722 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6723 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6724 | void mbedtls_psa_crypto_free( void ) |
| 6725 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6726 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6727 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6728 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6729 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6730 | } |
| 6731 | /* Wipe all remaining data, including configuration. |
| 6732 | * In particular, this sets all state indicator to the value |
| 6733 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6734 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6735 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 6736 | /* Unregister all secure element drivers, so that we restart from |
| 6737 | * a pristine state. */ |
| 6738 | psa_unregister_all_se_drivers( ); |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 6739 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6740 | } |
| 6741 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6742 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6743 | /** Recover a transaction that was interrupted by a power failure. |
| 6744 | * |
| 6745 | * This function is called during initialization, before psa_crypto_init() |
| 6746 | * returns. If this function returns a failure status, the initialization |
| 6747 | * fails. |
| 6748 | */ |
| 6749 | static psa_status_t psa_crypto_recover_transaction( |
| 6750 | const psa_crypto_transaction_t *transaction ) |
| 6751 | { |
| 6752 | switch( transaction->unknown.type ) |
| 6753 | { |
| 6754 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6755 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6756 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6757 | * is implemented. |
| 6758 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6759 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6760 | default: |
| 6761 | /* We found an unsupported transaction in the storage. |
| 6762 | * We don't know what state the storage is in. Give up. */ |
| 6763 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 6764 | } |
| 6765 | } |
| 6766 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6767 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6768 | psa_status_t psa_crypto_init( void ) |
| 6769 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6770 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6771 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6772 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6773 | if( global_data.initialized != 0 ) |
| 6774 | return( PSA_SUCCESS ); |
| 6775 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6776 | /* Initialize and seed the random generator. */ |
| 6777 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6778 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6779 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6780 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6781 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6782 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6783 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6784 | status = psa_initialize_key_slots( ); |
| 6785 | if( status != PSA_SUCCESS ) |
| 6786 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6787 | |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6788 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 6789 | status = psa_init_all_se_drivers( ); |
| 6790 | if( status != PSA_SUCCESS ) |
| 6791 | goto exit; |
| 6792 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 6793 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6794 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6795 | status = psa_crypto_load_transaction( ); |
| 6796 | if( status == PSA_SUCCESS ) |
| 6797 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6798 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6799 | if( status != PSA_SUCCESS ) |
| 6800 | goto exit; |
| 6801 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6802 | } |
| 6803 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6804 | { |
| 6805 | /* There's no transaction to complete. It's all good. */ |
| 6806 | status = PSA_SUCCESS; |
| 6807 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6808 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6809 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6810 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6811 | global_data.initialized = 1; |
| 6812 | |
| 6813 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6814 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6815 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6816 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6817 | } |
| 6818 | |
| 6819 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |