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 | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 55 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 56 | #include "mbedtls/aes.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 57 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 58 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 59 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 61 | #include "mbedtls/chacha20.h" |
| 62 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 63 | #include "mbedtls/cipher.h" |
| 64 | #include "mbedtls/ccm.h" |
| 65 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 66 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 67 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 68 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 69 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 70 | #include "mbedtls/error.h" |
| 71 | #include "mbedtls/gcm.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 72 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 73 | #include "mbedtls/md.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 74 | #include "md_wrap.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 75 | #include "mbedtls/pk.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 76 | #include "pk_wrap.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 77 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 78 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 79 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 80 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 81 | #include "mbedtls/sha1.h" |
| 82 | #include "mbedtls/sha256.h" |
| 83 | #include "mbedtls/sha512.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 84 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 85 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 86 | |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 87 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 88 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
| 89 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 90 | #define BUILTIN_ALG_ANY_HKDF 1 |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 91 | #endif |
| 92 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 93 | /****************************************************************/ |
| 94 | /* Global data, support functions and library management */ |
| 95 | /****************************************************************/ |
| 96 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 97 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 98 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 99 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 102 | /* Values for psa_global_data_t::rng_state */ |
| 103 | #define RNG_NOT_INITIALIZED 0 |
| 104 | #define RNG_INITIALIZED 1 |
| 105 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 106 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 107 | typedef struct |
| 108 | { |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 109 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 110 | unsigned rng_state : 2; |
Gilles Peskine | 2d8a182 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 111 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 112 | } psa_global_data_t; |
| 113 | |
| 114 | static psa_global_data_t global_data; |
| 115 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 116 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 117 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 118 | &global_data.rng.drbg; |
| 119 | #endif |
| 120 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 121 | #define GUARD_MODULE_INITIALIZED \ |
| 122 | if( global_data.initialized == 0 ) \ |
| 123 | return( PSA_ERROR_BAD_STATE ); |
| 124 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 125 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 126 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 127 | /* Mbed TLS error codes can combine a high-level error code and a |
| 128 | * low-level error code. The low-level error usually reflects the |
| 129 | * root cause better, so dispatch on that preferably. */ |
| 130 | int low_level_ret = - ( -ret & 0x007f ); |
| 131 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 132 | { |
| 133 | case 0: |
| 134 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 135 | |
| 136 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 137 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 138 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 139 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 140 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 141 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 142 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 143 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 144 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 145 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 146 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 147 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 148 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 149 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 150 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 151 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 152 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 153 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 154 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 155 | |
| 156 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 157 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 158 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 159 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 160 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 161 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 162 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 163 | |
| 164 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 165 | return( PSA_ERROR_BAD_STATE ); |
| 166 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 167 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 168 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 169 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 170 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 171 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 172 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 173 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 174 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 175 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 176 | return( PSA_ERROR_INVALID_PADDING ); |
| 177 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 178 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 179 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 180 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 181 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 182 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 183 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 184 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 185 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 186 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 187 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 188 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 189 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 190 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 191 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 192 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 193 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 194 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 195 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 196 | |
| 197 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 198 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 199 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 200 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 201 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 202 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 203 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 204 | |
| 205 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 206 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Mateusz Starzyk | f28261f | 2021-09-30 16:39:07 +0200 | [diff] [blame] | 207 | case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL: |
| 208 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 209 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 210 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 211 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 212 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 213 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 214 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 215 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 216 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 217 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 218 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 219 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 220 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 221 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 222 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 223 | #endif |
| 224 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 225 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 226 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 227 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 228 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 229 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 230 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 231 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 232 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 233 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 234 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 235 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 236 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 237 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 238 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 239 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 240 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 241 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 242 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 243 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 244 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 245 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 246 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 248 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 249 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 250 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 251 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 252 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 253 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 254 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 255 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 256 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 257 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 258 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 259 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 260 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 261 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 262 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 263 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 264 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 265 | return( PSA_ERROR_NOT_PERMITTED ); |
| 266 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 268 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 269 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 270 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 271 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 272 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 273 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | f9f1bdf | 2021-06-23 20:32:27 +0200 | [diff] [blame] | 274 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: |
| 275 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 276 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 277 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 278 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 279 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 280 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 281 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 282 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 283 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 284 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 285 | return( PSA_ERROR_INVALID_PADDING ); |
| 286 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 287 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 288 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 289 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 290 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 291 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 292 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 293 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 294 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 295 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 296 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 297 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 298 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 299 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 300 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 301 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 302 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 303 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 304 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 305 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 306 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 307 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 308 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 309 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 310 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 311 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 312 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 313 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 314 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 315 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 316 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 317 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 318 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 319 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 323 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 324 | |
| 325 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 326 | /****************************************************************/ |
| 327 | /* Key management */ |
| 328 | /****************************************************************/ |
| 329 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 330 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 331 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 332 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 333 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 334 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 335 | 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] | 336 | size_t bits, |
| 337 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 338 | { |
| 339 | switch( curve ) |
| 340 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 341 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 342 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 343 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 344 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 345 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 346 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 347 | #endif |
| 348 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 349 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 350 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 351 | #endif |
| 352 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 353 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 354 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 355 | #endif |
| 356 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 357 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 358 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 359 | #endif |
| 360 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 361 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 362 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 363 | case 528: |
| 364 | if( bits_is_sloppy ) |
| 365 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 366 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 367 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 368 | } |
| 369 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 370 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 371 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 372 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 373 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 374 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 375 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 376 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 377 | #endif |
| 378 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 379 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 380 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 381 | #endif |
| 382 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 383 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 384 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 385 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 386 | } |
| 387 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 388 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 389 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 390 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 391 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 392 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 393 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 394 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 395 | case 256: |
| 396 | if( bits_is_sloppy ) |
| 397 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 398 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 399 | #endif |
| 400 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 401 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 402 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 403 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 404 | } |
| 405 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 406 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 407 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 408 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 409 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 410 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 411 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 412 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 413 | #endif |
| 414 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 415 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 416 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 417 | #endif |
| 418 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 419 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 420 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 421 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 422 | } |
| 423 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 424 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 425 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 426 | (void) bits_is_sloppy; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 427 | return( MBEDTLS_ECP_DP_NONE ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 428 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 429 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 430 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 431 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 432 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 433 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 434 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 435 | 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] | 436 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 437 | { |
| 438 | /* Check that the bit size is acceptable for the key type */ |
| 439 | switch( type ) |
| 440 | { |
| 441 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 442 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 443 | case PSA_KEY_TYPE_DERIVE: |
Neil Armstrong | 4b5710f | 2022-05-25 11:30:27 +0200 | [diff] [blame] | 444 | case PSA_KEY_TYPE_PASSWORD: |
| 445 | case PSA_KEY_TYPE_PASSWORD_HASH: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 446 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 447 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 448 | case PSA_KEY_TYPE_AES: |
| 449 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 450 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 451 | break; |
| 452 | #endif |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 453 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 454 | case PSA_KEY_TYPE_ARIA: |
| 455 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 456 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 457 | break; |
| 458 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 459 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 460 | case PSA_KEY_TYPE_CAMELLIA: |
| 461 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 462 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 463 | break; |
| 464 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 465 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 466 | case PSA_KEY_TYPE_DES: |
| 467 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 468 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 469 | break; |
| 470 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 471 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 472 | case PSA_KEY_TYPE_CHACHA20: |
| 473 | if( bits != 256 ) |
| 474 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 475 | break; |
| 476 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 477 | default: |
| 478 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 479 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 480 | if( bits % 8 != 0 ) |
| 481 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 482 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 483 | return( PSA_SUCCESS ); |
| 484 | } |
| 485 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 486 | /** 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] | 487 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 488 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 489 | * 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] | 490 | * |
| 491 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 492 | * \param[in] key_type The key type of the key to be used with the |
| 493 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 494 | * |
| 495 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 496 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 497 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 498 | * 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] | 499 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 500 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 501 | psa_algorithm_t algorithm, |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 502 | psa_key_type_t key_type ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 503 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 504 | if( PSA_ALG_IS_HMAC( algorithm ) ) |
| 505 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 506 | if( key_type == PSA_KEY_TYPE_HMAC ) |
| 507 | return( PSA_SUCCESS ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 508 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 509 | |
| 510 | if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 511 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 512 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 513 | * key. */ |
| 514 | if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) == |
| 515 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC ) |
| 516 | { |
| 517 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 518 | * the block length (larger than 1) for block ciphers. */ |
| 519 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 ) |
| 520 | return( PSA_SUCCESS ); |
| 521 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 522 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 523 | |
| 524 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 527 | psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 528 | size_t buffer_length ) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 529 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 530 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 531 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 532 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 533 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 534 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 535 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 536 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 537 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 538 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 539 | } |
| 540 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 541 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 542 | const uint8_t* data, |
| 543 | size_t data_length ) |
| 544 | { |
| 545 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 546 | data_length ); |
| 547 | if( status != PSA_SUCCESS ) |
| 548 | return( status ); |
| 549 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 550 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 551 | return( PSA_SUCCESS ); |
| 552 | } |
| 553 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 554 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 555 | const psa_key_attributes_t *attributes, |
| 556 | const uint8_t *data, size_t data_length, |
| 557 | uint8_t *key_buffer, size_t key_buffer_size, |
| 558 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 559 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 560 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 561 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 562 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 563 | /* zero-length keys are never supported. */ |
| 564 | if( data_length == 0 ) |
| 565 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 566 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 567 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 568 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 569 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 570 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 571 | status = psa_validate_unstructured_key_bit_size( attributes->core.type, |
| 572 | *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 573 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 574 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 575 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 576 | /* Copy the key material. */ |
| 577 | memcpy( key_buffer, data, data_length ); |
| 578 | *key_buffer_length = data_length; |
| 579 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 580 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 581 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 582 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 583 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 584 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 585 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 586 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 587 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 588 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 589 | return( mbedtls_psa_ecp_import_key( attributes, |
| 590 | data, data_length, |
| 591 | key_buffer, key_buffer_size, |
| 592 | key_buffer_length, |
| 593 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 594 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 595 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 596 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 597 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 598 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 599 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 600 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 601 | return( mbedtls_psa_rsa_import_key( attributes, |
| 602 | data, data_length, |
| 603 | key_buffer, key_buffer_size, |
| 604 | key_buffer_length, |
| 605 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 606 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 607 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 608 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 609 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 610 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 611 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 614 | /** Calculate the intersection of two algorithm usage policies. |
| 615 | * |
| 616 | * Return 0 (which allows no operation) on incompatibility. |
| 617 | */ |
| 618 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 619 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 620 | psa_algorithm_t alg1, |
| 621 | psa_algorithm_t alg2 ) |
| 622 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 623 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 624 | if( alg1 == alg2 ) |
| 625 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 626 | /* If the policies are from the same hash-and-sign family, check |
| 627 | * 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] | 628 | if( PSA_ALG_IS_SIGN_HASH( alg1 ) && |
| 629 | PSA_ALG_IS_SIGN_HASH( alg2 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 630 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 631 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 632 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 633 | return( alg2 ); |
| 634 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 635 | return( alg1 ); |
| 636 | } |
| 637 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 638 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 639 | * restrictive tag length. */ |
| 640 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 641 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 642 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 643 | { |
| 644 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 645 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 646 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 647 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 648 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 649 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 650 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 651 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 652 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 653 | alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 654 | } |
| 655 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 656 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 657 | ( alg1_len <= alg2_len ) ) |
| 658 | { |
| 659 | return( alg2 ); |
| 660 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 661 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 662 | ( alg2_len <= alg1_len ) ) |
| 663 | { |
| 664 | return( alg1 ); |
| 665 | } |
| 666 | } |
| 667 | /* If the policies are from the same MAC family, check whether one |
| 668 | * of them is a minimum-MAC-length policy. Calculate the most |
| 669 | * restrictive tag length. */ |
| 670 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 671 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 672 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 673 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 674 | /* Validate the combination of key type and algorithm. Since the base |
| 675 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
| 676 | if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) ) |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 677 | return( 0 ); |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 678 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 679 | /* Get the (exact or at-least) output lengths for both sides of the |
| 680 | * requested intersection. None of the currently supported algorithms |
| 681 | * have an output length dependent on the actual key size, so setting it |
| 682 | * to a bogus value of 0 is currently OK. |
| 683 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 684 | * Note that for at-least-this-length wildcard algorithms, the output |
| 685 | * length is set to the shortest allowed length, which allows us to |
| 686 | * calculate the most restrictive tag length for the intersection. */ |
| 687 | size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 ); |
| 688 | size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 689 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 690 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 691 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 692 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 693 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 694 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 695 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 696 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 697 | |
| 698 | /* If only one is an at-least-this-length policy, the intersection would |
| 699 | * be the other (fixed-length) policy as long as said fixed length is |
| 700 | * equal to or larger than the shortest allowed length. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 701 | if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 702 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 703 | return( ( alg1_len <= alg2_len ) ? alg2 : 0 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 704 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 705 | if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 706 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 707 | return( ( alg2_len <= alg1_len ) ? alg1 : 0 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 708 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 709 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 710 | /* If none of them are wildcards, check whether they define the same tag |
| 711 | * length. This is still possible here when one is default-length and |
| 712 | * the other specific-length. Ensure to always return the |
| 713 | * specific-length version for the intersection. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 714 | if( alg1_len == alg2_len ) |
| 715 | return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 716 | } |
| 717 | /* If the policies are incompatible, allow nothing. */ |
| 718 | return( 0 ); |
| 719 | } |
| 720 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 721 | static int psa_key_algorithm_permits( psa_key_type_t key_type, |
| 722 | psa_algorithm_t policy_alg, |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 723 | psa_algorithm_t requested_alg ) |
| 724 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 725 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 726 | if( requested_alg == policy_alg ) |
| 727 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 728 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 729 | * and requested_alg is the same hash-and-sign family with any hash, |
| 730 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 731 | if( PSA_ALG_IS_SIGN_HASH( requested_alg ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 732 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 733 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 734 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 735 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 736 | } |
| 737 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 738 | * the requested algorithm, check the requested tag length to be |
| 739 | * equal-length or longer than the wildcard-specified length. */ |
| 740 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 741 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 742 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 743 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 744 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 745 | { |
| 746 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 747 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 748 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 749 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 750 | * algorithm, check whether their MAC lengths are compatible. */ |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 751 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 752 | PSA_ALG_IS_MAC( requested_alg ) && |
| 753 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 754 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 755 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 756 | /* Validate the combination of key type and algorithm. Since the policy |
| 757 | * and requested algorithms are the same, we only need this once. */ |
| 758 | if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 759 | return( 0 ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 760 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 761 | /* Get both the requested output length for the algorithm which is to be |
| 762 | * verified, and the default output length for the base algorithm. |
| 763 | * Note that none of the currently supported algorithms have an output |
| 764 | * length dependent on actual key size, so setting it to a bogus value |
| 765 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 766 | size_t requested_output_length = PSA_MAC_LENGTH( |
| 767 | key_type, 0, requested_alg ); |
| 768 | size_t default_output_length = PSA_MAC_LENGTH( |
| 769 | key_type, 0, |
| 770 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 771 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 772 | /* If the policy is default-length, only allow an algorithm with |
| 773 | * a declared exact-length matching the default. */ |
| 774 | if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 775 | return( requested_output_length == default_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 776 | |
| 777 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 778 | * length exactly matches the default length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 779 | if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 && |
| 780 | PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length ) |
| 781 | { |
| 782 | return( 1 ); |
| 783 | } |
| 784 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 785 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 786 | * check for the requested MAC length to be equal to or longer than the |
| 787 | * minimum allowed length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 788 | if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
| 789 | { |
| 790 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 791 | requested_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 792 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 793 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 794 | /* 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] | 795 | * a key derivation with that key agreement should also be allowed. This |
| 796 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 797 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 798 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 799 | { |
| 800 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 801 | policy_alg ); |
| 802 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 803 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 804 | return( 0 ); |
| 805 | } |
| 806 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 807 | /** Test whether a policy permits an algorithm. |
| 808 | * |
| 809 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 810 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 811 | * \note This function requires providing the key type for which the policy is |
| 812 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 813 | * have different properties depending on what kind of cipher it is |
| 814 | * combined with. |
| 815 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 816 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 817 | * allowed by the \p policy. |
| 818 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 819 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 820 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 821 | */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 822 | 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] | 823 | psa_key_type_t key_type, |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 824 | psa_algorithm_t alg ) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 825 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 826 | /* '0' is not a valid algorithm */ |
| 827 | if( alg == 0 ) |
| 828 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 829 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 830 | /* A requested algorithm cannot be a wildcard. */ |
| 831 | if( PSA_ALG_IS_WILDCARD( alg ) ) |
| 832 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 833 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 834 | if( psa_key_algorithm_permits( key_type, policy->alg, alg ) || |
| 835 | psa_key_algorithm_permits( key_type, policy->alg2, alg ) ) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 836 | return( PSA_SUCCESS ); |
| 837 | else |
| 838 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 839 | } |
| 840 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 841 | /** Restrict a key policy based on a constraint. |
| 842 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 843 | * \note This function requires providing the key type for which the policy is |
| 844 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 845 | * have different properties depending on what kind of cipher it is |
| 846 | * combined with. |
| 847 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 848 | * \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] | 849 | * \param[in,out] policy The policy to restrict. |
| 850 | * \param[in] constraint The policy constraint to apply. |
| 851 | * |
| 852 | * \retval #PSA_SUCCESS |
| 853 | * \c *policy contains the intersection of the original value of |
| 854 | * \c *policy and \c *constraint. |
| 855 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 856 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 857 | * \c *policy is unchanged. |
| 858 | */ |
| 859 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 860 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 861 | psa_key_policy_t *policy, |
| 862 | const psa_key_policy_t *constraint ) |
| 863 | { |
| 864 | psa_algorithm_t intersection_alg = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 865 | psa_key_policy_algorithm_intersection( key_type, policy->alg, |
| 866 | constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 867 | psa_algorithm_t intersection_alg2 = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 868 | psa_key_policy_algorithm_intersection( key_type, policy->alg2, |
| 869 | constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 870 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 871 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 872 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 873 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 874 | policy->usage &= constraint->usage; |
| 875 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 876 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 877 | return( PSA_SUCCESS ); |
| 878 | } |
| 879 | |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 880 | psa_status_t psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 881 | mbedtls_svc_key_id_t key, |
| 882 | psa_key_slot_t **p_slot, |
| 883 | psa_key_usage_t usage, |
| 884 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 885 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 886 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 887 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 888 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 889 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 890 | if( status != PSA_SUCCESS ) |
| 891 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 892 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 893 | |
| 894 | /* Enforce that usage policy for the key slot contains all the flags |
| 895 | * required by the usage parameter. There is one exception: public |
| 896 | * keys can always be exported, so we treat public key objects as |
| 897 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 898 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 899 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 900 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 901 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 902 | { |
| 903 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 904 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 905 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 906 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 907 | /* Enforce that the usage policy permits the requested algorithm. */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 908 | if( alg != 0 ) |
| 909 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 910 | status = psa_key_policy_permits( &slot->attr.policy, |
| 911 | slot->attr.type, |
| 912 | alg ); |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 913 | if( status != PSA_SUCCESS ) |
| 914 | goto error; |
| 915 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 916 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 917 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 918 | |
| 919 | error: |
| 920 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 921 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 922 | |
| 923 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 924 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 925 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 926 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 927 | * |
| 928 | * 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] | 929 | * available, as opposed to a key in a secure element and/or to be used |
| 930 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 931 | * |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 932 | * This is a temporary function that may be used instead of |
| 933 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 934 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 935 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 936 | * On success, the returned key slot is locked. It is the responsibility of the |
| 937 | * 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] | 938 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 939 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 940 | mbedtls_svc_key_id_t key, |
| 941 | psa_key_slot_t **p_slot, |
| 942 | psa_key_usage_t usage, |
| 943 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 944 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 945 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 946 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 947 | if( status != PSA_SUCCESS ) |
| 948 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 949 | |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 950 | if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 951 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 952 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 953 | *p_slot = NULL; |
| 954 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 955 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 956 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 957 | return( PSA_SUCCESS ); |
| 958 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 959 | |
Steven Cooreman | 7ddee7f | 2021-04-07 18:08:30 +0200 | [diff] [blame] | 960 | 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] | 961 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 962 | /* Data pointer will always be either a valid pointer or NULL in an |
| 963 | * initialized slot, so we can just free it. */ |
| 964 | if( slot->key.data != NULL ) |
| 965 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 966 | |
| 967 | mbedtls_free( slot->key.data ); |
| 968 | slot->key.data = NULL; |
| 969 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 970 | |
| 971 | return( PSA_SUCCESS ); |
| 972 | } |
| 973 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 974 | /** Completely wipe a slot in memory, including its policy. |
| 975 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 976 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 977 | { |
| 978 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 979 | |
TRodziewicz | 18cddc0 | 2021-07-13 12:19:15 +0200 | [diff] [blame] | 980 | /* |
| 981 | * 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] | 982 | * do our best to report an unexpected lock counter. Assert with |
| 983 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one: |
| 984 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 985 | * function is called as part of the execution of a test suite, the |
| 986 | * execution of the test suite is stopped in error if the assertion fails. |
TRodziewicz | 18cddc0 | 2021-07-13 12:19:15 +0200 | [diff] [blame] | 987 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 988 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 989 | { |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 990 | MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 991 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 992 | } |
| 993 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 994 | /* Multipart operations may still be using the key. This is safe |
| 995 | * because all multipart operation objects are independent from |
| 996 | * the key slot: if they need to access the key after the setup |
| 997 | * phase, they have a copy of the key. Note that this means that |
| 998 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 999 | /* At this point, key material and other type-specific content has |
| 1000 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1001 | * zeroize because the metadata is not particularly sensitive. */ |
| 1002 | memset( slot, 0, sizeof( *slot ) ); |
| 1003 | return( status ); |
| 1004 | } |
| 1005 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1006 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1007 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1008 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1009 | psa_status_t status; /* status of the last operation */ |
| 1010 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1011 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1012 | psa_se_drv_table_entry_t *driver; |
| 1013 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1014 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1015 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1016 | return( PSA_SUCCESS ); |
| 1017 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1018 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1019 | * 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] | 1020 | * key, this will load the key description from persistent memory if not |
| 1021 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1022 | * the key is operated by an SE or not and this information is needed by |
| 1023 | * the current implementation. |
| 1024 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1025 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1026 | if( status != PSA_SUCCESS ) |
| 1027 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1028 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1029 | /* |
| 1030 | * If the key slot containing the key description is under access by the |
| 1031 | * library (apart from the present access), the key cannot be destroyed |
| 1032 | * yet. For the time being, just return in error. Eventually (to be |
| 1033 | * implemented), the key should be destroyed when all accesses have |
| 1034 | * stopped. |
| 1035 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1036 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1037 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1038 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1039 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1040 | } |
| 1041 | |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1042 | if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) ) |
| 1043 | { |
| 1044 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1045 | * if we attempt it, depending on whether the key is merely read-only |
| 1046 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1047 | * Just do the best we can, which is to wipe the copy in memory |
| 1048 | * (done in this function's cleanup code). */ |
| 1049 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1050 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1053 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1054 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1055 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1056 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1057 | /* For a key in a secure element, we need to do three things: |
| 1058 | * remove the key file in internal storage, destroy the |
| 1059 | * key inside the secure element, and update the driver's |
| 1060 | * persistent data. Start a transaction that will encompass these |
| 1061 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1062 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1063 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1064 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1065 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1066 | status = psa_crypto_save_transaction( ); |
| 1067 | if( status != PSA_SUCCESS ) |
| 1068 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1069 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1070 | /* We should still try to destroy the key in the secure |
| 1071 | * element and the key metadata in storage. This is especially |
| 1072 | * important if the error is that the storage is full. |
| 1073 | * But how to do it exactly without risking an inconsistent |
| 1074 | * state after a reset? |
| 1075 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1076 | */ |
| 1077 | overall_status = status; |
| 1078 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1081 | status = psa_destroy_se_key( driver, |
| 1082 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1083 | if( overall_status == PSA_SUCCESS ) |
| 1084 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1085 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1086 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1087 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1088 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1089 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1090 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1091 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1092 | if( overall_status == PSA_SUCCESS ) |
| 1093 | overall_status = status; |
| 1094 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1095 | /* TODO: other slots may have a copy of the same key. We should |
| 1096 | * invalidate them. |
| 1097 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1098 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1099 | } |
| 1100 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1101 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1102 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1103 | if( driver != NULL ) |
| 1104 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1105 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1106 | if( overall_status == PSA_SUCCESS ) |
| 1107 | overall_status = status; |
| 1108 | status = psa_crypto_stop_transaction( ); |
| 1109 | if( overall_status == PSA_SUCCESS ) |
| 1110 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1111 | } |
| 1112 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1113 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1114 | exit: |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1115 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1116 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1117 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1118 | overall_status = status; |
| 1119 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1122 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1123 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1124 | static psa_status_t psa_get_rsa_public_exponent( |
| 1125 | const mbedtls_rsa_context *rsa, |
| 1126 | psa_key_attributes_t *attributes ) |
| 1127 | { |
| 1128 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1129 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1130 | uint8_t *buffer = NULL; |
| 1131 | size_t buflen; |
| 1132 | mbedtls_mpi_init( &mpi ); |
| 1133 | |
| 1134 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1135 | if( ret != 0 ) |
| 1136 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1137 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1138 | { |
| 1139 | /* It's the default value, which is reported as an empty string, |
| 1140 | * so there's nothing to do. */ |
| 1141 | goto exit; |
| 1142 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1143 | |
| 1144 | buflen = mbedtls_mpi_size( &mpi ); |
| 1145 | buffer = mbedtls_calloc( 1, buflen ); |
| 1146 | if( buffer == NULL ) |
| 1147 | { |
| 1148 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1149 | goto exit; |
| 1150 | } |
| 1151 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1152 | if( ret != 0 ) |
| 1153 | goto exit; |
| 1154 | attributes->domain_parameters = buffer; |
| 1155 | attributes->domain_parameters_size = buflen; |
| 1156 | |
| 1157 | exit: |
| 1158 | mbedtls_mpi_free( &mpi ); |
| 1159 | if( ret != 0 ) |
| 1160 | mbedtls_free( buffer ); |
| 1161 | return( mbedtls_to_psa_error( ret ) ); |
| 1162 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1163 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1164 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1165 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1166 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1167 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1168 | 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] | 1169 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1170 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1171 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1172 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1173 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1174 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1175 | psa_reset_key_attributes( attributes ); |
| 1176 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1177 | 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] | 1178 | if( status != PSA_SUCCESS ) |
| 1179 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1180 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1181 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1182 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1183 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1184 | |
| 1185 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 512ad81 | 2021-08-24 15:50:05 +0200 | [diff] [blame] | 1186 | if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1187 | psa_set_key_slot_number( attributes, |
| 1188 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1189 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1190 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1191 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1192 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1193 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1194 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1195 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1196 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1197 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1198 | * is not yet implemented. |
| 1199 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1200 | */ |
Ronald Cron | 9b8b69c | 2021-08-24 16:00:51 +0200 | [diff] [blame] | 1201 | if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1202 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1203 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1204 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1205 | status = mbedtls_psa_rsa_load_representation( |
| 1206 | slot->attr.type, |
| 1207 | slot->key.data, |
| 1208 | slot->key.bytes, |
| 1209 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1210 | if( status != PSA_SUCCESS ) |
| 1211 | break; |
| 1212 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1213 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1214 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1215 | mbedtls_rsa_free( rsa ); |
| 1216 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1217 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1218 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1219 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1220 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1221 | default: |
| 1222 | /* Nothing else to do. */ |
| 1223 | break; |
| 1224 | } |
| 1225 | |
| 1226 | if( status != PSA_SUCCESS ) |
| 1227 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1228 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1229 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1230 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1231 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1232 | } |
| 1233 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1234 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1235 | psa_status_t psa_get_key_slot_number( |
| 1236 | const psa_key_attributes_t *attributes, |
| 1237 | psa_key_slot_number_t *slot_number ) |
| 1238 | { |
| 1239 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1240 | { |
| 1241 | *slot_number = attributes->slot_number; |
| 1242 | return( PSA_SUCCESS ); |
| 1243 | } |
| 1244 | else |
| 1245 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1246 | } |
| 1247 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1248 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1249 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1250 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1251 | uint8_t *data, |
| 1252 | size_t data_size, |
| 1253 | size_t *data_length ) |
| 1254 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1255 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1256 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1257 | memcpy( data, key_buffer, key_buffer_size ); |
| 1258 | memset( data + key_buffer_size, 0, |
| 1259 | data_size - key_buffer_size ); |
| 1260 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1261 | return( PSA_SUCCESS ); |
| 1262 | } |
| 1263 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1264 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1265 | const psa_key_attributes_t *attributes, |
| 1266 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1267 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1268 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1269 | psa_key_type_t type = attributes->core.type; |
| 1270 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1271 | if( key_type_is_raw_bytes( type ) || |
| 1272 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1273 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1274 | { |
| 1275 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1276 | key_buffer, key_buffer_size, |
| 1277 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1278 | } |
| 1279 | else |
| 1280 | { |
| 1281 | /* This shouldn't happen in the reference implementation, but |
| 1282 | it is valid for a special-purpose implementation to omit |
| 1283 | support for exporting certain key types. */ |
| 1284 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1289 | uint8_t *data, |
| 1290 | size_t data_size, |
| 1291 | size_t *data_length ) |
| 1292 | { |
| 1293 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1294 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1295 | psa_key_slot_t *slot; |
| 1296 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1297 | /* Reject a zero-length output buffer now, since this can never be a |
| 1298 | * valid key representation. This way we know that data must be a valid |
| 1299 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1300 | if( data_size == 0 ) |
| 1301 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1302 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1303 | /* Set the key to empty now, so that even when there are errors, we always |
| 1304 | * set data_length to a value between 0 and data_size. On error, setting |
| 1305 | * the key to empty is a good choice because an empty key representation is |
| 1306 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1307 | *data_length = 0; |
| 1308 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1309 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1310 | * which don't require any flag, but |
| 1311 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1312 | */ |
| 1313 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1314 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1315 | if( status != PSA_SUCCESS ) |
| 1316 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1317 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1318 | psa_key_attributes_t attributes = { |
| 1319 | .core = slot->attr |
| 1320 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1321 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1322 | slot->key.data, slot->key.bytes, |
| 1323 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1324 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1325 | unlock_status = psa_unlock_key_slot( slot ); |
| 1326 | |
| 1327 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1328 | } |
| 1329 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1330 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1331 | const psa_key_attributes_t *attributes, |
| 1332 | const uint8_t *key_buffer, |
| 1333 | size_t key_buffer_size, |
| 1334 | uint8_t *data, |
| 1335 | size_t data_size, |
| 1336 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1337 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1338 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1339 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1340 | 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] | 1341 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1342 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1343 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1344 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1345 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1346 | key_buffer, key_buffer_size, |
| 1347 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1348 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1349 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1350 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1351 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1352 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1353 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1354 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1355 | key_buffer, |
| 1356 | key_buffer_size, |
| 1357 | data, |
| 1358 | data_size, |
| 1359 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1360 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1361 | /* We don't know how to convert a private RSA key to public. */ |
| 1362 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1363 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1364 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1365 | } |
| 1366 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1367 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1368 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1369 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1370 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1371 | key_buffer, |
| 1372 | key_buffer_size, |
| 1373 | data, |
| 1374 | data_size, |
| 1375 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1376 | #else |
| 1377 | /* 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] | 1378 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1379 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1380 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1381 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1382 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1383 | else |
| 1384 | { |
| 1385 | /* This shouldn't happen in the reference implementation, but |
| 1386 | it is valid for a special-purpose implementation to omit |
| 1387 | support for exporting certain key types. */ |
| 1388 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1389 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1390 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1391 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1392 | 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] | 1393 | uint8_t *data, |
| 1394 | size_t data_size, |
| 1395 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1396 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1397 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1398 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1399 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1400 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1401 | /* Reject a zero-length output buffer now, since this can never be a |
| 1402 | * valid key representation. This way we know that data must be a valid |
| 1403 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1404 | if( data_size == 0 ) |
| 1405 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1406 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1407 | /* Set the key to empty now, so that even when there are errors, we always |
| 1408 | * set data_length to a value between 0 and data_size. On error, setting |
| 1409 | * the key to empty is a good choice because an empty key representation is |
| 1410 | * unlikely to be accepted anywhere. */ |
| 1411 | *data_length = 0; |
| 1412 | |
| 1413 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1414 | 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] | 1415 | if( status != PSA_SUCCESS ) |
| 1416 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1417 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1418 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1419 | { |
| 1420 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1421 | goto exit; |
| 1422 | } |
| 1423 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1424 | psa_key_attributes_t attributes = { |
| 1425 | .core = slot->attr |
| 1426 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1427 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1428 | &attributes, slot->key.data, slot->key.bytes, |
| 1429 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1430 | |
| 1431 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1432 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1433 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1434 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1435 | } |
| 1436 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1437 | #if defined(static_assert) |
| 1438 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1439 | "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] | 1440 | 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] | 1441 | "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] | 1442 | 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] | 1443 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1444 | #endif |
| 1445 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1446 | /** Validate that a key policy is internally well-formed. |
| 1447 | * |
| 1448 | * This function only rejects invalid policies. It does not validate the |
| 1449 | * consistency of the policy with respect to other attributes of the key |
| 1450 | * such as the key type. |
| 1451 | */ |
| 1452 | 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] | 1453 | { |
| 1454 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1455 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1456 | PSA_KEY_USAGE_ENCRYPT | |
| 1457 | PSA_KEY_USAGE_DECRYPT | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 1458 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1459 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1460 | PSA_KEY_USAGE_SIGN_HASH | |
| 1461 | PSA_KEY_USAGE_VERIFY_HASH | |
Manuel Pégourié-Gonnard | 805251b | 2021-05-04 09:49:59 +0200 | [diff] [blame] | 1462 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1463 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1464 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1465 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1466 | return( PSA_SUCCESS ); |
| 1467 | } |
| 1468 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1469 | /** Validate the internal consistency of key attributes. |
| 1470 | * |
| 1471 | * This function only rejects invalid attribute values. If does not |
| 1472 | * validate the consistency of the attributes with any key data that may |
| 1473 | * be involved in the creation of the key. |
| 1474 | * |
| 1475 | * Call this function early in the key creation process. |
| 1476 | * |
| 1477 | * \param[in] attributes Key attributes for the new key. |
| 1478 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1479 | * NULL for a transparent key. |
| 1480 | * |
| 1481 | */ |
| 1482 | static psa_status_t psa_validate_key_attributes( |
| 1483 | const psa_key_attributes_t *attributes, |
| 1484 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1485 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1486 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1487 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1488 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1489 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1490 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1491 | if( status != PSA_SUCCESS ) |
| 1492 | return( status ); |
| 1493 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1494 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1495 | if( status != PSA_SUCCESS ) |
| 1496 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1497 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1498 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1499 | { |
| 1500 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1501 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1502 | } |
| 1503 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1504 | { |
Ronald Cron | 77e412c | 2021-03-31 17:36:31 +0200 | [diff] [blame] | 1505 | if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) ) |
| 1506 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1509 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1510 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1511 | return( status ); |
| 1512 | |
| 1513 | /* Refuse to create overly large keys. |
| 1514 | * Note that this doesn't trigger on import if the attributes don't |
| 1515 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1516 | * psa_import_key() needs its own checks. */ |
| 1517 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1518 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1519 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1520 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1521 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1522 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1523 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1524 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1525 | return( PSA_SUCCESS ); |
| 1526 | } |
| 1527 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1528 | /** Prepare a key slot to receive key material. |
| 1529 | * |
| 1530 | * This function allocates a key slot and sets its metadata. |
| 1531 | * |
| 1532 | * If this function fails, call psa_fail_key_creation(). |
| 1533 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1534 | * This function is intended to be used as follows: |
| 1535 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1536 | * it with the specified attributes, and in case of a volatile key assign it |
| 1537 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1538 | * -# Populate the slot with the key material. |
| 1539 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1540 | * In case of failure at any step, stop the sequence and call |
| 1541 | * psa_fail_key_creation(). |
| 1542 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1543 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1544 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1545 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1546 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1547 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1548 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1549 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1550 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1551 | * |
| 1552 | * \retval #PSA_SUCCESS |
| 1553 | * The key slot is ready to receive key material. |
| 1554 | * \return If this function fails, the key slot is an invalid state. |
| 1555 | * 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] | 1556 | */ |
| 1557 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1558 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1559 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1560 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1561 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1562 | { |
| 1563 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1564 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1565 | psa_key_slot_t *slot; |
| 1566 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1567 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1568 | *p_drv = NULL; |
| 1569 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1570 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1571 | if( status != PSA_SUCCESS ) |
| 1572 | return( status ); |
| 1573 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1574 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1575 | if( status != PSA_SUCCESS ) |
| 1576 | return( status ); |
| 1577 | slot = *p_slot; |
| 1578 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1579 | /* We're storing the declared bit-size of the key. It's up to each |
| 1580 | * creation mechanism to verify that this information is correct. |
| 1581 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1582 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1583 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1584 | * volatile key identifier associated to the slot returned to contain its |
| 1585 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1586 | |
| 1587 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1588 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1589 | { |
| 1590 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1591 | slot->attr.id = volatile_key_id; |
| 1592 | #else |
| 1593 | slot->attr.id.key_id = volatile_key_id; |
| 1594 | #endif |
| 1595 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1596 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1597 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1598 | * external-only flags, query `attributes`. Thanks to the check |
| 1599 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1600 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1601 | * may have set. */ |
| 1602 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1603 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1604 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1605 | /* 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] | 1606 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1607 | * create the key file in internal storage, create the |
| 1608 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1609 | * persistent data. This is done by starting a transaction that will |
| 1610 | * encompass these three actions. |
| 1611 | * For registering a volatile key, we just need to find an appropriate |
| 1612 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1613 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1614 | /* The first thing to do is to find a slot number for the new key. |
| 1615 | * We save the slot number in persistent storage as part of the |
| 1616 | * transaction data. It will be needed to recover if the power |
| 1617 | * fails during the key creation process, to clean up on the secure |
| 1618 | * element side after restarting. Obtaining a slot number from the |
| 1619 | * secure element driver updates its persistent state, but we do not yet |
| 1620 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1621 | * 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] | 1622 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1623 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1624 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1625 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1626 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1627 | if( status != PSA_SUCCESS ) |
| 1628 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1629 | |
| 1630 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1631 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1632 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1633 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1634 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1635 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1636 | status = psa_crypto_save_transaction( ); |
| 1637 | if( status != PSA_SUCCESS ) |
| 1638 | { |
| 1639 | (void) psa_crypto_stop_transaction( ); |
| 1640 | return( status ); |
| 1641 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1642 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1643 | |
| 1644 | status = psa_copy_key_material_into_slot( |
| 1645 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1646 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1647 | |
| 1648 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1649 | { |
| 1650 | /* Key registration only makes sense with a secure element. */ |
| 1651 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1652 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1653 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1654 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1655 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1656 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1657 | |
| 1658 | /** Finalize the creation of a key once its key material has been set. |
| 1659 | * |
| 1660 | * This entails writing the key to persistent storage. |
| 1661 | * |
| 1662 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1663 | * See the documentation of psa_start_key_creation() for the intended use |
| 1664 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1665 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1666 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1667 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1668 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1669 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1670 | * \param[in,out] slot Pointer to the slot with key material. |
| 1671 | * \param[in] driver The secure element driver for the key, |
| 1672 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1673 | * \param[out] key On success, identifier of the key. Note that the |
| 1674 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1675 | * |
| 1676 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1677 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1678 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1679 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1680 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1681 | * \retval #PSA_ERROR_DATA_INVALID |
| 1682 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1683 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1684 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1685 | * \return If this function fails, the key slot is an invalid state. |
| 1686 | * 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] | 1687 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1688 | static psa_status_t psa_finish_key_creation( |
| 1689 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1690 | psa_se_drv_table_entry_t *driver, |
| 1691 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1692 | { |
| 1693 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1694 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1695 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1696 | |
| 1697 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1698 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1699 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1700 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1701 | if( driver != NULL ) |
| 1702 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1703 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1704 | psa_key_slot_number_t slot_number = |
| 1705 | psa_key_slot_get_slot_number( slot ) ; |
| 1706 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1707 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1708 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1709 | sizeof( data.slot_number ), |
| 1710 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1711 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1712 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1713 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1714 | (uint8_t*) &data, |
| 1715 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1716 | } |
| 1717 | else |
| 1718 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1719 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1720 | /* Key material is saved in export representation in the slot, so |
| 1721 | * just pass the slot buffer for storage. */ |
| 1722 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1723 | slot->key.data, |
| 1724 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1725 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1726 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1727 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1728 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1729 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1730 | /* Finish the transaction for a key creation. This does not |
| 1731 | * happen when registering an existing key. Detect this case |
| 1732 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1733 | * creation of a persistent key in a secure element requires a transaction, |
| 1734 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1735 | if( driver != NULL && |
| 1736 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1737 | { |
| 1738 | status = psa_save_se_persistent_data( driver ); |
| 1739 | if( status != PSA_SUCCESS ) |
| 1740 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1741 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1742 | return( status ); |
| 1743 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1744 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1745 | } |
| 1746 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1747 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1748 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1749 | { |
| 1750 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1751 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1752 | if( status != PSA_SUCCESS ) |
| 1753 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1754 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1755 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1756 | return( status ); |
| 1757 | } |
| 1758 | |
| 1759 | /** Abort the creation of a key. |
| 1760 | * |
| 1761 | * You may call this function after calling psa_start_key_creation(), |
| 1762 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1763 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1764 | * See the documentation of psa_start_key_creation() for the intended use |
| 1765 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1766 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1767 | * \param[in,out] slot Pointer to the slot with key material. |
| 1768 | * \param[in] driver The secure element driver for the key, |
| 1769 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1770 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1771 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1772 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1773 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1774 | (void) driver; |
| 1775 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1776 | if( slot == NULL ) |
| 1777 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1778 | |
| 1779 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1780 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1781 | * element, and the failure happened later (when saving metadata |
| 1782 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1783 | * element. |
| 1784 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1785 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1786 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1787 | /* Abort the ongoing transaction if any (there may not be one if |
| 1788 | * the creation process failed before starting one, or if the |
| 1789 | * key creation is a registration of a key in a secure element). |
| 1790 | * Earlier functions must already have done what it takes to undo any |
| 1791 | * partial creation. All that's left is to update the transaction data |
| 1792 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1793 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1794 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1795 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1796 | psa_wipe_key_slot( slot ); |
| 1797 | } |
| 1798 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1799 | /** Validate optional attributes during key creation. |
| 1800 | * |
| 1801 | * Some key attributes are optional during key creation. If they are |
| 1802 | * specified in the attributes structure, check that they are consistent |
| 1803 | * with the data in the slot. |
| 1804 | * |
| 1805 | * This function should be called near the end of key creation, after |
| 1806 | * the slot in memory is fully populated but before saving persistent data. |
| 1807 | */ |
| 1808 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1809 | const psa_key_slot_t *slot, |
| 1810 | const psa_key_attributes_t *attributes ) |
| 1811 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1812 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1813 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1814 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1815 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1816 | } |
| 1817 | |
| 1818 | if( attributes->domain_parameters_size != 0 ) |
| 1819 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1820 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1821 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1822 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1823 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1824 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1825 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1826 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1827 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1828 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 1829 | slot->attr.type, |
| 1830 | slot->key.data, |
| 1831 | slot->key.bytes, |
| 1832 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1833 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1834 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1835 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1836 | mbedtls_mpi_init( &actual ); |
| 1837 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1838 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1839 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1840 | mbedtls_rsa_free( rsa ); |
| 1841 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1842 | if( ret != 0 ) |
| 1843 | goto rsa_exit; |
| 1844 | ret = mbedtls_mpi_read_binary( &required, |
| 1845 | attributes->domain_parameters, |
| 1846 | attributes->domain_parameters_size ); |
| 1847 | if( ret != 0 ) |
| 1848 | goto rsa_exit; |
| 1849 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1850 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1851 | rsa_exit: |
| 1852 | mbedtls_mpi_free( &actual ); |
| 1853 | mbedtls_mpi_free( &required ); |
| 1854 | if( ret != 0) |
| 1855 | return( mbedtls_to_psa_error( ret ) ); |
| 1856 | } |
| 1857 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1858 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1859 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1860 | { |
| 1861 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1862 | } |
| 1863 | } |
| 1864 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1865 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1866 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1867 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1868 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1869 | } |
| 1870 | |
| 1871 | return( PSA_SUCCESS ); |
| 1872 | } |
| 1873 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1874 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1875 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1876 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1877 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1878 | { |
| 1879 | psa_status_t status; |
| 1880 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1881 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1882 | size_t bits; |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1883 | size_t storage_size = data_length; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1884 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1885 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1886 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1887 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1888 | * This also rejects any key which might be encoded as an empty string, |
| 1889 | * which is never valid. */ |
| 1890 | if( data_length == 0 ) |
| 1891 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1892 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1893 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1894 | if( data_length > SIZE_MAX / 8 ) |
| 1895 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1896 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1897 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1898 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1899 | if( status != PSA_SUCCESS ) |
| 1900 | goto exit; |
| 1901 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1902 | /* 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] | 1903 | * storage ( thus not in the case of importing a key in a secure element |
| 1904 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 1905 | * buffer to hold the imported key material. */ |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1906 | if( slot->key.data == NULL ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1907 | { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1908 | if( psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 1909 | { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1910 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
| 1911 | attributes, data, data_length, &storage_size ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1912 | if( status != PSA_SUCCESS ) |
| 1913 | goto exit; |
| 1914 | } |
| 1915 | status = psa_allocate_buffer_to_slot( slot, storage_size ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1916 | if( status != PSA_SUCCESS ) |
| 1917 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1918 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1919 | |
| 1920 | bits = slot->attr.bits; |
| 1921 | status = psa_driver_wrapper_import_key( attributes, |
| 1922 | data, data_length, |
| 1923 | slot->key.data, |
| 1924 | slot->key.bytes, |
| 1925 | &slot->key.bytes, &bits ); |
| 1926 | if( status != PSA_SUCCESS ) |
| 1927 | goto exit; |
| 1928 | |
| 1929 | if( slot->attr.bits == 0 ) |
| 1930 | slot->attr.bits = (psa_key_bits_t) bits; |
| 1931 | else if( bits != slot->attr.bits ) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1932 | { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1933 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1934 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1935 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 1936 | |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1937 | /* Enforce a size limit, and in particular ensure that the bit |
| 1938 | * size fits in its representation type.*/ |
| 1939 | if( bits > PSA_MAX_KEY_BITS ) |
| 1940 | { |
| 1941 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1942 | goto exit; |
| 1943 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1944 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1945 | if( status != PSA_SUCCESS ) |
| 1946 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1947 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1948 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1949 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1950 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1951 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1952 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1953 | return( status ); |
| 1954 | } |
| 1955 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1956 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1957 | psa_status_t mbedtls_psa_register_se_key( |
| 1958 | const psa_key_attributes_t *attributes ) |
| 1959 | { |
| 1960 | psa_status_t status; |
| 1961 | psa_key_slot_t *slot = NULL; |
| 1962 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1963 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1964 | |
| 1965 | /* Leaving attributes unspecified is not currently supported. |
| 1966 | * It could make sense to query the key type and size from the |
| 1967 | * secure element, but not all secure elements support this |
| 1968 | * and the driver HAL doesn't currently support it. */ |
| 1969 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 1970 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1971 | if( psa_get_key_bits( attributes ) == 0 ) |
| 1972 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1973 | |
| 1974 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1975 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1976 | if( status != PSA_SUCCESS ) |
| 1977 | goto exit; |
| 1978 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1979 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1980 | |
| 1981 | exit: |
| 1982 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1983 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1984 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1985 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1986 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1987 | return( status ); |
| 1988 | } |
| 1989 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1990 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1991 | 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] | 1992 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1993 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1994 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1995 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1996 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1997 | psa_key_slot_t *source_slot = NULL; |
| 1998 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1999 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2000 | psa_se_drv_table_entry_t *driver = NULL; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2001 | size_t storage_size = 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2002 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2003 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2004 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2005 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2006 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2007 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2008 | goto exit; |
| 2009 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2010 | status = psa_validate_optional_attributes( source_slot, |
| 2011 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2012 | if( status != PSA_SUCCESS ) |
| 2013 | goto exit; |
| 2014 | |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2015 | /* The target key type and number of bits have been validated by |
| 2016 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2017 | * equal to the ones of the source key. So it is safe to inherit |
| 2018 | * them from the source key now." |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2019 | * */ |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2020 | actual_attributes.core.bits = source_slot->attr.bits; |
| 2021 | actual_attributes.core.type = source_slot->attr.type; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2022 | |
| 2023 | |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 2024 | status = psa_restrict_key_policy( source_slot->attr.type, |
| 2025 | &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2026 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2027 | if( status != PSA_SUCCESS ) |
| 2028 | goto exit; |
| 2029 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2030 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2031 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2032 | if( status != PSA_SUCCESS ) |
| 2033 | goto exit; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2034 | if( PSA_KEY_LIFETIME_GET_LOCATION( target_slot->attr.lifetime ) != |
| 2035 | PSA_KEY_LIFETIME_GET_LOCATION( source_slot->attr.lifetime ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2036 | { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2037 | /* |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2038 | * If the source and target keys are stored in different locations, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2039 | * the source key would need to be exported as plaintext and re-imported |
| 2040 | * in the other location. This has security implications which have not |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2041 | * been fully mapped. For now, this can be achieved through |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2042 | * appropriate API invocations from the application, if needed. |
| 2043 | * */ |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2044 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2045 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2046 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2047 | /* |
| 2048 | * When the source and target keys are within the same location, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2049 | * - For transparent keys it is a blind copy without any driver invocation, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2050 | * - For opaque keys this translates to an invocation of the drivers' |
| 2051 | * copy_key entry point through the dispatch layer. |
| 2052 | * */ |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2053 | if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) ) |
| 2054 | { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2055 | status = psa_driver_wrapper_get_key_buffer_size( &actual_attributes, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2056 | &storage_size ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2057 | if( status != PSA_SUCCESS ) |
| 2058 | goto exit; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2059 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2060 | status = psa_allocate_buffer_to_slot( target_slot, storage_size ); |
| 2061 | if( status != PSA_SUCCESS ) |
| 2062 | goto exit; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2063 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2064 | status = psa_driver_wrapper_copy_key( &actual_attributes, |
| 2065 | source_slot->key.data, |
| 2066 | source_slot->key.bytes, |
| 2067 | target_slot->key.data, |
| 2068 | target_slot->key.bytes, |
| 2069 | &target_slot->key.bytes ); |
| 2070 | if( status != PSA_SUCCESS ) |
| 2071 | goto exit; |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2072 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2073 | else |
| 2074 | { |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2075 | status = psa_copy_key_material_into_slot( target_slot, |
| 2076 | source_slot->key.data, |
| 2077 | source_slot->key.bytes ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2078 | if( status != PSA_SUCCESS ) |
| 2079 | goto exit; |
| 2080 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2081 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2082 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2083 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2084 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2085 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2086 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2087 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2088 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2089 | } |
| 2090 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2091 | |
| 2092 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2093 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2094 | /* Message digests */ |
| 2095 | /****************************************************************/ |
| 2096 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2097 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2098 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2099 | /* Aborting a non-active operation is allowed */ |
| 2100 | if( operation->id == 0 ) |
| 2101 | return( PSA_SUCCESS ); |
| 2102 | |
| 2103 | psa_status_t status = psa_driver_wrapper_hash_abort( operation ); |
| 2104 | operation->id = 0; |
| 2105 | |
| 2106 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2107 | } |
| 2108 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2109 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2110 | psa_algorithm_t alg ) |
| 2111 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2112 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2113 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2114 | /* A context must be freshly initialized before it can be set up. */ |
| 2115 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2116 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2117 | status = PSA_ERROR_BAD_STATE; |
| 2118 | goto exit; |
| 2119 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2120 | |
| 2121 | if( !PSA_ALG_IS_HASH( alg ) ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2122 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2123 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2124 | goto exit; |
| 2125 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2126 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2127 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2128 | * directly zeroes the int-sized dummy member of the context union. */ |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2129 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 2130 | |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2131 | status = psa_driver_wrapper_hash_setup( operation, alg ); |
| 2132 | |
| 2133 | exit: |
| 2134 | if( status != PSA_SUCCESS ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2135 | psa_hash_abort( operation ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2136 | |
| 2137 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2141 | const uint8_t *input, |
| 2142 | size_t input_length ) |
| 2143 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2144 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2145 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2146 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2147 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2148 | status = PSA_ERROR_BAD_STATE; |
| 2149 | goto exit; |
| 2150 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2151 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2152 | /* Don't require hash implementations to behave correctly on a |
| 2153 | * zero-length input, which may have an invalid pointer. */ |
| 2154 | if( input_length == 0 ) |
| 2155 | return( PSA_SUCCESS ); |
| 2156 | |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2157 | status = psa_driver_wrapper_hash_update( operation, input, input_length ); |
| 2158 | |
| 2159 | exit: |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2160 | if( status != PSA_SUCCESS ) |
| 2161 | psa_hash_abort( operation ); |
| 2162 | |
| 2163 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2167 | uint8_t *hash, |
| 2168 | size_t hash_size, |
| 2169 | size_t *hash_length ) |
| 2170 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2171 | *hash_length = 0; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2172 | if( operation->id == 0 ) |
| 2173 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2174 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2175 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2176 | operation, hash, hash_size, hash_length ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2177 | psa_hash_abort( operation ); |
| 2178 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2179 | } |
| 2180 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2181 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2182 | const uint8_t *hash, |
| 2183 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2184 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2185 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2186 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2187 | psa_status_t status = psa_hash_finish( |
| 2188 | operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2189 | actual_hash, sizeof( actual_hash ), |
| 2190 | &actual_hash_length ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2191 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2192 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2193 | goto exit; |
| 2194 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2195 | if( actual_hash_length != hash_length ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2196 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2197 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2198 | goto exit; |
| 2199 | } |
| 2200 | |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2201 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2202 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2203 | |
| 2204 | exit: |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2205 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2206 | if( status != PSA_SUCCESS ) |
| 2207 | psa_hash_abort(operation); |
| 2208 | |
| 2209 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2210 | } |
| 2211 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2212 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2213 | const uint8_t *input, size_t input_length, |
| 2214 | uint8_t *hash, size_t hash_size, |
| 2215 | size_t *hash_length ) |
| 2216 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2217 | *hash_length = 0; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2218 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2219 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2220 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2221 | return( psa_driver_wrapper_hash_compute( alg, input, input_length, |
| 2222 | hash, hash_size, hash_length ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2226 | const uint8_t *input, size_t input_length, |
| 2227 | const uint8_t *hash, size_t hash_length ) |
| 2228 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2229 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2230 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2231 | |
| 2232 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2233 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2234 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2235 | psa_status_t status = psa_driver_wrapper_hash_compute( |
| 2236 | alg, input, input_length, |
| 2237 | actual_hash, sizeof(actual_hash), |
| 2238 | &actual_hash_length ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2239 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2240 | goto exit; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2241 | if( actual_hash_length != hash_length ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2242 | { |
| 2243 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2244 | goto exit; |
| 2245 | } |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2246 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2247 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2248 | |
| 2249 | exit: |
| 2250 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
| 2251 | return( status ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2252 | } |
| 2253 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2254 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2255 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2256 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2257 | if( source_operation->id == 0 || |
| 2258 | target_operation->id != 0 ) |
| 2259 | { |
| 2260 | return( PSA_ERROR_BAD_STATE ); |
| 2261 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2262 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2263 | psa_status_t status = psa_driver_wrapper_hash_clone( source_operation, |
| 2264 | target_operation ); |
| 2265 | if( status != PSA_SUCCESS ) |
| 2266 | psa_hash_abort( target_operation ); |
| 2267 | |
| 2268 | return( status ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2269 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2270 | |
| 2271 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2272 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2273 | /* MAC */ |
| 2274 | /****************************************************************/ |
| 2275 | |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2276 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2277 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2278 | /* Aborting a non-active operation is allowed */ |
| 2279 | if( operation->id == 0 ) |
| 2280 | return( PSA_SUCCESS ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2281 | |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2282 | psa_status_t status = psa_driver_wrapper_mac_abort( operation ); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2283 | operation->mac_size = 0; |
| 2284 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2285 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2286 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2287 | return( status ); |
| 2288 | } |
| 2289 | |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2290 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2291 | psa_algorithm_t alg, |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2292 | const psa_key_attributes_t *attributes, |
| 2293 | uint8_t *mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2294 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2295 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2296 | psa_key_type_t key_type = psa_get_key_type( attributes ); |
| 2297 | size_t key_bits = psa_get_key_bits( attributes ); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2298 | |
Ronald Cron | 28ea050 | 2021-06-17 16:10:24 +0200 | [diff] [blame] | 2299 | if( ! PSA_ALG_IS_MAC( alg ) ) |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2300 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2301 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2302 | /* Validate the combination of key type and algorithm */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2303 | status = psa_mac_key_can_do( alg, key_type ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2304 | if( status != PSA_SUCCESS ) |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2305 | return( status ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2306 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2307 | /* Get the output length for the algorithm and key combination */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2308 | *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2309 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2310 | if( *mac_size < 4 ) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2311 | { |
| 2312 | /* A very short MAC is too short for security since it can be |
| 2313 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2314 | * so we make this our minimum, even though 32 bits is still |
| 2315 | * too small for security. */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2316 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2317 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2318 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2319 | if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits, |
| 2320 | PSA_ALG_FULL_LENGTH_MAC( alg ) ) ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2321 | { |
| 2322 | /* It's impossible to "truncate" to a larger length than the full length |
| 2323 | * of the algorithm. */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2324 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2325 | } |
| 2326 | |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2327 | if( *mac_size > PSA_MAC_MAX_SIZE ) |
| 2328 | { |
| 2329 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2330 | * that is disabled in the compile-time configuration. The result can |
| 2331 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2332 | * configuration into account. In this case, force a return of |
| 2333 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2334 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2335 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2336 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2337 | * systematically generated tests. */ |
| 2338 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2339 | } |
| 2340 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2341 | return( PSA_SUCCESS ); |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2342 | } |
| 2343 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2344 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
| 2345 | mbedtls_svc_key_id_t key, |
| 2346 | psa_algorithm_t alg, |
| 2347 | int is_sign ) |
| 2348 | { |
| 2349 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2350 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2351 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2352 | |
| 2353 | /* A context must be freshly initialized before it can be set up. */ |
| 2354 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2355 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2356 | status = PSA_ERROR_BAD_STATE; |
| 2357 | goto exit; |
| 2358 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2359 | |
| 2360 | status = psa_get_and_lock_key_slot_with_policy( |
| 2361 | key, |
| 2362 | &slot, |
Mateusz Starzyk | ce0e6a9 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2363 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2364 | alg ); |
| 2365 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2366 | goto exit; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2367 | |
| 2368 | psa_key_attributes_t attributes = { |
| 2369 | .core = slot->attr |
| 2370 | }; |
| 2371 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2372 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2373 | &operation->mac_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2374 | if( status != PSA_SUCCESS ) |
| 2375 | goto exit; |
| 2376 | |
| 2377 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2378 | /* Dispatch the MAC setup call with validated input */ |
| 2379 | if( is_sign ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2380 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2381 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 2382 | &attributes, |
| 2383 | slot->key.data, |
| 2384 | slot->key.bytes, |
| 2385 | alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2386 | } |
| 2387 | else |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2388 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2389 | status = psa_driver_wrapper_mac_verify_setup( operation, |
| 2390 | &attributes, |
| 2391 | slot->key.data, |
| 2392 | slot->key.bytes, |
| 2393 | alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2394 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2395 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2396 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2397 | if( status != PSA_SUCCESS ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2398 | psa_mac_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2399 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2400 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2401 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2402 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2403 | } |
| 2404 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2405 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2406 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2407 | psa_algorithm_t alg ) |
| 2408 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2409 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2413 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2414 | psa_algorithm_t alg ) |
| 2415 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2416 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2419 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2420 | const uint8_t *input, |
| 2421 | size_t input_length ) |
| 2422 | { |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2423 | if( operation->id == 0 ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2424 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2425 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2426 | /* Don't require hash implementations to behave correctly on a |
| 2427 | * zero-length input, which may have an invalid pointer. */ |
| 2428 | if( input_length == 0 ) |
| 2429 | return( PSA_SUCCESS ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2430 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2431 | psa_status_t status = psa_driver_wrapper_mac_update( operation, |
| 2432 | input, input_length ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2433 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2434 | psa_mac_abort( operation ); |
| 2435 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2436 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2437 | } |
| 2438 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2439 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2440 | uint8_t *mac, |
| 2441 | size_t mac_size, |
| 2442 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2443 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2444 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2445 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2446 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2447 | if( operation->id == 0 ) |
Dave Rodgman | 8036bdd | 2021-06-24 16:19:08 +0100 | [diff] [blame] | 2448 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2449 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2450 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2451 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2452 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2453 | if( ! operation->is_sign ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2454 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2455 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2456 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2457 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2458 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2459 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2460 | * once all the error checks are done. */ |
| 2461 | if( operation->mac_size == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2462 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2463 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2464 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2465 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2466 | |
| 2467 | if( mac_size < operation->mac_size ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2468 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2469 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2470 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2471 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2472 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2473 | status = psa_driver_wrapper_mac_sign_finish( operation, |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2474 | mac, operation->mac_size, |
| 2475 | mac_length ); |
| 2476 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2477 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2478 | /* In case of success, set the potential excess room in the output buffer |
| 2479 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2480 | * In case of error, set the output length and content to a safe default, |
| 2481 | * such that in case the caller misses an error check, the output would be |
| 2482 | * an unachievable MAC. |
| 2483 | */ |
| 2484 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2485 | { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2486 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2487 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2488 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2489 | |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2490 | if( mac_size > operation->mac_size ) |
| 2491 | memset( &mac[operation->mac_size], '!', |
| 2492 | mac_size - operation->mac_size ); |
| 2493 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2494 | abort_status = psa_mac_abort( operation ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2495 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2496 | return( status == PSA_SUCCESS ? abort_status : status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2499 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2500 | const uint8_t *mac, |
| 2501 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2502 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2503 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2504 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2505 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2506 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2507 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2508 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2509 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2510 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2511 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2512 | if( operation->is_sign ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2513 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2514 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2515 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2516 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2517 | |
| 2518 | if( operation->mac_size != mac_length ) |
| 2519 | { |
| 2520 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2521 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2522 | } |
| 2523 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2524 | status = psa_driver_wrapper_mac_verify_finish( operation, |
| 2525 | mac, mac_length ); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2526 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2527 | exit: |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2528 | abort_status = psa_mac_abort( operation ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2529 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2530 | return( status == PSA_SUCCESS ? abort_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2531 | } |
| 2532 | |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2533 | static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key, |
| 2534 | psa_algorithm_t alg, |
| 2535 | const uint8_t *input, |
| 2536 | size_t input_length, |
| 2537 | uint8_t *mac, |
| 2538 | size_t mac_size, |
| 2539 | size_t *mac_length, |
| 2540 | int is_sign ) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2541 | { |
| 2542 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2543 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2544 | psa_key_slot_t *slot; |
| 2545 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2546 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2547 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | ce0e6a9 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2548 | key, |
| 2549 | &slot, |
| 2550 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2551 | alg ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2552 | if( status != PSA_SUCCESS ) |
| 2553 | goto exit; |
| 2554 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2555 | psa_key_attributes_t attributes = { |
| 2556 | .core = slot->attr |
| 2557 | }; |
| 2558 | |
| 2559 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2560 | &operation_mac_size ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2561 | if( status != PSA_SUCCESS ) |
| 2562 | goto exit; |
| 2563 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2564 | if( mac_size < operation_mac_size ) |
| 2565 | { |
| 2566 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2567 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | status = psa_driver_wrapper_mac_compute( |
| 2571 | &attributes, |
| 2572 | slot->key.data, slot->key.bytes, |
| 2573 | alg, |
| 2574 | input, input_length, |
| 2575 | mac, operation_mac_size, mac_length ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2576 | |
| 2577 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2578 | /* In case of success, set the potential excess room in the output buffer |
| 2579 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2580 | * In case of error, set the output length and content to a safe default, |
| 2581 | * such that in case the caller misses an error check, the output would be |
| 2582 | * an unachievable MAC. |
| 2583 | */ |
| 2584 | if( status != PSA_SUCCESS ) |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2585 | { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2586 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2587 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2588 | } |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2589 | if( mac_size > operation_mac_size ) |
| 2590 | memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2591 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2592 | unlock_status = psa_unlock_key_slot( slot ); |
| 2593 | |
| 2594 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2595 | } |
| 2596 | |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2597 | psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key, |
| 2598 | psa_algorithm_t alg, |
| 2599 | const uint8_t *input, |
| 2600 | size_t input_length, |
| 2601 | uint8_t *mac, |
| 2602 | size_t mac_size, |
| 2603 | size_t *mac_length) |
| 2604 | { |
| 2605 | return( psa_mac_compute_internal( key, alg, |
| 2606 | input, input_length, |
| 2607 | mac, mac_size, mac_length, 1 ) ); |
| 2608 | } |
| 2609 | |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2610 | psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key, |
| 2611 | psa_algorithm_t alg, |
| 2612 | const uint8_t *input, |
| 2613 | size_t input_length, |
| 2614 | const uint8_t *mac, |
| 2615 | size_t mac_length) |
| 2616 | { |
| 2617 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2618 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2619 | size_t actual_mac_length; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2620 | |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2621 | status = psa_mac_compute_internal( key, alg, |
| 2622 | input, input_length, |
| 2623 | actual_mac, sizeof( actual_mac ), |
| 2624 | &actual_mac_length, 0 ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2625 | if( status != PSA_SUCCESS ) |
| 2626 | goto exit; |
| 2627 | |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2628 | if( mac_length != actual_mac_length ) |
| 2629 | { |
| 2630 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2631 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2632 | } |
| 2633 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 ) |
| 2634 | { |
| 2635 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2636 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2637 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2638 | |
| 2639 | exit: |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2640 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2641 | |
| 2642 | return ( status ); |
| 2643 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2644 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2645 | /****************************************************************/ |
| 2646 | /* Asymmetric cryptography */ |
| 2647 | /****************************************************************/ |
| 2648 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2649 | 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] | 2650 | psa_algorithm_t alg ) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2651 | { |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2652 | if( input_is_message ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2653 | { |
| 2654 | if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) ) |
| 2655 | return( PSA_ERROR_INVALID_ARGUMENT ); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2656 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2657 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2658 | { |
| 2659 | if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) ) |
| 2660 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2661 | } |
| 2662 | } |
| 2663 | else |
| 2664 | { |
Mateusz Starzyk | e6d3eda | 2021-08-26 11:46:14 +0200 | [diff] [blame] | 2665 | if( ! PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2666 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2667 | } |
| 2668 | |
| 2669 | return( PSA_SUCCESS ); |
| 2670 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2671 | |
| 2672 | 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] | 2673 | int input_is_message, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2674 | psa_algorithm_t alg, |
| 2675 | const uint8_t * input, |
| 2676 | size_t input_length, |
| 2677 | uint8_t * signature, |
| 2678 | size_t signature_size, |
| 2679 | size_t * signature_length ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2680 | { |
| 2681 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2682 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2683 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2684 | |
| 2685 | *signature_length = 0; |
| 2686 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2687 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2688 | if( status != PSA_SUCCESS ) |
| 2689 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2690 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2691 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2692 | * 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] | 2693 | * buffer can in principle be empty since it doesn't actually have |
| 2694 | * to be a hash.) */ |
| 2695 | if( signature_size == 0 ) |
| 2696 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2697 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2698 | status = psa_get_and_lock_key_slot_with_policy( |
| 2699 | key, &slot, |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2700 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2701 | PSA_KEY_USAGE_SIGN_HASH, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2702 | alg ); |
| 2703 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2704 | if( status != PSA_SUCCESS ) |
| 2705 | goto exit; |
| 2706 | |
| 2707 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
| 2708 | { |
| 2709 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2710 | goto exit; |
| 2711 | } |
| 2712 | |
| 2713 | psa_key_attributes_t attributes = { |
| 2714 | .core = slot->attr |
| 2715 | }; |
| 2716 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2717 | if( input_is_message ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2718 | { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2719 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2720 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2721 | alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2722 | signature, signature_size, signature_length ); |
| 2723 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2724 | else |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2725 | { |
| 2726 | |
| 2727 | status = psa_driver_wrapper_sign_hash( |
| 2728 | &attributes, slot->key.data, slot->key.bytes, |
| 2729 | alg, input, input_length, |
| 2730 | signature, signature_size, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2731 | } |
| 2732 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2733 | |
| 2734 | exit: |
| 2735 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2736 | * the trailing part on success) with something that isn't a valid signature |
| 2737 | * (barring an attack on the signature and deliberately-crafted input), |
| 2738 | * in case the caller doesn't check the return status properly. */ |
| 2739 | if( status == PSA_SUCCESS ) |
| 2740 | memset( signature + *signature_length, '!', |
| 2741 | signature_size - *signature_length ); |
| 2742 | else |
| 2743 | memset( signature, '!', signature_size ); |
| 2744 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2745 | * memset because signature may be NULL in this case. */ |
| 2746 | |
| 2747 | unlock_status = psa_unlock_key_slot( slot ); |
| 2748 | |
| 2749 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 2750 | } |
| 2751 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2752 | 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] | 2753 | int input_is_message, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2754 | psa_algorithm_t alg, |
| 2755 | const uint8_t * input, |
| 2756 | size_t input_length, |
| 2757 | const uint8_t * signature, |
| 2758 | size_t signature_length ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2759 | { |
| 2760 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2761 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2762 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2763 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2764 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2765 | if( status != PSA_SUCCESS ) |
| 2766 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2767 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2768 | status = psa_get_and_lock_key_slot_with_policy( |
| 2769 | key, &slot, |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2770 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2771 | PSA_KEY_USAGE_VERIFY_HASH, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2772 | alg ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2773 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2774 | if( status != PSA_SUCCESS ) |
| 2775 | return( status ); |
| 2776 | |
| 2777 | psa_key_attributes_t attributes = { |
| 2778 | .core = slot->attr |
| 2779 | }; |
| 2780 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2781 | if( input_is_message ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2782 | { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2783 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2784 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2785 | alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2786 | signature, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2787 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2788 | else |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2789 | { |
| 2790 | status = psa_driver_wrapper_verify_hash( |
| 2791 | &attributes, slot->key.data, slot->key.bytes, |
| 2792 | alg, input, input_length, |
| 2793 | signature, signature_length ); |
| 2794 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2795 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2796 | unlock_status = psa_unlock_key_slot( slot ); |
| 2797 | |
| 2798 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2799 | |
| 2800 | } |
| 2801 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2802 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2803 | const psa_key_attributes_t *attributes, |
| 2804 | const uint8_t *key_buffer, |
| 2805 | size_t key_buffer_size, |
| 2806 | psa_algorithm_t alg, |
| 2807 | const uint8_t *input, |
| 2808 | size_t input_length, |
| 2809 | uint8_t *signature, |
| 2810 | size_t signature_size, |
| 2811 | size_t *signature_length ) |
| 2812 | { |
| 2813 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2814 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2815 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2816 | { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2817 | size_t hash_length; |
| 2818 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2819 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2820 | status = psa_driver_wrapper_hash_compute( |
| 2821 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2822 | input, input_length, |
| 2823 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2824 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2825 | if( status != PSA_SUCCESS ) |
| 2826 | return status; |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2827 | |
| 2828 | return psa_driver_wrapper_sign_hash( |
| 2829 | attributes, key_buffer, key_buffer_size, |
| 2830 | alg, hash, hash_length, |
| 2831 | signature, signature_size, signature_length ); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2832 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2833 | |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2834 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2835 | } |
| 2836 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2837 | psa_status_t psa_sign_message( mbedtls_svc_key_id_t key, |
| 2838 | psa_algorithm_t alg, |
| 2839 | const uint8_t * input, |
| 2840 | size_t input_length, |
| 2841 | uint8_t * signature, |
| 2842 | size_t signature_size, |
| 2843 | size_t * signature_length ) |
| 2844 | { |
| 2845 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2846 | key, 1, alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2847 | signature, signature_size, signature_length ); |
| 2848 | } |
| 2849 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2850 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2851 | const psa_key_attributes_t *attributes, |
| 2852 | const uint8_t *key_buffer, |
| 2853 | size_t key_buffer_size, |
| 2854 | psa_algorithm_t alg, |
| 2855 | const uint8_t *input, |
| 2856 | size_t input_length, |
| 2857 | const uint8_t *signature, |
| 2858 | size_t signature_length ) |
| 2859 | { |
| 2860 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2861 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2862 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2863 | { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2864 | size_t hash_length; |
| 2865 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2866 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2867 | status = psa_driver_wrapper_hash_compute( |
| 2868 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2869 | input, input_length, |
| 2870 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2871 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2872 | if( status != PSA_SUCCESS ) |
| 2873 | return status; |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2874 | |
| 2875 | return psa_driver_wrapper_verify_hash( |
| 2876 | attributes, key_buffer, key_buffer_size, |
| 2877 | alg, hash, hash_length, |
| 2878 | signature, signature_length ); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2879 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2880 | |
gabor-mezei-arm | 474a35f | 2021-05-05 13:59:01 +0200 | [diff] [blame] | 2881 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2882 | } |
| 2883 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2884 | psa_status_t psa_verify_message( mbedtls_svc_key_id_t key, |
| 2885 | psa_algorithm_t alg, |
| 2886 | const uint8_t * input, |
| 2887 | size_t input_length, |
| 2888 | const uint8_t * signature, |
| 2889 | size_t signature_length ) |
| 2890 | { |
| 2891 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2892 | key, 1, alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2893 | signature, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2894 | } |
| 2895 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2896 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2897 | const psa_key_attributes_t *attributes, |
| 2898 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2899 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2900 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2901 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2902 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2903 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2904 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2905 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2906 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2907 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2908 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2909 | return( mbedtls_psa_rsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2910 | attributes, |
| 2911 | key_buffer, key_buffer_size, |
| 2912 | alg, hash, hash_length, |
| 2913 | signature, signature_size, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2914 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2915 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2916 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2917 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2918 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2919 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2920 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2921 | } |
Ronald Cron | 81ca97e | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 2922 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2923 | { |
| 2924 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 2925 | { |
| 2926 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2927 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2928 | return( mbedtls_psa_ecdsa_sign_hash( |
| 2929 | attributes, |
| 2930 | key_buffer, key_buffer_size, |
| 2931 | alg, hash, hash_length, |
| 2932 | signature, signature_size, signature_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2933 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2934 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2935 | } |
| 2936 | else |
| 2937 | { |
| 2938 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2939 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2940 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2941 | |
| 2942 | (void)key_buffer; |
| 2943 | (void)key_buffer_size; |
| 2944 | (void)hash; |
| 2945 | (void)hash_length; |
| 2946 | (void)signature; |
| 2947 | (void)signature_size; |
| 2948 | (void)signature_length; |
| 2949 | |
| 2950 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2951 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2952 | |
| 2953 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
| 2954 | psa_algorithm_t alg, |
| 2955 | const uint8_t *hash, |
| 2956 | size_t hash_length, |
| 2957 | uint8_t *signature, |
| 2958 | size_t signature_size, |
| 2959 | size_t *signature_length ) |
| 2960 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2961 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2962 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 2963 | signature, signature_size, signature_length ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2964 | } |
| 2965 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2966 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2967 | const psa_key_attributes_t *attributes, |
| 2968 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2969 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2970 | const uint8_t *signature, size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2971 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2972 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2973 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2974 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2975 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2976 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2977 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2978 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2979 | return( mbedtls_psa_rsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2980 | attributes, |
| 2981 | key_buffer, key_buffer_size, |
| 2982 | alg, hash, hash_length, |
| 2983 | signature, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2984 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2985 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2986 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2987 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2988 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2989 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2990 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2991 | } |
Ronald Cron | 81ca97e | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 2992 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2993 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2994 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 2995 | { |
| 2996 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2997 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2998 | return( mbedtls_psa_ecdsa_verify_hash( |
| 2999 | attributes, |
| 3000 | key_buffer, key_buffer_size, |
| 3001 | alg, hash, hash_length, |
| 3002 | signature, signature_length ) ); |
| 3003 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3004 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3005 | } |
| 3006 | else |
| 3007 | { |
| 3008 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3009 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3010 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3011 | |
| 3012 | (void)key_buffer; |
| 3013 | (void)key_buffer_size; |
| 3014 | (void)hash; |
| 3015 | (void)hash_length; |
| 3016 | (void)signature; |
| 3017 | (void)signature_length; |
| 3018 | |
| 3019 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3020 | } |
| 3021 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3022 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
| 3023 | psa_algorithm_t alg, |
| 3024 | const uint8_t *hash, |
| 3025 | size_t hash_length, |
| 3026 | const uint8_t *signature, |
| 3027 | size_t signature_length ) |
| 3028 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3029 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3030 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3031 | signature, signature_length ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3032 | } |
| 3033 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3034 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3035 | psa_algorithm_t alg, |
| 3036 | const uint8_t *input, |
| 3037 | size_t input_length, |
| 3038 | const uint8_t *salt, |
| 3039 | size_t salt_length, |
| 3040 | uint8_t *output, |
| 3041 | size_t output_size, |
| 3042 | size_t *output_length ) |
| 3043 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3044 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3045 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3046 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3047 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3048 | (void) input; |
| 3049 | (void) input_length; |
| 3050 | (void) salt; |
| 3051 | (void) output; |
| 3052 | (void) output_size; |
| 3053 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3054 | *output_length = 0; |
| 3055 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3056 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3057 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3058 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3059 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3060 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3061 | if( status != PSA_SUCCESS ) |
| 3062 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3063 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3064 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3065 | { |
| 3066 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3067 | goto exit; |
| 3068 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3069 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3070 | psa_key_attributes_t attributes = { |
| 3071 | .core = slot->attr |
| 3072 | }; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3073 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3074 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3075 | &attributes, slot->key.data, slot->key.bytes, |
| 3076 | alg, input, input_length, salt, salt_length, |
| 3077 | output, output_size, output_length ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3078 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3079 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3080 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3081 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3082 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3083 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3084 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3085 | psa_algorithm_t alg, |
| 3086 | const uint8_t *input, |
| 3087 | size_t input_length, |
| 3088 | const uint8_t *salt, |
| 3089 | size_t salt_length, |
| 3090 | uint8_t *output, |
| 3091 | size_t output_size, |
| 3092 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3093 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3094 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3095 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3096 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3097 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3098 | (void) input; |
| 3099 | (void) input_length; |
| 3100 | (void) salt; |
| 3101 | (void) output; |
| 3102 | (void) output_size; |
| 3103 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3104 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3105 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3106 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3107 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3108 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3109 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3110 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3111 | if( status != PSA_SUCCESS ) |
| 3112 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3113 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3114 | { |
| 3115 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3116 | goto exit; |
| 3117 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3118 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3119 | psa_key_attributes_t attributes = { |
| 3120 | .core = slot->attr |
| 3121 | }; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3122 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3123 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3124 | &attributes, slot->key.data, slot->key.bytes, |
| 3125 | alg, input, input_length, salt, salt_length, |
| 3126 | output, output_size, output_length ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3127 | |
| 3128 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3129 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3130 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3131 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3132 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3133 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3134 | |
| 3135 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3136 | /****************************************************************/ |
| 3137 | /* Symmetric cryptography */ |
| 3138 | /****************************************************************/ |
| 3139 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3140 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
| 3141 | mbedtls_svc_key_id_t key, |
| 3142 | psa_algorithm_t alg, |
| 3143 | mbedtls_operation_t cipher_operation ) |
| 3144 | { |
| 3145 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3146 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3147 | psa_key_slot_t *slot = NULL; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3148 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3149 | PSA_KEY_USAGE_ENCRYPT : |
| 3150 | PSA_KEY_USAGE_DECRYPT ); |
| 3151 | |
| 3152 | /* A context must be freshly initialized before it can be set up. */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3153 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3154 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3155 | status = PSA_ERROR_BAD_STATE; |
| 3156 | goto exit; |
| 3157 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3158 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3159 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3160 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3161 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3162 | goto exit; |
| 3163 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3164 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3165 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg ); |
| 3166 | if( status != PSA_SUCCESS ) |
| 3167 | goto exit; |
| 3168 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3169 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3170 | * 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] | 3171 | * so we only set it (in the driver wrapper) after resources have been |
| 3172 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3173 | operation->iv_set = 0; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3174 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3175 | operation->iv_required = 0; |
| 3176 | else |
| 3177 | operation->iv_required = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3178 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3179 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3180 | psa_key_attributes_t attributes = { |
| 3181 | .core = slot->attr |
| 3182 | }; |
| 3183 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3184 | /* Try doing the operation through a driver before using software fallback. */ |
| 3185 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3186 | status = psa_driver_wrapper_cipher_encrypt_setup( operation, |
| 3187 | &attributes, |
| 3188 | slot->key.data, |
| 3189 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3190 | alg ); |
| 3191 | else |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3192 | status = psa_driver_wrapper_cipher_decrypt_setup( operation, |
| 3193 | &attributes, |
| 3194 | slot->key.data, |
| 3195 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3196 | alg ); |
| 3197 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3198 | exit: |
Ronald Cron | 06aa442 | 2021-03-09 17:32:57 +0100 | [diff] [blame] | 3199 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3200 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3201 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3202 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3203 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3204 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3205 | } |
| 3206 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3207 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3208 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3209 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3210 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3211 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3212 | } |
| 3213 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3214 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3215 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3216 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3217 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3218 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3219 | } |
| 3220 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3221 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3222 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3223 | size_t iv_size, |
| 3224 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3225 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3226 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3227 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3228 | size_t default_iv_length; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3229 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3230 | if( operation->id == 0 ) |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3231 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3232 | status = PSA_ERROR_BAD_STATE; |
| 3233 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3234 | } |
| 3235 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3236 | if( operation->iv_set || ! operation->iv_required ) |
| 3237 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3238 | status = PSA_ERROR_BAD_STATE; |
| 3239 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3240 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3241 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3242 | default_iv_length = operation->default_iv_length; |
| 3243 | if( iv_size < default_iv_length ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3244 | { |
| 3245 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3246 | goto exit; |
| 3247 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3248 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3249 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
| 3250 | { |
| 3251 | status = PSA_ERROR_GENERIC_ERROR; |
| 3252 | goto exit; |
| 3253 | } |
| 3254 | |
| 3255 | status = psa_generate_random( local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3256 | if( status != PSA_SUCCESS ) |
| 3257 | goto exit; |
| 3258 | |
| 3259 | status = psa_driver_wrapper_cipher_set_iv( operation, |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3260 | local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3261 | |
| 3262 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3263 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3264 | { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3265 | memcpy( iv, local_iv, default_iv_length ); |
| 3266 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3267 | operation->iv_set = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3268 | } |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3269 | else |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3270 | { |
| 3271 | *iv_length = 0; |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3272 | psa_cipher_abort( operation ); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3273 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3274 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3275 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3276 | } |
| 3277 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3278 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3279 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3280 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3281 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3282 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3283 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3284 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3285 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3286 | status = PSA_ERROR_BAD_STATE; |
| 3287 | goto exit; |
| 3288 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3289 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3290 | if( operation->iv_set || ! operation->iv_required ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3291 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3292 | status = PSA_ERROR_BAD_STATE; |
| 3293 | goto exit; |
| 3294 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3295 | |
| 3296 | if( iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3297 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3298 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3299 | goto exit; |
| 3300 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3301 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3302 | status = psa_driver_wrapper_cipher_set_iv( operation, |
| 3303 | iv, |
| 3304 | iv_length ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3305 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3306 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3307 | if( status == PSA_SUCCESS ) |
| 3308 | operation->iv_set = 1; |
| 3309 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3310 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3311 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3312 | } |
| 3313 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3314 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3315 | const uint8_t *input, |
| 3316 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3317 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3318 | size_t output_size, |
| 3319 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3320 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3321 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3322 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3323 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3324 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3325 | status = PSA_ERROR_BAD_STATE; |
| 3326 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3327 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3328 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3329 | if( operation->iv_required && ! operation->iv_set ) |
| 3330 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3331 | status = PSA_ERROR_BAD_STATE; |
| 3332 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3333 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3334 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3335 | status = psa_driver_wrapper_cipher_update( operation, |
| 3336 | input, |
| 3337 | input_length, |
| 3338 | output, |
| 3339 | output_size, |
| 3340 | output_length ); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3341 | |
| 3342 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3343 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3344 | psa_cipher_abort( operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3345 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3346 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3347 | } |
| 3348 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3349 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3350 | uint8_t *output, |
| 3351 | size_t output_size, |
| 3352 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3353 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3354 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3355 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3356 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3357 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3358 | status = PSA_ERROR_BAD_STATE; |
| 3359 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3360 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3361 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3362 | if( operation->iv_required && ! operation->iv_set ) |
| 3363 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3364 | status = PSA_ERROR_BAD_STATE; |
| 3365 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3366 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3367 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3368 | status = psa_driver_wrapper_cipher_finish( operation, |
| 3369 | output, |
| 3370 | output_size, |
| 3371 | output_length ); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3372 | |
| 3373 | exit: |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3374 | if( status == PSA_SUCCESS ) |
| 3375 | return( psa_cipher_abort( operation ) ); |
| 3376 | else |
| 3377 | { |
| 3378 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3379 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3380 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3381 | return( status ); |
| 3382 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3383 | } |
| 3384 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3385 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3386 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3387 | if( operation->id == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3388 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3389 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3390 | * in use. It's ok to call abort on such an object, and there's |
| 3391 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3392 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3393 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3394 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3395 | psa_driver_wrapper_cipher_abort( operation ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3396 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3397 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3398 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3399 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3400 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3401 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3402 | } |
| 3403 | |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3404 | psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key, |
| 3405 | psa_algorithm_t alg, |
| 3406 | const uint8_t *input, |
| 3407 | size_t input_length, |
| 3408 | uint8_t *output, |
| 3409 | size_t output_size, |
| 3410 | size_t *output_length ) |
| 3411 | { |
| 3412 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3413 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3414 | psa_key_slot_t *slot = NULL; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3415 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3416 | size_t default_iv_length = 0; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3417 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3418 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3419 | { |
| 3420 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3421 | goto exit; |
| 3422 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3423 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3424 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3425 | PSA_KEY_USAGE_ENCRYPT, |
| 3426 | alg ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3427 | if( status != PSA_SUCCESS ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3428 | goto exit; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3429 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3430 | psa_key_attributes_t attributes = { |
| 3431 | .core = slot->attr |
| 3432 | }; |
| 3433 | |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3434 | default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
| 3435 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3436 | { |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3437 | status = PSA_ERROR_GENERIC_ERROR; |
| 3438 | goto exit; |
| 3439 | } |
| 3440 | |
| 3441 | if( default_iv_length > 0 ) |
| 3442 | { |
| 3443 | if( output_size < default_iv_length ) |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3444 | { |
| 3445 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3446 | goto exit; |
| 3447 | } |
| 3448 | |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3449 | status = psa_generate_random( local_iv, default_iv_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3450 | if( status != PSA_SUCCESS ) |
| 3451 | goto exit; |
| 3452 | } |
| 3453 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3454 | status = psa_driver_wrapper_cipher_encrypt( |
| 3455 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3456 | alg, local_iv, default_iv_length, input, input_length, |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 3457 | mbedtls_buffer_offset( output, default_iv_length ), |
| 3458 | output_size - default_iv_length, output_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3459 | |
| 3460 | exit: |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3461 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3462 | if( status == PSA_SUCCESS ) |
| 3463 | status = unlock_status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3464 | |
Ronald Cron | 9b67428 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3465 | if( status == PSA_SUCCESS ) |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3466 | { |
| 3467 | if( default_iv_length > 0 ) |
| 3468 | memcpy( output, local_iv, default_iv_length ); |
| 3469 | *output_length += default_iv_length; |
| 3470 | } |
Ronald Cron | 9b67428 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3471 | else |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3472 | *output_length = 0; |
| 3473 | |
| 3474 | return( status ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key, |
| 3478 | psa_algorithm_t alg, |
| 3479 | const uint8_t *input, |
| 3480 | size_t input_length, |
| 3481 | uint8_t *output, |
| 3482 | size_t output_size, |
| 3483 | size_t *output_length ) |
| 3484 | { |
| 3485 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3486 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3487 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3488 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3489 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3490 | { |
| 3491 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3492 | goto exit; |
| 3493 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3494 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3495 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3496 | PSA_KEY_USAGE_DECRYPT, |
| 3497 | alg ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3498 | if( status != PSA_SUCCESS ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3499 | goto exit; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3500 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3501 | psa_key_attributes_t attributes = { |
| 3502 | .core = slot->attr |
| 3503 | }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3504 | |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 3505 | if( alg == PSA_ALG_CCM_STAR_NO_TAG && input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) ) |
| 3506 | { |
| 3507 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3508 | goto exit; |
| 3509 | } |
| 3510 | 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] | 3511 | { |
| 3512 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3513 | goto exit; |
| 3514 | } |
| 3515 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3516 | status = psa_driver_wrapper_cipher_decrypt( |
| 3517 | &attributes, slot->key.data, slot->key.bytes, |
| 3518 | alg, input, input_length, |
| 3519 | output, output_size, output_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3520 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3521 | exit: |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3522 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3523 | if( status == PSA_SUCCESS ) |
| 3524 | status = unlock_status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3525 | |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3526 | if( status != PSA_SUCCESS ) |
| 3527 | *output_length = 0; |
| 3528 | |
| 3529 | return( status ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3533 | /****************************************************************/ |
| 3534 | /* AEAD */ |
| 3535 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3536 | |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3537 | /* Helper function to get the base algorithm from its variants. */ |
| 3538 | static psa_algorithm_t psa_aead_get_base_algorithm( psa_algorithm_t alg ) |
| 3539 | { |
| 3540 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg ); |
| 3541 | } |
| 3542 | |
| 3543 | /* Helper function to perform common nonce length checks. */ |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3544 | static psa_status_t psa_aead_check_nonce_length( psa_algorithm_t alg, |
| 3545 | size_t nonce_length ) |
| 3546 | { |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3547 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm( alg ); |
| 3548 | |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3549 | switch(base_alg) |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3550 | { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3551 | #if defined(PSA_WANT_ALG_GCM) |
| 3552 | case PSA_ALG_GCM: |
| 3553 | /* Not checking max nonce size here as GCM spec allows almost |
| 3554 | * arbitrarily large nonces. Please note that we do not generally |
| 3555 | * recommend the usage of nonces of greater length than |
| 3556 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter |
| 3557 | * size, which can then lead to collisions if you encrypt a very |
| 3558 | * large number of messages.*/ |
| 3559 | if( nonce_length != 0 ) |
| 3560 | return( PSA_SUCCESS ); |
| 3561 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3562 | #endif /* PSA_WANT_ALG_GCM */ |
| 3563 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3564 | case PSA_ALG_CCM: |
| 3565 | if( nonce_length >= 7 && nonce_length <= 13 ) |
| 3566 | return( PSA_SUCCESS ); |
| 3567 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3568 | #endif /* PSA_WANT_ALG_CCM */ |
| 3569 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3570 | case PSA_ALG_CHACHA20_POLY1305: |
| 3571 | if( nonce_length == 12 ) |
| 3572 | return( PSA_SUCCESS ); |
Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 3573 | else if( nonce_length == 8 ) |
| 3574 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3575 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3576 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3577 | default: |
Przemek Stekiel | 4c49927 | 2022-09-27 13:55:37 +0200 | [diff] [blame] | 3578 | (void) nonce_length; |
Bence Szépkúti | 357b78e | 2021-11-09 13:17:17 +0100 | [diff] [blame] | 3579 | return( PSA_ERROR_NOT_SUPPORTED ); |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3580 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3581 | |
Bence Szépkúti | 357b78e | 2021-11-09 13:17:17 +0100 | [diff] [blame] | 3582 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3583 | } |
| 3584 | |
Bence Szépkúti | aa3a6e4 | 2022-01-13 16:26:03 +0100 | [diff] [blame] | 3585 | static psa_status_t psa_aead_check_algorithm( psa_algorithm_t alg ) |
| 3586 | { |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3587 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3588 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3589 | |
| 3590 | return( PSA_SUCCESS ); |
| 3591 | } |
| 3592 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3593 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3594 | psa_algorithm_t alg, |
| 3595 | const uint8_t *nonce, |
| 3596 | size_t nonce_length, |
| 3597 | const uint8_t *additional_data, |
| 3598 | size_t additional_data_length, |
| 3599 | const uint8_t *plaintext, |
| 3600 | size_t plaintext_length, |
| 3601 | uint8_t *ciphertext, |
| 3602 | size_t ciphertext_size, |
| 3603 | size_t *ciphertext_length ) |
| 3604 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3605 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3606 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3607 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3608 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3609 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3610 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3611 | if( status != PSA_SUCCESS ) |
| 3612 | return( status ); |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3613 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3614 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3615 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3616 | if( status != PSA_SUCCESS ) |
| 3617 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3618 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3619 | psa_key_attributes_t attributes = { |
| 3620 | .core = slot->attr |
| 3621 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3622 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3623 | status = psa_aead_check_nonce_length( alg, nonce_length ); |
| 3624 | if( status != PSA_SUCCESS ) |
| 3625 | goto exit; |
| 3626 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3627 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3628 | &attributes, slot->key.data, slot->key.bytes, |
| 3629 | alg, |
| 3630 | nonce, nonce_length, |
| 3631 | additional_data, additional_data_length, |
| 3632 | plaintext, plaintext_length, |
| 3633 | ciphertext, ciphertext_size, ciphertext_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3634 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3635 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3636 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3637 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3638 | exit: |
Ronald Cron | 9f31017 | 2021-03-16 16:36:37 +0100 | [diff] [blame] | 3639 | psa_unlock_key_slot( slot ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3640 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3641 | return( status ); |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3642 | } |
| 3643 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3644 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3645 | psa_algorithm_t alg, |
| 3646 | const uint8_t *nonce, |
| 3647 | size_t nonce_length, |
| 3648 | const uint8_t *additional_data, |
| 3649 | size_t additional_data_length, |
| 3650 | const uint8_t *ciphertext, |
| 3651 | size_t ciphertext_length, |
| 3652 | uint8_t *plaintext, |
| 3653 | size_t plaintext_size, |
| 3654 | size_t *plaintext_length ) |
| 3655 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3656 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3657 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3658 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3659 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3660 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3661 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3662 | if( status != PSA_SUCCESS ) |
| 3663 | return( status ); |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3664 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3665 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3666 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3667 | if( status != PSA_SUCCESS ) |
| 3668 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3669 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3670 | psa_key_attributes_t attributes = { |
| 3671 | .core = slot->attr |
| 3672 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3673 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3674 | status = psa_aead_check_nonce_length( alg, nonce_length ); |
| 3675 | if( status != PSA_SUCCESS ) |
| 3676 | goto exit; |
| 3677 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3678 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3679 | &attributes, slot->key.data, slot->key.bytes, |
| 3680 | alg, |
| 3681 | nonce, nonce_length, |
| 3682 | additional_data, additional_data_length, |
| 3683 | ciphertext, ciphertext_length, |
| 3684 | plaintext, plaintext_size, plaintext_length ); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3685 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3686 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3687 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3688 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3689 | exit: |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3690 | psa_unlock_key_slot( slot ); |
| 3691 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3692 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3693 | } |
| 3694 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3695 | static psa_status_t psa_validate_tag_length( psa_algorithm_t alg ) { |
Przemek Stekiel | 8a05a64 | 2022-10-06 17:01:58 +0200 | [diff] [blame] | 3696 | const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3697 | |
| 3698 | switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ) |
| 3699 | { |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3700 | #if defined(PSA_WANT_ALG_CCM) |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3701 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
| 3702 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/ |
| 3703 | if( tag_len < 4 || tag_len > 16 || tag_len % 2 ) |
| 3704 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3705 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3706 | #endif /* PSA_WANT_ALG_CCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3707 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3708 | #if defined(PSA_WANT_ALG_GCM) |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3709 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
| 3710 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */ |
| 3711 | if( tag_len != 4 && tag_len != 8 && ( tag_len < 12 || tag_len > 16 ) ) |
| 3712 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3713 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3714 | #endif /* PSA_WANT_ALG_GCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3715 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3716 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3717 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3718 | /* We only support the default tag length. */ |
| 3719 | if( tag_len != 16 ) |
| 3720 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3721 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3722 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3723 | |
| 3724 | default: |
| 3725 | (void) tag_len; |
| 3726 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3727 | } |
| 3728 | return( PSA_SUCCESS ); |
| 3729 | } |
| 3730 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3731 | /* Set the key for a multipart authenticated operation. */ |
| 3732 | static psa_status_t psa_aead_setup( psa_aead_operation_t *operation, |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3733 | int is_encrypt, |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3734 | mbedtls_svc_key_id_t key, |
| 3735 | psa_algorithm_t alg ) |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3736 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3737 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3738 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3739 | psa_key_slot_t *slot = NULL; |
| 3740 | psa_key_usage_t key_usage = 0; |
| 3741 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3742 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3743 | if( status != PSA_SUCCESS ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3744 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3745 | |
| 3746 | if( operation->id != 0 ) |
| 3747 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3748 | status = PSA_ERROR_BAD_STATE; |
| 3749 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | if( operation->nonce_set || operation->lengths_set || |
| 3753 | operation->ad_started || operation->body_started ) |
| 3754 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3755 | status = PSA_ERROR_BAD_STATE; |
| 3756 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3757 | } |
| 3758 | |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3759 | if( is_encrypt ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3760 | key_usage = PSA_KEY_USAGE_ENCRYPT; |
| 3761 | else |
| 3762 | key_usage = PSA_KEY_USAGE_DECRYPT; |
| 3763 | |
| 3764 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, key_usage, |
| 3765 | alg ); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3766 | if( status != PSA_SUCCESS ) |
| 3767 | goto exit; |
| 3768 | |
| 3769 | psa_key_attributes_t attributes = { |
| 3770 | .core = slot->attr |
| 3771 | }; |
| 3772 | |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 3773 | if( ( status = psa_validate_tag_length( alg ) ) != PSA_SUCCESS ) |
| 3774 | goto exit; |
| 3775 | |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3776 | if( is_encrypt ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3777 | status = psa_driver_wrapper_aead_encrypt_setup( operation, |
| 3778 | &attributes, |
| 3779 | slot->key.data, |
| 3780 | slot->key.bytes, |
| 3781 | alg ); |
| 3782 | else |
| 3783 | status = psa_driver_wrapper_aead_decrypt_setup( operation, |
| 3784 | &attributes, |
| 3785 | slot->key.data, |
| 3786 | slot->key.bytes, |
| 3787 | alg ); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3788 | if( status != PSA_SUCCESS ) |
| 3789 | goto exit; |
| 3790 | |
| 3791 | operation->key_type = psa_get_key_type( &attributes ); |
| 3792 | |
| 3793 | exit: |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3794 | unlock_status = psa_unlock_key_slot( slot ); |
| 3795 | |
| 3796 | if( status == PSA_SUCCESS ) |
| 3797 | { |
| 3798 | status = unlock_status; |
| 3799 | operation->alg = psa_aead_get_base_algorithm( alg ); |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3800 | operation->is_encrypt = is_encrypt; |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3801 | } |
| 3802 | else |
| 3803 | psa_aead_abort( operation ); |
| 3804 | |
| 3805 | return( status ); |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3806 | } |
| 3807 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3808 | /* Set the key for a multipart authenticated encryption operation. */ |
| 3809 | psa_status_t psa_aead_encrypt_setup( psa_aead_operation_t *operation, |
| 3810 | mbedtls_svc_key_id_t key, |
| 3811 | psa_algorithm_t alg ) |
| 3812 | { |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3813 | return( psa_aead_setup( operation, 1, key, alg ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | /* Set the key for a multipart authenticated decryption operation. */ |
| 3817 | psa_status_t psa_aead_decrypt_setup( psa_aead_operation_t *operation, |
| 3818 | mbedtls_svc_key_id_t key, |
| 3819 | psa_algorithm_t alg ) |
| 3820 | { |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3821 | return( psa_aead_setup( operation, 0, key, alg ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | /* Generate a random nonce / IV for multipart AEAD operation */ |
| 3825 | psa_status_t psa_aead_generate_nonce( psa_aead_operation_t *operation, |
| 3826 | uint8_t *nonce, |
| 3827 | size_t nonce_size, |
| 3828 | size_t *nonce_length ) |
| 3829 | { |
| 3830 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3831 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; |
Paul Elliott | b91da71 | 2021-05-20 14:43:47 +0100 | [diff] [blame] | 3832 | size_t required_nonce_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3833 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3834 | *nonce_length = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3835 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3836 | if( operation->id == 0 ) |
| 3837 | { |
| 3838 | status = PSA_ERROR_BAD_STATE; |
| 3839 | goto exit; |
| 3840 | } |
| 3841 | |
Paul Elliott | daf5c89 | 2021-08-25 16:24:58 +0100 | [diff] [blame] | 3842 | if( operation->nonce_set || !operation->is_encrypt ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3843 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3844 | status = PSA_ERROR_BAD_STATE; |
| 3845 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3846 | } |
| 3847 | |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 3848 | /* For CCM, this size may not be correct according to the PSA |
| 3849 | * specification. The PSA Crypto 1.0.1 specification states: |
| 3850 | * |
| 3851 | * CCM encodes the plaintext length pLen in L octets, with L the smallest |
| 3852 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. |
| 3853 | * |
| 3854 | * However this restriction that L has to be the smallest integer is not |
| 3855 | * applied in practice, and it is not implementable here since the |
| 3856 | * plaintext length may or may not be known at this time. */ |
Paul Elliott | bbe90b5 | 2021-05-11 22:22:42 +0100 | [diff] [blame] | 3857 | required_nonce_size = PSA_AEAD_NONCE_LENGTH( operation->key_type, |
Paul Elliott | 3242f6c | 2021-08-25 16:33:47 +0100 | [diff] [blame] | 3858 | operation->alg ); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3859 | if( nonce_size < required_nonce_size ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3860 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3861 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3862 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3863 | } |
| 3864 | |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3865 | status = psa_generate_random( local_nonce, required_nonce_size ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3866 | if( status != PSA_SUCCESS ) |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3867 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3868 | |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3869 | status = psa_aead_set_nonce( operation, local_nonce, required_nonce_size ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3870 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3871 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3872 | if( status == PSA_SUCCESS ) |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3873 | { |
| 3874 | memcpy( nonce, local_nonce, required_nonce_size ); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3875 | *nonce_length = required_nonce_size; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3876 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3877 | else |
| 3878 | psa_aead_abort( operation ); |
| 3879 | |
| 3880 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | /* Set the nonce for a multipart authenticated encryption or decryption |
| 3884 | operation.*/ |
| 3885 | psa_status_t psa_aead_set_nonce( psa_aead_operation_t *operation, |
| 3886 | const uint8_t *nonce, |
| 3887 | size_t nonce_length ) |
| 3888 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3889 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3890 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3891 | if( operation->id == 0 ) |
| 3892 | { |
| 3893 | status = PSA_ERROR_BAD_STATE; |
| 3894 | goto exit; |
| 3895 | } |
| 3896 | |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 3897 | if( operation->nonce_set ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3898 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3899 | status = PSA_ERROR_BAD_STATE; |
| 3900 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3901 | } |
| 3902 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3903 | status = psa_aead_check_nonce_length( operation->alg, nonce_length ); |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3904 | if( status != PSA_SUCCESS ) |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 3905 | { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3906 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3907 | goto exit; |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 3908 | } |
Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 3909 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3910 | status = psa_driver_wrapper_aead_set_nonce( operation, nonce, |
| 3911 | nonce_length ); |
| 3912 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3913 | exit: |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3914 | if( status == PSA_SUCCESS ) |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3915 | operation->nonce_set = 1; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3916 | else |
| 3917 | psa_aead_abort( operation ); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3918 | |
| 3919 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | /* Declare the lengths of the message and additional data for multipart AEAD. */ |
| 3923 | psa_status_t psa_aead_set_lengths( psa_aead_operation_t *operation, |
| 3924 | size_t ad_length, |
| 3925 | size_t plaintext_length ) |
| 3926 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3927 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3928 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3929 | if( operation->id == 0 ) |
| 3930 | { |
| 3931 | status = PSA_ERROR_BAD_STATE; |
| 3932 | goto exit; |
| 3933 | } |
| 3934 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 3935 | if( operation->lengths_set || operation->ad_started || |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 3936 | operation->body_started ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3937 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3938 | status = PSA_ERROR_BAD_STATE; |
| 3939 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3940 | } |
| 3941 | |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3942 | switch(operation->alg) |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3943 | { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3944 | #if defined(PSA_WANT_ALG_GCM) |
| 3945 | case PSA_ALG_GCM: |
| 3946 | /* Lengths can only be too large for GCM if size_t is bigger than 32 |
| 3947 | * bits. Without the guard this code will generate warnings on 32bit |
| 3948 | * builds. */ |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3949 | #if SIZE_MAX > UINT32_MAX |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3950 | if( (( uint64_t ) ad_length ) >> 61 != 0 || |
| 3951 | (( uint64_t ) plaintext_length ) > 0xFFFFFFFE0ull ) |
| 3952 | { |
| 3953 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3954 | goto exit; |
| 3955 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3956 | #endif |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3957 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3958 | #endif /* PSA_WANT_ALG_GCM */ |
| 3959 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3960 | case PSA_ALG_CCM: |
| 3961 | if( ad_length > 0xFF00 ) |
| 3962 | { |
| 3963 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3964 | goto exit; |
| 3965 | } |
| 3966 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3967 | #endif /* PSA_WANT_ALG_CCM */ |
| 3968 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3969 | case PSA_ALG_CHACHA20_POLY1305: |
| 3970 | /* No length restrictions for ChaChaPoly. */ |
| 3971 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3972 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3973 | default: |
| 3974 | break; |
| 3975 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3976 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3977 | status = psa_driver_wrapper_aead_set_lengths( operation, ad_length, |
| 3978 | plaintext_length ); |
| 3979 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3980 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3981 | if( status == PSA_SUCCESS ) |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 3982 | { |
| 3983 | operation->ad_remaining = ad_length; |
| 3984 | operation->body_remaining = plaintext_length; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3985 | operation->lengths_set = 1; |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 3986 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3987 | else |
| 3988 | psa_aead_abort( operation ); |
| 3989 | |
| 3990 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3991 | } |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 3992 | |
| 3993 | /* Pass additional data to an active multipart AEAD operation. */ |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3994 | psa_status_t psa_aead_update_ad( psa_aead_operation_t *operation, |
| 3995 | const uint8_t *input, |
| 3996 | size_t input_length ) |
| 3997 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3998 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3999 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4000 | if( operation->id == 0 ) |
| 4001 | { |
| 4002 | status = PSA_ERROR_BAD_STATE; |
| 4003 | goto exit; |
| 4004 | } |
| 4005 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4006 | if( !operation->nonce_set || operation->body_started ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4007 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4008 | status = PSA_ERROR_BAD_STATE; |
| 4009 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4010 | } |
| 4011 | |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4012 | if( operation->lengths_set ) |
| 4013 | { |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4014 | if( operation->ad_remaining < input_length ) |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4015 | { |
| 4016 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4017 | goto exit; |
| 4018 | } |
| 4019 | |
| 4020 | operation->ad_remaining -= input_length; |
| 4021 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4022 | #if defined(PSA_WANT_ALG_CCM) |
| 4023 | else if( operation->alg == PSA_ALG_CCM ) |
| 4024 | { |
| 4025 | status = PSA_ERROR_BAD_STATE; |
| 4026 | goto exit; |
| 4027 | } |
| 4028 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4029 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4030 | status = psa_driver_wrapper_aead_update_ad( operation, input, |
| 4031 | input_length ); |
| 4032 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4033 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4034 | if( status == PSA_SUCCESS ) |
| 4035 | operation->ad_started = 1; |
| 4036 | else |
| 4037 | psa_aead_abort( operation ); |
| 4038 | |
| 4039 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4040 | } |
| 4041 | |
| 4042 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 4043 | operation.*/ |
| 4044 | psa_status_t psa_aead_update( psa_aead_operation_t *operation, |
| 4045 | const uint8_t *input, |
| 4046 | size_t input_length, |
| 4047 | uint8_t *output, |
| 4048 | size_t output_size, |
| 4049 | size_t *output_length ) |
| 4050 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4051 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4052 | |
| 4053 | *output_length = 0; |
| 4054 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4055 | if( operation->id == 0 ) |
| 4056 | { |
| 4057 | status = PSA_ERROR_BAD_STATE; |
| 4058 | goto exit; |
| 4059 | } |
| 4060 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4061 | if( !operation->nonce_set ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4062 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4063 | status = PSA_ERROR_BAD_STATE; |
| 4064 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4065 | } |
| 4066 | |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4067 | if( operation->lengths_set ) |
| 4068 | { |
| 4069 | /* Additional data length was supplied, but not all the additional |
| 4070 | data was supplied.*/ |
| 4071 | if( operation->ad_remaining != 0 ) |
| 4072 | { |
| 4073 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4074 | goto exit; |
| 4075 | } |
| 4076 | |
| 4077 | /* Too much data provided. */ |
| 4078 | if( operation->body_remaining < input_length ) |
| 4079 | { |
| 4080 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4081 | goto exit; |
| 4082 | } |
| 4083 | |
| 4084 | operation->body_remaining -= input_length; |
| 4085 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4086 | #if defined(PSA_WANT_ALG_CCM) |
| 4087 | else if( operation->alg == PSA_ALG_CCM ) |
| 4088 | { |
| 4089 | status = PSA_ERROR_BAD_STATE; |
| 4090 | goto exit; |
| 4091 | } |
| 4092 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4093 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4094 | status = psa_driver_wrapper_aead_update( operation, input, input_length, |
| 4095 | output, output_size, |
| 4096 | output_length ); |
| 4097 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4098 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4099 | if( status == PSA_SUCCESS ) |
| 4100 | operation->body_started = 1; |
| 4101 | else |
| 4102 | psa_aead_abort( operation ); |
| 4103 | |
| 4104 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4105 | } |
| 4106 | |
Paul Elliott | 69bf5fc | 2021-09-19 18:26:37 +0100 | [diff] [blame] | 4107 | 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] | 4108 | { |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4109 | if( operation->id == 0 || !operation->nonce_set ) |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4110 | return( PSA_ERROR_BAD_STATE ); |
| 4111 | |
Paul Elliott | a561444 | 2021-07-14 14:54:11 +0100 | [diff] [blame] | 4112 | if( operation->lengths_set && ( operation->ad_remaining != 0 || |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4113 | operation->body_remaining != 0 ) ) |
| 4114 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4115 | |
| 4116 | return( PSA_SUCCESS ); |
| 4117 | } |
| 4118 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4119 | /* Finish encrypting a message in a multipart AEAD operation. */ |
| 4120 | psa_status_t psa_aead_finish( psa_aead_operation_t *operation, |
| 4121 | uint8_t *ciphertext, |
| 4122 | size_t ciphertext_size, |
| 4123 | size_t *ciphertext_length, |
| 4124 | uint8_t *tag, |
| 4125 | size_t tag_size, |
| 4126 | size_t *tag_length ) |
| 4127 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4128 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4129 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4130 | *ciphertext_length = 0; |
Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 4131 | *tag_length = tag_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4132 | |
Paul Elliott | 814fffb | 2021-08-16 18:20:36 +0100 | [diff] [blame] | 4133 | status = psa_aead_final_checks( operation ); |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4134 | if( status != PSA_SUCCESS ) |
| 4135 | goto exit; |
| 4136 | |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4137 | if( !operation->is_encrypt ) |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4138 | { |
| 4139 | status = PSA_ERROR_BAD_STATE; |
| 4140 | goto exit; |
| 4141 | } |
| 4142 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4143 | status = psa_driver_wrapper_aead_finish( operation, ciphertext, |
| 4144 | ciphertext_size, |
| 4145 | ciphertext_length, |
| 4146 | tag, tag_size, tag_length ); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4147 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4148 | exit: |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4149 | /* 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] | 4150 | * the zero tag size, make sure the tag is set to something implausible. |
| 4151 | * Even if the operation succeeds, make sure we clear the rest of the |
| 4152 | * buffer to prevent potential leakage of anything previously placed in |
| 4153 | * the same buffer.*/ |
Paul Elliott | 90fdc11 | 2021-09-22 17:15:48 +0100 | [diff] [blame] | 4154 | if( tag != NULL ) |
Paul Elliott | ec95cc9 | 2021-09-19 22:33:09 +0100 | [diff] [blame] | 4155 | { |
| 4156 | if( status != PSA_SUCCESS ) |
| 4157 | memset( tag, '!', tag_size ); |
| 4158 | else if( *tag_length < tag_size ) |
| 4159 | memset( tag + *tag_length, '!', ( tag_size - *tag_length ) ); |
| 4160 | } |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4161 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4162 | psa_aead_abort( operation ); |
| 4163 | |
| 4164 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 4168 | operation.*/ |
| 4169 | psa_status_t psa_aead_verify( psa_aead_operation_t *operation, |
| 4170 | uint8_t *plaintext, |
| 4171 | size_t plaintext_size, |
| 4172 | size_t *plaintext_length, |
| 4173 | const uint8_t *tag, |
| 4174 | size_t tag_length ) |
| 4175 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4176 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4177 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4178 | *plaintext_length = 0; |
| 4179 | |
Paul Elliott | 814fffb | 2021-08-16 18:20:36 +0100 | [diff] [blame] | 4180 | status = psa_aead_final_checks( operation ); |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4181 | if( status != PSA_SUCCESS ) |
| 4182 | goto exit; |
| 4183 | |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4184 | if( operation->is_encrypt ) |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4185 | { |
| 4186 | status = PSA_ERROR_BAD_STATE; |
| 4187 | goto exit; |
| 4188 | } |
| 4189 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4190 | status = psa_driver_wrapper_aead_verify( operation, plaintext, |
| 4191 | plaintext_size, |
| 4192 | plaintext_length, |
| 4193 | tag, tag_length ); |
| 4194 | |
| 4195 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4196 | psa_aead_abort( operation ); |
| 4197 | |
| 4198 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | /* Abort an AEAD operation. */ |
Paul Elliott | bbe90b5 | 2021-05-11 22:22:42 +0100 | [diff] [blame] | 4202 | psa_status_t psa_aead_abort( psa_aead_operation_t *operation ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4203 | { |
| 4204 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4205 | |
| 4206 | if( operation->id == 0 ) |
| 4207 | { |
| 4208 | /* The object has (apparently) been initialized but it is not (yet) |
| 4209 | * in use. It's ok to call abort on such an object, and there's |
| 4210 | * nothing to do. */ |
| 4211 | return( PSA_SUCCESS ); |
| 4212 | } |
| 4213 | |
| 4214 | status = psa_driver_wrapper_aead_abort( operation ); |
| 4215 | |
Paul Elliott | 70618b2 | 2021-09-22 17:12:16 +0100 | [diff] [blame] | 4216 | memset( operation, 0, sizeof( *operation ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4217 | |
| 4218 | return( status ); |
| 4219 | } |
| 4220 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4221 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4222 | /* Generators */ |
| 4223 | /****************************************************************/ |
| 4224 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4225 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4226 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4227 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \ |
| 4228 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4229 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4230 | #endif /* At least one builtin KDF */ |
| 4231 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4232 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4233 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4234 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4235 | static psa_status_t psa_key_derivation_start_hmac( |
| 4236 | psa_mac_operation_t *operation, |
| 4237 | psa_algorithm_t hash_alg, |
| 4238 | const uint8_t *hmac_key, |
| 4239 | size_t hmac_key_length ) |
| 4240 | { |
| 4241 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4242 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4243 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 4244 | psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) ); |
| 4245 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 4246 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4247 | operation->is_sign = 1; |
| 4248 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
| 4249 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4250 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 4251 | &attributes, |
| 4252 | hmac_key, hmac_key_length, |
| 4253 | PSA_ALG_HMAC( hash_alg ) ); |
| 4254 | |
| 4255 | psa_reset_key_attributes( &attributes ); |
| 4256 | return( status ); |
| 4257 | } |
| 4258 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4259 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4260 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4261 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4262 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4263 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4264 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4265 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4266 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4267 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4268 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4269 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4270 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4271 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4272 | } |
| 4273 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4274 | 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] | 4275 | { |
| 4276 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4277 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4278 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4279 | { |
| 4280 | /* The object has (apparently) been initialized but it is not |
| 4281 | * in use. It's ok to call abort on such an object, and there's |
| 4282 | * nothing to do. */ |
| 4283 | } |
| 4284 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4285 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 4286 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4287 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4288 | mbedtls_free( operation->ctx.hkdf.info ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4289 | status = psa_mac_abort( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4290 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4291 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4292 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4293 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4294 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4295 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4296 | /* 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] | 4297 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4298 | { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4299 | if( operation->ctx.tls12_prf.secret != NULL ) |
| 4300 | { |
| 4301 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret, |
| 4302 | operation->ctx.tls12_prf.secret_length ); |
| 4303 | mbedtls_free( operation->ctx.tls12_prf.secret ); |
| 4304 | } |
| 4305 | |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4306 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4307 | { |
| 4308 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4309 | operation->ctx.tls12_prf.seed_length ); |
| 4310 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4311 | } |
| 4312 | |
| 4313 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4314 | { |
| 4315 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4316 | operation->ctx.tls12_prf.label_length ); |
| 4317 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4318 | } |
| 4319 | |
Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 4320 | if( operation->ctx.tls12_prf.other_secret != NULL ) |
| 4321 | { |
| 4322 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.other_secret, |
| 4323 | operation->ctx.tls12_prf.other_secret_length ); |
| 4324 | mbedtls_free( operation->ctx.tls12_prf.other_secret ); |
| 4325 | } |
| 4326 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4327 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4328 | |
| 4329 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4330 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4331 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4332 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4333 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4334 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4335 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 4336 | if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4337 | { |
| 4338 | mbedtls_platform_zeroize( operation->ctx.tls12_ecjpake_to_pms.data, |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 4339 | sizeof( operation->ctx.tls12_ecjpake_to_pms.data ) ); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4340 | } |
| 4341 | else |
| 4342 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4343 | { |
| 4344 | status = PSA_ERROR_BAD_STATE; |
| 4345 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4346 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4347 | return( status ); |
| 4348 | } |
| 4349 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4350 | 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] | 4351 | size_t *capacity) |
| 4352 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4353 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4354 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4355 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4356 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4359 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4360 | return( PSA_SUCCESS ); |
| 4361 | } |
| 4362 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4363 | 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] | 4364 | size_t capacity ) |
| 4365 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4366 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4367 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4368 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4369 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4370 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4371 | return( PSA_SUCCESS ); |
| 4372 | } |
| 4373 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4374 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4375 | /* Read some bytes from an HKDF-based operation. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4376 | 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] | 4377 | psa_algorithm_t kdf_alg, |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4378 | uint8_t *output, |
| 4379 | size_t output_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4380 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4381 | 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] | 4382 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4383 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4384 | psa_status_t status; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4385 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4386 | 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] | 4387 | #else |
| 4388 | const uint8_t last_block = 0xff; |
| 4389 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4390 | |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4391 | if( hkdf->state < HKDF_STATE_KEYED || |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4392 | ( !hkdf->info_set |
| 4393 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 4394 | && !PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) |
| 4395 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
| 4396 | ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4397 | return( PSA_ERROR_BAD_STATE ); |
| 4398 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4399 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4400 | while( output_length != 0 ) |
| 4401 | { |
| 4402 | /* Copy what remains of the current block */ |
| 4403 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4404 | if( n > output_length ) |
| 4405 | n = (uint8_t) output_length; |
| 4406 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4407 | output += n; |
| 4408 | output_length -= n; |
| 4409 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4410 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4411 | break; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4412 | /* We can't be wanting more output after the last block, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4413 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4414 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4415 | * object was corrupted or if this function is called directly |
| 4416 | * inside the library. */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4417 | if( hkdf->block_number == last_block ) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4418 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4419 | |
| 4420 | /* We need a new block */ |
| 4421 | ++hkdf->block_number; |
| 4422 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4423 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4424 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 4425 | hash_alg, |
| 4426 | hkdf->prk, |
| 4427 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4428 | if( status != PSA_SUCCESS ) |
| 4429 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4430 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4431 | if( hkdf->block_number != 1 ) |
| 4432 | { |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4433 | status = psa_mac_update( &hkdf->hmac, |
| 4434 | hkdf->output_block, |
| 4435 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4436 | if( status != PSA_SUCCESS ) |
| 4437 | return( status ); |
| 4438 | } |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4439 | status = psa_mac_update( &hkdf->hmac, |
| 4440 | hkdf->info, |
| 4441 | hkdf->info_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4442 | if( status != PSA_SUCCESS ) |
| 4443 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4444 | status = psa_mac_update( &hkdf->hmac, |
| 4445 | &hkdf->block_number, 1 ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4446 | if( status != PSA_SUCCESS ) |
| 4447 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4448 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 4449 | hkdf->output_block, |
| 4450 | sizeof( hkdf->output_block ), |
| 4451 | &hmac_output_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4452 | if( status != PSA_SUCCESS ) |
| 4453 | return( status ); |
| 4454 | } |
| 4455 | |
| 4456 | return( PSA_SUCCESS ); |
| 4457 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4458 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4459 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4460 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4461 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4462 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4463 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4464 | psa_algorithm_t alg ) |
| 4465 | { |
| 4466 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4467 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4468 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 4469 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4470 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4471 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4472 | /* We can't be wanting more output after block 0xff, otherwise |
| 4473 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4474 | * prevented this call. It could happen only if the operation |
| 4475 | * object was corrupted or if this function is called directly |
| 4476 | * inside the library. */ |
| 4477 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4478 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4479 | |
| 4480 | /* We need a new block */ |
| 4481 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4482 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4483 | |
| 4484 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4485 | * |
| 4486 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4487 | * |
| 4488 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4489 | * HMAC_hash(secret, A(2) + seed) + |
| 4490 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4491 | * |
| 4492 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4493 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4494 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4495 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4496 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4497 | * is currently extracted as `output_block` and where i is |
| 4498 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4499 | */ |
| 4500 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4501 | status = psa_key_derivation_start_hmac( &hmac, |
| 4502 | hash_alg, |
| 4503 | tls12_prf->secret, |
| 4504 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4505 | if( status != PSA_SUCCESS ) |
| 4506 | goto cleanup; |
| 4507 | |
| 4508 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4509 | if( tls12_prf->block_number == 1 ) |
| 4510 | { |
| 4511 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4512 | * the variable seed and in this instance means it in the context of the |
| 4513 | * P_hash function, where seed = label + seed.) */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4514 | status = psa_mac_update( &hmac, |
| 4515 | tls12_prf->label, |
| 4516 | tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4517 | if( status != PSA_SUCCESS ) |
| 4518 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4519 | status = psa_mac_update( &hmac, |
| 4520 | tls12_prf->seed, |
| 4521 | tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4522 | if( status != PSA_SUCCESS ) |
| 4523 | goto cleanup; |
| 4524 | } |
| 4525 | else |
| 4526 | { |
| 4527 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4528 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4529 | if( status != PSA_SUCCESS ) |
| 4530 | goto cleanup; |
| 4531 | } |
| 4532 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4533 | status = psa_mac_sign_finish( &hmac, |
| 4534 | tls12_prf->Ai, hash_length, |
| 4535 | &hmac_output_length ); |
| 4536 | if( hmac_output_length != hash_length ) |
| 4537 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4538 | if( status != PSA_SUCCESS ) |
| 4539 | goto cleanup; |
| 4540 | |
| 4541 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4542 | status = psa_key_derivation_start_hmac( &hmac, |
| 4543 | hash_alg, |
| 4544 | tls12_prf->secret, |
| 4545 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4546 | if( status != PSA_SUCCESS ) |
| 4547 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4548 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4549 | if( status != PSA_SUCCESS ) |
| 4550 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4551 | status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4552 | if( status != PSA_SUCCESS ) |
| 4553 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4554 | status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4555 | if( status != PSA_SUCCESS ) |
| 4556 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4557 | status = psa_mac_sign_finish( &hmac, |
| 4558 | tls12_prf->output_block, hash_length, |
| 4559 | &hmac_output_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4560 | if( status != PSA_SUCCESS ) |
| 4561 | goto cleanup; |
| 4562 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4563 | |
| 4564 | cleanup: |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4565 | cleanup_status = psa_mac_abort( &hmac ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4566 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4567 | status = cleanup_status; |
| 4568 | |
| 4569 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4570 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4571 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4572 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4573 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4574 | psa_algorithm_t alg, |
| 4575 | uint8_t *output, |
| 4576 | size_t output_length ) |
| 4577 | { |
| 4578 | 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] | 4579 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4580 | psa_status_t status; |
| 4581 | uint8_t offset, length; |
| 4582 | |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4583 | switch( tls12_prf->state ) |
| 4584 | { |
| 4585 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4586 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4587 | break; |
| 4588 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4589 | break; |
| 4590 | default: |
| 4591 | return( PSA_ERROR_BAD_STATE ); |
| 4592 | } |
| 4593 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4594 | while( output_length != 0 ) |
| 4595 | { |
| 4596 | /* Check if we have fully processed the current block. */ |
| 4597 | if( tls12_prf->left_in_block == 0 ) |
| 4598 | { |
| 4599 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4600 | alg ); |
| 4601 | if( status != PSA_SUCCESS ) |
| 4602 | return( status ); |
| 4603 | |
| 4604 | continue; |
| 4605 | } |
| 4606 | |
| 4607 | if( tls12_prf->left_in_block > output_length ) |
| 4608 | length = (uint8_t) output_length; |
| 4609 | else |
| 4610 | length = tls12_prf->left_in_block; |
| 4611 | |
| 4612 | offset = hash_length - tls12_prf->left_in_block; |
| 4613 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4614 | output += length; |
| 4615 | output_length -= length; |
| 4616 | tls12_prf->left_in_block -= length; |
| 4617 | } |
| 4618 | |
| 4619 | return( PSA_SUCCESS ); |
| 4620 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4621 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4622 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4623 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4624 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 4625 | static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 4626 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
| 4627 | uint8_t *output, |
| 4628 | size_t output_length ) |
| 4629 | { |
| 4630 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 4631 | size_t output_size = 0; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4632 | |
| 4633 | if( output_length != 32 ) |
| 4634 | return ( PSA_ERROR_INVALID_ARGUMENT ); |
| 4635 | |
| 4636 | status = psa_hash_compute( PSA_ALG_SHA_256, ecjpake->data, |
| 4637 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length, |
| 4638 | &output_size ); |
| 4639 | if( status != PSA_SUCCESS ) |
| 4640 | return ( status ); |
| 4641 | |
| 4642 | if( output_size != output_length ) |
| 4643 | return ( PSA_ERROR_GENERIC_ERROR ); |
| 4644 | |
| 4645 | return ( PSA_SUCCESS ); |
| 4646 | } |
| 4647 | #endif |
| 4648 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4649 | psa_status_t psa_key_derivation_output_bytes( |
| 4650 | psa_key_derivation_operation_t *operation, |
| 4651 | uint8_t *output, |
| 4652 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4653 | { |
| 4654 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4655 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4656 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4657 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4658 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4659 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4660 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4663 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4664 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4665 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4666 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4667 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4668 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4669 | goto exit; |
| 4670 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4671 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4672 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4673 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4674 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4675 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4676 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4677 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4678 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4679 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4680 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4681 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4682 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4683 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 4684 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4685 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4686 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, kdf_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4687 | output, output_length ); |
| 4688 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4689 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4690 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4691 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4692 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4693 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4694 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4695 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4696 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4697 | kdf_alg, output, |
| 4698 | output_length ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4699 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4700 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4701 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4702 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4703 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 4704 | if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4705 | { |
| 4706 | status = psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 4707 | &operation->ctx.tls12_ecjpake_to_pms, output, output_length ); |
| 4708 | } |
| 4709 | else |
| 4710 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
| 4711 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4712 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4713 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4714 | return( PSA_ERROR_BAD_STATE ); |
| 4715 | } |
| 4716 | |
| 4717 | exit: |
| 4718 | if( status != PSA_SUCCESS ) |
| 4719 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4720 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4721 | * This allows us to differentiate between exhausted operations and |
| 4722 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4723 | * operations. */ |
| 4724 | psa_algorithm_t alg = operation->alg; |
| 4725 | psa_key_derivation_abort( operation ); |
| 4726 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4727 | memset( output, '!', output_length ); |
| 4728 | } |
| 4729 | return( status ); |
| 4730 | } |
| 4731 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4732 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4733 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4734 | { |
| 4735 | if( data_size >= 8 ) |
| 4736 | mbedtls_des_key_set_parity( data ); |
| 4737 | if( data_size >= 16 ) |
| 4738 | mbedtls_des_key_set_parity( data + 8 ); |
| 4739 | if( data_size >= 24 ) |
| 4740 | mbedtls_des_key_set_parity( data + 16 ); |
| 4741 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4742 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4743 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4744 | /* |
Przemyslaw Stekiel | 705fb0f | 2021-11-24 08:47:29 +0100 | [diff] [blame] | 4745 | * ECC keys on a Weierstrass elliptic curve require the generation |
| 4746 | * of a private key which is an integer |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4747 | * in the range [1, N - 1], where N is the boundary of the private key domain: |
| 4748 | * N is the prime p for Diffie-Hellman, or the order of the |
| 4749 | * curve’s base point for ECC. |
| 4750 | * |
| 4751 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). |
| 4752 | * This function generates the private key using the following process: |
| 4753 | * |
| 4754 | * 1. Draw a byte string of length ceiling(m/8) bytes. |
| 4755 | * 2. If m is not a multiple of 8, set the most significant |
| 4756 | * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. |
| 4757 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. |
| 4758 | * 4. If k > N - 2, discard the result and return to step 1. |
| 4759 | * 5. Output k + 1 as the private key. |
| 4760 | * |
| 4761 | * This method allows compliance to NIST standards, specifically the methods titled |
| 4762 | * Key-Pair Generation by Testing Candidates in the following publications: |
| 4763 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment |
| 4764 | * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for |
| 4765 | * Diffie-Hellman keys. |
| 4766 | * |
| 4767 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature |
| 4768 | * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4769 | * |
| 4770 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 4771 | * always NULL. |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4772 | */ |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4773 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 4774 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 4775 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4776 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 4777 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4778 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4779 | psa_key_slot_t *slot, |
| 4780 | size_t bits, |
| 4781 | psa_key_derivation_operation_t *operation, |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4782 | uint8_t **data |
| 4783 | ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4784 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4785 | unsigned key_out_of_range = 1; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4786 | mbedtls_mpi k; |
| 4787 | mbedtls_mpi diff_N_2; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4788 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4789 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4790 | |
| 4791 | mbedtls_mpi_init( &k ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4792 | mbedtls_mpi_init( &diff_N_2 ); |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4793 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4794 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 4795 | slot->attr.type ); |
| 4796 | mbedtls_ecp_group_id grp_id = |
| 4797 | mbedtls_ecc_group_of_psa( curve, bits, 0 ); |
| 4798 | |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 4799 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 4800 | { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4801 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 4802 | goto cleanup; |
| 4803 | } |
| 4804 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4805 | mbedtls_ecp_group ecp_group; |
Przemyslaw Stekiel | 6534816 | 2021-11-18 11:57:07 +0100 | [diff] [blame] | 4806 | mbedtls_ecp_group_init( &ecp_group ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4807 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4808 | MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ecp_group, grp_id ) ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4809 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4810 | /* N is the boundary of the private key domain (ecp_group.N). */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4811 | /* Let m be the bit size of N. */ |
| 4812 | size_t m = ecp_group.nbits; |
| 4813 | |
| 4814 | size_t m_bytes = PSA_BITS_TO_BYTES( m ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4815 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4816 | /* Calculate N - 2 - it will be needed later. */ |
| 4817 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &diff_N_2, &ecp_group.N, 2 ) ); |
| 4818 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4819 | /* Note: This function is always called with *data == NULL and it |
| 4820 | * allocates memory for the data buffer. */ |
| 4821 | *data = mbedtls_calloc( 1, m_bytes ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4822 | if( *data == NULL ) |
| 4823 | { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4824 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4825 | goto cleanup; |
| 4826 | } |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4827 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4828 | while( key_out_of_range ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4829 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4830 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4831 | if( ( status = psa_key_derivation_output_bytes( operation, *data, m_bytes ) ) != 0 ) |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4832 | goto cleanup; |
| 4833 | |
| 4834 | /* 2. If m is not a multiple of 8 */ |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4835 | if( m % 8 != 0 ) |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4836 | { |
| 4837 | /* Set the most significant |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4838 | * (8 * ceiling(m/8) - m) bits of the first byte in |
| 4839 | * the string to zero. |
| 4840 | */ |
| 4841 | uint8_t clear_bit_mask = ( 1 << ( m % 8 ) ) - 1; |
| 4842 | (*data)[0] &= clear_bit_mask; |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | /* 3. Convert the string to integer k by decoding it as a |
| 4846 | * big-endian byte string. |
| 4847 | */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4848 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &k, *data, m_bytes ) ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4849 | |
| 4850 | /* 4. If k > N - 2, discard the result and return to step 1. |
| 4851 | * Result of comparison is returned. When it indicates error |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 4852 | * then this function is called again. |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4853 | */ |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4854 | 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] | 4855 | } |
| 4856 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4857 | /* 5. Output k + 1 as the private key. */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4858 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &k, &k, 1 ) ); |
| 4859 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &k, *data, m_bytes ) ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4860 | cleanup: |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4861 | if( ret != 0 ) |
Przemyslaw Stekiel | dc8d7d9 | 2021-12-02 09:15:20 +0100 | [diff] [blame] | 4862 | status = mbedtls_to_psa_error( ret ); |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4863 | if( status != PSA_SUCCESS ) { |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4864 | mbedtls_free( *data ); |
| 4865 | *data = NULL; |
| 4866 | } |
| 4867 | mbedtls_mpi_free( &k ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4868 | mbedtls_mpi_free( &diff_N_2 ); |
Przemyslaw Stekiel | dc8d7d9 | 2021-12-02 09:15:20 +0100 | [diff] [blame] | 4869 | return( status ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4870 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4871 | |
| 4872 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length |
| 4873 | * is determined by the curve, and sets the mandatory bits accordingly. That is: |
| 4874 | * |
| 4875 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): |
| 4876 | * draw a 32-byte string and process it as specified in |
| 4877 | * Elliptic Curves for Security [RFC7748] §5. |
| 4878 | * |
| 4879 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): |
| 4880 | * draw a 56-byte string and process it as specified in [RFC7748] §5. |
| 4881 | * |
| 4882 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 4883 | * always NULL. |
| 4884 | */ |
| 4885 | |
| 4886 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 4887 | size_t bits, |
| 4888 | psa_key_derivation_operation_t *operation, |
| 4889 | uint8_t **data |
| 4890 | ) |
| 4891 | { |
| 4892 | size_t output_length; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4893 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4894 | |
| 4895 | switch( bits ) |
| 4896 | { |
| 4897 | case 255: |
| 4898 | output_length = 32; |
| 4899 | break; |
| 4900 | case 448: |
| 4901 | output_length = 56; |
| 4902 | break; |
| 4903 | default: |
| 4904 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4905 | break; |
| 4906 | } |
| 4907 | |
| 4908 | *data = mbedtls_calloc( 1, output_length ); |
| 4909 | |
| 4910 | if( *data == NULL ) |
| 4911 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4912 | |
| 4913 | status = psa_key_derivation_output_bytes( operation, *data, output_length ); |
| 4914 | |
| 4915 | if( status != PSA_SUCCESS ) |
| 4916 | return status; |
| 4917 | |
| 4918 | switch( bits ) |
| 4919 | { |
| 4920 | case 255: |
| 4921 | (*data)[0] &= 248; |
| 4922 | (*data)[31] &= 127; |
| 4923 | (*data)[31] |= 64; |
| 4924 | break; |
| 4925 | case 448: |
| 4926 | (*data)[0] &= 252; |
| 4927 | (*data)[55] |= 128; |
| 4928 | break; |
| 4929 | default: |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4930 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4931 | break; |
| 4932 | } |
| 4933 | |
| 4934 | return status; |
| 4935 | } |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4936 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 4937 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 4938 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4939 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 4940 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4941 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4942 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4943 | psa_key_slot_t *slot, |
| 4944 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4945 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4946 | { |
| 4947 | uint8_t *data = NULL; |
| 4948 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4949 | size_t storage_size = bytes; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4950 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4951 | |
Przemek Stekiel | dcab6cc | 2022-03-01 14:22:29 +0100 | [diff] [blame] | 4952 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4953 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4954 | |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4955 | #if 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) |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4960 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4961 | { |
Przemyslaw Stekiel | 9248159 | 2022-01-04 10:09:46 +0100 | [diff] [blame] | 4962 | 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] | 4963 | if( PSA_ECC_FAMILY_IS_WEIERSTRASS( curve ) ) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4964 | { |
| 4965 | /* Weierstrass elliptic curve */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4966 | status = psa_generate_derived_ecc_key_weierstrass_helper( slot, bits, operation, &data ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4967 | if( status != PSA_SUCCESS ) |
| 4968 | goto exit; |
Przemyslaw Stekiel | 50fcc53 | 2021-11-26 10:54:52 +0100 | [diff] [blame] | 4969 | } |
| 4970 | else |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4971 | { |
| 4972 | /* Montgomery elliptic curve */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4973 | status = psa_generate_derived_ecc_key_montgomery_helper( bits, operation, &data ); |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4974 | if( status != PSA_SUCCESS ) |
| 4975 | goto exit; |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4976 | } |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4977 | } else |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4978 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 4979 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 4980 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4981 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 4982 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4983 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4984 | { |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4985 | if( bits % 8 != 0 ) |
| 4986 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4987 | data = mbedtls_calloc( 1, bytes ); |
| 4988 | if( data == NULL ) |
| 4989 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4990 | |
| 4991 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
| 4992 | if( status != PSA_SUCCESS ) |
| 4993 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4994 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4995 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
| 4996 | psa_des_set_key_parity( data, bytes ); |
Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 4997 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4998 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4999 | else |
Przemek Stekiel | dcab6cc | 2022-03-01 14:22:29 +0100 | [diff] [blame] | 5000 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5001 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5002 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5003 | psa_key_attributes_t attributes = { |
| 5004 | .core = slot->attr |
| 5005 | }; |
| 5006 | |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5007 | if( psa_key_lifetime_is_external( attributes.core.lifetime ) ) |
| 5008 | { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 5009 | status = psa_driver_wrapper_get_key_buffer_size( &attributes, |
| 5010 | &storage_size ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5011 | if( status != PSA_SUCCESS ) |
| 5012 | goto exit; |
| 5013 | } |
| 5014 | status = psa_allocate_buffer_to_slot( slot, storage_size ); |
| 5015 | if( status != PSA_SUCCESS ) |
| 5016 | goto exit; |
| 5017 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5018 | status = psa_driver_wrapper_import_key( &attributes, |
| 5019 | data, bytes, |
| 5020 | slot->key.data, |
| 5021 | slot->key.bytes, |
| 5022 | &slot->key.bytes, &bits ); |
| 5023 | if( bits != slot->attr.bits ) |
| 5024 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5025 | |
| 5026 | exit: |
| 5027 | mbedtls_free( data ); |
| 5028 | return( status ); |
| 5029 | } |
| 5030 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5031 | 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] | 5032 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5033 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5034 | { |
| 5035 | psa_status_t status; |
| 5036 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5037 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5038 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5039 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5040 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5041 | /* Reject any attempt to create a zero-length key so that we don't |
| 5042 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5043 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5044 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5045 | |
Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 5046 | if( operation->alg == PSA_ALG_NONE ) |
| 5047 | return( PSA_ERROR_BAD_STATE ); |
| 5048 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5049 | if( ! operation->can_output_key ) |
| 5050 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5051 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5052 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5053 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5054 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5055 | if( driver != NULL ) |
| 5056 | { |
| 5057 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5058 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5059 | } |
| 5060 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5061 | if( status == PSA_SUCCESS ) |
| 5062 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5063 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5064 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5065 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5066 | } |
| 5067 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5068 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5069 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5070 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5071 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5072 | return( status ); |
| 5073 | } |
| 5074 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5075 | |
| 5076 | |
| 5077 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5078 | /* Key derivation */ |
| 5079 | /****************************************************************/ |
| 5080 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5081 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5082 | static int is_kdf_alg_supported( psa_algorithm_t kdf_alg ) |
| 5083 | { |
| 5084 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 5085 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5086 | return( 1 ); |
| 5087 | #endif |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5088 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 5089 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5090 | return( 1 ); |
| 5091 | #endif |
| 5092 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
| 5093 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5094 | return( 1 ); |
| 5095 | #endif |
| 5096 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5097 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5098 | return( 1 ); |
| 5099 | #endif |
| 5100 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5101 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5102 | return( 1 ); |
| 5103 | #endif |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5104 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 5105 | if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5106 | return( 1 ); |
| 5107 | #endif |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5108 | return( 0 ); |
| 5109 | } |
| 5110 | |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5111 | static psa_status_t psa_hash_try_support( psa_algorithm_t alg ) |
| 5112 | { |
| 5113 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 5114 | psa_status_t status = psa_hash_setup( &operation, alg ); |
| 5115 | psa_hash_abort( &operation ); |
| 5116 | return( status ); |
| 5117 | } |
| 5118 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5119 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5120 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5121 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5122 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5123 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5124 | * initialisers for this union leave some bytes unspecified.) */ |
| 5125 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5126 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5127 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5128 | if( ! is_kdf_alg_supported( kdf_alg ) ) |
| 5129 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5130 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5131 | /* All currently supported key derivation algorithms (apart from |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5132 | * ecjpake to pms) are based on a hash algorithm. */ |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5133 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
| 5134 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 5135 | if( kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5136 | { |
| 5137 | if( hash_size == 0 ) |
| 5138 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5139 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5140 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 5141 | * we might fail later, which is somewhat unfriendly and potentially |
| 5142 | * risk-prone. */ |
| 5143 | psa_status_t status = psa_hash_try_support( hash_alg ); |
| 5144 | if( status != PSA_SUCCESS ) |
| 5145 | return( status ); |
| 5146 | } |
| 5147 | else |
| 5148 | { |
| 5149 | hash_size = PSA_HASH_LENGTH( PSA_ALG_SHA_256 ); |
| 5150 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5151 | |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5152 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5153 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
| 5154 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5155 | { |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5156 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5157 | } |
Andrzej Kurek | 7763829 | 2022-09-16 12:24:52 -0400 | [diff] [blame] | 5158 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5159 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Andrzej Kurek | b093650 | 2022-09-16 07:13:00 -0400 | [diff] [blame] | 5160 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) || |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 5161 | ( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) ) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5162 | operation->capacity = hash_size; |
| 5163 | else |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5164 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT || |
| 5165 | MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5166 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5167 | return( PSA_SUCCESS ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5168 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5169 | |
| 5170 | static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg ) |
| 5171 | { |
| 5172 | #if defined(PSA_WANT_ALG_ECDH) |
| 5173 | if( alg == PSA_ALG_ECDH ) |
| 5174 | return( PSA_SUCCESS ); |
| 5175 | #endif |
| 5176 | (void) alg; |
| 5177 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5178 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5179 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5180 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5181 | 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] | 5182 | psa_algorithm_t alg ) |
| 5183 | { |
| 5184 | psa_status_t status; |
| 5185 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5186 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5187 | return( PSA_ERROR_BAD_STATE ); |
| 5188 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5189 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5190 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5191 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5192 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5193 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5194 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5195 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ); |
| 5196 | status = psa_key_agreement_try_support( ka_alg ); |
| 5197 | if( status != PSA_SUCCESS ) |
| 5198 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5199 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5200 | #else |
| 5201 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5202 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5203 | } |
| 5204 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5205 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5206 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5207 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5208 | #else |
| 5209 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5210 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5211 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5212 | else |
| 5213 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5214 | |
| 5215 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5216 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5217 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5218 | } |
| 5219 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5220 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5221 | 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] | 5222 | psa_algorithm_t kdf_alg, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5223 | psa_key_derivation_step_t step, |
| 5224 | const uint8_t *data, |
| 5225 | size_t data_length ) |
| 5226 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5227 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5228 | psa_status_t status; |
| 5229 | switch( step ) |
| 5230 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5231 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5232 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5233 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
| 5234 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5235 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5236 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5237 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5238 | else |
| 5239 | { |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5240 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 5241 | hash_alg, |
| 5242 | data, data_length ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5243 | if( status != PSA_SUCCESS ) |
| 5244 | return( status ); |
| 5245 | hkdf->state = HKDF_STATE_STARTED; |
| 5246 | return( PSA_SUCCESS ); |
| 5247 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5248 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
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 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5251 | { |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 5252 | /* We shouldn't be in different state as HKDF_EXPAND only allows |
| 5253 | * two inputs: SECRET (this case) and INFO which does not modify |
| 5254 | * the state. It could happen only if the hkdf |
| 5255 | * object was corrupted. */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5256 | if( hkdf->state != HKDF_STATE_INIT ) |
| 5257 | return( PSA_ERROR_BAD_STATE ); |
| 5258 | |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 5259 | /* Allow only input that fits expected prk size */ |
| 5260 | if( data_length != PSA_HASH_LENGTH( hash_alg ) ) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5261 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5262 | |
| 5263 | memcpy( hkdf->prk, data, data_length ); |
| 5264 | } |
| 5265 | else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5266 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5267 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5268 | /* HKDF: If no salt was provided, use an empty salt. |
| 5269 | * HKDF-EXTRACT: salt is mandatory. */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5270 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5271 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5272 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 5273 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5274 | return( PSA_ERROR_BAD_STATE ); |
| 5275 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5276 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 5277 | hash_alg, |
| 5278 | NULL, 0 ); |
| 5279 | if( status != PSA_SUCCESS ) |
| 5280 | return( status ); |
| 5281 | hkdf->state = HKDF_STATE_STARTED; |
| 5282 | } |
| 5283 | if( hkdf->state != HKDF_STATE_STARTED ) |
| 5284 | return( PSA_ERROR_BAD_STATE ); |
| 5285 | status = psa_mac_update( &hkdf->hmac, |
| 5286 | data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5287 | if( status != PSA_SUCCESS ) |
| 5288 | return( status ); |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5289 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 5290 | hkdf->prk, |
| 5291 | sizeof( hkdf->prk ), |
| 5292 | &data_length ); |
| 5293 | if( status != PSA_SUCCESS ) |
| 5294 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5295 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5296 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5297 | hkdf->state = HKDF_STATE_KEYED; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5298 | hkdf->block_number = 0; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5299 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5300 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5301 | { |
| 5302 | /* The only block of output is the PRK. */ |
| 5303 | memcpy( hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH( hash_alg ) ); |
| 5304 | hkdf->offset_in_block = 0; |
| 5305 | } |
| 5306 | else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5307 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5308 | { |
| 5309 | /* Block 0 is empty, and the next block will be |
| 5310 | * generated by psa_key_derivation_hkdf_read(). */ |
| 5311 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
| 5312 | } |
| 5313 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5314 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5315 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5316 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5317 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5318 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5319 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 5320 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
| 5321 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) && |
| 5322 | hkdf->state == HKDF_STATE_INIT ) |
| 5323 | return( PSA_ERROR_BAD_STATE ); |
| 5324 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5325 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5326 | return( PSA_ERROR_BAD_STATE ); |
| 5327 | if( hkdf->info_set ) |
| 5328 | return( PSA_ERROR_BAD_STATE ); |
| 5329 | hkdf->info_length = data_length; |
| 5330 | if( data_length != 0 ) |
| 5331 | { |
| 5332 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5333 | if( hkdf->info == NULL ) |
| 5334 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5335 | memcpy( hkdf->info, data, data_length ); |
| 5336 | } |
| 5337 | hkdf->info_set = 1; |
| 5338 | return( PSA_SUCCESS ); |
| 5339 | default: |
| 5340 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5341 | } |
| 5342 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5343 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5344 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5345 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5346 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5347 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5348 | const uint8_t *data, |
| 5349 | size_t data_length ) |
| 5350 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5351 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5352 | return( PSA_ERROR_BAD_STATE ); |
| 5353 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5354 | if( data_length != 0 ) |
| 5355 | { |
| 5356 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5357 | if( prf->seed == NULL ) |
| 5358 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5359 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5360 | memcpy( prf->seed, data, data_length ); |
| 5361 | prf->seed_length = data_length; |
| 5362 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5363 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5364 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5365 | |
| 5366 | return( PSA_SUCCESS ); |
| 5367 | } |
| 5368 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5369 | 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] | 5370 | const uint8_t *data, |
| 5371 | size_t data_length ) |
| 5372 | { |
Przemek Stekiel | d7a2864 | 2022-04-07 14:58:33 +0200 | [diff] [blame] | 5373 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET && |
| 5374 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5375 | return( PSA_ERROR_BAD_STATE ); |
| 5376 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5377 | if( data_length != 0 ) |
| 5378 | { |
| 5379 | prf->secret = mbedtls_calloc( 1, data_length ); |
| 5380 | if( prf->secret == NULL ) |
| 5381 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5382 | |
| 5383 | memcpy( prf->secret, data, data_length ); |
| 5384 | prf->secret_length = data_length; |
| 5385 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5386 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5387 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5388 | |
| 5389 | return( PSA_SUCCESS ); |
| 5390 | } |
| 5391 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5392 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5393 | const uint8_t *data, |
| 5394 | size_t data_length ) |
| 5395 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5396 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5397 | return( PSA_ERROR_BAD_STATE ); |
| 5398 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5399 | if( data_length != 0 ) |
| 5400 | { |
| 5401 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5402 | if( prf->label == NULL ) |
| 5403 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5404 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5405 | memcpy( prf->label, data, data_length ); |
| 5406 | prf->label_length = data_length; |
| 5407 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5408 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5409 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5410 | |
| 5411 | return( PSA_SUCCESS ); |
| 5412 | } |
| 5413 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5414 | 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] | 5415 | psa_key_derivation_step_t step, |
| 5416 | const uint8_t *data, |
| 5417 | size_t data_length ) |
| 5418 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5419 | switch( step ) |
| 5420 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5421 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5422 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5423 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5424 | return( psa_tls12_prf_set_key( prf, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5425 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5426 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5427 | default: |
| 5428 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5429 | } |
| 5430 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5431 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5432 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5433 | |
| 5434 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5435 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5436 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5437 | const uint8_t *data, |
| 5438 | size_t data_length ) |
| 5439 | { |
| 5440 | psa_status_t status; |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5441 | const size_t pms_len = ( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? |
| 5442 | 4 + data_length + prf->other_secret_length : |
| 5443 | 4 + 2 * data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5444 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5445 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5446 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5447 | |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5448 | uint8_t *pms = mbedtls_calloc( 1, pms_len ); |
Przemek Stekiel | 937b90f | 2022-04-20 08:33:13 +0200 | [diff] [blame] | 5449 | if( pms == NULL ) |
| 5450 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5451 | uint8_t *cur = pms; |
| 5452 | |
| 5453 | /* pure-PSK: |
| 5454 | * Quoting RFC 4279, Section 2: |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5455 | * |
| 5456 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5457 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5458 | * uint16 with the value N, and the PSK itself. |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5459 | * |
| 5460 | * mixed-PSK: |
| 5461 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as |
| 5462 | * follows: concatenate a uint16 with the length of the other secret, |
| 5463 | * the other secret itself, uint16 with the length of PSK, and the |
| 5464 | * PSK itself. |
| 5465 | * For details please check: |
| 5466 | * - RFC 4279, Section 4 for the definition of RSA-PSK, |
| 5467 | * - RFC 4279, Section 3 for the definition of DHE-PSK, |
| 5468 | * - RFC 5489 for the definition of ECDHE-PSK. |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5469 | */ |
| 5470 | |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 5471 | if( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ) |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5472 | { |
| 5473 | *cur++ = MBEDTLS_BYTE_1( prf->other_secret_length ); |
| 5474 | *cur++ = MBEDTLS_BYTE_0( prf->other_secret_length ); |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 5475 | if( prf->other_secret_length != 0 ) |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 5476 | { |
| 5477 | memcpy( cur, prf->other_secret, prf->other_secret_length ); |
Przemek Stekiel | 03faf5d2 | 2022-04-20 08:37:43 +0200 | [diff] [blame] | 5478 | mbedtls_platform_zeroize( prf->other_secret, prf->other_secret_length ); |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 5479 | cur += prf->other_secret_length; |
| 5480 | } |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5481 | } |
| 5482 | else |
| 5483 | { |
| 5484 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 5485 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
| 5486 | memset( cur, 0, data_length ); |
| 5487 | cur += data_length; |
| 5488 | } |
| 5489 | |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 5490 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 5491 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5492 | memcpy( cur, data, data_length ); |
| 5493 | cur += data_length; |
| 5494 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5495 | status = psa_tls12_prf_set_key( prf, pms, cur - pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5496 | |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5497 | mbedtls_platform_zeroize( pms, pms_len ); |
| 5498 | mbedtls_free( pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5499 | return( status ); |
| 5500 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5501 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5502 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( |
| 5503 | psa_tls12_prf_key_derivation_t *prf, |
| 5504 | const uint8_t *data, |
| 5505 | size_t data_length ) |
| 5506 | { |
| 5507 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
| 5508 | return( PSA_ERROR_BAD_STATE ); |
| 5509 | |
| 5510 | if( data_length != 0 ) |
| 5511 | { |
| 5512 | prf->other_secret = mbedtls_calloc( 1, data_length ); |
| 5513 | if( prf->other_secret == NULL ) |
| 5514 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5515 | |
| 5516 | memcpy( prf->other_secret, data, data_length ); |
| 5517 | prf->other_secret_length = data_length; |
| 5518 | } |
| 5519 | else |
| 5520 | { |
| 5521 | prf->other_secret_length = 0; |
| 5522 | } |
| 5523 | |
| 5524 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; |
| 5525 | |
| 5526 | return( PSA_SUCCESS ); |
| 5527 | } |
| 5528 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5529 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5530 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5531 | psa_key_derivation_step_t step, |
| 5532 | const uint8_t *data, |
| 5533 | size_t data_length ) |
| 5534 | { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5535 | switch( step ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5536 | { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5537 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5538 | return( psa_tls12_prf_psk_to_ms_set_key( prf, |
| 5539 | data, data_length ) ); |
| 5540 | break; |
| 5541 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
| 5542 | return( psa_tls12_prf_psk_to_ms_set_other_key( prf, |
| 5543 | data, |
| 5544 | data_length ) ); |
| 5545 | break; |
| 5546 | default: |
| 5547 | return( psa_tls12_prf_input( prf, step, data, data_length ) ); |
| 5548 | break; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5549 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5550 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5551 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5552 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5553 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5554 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5555 | static psa_status_t psa_tls12_ecjpake_to_pms_input( |
| 5556 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5557 | psa_key_derivation_step_t step, |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5558 | const uint8_t *data, |
| 5559 | size_t data_length ) |
| 5560 | { |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5561 | if( data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE || |
| 5562 | step != PSA_KEY_DERIVATION_INPUT_SECRET ) |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5563 | { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5564 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5565 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5566 | |
| 5567 | /* Check if the passed point is in an uncompressed form */ |
| 5568 | if( data[0] != 0x04 ) |
| 5569 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5570 | |
| 5571 | /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */ |
| 5572 | memcpy( ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE ); |
| 5573 | |
| 5574 | return( PSA_SUCCESS ); |
| 5575 | } |
| 5576 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5577 | /** Check whether the given key type is acceptable for the given |
| 5578 | * input step of a key derivation. |
| 5579 | * |
| 5580 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5581 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5582 | * Both secret and non-secret inputs can alternatively have the type |
| 5583 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5584 | * that the input was passed as a buffer rather than via a key object. |
| 5585 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5586 | static int psa_key_derivation_check_input_type( |
| 5587 | psa_key_derivation_step_t step, |
| 5588 | psa_key_type_t key_type ) |
| 5589 | { |
| 5590 | switch( step ) |
| 5591 | { |
| 5592 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5593 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5594 | return( PSA_SUCCESS ); |
| 5595 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5596 | return( PSA_SUCCESS ); |
| 5597 | break; |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 5598 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
| 5599 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5600 | return( PSA_SUCCESS ); |
| 5601 | if( key_type == PSA_KEY_TYPE_NONE ) |
| 5602 | return( PSA_SUCCESS ); |
| 5603 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5604 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5605 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5606 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5607 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5608 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 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; |
| 5613 | } |
| 5614 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5615 | } |
| 5616 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5617 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5618 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5619 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5620 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5621 | const uint8_t *data, |
| 5622 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5623 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5624 | psa_status_t status; |
| 5625 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5626 | |
| 5627 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5628 | if( status != PSA_SUCCESS ) |
| 5629 | goto exit; |
| 5630 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5631 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 5632 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5633 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5634 | status = psa_hkdf_input( &operation->ctx.hkdf, kdf_alg, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5635 | step, data, data_length ); |
| 5636 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5637 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5638 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5639 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5640 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5641 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5642 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5643 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5644 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5645 | else |
| 5646 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 5647 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5648 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5649 | { |
| 5650 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5651 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5652 | } |
| 5653 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5654 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5655 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 5656 | if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5657 | { |
| 5658 | status = psa_tls12_ecjpake_to_pms_input( |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5659 | &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length ); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5660 | } |
| 5661 | else |
| 5662 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5663 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5664 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5665 | (void) data; |
| 5666 | (void) data_length; |
| 5667 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5668 | return( PSA_ERROR_BAD_STATE ); |
| 5669 | } |
| 5670 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5671 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5672 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5673 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5674 | return( status ); |
| 5675 | } |
| 5676 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5677 | psa_status_t psa_key_derivation_input_bytes( |
| 5678 | psa_key_derivation_operation_t *operation, |
| 5679 | psa_key_derivation_step_t step, |
| 5680 | const uint8_t *data, |
| 5681 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5682 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5683 | return( psa_key_derivation_input_internal( operation, step, |
| 5684 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5685 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5686 | } |
| 5687 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5688 | psa_status_t psa_key_derivation_input_key( |
| 5689 | psa_key_derivation_operation_t *operation, |
| 5690 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5691 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5692 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5693 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5694 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5695 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5696 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5697 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5698 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5699 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5700 | { |
| 5701 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5702 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5703 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5704 | |
| 5705 | /* Passing a key object as a SECRET input unlocks the permission |
| 5706 | * to output to a key object. */ |
| 5707 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5708 | operation->can_output_key = 1; |
| 5709 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5710 | status = psa_key_derivation_input_internal( operation, |
| 5711 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5712 | slot->key.data, |
| 5713 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5714 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5715 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5716 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5717 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5718 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5719 | |
| 5720 | |
| 5721 | |
| 5722 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5723 | /* Key agreement */ |
| 5724 | /****************************************************************/ |
| 5725 | |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5726 | psa_status_t psa_key_agreement_raw_builtin( const psa_key_attributes_t *attributes, |
| 5727 | const uint8_t *key_buffer, |
| 5728 | size_t key_buffer_size, |
| 5729 | psa_algorithm_t alg, |
| 5730 | const uint8_t *peer_key, |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5731 | size_t peer_key_length, |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5732 | uint8_t *shared_secret, |
| 5733 | size_t shared_secret_size, |
| 5734 | size_t *shared_secret_length ) |
| 5735 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5736 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5737 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5738 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5739 | case PSA_ALG_ECDH: |
Aditya Deshpande | 3f1606a | 2022-11-04 16:55:57 +0000 | [diff] [blame] | 5740 | return( mbedtls_psa_key_agreement_ecdh( attributes, key_buffer, |
Aditya Deshpande | 1ac41de | 2022-11-28 14:46:30 +0000 | [diff] [blame] | 5741 | key_buffer_size, alg, |
| 5742 | peer_key, peer_key_length, |
| 5743 | shared_secret, |
| 5744 | shared_secret_size, |
| 5745 | shared_secret_length ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5746 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5747 | default: |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5748 | (void) attributes; |
| 5749 | (void) key_buffer; |
| 5750 | (void) key_buffer_size; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5751 | (void) peer_key; |
| 5752 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5753 | (void) shared_secret; |
| 5754 | (void) shared_secret_size; |
| 5755 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5756 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5757 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5758 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5759 | |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5760 | /** Internal function for raw key agreement |
| 5761 | * Calls the driver wrapper which will hand off key agreement task |
Aditya Deshpande | 40c05cc | 2022-10-14 16:41:40 +0100 | [diff] [blame] | 5762 | * to the driver's implementation if a driver is present. |
| 5763 | * Fallback specified in the driver wrapper is built-in raw key agreement |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5764 | * (psa_key_agreement_raw_builtin). |
| 5765 | */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5766 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5767 | psa_key_slot_t *private_key, |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5768 | const uint8_t *peer_key, |
| 5769 | size_t peer_key_length, |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5770 | uint8_t *shared_secret, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5771 | size_t shared_secret_size, |
| 5772 | size_t *shared_secret_length ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5773 | { |
Aditya Deshpande | 1ac41de | 2022-11-28 14:46:30 +0000 | [diff] [blame] | 5774 | if( !PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5775 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5776 | |
| 5777 | psa_key_attributes_t attributes = { |
| 5778 | .core = private_key->attr |
| 5779 | }; |
| 5780 | |
Aditya Deshpande | 1ac41de | 2022-11-28 14:46:30 +0000 | [diff] [blame] | 5781 | return( psa_driver_wrapper_key_agreement( &attributes, |
| 5782 | private_key->key.data, |
| 5783 | private_key->key.bytes, alg, |
| 5784 | peer_key, peer_key_length, |
| 5785 | shared_secret, |
| 5786 | shared_secret_size, |
| 5787 | shared_secret_length ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | /* Note that if this function fails, you must call psa_key_derivation_abort() |
| 5791 | * to potentially free embedded data structures and wipe confidential data. |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5792 | */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5793 | 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] | 5794 | psa_key_derivation_step_t step, |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5795 | psa_key_slot_t *private_key, |
| 5796 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5797 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5798 | { |
| 5799 | psa_status_t status; |
Aditya Deshpande | 2f7fd76 | 2022-11-22 11:10:34 +0000 | [diff] [blame] | 5800 | uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE]; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5801 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5802 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg ); |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5803 | |
| 5804 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5805 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5806 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5807 | private_key, |
| 5808 | peer_key, peer_key_length, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5809 | shared_secret, |
| 5810 | sizeof( shared_secret ), |
| 5811 | &shared_secret_length ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5812 | if( status != PSA_SUCCESS ) |
| 5813 | goto exit; |
| 5814 | |
| 5815 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5816 | * the shared secret. A shared secret is permitted wherever a key |
| 5817 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5818 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5819 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5820 | shared_secret, |
| 5821 | shared_secret_length ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5822 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5823 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5824 | return( status ); |
| 5825 | } |
| 5826 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5827 | 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] | 5828 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5829 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5830 | const uint8_t *peer_key, |
| 5831 | size_t peer_key_length ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5832 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5833 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5834 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5835 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5836 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5837 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5838 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5839 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5840 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5841 | if( status != PSA_SUCCESS ) |
| 5842 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5843 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5844 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5845 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5846 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5847 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5848 | else |
| 5849 | { |
| 5850 | /* If a private key has been added as SECRET, we allow the derived |
| 5851 | * key material to be used as a key in PSA Crypto. */ |
| 5852 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5853 | operation->can_output_key = 1; |
| 5854 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5855 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5856 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5857 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5858 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5859 | } |
| 5860 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5861 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5862 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5863 | const uint8_t *peer_key, |
| 5864 | size_t peer_key_length, |
| 5865 | uint8_t *output, |
| 5866 | size_t output_size, |
| 5867 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5868 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5869 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5870 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5871 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5872 | |
| 5873 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5874 | { |
| 5875 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5876 | goto exit; |
| 5877 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5878 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5879 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5880 | if( status != PSA_SUCCESS ) |
| 5881 | goto exit; |
| 5882 | |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5883 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 5884 | * for the output size. The PSA specification only guarantees that this |
| 5885 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 5886 | * but it might be nice to allow smaller buffers if the output fits. |
| 5887 | * At the time of writing this comment, with only ECDH implemented, |
| 5888 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 5889 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 5890 | * be exact for it as well. */ |
| 5891 | size_t expected_length = |
| 5892 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits ); |
| 5893 | if( output_size < expected_length ) |
| 5894 | { |
| 5895 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5896 | goto exit; |
| 5897 | } |
| 5898 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5899 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5900 | peer_key, peer_key_length, |
| 5901 | output, output_size, |
| 5902 | output_length ); |
| 5903 | |
| 5904 | exit: |
| 5905 | if( status != PSA_SUCCESS ) |
| 5906 | { |
| 5907 | /* If an error happens and is not handled properly, the output |
| 5908 | * may be used as a key to protect sensitive data. Arrange for such |
| 5909 | * a key to be random, which is likely to result in decryption or |
| 5910 | * verification errors. This is better than filling the buffer with |
| 5911 | * some constant data such as zeros, which would result in the data |
| 5912 | * being protected with a reproducible, easily knowable key. |
| 5913 | */ |
| 5914 | psa_generate_random( output, output_size ); |
| 5915 | *output_length = output_size; |
| 5916 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5917 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5918 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5919 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5920 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5921 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5922 | |
| 5923 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5924 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5925 | /****************************************************************/ |
| 5926 | /* Random generation */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5927 | /****************************************************************/ |
| 5928 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5929 | /** Initialize the PSA random generator. |
| 5930 | */ |
| 5931 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 5932 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5933 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5934 | memset( rng, 0, sizeof( *rng ) ); |
| 5935 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5936 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5937 | /* Set default configuration if |
| 5938 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5939 | if( rng->entropy_init == NULL ) |
| 5940 | rng->entropy_init = mbedtls_entropy_init; |
| 5941 | if( rng->entropy_free == NULL ) |
| 5942 | rng->entropy_free = mbedtls_entropy_free; |
| 5943 | |
| 5944 | rng->entropy_init( &rng->entropy ); |
| 5945 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5946 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5947 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5948 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5949 | mbedtls_entropy_add_source( &rng->entropy, |
| 5950 | mbedtls_nv_seed_poll, NULL, |
| 5951 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5952 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5953 | #endif |
| 5954 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5955 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5956 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5957 | } |
| 5958 | |
| 5959 | /** Deinitialize the PSA random generator. |
| 5960 | */ |
| 5961 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 5962 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5963 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5964 | memset( rng, 0, sizeof( *rng ) ); |
| 5965 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5966 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5967 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5968 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5969 | } |
| 5970 | |
| 5971 | /** Seed the PSA random generator. |
| 5972 | */ |
| 5973 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 5974 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5975 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5976 | /* Do nothing: the external RNG seeds itself. */ |
| 5977 | (void) rng; |
| 5978 | return( PSA_SUCCESS ); |
| 5979 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5980 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5981 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 5982 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5983 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5984 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5985 | } |
| 5986 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5987 | psa_status_t psa_generate_random( uint8_t *output, |
| 5988 | size_t output_size ) |
| 5989 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5990 | GUARD_MODULE_INITIALIZED; |
| 5991 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5992 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5993 | |
| 5994 | size_t output_length = 0; |
| 5995 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 5996 | output, output_size, |
| 5997 | &output_length ); |
| 5998 | if( status != PSA_SUCCESS ) |
| 5999 | return( status ); |
| 6000 | /* Breaking up a request into smaller chunks is currently not supported |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame^] | 6001 | * for the external RNG interface. */ |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6002 | if( output_length != output_size ) |
| 6003 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6004 | return( PSA_SUCCESS ); |
| 6005 | |
| 6006 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6007 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6008 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6009 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6010 | size_t request_size = |
| 6011 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6012 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6013 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6014 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 6015 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6016 | if( ret != 0 ) |
| 6017 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6018 | output_size -= request_size; |
| 6019 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6020 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6021 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6022 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6023 | } |
| 6024 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6025 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6026 | * |
| 6027 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6028 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6029 | * PSA API doesn't support passing an explicit state. |
| 6030 | * |
| 6031 | * In the non-external case, psa_generate_random() calls an |
| 6032 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6033 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6034 | * instead of doing this back-and-forth between the PSA API and the |
| 6035 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6036 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6037 | */ |
| 6038 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6039 | int mbedtls_psa_get_random( void *p_rng, |
| 6040 | unsigned char *output, |
| 6041 | size_t output_size ) |
| 6042 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6043 | /* This function takes a pointer to the RNG state because that's what |
| 6044 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6045 | * its own state internally and doesn't let the caller access that state. |
| 6046 | * So we just ignore the state parameter, and in practice we'll pass |
| 6047 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6048 | (void) p_rng; |
| 6049 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6050 | if( status == PSA_SUCCESS ) |
| 6051 | return( 0 ); |
| 6052 | else |
| 6053 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6054 | } |
| 6055 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6056 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6057 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Chris Jones | ea0a865 | 2021-03-09 19:11:19 +0000 | [diff] [blame] | 6058 | #include "entropy_poll.h" |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6059 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6060 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6061 | size_t seed_size ) |
| 6062 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6063 | if( global_data.initialized ) |
| 6064 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6065 | |
| 6066 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6067 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6068 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6069 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6070 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6071 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6072 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6073 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6074 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6075 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6076 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6077 | * \param type The key type |
| 6078 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6079 | * |
| 6080 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6081 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6082 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 6083 | * The size in bits of the key is not valid. |
| 6084 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 6085 | * The type and/or the size in bits of the key or the combination of |
| 6086 | * the two is not supported. |
| 6087 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6088 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 6089 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6090 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6091 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6092 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6093 | if( key_type_is_raw_bytes( type ) ) |
| 6094 | { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 6095 | status = psa_validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6096 | if( status != PSA_SUCCESS ) |
| 6097 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6098 | } |
| 6099 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6100 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6101 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6102 | { |
| 6103 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6104 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6105 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6106 | /* Accept only byte-aligned keys, for the same reasons as |
| 6107 | * in psa_import_rsa_key(). */ |
| 6108 | if( bits % 8 != 0 ) |
| 6109 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6110 | } |
| 6111 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6112 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6113 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6114 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6115 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6116 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 6117 | /* To avoid empty block, return successfully here. */ |
| 6118 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6119 | } |
| 6120 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6121 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6122 | { |
| 6123 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6124 | } |
| 6125 | |
| 6126 | return( PSA_SUCCESS ); |
| 6127 | } |
| 6128 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 6129 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6130 | const psa_key_attributes_t *attributes, |
| 6131 | 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] | 6132 | { |
| 6133 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6134 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6135 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6136 | if( ( attributes->domain_parameters == NULL ) && |
| 6137 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6138 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6139 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6140 | if( key_type_is_raw_bytes( type ) ) |
| 6141 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6142 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6143 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6144 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6145 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6146 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6147 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6148 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 6149 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6150 | } |
| 6151 | else |
| 6152 | |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6153 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 6154 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6155 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6156 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 6157 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 6158 | key_buffer, |
| 6159 | key_buffer_size, |
| 6160 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6161 | } |
| 6162 | else |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6163 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 6164 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6165 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6166 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6167 | 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] | 6168 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 6169 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 6170 | key_buffer, |
| 6171 | key_buffer_size, |
| 6172 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6173 | } |
| 6174 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6175 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6176 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6177 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6178 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6179 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6180 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6181 | return( PSA_SUCCESS ); |
| 6182 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6183 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6184 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6185 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6186 | { |
| 6187 | psa_status_t status; |
| 6188 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6189 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6190 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6191 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6192 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6193 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6194 | /* Reject any attempt to create a zero-length key so that we don't |
| 6195 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6196 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6197 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6198 | |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6199 | /* Reject any attempt to create a public key. */ |
| 6200 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) ) |
| 6201 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6202 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6203 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6204 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6205 | if( status != PSA_SUCCESS ) |
| 6206 | goto exit; |
| 6207 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6208 | /* 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] | 6209 | * storage ( thus not in the case of generating a key in a secure element |
| 6210 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 6211 | * buffer to hold the generated key material. */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6212 | if( slot->key.data == NULL ) |
| 6213 | { |
| 6214 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 6215 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 6216 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6217 | status = psa_validate_key_type_and_size_for_key_generation( |
| 6218 | attributes->core.type, attributes->core.bits ); |
| 6219 | if( status != PSA_SUCCESS ) |
| 6220 | goto exit; |
| 6221 | |
| 6222 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 6223 | attributes->core.type, |
| 6224 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6225 | } |
| 6226 | else |
| 6227 | { |
| 6228 | status = psa_driver_wrapper_get_key_buffer_size( |
| 6229 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6230 | if( status != PSA_SUCCESS ) |
| 6231 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6232 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6233 | |
| 6234 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 6235 | if( status != PSA_SUCCESS ) |
| 6236 | goto exit; |
| 6237 | } |
| 6238 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6239 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6240 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6241 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6242 | if( status != PSA_SUCCESS ) |
| 6243 | psa_remove_key_data_from_memory( slot ); |
| 6244 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6245 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6246 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6247 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6248 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6249 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6250 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6251 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6252 | } |
| 6253 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6254 | /****************************************************************/ |
| 6255 | /* Module setup */ |
| 6256 | /****************************************************************/ |
| 6257 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6258 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6259 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6260 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6261 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6262 | { |
| 6263 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6264 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6265 | global_data.rng.entropy_init = entropy_init; |
| 6266 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6267 | return( PSA_SUCCESS ); |
| 6268 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6269 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6270 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6271 | void mbedtls_psa_crypto_free( void ) |
| 6272 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6273 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6274 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6275 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6276 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6277 | } |
| 6278 | /* Wipe all remaining data, including configuration. |
| 6279 | * In particular, this sets all state indicator to the value |
| 6280 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6281 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 6282 | |
| 6283 | /* Terminate drivers */ |
| 6284 | psa_driver_wrapper_free( ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6285 | } |
| 6286 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6287 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6288 | /** Recover a transaction that was interrupted by a power failure. |
| 6289 | * |
| 6290 | * This function is called during initialization, before psa_crypto_init() |
| 6291 | * returns. If this function returns a failure status, the initialization |
| 6292 | * fails. |
| 6293 | */ |
| 6294 | static psa_status_t psa_crypto_recover_transaction( |
| 6295 | const psa_crypto_transaction_t *transaction ) |
| 6296 | { |
| 6297 | switch( transaction->unknown.type ) |
| 6298 | { |
| 6299 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6300 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6301 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6302 | * is implemented. |
| 6303 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6304 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6305 | default: |
| 6306 | /* We found an unsupported transaction in the storage. |
| 6307 | * 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] | 6308 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6309 | } |
| 6310 | } |
| 6311 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6312 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6313 | psa_status_t psa_crypto_init( void ) |
| 6314 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6315 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6316 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6317 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6318 | if( global_data.initialized != 0 ) |
| 6319 | return( PSA_SUCCESS ); |
| 6320 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6321 | /* Initialize and seed the random generator. */ |
| 6322 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6323 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6324 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6325 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6326 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6327 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6328 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6329 | status = psa_initialize_key_slots( ); |
| 6330 | if( status != PSA_SUCCESS ) |
| 6331 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6332 | |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 6333 | /* Init drivers */ |
| 6334 | status = psa_driver_wrapper_init( ); |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6335 | if( status != PSA_SUCCESS ) |
| 6336 | goto exit; |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6337 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6338 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6339 | status = psa_crypto_load_transaction( ); |
| 6340 | if( status == PSA_SUCCESS ) |
| 6341 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6342 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6343 | if( status != PSA_SUCCESS ) |
| 6344 | goto exit; |
| 6345 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6346 | } |
| 6347 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6348 | { |
| 6349 | /* There's no transaction to complete. It's all good. */ |
| 6350 | status = PSA_SUCCESS; |
| 6351 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6352 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6353 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6354 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6355 | global_data.initialized = 1; |
| 6356 | |
| 6357 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6358 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6359 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6360 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6361 | } |
| 6362 | |
| 6363 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |