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 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 29 | #include "psa/crypto.h" |
Przemyslaw Stekiel | 705fb0f | 2021-11-24 08:47:29 +0100 | [diff] [blame] | 30 | #include "psa/crypto_values.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 32 | #include "psa_crypto_cipher.h" |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 35 | #include "psa_crypto_driver_wrappers.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 36 | #include "psa_crypto_ecp.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 37 | #include "psa_crypto_hash.h" |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 38 | #include "psa_crypto_mac.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 39 | #include "psa_crypto_rsa.h" |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 40 | #include "psa_crypto_ecp.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 42 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 43 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 44 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 45 | /* Include internal declarations that are useful for implementing persistently |
| 46 | * stored keys. */ |
| 47 | #include "psa_crypto_storage.h" |
| 48 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 49 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 50 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 51 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 52 | #include <stdlib.h> |
| 53 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 54 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 55 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 56 | #define mbedtls_calloc calloc |
| 57 | #define mbedtls_free free |
| 58 | #endif |
| 59 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 60 | #include "mbedtls/aes.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 61 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 62 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 63 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 64 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 65 | #include "mbedtls/chacha20.h" |
| 66 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 67 | #include "mbedtls/cipher.h" |
| 68 | #include "mbedtls/ccm.h" |
| 69 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 70 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 71 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 72 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 73 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 74 | #include "mbedtls/error.h" |
| 75 | #include "mbedtls/gcm.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 76 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 77 | #include "mbedtls/md.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 78 | #include "md_wrap.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 79 | #include "mbedtls/pk.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 80 | #include "pk_wrap.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 81 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 82 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 83 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 84 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 85 | #include "mbedtls/sha1.h" |
| 86 | #include "mbedtls/sha256.h" |
| 87 | #include "mbedtls/sha512.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 88 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 89 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 90 | |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 91 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 92 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
| 93 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 94 | #define BUILTIN_ALG_ANY_HKDF 1 |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 95 | #endif |
| 96 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 97 | /****************************************************************/ |
| 98 | /* Global data, support functions and library management */ |
| 99 | /****************************************************************/ |
| 100 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 101 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 102 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 103 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 106 | /* Values for psa_global_data_t::rng_state */ |
| 107 | #define RNG_NOT_INITIALIZED 0 |
| 108 | #define RNG_INITIALIZED 1 |
| 109 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 110 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 111 | typedef struct |
| 112 | { |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 113 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 114 | unsigned rng_state : 2; |
Gilles Peskine | 2d8a182 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 115 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 116 | } psa_global_data_t; |
| 117 | |
| 118 | static psa_global_data_t global_data; |
| 119 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 120 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 121 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 122 | &global_data.rng.drbg; |
| 123 | #endif |
| 124 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 125 | #define GUARD_MODULE_INITIALIZED \ |
| 126 | if( global_data.initialized == 0 ) \ |
| 127 | return( PSA_ERROR_BAD_STATE ); |
| 128 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 129 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 130 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 131 | /* Mbed TLS error codes can combine a high-level error code and a |
| 132 | * low-level error code. The low-level error usually reflects the |
| 133 | * root cause better, so dispatch on that preferably. */ |
| 134 | int low_level_ret = - ( -ret & 0x007f ); |
| 135 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 136 | { |
| 137 | case 0: |
| 138 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 139 | |
| 140 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 141 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 142 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 143 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 144 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 145 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 146 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 147 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 148 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 149 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 150 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 151 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 152 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 153 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 154 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 155 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 156 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 157 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 158 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 159 | |
| 160 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 161 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 162 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 163 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 164 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 165 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 166 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 167 | |
| 168 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 169 | return( PSA_ERROR_BAD_STATE ); |
| 170 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 171 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 172 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 173 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 174 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 175 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 176 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 177 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 178 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 179 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 180 | return( PSA_ERROR_INVALID_PADDING ); |
| 181 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 182 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 183 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 184 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 185 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 186 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 187 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 188 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 189 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 190 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 191 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 192 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 193 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 194 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 195 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 196 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 197 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 198 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 199 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 200 | |
| 201 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 202 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 203 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 204 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 205 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 206 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 207 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 208 | |
| 209 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 210 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Mateusz Starzyk | f28261f | 2021-09-30 16:39:07 +0200 | [diff] [blame] | 211 | case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL: |
| 212 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 213 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 214 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 215 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 216 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 217 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 218 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 219 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 220 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 221 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 222 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 223 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 224 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 225 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 226 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 227 | #endif |
| 228 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 229 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 230 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 231 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 232 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 233 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 234 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 235 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 236 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 237 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 238 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 239 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 240 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 241 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 242 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 243 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 244 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 245 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 246 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 248 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 249 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 250 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 251 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 252 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 253 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 254 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 255 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 256 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 257 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 258 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 259 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 260 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 261 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 262 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 263 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 264 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 265 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 266 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 267 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 268 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 269 | return( PSA_ERROR_NOT_PERMITTED ); |
| 270 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 271 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 272 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 273 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 274 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 275 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 276 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 277 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | f9f1bdf | 2021-06-23 20:32:27 +0200 | [diff] [blame] | 278 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: |
| 279 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 280 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 281 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 282 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 283 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 284 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 285 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 286 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 287 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 288 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 289 | return( PSA_ERROR_INVALID_PADDING ); |
| 290 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 291 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 292 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 293 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 294 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 295 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 296 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 297 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 298 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 299 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 300 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 301 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 302 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 303 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 304 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 305 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 306 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 307 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 308 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 309 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 310 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 311 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 312 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 313 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 314 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 315 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 316 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 317 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 318 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 319 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 320 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 321 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 322 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 323 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 327 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 328 | |
| 329 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 330 | /****************************************************************/ |
| 331 | /* Key management */ |
| 332 | /****************************************************************/ |
| 333 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 334 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 335 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 336 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 337 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 338 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 339 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 340 | size_t bits, |
| 341 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 342 | { |
| 343 | switch( curve ) |
| 344 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 345 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 346 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 347 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 348 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 349 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 350 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 351 | #endif |
| 352 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 353 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 354 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 355 | #endif |
| 356 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 357 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 358 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 359 | #endif |
| 360 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 361 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 362 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 363 | #endif |
| 364 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 365 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 366 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 367 | case 528: |
| 368 | if( bits_is_sloppy ) |
| 369 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 370 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 371 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 372 | } |
| 373 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 374 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 375 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 376 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 377 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 378 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 379 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 380 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 381 | #endif |
| 382 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 383 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 384 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 385 | #endif |
| 386 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 387 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 388 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 389 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 390 | } |
| 391 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 392 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 393 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 394 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 395 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 396 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 397 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 398 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 399 | case 256: |
| 400 | if( bits_is_sloppy ) |
| 401 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 402 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 403 | #endif |
| 404 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 405 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 406 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 407 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 408 | } |
| 409 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 410 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 411 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 412 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 413 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 414 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 415 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 416 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 417 | #endif |
| 418 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 419 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 420 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 421 | #endif |
| 422 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 423 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 424 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 425 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 426 | } |
| 427 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 428 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 429 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 430 | (void) bits_is_sloppy; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 431 | return( MBEDTLS_ECP_DP_NONE ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 432 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 433 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 434 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 435 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 436 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 437 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 438 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 439 | psa_status_t psa_validate_unstructured_key_bit_size( psa_key_type_t type, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 440 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 441 | { |
| 442 | /* Check that the bit size is acceptable for the key type */ |
| 443 | switch( type ) |
| 444 | { |
| 445 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 447 | case PSA_KEY_TYPE_DERIVE: |
| 448 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 449 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 450 | case PSA_KEY_TYPE_AES: |
| 451 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 452 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 453 | break; |
| 454 | #endif |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 455 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 456 | case PSA_KEY_TYPE_ARIA: |
| 457 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 458 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 459 | break; |
| 460 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 461 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 462 | case PSA_KEY_TYPE_CAMELLIA: |
| 463 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 464 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 465 | break; |
| 466 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 467 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 468 | case PSA_KEY_TYPE_DES: |
| 469 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 470 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 471 | break; |
| 472 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 473 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 474 | case PSA_KEY_TYPE_CHACHA20: |
| 475 | if( bits != 256 ) |
| 476 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 477 | break; |
| 478 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 479 | default: |
| 480 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 481 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 482 | if( bits % 8 != 0 ) |
| 483 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 484 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 485 | return( PSA_SUCCESS ); |
| 486 | } |
| 487 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 488 | /** Check whether a given key type is valid for use with a given MAC algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 489 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 490 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 491 | * when called with the validated \p algorithm and \p key_type is well-defined. |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 492 | * |
| 493 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 494 | * \param[in] key_type The key type of the key to be used with the |
| 495 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 496 | * |
| 497 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 498 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 499 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 500 | * The \p key_type is not valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 501 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 502 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 503 | psa_algorithm_t algorithm, |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 504 | psa_key_type_t key_type ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 505 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 506 | if( PSA_ALG_IS_HMAC( algorithm ) ) |
| 507 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 508 | if( key_type == PSA_KEY_TYPE_HMAC ) |
| 509 | return( PSA_SUCCESS ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 510 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 511 | |
| 512 | if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 513 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 514 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 515 | * key. */ |
| 516 | if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) == |
| 517 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC ) |
| 518 | { |
| 519 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 520 | * the block length (larger than 1) for block ciphers. */ |
| 521 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 ) |
| 522 | return( PSA_SUCCESS ); |
| 523 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 524 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 525 | |
| 526 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 529 | psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 530 | size_t buffer_length ) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 531 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 532 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 533 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 534 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 535 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 536 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 537 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 538 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 539 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 540 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 541 | } |
| 542 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 543 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 544 | const uint8_t* data, |
| 545 | size_t data_length ) |
| 546 | { |
| 547 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 548 | data_length ); |
| 549 | if( status != PSA_SUCCESS ) |
| 550 | return( status ); |
| 551 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 552 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 553 | return( PSA_SUCCESS ); |
| 554 | } |
| 555 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 556 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 557 | const psa_key_attributes_t *attributes, |
| 558 | const uint8_t *data, size_t data_length, |
| 559 | uint8_t *key_buffer, size_t key_buffer_size, |
| 560 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 561 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 562 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 563 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 564 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 565 | /* zero-length keys are never supported. */ |
| 566 | if( data_length == 0 ) |
| 567 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 568 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 569 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 570 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 571 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 572 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 573 | status = psa_validate_unstructured_key_bit_size( attributes->core.type, |
| 574 | *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 575 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 576 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 577 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 578 | /* Copy the key material. */ |
| 579 | memcpy( key_buffer, data, data_length ); |
| 580 | *key_buffer_length = data_length; |
| 581 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 582 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 583 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 584 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 585 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 586 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 587 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 588 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 589 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 590 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 591 | return( mbedtls_psa_ecp_import_key( attributes, |
| 592 | data, data_length, |
| 593 | key_buffer, key_buffer_size, |
| 594 | key_buffer_length, |
| 595 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 596 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 597 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 598 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 599 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 600 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 601 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 602 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 603 | return( mbedtls_psa_rsa_import_key( attributes, |
| 604 | data, data_length, |
| 605 | key_buffer, key_buffer_size, |
| 606 | key_buffer_length, |
| 607 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 608 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 609 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 610 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 611 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 612 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 613 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 614 | } |
| 615 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 616 | /** Calculate the intersection of two algorithm usage policies. |
| 617 | * |
| 618 | * Return 0 (which allows no operation) on incompatibility. |
| 619 | */ |
| 620 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 621 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 622 | psa_algorithm_t alg1, |
| 623 | psa_algorithm_t alg2 ) |
| 624 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 625 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 626 | if( alg1 == alg2 ) |
| 627 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 628 | /* If the policies are from the same hash-and-sign family, check |
| 629 | * if one is a wildcard. If so the other has the specific algorithm. */ |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 630 | if( PSA_ALG_IS_SIGN_HASH( alg1 ) && |
| 631 | PSA_ALG_IS_SIGN_HASH( alg2 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 632 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 633 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 634 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 635 | return( alg2 ); |
| 636 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 637 | return( alg1 ); |
| 638 | } |
| 639 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 640 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 641 | * restrictive tag length. */ |
| 642 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 643 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 644 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 645 | { |
| 646 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 647 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 648 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 649 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 650 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 651 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 652 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 653 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 654 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 655 | alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 656 | } |
| 657 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 658 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 659 | ( alg1_len <= alg2_len ) ) |
| 660 | { |
| 661 | return( alg2 ); |
| 662 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 663 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 664 | ( alg2_len <= alg1_len ) ) |
| 665 | { |
| 666 | return( alg1 ); |
| 667 | } |
| 668 | } |
| 669 | /* If the policies are from the same MAC family, check whether one |
| 670 | * of them is a minimum-MAC-length policy. Calculate the most |
| 671 | * restrictive tag length. */ |
| 672 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 673 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 674 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 675 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 676 | /* Validate the combination of key type and algorithm. Since the base |
| 677 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
| 678 | if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) ) |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 679 | return( 0 ); |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 680 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 681 | /* Get the (exact or at-least) output lengths for both sides of the |
| 682 | * requested intersection. None of the currently supported algorithms |
| 683 | * have an output length dependent on the actual key size, so setting it |
| 684 | * to a bogus value of 0 is currently OK. |
| 685 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 686 | * Note that for at-least-this-length wildcard algorithms, the output |
| 687 | * length is set to the shortest allowed length, which allows us to |
| 688 | * calculate the most restrictive tag length for the intersection. */ |
| 689 | size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 ); |
| 690 | size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 691 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 692 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 693 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 694 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 695 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 696 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 697 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 698 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 699 | |
| 700 | /* If only one is an at-least-this-length policy, the intersection would |
| 701 | * be the other (fixed-length) policy as long as said fixed length is |
| 702 | * equal to or larger than the shortest allowed length. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 703 | if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 704 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 705 | return( ( alg1_len <= alg2_len ) ? alg2 : 0 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 706 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 707 | if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 708 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 709 | return( ( alg2_len <= alg1_len ) ? alg1 : 0 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 710 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 711 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 712 | /* If none of them are wildcards, check whether they define the same tag |
| 713 | * length. This is still possible here when one is default-length and |
| 714 | * the other specific-length. Ensure to always return the |
| 715 | * specific-length version for the intersection. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 716 | if( alg1_len == alg2_len ) |
| 717 | return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 718 | } |
| 719 | /* If the policies are incompatible, allow nothing. */ |
| 720 | return( 0 ); |
| 721 | } |
| 722 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 723 | static int psa_key_algorithm_permits( psa_key_type_t key_type, |
| 724 | psa_algorithm_t policy_alg, |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 725 | psa_algorithm_t requested_alg ) |
| 726 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 727 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 728 | if( requested_alg == policy_alg ) |
| 729 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 730 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 731 | * and requested_alg is the same hash-and-sign family with any hash, |
| 732 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 733 | if( PSA_ALG_IS_SIGN_HASH( requested_alg ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 734 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 735 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 736 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 737 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 738 | } |
| 739 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 740 | * the requested algorithm, check the requested tag length to be |
| 741 | * equal-length or longer than the wildcard-specified length. */ |
| 742 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 743 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 744 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 745 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 746 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 747 | { |
| 748 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 749 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 750 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 751 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 752 | * algorithm, check whether their MAC lengths are compatible. */ |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 753 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 754 | PSA_ALG_IS_MAC( requested_alg ) && |
| 755 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 756 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 757 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 758 | /* Validate the combination of key type and algorithm. Since the policy |
| 759 | * and requested algorithms are the same, we only need this once. */ |
| 760 | if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 761 | return( 0 ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 762 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 763 | /* Get both the requested output length for the algorithm which is to be |
| 764 | * verified, and the default output length for the base algorithm. |
| 765 | * Note that none of the currently supported algorithms have an output |
| 766 | * length dependent on actual key size, so setting it to a bogus value |
| 767 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 768 | size_t requested_output_length = PSA_MAC_LENGTH( |
| 769 | key_type, 0, requested_alg ); |
| 770 | size_t default_output_length = PSA_MAC_LENGTH( |
| 771 | key_type, 0, |
| 772 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 773 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 774 | /* If the policy is default-length, only allow an algorithm with |
| 775 | * a declared exact-length matching the default. */ |
| 776 | if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 777 | return( requested_output_length == default_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 778 | |
| 779 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 780 | * length exactly matches the default length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 781 | if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 && |
| 782 | PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length ) |
| 783 | { |
| 784 | return( 1 ); |
| 785 | } |
| 786 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 787 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 788 | * check for the requested MAC length to be equal to or longer than the |
| 789 | * minimum allowed length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 790 | if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
| 791 | { |
| 792 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 793 | requested_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 794 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 795 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 796 | /* 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] | 797 | * a key derivation with that key agreement should also be allowed. This |
| 798 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 799 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 800 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 801 | { |
| 802 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 803 | policy_alg ); |
| 804 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 805 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 806 | return( 0 ); |
| 807 | } |
| 808 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 809 | /** Test whether a policy permits an algorithm. |
| 810 | * |
| 811 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 812 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 813 | * \note This function requires providing the key type for which the policy is |
| 814 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 815 | * have different properties depending on what kind of cipher it is |
| 816 | * combined with. |
| 817 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 818 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 819 | * allowed by the \p policy. |
| 820 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 821 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 822 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 823 | */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 824 | static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy, |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 825 | psa_key_type_t key_type, |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 826 | psa_algorithm_t alg ) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 827 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 828 | /* '0' is not a valid algorithm */ |
| 829 | if( alg == 0 ) |
| 830 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 831 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 832 | /* A requested algorithm cannot be a wildcard. */ |
| 833 | if( PSA_ALG_IS_WILDCARD( alg ) ) |
| 834 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 835 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 836 | if( psa_key_algorithm_permits( key_type, policy->alg, alg ) || |
| 837 | psa_key_algorithm_permits( key_type, policy->alg2, alg ) ) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 838 | return( PSA_SUCCESS ); |
| 839 | else |
| 840 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 843 | /** Restrict a key policy based on a constraint. |
| 844 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 845 | * \note This function requires providing the key type for which the policy is |
| 846 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 847 | * have different properties depending on what kind of cipher it is |
| 848 | * combined with. |
| 849 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 850 | * \param[in] key_type The key type for which to restrict the policy |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 851 | * \param[in,out] policy The policy to restrict. |
| 852 | * \param[in] constraint The policy constraint to apply. |
| 853 | * |
| 854 | * \retval #PSA_SUCCESS |
| 855 | * \c *policy contains the intersection of the original value of |
| 856 | * \c *policy and \c *constraint. |
| 857 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 858 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 859 | * \c *policy is unchanged. |
| 860 | */ |
| 861 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 862 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 863 | psa_key_policy_t *policy, |
| 864 | const psa_key_policy_t *constraint ) |
| 865 | { |
| 866 | psa_algorithm_t intersection_alg = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 867 | psa_key_policy_algorithm_intersection( key_type, policy->alg, |
| 868 | constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 869 | psa_algorithm_t intersection_alg2 = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 870 | psa_key_policy_algorithm_intersection( key_type, policy->alg2, |
| 871 | constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 872 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 873 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 874 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 875 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 876 | policy->usage &= constraint->usage; |
| 877 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 878 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 879 | return( PSA_SUCCESS ); |
| 880 | } |
| 881 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 882 | /** Get the description of a key given its identifier and policy constraints |
| 883 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 884 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 885 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 886 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 887 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 888 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 889 | * In case of a persistent key, the function loads the description of the key |
| 890 | * into a key slot if not already done. |
| 891 | * |
| 892 | * On success, the returned key slot is locked. It is the responsibility of |
| 893 | * 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] | 894 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 895 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 896 | mbedtls_svc_key_id_t key, |
| 897 | psa_key_slot_t **p_slot, |
| 898 | psa_key_usage_t usage, |
| 899 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 900 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 901 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 902 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 903 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 904 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 905 | if( status != PSA_SUCCESS ) |
| 906 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 907 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 908 | |
| 909 | /* Enforce that usage policy for the key slot contains all the flags |
| 910 | * required by the usage parameter. There is one exception: public |
| 911 | * keys can always be exported, so we treat public key objects as |
| 912 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 913 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 914 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 915 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 916 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 917 | { |
| 918 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 919 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 920 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 921 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 922 | /* Enforce that the usage policy permits the requested algorithm. */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 923 | if( alg != 0 ) |
| 924 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 925 | status = psa_key_policy_permits( &slot->attr.policy, |
| 926 | slot->attr.type, |
| 927 | alg ); |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 928 | if( status != PSA_SUCCESS ) |
| 929 | goto error; |
| 930 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 931 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 932 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 933 | |
| 934 | error: |
| 935 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 936 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 937 | |
| 938 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 939 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 940 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 941 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 942 | * |
| 943 | * A transparent key is a key for which the key material is directly |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 944 | * available, as opposed to a key in a secure element and/or to be used |
| 945 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 946 | * |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 947 | * This is a temporary function that may be used instead of |
| 948 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 949 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 950 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 951 | * On success, the returned key slot is locked. It is the responsibility of the |
| 952 | * 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] | 953 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 954 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 955 | mbedtls_svc_key_id_t key, |
| 956 | psa_key_slot_t **p_slot, |
| 957 | psa_key_usage_t usage, |
| 958 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 959 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 960 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 961 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 962 | if( status != PSA_SUCCESS ) |
| 963 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 964 | |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 965 | if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 966 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 967 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 968 | *p_slot = NULL; |
| 969 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 970 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 971 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 972 | return( PSA_SUCCESS ); |
| 973 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 974 | |
Steven Cooreman | 7ddee7f | 2021-04-07 18:08:30 +0200 | [diff] [blame] | 975 | 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] | 976 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 977 | /* Data pointer will always be either a valid pointer or NULL in an |
| 978 | * initialized slot, so we can just free it. */ |
| 979 | if( slot->key.data != NULL ) |
| 980 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 981 | |
| 982 | mbedtls_free( slot->key.data ); |
| 983 | slot->key.data = NULL; |
| 984 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 985 | |
| 986 | return( PSA_SUCCESS ); |
| 987 | } |
| 988 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 989 | /** Completely wipe a slot in memory, including its policy. |
| 990 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 991 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 992 | { |
| 993 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 994 | |
TRodziewicz | 18cddc0 | 2021-07-13 12:19:15 +0200 | [diff] [blame] | 995 | /* |
| 996 | * As the return error code may not be handled in case of multiple errors, |
TRodziewicz | c9890e9 | 2021-07-14 10:16:26 +0200 | [diff] [blame] | 997 | * do our best to report an unexpected lock counter. Assert with |
| 998 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one: |
| 999 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 1000 | * function is called as part of the execution of a test suite, the |
| 1001 | * execution of the test suite is stopped in error if the assertion fails. |
TRodziewicz | 18cddc0 | 2021-07-13 12:19:15 +0200 | [diff] [blame] | 1002 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1003 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1004 | { |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 1005 | MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1006 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1007 | } |
| 1008 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1009 | /* Multipart operations may still be using the key. This is safe |
| 1010 | * because all multipart operation objects are independent from |
| 1011 | * the key slot: if they need to access the key after the setup |
| 1012 | * phase, they have a copy of the key. Note that this means that |
| 1013 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1014 | /* At this point, key material and other type-specific content has |
| 1015 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1016 | * zeroize because the metadata is not particularly sensitive. */ |
| 1017 | memset( slot, 0, sizeof( *slot ) ); |
| 1018 | return( status ); |
| 1019 | } |
| 1020 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1021 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1022 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1023 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1024 | psa_status_t status; /* status of the last operation */ |
| 1025 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1026 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1027 | psa_se_drv_table_entry_t *driver; |
| 1028 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1029 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1030 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1031 | return( PSA_SUCCESS ); |
| 1032 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1033 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1034 | * 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] | 1035 | * key, this will load the key description from persistent memory if not |
| 1036 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1037 | * the key is operated by an SE or not and this information is needed by |
| 1038 | * the current implementation. |
| 1039 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1040 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1041 | if( status != PSA_SUCCESS ) |
| 1042 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1043 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1044 | /* |
| 1045 | * If the key slot containing the key description is under access by the |
| 1046 | * library (apart from the present access), the key cannot be destroyed |
| 1047 | * yet. For the time being, just return in error. Eventually (to be |
| 1048 | * implemented), the key should be destroyed when all accesses have |
| 1049 | * stopped. |
| 1050 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1051 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1052 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1053 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1054 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1055 | } |
| 1056 | |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1057 | if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) ) |
| 1058 | { |
| 1059 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1060 | * if we attempt it, depending on whether the key is merely read-only |
| 1061 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1062 | * Just do the best we can, which is to wipe the copy in memory |
| 1063 | * (done in this function's cleanup code). */ |
| 1064 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1065 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1068 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1069 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1070 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1071 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1072 | /* For a key in a secure element, we need to do three things: |
| 1073 | * remove the key file in internal storage, destroy the |
| 1074 | * key inside the secure element, and update the driver's |
| 1075 | * persistent data. Start a transaction that will encompass these |
| 1076 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1077 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1078 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1079 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1080 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1081 | status = psa_crypto_save_transaction( ); |
| 1082 | if( status != PSA_SUCCESS ) |
| 1083 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1084 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1085 | /* We should still try to destroy the key in the secure |
| 1086 | * element and the key metadata in storage. This is especially |
| 1087 | * important if the error is that the storage is full. |
| 1088 | * But how to do it exactly without risking an inconsistent |
| 1089 | * state after a reset? |
| 1090 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1091 | */ |
| 1092 | overall_status = status; |
| 1093 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1096 | status = psa_destroy_se_key( driver, |
| 1097 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1098 | if( overall_status == PSA_SUCCESS ) |
| 1099 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1100 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1101 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1102 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1103 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1104 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1105 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1106 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1107 | if( overall_status == PSA_SUCCESS ) |
| 1108 | overall_status = status; |
| 1109 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1110 | /* TODO: other slots may have a copy of the same key. We should |
| 1111 | * invalidate them. |
| 1112 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1113 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1114 | } |
| 1115 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1116 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1117 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1118 | if( driver != NULL ) |
| 1119 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1120 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1121 | if( overall_status == PSA_SUCCESS ) |
| 1122 | overall_status = status; |
| 1123 | status = psa_crypto_stop_transaction( ); |
| 1124 | if( overall_status == PSA_SUCCESS ) |
| 1125 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1126 | } |
| 1127 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1128 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1129 | exit: |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1130 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1131 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1132 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1133 | overall_status = status; |
| 1134 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1135 | } |
| 1136 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1137 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1138 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1139 | static psa_status_t psa_get_rsa_public_exponent( |
| 1140 | const mbedtls_rsa_context *rsa, |
| 1141 | psa_key_attributes_t *attributes ) |
| 1142 | { |
| 1143 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1144 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1145 | uint8_t *buffer = NULL; |
| 1146 | size_t buflen; |
| 1147 | mbedtls_mpi_init( &mpi ); |
| 1148 | |
| 1149 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1150 | if( ret != 0 ) |
| 1151 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1152 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1153 | { |
| 1154 | /* It's the default value, which is reported as an empty string, |
| 1155 | * so there's nothing to do. */ |
| 1156 | goto exit; |
| 1157 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1158 | |
| 1159 | buflen = mbedtls_mpi_size( &mpi ); |
| 1160 | buffer = mbedtls_calloc( 1, buflen ); |
| 1161 | if( buffer == NULL ) |
| 1162 | { |
| 1163 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1164 | goto exit; |
| 1165 | } |
| 1166 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1167 | if( ret != 0 ) |
| 1168 | goto exit; |
| 1169 | attributes->domain_parameters = buffer; |
| 1170 | attributes->domain_parameters_size = buflen; |
| 1171 | |
| 1172 | exit: |
| 1173 | mbedtls_mpi_free( &mpi ); |
| 1174 | if( ret != 0 ) |
| 1175 | mbedtls_free( buffer ); |
| 1176 | return( mbedtls_to_psa_error( ret ) ); |
| 1177 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1178 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1179 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1180 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1181 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1182 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1183 | 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] | 1184 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1185 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1186 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1187 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1188 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1189 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1190 | psa_reset_key_attributes( attributes ); |
| 1191 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1192 | 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] | 1193 | if( status != PSA_SUCCESS ) |
| 1194 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1195 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1196 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1197 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1198 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1199 | |
| 1200 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 512ad81 | 2021-08-24 15:50:05 +0200 | [diff] [blame] | 1201 | if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1202 | psa_set_key_slot_number( attributes, |
| 1203 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1204 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1205 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1206 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1207 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1208 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1209 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1210 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1211 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1212 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1213 | * is not yet implemented. |
| 1214 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1215 | */ |
Ronald Cron | 9b8b69c | 2021-08-24 16:00:51 +0200 | [diff] [blame] | 1216 | if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1217 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1218 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1219 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1220 | status = mbedtls_psa_rsa_load_representation( |
| 1221 | slot->attr.type, |
| 1222 | slot->key.data, |
| 1223 | slot->key.bytes, |
| 1224 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1225 | if( status != PSA_SUCCESS ) |
| 1226 | break; |
| 1227 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1228 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1229 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1230 | mbedtls_rsa_free( rsa ); |
| 1231 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1232 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1233 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1234 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1235 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1236 | default: |
| 1237 | /* Nothing else to do. */ |
| 1238 | break; |
| 1239 | } |
| 1240 | |
| 1241 | if( status != PSA_SUCCESS ) |
| 1242 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1243 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1244 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1245 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1246 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1247 | } |
| 1248 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1249 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1250 | psa_status_t psa_get_key_slot_number( |
| 1251 | const psa_key_attributes_t *attributes, |
| 1252 | psa_key_slot_number_t *slot_number ) |
| 1253 | { |
| 1254 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1255 | { |
| 1256 | *slot_number = attributes->slot_number; |
| 1257 | return( PSA_SUCCESS ); |
| 1258 | } |
| 1259 | else |
| 1260 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1261 | } |
| 1262 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1263 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1264 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1265 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1266 | uint8_t *data, |
| 1267 | size_t data_size, |
| 1268 | size_t *data_length ) |
| 1269 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1270 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1271 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1272 | memcpy( data, key_buffer, key_buffer_size ); |
| 1273 | memset( data + key_buffer_size, 0, |
| 1274 | data_size - key_buffer_size ); |
| 1275 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1276 | return( PSA_SUCCESS ); |
| 1277 | } |
| 1278 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1279 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1280 | const psa_key_attributes_t *attributes, |
| 1281 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1282 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1283 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1284 | psa_key_type_t type = attributes->core.type; |
| 1285 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1286 | if( key_type_is_raw_bytes( type ) || |
| 1287 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1288 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1289 | { |
| 1290 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1291 | key_buffer, key_buffer_size, |
| 1292 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1293 | } |
| 1294 | else |
| 1295 | { |
| 1296 | /* This shouldn't happen in the reference implementation, but |
| 1297 | it is valid for a special-purpose implementation to omit |
| 1298 | support for exporting certain key types. */ |
| 1299 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1304 | uint8_t *data, |
| 1305 | size_t data_size, |
| 1306 | size_t *data_length ) |
| 1307 | { |
| 1308 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1309 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1310 | psa_key_slot_t *slot; |
| 1311 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1312 | /* Reject a zero-length output buffer now, since this can never be a |
| 1313 | * valid key representation. This way we know that data must be a valid |
| 1314 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1315 | if( data_size == 0 ) |
| 1316 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1317 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1318 | /* Set the key to empty now, so that even when there are errors, we always |
| 1319 | * set data_length to a value between 0 and data_size. On error, setting |
| 1320 | * the key to empty is a good choice because an empty key representation is |
| 1321 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1322 | *data_length = 0; |
| 1323 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1324 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1325 | * which don't require any flag, but |
| 1326 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1327 | */ |
| 1328 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1329 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1330 | if( status != PSA_SUCCESS ) |
| 1331 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1332 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1333 | psa_key_attributes_t attributes = { |
| 1334 | .core = slot->attr |
| 1335 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1336 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1337 | slot->key.data, slot->key.bytes, |
| 1338 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1339 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1340 | unlock_status = psa_unlock_key_slot( slot ); |
| 1341 | |
| 1342 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1343 | } |
| 1344 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1345 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1346 | const psa_key_attributes_t *attributes, |
| 1347 | const uint8_t *key_buffer, |
| 1348 | size_t key_buffer_size, |
| 1349 | uint8_t *data, |
| 1350 | size_t data_size, |
| 1351 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1352 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1353 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1354 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1355 | if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) ) |
Moran Peker | 17e36e1 | 2018-05-02 12:55:20 +0300 | [diff] [blame] | 1356 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1357 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1358 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1359 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1360 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1361 | key_buffer, key_buffer_size, |
| 1362 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1363 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1364 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1365 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1366 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1367 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1368 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1369 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1370 | key_buffer, |
| 1371 | key_buffer_size, |
| 1372 | data, |
| 1373 | data_size, |
| 1374 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1375 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1376 | /* We don't know how to convert a private RSA key to public. */ |
| 1377 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1378 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1379 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1380 | } |
| 1381 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1382 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1383 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1384 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1385 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1386 | key_buffer, |
| 1387 | key_buffer_size, |
| 1388 | data, |
| 1389 | data_size, |
| 1390 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1391 | #else |
| 1392 | /* 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] | 1393 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1394 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1395 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1396 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1397 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1398 | else |
| 1399 | { |
| 1400 | /* This shouldn't happen in the reference implementation, but |
| 1401 | it is valid for a special-purpose implementation to omit |
| 1402 | support for exporting certain key types. */ |
| 1403 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1404 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1405 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1406 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1407 | 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] | 1408 | uint8_t *data, |
| 1409 | size_t data_size, |
| 1410 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1411 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1412 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1413 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1414 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1415 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1416 | /* Reject a zero-length output buffer now, since this can never be a |
| 1417 | * valid key representation. This way we know that data must be a valid |
| 1418 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1419 | if( data_size == 0 ) |
| 1420 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1421 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1422 | /* Set the key to empty now, so that even when there are errors, we always |
| 1423 | * set data_length to a value between 0 and data_size. On error, setting |
| 1424 | * the key to empty is a good choice because an empty key representation is |
| 1425 | * unlikely to be accepted anywhere. */ |
| 1426 | *data_length = 0; |
| 1427 | |
| 1428 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1429 | 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] | 1430 | if( status != PSA_SUCCESS ) |
| 1431 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1432 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1433 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1434 | { |
| 1435 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1436 | goto exit; |
| 1437 | } |
| 1438 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1439 | psa_key_attributes_t attributes = { |
| 1440 | .core = slot->attr |
| 1441 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1442 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1443 | &attributes, slot->key.data, slot->key.bytes, |
| 1444 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1445 | |
| 1446 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1447 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1448 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1449 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1450 | } |
| 1451 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1452 | #if defined(static_assert) |
| 1453 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1454 | "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] | 1455 | 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] | 1456 | "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] | 1457 | 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] | 1458 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1459 | #endif |
| 1460 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1461 | /** Validate that a key policy is internally well-formed. |
| 1462 | * |
| 1463 | * This function only rejects invalid policies. It does not validate the |
| 1464 | * consistency of the policy with respect to other attributes of the key |
| 1465 | * such as the key type. |
| 1466 | */ |
| 1467 | 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] | 1468 | { |
| 1469 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1470 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1471 | PSA_KEY_USAGE_ENCRYPT | |
| 1472 | PSA_KEY_USAGE_DECRYPT | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 1473 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1474 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1475 | PSA_KEY_USAGE_SIGN_HASH | |
| 1476 | PSA_KEY_USAGE_VERIFY_HASH | |
Manuel Pégourié-Gonnard | 805251b | 2021-05-04 09:49:59 +0200 | [diff] [blame] | 1477 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1478 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1479 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1480 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1481 | return( PSA_SUCCESS ); |
| 1482 | } |
| 1483 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1484 | /** Validate the internal consistency of key attributes. |
| 1485 | * |
| 1486 | * This function only rejects invalid attribute values. If does not |
| 1487 | * validate the consistency of the attributes with any key data that may |
| 1488 | * be involved in the creation of the key. |
| 1489 | * |
| 1490 | * Call this function early in the key creation process. |
| 1491 | * |
| 1492 | * \param[in] attributes Key attributes for the new key. |
| 1493 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1494 | * NULL for a transparent key. |
| 1495 | * |
| 1496 | */ |
| 1497 | static psa_status_t psa_validate_key_attributes( |
| 1498 | const psa_key_attributes_t *attributes, |
| 1499 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1500 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1501 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1502 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1503 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1504 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1505 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1506 | if( status != PSA_SUCCESS ) |
| 1507 | return( status ); |
| 1508 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1509 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1510 | if( status != PSA_SUCCESS ) |
| 1511 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1512 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1513 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1514 | { |
| 1515 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1516 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1517 | } |
| 1518 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1519 | { |
Ronald Cron | 77e412c | 2021-03-31 17:36:31 +0200 | [diff] [blame] | 1520 | if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) ) |
| 1521 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1522 | } |
| 1523 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1524 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1525 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1526 | return( status ); |
| 1527 | |
| 1528 | /* Refuse to create overly large keys. |
| 1529 | * Note that this doesn't trigger on import if the attributes don't |
| 1530 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1531 | * psa_import_key() needs its own checks. */ |
| 1532 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1533 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1534 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1535 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1536 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1537 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1538 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1539 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1540 | return( PSA_SUCCESS ); |
| 1541 | } |
| 1542 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1543 | /** Prepare a key slot to receive key material. |
| 1544 | * |
| 1545 | * This function allocates a key slot and sets its metadata. |
| 1546 | * |
| 1547 | * If this function fails, call psa_fail_key_creation(). |
| 1548 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1549 | * This function is intended to be used as follows: |
| 1550 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1551 | * it with the specified attributes, and in case of a volatile key assign it |
| 1552 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1553 | * -# Populate the slot with the key material. |
| 1554 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1555 | * In case of failure at any step, stop the sequence and call |
| 1556 | * psa_fail_key_creation(). |
| 1557 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1558 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1559 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1560 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1561 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1562 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1563 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1564 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1565 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1566 | * |
| 1567 | * \retval #PSA_SUCCESS |
| 1568 | * The key slot is ready to receive key material. |
| 1569 | * \return If this function fails, the key slot is an invalid state. |
| 1570 | * 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] | 1571 | */ |
| 1572 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1573 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1574 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1575 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1576 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1577 | { |
| 1578 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1579 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1580 | psa_key_slot_t *slot; |
| 1581 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1582 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1583 | *p_drv = NULL; |
| 1584 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1585 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1586 | if( status != PSA_SUCCESS ) |
| 1587 | return( status ); |
| 1588 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1589 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1590 | if( status != PSA_SUCCESS ) |
| 1591 | return( status ); |
| 1592 | slot = *p_slot; |
| 1593 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1594 | /* We're storing the declared bit-size of the key. It's up to each |
| 1595 | * creation mechanism to verify that this information is correct. |
| 1596 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1597 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1598 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1599 | * volatile key identifier associated to the slot returned to contain its |
| 1600 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1601 | |
| 1602 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1603 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1604 | { |
| 1605 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1606 | slot->attr.id = volatile_key_id; |
| 1607 | #else |
| 1608 | slot->attr.id.key_id = volatile_key_id; |
| 1609 | #endif |
| 1610 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1611 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1612 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1613 | * external-only flags, query `attributes`. Thanks to the check |
| 1614 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1615 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1616 | * may have set. */ |
| 1617 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1618 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1619 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1620 | /* 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] | 1621 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1622 | * create the key file in internal storage, create the |
| 1623 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1624 | * persistent data. This is done by starting a transaction that will |
| 1625 | * encompass these three actions. |
| 1626 | * For registering a volatile key, we just need to find an appropriate |
| 1627 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1628 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1629 | /* The first thing to do is to find a slot number for the new key. |
| 1630 | * We save the slot number in persistent storage as part of the |
| 1631 | * transaction data. It will be needed to recover if the power |
| 1632 | * fails during the key creation process, to clean up on the secure |
| 1633 | * element side after restarting. Obtaining a slot number from the |
| 1634 | * secure element driver updates its persistent state, but we do not yet |
| 1635 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1636 | * 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] | 1637 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1638 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1639 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1640 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1641 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1642 | if( status != PSA_SUCCESS ) |
| 1643 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1644 | |
| 1645 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1646 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1647 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1648 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1649 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1650 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1651 | status = psa_crypto_save_transaction( ); |
| 1652 | if( status != PSA_SUCCESS ) |
| 1653 | { |
| 1654 | (void) psa_crypto_stop_transaction( ); |
| 1655 | return( status ); |
| 1656 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1657 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1658 | |
| 1659 | status = psa_copy_key_material_into_slot( |
| 1660 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1661 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1662 | |
| 1663 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1664 | { |
| 1665 | /* Key registration only makes sense with a secure element. */ |
| 1666 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1667 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1668 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1669 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1670 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1671 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1672 | |
| 1673 | /** Finalize the creation of a key once its key material has been set. |
| 1674 | * |
| 1675 | * This entails writing the key to persistent storage. |
| 1676 | * |
| 1677 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1678 | * See the documentation of psa_start_key_creation() for the intended use |
| 1679 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1680 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1681 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1682 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1683 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1684 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1685 | * \param[in,out] slot Pointer to the slot with key material. |
| 1686 | * \param[in] driver The secure element driver for the key, |
| 1687 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1688 | * \param[out] key On success, identifier of the key. Note that the |
| 1689 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1690 | * |
| 1691 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1692 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1693 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1694 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1695 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1696 | * \retval #PSA_ERROR_DATA_INVALID |
| 1697 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1698 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1699 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1700 | * \return If this function fails, the key slot is an invalid state. |
| 1701 | * 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] | 1702 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1703 | static psa_status_t psa_finish_key_creation( |
| 1704 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1705 | psa_se_drv_table_entry_t *driver, |
| 1706 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1707 | { |
| 1708 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1709 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1710 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1711 | |
| 1712 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1713 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1714 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1715 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1716 | if( driver != NULL ) |
| 1717 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1718 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1719 | psa_key_slot_number_t slot_number = |
| 1720 | psa_key_slot_get_slot_number( slot ) ; |
| 1721 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1722 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1723 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1724 | sizeof( data.slot_number ), |
| 1725 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1726 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1727 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1728 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1729 | (uint8_t*) &data, |
| 1730 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1731 | } |
| 1732 | else |
| 1733 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1734 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1735 | /* Key material is saved in export representation in the slot, so |
| 1736 | * just pass the slot buffer for storage. */ |
| 1737 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1738 | slot->key.data, |
| 1739 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1740 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1741 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1742 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1743 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1744 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1745 | /* Finish the transaction for a key creation. This does not |
| 1746 | * happen when registering an existing key. Detect this case |
| 1747 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1748 | * creation of a persistent key in a secure element requires a transaction, |
| 1749 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1750 | if( driver != NULL && |
| 1751 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1752 | { |
| 1753 | status = psa_save_se_persistent_data( driver ); |
| 1754 | if( status != PSA_SUCCESS ) |
| 1755 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1756 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1757 | return( status ); |
| 1758 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1759 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1760 | } |
| 1761 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1762 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1763 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1764 | { |
| 1765 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1766 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1767 | if( status != PSA_SUCCESS ) |
| 1768 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1769 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1770 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1771 | return( status ); |
| 1772 | } |
| 1773 | |
| 1774 | /** Abort the creation of a key. |
| 1775 | * |
| 1776 | * You may call this function after calling psa_start_key_creation(), |
| 1777 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1778 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1779 | * See the documentation of psa_start_key_creation() for the intended use |
| 1780 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1781 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1782 | * \param[in,out] slot Pointer to the slot with key material. |
| 1783 | * \param[in] driver The secure element driver for the key, |
| 1784 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1785 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1786 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1787 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1788 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1789 | (void) driver; |
| 1790 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1791 | if( slot == NULL ) |
| 1792 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1793 | |
| 1794 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1795 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1796 | * element, and the failure happened later (when saving metadata |
| 1797 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1798 | * element. |
| 1799 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1800 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1801 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1802 | /* Abort the ongoing transaction if any (there may not be one if |
| 1803 | * the creation process failed before starting one, or if the |
| 1804 | * key creation is a registration of a key in a secure element). |
| 1805 | * Earlier functions must already have done what it takes to undo any |
| 1806 | * partial creation. All that's left is to update the transaction data |
| 1807 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1808 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1809 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1810 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1811 | psa_wipe_key_slot( slot ); |
| 1812 | } |
| 1813 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1814 | /** Validate optional attributes during key creation. |
| 1815 | * |
| 1816 | * Some key attributes are optional during key creation. If they are |
| 1817 | * specified in the attributes structure, check that they are consistent |
| 1818 | * with the data in the slot. |
| 1819 | * |
| 1820 | * This function should be called near the end of key creation, after |
| 1821 | * the slot in memory is fully populated but before saving persistent data. |
| 1822 | */ |
| 1823 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1824 | const psa_key_slot_t *slot, |
| 1825 | const psa_key_attributes_t *attributes ) |
| 1826 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1827 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1828 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1829 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1830 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1831 | } |
| 1832 | |
| 1833 | if( attributes->domain_parameters_size != 0 ) |
| 1834 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1835 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1836 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1837 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1838 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1839 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1840 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1841 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1842 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1843 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 1844 | slot->attr.type, |
| 1845 | slot->key.data, |
| 1846 | slot->key.bytes, |
| 1847 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1848 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1849 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1850 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1851 | mbedtls_mpi_init( &actual ); |
| 1852 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1853 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1854 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1855 | mbedtls_rsa_free( rsa ); |
| 1856 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1857 | if( ret != 0 ) |
| 1858 | goto rsa_exit; |
| 1859 | ret = mbedtls_mpi_read_binary( &required, |
| 1860 | attributes->domain_parameters, |
| 1861 | attributes->domain_parameters_size ); |
| 1862 | if( ret != 0 ) |
| 1863 | goto rsa_exit; |
| 1864 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1865 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1866 | rsa_exit: |
| 1867 | mbedtls_mpi_free( &actual ); |
| 1868 | mbedtls_mpi_free( &required ); |
| 1869 | if( ret != 0) |
| 1870 | return( mbedtls_to_psa_error( ret ) ); |
| 1871 | } |
| 1872 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1873 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1874 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1875 | { |
| 1876 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1877 | } |
| 1878 | } |
| 1879 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1880 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1881 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1882 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1883 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1884 | } |
| 1885 | |
| 1886 | return( PSA_SUCCESS ); |
| 1887 | } |
| 1888 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1889 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1890 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1891 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1892 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1893 | { |
| 1894 | psa_status_t status; |
| 1895 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1896 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1897 | size_t bits; |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1898 | size_t storage_size = data_length; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1899 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1900 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1901 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1902 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1903 | * This also rejects any key which might be encoded as an empty string, |
| 1904 | * which is never valid. */ |
| 1905 | if( data_length == 0 ) |
| 1906 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1907 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1908 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1909 | if( data_length > SIZE_MAX / 8 ) |
| 1910 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1911 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1912 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1913 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1914 | if( status != PSA_SUCCESS ) |
| 1915 | goto exit; |
| 1916 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1917 | /* In the case of a transparent key or an opaque key stored in local |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1918 | * storage ( thus not in the case of importing a key in a secure element |
| 1919 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 1920 | * buffer to hold the imported key material. */ |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1921 | if( slot->key.data == NULL ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1922 | { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1923 | if( psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 1924 | { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1925 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
| 1926 | attributes, data, data_length, &storage_size ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1927 | if( status != PSA_SUCCESS ) |
| 1928 | goto exit; |
| 1929 | } |
| 1930 | status = psa_allocate_buffer_to_slot( slot, storage_size ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1931 | if( status != PSA_SUCCESS ) |
| 1932 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1933 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1934 | |
| 1935 | bits = slot->attr.bits; |
| 1936 | status = psa_driver_wrapper_import_key( attributes, |
| 1937 | data, data_length, |
| 1938 | slot->key.data, |
| 1939 | slot->key.bytes, |
| 1940 | &slot->key.bytes, &bits ); |
| 1941 | if( status != PSA_SUCCESS ) |
| 1942 | goto exit; |
| 1943 | |
| 1944 | if( slot->attr.bits == 0 ) |
| 1945 | slot->attr.bits = (psa_key_bits_t) bits; |
| 1946 | else if( bits != slot->attr.bits ) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1947 | { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1948 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1949 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1950 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 1951 | |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1952 | /* Enforce a size limit, and in particular ensure that the bit |
| 1953 | * size fits in its representation type.*/ |
| 1954 | if( bits > PSA_MAX_KEY_BITS ) |
| 1955 | { |
| 1956 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1957 | goto exit; |
| 1958 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1959 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1960 | if( status != PSA_SUCCESS ) |
| 1961 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1962 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1963 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1964 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1965 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1966 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1967 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1968 | return( status ); |
| 1969 | } |
| 1970 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1971 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1972 | psa_status_t mbedtls_psa_register_se_key( |
| 1973 | const psa_key_attributes_t *attributes ) |
| 1974 | { |
| 1975 | psa_status_t status; |
| 1976 | psa_key_slot_t *slot = NULL; |
| 1977 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1978 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1979 | |
| 1980 | /* Leaving attributes unspecified is not currently supported. |
| 1981 | * It could make sense to query the key type and size from the |
| 1982 | * secure element, but not all secure elements support this |
| 1983 | * and the driver HAL doesn't currently support it. */ |
| 1984 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 1985 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1986 | if( psa_get_key_bits( attributes ) == 0 ) |
| 1987 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1988 | |
| 1989 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1990 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1991 | if( status != PSA_SUCCESS ) |
| 1992 | goto exit; |
| 1993 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1994 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1995 | |
| 1996 | exit: |
| 1997 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1998 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1999 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2000 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2001 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2002 | return( status ); |
| 2003 | } |
| 2004 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2005 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2006 | 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] | 2007 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2008 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2009 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2010 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2011 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2012 | psa_key_slot_t *source_slot = NULL; |
| 2013 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2014 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2015 | psa_se_drv_table_entry_t *driver = NULL; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2016 | size_t storage_size = 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2017 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2018 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2019 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2020 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2021 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2022 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2023 | goto exit; |
| 2024 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2025 | status = psa_validate_optional_attributes( source_slot, |
| 2026 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2027 | if( status != PSA_SUCCESS ) |
| 2028 | goto exit; |
| 2029 | |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2030 | /* The target key type and number of bits have been validated by |
| 2031 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2032 | * equal to the ones of the source key. So it is safe to inherit |
| 2033 | * them from the source key now." |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2034 | * */ |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2035 | actual_attributes.core.bits = source_slot->attr.bits; |
| 2036 | actual_attributes.core.type = source_slot->attr.type; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2037 | |
| 2038 | |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 2039 | status = psa_restrict_key_policy( source_slot->attr.type, |
| 2040 | &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2041 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2042 | if( status != PSA_SUCCESS ) |
| 2043 | goto exit; |
| 2044 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2045 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2046 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2047 | if( status != PSA_SUCCESS ) |
| 2048 | goto exit; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2049 | if( PSA_KEY_LIFETIME_GET_LOCATION( target_slot->attr.lifetime ) != |
| 2050 | PSA_KEY_LIFETIME_GET_LOCATION( source_slot->attr.lifetime ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2051 | { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2052 | /* |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2053 | * If the source and target keys are stored in different locations, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2054 | * the source key would need to be exported as plaintext and re-imported |
| 2055 | * in the other location. This has security implications which have not |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2056 | * been fully mapped. For now, this can be achieved through |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2057 | * appropriate API invocations from the application, if needed. |
| 2058 | * */ |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2059 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2060 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2061 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2062 | /* |
| 2063 | * When the source and target keys are within the same location, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2064 | * - For transparent keys it is a blind copy without any driver invocation, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2065 | * - For opaque keys this translates to an invocation of the drivers' |
| 2066 | * copy_key entry point through the dispatch layer. |
| 2067 | * */ |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2068 | if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) ) |
| 2069 | { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2070 | status = psa_driver_wrapper_get_key_buffer_size( &actual_attributes, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2071 | &storage_size ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2072 | if( status != PSA_SUCCESS ) |
| 2073 | goto exit; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2074 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2075 | status = psa_allocate_buffer_to_slot( target_slot, storage_size ); |
| 2076 | if( status != PSA_SUCCESS ) |
| 2077 | goto exit; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2078 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2079 | status = psa_driver_wrapper_copy_key( &actual_attributes, |
| 2080 | source_slot->key.data, |
| 2081 | source_slot->key.bytes, |
| 2082 | target_slot->key.data, |
| 2083 | target_slot->key.bytes, |
| 2084 | &target_slot->key.bytes ); |
| 2085 | if( status != PSA_SUCCESS ) |
| 2086 | goto exit; |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2087 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2088 | else |
| 2089 | { |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2090 | status = psa_copy_key_material_into_slot( target_slot, |
| 2091 | source_slot->key.data, |
| 2092 | source_slot->key.bytes ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2093 | if( status != PSA_SUCCESS ) |
| 2094 | goto exit; |
| 2095 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2096 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2097 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2098 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2099 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2100 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2101 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2102 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2103 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2106 | |
| 2107 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2108 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2109 | /* Message digests */ |
| 2110 | /****************************************************************/ |
| 2111 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2112 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2113 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2114 | /* Aborting a non-active operation is allowed */ |
| 2115 | if( operation->id == 0 ) |
| 2116 | return( PSA_SUCCESS ); |
| 2117 | |
| 2118 | psa_status_t status = psa_driver_wrapper_hash_abort( operation ); |
| 2119 | operation->id = 0; |
| 2120 | |
| 2121 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2122 | } |
| 2123 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2124 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2125 | psa_algorithm_t alg ) |
| 2126 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2127 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2128 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2129 | /* A context must be freshly initialized before it can be set up. */ |
| 2130 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2131 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2132 | status = PSA_ERROR_BAD_STATE; |
| 2133 | goto exit; |
| 2134 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2135 | |
| 2136 | if( !PSA_ALG_IS_HASH( alg ) ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2137 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2138 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2139 | goto exit; |
| 2140 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2141 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2142 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2143 | * directly zeroes the int-sized dummy member of the context union. */ |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2144 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 2145 | |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2146 | status = psa_driver_wrapper_hash_setup( operation, alg ); |
| 2147 | |
| 2148 | exit: |
| 2149 | if( status != PSA_SUCCESS ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2150 | psa_hash_abort( operation ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2151 | |
| 2152 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2156 | const uint8_t *input, |
| 2157 | size_t input_length ) |
| 2158 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2159 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2160 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2161 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2162 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2163 | status = PSA_ERROR_BAD_STATE; |
| 2164 | goto exit; |
| 2165 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2166 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2167 | /* Don't require hash implementations to behave correctly on a |
| 2168 | * zero-length input, which may have an invalid pointer. */ |
| 2169 | if( input_length == 0 ) |
| 2170 | return( PSA_SUCCESS ); |
| 2171 | |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2172 | status = psa_driver_wrapper_hash_update( operation, input, input_length ); |
| 2173 | |
| 2174 | exit: |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2175 | if( status != PSA_SUCCESS ) |
| 2176 | psa_hash_abort( operation ); |
| 2177 | |
| 2178 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2182 | uint8_t *hash, |
| 2183 | size_t hash_size, |
| 2184 | size_t *hash_length ) |
| 2185 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2186 | *hash_length = 0; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2187 | if( operation->id == 0 ) |
| 2188 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2189 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2190 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2191 | operation, hash, hash_size, hash_length ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2192 | psa_hash_abort( operation ); |
| 2193 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2194 | } |
| 2195 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2196 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2197 | const uint8_t *hash, |
| 2198 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2199 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2200 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2201 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2202 | psa_status_t status = psa_hash_finish( |
| 2203 | operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2204 | actual_hash, sizeof( actual_hash ), |
| 2205 | &actual_hash_length ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2206 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2207 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2208 | goto exit; |
| 2209 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2210 | if( actual_hash_length != hash_length ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2211 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2212 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2213 | goto exit; |
| 2214 | } |
| 2215 | |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2216 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2217 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2218 | |
| 2219 | exit: |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2220 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2221 | if( status != PSA_SUCCESS ) |
| 2222 | psa_hash_abort(operation); |
| 2223 | |
| 2224 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2225 | } |
| 2226 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2227 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2228 | const uint8_t *input, size_t input_length, |
| 2229 | uint8_t *hash, size_t hash_size, |
| 2230 | size_t *hash_length ) |
| 2231 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2232 | *hash_length = 0; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2233 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2234 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2235 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2236 | return( psa_driver_wrapper_hash_compute( alg, input, input_length, |
| 2237 | hash, hash_size, hash_length ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2241 | const uint8_t *input, size_t input_length, |
| 2242 | const uint8_t *hash, size_t hash_length ) |
| 2243 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2244 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2245 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2246 | |
| 2247 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2248 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2249 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2250 | psa_status_t status = psa_driver_wrapper_hash_compute( |
| 2251 | alg, input, input_length, |
| 2252 | actual_hash, sizeof(actual_hash), |
| 2253 | &actual_hash_length ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2254 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2255 | goto exit; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2256 | if( actual_hash_length != hash_length ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2257 | { |
| 2258 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2259 | goto exit; |
| 2260 | } |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2261 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2262 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2263 | |
| 2264 | exit: |
| 2265 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
| 2266 | return( status ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2267 | } |
| 2268 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2269 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2270 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2271 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2272 | if( source_operation->id == 0 || |
| 2273 | target_operation->id != 0 ) |
| 2274 | { |
| 2275 | return( PSA_ERROR_BAD_STATE ); |
| 2276 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2277 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2278 | psa_status_t status = psa_driver_wrapper_hash_clone( source_operation, |
| 2279 | target_operation ); |
| 2280 | if( status != PSA_SUCCESS ) |
| 2281 | psa_hash_abort( target_operation ); |
| 2282 | |
| 2283 | return( status ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2284 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2285 | |
| 2286 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2287 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2288 | /* MAC */ |
| 2289 | /****************************************************************/ |
| 2290 | |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2291 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2292 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2293 | /* Aborting a non-active operation is allowed */ |
| 2294 | if( operation->id == 0 ) |
| 2295 | return( PSA_SUCCESS ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2296 | |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2297 | psa_status_t status = psa_driver_wrapper_mac_abort( operation ); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2298 | operation->mac_size = 0; |
| 2299 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2300 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2301 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2302 | return( status ); |
| 2303 | } |
| 2304 | |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2305 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2306 | psa_algorithm_t alg, |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2307 | const psa_key_attributes_t *attributes, |
| 2308 | uint8_t *mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2309 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2310 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2311 | psa_key_type_t key_type = psa_get_key_type( attributes ); |
| 2312 | size_t key_bits = psa_get_key_bits( attributes ); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2313 | |
Ronald Cron | 28ea050 | 2021-06-17 16:10:24 +0200 | [diff] [blame] | 2314 | if( ! PSA_ALG_IS_MAC( alg ) ) |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2315 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2316 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2317 | /* Validate the combination of key type and algorithm */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2318 | status = psa_mac_key_can_do( alg, key_type ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2319 | if( status != PSA_SUCCESS ) |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2320 | return( status ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2321 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2322 | /* Get the output length for the algorithm and key combination */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2323 | *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2324 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2325 | if( *mac_size < 4 ) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2326 | { |
| 2327 | /* A very short MAC is too short for security since it can be |
| 2328 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2329 | * so we make this our minimum, even though 32 bits is still |
| 2330 | * too small for security. */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2331 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2332 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2333 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2334 | if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits, |
| 2335 | PSA_ALG_FULL_LENGTH_MAC( alg ) ) ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2336 | { |
| 2337 | /* It's impossible to "truncate" to a larger length than the full length |
| 2338 | * of the algorithm. */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2339 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2340 | } |
| 2341 | |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2342 | if( *mac_size > PSA_MAC_MAX_SIZE ) |
| 2343 | { |
| 2344 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2345 | * that is disabled in the compile-time configuration. The result can |
| 2346 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2347 | * configuration into account. In this case, force a return of |
| 2348 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2349 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2350 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2351 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2352 | * systematically generated tests. */ |
| 2353 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2354 | } |
| 2355 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2356 | return( PSA_SUCCESS ); |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2357 | } |
| 2358 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2359 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
| 2360 | mbedtls_svc_key_id_t key, |
| 2361 | psa_algorithm_t alg, |
| 2362 | int is_sign ) |
| 2363 | { |
| 2364 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2365 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2366 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2367 | |
| 2368 | /* A context must be freshly initialized before it can be set up. */ |
| 2369 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2370 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2371 | status = PSA_ERROR_BAD_STATE; |
| 2372 | goto exit; |
| 2373 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2374 | |
| 2375 | status = psa_get_and_lock_key_slot_with_policy( |
| 2376 | key, |
| 2377 | &slot, |
Mateusz Starzyk | ce0e6a9 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2378 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2379 | alg ); |
| 2380 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2381 | goto exit; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2382 | |
| 2383 | psa_key_attributes_t attributes = { |
| 2384 | .core = slot->attr |
| 2385 | }; |
| 2386 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2387 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2388 | &operation->mac_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2389 | if( status != PSA_SUCCESS ) |
| 2390 | goto exit; |
| 2391 | |
| 2392 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2393 | /* Dispatch the MAC setup call with validated input */ |
| 2394 | if( is_sign ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2395 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2396 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 2397 | &attributes, |
| 2398 | slot->key.data, |
| 2399 | slot->key.bytes, |
| 2400 | alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2401 | } |
| 2402 | else |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2403 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2404 | status = psa_driver_wrapper_mac_verify_setup( operation, |
| 2405 | &attributes, |
| 2406 | slot->key.data, |
| 2407 | slot->key.bytes, |
| 2408 | alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2409 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2410 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2411 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2412 | if( status != PSA_SUCCESS ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2413 | psa_mac_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2414 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2415 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2416 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2417 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2418 | } |
| 2419 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2420 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2421 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2422 | psa_algorithm_t alg ) |
| 2423 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2424 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2428 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2429 | psa_algorithm_t alg ) |
| 2430 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2431 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2432 | } |
| 2433 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2434 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2435 | const uint8_t *input, |
| 2436 | size_t input_length ) |
| 2437 | { |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2438 | if( operation->id == 0 ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2439 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2440 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2441 | /* Don't require hash implementations to behave correctly on a |
| 2442 | * zero-length input, which may have an invalid pointer. */ |
| 2443 | if( input_length == 0 ) |
| 2444 | return( PSA_SUCCESS ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2445 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2446 | psa_status_t status = psa_driver_wrapper_mac_update( operation, |
| 2447 | input, input_length ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2448 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2449 | psa_mac_abort( operation ); |
| 2450 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2451 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2452 | } |
| 2453 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2454 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2455 | uint8_t *mac, |
| 2456 | size_t mac_size, |
| 2457 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2458 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2459 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2460 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2461 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2462 | if( operation->id == 0 ) |
Dave Rodgman | 8036bdd | 2021-06-24 16:19:08 +0100 | [diff] [blame] | 2463 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2464 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2465 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2466 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2467 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2468 | if( ! operation->is_sign ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2469 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2470 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2471 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2472 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2473 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2474 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2475 | * once all the error checks are done. */ |
| 2476 | if( operation->mac_size == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2477 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2478 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2479 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2480 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2481 | |
| 2482 | if( mac_size < operation->mac_size ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2483 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2484 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2485 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2486 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2487 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2488 | status = psa_driver_wrapper_mac_sign_finish( operation, |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2489 | mac, operation->mac_size, |
| 2490 | mac_length ); |
| 2491 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2492 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2493 | /* In case of success, set the potential excess room in the output buffer |
| 2494 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2495 | * In case of error, set the output length and content to a safe default, |
| 2496 | * such that in case the caller misses an error check, the output would be |
| 2497 | * an unachievable MAC. |
| 2498 | */ |
| 2499 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2500 | { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2501 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2502 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2503 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2504 | |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2505 | if( mac_size > operation->mac_size ) |
| 2506 | memset( &mac[operation->mac_size], '!', |
| 2507 | mac_size - operation->mac_size ); |
| 2508 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2509 | abort_status = psa_mac_abort( operation ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2510 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2511 | return( status == PSA_SUCCESS ? abort_status : status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2512 | } |
| 2513 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2514 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2515 | const uint8_t *mac, |
| 2516 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2517 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2518 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2519 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2520 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2521 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2522 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2523 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2524 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2525 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2526 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2527 | if( operation->is_sign ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2528 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2529 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2530 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2531 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2532 | |
| 2533 | if( operation->mac_size != mac_length ) |
| 2534 | { |
| 2535 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2536 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2537 | } |
| 2538 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2539 | status = psa_driver_wrapper_mac_verify_finish( operation, |
| 2540 | mac, mac_length ); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2541 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2542 | exit: |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2543 | abort_status = psa_mac_abort( operation ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2544 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2545 | return( status == PSA_SUCCESS ? abort_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2546 | } |
| 2547 | |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2548 | static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key, |
| 2549 | psa_algorithm_t alg, |
| 2550 | const uint8_t *input, |
| 2551 | size_t input_length, |
| 2552 | uint8_t *mac, |
| 2553 | size_t mac_size, |
| 2554 | size_t *mac_length, |
| 2555 | int is_sign ) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2556 | { |
| 2557 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2558 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2559 | psa_key_slot_t *slot; |
| 2560 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2561 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2562 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | ce0e6a9 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2563 | key, |
| 2564 | &slot, |
| 2565 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2566 | alg ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2567 | if( status != PSA_SUCCESS ) |
| 2568 | goto exit; |
| 2569 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2570 | psa_key_attributes_t attributes = { |
| 2571 | .core = slot->attr |
| 2572 | }; |
| 2573 | |
| 2574 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2575 | &operation_mac_size ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2576 | if( status != PSA_SUCCESS ) |
| 2577 | goto exit; |
| 2578 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2579 | if( mac_size < operation_mac_size ) |
| 2580 | { |
| 2581 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2582 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | status = psa_driver_wrapper_mac_compute( |
| 2586 | &attributes, |
| 2587 | slot->key.data, slot->key.bytes, |
| 2588 | alg, |
| 2589 | input, input_length, |
| 2590 | mac, operation_mac_size, mac_length ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2591 | |
| 2592 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2593 | /* In case of success, set the potential excess room in the output buffer |
| 2594 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2595 | * In case of error, set the output length and content to a safe default, |
| 2596 | * such that in case the caller misses an error check, the output would be |
| 2597 | * an unachievable MAC. |
| 2598 | */ |
| 2599 | if( status != PSA_SUCCESS ) |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2600 | { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2601 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2602 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2603 | } |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2604 | if( mac_size > operation_mac_size ) |
| 2605 | memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2606 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2607 | unlock_status = psa_unlock_key_slot( slot ); |
| 2608 | |
| 2609 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2610 | } |
| 2611 | |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2612 | psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key, |
| 2613 | psa_algorithm_t alg, |
| 2614 | const uint8_t *input, |
| 2615 | size_t input_length, |
| 2616 | uint8_t *mac, |
| 2617 | size_t mac_size, |
| 2618 | size_t *mac_length) |
| 2619 | { |
| 2620 | return( psa_mac_compute_internal( key, alg, |
| 2621 | input, input_length, |
| 2622 | mac, mac_size, mac_length, 1 ) ); |
| 2623 | } |
| 2624 | |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2625 | psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key, |
| 2626 | psa_algorithm_t alg, |
| 2627 | const uint8_t *input, |
| 2628 | size_t input_length, |
| 2629 | const uint8_t *mac, |
| 2630 | size_t mac_length) |
| 2631 | { |
| 2632 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2633 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2634 | size_t actual_mac_length; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2635 | |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2636 | status = psa_mac_compute_internal( key, alg, |
| 2637 | input, input_length, |
| 2638 | actual_mac, sizeof( actual_mac ), |
| 2639 | &actual_mac_length, 0 ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2640 | if( status != PSA_SUCCESS ) |
| 2641 | goto exit; |
| 2642 | |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2643 | if( mac_length != actual_mac_length ) |
| 2644 | { |
| 2645 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2646 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2647 | } |
| 2648 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 ) |
| 2649 | { |
| 2650 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2651 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2652 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2653 | |
| 2654 | exit: |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2655 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2656 | |
| 2657 | return ( status ); |
| 2658 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2659 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2660 | /****************************************************************/ |
| 2661 | /* Asymmetric cryptography */ |
| 2662 | /****************************************************************/ |
| 2663 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2664 | static psa_status_t psa_sign_verify_check_alg( int input_is_message, |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2665 | psa_algorithm_t alg ) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2666 | { |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2667 | if( input_is_message ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2668 | { |
| 2669 | if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) ) |
| 2670 | return( PSA_ERROR_INVALID_ARGUMENT ); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2671 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2672 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2673 | { |
| 2674 | if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) ) |
| 2675 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2676 | } |
| 2677 | } |
| 2678 | else |
| 2679 | { |
Mateusz Starzyk | e6d3eda | 2021-08-26 11:46:14 +0200 | [diff] [blame] | 2680 | if( ! PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2681 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2682 | } |
| 2683 | |
| 2684 | return( PSA_SUCCESS ); |
| 2685 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2686 | |
| 2687 | static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key, |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2688 | int input_is_message, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2689 | psa_algorithm_t alg, |
| 2690 | const uint8_t * input, |
| 2691 | size_t input_length, |
| 2692 | uint8_t * signature, |
| 2693 | size_t signature_size, |
| 2694 | size_t * signature_length ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2695 | { |
| 2696 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2697 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2698 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2699 | |
| 2700 | *signature_length = 0; |
| 2701 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2702 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2703 | if( status != PSA_SUCCESS ) |
| 2704 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2705 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2706 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2707 | * that signature must be a valid pointer. (On the other hand, the input |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2708 | * buffer can in principle be empty since it doesn't actually have |
| 2709 | * to be a hash.) */ |
| 2710 | if( signature_size == 0 ) |
| 2711 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2712 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2713 | status = psa_get_and_lock_key_slot_with_policy( |
| 2714 | key, &slot, |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2715 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2716 | PSA_KEY_USAGE_SIGN_HASH, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2717 | alg ); |
| 2718 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2719 | if( status != PSA_SUCCESS ) |
| 2720 | goto exit; |
| 2721 | |
| 2722 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
| 2723 | { |
| 2724 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2725 | goto exit; |
| 2726 | } |
| 2727 | |
| 2728 | psa_key_attributes_t attributes = { |
| 2729 | .core = slot->attr |
| 2730 | }; |
| 2731 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2732 | if( input_is_message ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2733 | { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2734 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2735 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2736 | alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2737 | signature, signature_size, signature_length ); |
| 2738 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2739 | else |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2740 | { |
| 2741 | |
| 2742 | status = psa_driver_wrapper_sign_hash( |
| 2743 | &attributes, slot->key.data, slot->key.bytes, |
| 2744 | alg, input, input_length, |
| 2745 | signature, signature_size, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2746 | } |
| 2747 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2748 | |
| 2749 | exit: |
| 2750 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2751 | * the trailing part on success) with something that isn't a valid signature |
| 2752 | * (barring an attack on the signature and deliberately-crafted input), |
| 2753 | * in case the caller doesn't check the return status properly. */ |
| 2754 | if( status == PSA_SUCCESS ) |
| 2755 | memset( signature + *signature_length, '!', |
| 2756 | signature_size - *signature_length ); |
| 2757 | else |
| 2758 | memset( signature, '!', signature_size ); |
| 2759 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2760 | * memset because signature may be NULL in this case. */ |
| 2761 | |
| 2762 | unlock_status = psa_unlock_key_slot( slot ); |
| 2763 | |
| 2764 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 2765 | } |
| 2766 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2767 | static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key, |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2768 | int input_is_message, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2769 | psa_algorithm_t alg, |
| 2770 | const uint8_t * input, |
| 2771 | size_t input_length, |
| 2772 | const uint8_t * signature, |
| 2773 | size_t signature_length ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2774 | { |
| 2775 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2776 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2777 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2778 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2779 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2780 | if( status != PSA_SUCCESS ) |
| 2781 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2782 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2783 | status = psa_get_and_lock_key_slot_with_policy( |
| 2784 | key, &slot, |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2785 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2786 | PSA_KEY_USAGE_VERIFY_HASH, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2787 | alg ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2788 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2789 | if( status != PSA_SUCCESS ) |
| 2790 | return( status ); |
| 2791 | |
| 2792 | psa_key_attributes_t attributes = { |
| 2793 | .core = slot->attr |
| 2794 | }; |
| 2795 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2796 | if( input_is_message ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2797 | { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2798 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2799 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2800 | alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2801 | signature, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2802 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2803 | else |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2804 | { |
| 2805 | status = psa_driver_wrapper_verify_hash( |
| 2806 | &attributes, slot->key.data, slot->key.bytes, |
| 2807 | alg, input, input_length, |
| 2808 | signature, signature_length ); |
| 2809 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2810 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2811 | unlock_status = psa_unlock_key_slot( slot ); |
| 2812 | |
| 2813 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2814 | |
| 2815 | } |
| 2816 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2817 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2818 | const psa_key_attributes_t *attributes, |
| 2819 | const uint8_t *key_buffer, |
| 2820 | size_t key_buffer_size, |
| 2821 | psa_algorithm_t alg, |
| 2822 | const uint8_t *input, |
| 2823 | size_t input_length, |
| 2824 | uint8_t *signature, |
| 2825 | size_t signature_size, |
| 2826 | size_t *signature_length ) |
| 2827 | { |
| 2828 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2829 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2830 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2831 | { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2832 | size_t hash_length; |
| 2833 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2834 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2835 | status = psa_driver_wrapper_hash_compute( |
| 2836 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2837 | input, input_length, |
| 2838 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2839 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2840 | if( status != PSA_SUCCESS ) |
| 2841 | return status; |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2842 | |
| 2843 | return psa_driver_wrapper_sign_hash( |
| 2844 | attributes, key_buffer, key_buffer_size, |
| 2845 | alg, hash, hash_length, |
| 2846 | signature, signature_size, signature_length ); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2847 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2848 | |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2849 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2850 | } |
| 2851 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2852 | psa_status_t psa_sign_message( mbedtls_svc_key_id_t key, |
| 2853 | psa_algorithm_t alg, |
| 2854 | const uint8_t * input, |
| 2855 | size_t input_length, |
| 2856 | uint8_t * signature, |
| 2857 | size_t signature_size, |
| 2858 | size_t * signature_length ) |
| 2859 | { |
| 2860 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2861 | key, 1, alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2862 | signature, signature_size, signature_length ); |
| 2863 | } |
| 2864 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2865 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2866 | const psa_key_attributes_t *attributes, |
| 2867 | const uint8_t *key_buffer, |
| 2868 | size_t key_buffer_size, |
| 2869 | psa_algorithm_t alg, |
| 2870 | const uint8_t *input, |
| 2871 | size_t input_length, |
| 2872 | const uint8_t *signature, |
| 2873 | size_t signature_length ) |
| 2874 | { |
| 2875 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2876 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2877 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2878 | { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2879 | size_t hash_length; |
| 2880 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2881 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2882 | status = psa_driver_wrapper_hash_compute( |
| 2883 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2884 | input, input_length, |
| 2885 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2886 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2887 | if( status != PSA_SUCCESS ) |
| 2888 | return status; |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2889 | |
| 2890 | return psa_driver_wrapper_verify_hash( |
| 2891 | attributes, key_buffer, key_buffer_size, |
| 2892 | alg, hash, hash_length, |
| 2893 | signature, signature_length ); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2894 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2895 | |
gabor-mezei-arm | 474a35f | 2021-05-05 13:59:01 +0200 | [diff] [blame] | 2896 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2897 | } |
| 2898 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2899 | psa_status_t psa_verify_message( mbedtls_svc_key_id_t key, |
| 2900 | psa_algorithm_t alg, |
| 2901 | const uint8_t * input, |
| 2902 | size_t input_length, |
| 2903 | const uint8_t * signature, |
| 2904 | size_t signature_length ) |
| 2905 | { |
| 2906 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2907 | key, 1, alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2908 | signature, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2909 | } |
| 2910 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2911 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2912 | const psa_key_attributes_t *attributes, |
| 2913 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2914 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2915 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2916 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2917 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2918 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2919 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2920 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2921 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2922 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2923 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2924 | return( mbedtls_psa_rsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2925 | attributes, |
| 2926 | key_buffer, key_buffer_size, |
| 2927 | alg, hash, hash_length, |
| 2928 | signature, signature_size, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2929 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2930 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2931 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2932 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2933 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2934 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2935 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2936 | } |
Ronald Cron | 81ca97e | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 2937 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2938 | { |
| 2939 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 2940 | { |
| 2941 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2942 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2943 | return( mbedtls_psa_ecdsa_sign_hash( |
| 2944 | attributes, |
| 2945 | key_buffer, key_buffer_size, |
| 2946 | alg, hash, hash_length, |
| 2947 | signature, signature_size, signature_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2948 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2949 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2950 | } |
| 2951 | else |
| 2952 | { |
| 2953 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2954 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2955 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2956 | |
| 2957 | (void)key_buffer; |
| 2958 | (void)key_buffer_size; |
| 2959 | (void)hash; |
| 2960 | (void)hash_length; |
| 2961 | (void)signature; |
| 2962 | (void)signature_size; |
| 2963 | (void)signature_length; |
| 2964 | |
| 2965 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2966 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2967 | |
| 2968 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
| 2969 | psa_algorithm_t alg, |
| 2970 | const uint8_t *hash, |
| 2971 | size_t hash_length, |
| 2972 | uint8_t *signature, |
| 2973 | size_t signature_size, |
| 2974 | size_t *signature_length ) |
| 2975 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2976 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2977 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 2978 | signature, signature_size, signature_length ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2979 | } |
| 2980 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2981 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2982 | const psa_key_attributes_t *attributes, |
| 2983 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2984 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2985 | const uint8_t *signature, size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2986 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2987 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2988 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2989 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2990 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2991 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2992 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2993 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2994 | return( mbedtls_psa_rsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2995 | attributes, |
| 2996 | key_buffer, key_buffer_size, |
| 2997 | alg, hash, hash_length, |
| 2998 | signature, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2999 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3000 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3001 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3002 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3003 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3004 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3005 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3006 | } |
Ronald Cron | 81ca97e | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 3007 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3008 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3009 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 3010 | { |
| 3011 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3012 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3013 | return( mbedtls_psa_ecdsa_verify_hash( |
| 3014 | attributes, |
| 3015 | key_buffer, key_buffer_size, |
| 3016 | alg, hash, hash_length, |
| 3017 | signature, signature_length ) ); |
| 3018 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3019 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3020 | } |
| 3021 | else |
| 3022 | { |
| 3023 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3024 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3025 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3026 | |
| 3027 | (void)key_buffer; |
| 3028 | (void)key_buffer_size; |
| 3029 | (void)hash; |
| 3030 | (void)hash_length; |
| 3031 | (void)signature; |
| 3032 | (void)signature_length; |
| 3033 | |
| 3034 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3035 | } |
| 3036 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3037 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
| 3038 | psa_algorithm_t alg, |
| 3039 | const uint8_t *hash, |
| 3040 | size_t hash_length, |
| 3041 | const uint8_t *signature, |
| 3042 | size_t signature_length ) |
| 3043 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3044 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3045 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3046 | signature, signature_length ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3047 | } |
| 3048 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3049 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3050 | psa_algorithm_t alg, |
| 3051 | const uint8_t *input, |
| 3052 | size_t input_length, |
| 3053 | const uint8_t *salt, |
| 3054 | size_t salt_length, |
| 3055 | uint8_t *output, |
| 3056 | size_t output_size, |
| 3057 | size_t *output_length ) |
| 3058 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3059 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3060 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3061 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3062 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3063 | (void) input; |
| 3064 | (void) input_length; |
| 3065 | (void) salt; |
| 3066 | (void) output; |
| 3067 | (void) output_size; |
| 3068 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3069 | *output_length = 0; |
| 3070 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3071 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3072 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3073 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3074 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3075 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3076 | if( status != PSA_SUCCESS ) |
| 3077 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3078 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3079 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3080 | { |
| 3081 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3082 | goto exit; |
| 3083 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3084 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3085 | psa_key_attributes_t attributes = { |
| 3086 | .core = slot->attr |
| 3087 | }; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3088 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3089 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3090 | &attributes, slot->key.data, slot->key.bytes, |
| 3091 | alg, input, input_length, salt, salt_length, |
| 3092 | output, output_size, output_length ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3093 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3094 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3095 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3096 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3097 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3098 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3099 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3100 | psa_algorithm_t alg, |
| 3101 | const uint8_t *input, |
| 3102 | size_t input_length, |
| 3103 | const uint8_t *salt, |
| 3104 | size_t salt_length, |
| 3105 | uint8_t *output, |
| 3106 | size_t output_size, |
| 3107 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3108 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3109 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3110 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3111 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3112 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3113 | (void) input; |
| 3114 | (void) input_length; |
| 3115 | (void) salt; |
| 3116 | (void) output; |
| 3117 | (void) output_size; |
| 3118 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3119 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3120 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3121 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3122 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3123 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3124 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3125 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3126 | if( status != PSA_SUCCESS ) |
| 3127 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3128 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3129 | { |
| 3130 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3131 | goto exit; |
| 3132 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3133 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3134 | psa_key_attributes_t attributes = { |
| 3135 | .core = slot->attr |
| 3136 | }; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3137 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3138 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3139 | &attributes, slot->key.data, slot->key.bytes, |
| 3140 | alg, input, input_length, salt, salt_length, |
| 3141 | output, output_size, output_length ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3142 | |
| 3143 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3144 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3145 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3146 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3147 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3148 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3149 | |
| 3150 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3151 | /****************************************************************/ |
| 3152 | /* Symmetric cryptography */ |
| 3153 | /****************************************************************/ |
| 3154 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3155 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
| 3156 | mbedtls_svc_key_id_t key, |
| 3157 | psa_algorithm_t alg, |
| 3158 | mbedtls_operation_t cipher_operation ) |
| 3159 | { |
| 3160 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3161 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3162 | psa_key_slot_t *slot = NULL; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3163 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3164 | PSA_KEY_USAGE_ENCRYPT : |
| 3165 | PSA_KEY_USAGE_DECRYPT ); |
| 3166 | |
| 3167 | /* A context must be freshly initialized before it can be set up. */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3168 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3169 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3170 | status = PSA_ERROR_BAD_STATE; |
| 3171 | goto exit; |
| 3172 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3173 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3174 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3175 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3176 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3177 | goto exit; |
| 3178 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3179 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3180 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg ); |
| 3181 | if( status != PSA_SUCCESS ) |
| 3182 | goto exit; |
| 3183 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3184 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3185 | * is used to indicate to psa_cipher_abort that there are resources to free, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3186 | * so we only set it (in the driver wrapper) after resources have been |
| 3187 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3188 | operation->iv_set = 0; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3189 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3190 | operation->iv_required = 0; |
| 3191 | else |
| 3192 | operation->iv_required = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3193 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3194 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3195 | psa_key_attributes_t attributes = { |
| 3196 | .core = slot->attr |
| 3197 | }; |
| 3198 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3199 | /* Try doing the operation through a driver before using software fallback. */ |
| 3200 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3201 | status = psa_driver_wrapper_cipher_encrypt_setup( operation, |
| 3202 | &attributes, |
| 3203 | slot->key.data, |
| 3204 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3205 | alg ); |
| 3206 | else |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3207 | status = psa_driver_wrapper_cipher_decrypt_setup( operation, |
| 3208 | &attributes, |
| 3209 | slot->key.data, |
| 3210 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3211 | alg ); |
| 3212 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3213 | exit: |
Ronald Cron | 06aa442 | 2021-03-09 17:32:57 +0100 | [diff] [blame] | 3214 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3215 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3216 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3217 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3218 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3219 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3220 | } |
| 3221 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3222 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3223 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3224 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3225 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3226 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3227 | } |
| 3228 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3229 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3230 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3231 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3232 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3233 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3234 | } |
| 3235 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3236 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3237 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3238 | size_t iv_size, |
| 3239 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3240 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3241 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3242 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3243 | size_t default_iv_length; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3244 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3245 | if( operation->id == 0 ) |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3246 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3247 | status = PSA_ERROR_BAD_STATE; |
| 3248 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3249 | } |
| 3250 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3251 | if( operation->iv_set || ! operation->iv_required ) |
| 3252 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3253 | status = PSA_ERROR_BAD_STATE; |
| 3254 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3255 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3256 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3257 | default_iv_length = operation->default_iv_length; |
| 3258 | if( iv_size < default_iv_length ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3259 | { |
| 3260 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3261 | goto exit; |
| 3262 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3263 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3264 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
| 3265 | { |
| 3266 | status = PSA_ERROR_GENERIC_ERROR; |
| 3267 | goto exit; |
| 3268 | } |
| 3269 | |
| 3270 | status = psa_generate_random( local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3271 | if( status != PSA_SUCCESS ) |
| 3272 | goto exit; |
| 3273 | |
| 3274 | status = psa_driver_wrapper_cipher_set_iv( operation, |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3275 | local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3276 | |
| 3277 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3278 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3279 | { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3280 | memcpy( iv, local_iv, default_iv_length ); |
| 3281 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3282 | operation->iv_set = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3283 | } |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3284 | else |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3285 | { |
| 3286 | *iv_length = 0; |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3287 | psa_cipher_abort( operation ); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3288 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3289 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3290 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3291 | } |
| 3292 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3293 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3294 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3295 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3296 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3297 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3298 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3299 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3300 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3301 | status = PSA_ERROR_BAD_STATE; |
| 3302 | goto exit; |
| 3303 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3304 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3305 | if( operation->iv_set || ! operation->iv_required ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3306 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3307 | status = PSA_ERROR_BAD_STATE; |
| 3308 | goto exit; |
| 3309 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3310 | |
| 3311 | if( iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3312 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3313 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3314 | goto exit; |
| 3315 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3316 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3317 | status = psa_driver_wrapper_cipher_set_iv( operation, |
| 3318 | iv, |
| 3319 | iv_length ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3320 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3321 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3322 | if( status == PSA_SUCCESS ) |
| 3323 | operation->iv_set = 1; |
| 3324 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3325 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3326 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3327 | } |
| 3328 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3329 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3330 | const uint8_t *input, |
| 3331 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3332 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3333 | size_t output_size, |
| 3334 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3335 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3336 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3337 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3338 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3339 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3340 | status = PSA_ERROR_BAD_STATE; |
| 3341 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3342 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3343 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3344 | if( operation->iv_required && ! operation->iv_set ) |
| 3345 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3346 | status = PSA_ERROR_BAD_STATE; |
| 3347 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3348 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3349 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3350 | status = psa_driver_wrapper_cipher_update( operation, |
| 3351 | input, |
| 3352 | input_length, |
| 3353 | output, |
| 3354 | output_size, |
| 3355 | output_length ); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3356 | |
| 3357 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3358 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3359 | psa_cipher_abort( operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3360 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3361 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3362 | } |
| 3363 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3364 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3365 | uint8_t *output, |
| 3366 | size_t output_size, |
| 3367 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3368 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3369 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3370 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3371 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3372 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3373 | status = PSA_ERROR_BAD_STATE; |
| 3374 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3375 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3376 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3377 | if( operation->iv_required && ! operation->iv_set ) |
| 3378 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3379 | status = PSA_ERROR_BAD_STATE; |
| 3380 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3381 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3382 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3383 | status = psa_driver_wrapper_cipher_finish( operation, |
| 3384 | output, |
| 3385 | output_size, |
| 3386 | output_length ); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3387 | |
| 3388 | exit: |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3389 | if( status == PSA_SUCCESS ) |
| 3390 | return( psa_cipher_abort( operation ) ); |
| 3391 | else |
| 3392 | { |
| 3393 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3394 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3395 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3396 | return( status ); |
| 3397 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3398 | } |
| 3399 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3400 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3401 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3402 | if( operation->id == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3403 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3404 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3405 | * in use. It's ok to call abort on such an object, and there's |
| 3406 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3407 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3408 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3409 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3410 | psa_driver_wrapper_cipher_abort( operation ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3411 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3412 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3413 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3414 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3415 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3416 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3417 | } |
| 3418 | |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3419 | psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key, |
| 3420 | psa_algorithm_t alg, |
| 3421 | const uint8_t *input, |
| 3422 | size_t input_length, |
| 3423 | uint8_t *output, |
| 3424 | size_t output_size, |
| 3425 | size_t *output_length ) |
| 3426 | { |
| 3427 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3428 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3429 | psa_key_slot_t *slot = NULL; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3430 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3431 | size_t default_iv_length = 0; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3432 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3433 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3434 | { |
| 3435 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3436 | goto exit; |
| 3437 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3438 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3439 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3440 | PSA_KEY_USAGE_ENCRYPT, |
| 3441 | alg ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3442 | if( status != PSA_SUCCESS ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3443 | goto exit; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3444 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3445 | psa_key_attributes_t attributes = { |
| 3446 | .core = slot->attr |
| 3447 | }; |
| 3448 | |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3449 | default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
| 3450 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3451 | { |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3452 | status = PSA_ERROR_GENERIC_ERROR; |
| 3453 | goto exit; |
| 3454 | } |
| 3455 | |
| 3456 | if( default_iv_length > 0 ) |
| 3457 | { |
| 3458 | if( output_size < default_iv_length ) |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3459 | { |
| 3460 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3461 | goto exit; |
| 3462 | } |
| 3463 | |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3464 | status = psa_generate_random( local_iv, default_iv_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3465 | if( status != PSA_SUCCESS ) |
| 3466 | goto exit; |
| 3467 | } |
| 3468 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3469 | status = psa_driver_wrapper_cipher_encrypt( |
| 3470 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3471 | alg, local_iv, default_iv_length, input, input_length, |
| 3472 | output + default_iv_length, output_size - default_iv_length, |
| 3473 | output_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3474 | |
| 3475 | exit: |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3476 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3477 | if( status == PSA_SUCCESS ) |
| 3478 | status = unlock_status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3479 | |
Ronald Cron | 9b67428 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3480 | if( status == PSA_SUCCESS ) |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3481 | { |
| 3482 | if( default_iv_length > 0 ) |
| 3483 | memcpy( output, local_iv, default_iv_length ); |
| 3484 | *output_length += default_iv_length; |
| 3485 | } |
Ronald Cron | 9b67428 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3486 | else |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3487 | *output_length = 0; |
| 3488 | |
| 3489 | return( status ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key, |
| 3493 | psa_algorithm_t alg, |
| 3494 | const uint8_t *input, |
| 3495 | size_t input_length, |
| 3496 | uint8_t *output, |
| 3497 | size_t output_size, |
| 3498 | size_t *output_length ) |
| 3499 | { |
| 3500 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3501 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3502 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3503 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3504 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3505 | { |
| 3506 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3507 | goto exit; |
| 3508 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3509 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3510 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3511 | PSA_KEY_USAGE_DECRYPT, |
| 3512 | alg ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3513 | if( status != PSA_SUCCESS ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3514 | goto exit; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3515 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3516 | psa_key_attributes_t attributes = { |
| 3517 | .core = slot->attr |
| 3518 | }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3519 | |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 3520 | if( alg == PSA_ALG_CCM_STAR_NO_TAG && input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) ) |
| 3521 | { |
| 3522 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3523 | goto exit; |
| 3524 | } |
| 3525 | else if ( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) ) |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3526 | { |
| 3527 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3528 | goto exit; |
| 3529 | } |
| 3530 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3531 | status = psa_driver_wrapper_cipher_decrypt( |
| 3532 | &attributes, slot->key.data, slot->key.bytes, |
| 3533 | alg, input, input_length, |
| 3534 | output, output_size, output_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3535 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3536 | exit: |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3537 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3538 | if( status == PSA_SUCCESS ) |
| 3539 | status = unlock_status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3540 | |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3541 | if( status != PSA_SUCCESS ) |
| 3542 | *output_length = 0; |
| 3543 | |
| 3544 | return( status ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3548 | /****************************************************************/ |
| 3549 | /* AEAD */ |
| 3550 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3551 | |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3552 | /* Helper function to get the base algorithm from its variants. */ |
| 3553 | static psa_algorithm_t psa_aead_get_base_algorithm( psa_algorithm_t alg ) |
| 3554 | { |
| 3555 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg ); |
| 3556 | } |
| 3557 | |
| 3558 | /* Helper function to perform common nonce length checks. */ |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3559 | static psa_status_t psa_aead_check_nonce_length( psa_algorithm_t alg, |
| 3560 | size_t nonce_length ) |
| 3561 | { |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3562 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm( alg ); |
| 3563 | |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3564 | switch(base_alg) |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3565 | { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3566 | #if defined(PSA_WANT_ALG_GCM) |
| 3567 | case PSA_ALG_GCM: |
| 3568 | /* Not checking max nonce size here as GCM spec allows almost |
| 3569 | * arbitrarily large nonces. Please note that we do not generally |
| 3570 | * recommend the usage of nonces of greater length than |
| 3571 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter |
| 3572 | * size, which can then lead to collisions if you encrypt a very |
| 3573 | * large number of messages.*/ |
| 3574 | if( nonce_length != 0 ) |
| 3575 | return( PSA_SUCCESS ); |
| 3576 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3577 | #endif /* PSA_WANT_ALG_GCM */ |
| 3578 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3579 | case PSA_ALG_CCM: |
| 3580 | if( nonce_length >= 7 && nonce_length <= 13 ) |
| 3581 | return( PSA_SUCCESS ); |
| 3582 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3583 | #endif /* PSA_WANT_ALG_CCM */ |
| 3584 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3585 | case PSA_ALG_CHACHA20_POLY1305: |
| 3586 | if( nonce_length == 12 ) |
| 3587 | return( PSA_SUCCESS ); |
Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 3588 | else if( nonce_length == 8 ) |
| 3589 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3590 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3591 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3592 | default: |
Bence Szépkúti | 357b78e | 2021-11-09 13:17:17 +0100 | [diff] [blame] | 3593 | return( PSA_ERROR_NOT_SUPPORTED ); |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3594 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3595 | |
Bence Szépkúti | 357b78e | 2021-11-09 13:17:17 +0100 | [diff] [blame] | 3596 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3597 | } |
| 3598 | |
Bence Szépkúti | aa3a6e4 | 2022-01-13 16:26:03 +0100 | [diff] [blame] | 3599 | static psa_status_t psa_aead_check_algorithm( psa_algorithm_t alg ) |
| 3600 | { |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3601 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3602 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3603 | |
| 3604 | return( PSA_SUCCESS ); |
| 3605 | } |
| 3606 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3607 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3608 | psa_algorithm_t alg, |
| 3609 | const uint8_t *nonce, |
| 3610 | size_t nonce_length, |
| 3611 | const uint8_t *additional_data, |
| 3612 | size_t additional_data_length, |
| 3613 | const uint8_t *plaintext, |
| 3614 | size_t plaintext_length, |
| 3615 | uint8_t *ciphertext, |
| 3616 | size_t ciphertext_size, |
| 3617 | size_t *ciphertext_length ) |
| 3618 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3619 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3620 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3621 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3622 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3623 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3624 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3625 | if( status != PSA_SUCCESS ) |
| 3626 | return( status ); |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3627 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3628 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3629 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3630 | if( status != PSA_SUCCESS ) |
| 3631 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3632 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3633 | psa_key_attributes_t attributes = { |
| 3634 | .core = slot->attr |
| 3635 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3636 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3637 | status = psa_aead_check_nonce_length( alg, nonce_length ); |
| 3638 | if( status != PSA_SUCCESS ) |
| 3639 | goto exit; |
| 3640 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3641 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3642 | &attributes, slot->key.data, slot->key.bytes, |
| 3643 | alg, |
| 3644 | nonce, nonce_length, |
| 3645 | additional_data, additional_data_length, |
| 3646 | plaintext, plaintext_length, |
| 3647 | ciphertext, ciphertext_size, ciphertext_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3648 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3649 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3650 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3651 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3652 | exit: |
Ronald Cron | 9f31017 | 2021-03-16 16:36:37 +0100 | [diff] [blame] | 3653 | psa_unlock_key_slot( slot ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3654 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3655 | return( status ); |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3656 | } |
| 3657 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3658 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3659 | psa_algorithm_t alg, |
| 3660 | const uint8_t *nonce, |
| 3661 | size_t nonce_length, |
| 3662 | const uint8_t *additional_data, |
| 3663 | size_t additional_data_length, |
| 3664 | const uint8_t *ciphertext, |
| 3665 | size_t ciphertext_length, |
| 3666 | uint8_t *plaintext, |
| 3667 | size_t plaintext_size, |
| 3668 | size_t *plaintext_length ) |
| 3669 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3670 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3671 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3672 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3673 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3674 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3675 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3676 | if( status != PSA_SUCCESS ) |
| 3677 | return( status ); |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3678 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3679 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3680 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3681 | if( status != PSA_SUCCESS ) |
| 3682 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3683 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3684 | psa_key_attributes_t attributes = { |
| 3685 | .core = slot->attr |
| 3686 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3687 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3688 | status = psa_aead_check_nonce_length( alg, nonce_length ); |
| 3689 | if( status != PSA_SUCCESS ) |
| 3690 | goto exit; |
| 3691 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3692 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3693 | &attributes, slot->key.data, slot->key.bytes, |
| 3694 | alg, |
| 3695 | nonce, nonce_length, |
| 3696 | additional_data, additional_data_length, |
| 3697 | ciphertext, ciphertext_length, |
| 3698 | plaintext, plaintext_size, plaintext_length ); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3699 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3700 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3701 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3702 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3703 | exit: |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3704 | psa_unlock_key_slot( slot ); |
| 3705 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3706 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3707 | } |
| 3708 | |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3709 | static psa_status_t psa_validate_tag_length( psa_aead_operation_t *operation, |
| 3710 | psa_algorithm_t alg ) { |
| 3711 | uint8_t tag_len = 0; |
| 3712 | if( psa_driver_get_tag_len( operation, &tag_len ) != PSA_SUCCESS ) |
| 3713 | { |
| 3714 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3715 | } |
| 3716 | |
| 3717 | switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ) |
| 3718 | { |
| 3719 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 3720 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
| 3721 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/ |
| 3722 | if( tag_len < 4 || tag_len > 16 || tag_len % 2 ) |
| 3723 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3724 | break; |
| 3725 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 3726 | |
| 3727 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 3728 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
| 3729 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */ |
| 3730 | if( tag_len != 4 && tag_len != 8 && ( tag_len < 12 || tag_len > 16 ) ) |
| 3731 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3732 | break; |
| 3733 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 3734 | |
| 3735 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 3736 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3737 | /* We only support the default tag length. */ |
| 3738 | if( tag_len != 16 ) |
| 3739 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3740 | break; |
| 3741 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 3742 | |
| 3743 | default: |
| 3744 | (void) tag_len; |
| 3745 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3746 | } |
| 3747 | return( PSA_SUCCESS ); |
| 3748 | } |
| 3749 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3750 | /* Set the key for a multipart authenticated operation. */ |
| 3751 | static psa_status_t psa_aead_setup( psa_aead_operation_t *operation, |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3752 | int is_encrypt, |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3753 | mbedtls_svc_key_id_t key, |
| 3754 | psa_algorithm_t alg ) |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3755 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3756 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3757 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3758 | psa_key_slot_t *slot = NULL; |
| 3759 | psa_key_usage_t key_usage = 0; |
| 3760 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3761 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3762 | if( status != PSA_SUCCESS ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3763 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3764 | |
| 3765 | if( operation->id != 0 ) |
| 3766 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3767 | status = PSA_ERROR_BAD_STATE; |
| 3768 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | if( operation->nonce_set || operation->lengths_set || |
| 3772 | operation->ad_started || operation->body_started ) |
| 3773 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3774 | status = PSA_ERROR_BAD_STATE; |
| 3775 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3776 | } |
| 3777 | |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3778 | if( is_encrypt ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3779 | key_usage = PSA_KEY_USAGE_ENCRYPT; |
| 3780 | else |
| 3781 | key_usage = PSA_KEY_USAGE_DECRYPT; |
| 3782 | |
| 3783 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, key_usage, |
| 3784 | alg ); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3785 | if( status != PSA_SUCCESS ) |
| 3786 | goto exit; |
| 3787 | |
| 3788 | psa_key_attributes_t attributes = { |
| 3789 | .core = slot->attr |
| 3790 | }; |
| 3791 | |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3792 | if( is_encrypt ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3793 | status = psa_driver_wrapper_aead_encrypt_setup( operation, |
| 3794 | &attributes, |
| 3795 | slot->key.data, |
| 3796 | slot->key.bytes, |
| 3797 | alg ); |
| 3798 | else |
| 3799 | status = psa_driver_wrapper_aead_decrypt_setup( operation, |
| 3800 | &attributes, |
| 3801 | slot->key.data, |
| 3802 | slot->key.bytes, |
| 3803 | alg ); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3804 | if( status != PSA_SUCCESS ) |
| 3805 | goto exit; |
| 3806 | |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3807 | if( ( status = psa_validate_tag_length( operation, alg ) ) != PSA_SUCCESS ) |
| 3808 | goto exit; |
| 3809 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3810 | operation->key_type = psa_get_key_type( &attributes ); |
| 3811 | |
| 3812 | exit: |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3813 | unlock_status = psa_unlock_key_slot( slot ); |
| 3814 | |
| 3815 | if( status == PSA_SUCCESS ) |
| 3816 | { |
| 3817 | status = unlock_status; |
| 3818 | operation->alg = psa_aead_get_base_algorithm( alg ); |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3819 | operation->is_encrypt = is_encrypt; |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3820 | } |
| 3821 | else |
| 3822 | psa_aead_abort( operation ); |
| 3823 | |
| 3824 | return( status ); |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3825 | } |
| 3826 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3827 | /* Set the key for a multipart authenticated encryption operation. */ |
| 3828 | psa_status_t psa_aead_encrypt_setup( psa_aead_operation_t *operation, |
| 3829 | mbedtls_svc_key_id_t key, |
| 3830 | psa_algorithm_t alg ) |
| 3831 | { |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3832 | return( psa_aead_setup( operation, 1, key, alg ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | /* Set the key for a multipart authenticated decryption operation. */ |
| 3836 | psa_status_t psa_aead_decrypt_setup( psa_aead_operation_t *operation, |
| 3837 | mbedtls_svc_key_id_t key, |
| 3838 | psa_algorithm_t alg ) |
| 3839 | { |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3840 | return( psa_aead_setup( operation, 0, key, alg ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | /* Generate a random nonce / IV for multipart AEAD operation */ |
| 3844 | psa_status_t psa_aead_generate_nonce( psa_aead_operation_t *operation, |
| 3845 | uint8_t *nonce, |
| 3846 | size_t nonce_size, |
| 3847 | size_t *nonce_length ) |
| 3848 | { |
| 3849 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3850 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; |
Paul Elliott | b91da71 | 2021-05-20 14:43:47 +0100 | [diff] [blame] | 3851 | size_t required_nonce_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3852 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3853 | *nonce_length = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3854 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3855 | if( operation->id == 0 ) |
| 3856 | { |
| 3857 | status = PSA_ERROR_BAD_STATE; |
| 3858 | goto exit; |
| 3859 | } |
| 3860 | |
Paul Elliott | daf5c89 | 2021-08-25 16:24:58 +0100 | [diff] [blame] | 3861 | if( operation->nonce_set || !operation->is_encrypt ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3862 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3863 | status = PSA_ERROR_BAD_STATE; |
| 3864 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3865 | } |
| 3866 | |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 3867 | /* For CCM, this size may not be correct according to the PSA |
| 3868 | * specification. The PSA Crypto 1.0.1 specification states: |
| 3869 | * |
| 3870 | * CCM encodes the plaintext length pLen in L octets, with L the smallest |
| 3871 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. |
| 3872 | * |
| 3873 | * However this restriction that L has to be the smallest integer is not |
| 3874 | * applied in practice, and it is not implementable here since the |
| 3875 | * plaintext length may or may not be known at this time. */ |
Paul Elliott | bbe90b5 | 2021-05-11 22:22:42 +0100 | [diff] [blame] | 3876 | required_nonce_size = PSA_AEAD_NONCE_LENGTH( operation->key_type, |
Paul Elliott | 3242f6c | 2021-08-25 16:33:47 +0100 | [diff] [blame] | 3877 | operation->alg ); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3878 | if( nonce_size < required_nonce_size ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3879 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3880 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3881 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3882 | } |
| 3883 | |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3884 | status = psa_generate_random( local_nonce, required_nonce_size ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3885 | if( status != PSA_SUCCESS ) |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3886 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3887 | |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3888 | status = psa_aead_set_nonce( operation, local_nonce, required_nonce_size ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3889 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3890 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3891 | if( status == PSA_SUCCESS ) |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3892 | { |
| 3893 | memcpy( nonce, local_nonce, required_nonce_size ); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3894 | *nonce_length = required_nonce_size; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3895 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3896 | else |
| 3897 | psa_aead_abort( operation ); |
| 3898 | |
| 3899 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | /* Set the nonce for a multipart authenticated encryption or decryption |
| 3903 | operation.*/ |
| 3904 | psa_status_t psa_aead_set_nonce( psa_aead_operation_t *operation, |
| 3905 | const uint8_t *nonce, |
| 3906 | size_t nonce_length ) |
| 3907 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3908 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3909 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3910 | if( operation->id == 0 ) |
| 3911 | { |
| 3912 | status = PSA_ERROR_BAD_STATE; |
| 3913 | goto exit; |
| 3914 | } |
| 3915 | |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 3916 | if( operation->nonce_set ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3917 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3918 | status = PSA_ERROR_BAD_STATE; |
| 3919 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3920 | } |
| 3921 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3922 | status = psa_aead_check_nonce_length( operation->alg, nonce_length ); |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3923 | if( status != PSA_SUCCESS ) |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 3924 | { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3925 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3926 | goto exit; |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 3927 | } |
Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 3928 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3929 | status = psa_driver_wrapper_aead_set_nonce( operation, nonce, |
| 3930 | nonce_length ); |
| 3931 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3932 | exit: |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3933 | if( status == PSA_SUCCESS ) |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3934 | operation->nonce_set = 1; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3935 | else |
| 3936 | psa_aead_abort( operation ); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3937 | |
| 3938 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | /* Declare the lengths of the message and additional data for multipart AEAD. */ |
| 3942 | psa_status_t psa_aead_set_lengths( psa_aead_operation_t *operation, |
| 3943 | size_t ad_length, |
| 3944 | size_t plaintext_length ) |
| 3945 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3946 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3947 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3948 | if( operation->id == 0 ) |
| 3949 | { |
| 3950 | status = PSA_ERROR_BAD_STATE; |
| 3951 | goto exit; |
| 3952 | } |
| 3953 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 3954 | if( operation->lengths_set || operation->ad_started || |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 3955 | operation->body_started ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3956 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3957 | status = PSA_ERROR_BAD_STATE; |
| 3958 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3959 | } |
| 3960 | |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3961 | switch(operation->alg) |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3962 | { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3963 | #if defined(PSA_WANT_ALG_GCM) |
| 3964 | case PSA_ALG_GCM: |
| 3965 | /* Lengths can only be too large for GCM if size_t is bigger than 32 |
| 3966 | * bits. Without the guard this code will generate warnings on 32bit |
| 3967 | * builds. */ |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3968 | #if SIZE_MAX > UINT32_MAX |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3969 | if( (( uint64_t ) ad_length ) >> 61 != 0 || |
| 3970 | (( uint64_t ) plaintext_length ) > 0xFFFFFFFE0ull ) |
| 3971 | { |
| 3972 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3973 | goto exit; |
| 3974 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3975 | #endif |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3976 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3977 | #endif /* PSA_WANT_ALG_GCM */ |
| 3978 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3979 | case PSA_ALG_CCM: |
| 3980 | if( ad_length > 0xFF00 ) |
| 3981 | { |
| 3982 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3983 | goto exit; |
| 3984 | } |
| 3985 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3986 | #endif /* PSA_WANT_ALG_CCM */ |
| 3987 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3988 | case PSA_ALG_CHACHA20_POLY1305: |
| 3989 | /* No length restrictions for ChaChaPoly. */ |
| 3990 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3991 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3992 | default: |
| 3993 | break; |
| 3994 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3995 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3996 | status = psa_driver_wrapper_aead_set_lengths( operation, ad_length, |
| 3997 | plaintext_length ); |
| 3998 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3999 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4000 | if( status == PSA_SUCCESS ) |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4001 | { |
| 4002 | operation->ad_remaining = ad_length; |
| 4003 | operation->body_remaining = plaintext_length; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4004 | operation->lengths_set = 1; |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4005 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4006 | else |
| 4007 | psa_aead_abort( operation ); |
| 4008 | |
| 4009 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4010 | } |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 4011 | |
| 4012 | /* Pass additional data to an active multipart AEAD operation. */ |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4013 | psa_status_t psa_aead_update_ad( psa_aead_operation_t *operation, |
| 4014 | const uint8_t *input, |
| 4015 | size_t input_length ) |
| 4016 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4017 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4018 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4019 | if( operation->id == 0 ) |
| 4020 | { |
| 4021 | status = PSA_ERROR_BAD_STATE; |
| 4022 | goto exit; |
| 4023 | } |
| 4024 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4025 | if( !operation->nonce_set || operation->body_started ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4026 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4027 | status = PSA_ERROR_BAD_STATE; |
| 4028 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4029 | } |
| 4030 | |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4031 | if( operation->lengths_set ) |
| 4032 | { |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4033 | if( operation->ad_remaining < input_length ) |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4034 | { |
| 4035 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4036 | goto exit; |
| 4037 | } |
| 4038 | |
| 4039 | operation->ad_remaining -= input_length; |
| 4040 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4041 | #if defined(PSA_WANT_ALG_CCM) |
| 4042 | else if( operation->alg == PSA_ALG_CCM ) |
| 4043 | { |
| 4044 | status = PSA_ERROR_BAD_STATE; |
| 4045 | goto exit; |
| 4046 | } |
| 4047 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4048 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4049 | status = psa_driver_wrapper_aead_update_ad( operation, input, |
| 4050 | input_length ); |
| 4051 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4052 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4053 | if( status == PSA_SUCCESS ) |
| 4054 | operation->ad_started = 1; |
| 4055 | else |
| 4056 | psa_aead_abort( operation ); |
| 4057 | |
| 4058 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4059 | } |
| 4060 | |
| 4061 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 4062 | operation.*/ |
| 4063 | psa_status_t psa_aead_update( psa_aead_operation_t *operation, |
| 4064 | const uint8_t *input, |
| 4065 | size_t input_length, |
| 4066 | uint8_t *output, |
| 4067 | size_t output_size, |
| 4068 | size_t *output_length ) |
| 4069 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4070 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4071 | |
| 4072 | *output_length = 0; |
| 4073 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4074 | if( operation->id == 0 ) |
| 4075 | { |
| 4076 | status = PSA_ERROR_BAD_STATE; |
| 4077 | goto exit; |
| 4078 | } |
| 4079 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4080 | if( !operation->nonce_set ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4081 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4082 | status = PSA_ERROR_BAD_STATE; |
| 4083 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4084 | } |
| 4085 | |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4086 | if( operation->lengths_set ) |
| 4087 | { |
| 4088 | /* Additional data length was supplied, but not all the additional |
| 4089 | data was supplied.*/ |
| 4090 | if( operation->ad_remaining != 0 ) |
| 4091 | { |
| 4092 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4093 | goto exit; |
| 4094 | } |
| 4095 | |
| 4096 | /* Too much data provided. */ |
| 4097 | if( operation->body_remaining < input_length ) |
| 4098 | { |
| 4099 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4100 | goto exit; |
| 4101 | } |
| 4102 | |
| 4103 | operation->body_remaining -= input_length; |
| 4104 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4105 | #if defined(PSA_WANT_ALG_CCM) |
| 4106 | else if( operation->alg == PSA_ALG_CCM ) |
| 4107 | { |
| 4108 | status = PSA_ERROR_BAD_STATE; |
| 4109 | goto exit; |
| 4110 | } |
| 4111 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4112 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4113 | status = psa_driver_wrapper_aead_update( operation, input, input_length, |
| 4114 | output, output_size, |
| 4115 | output_length ); |
| 4116 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4117 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4118 | if( status == PSA_SUCCESS ) |
| 4119 | operation->body_started = 1; |
| 4120 | else |
| 4121 | psa_aead_abort( operation ); |
| 4122 | |
| 4123 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4124 | } |
| 4125 | |
Paul Elliott | 69bf5fc | 2021-09-19 18:26:37 +0100 | [diff] [blame] | 4126 | static psa_status_t psa_aead_final_checks( const psa_aead_operation_t *operation ) |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4127 | { |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4128 | if( operation->id == 0 || !operation->nonce_set ) |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4129 | return( PSA_ERROR_BAD_STATE ); |
| 4130 | |
Paul Elliott | a561444 | 2021-07-14 14:54:11 +0100 | [diff] [blame] | 4131 | if( operation->lengths_set && ( operation->ad_remaining != 0 || |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4132 | operation->body_remaining != 0 ) ) |
| 4133 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4134 | |
| 4135 | return( PSA_SUCCESS ); |
| 4136 | } |
| 4137 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4138 | /* Finish encrypting a message in a multipart AEAD operation. */ |
| 4139 | psa_status_t psa_aead_finish( psa_aead_operation_t *operation, |
| 4140 | uint8_t *ciphertext, |
| 4141 | size_t ciphertext_size, |
| 4142 | size_t *ciphertext_length, |
| 4143 | uint8_t *tag, |
| 4144 | size_t tag_size, |
| 4145 | size_t *tag_length ) |
| 4146 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4147 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4148 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4149 | *ciphertext_length = 0; |
Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 4150 | *tag_length = tag_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4151 | |
Paul Elliott | 814fffb | 2021-08-16 18:20:36 +0100 | [diff] [blame] | 4152 | status = psa_aead_final_checks( operation ); |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4153 | if( status != PSA_SUCCESS ) |
| 4154 | goto exit; |
| 4155 | |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4156 | if( !operation->is_encrypt ) |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4157 | { |
| 4158 | status = PSA_ERROR_BAD_STATE; |
| 4159 | goto exit; |
| 4160 | } |
| 4161 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4162 | status = psa_driver_wrapper_aead_finish( operation, ciphertext, |
| 4163 | ciphertext_size, |
| 4164 | ciphertext_length, |
| 4165 | tag, tag_size, tag_length ); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4166 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4167 | exit: |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4168 | /* In case the operation fails and the user fails to check for failure or |
Paul Elliott | 4c916e8 | 2021-09-19 18:34:50 +0100 | [diff] [blame] | 4169 | * the zero tag size, make sure the tag is set to something implausible. |
| 4170 | * Even if the operation succeeds, make sure we clear the rest of the |
| 4171 | * buffer to prevent potential leakage of anything previously placed in |
| 4172 | * the same buffer.*/ |
Paul Elliott | 90fdc11 | 2021-09-22 17:15:48 +0100 | [diff] [blame] | 4173 | if( tag != NULL ) |
Paul Elliott | ec95cc9 | 2021-09-19 22:33:09 +0100 | [diff] [blame] | 4174 | { |
| 4175 | if( status != PSA_SUCCESS ) |
| 4176 | memset( tag, '!', tag_size ); |
| 4177 | else if( *tag_length < tag_size ) |
| 4178 | memset( tag + *tag_length, '!', ( tag_size - *tag_length ) ); |
| 4179 | } |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4180 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4181 | psa_aead_abort( operation ); |
| 4182 | |
| 4183 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 4187 | operation.*/ |
| 4188 | psa_status_t psa_aead_verify( psa_aead_operation_t *operation, |
| 4189 | uint8_t *plaintext, |
| 4190 | size_t plaintext_size, |
| 4191 | size_t *plaintext_length, |
| 4192 | const uint8_t *tag, |
| 4193 | size_t tag_length ) |
| 4194 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4195 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4196 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4197 | *plaintext_length = 0; |
| 4198 | |
Paul Elliott | 814fffb | 2021-08-16 18:20:36 +0100 | [diff] [blame] | 4199 | status = psa_aead_final_checks( operation ); |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4200 | if( status != PSA_SUCCESS ) |
| 4201 | goto exit; |
| 4202 | |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4203 | if( operation->is_encrypt ) |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4204 | { |
| 4205 | status = PSA_ERROR_BAD_STATE; |
| 4206 | goto exit; |
| 4207 | } |
| 4208 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4209 | status = psa_driver_wrapper_aead_verify( operation, plaintext, |
| 4210 | plaintext_size, |
| 4211 | plaintext_length, |
| 4212 | tag, tag_length ); |
| 4213 | |
| 4214 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4215 | psa_aead_abort( operation ); |
| 4216 | |
| 4217 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | /* Abort an AEAD operation. */ |
Paul Elliott | bbe90b5 | 2021-05-11 22:22:42 +0100 | [diff] [blame] | 4221 | psa_status_t psa_aead_abort( psa_aead_operation_t *operation ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4222 | { |
| 4223 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4224 | |
| 4225 | if( operation->id == 0 ) |
| 4226 | { |
| 4227 | /* The object has (apparently) been initialized but it is not (yet) |
| 4228 | * in use. It's ok to call abort on such an object, and there's |
| 4229 | * nothing to do. */ |
| 4230 | return( PSA_SUCCESS ); |
| 4231 | } |
| 4232 | |
| 4233 | status = psa_driver_wrapper_aead_abort( operation ); |
| 4234 | |
Paul Elliott | 70618b2 | 2021-09-22 17:12:16 +0100 | [diff] [blame] | 4235 | memset( operation, 0, sizeof( *operation ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4236 | |
| 4237 | return( status ); |
| 4238 | } |
| 4239 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4240 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4241 | /* Generators */ |
| 4242 | /****************************************************************/ |
| 4243 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4244 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4245 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4246 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \ |
| 4247 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4248 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4249 | #endif /* At least one builtin KDF */ |
| 4250 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4251 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4252 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4253 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4254 | static psa_status_t psa_key_derivation_start_hmac( |
| 4255 | psa_mac_operation_t *operation, |
| 4256 | psa_algorithm_t hash_alg, |
| 4257 | const uint8_t *hmac_key, |
| 4258 | size_t hmac_key_length ) |
| 4259 | { |
| 4260 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4261 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4262 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 4263 | psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) ); |
| 4264 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 4265 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4266 | operation->is_sign = 1; |
| 4267 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
| 4268 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4269 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 4270 | &attributes, |
| 4271 | hmac_key, hmac_key_length, |
| 4272 | PSA_ALG_HMAC( hash_alg ) ); |
| 4273 | |
| 4274 | psa_reset_key_attributes( &attributes ); |
| 4275 | return( status ); |
| 4276 | } |
| 4277 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4278 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4279 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4280 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4281 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4282 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4283 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4284 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4285 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4286 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4287 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4288 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4289 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4290 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4291 | } |
| 4292 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4293 | 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] | 4294 | { |
| 4295 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4296 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4297 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4298 | { |
| 4299 | /* The object has (apparently) been initialized but it is not |
| 4300 | * in use. It's ok to call abort on such an object, and there's |
| 4301 | * nothing to do. */ |
| 4302 | } |
| 4303 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4304 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 4305 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4306 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4307 | mbedtls_free( operation->ctx.hkdf.info ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4308 | status = psa_mac_abort( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4309 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4310 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4311 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4312 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4313 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4314 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4315 | /* 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] | 4316 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4317 | { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4318 | if( operation->ctx.tls12_prf.secret != NULL ) |
| 4319 | { |
| 4320 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret, |
| 4321 | operation->ctx.tls12_prf.secret_length ); |
| 4322 | mbedtls_free( operation->ctx.tls12_prf.secret ); |
| 4323 | } |
| 4324 | |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4325 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4326 | { |
| 4327 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4328 | operation->ctx.tls12_prf.seed_length ); |
| 4329 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4330 | } |
| 4331 | |
| 4332 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4333 | { |
| 4334 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4335 | operation->ctx.tls12_prf.label_length ); |
| 4336 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4337 | } |
| 4338 | |
Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 4339 | if( operation->ctx.tls12_prf.other_secret != NULL ) |
| 4340 | { |
| 4341 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.other_secret, |
| 4342 | operation->ctx.tls12_prf.other_secret_length ); |
| 4343 | mbedtls_free( operation->ctx.tls12_prf.other_secret ); |
| 4344 | } |
| 4345 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4346 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4347 | |
| 4348 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4349 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4350 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4351 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4352 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4353 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4354 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 4355 | if( PSA_ALG_IS_TLS12_ECJPAKE_TO_PMS( kdf_alg ) ) |
| 4356 | { |
| 4357 | mbedtls_platform_zeroize( operation->ctx.tls12_ecjpake_to_pms.data, |
| 4358 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE ); |
| 4359 | } |
| 4360 | else |
| 4361 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4362 | { |
| 4363 | status = PSA_ERROR_BAD_STATE; |
| 4364 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4365 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4366 | return( status ); |
| 4367 | } |
| 4368 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4369 | 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] | 4370 | size_t *capacity) |
| 4371 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4372 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4373 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4374 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4375 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4378 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4379 | return( PSA_SUCCESS ); |
| 4380 | } |
| 4381 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4382 | 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] | 4383 | size_t capacity ) |
| 4384 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4385 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4386 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4387 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4388 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4389 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4390 | return( PSA_SUCCESS ); |
| 4391 | } |
| 4392 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4393 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4394 | /* Read some bytes from an HKDF-based operation. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4395 | static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf, |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4396 | psa_algorithm_t kdf_alg, |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4397 | uint8_t *output, |
| 4398 | size_t output_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4399 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4400 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4401 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4402 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4403 | psa_status_t status; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4404 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4405 | const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ? 0 : 0xff; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4406 | #else |
| 4407 | const uint8_t last_block = 0xff; |
| 4408 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4409 | |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4410 | if( hkdf->state < HKDF_STATE_KEYED || |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4411 | ( !hkdf->info_set |
| 4412 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 4413 | && !PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) |
| 4414 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
| 4415 | ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4416 | return( PSA_ERROR_BAD_STATE ); |
| 4417 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4418 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4419 | while( output_length != 0 ) |
| 4420 | { |
| 4421 | /* Copy what remains of the current block */ |
| 4422 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4423 | if( n > output_length ) |
| 4424 | n = (uint8_t) output_length; |
| 4425 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4426 | output += n; |
| 4427 | output_length -= n; |
| 4428 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4429 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4430 | break; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4431 | /* We can't be wanting more output after the last block, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4432 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4433 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4434 | * object was corrupted or if this function is called directly |
| 4435 | * inside the library. */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4436 | if( hkdf->block_number == last_block ) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4437 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4438 | |
| 4439 | /* We need a new block */ |
| 4440 | ++hkdf->block_number; |
| 4441 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4442 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4443 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 4444 | hash_alg, |
| 4445 | hkdf->prk, |
| 4446 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4447 | if( status != PSA_SUCCESS ) |
| 4448 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4449 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4450 | if( hkdf->block_number != 1 ) |
| 4451 | { |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4452 | status = psa_mac_update( &hkdf->hmac, |
| 4453 | hkdf->output_block, |
| 4454 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4455 | if( status != PSA_SUCCESS ) |
| 4456 | return( status ); |
| 4457 | } |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4458 | status = psa_mac_update( &hkdf->hmac, |
| 4459 | hkdf->info, |
| 4460 | hkdf->info_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4461 | if( status != PSA_SUCCESS ) |
| 4462 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4463 | status = psa_mac_update( &hkdf->hmac, |
| 4464 | &hkdf->block_number, 1 ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4465 | if( status != PSA_SUCCESS ) |
| 4466 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4467 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 4468 | hkdf->output_block, |
| 4469 | sizeof( hkdf->output_block ), |
| 4470 | &hmac_output_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4471 | if( status != PSA_SUCCESS ) |
| 4472 | return( status ); |
| 4473 | } |
| 4474 | |
| 4475 | return( PSA_SUCCESS ); |
| 4476 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4477 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4478 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4479 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4480 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4481 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4482 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4483 | psa_algorithm_t alg ) |
| 4484 | { |
| 4485 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4486 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4487 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 4488 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4489 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4490 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4491 | /* We can't be wanting more output after block 0xff, otherwise |
| 4492 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4493 | * prevented this call. It could happen only if the operation |
| 4494 | * object was corrupted or if this function is called directly |
| 4495 | * inside the library. */ |
| 4496 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4497 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4498 | |
| 4499 | /* We need a new block */ |
| 4500 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4501 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4502 | |
| 4503 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4504 | * |
| 4505 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4506 | * |
| 4507 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4508 | * HMAC_hash(secret, A(2) + seed) + |
| 4509 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4510 | * |
| 4511 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4512 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4513 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4514 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4515 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4516 | * is currently extracted as `output_block` and where i is |
| 4517 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4518 | */ |
| 4519 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4520 | status = psa_key_derivation_start_hmac( &hmac, |
| 4521 | hash_alg, |
| 4522 | tls12_prf->secret, |
| 4523 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4524 | if( status != PSA_SUCCESS ) |
| 4525 | goto cleanup; |
| 4526 | |
| 4527 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4528 | if( tls12_prf->block_number == 1 ) |
| 4529 | { |
| 4530 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4531 | * the variable seed and in this instance means it in the context of the |
| 4532 | * P_hash function, where seed = label + seed.) */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4533 | status = psa_mac_update( &hmac, |
| 4534 | tls12_prf->label, |
| 4535 | tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4536 | if( status != PSA_SUCCESS ) |
| 4537 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4538 | status = psa_mac_update( &hmac, |
| 4539 | tls12_prf->seed, |
| 4540 | tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4541 | if( status != PSA_SUCCESS ) |
| 4542 | goto cleanup; |
| 4543 | } |
| 4544 | else |
| 4545 | { |
| 4546 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4547 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4548 | if( status != PSA_SUCCESS ) |
| 4549 | goto cleanup; |
| 4550 | } |
| 4551 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4552 | status = psa_mac_sign_finish( &hmac, |
| 4553 | tls12_prf->Ai, hash_length, |
| 4554 | &hmac_output_length ); |
| 4555 | if( hmac_output_length != hash_length ) |
| 4556 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4557 | if( status != PSA_SUCCESS ) |
| 4558 | goto cleanup; |
| 4559 | |
| 4560 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4561 | status = psa_key_derivation_start_hmac( &hmac, |
| 4562 | hash_alg, |
| 4563 | tls12_prf->secret, |
| 4564 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4565 | if( status != PSA_SUCCESS ) |
| 4566 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4567 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4568 | if( status != PSA_SUCCESS ) |
| 4569 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4570 | status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4571 | if( status != PSA_SUCCESS ) |
| 4572 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4573 | status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4574 | if( status != PSA_SUCCESS ) |
| 4575 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4576 | status = psa_mac_sign_finish( &hmac, |
| 4577 | tls12_prf->output_block, hash_length, |
| 4578 | &hmac_output_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4579 | if( status != PSA_SUCCESS ) |
| 4580 | goto cleanup; |
| 4581 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4582 | |
| 4583 | cleanup: |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4584 | cleanup_status = psa_mac_abort( &hmac ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4585 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4586 | status = cleanup_status; |
| 4587 | |
| 4588 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4589 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4590 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4591 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4592 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4593 | psa_algorithm_t alg, |
| 4594 | uint8_t *output, |
| 4595 | size_t output_length ) |
| 4596 | { |
| 4597 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4598 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4599 | psa_status_t status; |
| 4600 | uint8_t offset, length; |
| 4601 | |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4602 | switch( tls12_prf->state ) |
| 4603 | { |
| 4604 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4605 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4606 | break; |
| 4607 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4608 | break; |
| 4609 | default: |
| 4610 | return( PSA_ERROR_BAD_STATE ); |
| 4611 | } |
| 4612 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4613 | while( output_length != 0 ) |
| 4614 | { |
| 4615 | /* Check if we have fully processed the current block. */ |
| 4616 | if( tls12_prf->left_in_block == 0 ) |
| 4617 | { |
| 4618 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4619 | alg ); |
| 4620 | if( status != PSA_SUCCESS ) |
| 4621 | return( status ); |
| 4622 | |
| 4623 | continue; |
| 4624 | } |
| 4625 | |
| 4626 | if( tls12_prf->left_in_block > output_length ) |
| 4627 | length = (uint8_t) output_length; |
| 4628 | else |
| 4629 | length = tls12_prf->left_in_block; |
| 4630 | |
| 4631 | offset = hash_length - tls12_prf->left_in_block; |
| 4632 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4633 | output += length; |
| 4634 | output_length -= length; |
| 4635 | tls12_prf->left_in_block -= length; |
| 4636 | } |
| 4637 | |
| 4638 | return( PSA_SUCCESS ); |
| 4639 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4640 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4641 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4642 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4643 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 4644 | static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 4645 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
| 4646 | uint8_t *output, |
| 4647 | size_t output_length ) |
| 4648 | { |
| 4649 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4650 | size_t output_size; |
| 4651 | |
| 4652 | if( output_length != 32 ) |
| 4653 | return ( PSA_ERROR_INVALID_ARGUMENT ); |
| 4654 | |
| 4655 | status = psa_hash_compute( PSA_ALG_SHA_256, ecjpake->data, |
| 4656 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length, |
| 4657 | &output_size ); |
| 4658 | if( status != PSA_SUCCESS ) |
| 4659 | return ( status ); |
| 4660 | |
| 4661 | if( output_size != output_length ) |
| 4662 | return ( PSA_ERROR_GENERIC_ERROR ); |
| 4663 | |
| 4664 | return ( PSA_SUCCESS ); |
| 4665 | } |
| 4666 | #endif |
| 4667 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4668 | psa_status_t psa_key_derivation_output_bytes( |
| 4669 | psa_key_derivation_operation_t *operation, |
| 4670 | uint8_t *output, |
| 4671 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4672 | { |
| 4673 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4674 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4675 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4676 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4677 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4678 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4679 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4682 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4683 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4684 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4685 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4686 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4687 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4688 | goto exit; |
| 4689 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4690 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4691 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4692 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4693 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4694 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4695 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4696 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4697 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4698 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4699 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4700 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4701 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4702 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 4703 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4704 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4705 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, kdf_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4706 | output, output_length ); |
| 4707 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4708 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4709 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4710 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4711 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4712 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4713 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4714 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4715 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4716 | kdf_alg, output, |
| 4717 | output_length ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4718 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4719 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4720 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4721 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4722 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 4723 | if( PSA_ALG_IS_TLS12_ECJPAKE_TO_PMS( kdf_alg ) ) |
| 4724 | { |
| 4725 | status = psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 4726 | &operation->ctx.tls12_ecjpake_to_pms, output, output_length ); |
| 4727 | } |
| 4728 | else |
| 4729 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
| 4730 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4731 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4732 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4733 | return( PSA_ERROR_BAD_STATE ); |
| 4734 | } |
| 4735 | |
| 4736 | exit: |
| 4737 | if( status != PSA_SUCCESS ) |
| 4738 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4739 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4740 | * This allows us to differentiate between exhausted operations and |
| 4741 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4742 | * operations. */ |
| 4743 | psa_algorithm_t alg = operation->alg; |
| 4744 | psa_key_derivation_abort( operation ); |
| 4745 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4746 | memset( output, '!', output_length ); |
| 4747 | } |
| 4748 | return( status ); |
| 4749 | } |
| 4750 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4751 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4752 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4753 | { |
| 4754 | if( data_size >= 8 ) |
| 4755 | mbedtls_des_key_set_parity( data ); |
| 4756 | if( data_size >= 16 ) |
| 4757 | mbedtls_des_key_set_parity( data + 8 ); |
| 4758 | if( data_size >= 24 ) |
| 4759 | mbedtls_des_key_set_parity( data + 16 ); |
| 4760 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4761 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4762 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4763 | /* |
Przemyslaw Stekiel | 705fb0f | 2021-11-24 08:47:29 +0100 | [diff] [blame] | 4764 | * ECC keys on a Weierstrass elliptic curve require the generation |
| 4765 | * of a private key which is an integer |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4766 | * in the range [1, N - 1], where N is the boundary of the private key domain: |
| 4767 | * N is the prime p for Diffie-Hellman, or the order of the |
| 4768 | * curve’s base point for ECC. |
| 4769 | * |
| 4770 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). |
| 4771 | * This function generates the private key using the following process: |
| 4772 | * |
| 4773 | * 1. Draw a byte string of length ceiling(m/8) bytes. |
| 4774 | * 2. If m is not a multiple of 8, set the most significant |
| 4775 | * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. |
| 4776 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. |
| 4777 | * 4. If k > N - 2, discard the result and return to step 1. |
| 4778 | * 5. Output k + 1 as the private key. |
| 4779 | * |
| 4780 | * This method allows compliance to NIST standards, specifically the methods titled |
| 4781 | * Key-Pair Generation by Testing Candidates in the following publications: |
| 4782 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment |
| 4783 | * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for |
| 4784 | * Diffie-Hellman keys. |
| 4785 | * |
| 4786 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature |
| 4787 | * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4788 | * |
| 4789 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 4790 | * always NULL. |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4791 | */ |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4792 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 4793 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 4794 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4795 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 4796 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4797 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4798 | psa_key_slot_t *slot, |
| 4799 | size_t bits, |
| 4800 | psa_key_derivation_operation_t *operation, |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4801 | uint8_t **data |
| 4802 | ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4803 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4804 | unsigned key_out_of_range = 1; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4805 | mbedtls_mpi k; |
| 4806 | mbedtls_mpi diff_N_2; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4807 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4808 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4809 | |
| 4810 | mbedtls_mpi_init( &k ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4811 | mbedtls_mpi_init( &diff_N_2 ); |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4812 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4813 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 4814 | slot->attr.type ); |
| 4815 | mbedtls_ecp_group_id grp_id = |
| 4816 | mbedtls_ecc_group_of_psa( curve, bits, 0 ); |
| 4817 | |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 4818 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 4819 | { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4820 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 4821 | goto cleanup; |
| 4822 | } |
| 4823 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4824 | mbedtls_ecp_group ecp_group; |
Przemyslaw Stekiel | 6534816 | 2021-11-18 11:57:07 +0100 | [diff] [blame] | 4825 | mbedtls_ecp_group_init( &ecp_group ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4826 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4827 | MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ecp_group, grp_id ) ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4828 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4829 | /* N is the boundary of the private key domain (ecp_group.N). */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4830 | /* Let m be the bit size of N. */ |
| 4831 | size_t m = ecp_group.nbits; |
| 4832 | |
| 4833 | size_t m_bytes = PSA_BITS_TO_BYTES( m ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4834 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4835 | /* Calculate N - 2 - it will be needed later. */ |
| 4836 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &diff_N_2, &ecp_group.N, 2 ) ); |
| 4837 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4838 | /* Note: This function is always called with *data == NULL and it |
| 4839 | * allocates memory for the data buffer. */ |
| 4840 | *data = mbedtls_calloc( 1, m_bytes ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4841 | if( *data == NULL ) |
| 4842 | { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4843 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4844 | goto cleanup; |
| 4845 | } |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4846 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4847 | while( key_out_of_range ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4848 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4849 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4850 | if( ( status = psa_key_derivation_output_bytes( operation, *data, m_bytes ) ) != 0 ) |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4851 | goto cleanup; |
| 4852 | |
| 4853 | /* 2. If m is not a multiple of 8 */ |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4854 | if( m % 8 != 0 ) |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4855 | { |
| 4856 | /* Set the most significant |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4857 | * (8 * ceiling(m/8) - m) bits of the first byte in |
| 4858 | * the string to zero. |
| 4859 | */ |
| 4860 | uint8_t clear_bit_mask = ( 1 << ( m % 8 ) ) - 1; |
| 4861 | (*data)[0] &= clear_bit_mask; |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4862 | } |
| 4863 | |
| 4864 | /* 3. Convert the string to integer k by decoding it as a |
| 4865 | * big-endian byte string. |
| 4866 | */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4867 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &k, *data, m_bytes ) ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4868 | |
| 4869 | /* 4. If k > N - 2, discard the result and return to step 1. |
| 4870 | * Result of comparison is returned. When it indicates error |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 4871 | * then this function is called again. |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4872 | */ |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4873 | MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( &diff_N_2, &k, &key_out_of_range ) ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4874 | } |
| 4875 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4876 | /* 5. Output k + 1 as the private key. */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4877 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &k, &k, 1 ) ); |
| 4878 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &k, *data, m_bytes ) ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4879 | cleanup: |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4880 | if( ret != 0 ) |
Przemyslaw Stekiel | dc8d7d9 | 2021-12-02 09:15:20 +0100 | [diff] [blame] | 4881 | status = mbedtls_to_psa_error( ret ); |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4882 | if( status != PSA_SUCCESS ) { |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4883 | mbedtls_free( *data ); |
| 4884 | *data = NULL; |
| 4885 | } |
| 4886 | mbedtls_mpi_free( &k ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4887 | mbedtls_mpi_free( &diff_N_2 ); |
Przemyslaw Stekiel | dc8d7d9 | 2021-12-02 09:15:20 +0100 | [diff] [blame] | 4888 | return( status ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4889 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4890 | |
| 4891 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length |
| 4892 | * is determined by the curve, and sets the mandatory bits accordingly. That is: |
| 4893 | * |
| 4894 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): |
| 4895 | * draw a 32-byte string and process it as specified in |
| 4896 | * Elliptic Curves for Security [RFC7748] §5. |
| 4897 | * |
| 4898 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): |
| 4899 | * draw a 56-byte string and process it as specified in [RFC7748] §5. |
| 4900 | * |
| 4901 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 4902 | * always NULL. |
| 4903 | */ |
| 4904 | |
| 4905 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 4906 | size_t bits, |
| 4907 | psa_key_derivation_operation_t *operation, |
| 4908 | uint8_t **data |
| 4909 | ) |
| 4910 | { |
| 4911 | size_t output_length; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4912 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4913 | |
| 4914 | switch( bits ) |
| 4915 | { |
| 4916 | case 255: |
| 4917 | output_length = 32; |
| 4918 | break; |
| 4919 | case 448: |
| 4920 | output_length = 56; |
| 4921 | break; |
| 4922 | default: |
| 4923 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4924 | break; |
| 4925 | } |
| 4926 | |
| 4927 | *data = mbedtls_calloc( 1, output_length ); |
| 4928 | |
| 4929 | if( *data == NULL ) |
| 4930 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4931 | |
| 4932 | status = psa_key_derivation_output_bytes( operation, *data, output_length ); |
| 4933 | |
| 4934 | if( status != PSA_SUCCESS ) |
| 4935 | return status; |
| 4936 | |
| 4937 | switch( bits ) |
| 4938 | { |
| 4939 | case 255: |
| 4940 | (*data)[0] &= 248; |
| 4941 | (*data)[31] &= 127; |
| 4942 | (*data)[31] |= 64; |
| 4943 | break; |
| 4944 | case 448: |
| 4945 | (*data)[0] &= 252; |
| 4946 | (*data)[55] |= 128; |
| 4947 | break; |
| 4948 | default: |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4949 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4950 | break; |
| 4951 | } |
| 4952 | |
| 4953 | return status; |
| 4954 | } |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4955 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 4956 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 4957 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4958 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 4959 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4960 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4961 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4962 | psa_key_slot_t *slot, |
| 4963 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4964 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4965 | { |
| 4966 | uint8_t *data = NULL; |
| 4967 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4968 | size_t storage_size = bytes; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4969 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4970 | |
Przemek Stekiel | dcab6cc | 2022-03-01 14:22:29 +0100 | [diff] [blame] | 4971 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4972 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4973 | |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4974 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 4975 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 4976 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4977 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 4978 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4979 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4980 | { |
Przemyslaw Stekiel | 9248159 | 2022-01-04 10:09:46 +0100 | [diff] [blame] | 4981 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ); |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4982 | if( PSA_ECC_FAMILY_IS_WEIERSTRASS( curve ) ) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4983 | { |
| 4984 | /* Weierstrass elliptic curve */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4985 | status = psa_generate_derived_ecc_key_weierstrass_helper( slot, bits, operation, &data ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4986 | if( status != PSA_SUCCESS ) |
| 4987 | goto exit; |
Przemyslaw Stekiel | 50fcc53 | 2021-11-26 10:54:52 +0100 | [diff] [blame] | 4988 | } |
| 4989 | else |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4990 | { |
| 4991 | /* Montgomery elliptic curve */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4992 | status = psa_generate_derived_ecc_key_montgomery_helper( bits, operation, &data ); |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4993 | if( status != PSA_SUCCESS ) |
| 4994 | goto exit; |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4995 | } |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4996 | } else |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4997 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 4998 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 4999 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 5000 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 5001 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5002 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 5003 | { |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5004 | if( bits % 8 != 0 ) |
| 5005 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5006 | data = mbedtls_calloc( 1, bytes ); |
| 5007 | if( data == NULL ) |
| 5008 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5009 | |
| 5010 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
| 5011 | if( status != PSA_SUCCESS ) |
| 5012 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5013 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5014 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
| 5015 | psa_des_set_key_parity( data, bytes ); |
Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 5016 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5017 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5018 | else |
Przemek Stekiel | dcab6cc | 2022-03-01 14:22:29 +0100 | [diff] [blame] | 5019 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5020 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5021 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5022 | psa_key_attributes_t attributes = { |
| 5023 | .core = slot->attr |
| 5024 | }; |
| 5025 | |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5026 | if( psa_key_lifetime_is_external( attributes.core.lifetime ) ) |
| 5027 | { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 5028 | status = psa_driver_wrapper_get_key_buffer_size( &attributes, |
| 5029 | &storage_size ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5030 | if( status != PSA_SUCCESS ) |
| 5031 | goto exit; |
| 5032 | } |
| 5033 | status = psa_allocate_buffer_to_slot( slot, storage_size ); |
| 5034 | if( status != PSA_SUCCESS ) |
| 5035 | goto exit; |
| 5036 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5037 | status = psa_driver_wrapper_import_key( &attributes, |
| 5038 | data, bytes, |
| 5039 | slot->key.data, |
| 5040 | slot->key.bytes, |
| 5041 | &slot->key.bytes, &bits ); |
| 5042 | if( bits != slot->attr.bits ) |
| 5043 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5044 | |
| 5045 | exit: |
| 5046 | mbedtls_free( data ); |
| 5047 | return( status ); |
| 5048 | } |
| 5049 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5050 | 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] | 5051 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5052 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5053 | { |
| 5054 | psa_status_t status; |
| 5055 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5056 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5057 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5058 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5059 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5060 | /* Reject any attempt to create a zero-length key so that we don't |
| 5061 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5062 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5063 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5064 | |
Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 5065 | if( operation->alg == PSA_ALG_NONE ) |
| 5066 | return( PSA_ERROR_BAD_STATE ); |
| 5067 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5068 | if( ! operation->can_output_key ) |
| 5069 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5070 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5071 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5072 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5073 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5074 | if( driver != NULL ) |
| 5075 | { |
| 5076 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5077 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5078 | } |
| 5079 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5080 | if( status == PSA_SUCCESS ) |
| 5081 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5082 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5083 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5084 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5085 | } |
| 5086 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5087 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5088 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5089 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5090 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5091 | return( status ); |
| 5092 | } |
| 5093 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5094 | |
| 5095 | |
| 5096 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5097 | /* Key derivation */ |
| 5098 | /****************************************************************/ |
| 5099 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5100 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5101 | static int is_kdf_alg_supported( psa_algorithm_t kdf_alg ) |
| 5102 | { |
| 5103 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 5104 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5105 | return( 1 ); |
| 5106 | #endif |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5107 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 5108 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5109 | return( 1 ); |
| 5110 | #endif |
| 5111 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
| 5112 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5113 | return( 1 ); |
| 5114 | #endif |
| 5115 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5116 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5117 | return( 1 ); |
| 5118 | #endif |
| 5119 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5120 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5121 | return( 1 ); |
| 5122 | #endif |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5123 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5124 | if( PSA_ALG_IS_TLS12_ECJPAKE_TO_PMS( kdf_alg ) ) |
| 5125 | return( 1 ); |
| 5126 | #endif |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5127 | return( 0 ); |
| 5128 | } |
| 5129 | |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5130 | static psa_status_t psa_hash_try_support( psa_algorithm_t alg ) |
| 5131 | { |
| 5132 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 5133 | psa_status_t status = psa_hash_setup( &operation, alg ); |
| 5134 | psa_hash_abort( &operation ); |
| 5135 | return( status ); |
| 5136 | } |
| 5137 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5138 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5139 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5140 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5141 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5142 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5143 | * initialisers for this union leave some bytes unspecified.) */ |
| 5144 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5145 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5146 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5147 | if( ! is_kdf_alg_supported( kdf_alg ) ) |
| 5148 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5149 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5150 | /* All currently supported key derivation algorithms (apart from |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5151 | * ecjpake to pms) are based on a hash algorithm. */ |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5152 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
| 5153 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5154 | if( !PSA_ALG_IS_TLS12_ECJPAKE_TO_PMS( kdf_alg ) ) |
| 5155 | { |
| 5156 | if( hash_size == 0 ) |
| 5157 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5158 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5159 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 5160 | * we might fail later, which is somewhat unfriendly and potentially |
| 5161 | * risk-prone. */ |
| 5162 | psa_status_t status = psa_hash_try_support( hash_alg ); |
| 5163 | if( status != PSA_SUCCESS ) |
| 5164 | return( status ); |
| 5165 | } |
| 5166 | else |
| 5167 | { |
| 5168 | hash_size = PSA_HASH_LENGTH( PSA_ALG_SHA_256 ); |
| 5169 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5170 | |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5171 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5172 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
| 5173 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5174 | { |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5175 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5176 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5177 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Andrzej Kurek | b093650 | 2022-09-16 07:13:00 -0400 | [diff] [blame^] | 5178 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) || |
| 5179 | PSA_ALG_IS_TLS12_ECJPAKE_TO_PMS ( kdf_alg )) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5180 | operation->capacity = hash_size; |
| 5181 | else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5182 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5183 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5184 | return( PSA_SUCCESS ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5185 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5186 | |
| 5187 | static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg ) |
| 5188 | { |
| 5189 | #if defined(PSA_WANT_ALG_ECDH) |
| 5190 | if( alg == PSA_ALG_ECDH ) |
| 5191 | return( PSA_SUCCESS ); |
| 5192 | #endif |
| 5193 | (void) alg; |
| 5194 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5195 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5196 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5197 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5198 | 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] | 5199 | psa_algorithm_t alg ) |
| 5200 | { |
| 5201 | psa_status_t status; |
| 5202 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5203 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5204 | return( PSA_ERROR_BAD_STATE ); |
| 5205 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5206 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5207 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5208 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5209 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5210 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5211 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5212 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ); |
| 5213 | status = psa_key_agreement_try_support( ka_alg ); |
| 5214 | if( status != PSA_SUCCESS ) |
| 5215 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5216 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5217 | #else |
| 5218 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5219 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5220 | } |
| 5221 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5222 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5223 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5224 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5225 | #else |
| 5226 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5227 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5228 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5229 | else |
| 5230 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5231 | |
| 5232 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5233 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5234 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5235 | } |
| 5236 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5237 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5238 | static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf, |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5239 | psa_algorithm_t kdf_alg, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5240 | psa_key_derivation_step_t step, |
| 5241 | const uint8_t *data, |
| 5242 | size_t data_length ) |
| 5243 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5244 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5245 | psa_status_t status; |
| 5246 | switch( step ) |
| 5247 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5248 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5249 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5250 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
| 5251 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5252 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5253 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5254 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5255 | else |
| 5256 | { |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5257 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 5258 | hash_alg, |
| 5259 | data, data_length ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5260 | if( status != PSA_SUCCESS ) |
| 5261 | return( status ); |
| 5262 | hkdf->state = HKDF_STATE_STARTED; |
| 5263 | return( PSA_SUCCESS ); |
| 5264 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5265 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5266 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5267 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5268 | { |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 5269 | /* We shouldn't be in different state as HKDF_EXPAND only allows |
| 5270 | * two inputs: SECRET (this case) and INFO which does not modify |
| 5271 | * the state. It could happen only if the hkdf |
| 5272 | * object was corrupted. */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5273 | if( hkdf->state != HKDF_STATE_INIT ) |
| 5274 | return( PSA_ERROR_BAD_STATE ); |
| 5275 | |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 5276 | /* Allow only input that fits expected prk size */ |
| 5277 | if( data_length != PSA_HASH_LENGTH( hash_alg ) ) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5278 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5279 | |
| 5280 | memcpy( hkdf->prk, data, data_length ); |
| 5281 | } |
| 5282 | else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5283 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5284 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5285 | /* HKDF: If no salt was provided, use an empty salt. |
| 5286 | * HKDF-EXTRACT: salt is mandatory. */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5287 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5288 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5289 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 5290 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5291 | return( PSA_ERROR_BAD_STATE ); |
| 5292 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5293 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 5294 | hash_alg, |
| 5295 | NULL, 0 ); |
| 5296 | if( status != PSA_SUCCESS ) |
| 5297 | return( status ); |
| 5298 | hkdf->state = HKDF_STATE_STARTED; |
| 5299 | } |
| 5300 | if( hkdf->state != HKDF_STATE_STARTED ) |
| 5301 | return( PSA_ERROR_BAD_STATE ); |
| 5302 | status = psa_mac_update( &hkdf->hmac, |
| 5303 | data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5304 | if( status != PSA_SUCCESS ) |
| 5305 | return( status ); |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5306 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 5307 | hkdf->prk, |
| 5308 | sizeof( hkdf->prk ), |
| 5309 | &data_length ); |
| 5310 | if( status != PSA_SUCCESS ) |
| 5311 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5312 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5313 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5314 | hkdf->state = HKDF_STATE_KEYED; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5315 | hkdf->block_number = 0; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5316 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5317 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5318 | { |
| 5319 | /* The only block of output is the PRK. */ |
| 5320 | memcpy( hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH( hash_alg ) ); |
| 5321 | hkdf->offset_in_block = 0; |
| 5322 | } |
| 5323 | else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5324 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5325 | { |
| 5326 | /* Block 0 is empty, and the next block will be |
| 5327 | * generated by psa_key_derivation_hkdf_read(). */ |
| 5328 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
| 5329 | } |
| 5330 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5331 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5332 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5333 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5334 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5335 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5336 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 5337 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
| 5338 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) && |
| 5339 | hkdf->state == HKDF_STATE_INIT ) |
| 5340 | return( PSA_ERROR_BAD_STATE ); |
| 5341 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5342 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5343 | return( PSA_ERROR_BAD_STATE ); |
| 5344 | if( hkdf->info_set ) |
| 5345 | return( PSA_ERROR_BAD_STATE ); |
| 5346 | hkdf->info_length = data_length; |
| 5347 | if( data_length != 0 ) |
| 5348 | { |
| 5349 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5350 | if( hkdf->info == NULL ) |
| 5351 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5352 | memcpy( hkdf->info, data, data_length ); |
| 5353 | } |
| 5354 | hkdf->info_set = 1; |
| 5355 | return( PSA_SUCCESS ); |
| 5356 | default: |
| 5357 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5358 | } |
| 5359 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5360 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5361 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5362 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5363 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5364 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5365 | const uint8_t *data, |
| 5366 | size_t data_length ) |
| 5367 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5368 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5369 | return( PSA_ERROR_BAD_STATE ); |
| 5370 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5371 | if( data_length != 0 ) |
| 5372 | { |
| 5373 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5374 | if( prf->seed == NULL ) |
| 5375 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5376 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5377 | memcpy( prf->seed, data, data_length ); |
| 5378 | prf->seed_length = data_length; |
| 5379 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5380 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5381 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5382 | |
| 5383 | return( PSA_SUCCESS ); |
| 5384 | } |
| 5385 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5386 | static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5387 | const uint8_t *data, |
| 5388 | size_t data_length ) |
| 5389 | { |
Przemek Stekiel | d7a2864 | 2022-04-07 14:58:33 +0200 | [diff] [blame] | 5390 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET && |
| 5391 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5392 | return( PSA_ERROR_BAD_STATE ); |
| 5393 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5394 | if( data_length != 0 ) |
| 5395 | { |
| 5396 | prf->secret = mbedtls_calloc( 1, data_length ); |
| 5397 | if( prf->secret == NULL ) |
| 5398 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5399 | |
| 5400 | memcpy( prf->secret, data, data_length ); |
| 5401 | prf->secret_length = data_length; |
| 5402 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5403 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5404 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5405 | |
| 5406 | return( PSA_SUCCESS ); |
| 5407 | } |
| 5408 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5409 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5410 | const uint8_t *data, |
| 5411 | size_t data_length ) |
| 5412 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5413 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5414 | return( PSA_ERROR_BAD_STATE ); |
| 5415 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5416 | if( data_length != 0 ) |
| 5417 | { |
| 5418 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5419 | if( prf->label == NULL ) |
| 5420 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5421 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5422 | memcpy( prf->label, data, data_length ); |
| 5423 | prf->label_length = data_length; |
| 5424 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5425 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5426 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5427 | |
| 5428 | return( PSA_SUCCESS ); |
| 5429 | } |
| 5430 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5431 | static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5432 | psa_key_derivation_step_t step, |
| 5433 | const uint8_t *data, |
| 5434 | size_t data_length ) |
| 5435 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5436 | switch( step ) |
| 5437 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5438 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5439 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5440 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5441 | return( psa_tls12_prf_set_key( prf, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5442 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5443 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5444 | default: |
| 5445 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5446 | } |
| 5447 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5448 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5449 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5450 | |
| 5451 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5452 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5453 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5454 | const uint8_t *data, |
| 5455 | size_t data_length ) |
| 5456 | { |
| 5457 | psa_status_t status; |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5458 | const size_t pms_len = ( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? |
| 5459 | 4 + data_length + prf->other_secret_length : |
| 5460 | 4 + 2 * data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5461 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5462 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5463 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5464 | |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5465 | uint8_t *pms = mbedtls_calloc( 1, pms_len ); |
Przemek Stekiel | 937b90f | 2022-04-20 08:33:13 +0200 | [diff] [blame] | 5466 | if( pms == NULL ) |
| 5467 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5468 | uint8_t *cur = pms; |
| 5469 | |
| 5470 | /* pure-PSK: |
| 5471 | * Quoting RFC 4279, Section 2: |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5472 | * |
| 5473 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5474 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5475 | * uint16 with the value N, and the PSK itself. |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5476 | * |
| 5477 | * mixed-PSK: |
| 5478 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as |
| 5479 | * follows: concatenate a uint16 with the length of the other secret, |
| 5480 | * the other secret itself, uint16 with the length of PSK, and the |
| 5481 | * PSK itself. |
| 5482 | * For details please check: |
| 5483 | * - RFC 4279, Section 4 for the definition of RSA-PSK, |
| 5484 | * - RFC 4279, Section 3 for the definition of DHE-PSK, |
| 5485 | * - RFC 5489 for the definition of ECDHE-PSK. |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5486 | */ |
| 5487 | |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 5488 | if( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ) |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5489 | { |
| 5490 | *cur++ = MBEDTLS_BYTE_1( prf->other_secret_length ); |
| 5491 | *cur++ = MBEDTLS_BYTE_0( prf->other_secret_length ); |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 5492 | if( prf->other_secret_length != 0 ) |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 5493 | { |
| 5494 | memcpy( cur, prf->other_secret, prf->other_secret_length ); |
Przemek Stekiel | 03faf5d2 | 2022-04-20 08:37:43 +0200 | [diff] [blame] | 5495 | mbedtls_platform_zeroize( prf->other_secret, prf->other_secret_length ); |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 5496 | cur += prf->other_secret_length; |
| 5497 | } |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5498 | } |
| 5499 | else |
| 5500 | { |
| 5501 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 5502 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
| 5503 | memset( cur, 0, data_length ); |
| 5504 | cur += data_length; |
| 5505 | } |
| 5506 | |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 5507 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 5508 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5509 | memcpy( cur, data, data_length ); |
| 5510 | cur += data_length; |
| 5511 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5512 | status = psa_tls12_prf_set_key( prf, pms, cur - pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5513 | |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5514 | mbedtls_platform_zeroize( pms, pms_len ); |
| 5515 | mbedtls_free( pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5516 | return( status ); |
| 5517 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5518 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5519 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( |
| 5520 | psa_tls12_prf_key_derivation_t *prf, |
| 5521 | const uint8_t *data, |
| 5522 | size_t data_length ) |
| 5523 | { |
| 5524 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
| 5525 | return( PSA_ERROR_BAD_STATE ); |
| 5526 | |
| 5527 | if( data_length != 0 ) |
| 5528 | { |
| 5529 | prf->other_secret = mbedtls_calloc( 1, data_length ); |
| 5530 | if( prf->other_secret == NULL ) |
| 5531 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5532 | |
| 5533 | memcpy( prf->other_secret, data, data_length ); |
| 5534 | prf->other_secret_length = data_length; |
| 5535 | } |
| 5536 | else |
| 5537 | { |
| 5538 | prf->other_secret_length = 0; |
| 5539 | } |
| 5540 | |
| 5541 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; |
| 5542 | |
| 5543 | return( PSA_SUCCESS ); |
| 5544 | } |
| 5545 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5546 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5547 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5548 | psa_key_derivation_step_t step, |
| 5549 | const uint8_t *data, |
| 5550 | size_t data_length ) |
| 5551 | { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5552 | switch( step ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5553 | { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5554 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5555 | return( psa_tls12_prf_psk_to_ms_set_key( prf, |
| 5556 | data, data_length ) ); |
| 5557 | break; |
| 5558 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
| 5559 | return( psa_tls12_prf_psk_to_ms_set_other_key( prf, |
| 5560 | data, |
| 5561 | data_length ) ); |
| 5562 | break; |
| 5563 | default: |
| 5564 | return( psa_tls12_prf_input( prf, step, data, data_length ) ); |
| 5565 | break; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5566 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5567 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5568 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5569 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5570 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5571 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5572 | static psa_status_t psa_tls12_ecjpake_to_pms_input( |
| 5573 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5574 | psa_key_derivation_step_t step, |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5575 | const uint8_t *data, |
| 5576 | size_t data_length ) |
| 5577 | { |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5578 | if( data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE || |
| 5579 | step != PSA_KEY_DERIVATION_INPUT_SECRET ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5580 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5581 | |
| 5582 | /* Check if the passed point is in an uncompressed form */ |
| 5583 | if( data[0] != 0x04 ) |
| 5584 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5585 | |
| 5586 | /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */ |
| 5587 | memcpy( ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE ); |
| 5588 | |
| 5589 | return( PSA_SUCCESS ); |
| 5590 | } |
| 5591 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5592 | /** Check whether the given key type is acceptable for the given |
| 5593 | * input step of a key derivation. |
| 5594 | * |
| 5595 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5596 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5597 | * Both secret and non-secret inputs can alternatively have the type |
| 5598 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5599 | * that the input was passed as a buffer rather than via a key object. |
| 5600 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5601 | static int psa_key_derivation_check_input_type( |
| 5602 | psa_key_derivation_step_t step, |
| 5603 | psa_key_type_t key_type ) |
| 5604 | { |
| 5605 | switch( step ) |
| 5606 | { |
| 5607 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5608 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5609 | return( PSA_SUCCESS ); |
| 5610 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5611 | return( PSA_SUCCESS ); |
| 5612 | break; |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 5613 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
| 5614 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5615 | return( PSA_SUCCESS ); |
| 5616 | if( key_type == PSA_KEY_TYPE_NONE ) |
| 5617 | return( PSA_SUCCESS ); |
| 5618 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5619 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5620 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5621 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5622 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5623 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5624 | return( PSA_SUCCESS ); |
| 5625 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5626 | return( PSA_SUCCESS ); |
| 5627 | break; |
| 5628 | } |
| 5629 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5630 | } |
| 5631 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5632 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5633 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5634 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5635 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5636 | const uint8_t *data, |
| 5637 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5638 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5639 | psa_status_t status; |
| 5640 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5641 | |
| 5642 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5643 | if( status != PSA_SUCCESS ) |
| 5644 | goto exit; |
| 5645 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5646 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 5647 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5648 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5649 | status = psa_hkdf_input( &operation->ctx.hkdf, kdf_alg, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5650 | step, data, data_length ); |
| 5651 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5652 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5653 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5654 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5655 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5656 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5657 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5658 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5659 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5660 | else |
| 5661 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 5662 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5663 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5664 | { |
| 5665 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5666 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5667 | } |
| 5668 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5669 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5670 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5671 | if( PSA_ALG_IS_TLS12_ECJPAKE_TO_PMS( kdf_alg ) ) |
| 5672 | { |
| 5673 | status = psa_tls12_ecjpake_to_pms_input( |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5674 | &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length ); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5675 | } |
| 5676 | else |
| 5677 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5678 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5679 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5680 | (void) data; |
| 5681 | (void) data_length; |
| 5682 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5683 | return( PSA_ERROR_BAD_STATE ); |
| 5684 | } |
| 5685 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5686 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5687 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5688 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5689 | return( status ); |
| 5690 | } |
| 5691 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5692 | psa_status_t psa_key_derivation_input_bytes( |
| 5693 | psa_key_derivation_operation_t *operation, |
| 5694 | psa_key_derivation_step_t step, |
| 5695 | const uint8_t *data, |
| 5696 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5697 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5698 | return( psa_key_derivation_input_internal( operation, step, |
| 5699 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5700 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5701 | } |
| 5702 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5703 | psa_status_t psa_key_derivation_input_key( |
| 5704 | psa_key_derivation_operation_t *operation, |
| 5705 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5706 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5707 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5708 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5709 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5710 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5711 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5712 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5713 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5714 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5715 | { |
| 5716 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5717 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5718 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5719 | |
| 5720 | /* Passing a key object as a SECRET input unlocks the permission |
| 5721 | * to output to a key object. */ |
| 5722 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5723 | operation->can_output_key = 1; |
| 5724 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5725 | status = psa_key_derivation_input_internal( operation, |
| 5726 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5727 | slot->key.data, |
| 5728 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5729 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5730 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5731 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5732 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5733 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5734 | |
| 5735 | |
| 5736 | |
| 5737 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5738 | /* Key agreement */ |
| 5739 | /****************************************************************/ |
| 5740 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5741 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5742 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5743 | size_t peer_key_length, |
| 5744 | const mbedtls_ecp_keypair *our_key, |
| 5745 | uint8_t *shared_secret, |
| 5746 | size_t shared_secret_size, |
| 5747 | size_t *shared_secret_length ) |
| 5748 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5749 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5750 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5751 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5752 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5753 | 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] | 5754 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5755 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5756 | status = mbedtls_psa_ecp_load_representation( |
| 5757 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5758 | bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5759 | peer_key, |
| 5760 | peer_key_length, |
| 5761 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5762 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5763 | goto exit; |
| 5764 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5765 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5766 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5767 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5768 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5769 | status = mbedtls_to_psa_error( |
| 5770 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5771 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5772 | goto exit; |
| 5773 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5774 | status = mbedtls_to_psa_error( |
| 5775 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5776 | shared_secret_length, |
| 5777 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5778 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5779 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5780 | if( status != PSA_SUCCESS ) |
| 5781 | goto exit; |
| 5782 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5783 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5784 | |
| 5785 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5786 | if( status != PSA_SUCCESS ) |
| 5787 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5788 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5789 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 5790 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5791 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5792 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5793 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5794 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5795 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5796 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5797 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5798 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5799 | psa_key_slot_t *private_key, |
| 5800 | const uint8_t *peer_key, |
| 5801 | size_t peer_key_length, |
| 5802 | uint8_t *shared_secret, |
| 5803 | size_t shared_secret_size, |
| 5804 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5805 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5806 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5807 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5808 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5809 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5810 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5811 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5812 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5813 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
| 5814 | private_key->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5815 | private_key->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5816 | private_key->key.data, |
| 5817 | private_key->key.bytes, |
| 5818 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5819 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5820 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5821 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5822 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5823 | shared_secret, shared_secret_size, |
| 5824 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5825 | mbedtls_ecp_keypair_free( ecp ); |
| 5826 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5827 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5828 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5829 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5830 | (void) private_key; |
| 5831 | (void) peer_key; |
| 5832 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5833 | (void) shared_secret; |
| 5834 | (void) shared_secret_size; |
| 5835 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5836 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5837 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5838 | } |
| 5839 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5840 | /* 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] | 5841 | * to potentially free embedded data structures and wipe confidential data. |
| 5842 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5843 | 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] | 5844 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5845 | psa_key_slot_t *private_key, |
| 5846 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5847 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5848 | { |
| 5849 | psa_status_t status; |
| 5850 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5851 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5852 | 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] | 5853 | |
| 5854 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5855 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5856 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5857 | private_key, |
| 5858 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5859 | shared_secret, |
| 5860 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5861 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5862 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5863 | goto exit; |
| 5864 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5865 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5866 | * the shared secret. A shared secret is permitted wherever a key |
| 5867 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5868 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5869 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5870 | shared_secret, |
| 5871 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5872 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5873 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5874 | return( status ); |
| 5875 | } |
| 5876 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5877 | 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] | 5878 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5879 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5880 | const uint8_t *peer_key, |
| 5881 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5882 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5883 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5884 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5885 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5886 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5887 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5888 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5889 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5890 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5891 | if( status != PSA_SUCCESS ) |
| 5892 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5893 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5894 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5895 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5896 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5897 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5898 | else |
| 5899 | { |
| 5900 | /* If a private key has been added as SECRET, we allow the derived |
| 5901 | * key material to be used as a key in PSA Crypto. */ |
| 5902 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5903 | operation->can_output_key = 1; |
| 5904 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5905 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5906 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5907 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5908 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5909 | } |
| 5910 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5911 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5912 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5913 | const uint8_t *peer_key, |
| 5914 | size_t peer_key_length, |
| 5915 | uint8_t *output, |
| 5916 | size_t output_size, |
| 5917 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5918 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5919 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5920 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5921 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5922 | |
| 5923 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5924 | { |
| 5925 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5926 | goto exit; |
| 5927 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5928 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5929 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5930 | if( status != PSA_SUCCESS ) |
| 5931 | goto exit; |
| 5932 | |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5933 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 5934 | * for the output size. The PSA specification only guarantees that this |
| 5935 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 5936 | * but it might be nice to allow smaller buffers if the output fits. |
| 5937 | * At the time of writing this comment, with only ECDH implemented, |
| 5938 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 5939 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 5940 | * be exact for it as well. */ |
| 5941 | size_t expected_length = |
| 5942 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits ); |
| 5943 | if( output_size < expected_length ) |
| 5944 | { |
| 5945 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5946 | goto exit; |
| 5947 | } |
| 5948 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5949 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5950 | peer_key, peer_key_length, |
| 5951 | output, output_size, |
| 5952 | output_length ); |
| 5953 | |
| 5954 | exit: |
| 5955 | if( status != PSA_SUCCESS ) |
| 5956 | { |
| 5957 | /* If an error happens and is not handled properly, the output |
| 5958 | * may be used as a key to protect sensitive data. Arrange for such |
| 5959 | * a key to be random, which is likely to result in decryption or |
| 5960 | * verification errors. This is better than filling the buffer with |
| 5961 | * some constant data such as zeros, which would result in the data |
| 5962 | * being protected with a reproducible, easily knowable key. |
| 5963 | */ |
| 5964 | psa_generate_random( output, output_size ); |
| 5965 | *output_length = output_size; |
| 5966 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5967 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5968 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5969 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5970 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5971 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5972 | |
| 5973 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5974 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5975 | /****************************************************************/ |
| 5976 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5977 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5978 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5979 | /** Initialize the PSA random generator. |
| 5980 | */ |
| 5981 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 5982 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5983 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5984 | memset( rng, 0, sizeof( *rng ) ); |
| 5985 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5986 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5987 | /* Set default configuration if |
| 5988 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5989 | if( rng->entropy_init == NULL ) |
| 5990 | rng->entropy_init = mbedtls_entropy_init; |
| 5991 | if( rng->entropy_free == NULL ) |
| 5992 | rng->entropy_free = mbedtls_entropy_free; |
| 5993 | |
| 5994 | rng->entropy_init( &rng->entropy ); |
| 5995 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5996 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5997 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5998 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5999 | mbedtls_entropy_add_source( &rng->entropy, |
| 6000 | mbedtls_nv_seed_poll, NULL, |
| 6001 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6002 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6003 | #endif |
| 6004 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6005 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6006 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6007 | } |
| 6008 | |
| 6009 | /** Deinitialize the PSA random generator. |
| 6010 | */ |
| 6011 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 6012 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6013 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6014 | memset( rng, 0, sizeof( *rng ) ); |
| 6015 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6016 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6017 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6018 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6019 | } |
| 6020 | |
| 6021 | /** Seed the PSA random generator. |
| 6022 | */ |
| 6023 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 6024 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6025 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6026 | /* Do nothing: the external RNG seeds itself. */ |
| 6027 | (void) rng; |
| 6028 | return( PSA_SUCCESS ); |
| 6029 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6030 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6031 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 6032 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6033 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6034 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6035 | } |
| 6036 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6037 | psa_status_t psa_generate_random( uint8_t *output, |
| 6038 | size_t output_size ) |
| 6039 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6040 | GUARD_MODULE_INITIALIZED; |
| 6041 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6042 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6043 | |
| 6044 | size_t output_length = 0; |
| 6045 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 6046 | output, output_size, |
| 6047 | &output_length ); |
| 6048 | if( status != PSA_SUCCESS ) |
| 6049 | return( status ); |
| 6050 | /* Breaking up a request into smaller chunks is currently not supported |
| 6051 | * for the extrernal RNG interface. */ |
| 6052 | if( output_length != output_size ) |
| 6053 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6054 | return( PSA_SUCCESS ); |
| 6055 | |
| 6056 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6057 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6058 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6059 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6060 | size_t request_size = |
| 6061 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6062 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6063 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6064 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 6065 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6066 | if( ret != 0 ) |
| 6067 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6068 | output_size -= request_size; |
| 6069 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6070 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6071 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6072 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6073 | } |
| 6074 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6075 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6076 | * |
| 6077 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6078 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6079 | * PSA API doesn't support passing an explicit state. |
| 6080 | * |
| 6081 | * In the non-external case, psa_generate_random() calls an |
| 6082 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6083 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6084 | * instead of doing this back-and-forth between the PSA API and the |
| 6085 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6086 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6087 | */ |
| 6088 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6089 | int mbedtls_psa_get_random( void *p_rng, |
| 6090 | unsigned char *output, |
| 6091 | size_t output_size ) |
| 6092 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6093 | /* This function takes a pointer to the RNG state because that's what |
| 6094 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6095 | * its own state internally and doesn't let the caller access that state. |
| 6096 | * So we just ignore the state parameter, and in practice we'll pass |
| 6097 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6098 | (void) p_rng; |
| 6099 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6100 | if( status == PSA_SUCCESS ) |
| 6101 | return( 0 ); |
| 6102 | else |
| 6103 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6104 | } |
| 6105 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6106 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6107 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Chris Jones | ea0a865 | 2021-03-09 19:11:19 +0000 | [diff] [blame] | 6108 | #include "entropy_poll.h" |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6109 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6110 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6111 | size_t seed_size ) |
| 6112 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6113 | if( global_data.initialized ) |
| 6114 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6115 | |
| 6116 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6117 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6118 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6119 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6120 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6121 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6122 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6123 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6124 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6125 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6126 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6127 | * \param type The key type |
| 6128 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6129 | * |
| 6130 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6131 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6132 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 6133 | * The size in bits of the key is not valid. |
| 6134 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 6135 | * The type and/or the size in bits of the key or the combination of |
| 6136 | * the two is not supported. |
| 6137 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6138 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 6139 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6140 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6141 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6142 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6143 | if( key_type_is_raw_bytes( type ) ) |
| 6144 | { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 6145 | status = psa_validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6146 | if( status != PSA_SUCCESS ) |
| 6147 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6148 | } |
| 6149 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6150 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6151 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6152 | { |
| 6153 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6154 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6155 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6156 | /* Accept only byte-aligned keys, for the same reasons as |
| 6157 | * in psa_import_rsa_key(). */ |
| 6158 | if( bits % 8 != 0 ) |
| 6159 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6160 | } |
| 6161 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6162 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6163 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6164 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6165 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6166 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 6167 | /* To avoid empty block, return successfully here. */ |
| 6168 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6169 | } |
| 6170 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6171 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6172 | { |
| 6173 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6174 | } |
| 6175 | |
| 6176 | return( PSA_SUCCESS ); |
| 6177 | } |
| 6178 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 6179 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6180 | const psa_key_attributes_t *attributes, |
| 6181 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6182 | { |
| 6183 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6184 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6185 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6186 | if( ( attributes->domain_parameters == NULL ) && |
| 6187 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6188 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6189 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6190 | if( key_type_is_raw_bytes( type ) ) |
| 6191 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6192 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6193 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6194 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6195 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6196 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6197 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6198 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 6199 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6200 | } |
| 6201 | else |
| 6202 | |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6203 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 6204 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6205 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6206 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 6207 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 6208 | key_buffer, |
| 6209 | key_buffer_size, |
| 6210 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6211 | } |
| 6212 | else |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6213 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 6214 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6215 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6216 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6217 | 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] | 6218 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 6219 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 6220 | key_buffer, |
| 6221 | key_buffer_size, |
| 6222 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6223 | } |
| 6224 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6225 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6226 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6227 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6228 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6229 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6230 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6231 | return( PSA_SUCCESS ); |
| 6232 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6233 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6234 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6235 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6236 | { |
| 6237 | psa_status_t status; |
| 6238 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6239 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6240 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6241 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6242 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6243 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6244 | /* Reject any attempt to create a zero-length key so that we don't |
| 6245 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6246 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6248 | |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6249 | /* Reject any attempt to create a public key. */ |
| 6250 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) ) |
| 6251 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6252 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6253 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6254 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6255 | if( status != PSA_SUCCESS ) |
| 6256 | goto exit; |
| 6257 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6258 | /* In the case of a transparent key or an opaque key stored in local |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 6259 | * storage ( thus not in the case of generating a key in a secure element |
| 6260 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 6261 | * buffer to hold the generated key material. */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6262 | if( slot->key.data == NULL ) |
| 6263 | { |
| 6264 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 6265 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 6266 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6267 | status = psa_validate_key_type_and_size_for_key_generation( |
| 6268 | attributes->core.type, attributes->core.bits ); |
| 6269 | if( status != PSA_SUCCESS ) |
| 6270 | goto exit; |
| 6271 | |
| 6272 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 6273 | attributes->core.type, |
| 6274 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6275 | } |
| 6276 | else |
| 6277 | { |
| 6278 | status = psa_driver_wrapper_get_key_buffer_size( |
| 6279 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6280 | if( status != PSA_SUCCESS ) |
| 6281 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6282 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6283 | |
| 6284 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 6285 | if( status != PSA_SUCCESS ) |
| 6286 | goto exit; |
| 6287 | } |
| 6288 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6289 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6290 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6291 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6292 | if( status != PSA_SUCCESS ) |
| 6293 | psa_remove_key_data_from_memory( slot ); |
| 6294 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6295 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6296 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6297 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6298 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6299 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6300 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6301 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6302 | } |
| 6303 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6304 | /****************************************************************/ |
| 6305 | /* Module setup */ |
| 6306 | /****************************************************************/ |
| 6307 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6308 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6309 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6310 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6311 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6312 | { |
| 6313 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6314 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6315 | global_data.rng.entropy_init = entropy_init; |
| 6316 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6317 | return( PSA_SUCCESS ); |
| 6318 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6319 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6320 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6321 | void mbedtls_psa_crypto_free( void ) |
| 6322 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6323 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6324 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6325 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6326 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6327 | } |
| 6328 | /* Wipe all remaining data, including configuration. |
| 6329 | * In particular, this sets all state indicator to the value |
| 6330 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6331 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 6332 | |
| 6333 | /* Terminate drivers */ |
| 6334 | psa_driver_wrapper_free( ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6335 | } |
| 6336 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6337 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6338 | /** Recover a transaction that was interrupted by a power failure. |
| 6339 | * |
| 6340 | * This function is called during initialization, before psa_crypto_init() |
| 6341 | * returns. If this function returns a failure status, the initialization |
| 6342 | * fails. |
| 6343 | */ |
| 6344 | static psa_status_t psa_crypto_recover_transaction( |
| 6345 | const psa_crypto_transaction_t *transaction ) |
| 6346 | { |
| 6347 | switch( transaction->unknown.type ) |
| 6348 | { |
| 6349 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6350 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6351 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6352 | * is implemented. |
| 6353 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6354 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6355 | default: |
| 6356 | /* We found an unsupported transaction in the storage. |
| 6357 | * We don't know what state the storage is in. Give up. */ |
gabor-mezei-arm | fe30924 | 2020-11-09 17:39:56 +0100 | [diff] [blame] | 6358 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6359 | } |
| 6360 | } |
| 6361 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6362 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6363 | psa_status_t psa_crypto_init( void ) |
| 6364 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6365 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6366 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6367 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6368 | if( global_data.initialized != 0 ) |
| 6369 | return( PSA_SUCCESS ); |
| 6370 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6371 | /* Initialize and seed the random generator. */ |
| 6372 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6373 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6374 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6375 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6376 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6377 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6378 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6379 | status = psa_initialize_key_slots( ); |
| 6380 | if( status != PSA_SUCCESS ) |
| 6381 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6382 | |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 6383 | /* Init drivers */ |
| 6384 | status = psa_driver_wrapper_init( ); |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6385 | if( status != PSA_SUCCESS ) |
| 6386 | goto exit; |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6387 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6388 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6389 | status = psa_crypto_load_transaction( ); |
| 6390 | if( status == PSA_SUCCESS ) |
| 6391 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6392 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6393 | if( status != PSA_SUCCESS ) |
| 6394 | goto exit; |
| 6395 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6396 | } |
| 6397 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6398 | { |
| 6399 | /* There's no transaction to complete. It's all good. */ |
| 6400 | status = PSA_SUCCESS; |
| 6401 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6402 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6403 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6404 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6405 | global_data.initialized = 1; |
| 6406 | |
| 6407 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6408 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6409 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6410 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6411 | } |
| 6412 | |
| 6413 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |