Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 21 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 24 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 25 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 26 | #include "check_crypto_config.h" |
| 27 | #endif |
| 28 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 29 | #include "psa/crypto.h" |
Przemyslaw Stekiel | 705fb0f | 2021-11-24 08:47:29 +0100 | [diff] [blame] | 30 | #include "psa/crypto_values.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 32 | #include "psa_crypto_cipher.h" |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 35 | #include "psa_crypto_driver_wrappers.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 36 | #include "psa_crypto_ecp.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 37 | #include "psa_crypto_hash.h" |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 38 | #include "psa_crypto_mac.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 39 | #include "psa_crypto_rsa.h" |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 40 | #include "psa_crypto_ecp.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 42 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 43 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 44 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 45 | /* Include internal declarations that are useful for implementing persistently |
| 46 | * stored keys. */ |
| 47 | #include "psa_crypto_storage.h" |
| 48 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 49 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 50 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 51 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 52 | #include <stdlib.h> |
| 53 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 54 | #include "mbedtls/platform.h" |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 55 | #if !defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 56 | #define mbedtls_calloc calloc |
| 57 | #define mbedtls_free free |
| 58 | #endif |
| 59 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 60 | #include "mbedtls/aes.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 61 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 62 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 63 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 64 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 65 | #include "mbedtls/chacha20.h" |
| 66 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 67 | #include "mbedtls/cipher.h" |
| 68 | #include "mbedtls/ccm.h" |
| 69 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 70 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 71 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 72 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 73 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 74 | #include "mbedtls/error.h" |
| 75 | #include "mbedtls/gcm.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 76 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 77 | #include "mbedtls/md.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 78 | #include "md_wrap.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 79 | #include "mbedtls/pk.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 80 | #include "pk_wrap.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 81 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 82 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 83 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 84 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 85 | #include "mbedtls/sha1.h" |
| 86 | #include "mbedtls/sha256.h" |
| 87 | #include "mbedtls/sha512.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 88 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 89 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 90 | |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 91 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 92 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
| 93 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 94 | #define BUILTIN_ALG_ANY_HKDF 1 |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 95 | #endif |
| 96 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 97 | /****************************************************************/ |
| 98 | /* Global data, support functions and library management */ |
| 99 | /****************************************************************/ |
| 100 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 101 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 102 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 103 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 106 | /* Values for psa_global_data_t::rng_state */ |
| 107 | #define RNG_NOT_INITIALIZED 0 |
| 108 | #define RNG_INITIALIZED 1 |
| 109 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 110 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 111 | typedef struct |
| 112 | { |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 113 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 114 | unsigned rng_state : 2; |
Gilles Peskine | 2d8a182 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 115 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 116 | } psa_global_data_t; |
| 117 | |
| 118 | static psa_global_data_t global_data; |
| 119 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 120 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 121 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 122 | &global_data.rng.drbg; |
| 123 | #endif |
| 124 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 125 | #define GUARD_MODULE_INITIALIZED \ |
| 126 | if( global_data.initialized == 0 ) \ |
| 127 | return( PSA_ERROR_BAD_STATE ); |
| 128 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 129 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 130 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 131 | /* Mbed TLS error codes can combine a high-level error code and a |
| 132 | * low-level error code. The low-level error usually reflects the |
| 133 | * root cause better, so dispatch on that preferably. */ |
| 134 | int low_level_ret = - ( -ret & 0x007f ); |
| 135 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 136 | { |
| 137 | case 0: |
| 138 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 139 | |
| 140 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 141 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 142 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 143 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 144 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 145 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 146 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 147 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 148 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 149 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 150 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 151 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 152 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 153 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 154 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 155 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 156 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 157 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 158 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 159 | |
| 160 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 161 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 162 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 163 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 164 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 165 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 166 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 167 | |
| 168 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 169 | return( PSA_ERROR_BAD_STATE ); |
| 170 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 171 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 172 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 173 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 174 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 175 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 176 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 177 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 178 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 179 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 180 | return( PSA_ERROR_INVALID_PADDING ); |
| 181 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 182 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 183 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 184 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 185 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 186 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 187 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 188 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 189 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 190 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 191 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 192 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 193 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 194 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 195 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 196 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 197 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 198 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 199 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 200 | |
| 201 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 202 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 203 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 204 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 205 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 206 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 207 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 208 | |
| 209 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 210 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Mateusz Starzyk | f28261f | 2021-09-30 16:39:07 +0200 | [diff] [blame] | 211 | case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL: |
| 212 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 213 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 214 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 215 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 216 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 217 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 218 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 219 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 220 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 221 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 222 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 223 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 224 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 225 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 226 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 227 | #endif |
| 228 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 229 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 230 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 231 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 232 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 233 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 234 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 235 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 236 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 237 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 238 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 239 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 240 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 241 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 242 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 243 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 244 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 245 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 246 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 248 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 249 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 250 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 251 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 252 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 253 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 254 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 255 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 256 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 257 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 258 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 259 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 260 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 261 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 262 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 263 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 264 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 265 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 266 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 267 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 268 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 269 | return( PSA_ERROR_NOT_PERMITTED ); |
| 270 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 271 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 272 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 273 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 274 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 275 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 276 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 277 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | f9f1bdf | 2021-06-23 20:32:27 +0200 | [diff] [blame] | 278 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: |
| 279 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 280 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 281 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 282 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 283 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 284 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 285 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 286 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 287 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 288 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 289 | return( PSA_ERROR_INVALID_PADDING ); |
| 290 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 291 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 292 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 293 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 294 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 295 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 296 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 297 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 298 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 299 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 300 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 301 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 302 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 303 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 304 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 305 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 306 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 307 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 308 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 309 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 310 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 311 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 312 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 313 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 314 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 315 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 316 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 317 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 318 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 319 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 320 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 321 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 322 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 323 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 327 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 328 | |
| 329 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 330 | /****************************************************************/ |
| 331 | /* Key management */ |
| 332 | /****************************************************************/ |
| 333 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 334 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 335 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 336 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 337 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 338 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 339 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 340 | size_t bits, |
| 341 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 342 | { |
| 343 | switch( curve ) |
| 344 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 345 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 346 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 347 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 348 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 349 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 350 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 351 | #endif |
| 352 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 353 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 354 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 355 | #endif |
| 356 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 357 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 358 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 359 | #endif |
| 360 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 361 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 362 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 363 | #endif |
| 364 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 365 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 366 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 367 | case 528: |
| 368 | if( bits_is_sloppy ) |
| 369 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 370 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 371 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 372 | } |
| 373 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 374 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 375 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 376 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 377 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 378 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 379 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 380 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 381 | #endif |
| 382 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 383 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 384 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 385 | #endif |
| 386 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 387 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 388 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 389 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 390 | } |
| 391 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 392 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 393 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 394 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 395 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 396 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 397 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 398 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 399 | case 256: |
| 400 | if( bits_is_sloppy ) |
| 401 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 402 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 403 | #endif |
| 404 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 405 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 406 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 407 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 408 | } |
| 409 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 410 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 411 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 412 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 413 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 414 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 415 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 416 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 417 | #endif |
| 418 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 419 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 420 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 421 | #endif |
| 422 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 423 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 424 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 425 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 426 | } |
| 427 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 428 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 429 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 430 | (void) bits_is_sloppy; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 431 | return( MBEDTLS_ECP_DP_NONE ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 432 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 433 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 434 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 435 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 436 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 437 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 438 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 439 | psa_status_t psa_validate_unstructured_key_bit_size( psa_key_type_t type, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 440 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 441 | { |
| 442 | /* Check that the bit size is acceptable for the key type */ |
| 443 | switch( type ) |
| 444 | { |
| 445 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 447 | case PSA_KEY_TYPE_DERIVE: |
Neil Armstrong | 4b5710f | 2022-05-25 11:30:27 +0200 | [diff] [blame] | 448 | case PSA_KEY_TYPE_PASSWORD: |
| 449 | case PSA_KEY_TYPE_PASSWORD_HASH: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 450 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 451 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 452 | case PSA_KEY_TYPE_AES: |
| 453 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 454 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 455 | break; |
| 456 | #endif |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 457 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 458 | case PSA_KEY_TYPE_ARIA: |
| 459 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 460 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 461 | break; |
| 462 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 463 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 464 | case PSA_KEY_TYPE_CAMELLIA: |
| 465 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 466 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 467 | break; |
| 468 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 469 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 470 | case PSA_KEY_TYPE_DES: |
| 471 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 472 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 473 | break; |
| 474 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 475 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 476 | case PSA_KEY_TYPE_CHACHA20: |
| 477 | if( bits != 256 ) |
| 478 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 479 | break; |
| 480 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 481 | default: |
| 482 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 483 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 484 | if( bits % 8 != 0 ) |
| 485 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 486 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 487 | return( PSA_SUCCESS ); |
| 488 | } |
| 489 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 490 | /** 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] | 491 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 492 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 493 | * 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] | 494 | * |
| 495 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 496 | * \param[in] key_type The key type of the key to be used with the |
| 497 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 498 | * |
| 499 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 500 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 501 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 502 | * 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] | 503 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 504 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 505 | psa_algorithm_t algorithm, |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 506 | psa_key_type_t key_type ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 507 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 508 | if( PSA_ALG_IS_HMAC( algorithm ) ) |
| 509 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 510 | if( key_type == PSA_KEY_TYPE_HMAC ) |
| 511 | return( PSA_SUCCESS ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 512 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 513 | |
| 514 | if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 515 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 516 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 517 | * key. */ |
| 518 | if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) == |
| 519 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC ) |
| 520 | { |
| 521 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 522 | * the block length (larger than 1) for block ciphers. */ |
| 523 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 ) |
| 524 | return( PSA_SUCCESS ); |
| 525 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 526 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 527 | |
| 528 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 529 | } |
| 530 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 531 | psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 532 | size_t buffer_length ) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 533 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 534 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 535 | return( PSA_ERROR_ALREADY_EXISTS ); |
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.data = mbedtls_calloc( 1, buffer_length ); |
| 538 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 539 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 540 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 541 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 542 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 543 | } |
| 544 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 545 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 546 | const uint8_t* data, |
| 547 | size_t data_length ) |
| 548 | { |
| 549 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 550 | data_length ); |
| 551 | if( status != PSA_SUCCESS ) |
| 552 | return( status ); |
| 553 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 554 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 555 | return( PSA_SUCCESS ); |
| 556 | } |
| 557 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 558 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 559 | const psa_key_attributes_t *attributes, |
| 560 | const uint8_t *data, size_t data_length, |
| 561 | uint8_t *key_buffer, size_t key_buffer_size, |
| 562 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 563 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 564 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 565 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 566 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 567 | /* zero-length keys are never supported. */ |
| 568 | if( data_length == 0 ) |
| 569 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 570 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 571 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 572 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 573 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 574 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 575 | status = psa_validate_unstructured_key_bit_size( attributes->core.type, |
| 576 | *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 577 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 578 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 579 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 580 | /* Copy the key material. */ |
| 581 | memcpy( key_buffer, data, data_length ); |
| 582 | *key_buffer_length = data_length; |
| 583 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 584 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 585 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 586 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 587 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 588 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 589 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 590 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 591 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 592 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 593 | return( mbedtls_psa_ecp_import_key( attributes, |
| 594 | data, data_length, |
| 595 | key_buffer, key_buffer_size, |
| 596 | key_buffer_length, |
| 597 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 598 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 599 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 600 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 601 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 602 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 603 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 604 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 605 | return( mbedtls_psa_rsa_import_key( attributes, |
| 606 | data, data_length, |
| 607 | key_buffer, key_buffer_size, |
| 608 | key_buffer_length, |
| 609 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 610 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 611 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 612 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 613 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 614 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 615 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 618 | /** Calculate the intersection of two algorithm usage policies. |
| 619 | * |
| 620 | * Return 0 (which allows no operation) on incompatibility. |
| 621 | */ |
| 622 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 623 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 624 | psa_algorithm_t alg1, |
| 625 | psa_algorithm_t alg2 ) |
| 626 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 627 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 628 | if( alg1 == alg2 ) |
| 629 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 630 | /* If the policies are from the same hash-and-sign family, check |
| 631 | * 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] | 632 | if( PSA_ALG_IS_SIGN_HASH( alg1 ) && |
| 633 | PSA_ALG_IS_SIGN_HASH( alg2 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 634 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 635 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 636 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 637 | return( alg2 ); |
| 638 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 639 | return( alg1 ); |
| 640 | } |
| 641 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 642 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 643 | * restrictive tag length. */ |
| 644 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 645 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 646 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 647 | { |
| 648 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 649 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 650 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 651 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 652 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 653 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 654 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 655 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 656 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 657 | alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 658 | } |
| 659 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 660 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 661 | ( alg1_len <= alg2_len ) ) |
| 662 | { |
| 663 | return( alg2 ); |
| 664 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 665 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 666 | ( alg2_len <= alg1_len ) ) |
| 667 | { |
| 668 | return( alg1 ); |
| 669 | } |
| 670 | } |
| 671 | /* If the policies are from the same MAC family, check whether one |
| 672 | * of them is a minimum-MAC-length policy. Calculate the most |
| 673 | * restrictive tag length. */ |
| 674 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 675 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 676 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 677 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 678 | /* Validate the combination of key type and algorithm. Since the base |
| 679 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
| 680 | if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) ) |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 681 | return( 0 ); |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 682 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 683 | /* Get the (exact or at-least) output lengths for both sides of the |
| 684 | * requested intersection. None of the currently supported algorithms |
| 685 | * have an output length dependent on the actual key size, so setting it |
| 686 | * to a bogus value of 0 is currently OK. |
| 687 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 688 | * Note that for at-least-this-length wildcard algorithms, the output |
| 689 | * length is set to the shortest allowed length, which allows us to |
| 690 | * calculate the most restrictive tag length for the intersection. */ |
| 691 | size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 ); |
| 692 | size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 693 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 694 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 695 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 696 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 697 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 698 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 699 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 700 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 701 | |
| 702 | /* If only one is an at-least-this-length policy, the intersection would |
| 703 | * be the other (fixed-length) policy as long as said fixed length is |
| 704 | * equal to or larger than the shortest allowed length. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 705 | if( ( alg1 & 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( ( alg1_len <= alg2_len ) ? alg2 : 0 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 708 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 709 | if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 710 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 711 | return( ( alg2_len <= alg1_len ) ? alg1 : 0 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 712 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 713 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 714 | /* If none of them are wildcards, check whether they define the same tag |
| 715 | * length. This is still possible here when one is default-length and |
| 716 | * the other specific-length. Ensure to always return the |
| 717 | * specific-length version for the intersection. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 718 | if( alg1_len == alg2_len ) |
| 719 | return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 720 | } |
| 721 | /* If the policies are incompatible, allow nothing. */ |
| 722 | return( 0 ); |
| 723 | } |
| 724 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 725 | static int psa_key_algorithm_permits( psa_key_type_t key_type, |
| 726 | psa_algorithm_t policy_alg, |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 727 | psa_algorithm_t requested_alg ) |
| 728 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 729 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 730 | if( requested_alg == policy_alg ) |
| 731 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 732 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 733 | * and requested_alg is the same hash-and-sign family with any hash, |
| 734 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 735 | if( PSA_ALG_IS_SIGN_HASH( requested_alg ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 736 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 737 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 738 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 739 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 740 | } |
| 741 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 742 | * the requested algorithm, check the requested tag length to be |
| 743 | * equal-length or longer than the wildcard-specified length. */ |
| 744 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 745 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 746 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 747 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 748 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 749 | { |
| 750 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 751 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 752 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 753 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 754 | * algorithm, check whether their MAC lengths are compatible. */ |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 755 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 756 | PSA_ALG_IS_MAC( requested_alg ) && |
| 757 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 758 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 759 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 760 | /* Validate the combination of key type and algorithm. Since the policy |
| 761 | * and requested algorithms are the same, we only need this once. */ |
| 762 | if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 763 | return( 0 ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 764 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 765 | /* Get both the requested output length for the algorithm which is to be |
| 766 | * verified, and the default output length for the base algorithm. |
| 767 | * Note that none of the currently supported algorithms have an output |
| 768 | * length dependent on actual key size, so setting it to a bogus value |
| 769 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 770 | size_t requested_output_length = PSA_MAC_LENGTH( |
| 771 | key_type, 0, requested_alg ); |
| 772 | size_t default_output_length = PSA_MAC_LENGTH( |
| 773 | key_type, 0, |
| 774 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 775 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 776 | /* If the policy is default-length, only allow an algorithm with |
| 777 | * a declared exact-length matching the default. */ |
| 778 | if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 779 | return( requested_output_length == default_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 780 | |
| 781 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 782 | * length exactly matches the default length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 783 | if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 && |
| 784 | PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length ) |
| 785 | { |
| 786 | return( 1 ); |
| 787 | } |
| 788 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 789 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 790 | * check for the requested MAC length to be equal to or longer than the |
| 791 | * minimum allowed length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 792 | if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
| 793 | { |
| 794 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 795 | requested_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 796 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 797 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 798 | /* 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] | 799 | * a key derivation with that key agreement should also be allowed. This |
| 800 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 801 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 802 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 803 | { |
| 804 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 805 | policy_alg ); |
| 806 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 807 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 808 | return( 0 ); |
| 809 | } |
| 810 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 811 | /** Test whether a policy permits an algorithm. |
| 812 | * |
| 813 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 814 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 815 | * \note This function requires providing the key type for which the policy is |
| 816 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 817 | * have different properties depending on what kind of cipher it is |
| 818 | * combined with. |
| 819 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 820 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 821 | * allowed by the \p policy. |
| 822 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 823 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 824 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 825 | */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 826 | 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] | 827 | psa_key_type_t key_type, |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 828 | psa_algorithm_t alg ) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 829 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 830 | /* '0' is not a valid algorithm */ |
| 831 | if( alg == 0 ) |
| 832 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 833 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 834 | /* A requested algorithm cannot be a wildcard. */ |
| 835 | if( PSA_ALG_IS_WILDCARD( alg ) ) |
| 836 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 837 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 838 | if( psa_key_algorithm_permits( key_type, policy->alg, alg ) || |
| 839 | psa_key_algorithm_permits( key_type, policy->alg2, alg ) ) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 840 | return( PSA_SUCCESS ); |
| 841 | else |
| 842 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 843 | } |
| 844 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 845 | /** Restrict a key policy based on a constraint. |
| 846 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 847 | * \note This function requires providing the key type for which the policy is |
| 848 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 849 | * have different properties depending on what kind of cipher it is |
| 850 | * combined with. |
| 851 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 852 | * \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] | 853 | * \param[in,out] policy The policy to restrict. |
| 854 | * \param[in] constraint The policy constraint to apply. |
| 855 | * |
| 856 | * \retval #PSA_SUCCESS |
| 857 | * \c *policy contains the intersection of the original value of |
| 858 | * \c *policy and \c *constraint. |
| 859 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 860 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 861 | * \c *policy is unchanged. |
| 862 | */ |
| 863 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 864 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 865 | psa_key_policy_t *policy, |
| 866 | const psa_key_policy_t *constraint ) |
| 867 | { |
| 868 | psa_algorithm_t intersection_alg = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 869 | psa_key_policy_algorithm_intersection( key_type, policy->alg, |
| 870 | constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 871 | psa_algorithm_t intersection_alg2 = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 872 | psa_key_policy_algorithm_intersection( key_type, policy->alg2, |
| 873 | constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 874 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 875 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 876 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 877 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 878 | policy->usage &= constraint->usage; |
| 879 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 880 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 881 | return( PSA_SUCCESS ); |
| 882 | } |
| 883 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 884 | /** Get the description of a key given its identifier and policy constraints |
| 885 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 886 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 887 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 888 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 889 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 890 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 891 | * In case of a persistent key, the function loads the description of the key |
| 892 | * into a key slot if not already done. |
| 893 | * |
| 894 | * On success, the returned key slot is locked. It is the responsibility of |
| 895 | * the caller to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 896 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 897 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 898 | mbedtls_svc_key_id_t key, |
| 899 | psa_key_slot_t **p_slot, |
| 900 | psa_key_usage_t usage, |
| 901 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 902 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 903 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 904 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 905 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 906 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 907 | if( status != PSA_SUCCESS ) |
| 908 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 909 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 910 | |
| 911 | /* Enforce that usage policy for the key slot contains all the flags |
| 912 | * required by the usage parameter. There is one exception: public |
| 913 | * keys can always be exported, so we treat public key objects as |
| 914 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 915 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 916 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 917 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 918 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 919 | { |
| 920 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 921 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 922 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 923 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 924 | /* Enforce that the usage policy permits the requested algorithm. */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 925 | if( alg != 0 ) |
| 926 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 927 | status = psa_key_policy_permits( &slot->attr.policy, |
| 928 | slot->attr.type, |
| 929 | alg ); |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 930 | if( status != PSA_SUCCESS ) |
| 931 | goto error; |
| 932 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 933 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 934 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 935 | |
| 936 | error: |
| 937 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 938 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 939 | |
| 940 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 941 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 942 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 943 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 944 | * |
| 945 | * 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] | 946 | * available, as opposed to a key in a secure element and/or to be used |
| 947 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 948 | * |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 949 | * This is a temporary function that may be used instead of |
| 950 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 951 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 952 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 953 | * On success, the returned key slot is locked. It is the responsibility of the |
| 954 | * 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] | 955 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 956 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 957 | mbedtls_svc_key_id_t key, |
| 958 | psa_key_slot_t **p_slot, |
| 959 | psa_key_usage_t usage, |
| 960 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 961 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 962 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 963 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 964 | if( status != PSA_SUCCESS ) |
| 965 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 966 | |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 967 | if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 968 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 969 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 970 | *p_slot = NULL; |
| 971 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 972 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 973 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 974 | return( PSA_SUCCESS ); |
| 975 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 976 | |
Steven Cooreman | 7ddee7f | 2021-04-07 18:08:30 +0200 | [diff] [blame] | 977 | 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] | 978 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 979 | /* Data pointer will always be either a valid pointer or NULL in an |
| 980 | * initialized slot, so we can just free it. */ |
| 981 | if( slot->key.data != NULL ) |
| 982 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 983 | |
| 984 | mbedtls_free( slot->key.data ); |
| 985 | slot->key.data = NULL; |
| 986 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 987 | |
| 988 | return( PSA_SUCCESS ); |
| 989 | } |
| 990 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 991 | /** Completely wipe a slot in memory, including its policy. |
| 992 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 993 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 994 | { |
| 995 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 996 | |
TRodziewicz | 18cddc0 | 2021-07-13 12:19:15 +0200 | [diff] [blame] | 997 | /* |
| 998 | * 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] | 999 | * do our best to report an unexpected lock counter. Assert with |
| 1000 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one: |
| 1001 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 1002 | * function is called as part of the execution of a test suite, the |
| 1003 | * execution of the test suite is stopped in error if the assertion fails. |
TRodziewicz | 18cddc0 | 2021-07-13 12:19:15 +0200 | [diff] [blame] | 1004 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1005 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1006 | { |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 1007 | MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1008 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1009 | } |
| 1010 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1011 | /* Multipart operations may still be using the key. This is safe |
| 1012 | * because all multipart operation objects are independent from |
| 1013 | * the key slot: if they need to access the key after the setup |
| 1014 | * phase, they have a copy of the key. Note that this means that |
| 1015 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1016 | /* At this point, key material and other type-specific content has |
| 1017 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1018 | * zeroize because the metadata is not particularly sensitive. */ |
| 1019 | memset( slot, 0, sizeof( *slot ) ); |
| 1020 | return( status ); |
| 1021 | } |
| 1022 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1023 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1024 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1025 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1026 | psa_status_t status; /* status of the last operation */ |
| 1027 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1028 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1029 | psa_se_drv_table_entry_t *driver; |
| 1030 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1031 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1032 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1033 | return( PSA_SUCCESS ); |
| 1034 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1035 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1036 | * 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] | 1037 | * key, this will load the key description from persistent memory if not |
| 1038 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1039 | * the key is operated by an SE or not and this information is needed by |
| 1040 | * the current implementation. |
| 1041 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1042 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1043 | if( status != PSA_SUCCESS ) |
| 1044 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1045 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1046 | /* |
| 1047 | * If the key slot containing the key description is under access by the |
| 1048 | * library (apart from the present access), the key cannot be destroyed |
| 1049 | * yet. For the time being, just return in error. Eventually (to be |
| 1050 | * implemented), the key should be destroyed when all accesses have |
| 1051 | * stopped. |
| 1052 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1053 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1054 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1055 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1056 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1057 | } |
| 1058 | |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1059 | if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) ) |
| 1060 | { |
| 1061 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1062 | * if we attempt it, depending on whether the key is merely read-only |
| 1063 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1064 | * Just do the best we can, which is to wipe the copy in memory |
| 1065 | * (done in this function's cleanup code). */ |
| 1066 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1067 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1070 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1071 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1072 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1073 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1074 | /* For a key in a secure element, we need to do three things: |
| 1075 | * remove the key file in internal storage, destroy the |
| 1076 | * key inside the secure element, and update the driver's |
| 1077 | * persistent data. Start a transaction that will encompass these |
| 1078 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1079 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1080 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1081 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1082 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1083 | status = psa_crypto_save_transaction( ); |
| 1084 | if( status != PSA_SUCCESS ) |
| 1085 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1086 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1087 | /* We should still try to destroy the key in the secure |
| 1088 | * element and the key metadata in storage. This is especially |
| 1089 | * important if the error is that the storage is full. |
| 1090 | * But how to do it exactly without risking an inconsistent |
| 1091 | * state after a reset? |
| 1092 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1093 | */ |
| 1094 | overall_status = status; |
| 1095 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1096 | } |
| 1097 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1098 | status = psa_destroy_se_key( driver, |
| 1099 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1100 | if( overall_status == PSA_SUCCESS ) |
| 1101 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1102 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1103 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1104 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1105 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1106 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1107 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1108 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1109 | if( overall_status == PSA_SUCCESS ) |
| 1110 | overall_status = status; |
| 1111 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1112 | /* TODO: other slots may have a copy of the same key. We should |
| 1113 | * invalidate them. |
| 1114 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1115 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1116 | } |
| 1117 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1118 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1119 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1120 | if( driver != NULL ) |
| 1121 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1122 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1123 | if( overall_status == PSA_SUCCESS ) |
| 1124 | overall_status = status; |
| 1125 | status = psa_crypto_stop_transaction( ); |
| 1126 | if( overall_status == PSA_SUCCESS ) |
| 1127 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1128 | } |
| 1129 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1130 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1131 | exit: |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1132 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1133 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1134 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1135 | overall_status = status; |
| 1136 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1139 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1140 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1141 | static psa_status_t psa_get_rsa_public_exponent( |
| 1142 | const mbedtls_rsa_context *rsa, |
| 1143 | psa_key_attributes_t *attributes ) |
| 1144 | { |
| 1145 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1146 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1147 | uint8_t *buffer = NULL; |
| 1148 | size_t buflen; |
| 1149 | mbedtls_mpi_init( &mpi ); |
| 1150 | |
| 1151 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1152 | if( ret != 0 ) |
| 1153 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1154 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1155 | { |
| 1156 | /* It's the default value, which is reported as an empty string, |
| 1157 | * so there's nothing to do. */ |
| 1158 | goto exit; |
| 1159 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1160 | |
| 1161 | buflen = mbedtls_mpi_size( &mpi ); |
| 1162 | buffer = mbedtls_calloc( 1, buflen ); |
| 1163 | if( buffer == NULL ) |
| 1164 | { |
| 1165 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1166 | goto exit; |
| 1167 | } |
| 1168 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1169 | if( ret != 0 ) |
| 1170 | goto exit; |
| 1171 | attributes->domain_parameters = buffer; |
| 1172 | attributes->domain_parameters_size = buflen; |
| 1173 | |
| 1174 | exit: |
| 1175 | mbedtls_mpi_free( &mpi ); |
| 1176 | if( ret != 0 ) |
| 1177 | mbedtls_free( buffer ); |
| 1178 | return( mbedtls_to_psa_error( ret ) ); |
| 1179 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1180 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1181 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1182 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1183 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1184 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1185 | 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] | 1186 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1187 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1188 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1189 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1190 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1191 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1192 | psa_reset_key_attributes( attributes ); |
| 1193 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1194 | 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] | 1195 | if( status != PSA_SUCCESS ) |
| 1196 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1197 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1198 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1199 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1200 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1201 | |
| 1202 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 512ad81 | 2021-08-24 15:50:05 +0200 | [diff] [blame] | 1203 | if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1204 | psa_set_key_slot_number( attributes, |
| 1205 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1206 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1207 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1208 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1209 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1210 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1211 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1212 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1213 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1214 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1215 | * is not yet implemented. |
| 1216 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1217 | */ |
Ronald Cron | 9b8b69c | 2021-08-24 16:00:51 +0200 | [diff] [blame] | 1218 | if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1219 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1220 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1221 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1222 | status = mbedtls_psa_rsa_load_representation( |
| 1223 | slot->attr.type, |
| 1224 | slot->key.data, |
| 1225 | slot->key.bytes, |
| 1226 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1227 | if( status != PSA_SUCCESS ) |
| 1228 | break; |
| 1229 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1230 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1231 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1232 | mbedtls_rsa_free( rsa ); |
| 1233 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1234 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1235 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1236 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1237 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1238 | default: |
| 1239 | /* Nothing else to do. */ |
| 1240 | break; |
| 1241 | } |
| 1242 | |
| 1243 | if( status != PSA_SUCCESS ) |
| 1244 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1245 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1246 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1247 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1248 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1249 | } |
| 1250 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1251 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1252 | psa_status_t psa_get_key_slot_number( |
| 1253 | const psa_key_attributes_t *attributes, |
| 1254 | psa_key_slot_number_t *slot_number ) |
| 1255 | { |
| 1256 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1257 | { |
| 1258 | *slot_number = attributes->slot_number; |
| 1259 | return( PSA_SUCCESS ); |
| 1260 | } |
| 1261 | else |
| 1262 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1263 | } |
| 1264 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1265 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1266 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1267 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1268 | uint8_t *data, |
| 1269 | size_t data_size, |
| 1270 | size_t *data_length ) |
| 1271 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1272 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1273 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1274 | memcpy( data, key_buffer, key_buffer_size ); |
| 1275 | memset( data + key_buffer_size, 0, |
| 1276 | data_size - key_buffer_size ); |
| 1277 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1278 | return( PSA_SUCCESS ); |
| 1279 | } |
| 1280 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1281 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1282 | const psa_key_attributes_t *attributes, |
| 1283 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1284 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1285 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1286 | psa_key_type_t type = attributes->core.type; |
| 1287 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1288 | if( key_type_is_raw_bytes( type ) || |
| 1289 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1290 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1291 | { |
| 1292 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1293 | key_buffer, key_buffer_size, |
| 1294 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1295 | } |
| 1296 | else |
| 1297 | { |
| 1298 | /* This shouldn't happen in the reference implementation, but |
| 1299 | it is valid for a special-purpose implementation to omit |
| 1300 | support for exporting certain key types. */ |
| 1301 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1306 | uint8_t *data, |
| 1307 | size_t data_size, |
| 1308 | size_t *data_length ) |
| 1309 | { |
| 1310 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1311 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1312 | psa_key_slot_t *slot; |
| 1313 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1314 | /* Reject a zero-length output buffer now, since this can never be a |
| 1315 | * valid key representation. This way we know that data must be a valid |
| 1316 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1317 | if( data_size == 0 ) |
| 1318 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1319 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1320 | /* Set the key to empty now, so that even when there are errors, we always |
| 1321 | * set data_length to a value between 0 and data_size. On error, setting |
| 1322 | * the key to empty is a good choice because an empty key representation is |
| 1323 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1324 | *data_length = 0; |
| 1325 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1326 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1327 | * which don't require any flag, but |
| 1328 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1329 | */ |
| 1330 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1331 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1332 | if( status != PSA_SUCCESS ) |
| 1333 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1334 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1335 | psa_key_attributes_t attributes = { |
| 1336 | .core = slot->attr |
| 1337 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1338 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1339 | slot->key.data, slot->key.bytes, |
| 1340 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1341 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1342 | unlock_status = psa_unlock_key_slot( slot ); |
| 1343 | |
| 1344 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1345 | } |
| 1346 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1347 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1348 | const psa_key_attributes_t *attributes, |
| 1349 | const uint8_t *key_buffer, |
| 1350 | size_t key_buffer_size, |
| 1351 | uint8_t *data, |
| 1352 | size_t data_size, |
| 1353 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1354 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1355 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1356 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1357 | 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] | 1358 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1359 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1360 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1361 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1362 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1363 | key_buffer, key_buffer_size, |
| 1364 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1365 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1366 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1367 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1368 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1369 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1370 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1371 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1372 | key_buffer, |
| 1373 | key_buffer_size, |
| 1374 | data, |
| 1375 | data_size, |
| 1376 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1377 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1378 | /* We don't know how to convert a private RSA key to public. */ |
| 1379 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1380 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1381 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1382 | } |
| 1383 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1384 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1385 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1386 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1387 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1388 | key_buffer, |
| 1389 | key_buffer_size, |
| 1390 | data, |
| 1391 | data_size, |
| 1392 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1393 | #else |
| 1394 | /* 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] | 1395 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1396 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1397 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1398 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1399 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1400 | else |
| 1401 | { |
| 1402 | /* This shouldn't happen in the reference implementation, but |
| 1403 | it is valid for a special-purpose implementation to omit |
| 1404 | support for exporting certain key types. */ |
| 1405 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1406 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1407 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1408 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1409 | 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] | 1410 | uint8_t *data, |
| 1411 | size_t data_size, |
| 1412 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1413 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1414 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1415 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1416 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1417 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1418 | /* Reject a zero-length output buffer now, since this can never be a |
| 1419 | * valid key representation. This way we know that data must be a valid |
| 1420 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1421 | if( data_size == 0 ) |
| 1422 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1423 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1424 | /* Set the key to empty now, so that even when there are errors, we always |
| 1425 | * set data_length to a value between 0 and data_size. On error, setting |
| 1426 | * the key to empty is a good choice because an empty key representation is |
| 1427 | * unlikely to be accepted anywhere. */ |
| 1428 | *data_length = 0; |
| 1429 | |
| 1430 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1431 | 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] | 1432 | if( status != PSA_SUCCESS ) |
| 1433 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1434 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1435 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1436 | { |
| 1437 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1438 | goto exit; |
| 1439 | } |
| 1440 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1441 | psa_key_attributes_t attributes = { |
| 1442 | .core = slot->attr |
| 1443 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1444 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1445 | &attributes, slot->key.data, slot->key.bytes, |
| 1446 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1447 | |
| 1448 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1449 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1450 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1451 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1452 | } |
| 1453 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1454 | #if defined(static_assert) |
| 1455 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1456 | "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] | 1457 | 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] | 1458 | "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] | 1459 | 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] | 1460 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1461 | #endif |
| 1462 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1463 | /** Validate that a key policy is internally well-formed. |
| 1464 | * |
| 1465 | * This function only rejects invalid policies. It does not validate the |
| 1466 | * consistency of the policy with respect to other attributes of the key |
| 1467 | * such as the key type. |
| 1468 | */ |
| 1469 | 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] | 1470 | { |
| 1471 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1472 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1473 | PSA_KEY_USAGE_ENCRYPT | |
| 1474 | PSA_KEY_USAGE_DECRYPT | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 1475 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1476 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1477 | PSA_KEY_USAGE_SIGN_HASH | |
| 1478 | PSA_KEY_USAGE_VERIFY_HASH | |
Manuel Pégourié-Gonnard | 805251b | 2021-05-04 09:49:59 +0200 | [diff] [blame] | 1479 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1480 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1481 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1482 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1483 | return( PSA_SUCCESS ); |
| 1484 | } |
| 1485 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1486 | /** Validate the internal consistency of key attributes. |
| 1487 | * |
| 1488 | * This function only rejects invalid attribute values. If does not |
| 1489 | * validate the consistency of the attributes with any key data that may |
| 1490 | * be involved in the creation of the key. |
| 1491 | * |
| 1492 | * Call this function early in the key creation process. |
| 1493 | * |
| 1494 | * \param[in] attributes Key attributes for the new key. |
| 1495 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1496 | * NULL for a transparent key. |
| 1497 | * |
| 1498 | */ |
| 1499 | static psa_status_t psa_validate_key_attributes( |
| 1500 | const psa_key_attributes_t *attributes, |
| 1501 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1502 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1503 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1504 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1505 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1506 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1507 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1508 | if( status != PSA_SUCCESS ) |
| 1509 | return( status ); |
| 1510 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1511 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1512 | if( status != PSA_SUCCESS ) |
| 1513 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1514 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1515 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1516 | { |
| 1517 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1518 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1519 | } |
| 1520 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1521 | { |
Ronald Cron | 77e412c | 2021-03-31 17:36:31 +0200 | [diff] [blame] | 1522 | if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) ) |
| 1523 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1526 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1527 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1528 | return( status ); |
| 1529 | |
| 1530 | /* Refuse to create overly large keys. |
| 1531 | * Note that this doesn't trigger on import if the attributes don't |
| 1532 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1533 | * psa_import_key() needs its own checks. */ |
| 1534 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1535 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1536 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1537 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1538 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1539 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1540 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1541 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1542 | return( PSA_SUCCESS ); |
| 1543 | } |
| 1544 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1545 | /** Prepare a key slot to receive key material. |
| 1546 | * |
| 1547 | * This function allocates a key slot and sets its metadata. |
| 1548 | * |
| 1549 | * If this function fails, call psa_fail_key_creation(). |
| 1550 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1551 | * This function is intended to be used as follows: |
| 1552 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1553 | * it with the specified attributes, and in case of a volatile key assign it |
| 1554 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1555 | * -# Populate the slot with the key material. |
| 1556 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1557 | * In case of failure at any step, stop the sequence and call |
| 1558 | * psa_fail_key_creation(). |
| 1559 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1560 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1561 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1562 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1563 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1564 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1565 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1566 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1567 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1568 | * |
| 1569 | * \retval #PSA_SUCCESS |
| 1570 | * The key slot is ready to receive key material. |
| 1571 | * \return If this function fails, the key slot is an invalid state. |
| 1572 | * 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] | 1573 | */ |
| 1574 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1575 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1576 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1577 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1578 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1579 | { |
| 1580 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1581 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1582 | psa_key_slot_t *slot; |
| 1583 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1584 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1585 | *p_drv = NULL; |
| 1586 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1587 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1588 | if( status != PSA_SUCCESS ) |
| 1589 | return( status ); |
| 1590 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1591 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1592 | if( status != PSA_SUCCESS ) |
| 1593 | return( status ); |
| 1594 | slot = *p_slot; |
| 1595 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1596 | /* We're storing the declared bit-size of the key. It's up to each |
| 1597 | * creation mechanism to verify that this information is correct. |
| 1598 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1599 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1600 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1601 | * volatile key identifier associated to the slot returned to contain its |
| 1602 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1603 | |
| 1604 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1605 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1606 | { |
| 1607 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1608 | slot->attr.id = volatile_key_id; |
| 1609 | #else |
| 1610 | slot->attr.id.key_id = volatile_key_id; |
| 1611 | #endif |
| 1612 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1613 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1614 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1615 | * external-only flags, query `attributes`. Thanks to the check |
| 1616 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1617 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1618 | * may have set. */ |
| 1619 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1620 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1621 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1622 | /* 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] | 1623 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1624 | * create the key file in internal storage, create the |
| 1625 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1626 | * persistent data. This is done by starting a transaction that will |
| 1627 | * encompass these three actions. |
| 1628 | * For registering a volatile key, we just need to find an appropriate |
| 1629 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1630 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1631 | /* The first thing to do is to find a slot number for the new key. |
| 1632 | * We save the slot number in persistent storage as part of the |
| 1633 | * transaction data. It will be needed to recover if the power |
| 1634 | * fails during the key creation process, to clean up on the secure |
| 1635 | * element side after restarting. Obtaining a slot number from the |
| 1636 | * secure element driver updates its persistent state, but we do not yet |
| 1637 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1638 | * 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] | 1639 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1640 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1641 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1642 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1643 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1644 | if( status != PSA_SUCCESS ) |
| 1645 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1646 | |
| 1647 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1648 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1649 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1650 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1651 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1652 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1653 | status = psa_crypto_save_transaction( ); |
| 1654 | if( status != PSA_SUCCESS ) |
| 1655 | { |
| 1656 | (void) psa_crypto_stop_transaction( ); |
| 1657 | return( status ); |
| 1658 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1659 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1660 | |
| 1661 | status = psa_copy_key_material_into_slot( |
| 1662 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1663 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1664 | |
| 1665 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1666 | { |
| 1667 | /* Key registration only makes sense with a secure element. */ |
| 1668 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1669 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1670 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1671 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1672 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1673 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1674 | |
| 1675 | /** Finalize the creation of a key once its key material has been set. |
| 1676 | * |
| 1677 | * This entails writing the key to persistent storage. |
| 1678 | * |
| 1679 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1680 | * See the documentation of psa_start_key_creation() for the intended use |
| 1681 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1682 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1683 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1684 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1685 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1686 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1687 | * \param[in,out] slot Pointer to the slot with key material. |
| 1688 | * \param[in] driver The secure element driver for the key, |
| 1689 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1690 | * \param[out] key On success, identifier of the key. Note that the |
| 1691 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1692 | * |
| 1693 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1694 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1695 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1696 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1697 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1698 | * \retval #PSA_ERROR_DATA_INVALID |
| 1699 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1700 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1701 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1702 | * \return If this function fails, the key slot is an invalid state. |
| 1703 | * 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] | 1704 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1705 | static psa_status_t psa_finish_key_creation( |
| 1706 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1707 | psa_se_drv_table_entry_t *driver, |
| 1708 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1709 | { |
| 1710 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1711 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1712 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1713 | |
| 1714 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1715 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1716 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1717 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1718 | if( driver != NULL ) |
| 1719 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1720 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1721 | psa_key_slot_number_t slot_number = |
| 1722 | psa_key_slot_get_slot_number( slot ) ; |
| 1723 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1724 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1725 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1726 | sizeof( data.slot_number ), |
| 1727 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1728 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1729 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1730 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1731 | (uint8_t*) &data, |
| 1732 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1733 | } |
| 1734 | else |
| 1735 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1736 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1737 | /* Key material is saved in export representation in the slot, so |
| 1738 | * just pass the slot buffer for storage. */ |
| 1739 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1740 | slot->key.data, |
| 1741 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1742 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1743 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1744 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1745 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1746 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1747 | /* Finish the transaction for a key creation. This does not |
| 1748 | * happen when registering an existing key. Detect this case |
| 1749 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1750 | * creation of a persistent key in a secure element requires a transaction, |
| 1751 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1752 | if( driver != NULL && |
| 1753 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1754 | { |
| 1755 | status = psa_save_se_persistent_data( driver ); |
| 1756 | if( status != PSA_SUCCESS ) |
| 1757 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1758 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1759 | return( status ); |
| 1760 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1761 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1762 | } |
| 1763 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1764 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1765 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1766 | { |
| 1767 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1768 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1769 | if( status != PSA_SUCCESS ) |
| 1770 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1771 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1772 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1773 | return( status ); |
| 1774 | } |
| 1775 | |
| 1776 | /** Abort the creation of a key. |
| 1777 | * |
| 1778 | * You may call this function after calling psa_start_key_creation(), |
| 1779 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1780 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1781 | * See the documentation of psa_start_key_creation() for the intended use |
| 1782 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1783 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1784 | * \param[in,out] slot Pointer to the slot with key material. |
| 1785 | * \param[in] driver The secure element driver for the key, |
| 1786 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1787 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1788 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1789 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1790 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1791 | (void) driver; |
| 1792 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1793 | if( slot == NULL ) |
| 1794 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1795 | |
| 1796 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1797 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1798 | * element, and the failure happened later (when saving metadata |
| 1799 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1800 | * element. |
| 1801 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1802 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1803 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1804 | /* Abort the ongoing transaction if any (there may not be one if |
| 1805 | * the creation process failed before starting one, or if the |
| 1806 | * key creation is a registration of a key in a secure element). |
| 1807 | * Earlier functions must already have done what it takes to undo any |
| 1808 | * partial creation. All that's left is to update the transaction data |
| 1809 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1810 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1811 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1812 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1813 | psa_wipe_key_slot( slot ); |
| 1814 | } |
| 1815 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1816 | /** Validate optional attributes during key creation. |
| 1817 | * |
| 1818 | * Some key attributes are optional during key creation. If they are |
| 1819 | * specified in the attributes structure, check that they are consistent |
| 1820 | * with the data in the slot. |
| 1821 | * |
| 1822 | * This function should be called near the end of key creation, after |
| 1823 | * the slot in memory is fully populated but before saving persistent data. |
| 1824 | */ |
| 1825 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1826 | const psa_key_slot_t *slot, |
| 1827 | const psa_key_attributes_t *attributes ) |
| 1828 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1829 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1830 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1831 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1832 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1833 | } |
| 1834 | |
| 1835 | if( attributes->domain_parameters_size != 0 ) |
| 1836 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1837 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1838 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1839 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1840 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1841 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1842 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1843 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1844 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1845 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 1846 | slot->attr.type, |
| 1847 | slot->key.data, |
| 1848 | slot->key.bytes, |
| 1849 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1850 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1851 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1852 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1853 | mbedtls_mpi_init( &actual ); |
| 1854 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1855 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1856 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1857 | mbedtls_rsa_free( rsa ); |
| 1858 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1859 | if( ret != 0 ) |
| 1860 | goto rsa_exit; |
| 1861 | ret = mbedtls_mpi_read_binary( &required, |
| 1862 | attributes->domain_parameters, |
| 1863 | attributes->domain_parameters_size ); |
| 1864 | if( ret != 0 ) |
| 1865 | goto rsa_exit; |
| 1866 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1867 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1868 | rsa_exit: |
| 1869 | mbedtls_mpi_free( &actual ); |
| 1870 | mbedtls_mpi_free( &required ); |
| 1871 | if( ret != 0) |
| 1872 | return( mbedtls_to_psa_error( ret ) ); |
| 1873 | } |
| 1874 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1875 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1876 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1877 | { |
| 1878 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1879 | } |
| 1880 | } |
| 1881 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1882 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1883 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1884 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1885 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1886 | } |
| 1887 | |
| 1888 | return( PSA_SUCCESS ); |
| 1889 | } |
| 1890 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1891 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1892 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1893 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1894 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1895 | { |
| 1896 | psa_status_t status; |
| 1897 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1898 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1899 | size_t bits; |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1900 | size_t storage_size = data_length; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1901 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1902 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1903 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1904 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1905 | * This also rejects any key which might be encoded as an empty string, |
| 1906 | * which is never valid. */ |
| 1907 | if( data_length == 0 ) |
| 1908 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1909 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1910 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1911 | if( data_length > SIZE_MAX / 8 ) |
| 1912 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1913 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1914 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1915 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1916 | if( status != PSA_SUCCESS ) |
| 1917 | goto exit; |
| 1918 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1919 | /* 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] | 1920 | * storage ( thus not in the case of importing a key in a secure element |
| 1921 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 1922 | * buffer to hold the imported key material. */ |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1923 | if( slot->key.data == NULL ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1924 | { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1925 | if( psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 1926 | { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1927 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
| 1928 | attributes, data, data_length, &storage_size ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1929 | if( status != PSA_SUCCESS ) |
| 1930 | goto exit; |
| 1931 | } |
| 1932 | status = psa_allocate_buffer_to_slot( slot, storage_size ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1933 | if( status != PSA_SUCCESS ) |
| 1934 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1935 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1936 | |
| 1937 | bits = slot->attr.bits; |
| 1938 | status = psa_driver_wrapper_import_key( attributes, |
| 1939 | data, data_length, |
| 1940 | slot->key.data, |
| 1941 | slot->key.bytes, |
| 1942 | &slot->key.bytes, &bits ); |
| 1943 | if( status != PSA_SUCCESS ) |
| 1944 | goto exit; |
| 1945 | |
| 1946 | if( slot->attr.bits == 0 ) |
| 1947 | slot->attr.bits = (psa_key_bits_t) bits; |
| 1948 | else if( bits != slot->attr.bits ) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1949 | { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1950 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1951 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1952 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 1953 | |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1954 | /* Enforce a size limit, and in particular ensure that the bit |
| 1955 | * size fits in its representation type.*/ |
| 1956 | if( bits > PSA_MAX_KEY_BITS ) |
| 1957 | { |
| 1958 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1959 | goto exit; |
| 1960 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1961 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1962 | if( status != PSA_SUCCESS ) |
| 1963 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1964 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1965 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1966 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1967 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1968 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1969 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1970 | return( status ); |
| 1971 | } |
| 1972 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1973 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1974 | psa_status_t mbedtls_psa_register_se_key( |
| 1975 | const psa_key_attributes_t *attributes ) |
| 1976 | { |
| 1977 | psa_status_t status; |
| 1978 | psa_key_slot_t *slot = NULL; |
| 1979 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1980 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1981 | |
| 1982 | /* Leaving attributes unspecified is not currently supported. |
| 1983 | * It could make sense to query the key type and size from the |
| 1984 | * secure element, but not all secure elements support this |
| 1985 | * and the driver HAL doesn't currently support it. */ |
| 1986 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 1987 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1988 | if( psa_get_key_bits( attributes ) == 0 ) |
| 1989 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1990 | |
| 1991 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1992 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1993 | if( status != PSA_SUCCESS ) |
| 1994 | goto exit; |
| 1995 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1996 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1997 | |
| 1998 | exit: |
| 1999 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2000 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2001 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2002 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2003 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2004 | return( status ); |
| 2005 | } |
| 2006 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2007 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2008 | 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] | 2009 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2010 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2011 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2012 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2013 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2014 | psa_key_slot_t *source_slot = NULL; |
| 2015 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2016 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2017 | psa_se_drv_table_entry_t *driver = NULL; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2018 | size_t storage_size = 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2019 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2020 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2021 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2022 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2023 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2024 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2025 | goto exit; |
| 2026 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2027 | status = psa_validate_optional_attributes( source_slot, |
| 2028 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2029 | if( status != PSA_SUCCESS ) |
| 2030 | goto exit; |
| 2031 | |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2032 | /* The target key type and number of bits have been validated by |
| 2033 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2034 | * equal to the ones of the source key. So it is safe to inherit |
| 2035 | * them from the source key now." |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2036 | * */ |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2037 | actual_attributes.core.bits = source_slot->attr.bits; |
| 2038 | actual_attributes.core.type = source_slot->attr.type; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2039 | |
| 2040 | |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 2041 | status = psa_restrict_key_policy( source_slot->attr.type, |
| 2042 | &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2043 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2044 | if( status != PSA_SUCCESS ) |
| 2045 | goto exit; |
| 2046 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2047 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2048 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2049 | if( status != PSA_SUCCESS ) |
| 2050 | goto exit; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2051 | if( PSA_KEY_LIFETIME_GET_LOCATION( target_slot->attr.lifetime ) != |
| 2052 | PSA_KEY_LIFETIME_GET_LOCATION( source_slot->attr.lifetime ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2053 | { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2054 | /* |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2055 | * If the source and target keys are stored in different locations, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2056 | * the source key would need to be exported as plaintext and re-imported |
| 2057 | * in the other location. This has security implications which have not |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2058 | * been fully mapped. For now, this can be achieved through |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2059 | * appropriate API invocations from the application, if needed. |
| 2060 | * */ |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2061 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2062 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2063 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2064 | /* |
| 2065 | * When the source and target keys are within the same location, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2066 | * - For transparent keys it is a blind copy without any driver invocation, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2067 | * - For opaque keys this translates to an invocation of the drivers' |
| 2068 | * copy_key entry point through the dispatch layer. |
| 2069 | * */ |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2070 | if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) ) |
| 2071 | { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2072 | status = psa_driver_wrapper_get_key_buffer_size( &actual_attributes, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2073 | &storage_size ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2074 | if( status != PSA_SUCCESS ) |
| 2075 | goto exit; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2076 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2077 | status = psa_allocate_buffer_to_slot( target_slot, storage_size ); |
| 2078 | if( status != PSA_SUCCESS ) |
| 2079 | goto exit; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2080 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2081 | status = psa_driver_wrapper_copy_key( &actual_attributes, |
| 2082 | source_slot->key.data, |
| 2083 | source_slot->key.bytes, |
| 2084 | target_slot->key.data, |
| 2085 | target_slot->key.bytes, |
| 2086 | &target_slot->key.bytes ); |
| 2087 | if( status != PSA_SUCCESS ) |
| 2088 | goto exit; |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2089 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2090 | else |
| 2091 | { |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2092 | status = psa_copy_key_material_into_slot( target_slot, |
| 2093 | source_slot->key.data, |
| 2094 | source_slot->key.bytes ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2095 | if( status != PSA_SUCCESS ) |
| 2096 | goto exit; |
| 2097 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2098 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2099 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2100 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2101 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2102 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2103 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2104 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2105 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2106 | } |
| 2107 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2108 | |
| 2109 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2110 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2111 | /* Message digests */ |
| 2112 | /****************************************************************/ |
| 2113 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2114 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2115 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2116 | /* Aborting a non-active operation is allowed */ |
| 2117 | if( operation->id == 0 ) |
| 2118 | return( PSA_SUCCESS ); |
| 2119 | |
| 2120 | psa_status_t status = psa_driver_wrapper_hash_abort( operation ); |
| 2121 | operation->id = 0; |
| 2122 | |
| 2123 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2124 | } |
| 2125 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2126 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2127 | psa_algorithm_t alg ) |
| 2128 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2129 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2130 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2131 | /* A context must be freshly initialized before it can be set up. */ |
| 2132 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2133 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2134 | status = PSA_ERROR_BAD_STATE; |
| 2135 | goto exit; |
| 2136 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2137 | |
| 2138 | if( !PSA_ALG_IS_HASH( alg ) ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2139 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2140 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2141 | goto exit; |
| 2142 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2143 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2144 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2145 | * directly zeroes the int-sized dummy member of the context union. */ |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2146 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 2147 | |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2148 | status = psa_driver_wrapper_hash_setup( operation, alg ); |
| 2149 | |
| 2150 | exit: |
| 2151 | if( status != PSA_SUCCESS ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2152 | psa_hash_abort( operation ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2153 | |
| 2154 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2158 | const uint8_t *input, |
| 2159 | size_t input_length ) |
| 2160 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2161 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2162 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2163 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2164 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2165 | status = PSA_ERROR_BAD_STATE; |
| 2166 | goto exit; |
| 2167 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2168 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2169 | /* Don't require hash implementations to behave correctly on a |
| 2170 | * zero-length input, which may have an invalid pointer. */ |
| 2171 | if( input_length == 0 ) |
| 2172 | return( PSA_SUCCESS ); |
| 2173 | |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2174 | status = psa_driver_wrapper_hash_update( operation, input, input_length ); |
| 2175 | |
| 2176 | exit: |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2177 | if( status != PSA_SUCCESS ) |
| 2178 | psa_hash_abort( operation ); |
| 2179 | |
| 2180 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2184 | uint8_t *hash, |
| 2185 | size_t hash_size, |
| 2186 | size_t *hash_length ) |
| 2187 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2188 | *hash_length = 0; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2189 | if( operation->id == 0 ) |
| 2190 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2191 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2192 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2193 | operation, hash, hash_size, hash_length ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2194 | psa_hash_abort( operation ); |
| 2195 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2196 | } |
| 2197 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2198 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2199 | const uint8_t *hash, |
| 2200 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2201 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2202 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2203 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2204 | psa_status_t status = psa_hash_finish( |
| 2205 | operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2206 | actual_hash, sizeof( actual_hash ), |
| 2207 | &actual_hash_length ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2208 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2209 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2210 | goto exit; |
| 2211 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2212 | if( actual_hash_length != hash_length ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2213 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2214 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2215 | goto exit; |
| 2216 | } |
| 2217 | |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2218 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2219 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2220 | |
| 2221 | exit: |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2222 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2223 | if( status != PSA_SUCCESS ) |
| 2224 | psa_hash_abort(operation); |
| 2225 | |
| 2226 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2227 | } |
| 2228 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2229 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2230 | const uint8_t *input, size_t input_length, |
| 2231 | uint8_t *hash, size_t hash_size, |
| 2232 | size_t *hash_length ) |
| 2233 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2234 | *hash_length = 0; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2235 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2236 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2237 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2238 | return( psa_driver_wrapper_hash_compute( alg, input, input_length, |
| 2239 | hash, hash_size, hash_length ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2243 | const uint8_t *input, size_t input_length, |
| 2244 | const uint8_t *hash, size_t hash_length ) |
| 2245 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2246 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2247 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2248 | |
| 2249 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2250 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2251 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2252 | psa_status_t status = psa_driver_wrapper_hash_compute( |
| 2253 | alg, input, input_length, |
| 2254 | actual_hash, sizeof(actual_hash), |
| 2255 | &actual_hash_length ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2256 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2257 | goto exit; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2258 | if( actual_hash_length != hash_length ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2259 | { |
| 2260 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2261 | goto exit; |
| 2262 | } |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2263 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2264 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2265 | |
| 2266 | exit: |
| 2267 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
| 2268 | return( status ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2269 | } |
| 2270 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2271 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2272 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2273 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2274 | if( source_operation->id == 0 || |
| 2275 | target_operation->id != 0 ) |
| 2276 | { |
| 2277 | return( PSA_ERROR_BAD_STATE ); |
| 2278 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2279 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2280 | psa_status_t status = psa_driver_wrapper_hash_clone( source_operation, |
| 2281 | target_operation ); |
| 2282 | if( status != PSA_SUCCESS ) |
| 2283 | psa_hash_abort( target_operation ); |
| 2284 | |
| 2285 | return( status ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2286 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2287 | |
| 2288 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2289 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2290 | /* MAC */ |
| 2291 | /****************************************************************/ |
| 2292 | |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2293 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2294 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2295 | /* Aborting a non-active operation is allowed */ |
| 2296 | if( operation->id == 0 ) |
| 2297 | return( PSA_SUCCESS ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2298 | |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2299 | psa_status_t status = psa_driver_wrapper_mac_abort( operation ); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2300 | operation->mac_size = 0; |
| 2301 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2302 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2303 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2304 | return( status ); |
| 2305 | } |
| 2306 | |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2307 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2308 | psa_algorithm_t alg, |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2309 | const psa_key_attributes_t *attributes, |
| 2310 | uint8_t *mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2311 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2312 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2313 | psa_key_type_t key_type = psa_get_key_type( attributes ); |
| 2314 | size_t key_bits = psa_get_key_bits( attributes ); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2315 | |
Ronald Cron | 28ea050 | 2021-06-17 16:10:24 +0200 | [diff] [blame] | 2316 | if( ! PSA_ALG_IS_MAC( alg ) ) |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2317 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2318 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2319 | /* Validate the combination of key type and algorithm */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2320 | status = psa_mac_key_can_do( alg, key_type ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2321 | if( status != PSA_SUCCESS ) |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2322 | return( status ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2323 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2324 | /* Get the output length for the algorithm and key combination */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2325 | *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2326 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2327 | if( *mac_size < 4 ) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2328 | { |
| 2329 | /* A very short MAC is too short for security since it can be |
| 2330 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2331 | * so we make this our minimum, even though 32 bits is still |
| 2332 | * too small for security. */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2333 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2334 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2335 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2336 | if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits, |
| 2337 | PSA_ALG_FULL_LENGTH_MAC( alg ) ) ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2338 | { |
| 2339 | /* It's impossible to "truncate" to a larger length than the full length |
| 2340 | * of the algorithm. */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2341 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2342 | } |
| 2343 | |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2344 | if( *mac_size > PSA_MAC_MAX_SIZE ) |
| 2345 | { |
| 2346 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2347 | * that is disabled in the compile-time configuration. The result can |
| 2348 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2349 | * configuration into account. In this case, force a return of |
| 2350 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2351 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2352 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2353 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2354 | * systematically generated tests. */ |
| 2355 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2356 | } |
| 2357 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2358 | return( PSA_SUCCESS ); |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2361 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
| 2362 | mbedtls_svc_key_id_t key, |
| 2363 | psa_algorithm_t alg, |
| 2364 | int is_sign ) |
| 2365 | { |
| 2366 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2367 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2368 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2369 | |
| 2370 | /* A context must be freshly initialized before it can be set up. */ |
| 2371 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2372 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2373 | status = PSA_ERROR_BAD_STATE; |
| 2374 | goto exit; |
| 2375 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2376 | |
| 2377 | status = psa_get_and_lock_key_slot_with_policy( |
| 2378 | key, |
| 2379 | &slot, |
Mateusz Starzyk | ce0e6a9 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2380 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2381 | alg ); |
| 2382 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2383 | goto exit; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2384 | |
| 2385 | psa_key_attributes_t attributes = { |
| 2386 | .core = slot->attr |
| 2387 | }; |
| 2388 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2389 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2390 | &operation->mac_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2391 | if( status != PSA_SUCCESS ) |
| 2392 | goto exit; |
| 2393 | |
| 2394 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2395 | /* Dispatch the MAC setup call with validated input */ |
| 2396 | if( is_sign ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2397 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2398 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 2399 | &attributes, |
| 2400 | slot->key.data, |
| 2401 | slot->key.bytes, |
| 2402 | alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2403 | } |
| 2404 | else |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2405 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2406 | status = psa_driver_wrapper_mac_verify_setup( operation, |
| 2407 | &attributes, |
| 2408 | slot->key.data, |
| 2409 | slot->key.bytes, |
| 2410 | alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2411 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2412 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2413 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2414 | if( status != PSA_SUCCESS ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2415 | psa_mac_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2416 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2417 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2418 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2419 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2420 | } |
| 2421 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2422 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2423 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2424 | psa_algorithm_t alg ) |
| 2425 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2426 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2430 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2431 | psa_algorithm_t alg ) |
| 2432 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2433 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2434 | } |
| 2435 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2436 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2437 | const uint8_t *input, |
| 2438 | size_t input_length ) |
| 2439 | { |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2440 | if( operation->id == 0 ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2441 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2442 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2443 | /* Don't require hash implementations to behave correctly on a |
| 2444 | * zero-length input, which may have an invalid pointer. */ |
| 2445 | if( input_length == 0 ) |
| 2446 | return( PSA_SUCCESS ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2447 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2448 | psa_status_t status = psa_driver_wrapper_mac_update( operation, |
| 2449 | input, input_length ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2450 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2451 | psa_mac_abort( operation ); |
| 2452 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2453 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2454 | } |
| 2455 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2456 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2457 | uint8_t *mac, |
| 2458 | size_t mac_size, |
| 2459 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2460 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2461 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2462 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2463 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2464 | if( operation->id == 0 ) |
Dave Rodgman | 8036bdd | 2021-06-24 16:19:08 +0100 | [diff] [blame] | 2465 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2466 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2467 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2468 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2469 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2470 | if( ! operation->is_sign ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2471 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2472 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2473 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2474 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2475 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2476 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2477 | * once all the error checks are done. */ |
| 2478 | if( operation->mac_size == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2479 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2480 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2481 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2482 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2483 | |
| 2484 | if( mac_size < operation->mac_size ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2485 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2486 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2487 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2488 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2489 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2490 | status = psa_driver_wrapper_mac_sign_finish( operation, |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2491 | mac, operation->mac_size, |
| 2492 | mac_length ); |
| 2493 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2494 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2495 | /* In case of success, set the potential excess room in the output buffer |
| 2496 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2497 | * In case of error, set the output length and content to a safe default, |
| 2498 | * such that in case the caller misses an error check, the output would be |
| 2499 | * an unachievable MAC. |
| 2500 | */ |
| 2501 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2502 | { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2503 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2504 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2505 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2506 | |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2507 | if( mac_size > operation->mac_size ) |
| 2508 | memset( &mac[operation->mac_size], '!', |
| 2509 | mac_size - operation->mac_size ); |
| 2510 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2511 | abort_status = psa_mac_abort( operation ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2512 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2513 | return( status == PSA_SUCCESS ? abort_status : status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2514 | } |
| 2515 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2516 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2517 | const uint8_t *mac, |
| 2518 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2519 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2520 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2521 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2522 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2523 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2524 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2525 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2526 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2527 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2528 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2529 | if( operation->is_sign ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2530 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2531 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2532 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2533 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2534 | |
| 2535 | if( operation->mac_size != mac_length ) |
| 2536 | { |
| 2537 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2538 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2539 | } |
| 2540 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2541 | status = psa_driver_wrapper_mac_verify_finish( operation, |
| 2542 | mac, mac_length ); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2543 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2544 | exit: |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2545 | abort_status = psa_mac_abort( operation ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2546 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2547 | return( status == PSA_SUCCESS ? abort_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2548 | } |
| 2549 | |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2550 | static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key, |
| 2551 | psa_algorithm_t alg, |
| 2552 | const uint8_t *input, |
| 2553 | size_t input_length, |
| 2554 | uint8_t *mac, |
| 2555 | size_t mac_size, |
| 2556 | size_t *mac_length, |
| 2557 | int is_sign ) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2558 | { |
| 2559 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2560 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2561 | psa_key_slot_t *slot; |
| 2562 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2563 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2564 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | ce0e6a9 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2565 | key, |
| 2566 | &slot, |
| 2567 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2568 | alg ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2569 | if( status != PSA_SUCCESS ) |
| 2570 | goto exit; |
| 2571 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2572 | psa_key_attributes_t attributes = { |
| 2573 | .core = slot->attr |
| 2574 | }; |
| 2575 | |
| 2576 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2577 | &operation_mac_size ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2578 | if( status != PSA_SUCCESS ) |
| 2579 | goto exit; |
| 2580 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2581 | if( mac_size < operation_mac_size ) |
| 2582 | { |
| 2583 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2584 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | status = psa_driver_wrapper_mac_compute( |
| 2588 | &attributes, |
| 2589 | slot->key.data, slot->key.bytes, |
| 2590 | alg, |
| 2591 | input, input_length, |
| 2592 | mac, operation_mac_size, mac_length ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2593 | |
| 2594 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2595 | /* In case of success, set the potential excess room in the output buffer |
| 2596 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2597 | * In case of error, set the output length and content to a safe default, |
| 2598 | * such that in case the caller misses an error check, the output would be |
| 2599 | * an unachievable MAC. |
| 2600 | */ |
| 2601 | if( status != PSA_SUCCESS ) |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2602 | { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2603 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2604 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2605 | } |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2606 | if( mac_size > operation_mac_size ) |
| 2607 | memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2608 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2609 | unlock_status = psa_unlock_key_slot( slot ); |
| 2610 | |
| 2611 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2612 | } |
| 2613 | |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2614 | psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key, |
| 2615 | psa_algorithm_t alg, |
| 2616 | const uint8_t *input, |
| 2617 | size_t input_length, |
| 2618 | uint8_t *mac, |
| 2619 | size_t mac_size, |
| 2620 | size_t *mac_length) |
| 2621 | { |
| 2622 | return( psa_mac_compute_internal( key, alg, |
| 2623 | input, input_length, |
| 2624 | mac, mac_size, mac_length, 1 ) ); |
| 2625 | } |
| 2626 | |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2627 | psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key, |
| 2628 | psa_algorithm_t alg, |
| 2629 | const uint8_t *input, |
| 2630 | size_t input_length, |
| 2631 | const uint8_t *mac, |
| 2632 | size_t mac_length) |
| 2633 | { |
| 2634 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2635 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2636 | size_t actual_mac_length; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2637 | |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2638 | status = psa_mac_compute_internal( key, alg, |
| 2639 | input, input_length, |
| 2640 | actual_mac, sizeof( actual_mac ), |
| 2641 | &actual_mac_length, 0 ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2642 | if( status != PSA_SUCCESS ) |
| 2643 | goto exit; |
| 2644 | |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2645 | if( mac_length != actual_mac_length ) |
| 2646 | { |
| 2647 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2648 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2649 | } |
| 2650 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 ) |
| 2651 | { |
| 2652 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2653 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2654 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2655 | |
| 2656 | exit: |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2657 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2658 | |
| 2659 | return ( status ); |
| 2660 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2661 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2662 | /****************************************************************/ |
| 2663 | /* Asymmetric cryptography */ |
| 2664 | /****************************************************************/ |
| 2665 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2666 | 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] | 2667 | psa_algorithm_t alg ) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2668 | { |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2669 | if( input_is_message ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2670 | { |
| 2671 | if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) ) |
| 2672 | return( PSA_ERROR_INVALID_ARGUMENT ); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2673 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2674 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2675 | { |
| 2676 | if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) ) |
| 2677 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2678 | } |
| 2679 | } |
| 2680 | else |
| 2681 | { |
Mateusz Starzyk | e6d3eda | 2021-08-26 11:46:14 +0200 | [diff] [blame] | 2682 | if( ! PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2683 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2684 | } |
| 2685 | |
| 2686 | return( PSA_SUCCESS ); |
| 2687 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2688 | |
| 2689 | 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] | 2690 | int input_is_message, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2691 | psa_algorithm_t alg, |
| 2692 | const uint8_t * input, |
| 2693 | size_t input_length, |
| 2694 | uint8_t * signature, |
| 2695 | size_t signature_size, |
| 2696 | size_t * signature_length ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2697 | { |
| 2698 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2699 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2700 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2701 | |
| 2702 | *signature_length = 0; |
| 2703 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2704 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2705 | if( status != PSA_SUCCESS ) |
| 2706 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2707 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2708 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2709 | * 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] | 2710 | * buffer can in principle be empty since it doesn't actually have |
| 2711 | * to be a hash.) */ |
| 2712 | if( signature_size == 0 ) |
| 2713 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2714 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2715 | status = psa_get_and_lock_key_slot_with_policy( |
| 2716 | key, &slot, |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2717 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2718 | PSA_KEY_USAGE_SIGN_HASH, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2719 | alg ); |
| 2720 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2721 | if( status != PSA_SUCCESS ) |
| 2722 | goto exit; |
| 2723 | |
| 2724 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
| 2725 | { |
| 2726 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2727 | goto exit; |
| 2728 | } |
| 2729 | |
| 2730 | psa_key_attributes_t attributes = { |
| 2731 | .core = slot->attr |
| 2732 | }; |
| 2733 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2734 | if( input_is_message ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2735 | { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2736 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2737 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2738 | alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2739 | signature, signature_size, signature_length ); |
| 2740 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2741 | else |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2742 | { |
| 2743 | |
| 2744 | status = psa_driver_wrapper_sign_hash( |
| 2745 | &attributes, slot->key.data, slot->key.bytes, |
| 2746 | alg, input, input_length, |
| 2747 | signature, signature_size, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2748 | } |
| 2749 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2750 | |
| 2751 | exit: |
| 2752 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2753 | * the trailing part on success) with something that isn't a valid signature |
| 2754 | * (barring an attack on the signature and deliberately-crafted input), |
| 2755 | * in case the caller doesn't check the return status properly. */ |
| 2756 | if( status == PSA_SUCCESS ) |
| 2757 | memset( signature + *signature_length, '!', |
| 2758 | signature_size - *signature_length ); |
| 2759 | else |
| 2760 | memset( signature, '!', signature_size ); |
| 2761 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2762 | * memset because signature may be NULL in this case. */ |
| 2763 | |
| 2764 | unlock_status = psa_unlock_key_slot( slot ); |
| 2765 | |
| 2766 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 2767 | } |
| 2768 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2769 | 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] | 2770 | int input_is_message, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2771 | psa_algorithm_t alg, |
| 2772 | const uint8_t * input, |
| 2773 | size_t input_length, |
| 2774 | const uint8_t * signature, |
| 2775 | size_t signature_length ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2776 | { |
| 2777 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2778 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2779 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2780 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2781 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2782 | if( status != PSA_SUCCESS ) |
| 2783 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2784 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2785 | status = psa_get_and_lock_key_slot_with_policy( |
| 2786 | key, &slot, |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2787 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2788 | PSA_KEY_USAGE_VERIFY_HASH, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2789 | alg ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2790 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2791 | if( status != PSA_SUCCESS ) |
| 2792 | return( status ); |
| 2793 | |
| 2794 | psa_key_attributes_t attributes = { |
| 2795 | .core = slot->attr |
| 2796 | }; |
| 2797 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2798 | if( input_is_message ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2799 | { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2800 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2801 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2802 | alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2803 | signature, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2804 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2805 | else |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2806 | { |
| 2807 | status = psa_driver_wrapper_verify_hash( |
| 2808 | &attributes, slot->key.data, slot->key.bytes, |
| 2809 | alg, input, input_length, |
| 2810 | signature, signature_length ); |
| 2811 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2812 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2813 | unlock_status = psa_unlock_key_slot( slot ); |
| 2814 | |
| 2815 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2816 | |
| 2817 | } |
| 2818 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2819 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2820 | const psa_key_attributes_t *attributes, |
| 2821 | const uint8_t *key_buffer, |
| 2822 | size_t key_buffer_size, |
| 2823 | psa_algorithm_t alg, |
| 2824 | const uint8_t *input, |
| 2825 | size_t input_length, |
| 2826 | uint8_t *signature, |
| 2827 | size_t signature_size, |
| 2828 | size_t *signature_length ) |
| 2829 | { |
| 2830 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2831 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2832 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2833 | { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2834 | size_t hash_length; |
| 2835 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2836 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2837 | status = psa_driver_wrapper_hash_compute( |
| 2838 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2839 | input, input_length, |
| 2840 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2841 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2842 | if( status != PSA_SUCCESS ) |
| 2843 | return status; |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2844 | |
| 2845 | return psa_driver_wrapper_sign_hash( |
| 2846 | attributes, key_buffer, key_buffer_size, |
| 2847 | alg, hash, hash_length, |
| 2848 | signature, signature_size, signature_length ); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2849 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2850 | |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2851 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2852 | } |
| 2853 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2854 | psa_status_t psa_sign_message( mbedtls_svc_key_id_t key, |
| 2855 | psa_algorithm_t alg, |
| 2856 | const uint8_t * input, |
| 2857 | size_t input_length, |
| 2858 | uint8_t * signature, |
| 2859 | size_t signature_size, |
| 2860 | size_t * signature_length ) |
| 2861 | { |
| 2862 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2863 | key, 1, alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2864 | signature, signature_size, signature_length ); |
| 2865 | } |
| 2866 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2867 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2868 | const psa_key_attributes_t *attributes, |
| 2869 | const uint8_t *key_buffer, |
| 2870 | size_t key_buffer_size, |
| 2871 | psa_algorithm_t alg, |
| 2872 | const uint8_t *input, |
| 2873 | size_t input_length, |
| 2874 | const uint8_t *signature, |
| 2875 | size_t signature_length ) |
| 2876 | { |
| 2877 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2878 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2879 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2880 | { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2881 | size_t hash_length; |
| 2882 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2883 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2884 | status = psa_driver_wrapper_hash_compute( |
| 2885 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2886 | input, input_length, |
| 2887 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2888 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2889 | if( status != PSA_SUCCESS ) |
| 2890 | return status; |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2891 | |
| 2892 | return psa_driver_wrapper_verify_hash( |
| 2893 | attributes, key_buffer, key_buffer_size, |
| 2894 | alg, hash, hash_length, |
| 2895 | signature, signature_length ); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2896 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2897 | |
gabor-mezei-arm | 474a35f | 2021-05-05 13:59:01 +0200 | [diff] [blame] | 2898 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2901 | psa_status_t psa_verify_message( mbedtls_svc_key_id_t key, |
| 2902 | psa_algorithm_t alg, |
| 2903 | const uint8_t * input, |
| 2904 | size_t input_length, |
| 2905 | const uint8_t * signature, |
| 2906 | size_t signature_length ) |
| 2907 | { |
| 2908 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2909 | key, 1, alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2910 | signature, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2911 | } |
| 2912 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2913 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2914 | const psa_key_attributes_t *attributes, |
| 2915 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2916 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2917 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2918 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2919 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2920 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2921 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2922 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2923 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2924 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2925 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2926 | return( mbedtls_psa_rsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2927 | attributes, |
| 2928 | key_buffer, key_buffer_size, |
| 2929 | alg, hash, hash_length, |
| 2930 | signature, signature_size, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2931 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2932 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2933 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2934 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2935 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2936 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2937 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2938 | } |
Ronald Cron | 81ca97e | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 2939 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2940 | { |
| 2941 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 2942 | { |
| 2943 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2944 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2945 | return( mbedtls_psa_ecdsa_sign_hash( |
| 2946 | attributes, |
| 2947 | key_buffer, key_buffer_size, |
| 2948 | alg, hash, hash_length, |
| 2949 | signature, signature_size, signature_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2950 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2951 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2952 | } |
| 2953 | else |
| 2954 | { |
| 2955 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2956 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2957 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2958 | |
| 2959 | (void)key_buffer; |
| 2960 | (void)key_buffer_size; |
| 2961 | (void)hash; |
| 2962 | (void)hash_length; |
| 2963 | (void)signature; |
| 2964 | (void)signature_size; |
| 2965 | (void)signature_length; |
| 2966 | |
| 2967 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2968 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2969 | |
| 2970 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
| 2971 | psa_algorithm_t alg, |
| 2972 | const uint8_t *hash, |
| 2973 | size_t hash_length, |
| 2974 | uint8_t *signature, |
| 2975 | size_t signature_size, |
| 2976 | size_t *signature_length ) |
| 2977 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2978 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2979 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 2980 | signature, signature_size, signature_length ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2981 | } |
| 2982 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2983 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2984 | const psa_key_attributes_t *attributes, |
| 2985 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2986 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2987 | const uint8_t *signature, size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2988 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2989 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2990 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2991 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2992 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2993 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2994 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2995 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2996 | return( mbedtls_psa_rsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2997 | attributes, |
| 2998 | key_buffer, key_buffer_size, |
| 2999 | alg, hash, hash_length, |
| 3000 | signature, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3001 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3002 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3003 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3004 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3005 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3006 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3007 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3008 | } |
Ronald Cron | 81ca97e | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 3009 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3010 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3011 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 3012 | { |
| 3013 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3014 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3015 | return( mbedtls_psa_ecdsa_verify_hash( |
| 3016 | attributes, |
| 3017 | key_buffer, key_buffer_size, |
| 3018 | alg, hash, hash_length, |
| 3019 | signature, signature_length ) ); |
| 3020 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3021 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3022 | } |
| 3023 | else |
| 3024 | { |
| 3025 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3026 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3027 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3028 | |
| 3029 | (void)key_buffer; |
| 3030 | (void)key_buffer_size; |
| 3031 | (void)hash; |
| 3032 | (void)hash_length; |
| 3033 | (void)signature; |
| 3034 | (void)signature_length; |
| 3035 | |
| 3036 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3037 | } |
| 3038 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3039 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
| 3040 | psa_algorithm_t alg, |
| 3041 | const uint8_t *hash, |
| 3042 | size_t hash_length, |
| 3043 | const uint8_t *signature, |
| 3044 | size_t signature_length ) |
| 3045 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3046 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3047 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3048 | signature, signature_length ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3049 | } |
| 3050 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3051 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3052 | psa_algorithm_t alg, |
| 3053 | const uint8_t *input, |
| 3054 | size_t input_length, |
| 3055 | const uint8_t *salt, |
| 3056 | size_t salt_length, |
| 3057 | uint8_t *output, |
| 3058 | size_t output_size, |
| 3059 | size_t *output_length ) |
| 3060 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3061 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3062 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3063 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3064 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3065 | (void) input; |
| 3066 | (void) input_length; |
| 3067 | (void) salt; |
| 3068 | (void) output; |
| 3069 | (void) output_size; |
| 3070 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3071 | *output_length = 0; |
| 3072 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3073 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3074 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3075 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3076 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3077 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3078 | if( status != PSA_SUCCESS ) |
| 3079 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3080 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3081 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3082 | { |
| 3083 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3084 | goto exit; |
| 3085 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3086 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3087 | psa_key_attributes_t attributes = { |
| 3088 | .core = slot->attr |
| 3089 | }; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3090 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3091 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3092 | &attributes, slot->key.data, slot->key.bytes, |
| 3093 | alg, input, input_length, salt, salt_length, |
| 3094 | output, output_size, output_length ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3095 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3096 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3097 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3098 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3099 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3100 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3101 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3102 | psa_algorithm_t alg, |
| 3103 | const uint8_t *input, |
| 3104 | size_t input_length, |
| 3105 | const uint8_t *salt, |
| 3106 | size_t salt_length, |
| 3107 | uint8_t *output, |
| 3108 | size_t output_size, |
| 3109 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3110 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3111 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3112 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3113 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3114 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3115 | (void) input; |
| 3116 | (void) input_length; |
| 3117 | (void) salt; |
| 3118 | (void) output; |
| 3119 | (void) output_size; |
| 3120 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3121 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3122 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3123 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3124 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3125 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3126 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3127 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3128 | if( status != PSA_SUCCESS ) |
| 3129 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3130 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3131 | { |
| 3132 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3133 | goto exit; |
| 3134 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3135 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3136 | psa_key_attributes_t attributes = { |
| 3137 | .core = slot->attr |
| 3138 | }; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3139 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3140 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3141 | &attributes, slot->key.data, slot->key.bytes, |
| 3142 | alg, input, input_length, salt, salt_length, |
| 3143 | output, output_size, output_length ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3144 | |
| 3145 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3146 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3147 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3148 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3149 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3150 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3151 | |
| 3152 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3153 | /****************************************************************/ |
| 3154 | /* Symmetric cryptography */ |
| 3155 | /****************************************************************/ |
| 3156 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3157 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
| 3158 | mbedtls_svc_key_id_t key, |
| 3159 | psa_algorithm_t alg, |
| 3160 | mbedtls_operation_t cipher_operation ) |
| 3161 | { |
| 3162 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3163 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3164 | psa_key_slot_t *slot = NULL; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3165 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3166 | PSA_KEY_USAGE_ENCRYPT : |
| 3167 | PSA_KEY_USAGE_DECRYPT ); |
| 3168 | |
| 3169 | /* A context must be freshly initialized before it can be set up. */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3170 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3171 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3172 | status = PSA_ERROR_BAD_STATE; |
| 3173 | goto exit; |
| 3174 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3175 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3176 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3177 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3178 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3179 | goto exit; |
| 3180 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3181 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3182 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg ); |
| 3183 | if( status != PSA_SUCCESS ) |
| 3184 | goto exit; |
| 3185 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3186 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3187 | * 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] | 3188 | * so we only set it (in the driver wrapper) after resources have been |
| 3189 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3190 | operation->iv_set = 0; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3191 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3192 | operation->iv_required = 0; |
| 3193 | else |
| 3194 | operation->iv_required = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3195 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3196 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3197 | psa_key_attributes_t attributes = { |
| 3198 | .core = slot->attr |
| 3199 | }; |
| 3200 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3201 | /* Try doing the operation through a driver before using software fallback. */ |
| 3202 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3203 | status = psa_driver_wrapper_cipher_encrypt_setup( operation, |
| 3204 | &attributes, |
| 3205 | slot->key.data, |
| 3206 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3207 | alg ); |
| 3208 | else |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3209 | status = psa_driver_wrapper_cipher_decrypt_setup( operation, |
| 3210 | &attributes, |
| 3211 | slot->key.data, |
| 3212 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3213 | alg ); |
| 3214 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3215 | exit: |
Ronald Cron | 06aa442 | 2021-03-09 17:32:57 +0100 | [diff] [blame] | 3216 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3217 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3218 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3219 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3220 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3221 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3222 | } |
| 3223 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3224 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3225 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3226 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3227 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3228 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3229 | } |
| 3230 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3231 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3232 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3233 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3234 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3235 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3236 | } |
| 3237 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3238 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3239 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3240 | size_t iv_size, |
| 3241 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3242 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3243 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3244 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3245 | size_t default_iv_length; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3246 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3247 | if( operation->id == 0 ) |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3248 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3249 | status = PSA_ERROR_BAD_STATE; |
| 3250 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3251 | } |
| 3252 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3253 | if( operation->iv_set || ! operation->iv_required ) |
| 3254 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3255 | status = PSA_ERROR_BAD_STATE; |
| 3256 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3257 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3258 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3259 | default_iv_length = operation->default_iv_length; |
| 3260 | if( iv_size < default_iv_length ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3261 | { |
| 3262 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3263 | goto exit; |
| 3264 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3265 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3266 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
| 3267 | { |
| 3268 | status = PSA_ERROR_GENERIC_ERROR; |
| 3269 | goto exit; |
| 3270 | } |
| 3271 | |
| 3272 | status = psa_generate_random( local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3273 | if( status != PSA_SUCCESS ) |
| 3274 | goto exit; |
| 3275 | |
| 3276 | status = psa_driver_wrapper_cipher_set_iv( operation, |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3277 | local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3278 | |
| 3279 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3280 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3281 | { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3282 | memcpy( iv, local_iv, default_iv_length ); |
| 3283 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3284 | operation->iv_set = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3285 | } |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3286 | else |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3287 | { |
| 3288 | *iv_length = 0; |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3289 | psa_cipher_abort( operation ); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3290 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3291 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3292 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3293 | } |
| 3294 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3295 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3296 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3297 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3298 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3299 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3300 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3301 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3302 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3303 | status = PSA_ERROR_BAD_STATE; |
| 3304 | goto exit; |
| 3305 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3306 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3307 | if( operation->iv_set || ! operation->iv_required ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3308 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3309 | status = PSA_ERROR_BAD_STATE; |
| 3310 | goto exit; |
| 3311 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3312 | |
| 3313 | if( iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3314 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3315 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3316 | goto exit; |
| 3317 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3318 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3319 | status = psa_driver_wrapper_cipher_set_iv( operation, |
| 3320 | iv, |
| 3321 | iv_length ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3322 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3323 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3324 | if( status == PSA_SUCCESS ) |
| 3325 | operation->iv_set = 1; |
| 3326 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3327 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3328 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3329 | } |
| 3330 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3331 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3332 | const uint8_t *input, |
| 3333 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3334 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3335 | size_t output_size, |
| 3336 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3337 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3338 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3339 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3340 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3341 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3342 | status = PSA_ERROR_BAD_STATE; |
| 3343 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3344 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3345 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3346 | if( operation->iv_required && ! operation->iv_set ) |
| 3347 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3348 | status = PSA_ERROR_BAD_STATE; |
| 3349 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3350 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3351 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3352 | status = psa_driver_wrapper_cipher_update( operation, |
| 3353 | input, |
| 3354 | input_length, |
| 3355 | output, |
| 3356 | output_size, |
| 3357 | output_length ); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3358 | |
| 3359 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3360 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3361 | psa_cipher_abort( operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3362 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3363 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3364 | } |
| 3365 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3366 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3367 | uint8_t *output, |
| 3368 | size_t output_size, |
| 3369 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3370 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3371 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3372 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3373 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3374 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3375 | status = PSA_ERROR_BAD_STATE; |
| 3376 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3377 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3378 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3379 | if( operation->iv_required && ! operation->iv_set ) |
| 3380 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3381 | status = PSA_ERROR_BAD_STATE; |
| 3382 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3383 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3384 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3385 | status = psa_driver_wrapper_cipher_finish( operation, |
| 3386 | output, |
| 3387 | output_size, |
| 3388 | output_length ); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3389 | |
| 3390 | exit: |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3391 | if( status == PSA_SUCCESS ) |
| 3392 | return( psa_cipher_abort( operation ) ); |
| 3393 | else |
| 3394 | { |
| 3395 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3396 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3397 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3398 | return( status ); |
| 3399 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3400 | } |
| 3401 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3402 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3403 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3404 | if( operation->id == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3405 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3406 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3407 | * in use. It's ok to call abort on such an object, and there's |
| 3408 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3409 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3410 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3411 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3412 | psa_driver_wrapper_cipher_abort( operation ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3413 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3414 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3415 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3416 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3417 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3418 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3419 | } |
| 3420 | |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3421 | psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key, |
| 3422 | psa_algorithm_t alg, |
| 3423 | const uint8_t *input, |
| 3424 | size_t input_length, |
| 3425 | uint8_t *output, |
| 3426 | size_t output_size, |
| 3427 | size_t *output_length ) |
| 3428 | { |
| 3429 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3430 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3431 | psa_key_slot_t *slot = NULL; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3432 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3433 | size_t default_iv_length = 0; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3434 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3435 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3436 | { |
| 3437 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3438 | goto exit; |
| 3439 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3440 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3441 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3442 | PSA_KEY_USAGE_ENCRYPT, |
| 3443 | alg ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3444 | if( status != PSA_SUCCESS ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3445 | goto exit; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3446 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3447 | psa_key_attributes_t attributes = { |
| 3448 | .core = slot->attr |
| 3449 | }; |
| 3450 | |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3451 | default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
| 3452 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3453 | { |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3454 | status = PSA_ERROR_GENERIC_ERROR; |
| 3455 | goto exit; |
| 3456 | } |
| 3457 | |
| 3458 | if( default_iv_length > 0 ) |
| 3459 | { |
| 3460 | if( output_size < default_iv_length ) |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3461 | { |
| 3462 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3463 | goto exit; |
| 3464 | } |
| 3465 | |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3466 | status = psa_generate_random( local_iv, default_iv_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3467 | if( status != PSA_SUCCESS ) |
| 3468 | goto exit; |
| 3469 | } |
| 3470 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3471 | status = psa_driver_wrapper_cipher_encrypt( |
| 3472 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3473 | alg, local_iv, default_iv_length, input, input_length, |
| 3474 | output + default_iv_length, output_size - default_iv_length, |
| 3475 | output_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3476 | |
| 3477 | exit: |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3478 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3479 | if( status == PSA_SUCCESS ) |
| 3480 | status = unlock_status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3481 | |
Ronald Cron | 9b67428 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3482 | if( status == PSA_SUCCESS ) |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3483 | { |
| 3484 | if( default_iv_length > 0 ) |
| 3485 | memcpy( output, local_iv, default_iv_length ); |
| 3486 | *output_length += default_iv_length; |
| 3487 | } |
Ronald Cron | 9b67428 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3488 | else |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3489 | *output_length = 0; |
| 3490 | |
| 3491 | return( status ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key, |
| 3495 | psa_algorithm_t alg, |
| 3496 | const uint8_t *input, |
| 3497 | size_t input_length, |
| 3498 | uint8_t *output, |
| 3499 | size_t output_size, |
| 3500 | size_t *output_length ) |
| 3501 | { |
| 3502 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3503 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3504 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3505 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3506 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3507 | { |
| 3508 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3509 | goto exit; |
| 3510 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3511 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3512 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3513 | PSA_KEY_USAGE_DECRYPT, |
| 3514 | alg ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3515 | if( status != PSA_SUCCESS ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3516 | goto exit; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3517 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3518 | psa_key_attributes_t attributes = { |
| 3519 | .core = slot->attr |
| 3520 | }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3521 | |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 3522 | if( alg == PSA_ALG_CCM_STAR_NO_TAG && input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) ) |
| 3523 | { |
| 3524 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3525 | goto exit; |
| 3526 | } |
| 3527 | 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] | 3528 | { |
| 3529 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3530 | goto exit; |
| 3531 | } |
| 3532 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3533 | status = psa_driver_wrapper_cipher_decrypt( |
| 3534 | &attributes, slot->key.data, slot->key.bytes, |
| 3535 | alg, input, input_length, |
| 3536 | output, output_size, output_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3537 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3538 | exit: |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3539 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3540 | if( status == PSA_SUCCESS ) |
| 3541 | status = unlock_status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3542 | |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3543 | if( status != PSA_SUCCESS ) |
| 3544 | *output_length = 0; |
| 3545 | |
| 3546 | return( status ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3550 | /****************************************************************/ |
| 3551 | /* AEAD */ |
| 3552 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3553 | |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3554 | /* Helper function to get the base algorithm from its variants. */ |
| 3555 | static psa_algorithm_t psa_aead_get_base_algorithm( psa_algorithm_t alg ) |
| 3556 | { |
| 3557 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg ); |
| 3558 | } |
| 3559 | |
| 3560 | /* Helper function to perform common nonce length checks. */ |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3561 | static psa_status_t psa_aead_check_nonce_length( psa_algorithm_t alg, |
| 3562 | size_t nonce_length ) |
| 3563 | { |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3564 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm( alg ); |
| 3565 | |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3566 | switch(base_alg) |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3567 | { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3568 | #if defined(PSA_WANT_ALG_GCM) |
| 3569 | case PSA_ALG_GCM: |
| 3570 | /* Not checking max nonce size here as GCM spec allows almost |
| 3571 | * arbitrarily large nonces. Please note that we do not generally |
| 3572 | * recommend the usage of nonces of greater length than |
| 3573 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter |
| 3574 | * size, which can then lead to collisions if you encrypt a very |
| 3575 | * large number of messages.*/ |
| 3576 | if( nonce_length != 0 ) |
| 3577 | return( PSA_SUCCESS ); |
| 3578 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3579 | #endif /* PSA_WANT_ALG_GCM */ |
| 3580 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3581 | case PSA_ALG_CCM: |
| 3582 | if( nonce_length >= 7 && nonce_length <= 13 ) |
| 3583 | return( PSA_SUCCESS ); |
| 3584 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3585 | #endif /* PSA_WANT_ALG_CCM */ |
| 3586 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3587 | case PSA_ALG_CHACHA20_POLY1305: |
| 3588 | if( nonce_length == 12 ) |
| 3589 | return( PSA_SUCCESS ); |
Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 3590 | else if( nonce_length == 8 ) |
| 3591 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3592 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3593 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3594 | default: |
Przemek Stekiel | ff1efc9 | 2022-10-02 21:12:17 +0200 | [diff] [blame] | 3595 | (void) nonce_length; |
Bence Szépkúti | 357b78e | 2021-11-09 13:17:17 +0100 | [diff] [blame] | 3596 | return( PSA_ERROR_NOT_SUPPORTED ); |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3597 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3598 | |
Bence Szépkúti | 357b78e | 2021-11-09 13:17:17 +0100 | [diff] [blame] | 3599 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3600 | } |
| 3601 | |
Bence Szépkúti | aa3a6e4 | 2022-01-13 16:26:03 +0100 | [diff] [blame] | 3602 | static psa_status_t psa_aead_check_algorithm( psa_algorithm_t alg ) |
| 3603 | { |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3604 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3605 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3606 | |
| 3607 | return( PSA_SUCCESS ); |
| 3608 | } |
| 3609 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3610 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3611 | psa_algorithm_t alg, |
| 3612 | const uint8_t *nonce, |
| 3613 | size_t nonce_length, |
| 3614 | const uint8_t *additional_data, |
| 3615 | size_t additional_data_length, |
| 3616 | const uint8_t *plaintext, |
| 3617 | size_t plaintext_length, |
| 3618 | uint8_t *ciphertext, |
| 3619 | size_t ciphertext_size, |
| 3620 | size_t *ciphertext_length ) |
| 3621 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3622 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3623 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3624 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3625 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3626 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3627 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3628 | if( status != PSA_SUCCESS ) |
| 3629 | return( status ); |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3630 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3631 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3632 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3633 | if( status != PSA_SUCCESS ) |
| 3634 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3635 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3636 | psa_key_attributes_t attributes = { |
| 3637 | .core = slot->attr |
| 3638 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3639 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3640 | status = psa_aead_check_nonce_length( alg, nonce_length ); |
| 3641 | if( status != PSA_SUCCESS ) |
| 3642 | goto exit; |
| 3643 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3644 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3645 | &attributes, slot->key.data, slot->key.bytes, |
| 3646 | alg, |
| 3647 | nonce, nonce_length, |
| 3648 | additional_data, additional_data_length, |
| 3649 | plaintext, plaintext_length, |
| 3650 | ciphertext, ciphertext_size, ciphertext_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3651 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3652 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3653 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3654 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3655 | exit: |
Ronald Cron | 9f31017 | 2021-03-16 16:36:37 +0100 | [diff] [blame] | 3656 | psa_unlock_key_slot( slot ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3657 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3658 | return( status ); |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3659 | } |
| 3660 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3661 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3662 | psa_algorithm_t alg, |
| 3663 | const uint8_t *nonce, |
| 3664 | size_t nonce_length, |
| 3665 | const uint8_t *additional_data, |
| 3666 | size_t additional_data_length, |
| 3667 | const uint8_t *ciphertext, |
| 3668 | size_t ciphertext_length, |
| 3669 | uint8_t *plaintext, |
| 3670 | size_t plaintext_size, |
| 3671 | size_t *plaintext_length ) |
| 3672 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3673 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3674 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3675 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3676 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3677 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3678 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3679 | if( status != PSA_SUCCESS ) |
| 3680 | return( status ); |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3681 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3682 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3683 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3684 | if( status != PSA_SUCCESS ) |
| 3685 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3686 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3687 | psa_key_attributes_t attributes = { |
| 3688 | .core = slot->attr |
| 3689 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3690 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3691 | status = psa_aead_check_nonce_length( alg, nonce_length ); |
| 3692 | if( status != PSA_SUCCESS ) |
| 3693 | goto exit; |
| 3694 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3695 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3696 | &attributes, slot->key.data, slot->key.bytes, |
| 3697 | alg, |
| 3698 | nonce, nonce_length, |
| 3699 | additional_data, additional_data_length, |
| 3700 | ciphertext, ciphertext_length, |
| 3701 | plaintext, plaintext_size, plaintext_length ); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3702 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3703 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3704 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3705 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3706 | exit: |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3707 | psa_unlock_key_slot( slot ); |
| 3708 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3709 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3710 | } |
| 3711 | |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3712 | static psa_status_t psa_validate_tag_length( psa_aead_operation_t *operation, |
| 3713 | psa_algorithm_t alg ) { |
Przemek Stekiel | 8a05a64 | 2022-10-06 17:01:58 +0200 | [diff] [blame^] | 3714 | const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3715 | |
| 3716 | switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ) |
| 3717 | { |
| 3718 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 3719 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
| 3720 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/ |
| 3721 | if( tag_len < 4 || tag_len > 16 || tag_len % 2 ) |
| 3722 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3723 | break; |
| 3724 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 3725 | |
| 3726 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 3727 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
| 3728 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */ |
| 3729 | if( tag_len != 4 && tag_len != 8 && ( tag_len < 12 || tag_len > 16 ) ) |
| 3730 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3731 | break; |
| 3732 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 3733 | |
| 3734 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 3735 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3736 | /* We only support the default tag length. */ |
| 3737 | if( tag_len != 16 ) |
| 3738 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3739 | break; |
| 3740 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 3741 | |
| 3742 | default: |
| 3743 | (void) tag_len; |
| 3744 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3745 | } |
| 3746 | return( PSA_SUCCESS ); |
| 3747 | } |
| 3748 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3749 | /* Set the key for a multipart authenticated operation. */ |
| 3750 | static psa_status_t psa_aead_setup( psa_aead_operation_t *operation, |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3751 | int is_encrypt, |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3752 | mbedtls_svc_key_id_t key, |
| 3753 | psa_algorithm_t alg ) |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3754 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3755 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3756 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3757 | psa_key_slot_t *slot = NULL; |
| 3758 | psa_key_usage_t key_usage = 0; |
| 3759 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3760 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3761 | if( status != PSA_SUCCESS ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3762 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3763 | |
| 3764 | if( operation->id != 0 ) |
| 3765 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3766 | status = PSA_ERROR_BAD_STATE; |
| 3767 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | if( operation->nonce_set || operation->lengths_set || |
| 3771 | operation->ad_started || operation->body_started ) |
| 3772 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3773 | status = PSA_ERROR_BAD_STATE; |
| 3774 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3775 | } |
| 3776 | |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3777 | if( is_encrypt ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3778 | key_usage = PSA_KEY_USAGE_ENCRYPT; |
| 3779 | else |
| 3780 | key_usage = PSA_KEY_USAGE_DECRYPT; |
| 3781 | |
| 3782 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, key_usage, |
| 3783 | alg ); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3784 | if( status != PSA_SUCCESS ) |
| 3785 | goto exit; |
| 3786 | |
| 3787 | psa_key_attributes_t attributes = { |
| 3788 | .core = slot->attr |
| 3789 | }; |
| 3790 | |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3791 | if( is_encrypt ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3792 | status = psa_driver_wrapper_aead_encrypt_setup( operation, |
| 3793 | &attributes, |
| 3794 | slot->key.data, |
| 3795 | slot->key.bytes, |
| 3796 | alg ); |
| 3797 | else |
| 3798 | status = psa_driver_wrapper_aead_decrypt_setup( operation, |
| 3799 | &attributes, |
| 3800 | slot->key.data, |
| 3801 | slot->key.bytes, |
| 3802 | alg ); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3803 | if( status != PSA_SUCCESS ) |
| 3804 | goto exit; |
| 3805 | |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3806 | if( ( status = psa_validate_tag_length( operation, alg ) ) != PSA_SUCCESS ) |
| 3807 | goto exit; |
| 3808 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3809 | operation->key_type = psa_get_key_type( &attributes ); |
| 3810 | |
| 3811 | exit: |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3812 | unlock_status = psa_unlock_key_slot( slot ); |
| 3813 | |
| 3814 | if( status == PSA_SUCCESS ) |
| 3815 | { |
| 3816 | status = unlock_status; |
| 3817 | operation->alg = psa_aead_get_base_algorithm( alg ); |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3818 | operation->is_encrypt = is_encrypt; |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3819 | } |
| 3820 | else |
| 3821 | psa_aead_abort( operation ); |
| 3822 | |
| 3823 | return( status ); |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3824 | } |
| 3825 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3826 | /* Set the key for a multipart authenticated encryption operation. */ |
| 3827 | psa_status_t psa_aead_encrypt_setup( psa_aead_operation_t *operation, |
| 3828 | mbedtls_svc_key_id_t key, |
| 3829 | psa_algorithm_t alg ) |
| 3830 | { |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3831 | return( psa_aead_setup( operation, 1, key, alg ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | /* Set the key for a multipart authenticated decryption operation. */ |
| 3835 | psa_status_t psa_aead_decrypt_setup( psa_aead_operation_t *operation, |
| 3836 | mbedtls_svc_key_id_t key, |
| 3837 | psa_algorithm_t alg ) |
| 3838 | { |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3839 | return( psa_aead_setup( operation, 0, key, alg ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | /* Generate a random nonce / IV for multipart AEAD operation */ |
| 3843 | psa_status_t psa_aead_generate_nonce( psa_aead_operation_t *operation, |
| 3844 | uint8_t *nonce, |
| 3845 | size_t nonce_size, |
| 3846 | size_t *nonce_length ) |
| 3847 | { |
| 3848 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3849 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; |
Paul Elliott | b91da71 | 2021-05-20 14:43:47 +0100 | [diff] [blame] | 3850 | size_t required_nonce_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3851 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3852 | *nonce_length = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3853 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3854 | if( operation->id == 0 ) |
| 3855 | { |
| 3856 | status = PSA_ERROR_BAD_STATE; |
| 3857 | goto exit; |
| 3858 | } |
| 3859 | |
Paul Elliott | daf5c89 | 2021-08-25 16:24:58 +0100 | [diff] [blame] | 3860 | if( operation->nonce_set || !operation->is_encrypt ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3861 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3862 | status = PSA_ERROR_BAD_STATE; |
| 3863 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3864 | } |
| 3865 | |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 3866 | /* For CCM, this size may not be correct according to the PSA |
| 3867 | * specification. The PSA Crypto 1.0.1 specification states: |
| 3868 | * |
| 3869 | * CCM encodes the plaintext length pLen in L octets, with L the smallest |
| 3870 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. |
| 3871 | * |
| 3872 | * However this restriction that L has to be the smallest integer is not |
| 3873 | * applied in practice, and it is not implementable here since the |
| 3874 | * plaintext length may or may not be known at this time. */ |
Paul Elliott | bbe90b5 | 2021-05-11 22:22:42 +0100 | [diff] [blame] | 3875 | required_nonce_size = PSA_AEAD_NONCE_LENGTH( operation->key_type, |
Paul Elliott | 3242f6c | 2021-08-25 16:33:47 +0100 | [diff] [blame] | 3876 | operation->alg ); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3877 | if( nonce_size < required_nonce_size ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3878 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3879 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3880 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3881 | } |
| 3882 | |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3883 | status = psa_generate_random( local_nonce, required_nonce_size ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3884 | if( status != PSA_SUCCESS ) |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3885 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3886 | |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3887 | status = psa_aead_set_nonce( operation, local_nonce, required_nonce_size ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3888 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3889 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3890 | if( status == PSA_SUCCESS ) |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3891 | { |
| 3892 | memcpy( nonce, local_nonce, required_nonce_size ); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3893 | *nonce_length = required_nonce_size; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3894 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3895 | else |
| 3896 | psa_aead_abort( operation ); |
| 3897 | |
| 3898 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | /* Set the nonce for a multipart authenticated encryption or decryption |
| 3902 | operation.*/ |
| 3903 | psa_status_t psa_aead_set_nonce( psa_aead_operation_t *operation, |
| 3904 | const uint8_t *nonce, |
| 3905 | size_t nonce_length ) |
| 3906 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3907 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3908 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3909 | if( operation->id == 0 ) |
| 3910 | { |
| 3911 | status = PSA_ERROR_BAD_STATE; |
| 3912 | goto exit; |
| 3913 | } |
| 3914 | |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 3915 | if( operation->nonce_set ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3916 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3917 | status = PSA_ERROR_BAD_STATE; |
| 3918 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3919 | } |
| 3920 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3921 | status = psa_aead_check_nonce_length( operation->alg, nonce_length ); |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3922 | if( status != PSA_SUCCESS ) |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 3923 | { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3924 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3925 | goto exit; |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 3926 | } |
Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 3927 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3928 | status = psa_driver_wrapper_aead_set_nonce( operation, nonce, |
| 3929 | nonce_length ); |
| 3930 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3931 | exit: |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3932 | if( status == PSA_SUCCESS ) |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3933 | operation->nonce_set = 1; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3934 | else |
| 3935 | psa_aead_abort( operation ); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3936 | |
| 3937 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | /* Declare the lengths of the message and additional data for multipart AEAD. */ |
| 3941 | psa_status_t psa_aead_set_lengths( psa_aead_operation_t *operation, |
| 3942 | size_t ad_length, |
| 3943 | size_t plaintext_length ) |
| 3944 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3945 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3946 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3947 | if( operation->id == 0 ) |
| 3948 | { |
| 3949 | status = PSA_ERROR_BAD_STATE; |
| 3950 | goto exit; |
| 3951 | } |
| 3952 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 3953 | if( operation->lengths_set || operation->ad_started || |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 3954 | operation->body_started ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3955 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3956 | status = PSA_ERROR_BAD_STATE; |
| 3957 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3958 | } |
| 3959 | |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3960 | switch(operation->alg) |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3961 | { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3962 | #if defined(PSA_WANT_ALG_GCM) |
| 3963 | case PSA_ALG_GCM: |
| 3964 | /* Lengths can only be too large for GCM if size_t is bigger than 32 |
| 3965 | * bits. Without the guard this code will generate warnings on 32bit |
| 3966 | * builds. */ |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3967 | #if SIZE_MAX > UINT32_MAX |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3968 | if( (( uint64_t ) ad_length ) >> 61 != 0 || |
| 3969 | (( uint64_t ) plaintext_length ) > 0xFFFFFFFE0ull ) |
| 3970 | { |
| 3971 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3972 | goto exit; |
| 3973 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3974 | #endif |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3975 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3976 | #endif /* PSA_WANT_ALG_GCM */ |
| 3977 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3978 | case PSA_ALG_CCM: |
| 3979 | if( ad_length > 0xFF00 ) |
| 3980 | { |
| 3981 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3982 | goto exit; |
| 3983 | } |
| 3984 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3985 | #endif /* PSA_WANT_ALG_CCM */ |
| 3986 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3987 | case PSA_ALG_CHACHA20_POLY1305: |
| 3988 | /* No length restrictions for ChaChaPoly. */ |
| 3989 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3990 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3991 | default: |
| 3992 | break; |
| 3993 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3994 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3995 | status = psa_driver_wrapper_aead_set_lengths( operation, ad_length, |
| 3996 | plaintext_length ); |
| 3997 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3998 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3999 | if( status == PSA_SUCCESS ) |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4000 | { |
| 4001 | operation->ad_remaining = ad_length; |
| 4002 | operation->body_remaining = plaintext_length; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4003 | operation->lengths_set = 1; |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4004 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4005 | else |
| 4006 | psa_aead_abort( operation ); |
| 4007 | |
| 4008 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4009 | } |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 4010 | |
| 4011 | /* Pass additional data to an active multipart AEAD operation. */ |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4012 | psa_status_t psa_aead_update_ad( psa_aead_operation_t *operation, |
| 4013 | const uint8_t *input, |
| 4014 | size_t input_length ) |
| 4015 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4016 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4017 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4018 | if( operation->id == 0 ) |
| 4019 | { |
| 4020 | status = PSA_ERROR_BAD_STATE; |
| 4021 | goto exit; |
| 4022 | } |
| 4023 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4024 | if( !operation->nonce_set || operation->body_started ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4025 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4026 | status = PSA_ERROR_BAD_STATE; |
| 4027 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4028 | } |
| 4029 | |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4030 | if( operation->lengths_set ) |
| 4031 | { |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4032 | if( operation->ad_remaining < input_length ) |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4033 | { |
| 4034 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4035 | goto exit; |
| 4036 | } |
| 4037 | |
| 4038 | operation->ad_remaining -= input_length; |
| 4039 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4040 | #if defined(PSA_WANT_ALG_CCM) |
| 4041 | else if( operation->alg == PSA_ALG_CCM ) |
| 4042 | { |
| 4043 | status = PSA_ERROR_BAD_STATE; |
| 4044 | goto exit; |
| 4045 | } |
| 4046 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4047 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4048 | status = psa_driver_wrapper_aead_update_ad( operation, input, |
| 4049 | input_length ); |
| 4050 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4051 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4052 | if( status == PSA_SUCCESS ) |
| 4053 | operation->ad_started = 1; |
| 4054 | else |
| 4055 | psa_aead_abort( operation ); |
| 4056 | |
| 4057 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 4061 | operation.*/ |
| 4062 | psa_status_t psa_aead_update( psa_aead_operation_t *operation, |
| 4063 | const uint8_t *input, |
| 4064 | size_t input_length, |
| 4065 | uint8_t *output, |
| 4066 | size_t output_size, |
| 4067 | size_t *output_length ) |
| 4068 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4069 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4070 | |
| 4071 | *output_length = 0; |
| 4072 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4073 | if( operation->id == 0 ) |
| 4074 | { |
| 4075 | status = PSA_ERROR_BAD_STATE; |
| 4076 | goto exit; |
| 4077 | } |
| 4078 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4079 | if( !operation->nonce_set ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4080 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4081 | status = PSA_ERROR_BAD_STATE; |
| 4082 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4083 | } |
| 4084 | |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4085 | if( operation->lengths_set ) |
| 4086 | { |
| 4087 | /* Additional data length was supplied, but not all the additional |
| 4088 | data was supplied.*/ |
| 4089 | if( operation->ad_remaining != 0 ) |
| 4090 | { |
| 4091 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4092 | goto exit; |
| 4093 | } |
| 4094 | |
| 4095 | /* Too much data provided. */ |
| 4096 | if( operation->body_remaining < input_length ) |
| 4097 | { |
| 4098 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4099 | goto exit; |
| 4100 | } |
| 4101 | |
| 4102 | operation->body_remaining -= input_length; |
| 4103 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4104 | #if defined(PSA_WANT_ALG_CCM) |
| 4105 | else if( operation->alg == PSA_ALG_CCM ) |
| 4106 | { |
| 4107 | status = PSA_ERROR_BAD_STATE; |
| 4108 | goto exit; |
| 4109 | } |
| 4110 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4111 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4112 | status = psa_driver_wrapper_aead_update( operation, input, input_length, |
| 4113 | output, output_size, |
| 4114 | output_length ); |
| 4115 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4116 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4117 | if( status == PSA_SUCCESS ) |
| 4118 | operation->body_started = 1; |
| 4119 | else |
| 4120 | psa_aead_abort( operation ); |
| 4121 | |
| 4122 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4123 | } |
| 4124 | |
Paul Elliott | 69bf5fc | 2021-09-19 18:26:37 +0100 | [diff] [blame] | 4125 | 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] | 4126 | { |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4127 | if( operation->id == 0 || !operation->nonce_set ) |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4128 | return( PSA_ERROR_BAD_STATE ); |
| 4129 | |
Paul Elliott | a561444 | 2021-07-14 14:54:11 +0100 | [diff] [blame] | 4130 | if( operation->lengths_set && ( operation->ad_remaining != 0 || |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4131 | operation->body_remaining != 0 ) ) |
| 4132 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4133 | |
| 4134 | return( PSA_SUCCESS ); |
| 4135 | } |
| 4136 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4137 | /* Finish encrypting a message in a multipart AEAD operation. */ |
| 4138 | psa_status_t psa_aead_finish( psa_aead_operation_t *operation, |
| 4139 | uint8_t *ciphertext, |
| 4140 | size_t ciphertext_size, |
| 4141 | size_t *ciphertext_length, |
| 4142 | uint8_t *tag, |
| 4143 | size_t tag_size, |
| 4144 | size_t *tag_length ) |
| 4145 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4146 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4147 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4148 | *ciphertext_length = 0; |
Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 4149 | *tag_length = tag_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4150 | |
Paul Elliott | 814fffb | 2021-08-16 18:20:36 +0100 | [diff] [blame] | 4151 | status = psa_aead_final_checks( operation ); |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4152 | if( status != PSA_SUCCESS ) |
| 4153 | goto exit; |
| 4154 | |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4155 | if( !operation->is_encrypt ) |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4156 | { |
| 4157 | status = PSA_ERROR_BAD_STATE; |
| 4158 | goto exit; |
| 4159 | } |
| 4160 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4161 | status = psa_driver_wrapper_aead_finish( operation, ciphertext, |
| 4162 | ciphertext_size, |
| 4163 | ciphertext_length, |
| 4164 | tag, tag_size, tag_length ); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4165 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4166 | exit: |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4167 | /* 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] | 4168 | * the zero tag size, make sure the tag is set to something implausible. |
| 4169 | * Even if the operation succeeds, make sure we clear the rest of the |
| 4170 | * buffer to prevent potential leakage of anything previously placed in |
| 4171 | * the same buffer.*/ |
Paul Elliott | 90fdc11 | 2021-09-22 17:15:48 +0100 | [diff] [blame] | 4172 | if( tag != NULL ) |
Paul Elliott | ec95cc9 | 2021-09-19 22:33:09 +0100 | [diff] [blame] | 4173 | { |
| 4174 | if( status != PSA_SUCCESS ) |
| 4175 | memset( tag, '!', tag_size ); |
| 4176 | else if( *tag_length < tag_size ) |
| 4177 | memset( tag + *tag_length, '!', ( tag_size - *tag_length ) ); |
| 4178 | } |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4179 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4180 | psa_aead_abort( operation ); |
| 4181 | |
| 4182 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 4186 | operation.*/ |
| 4187 | psa_status_t psa_aead_verify( psa_aead_operation_t *operation, |
| 4188 | uint8_t *plaintext, |
| 4189 | size_t plaintext_size, |
| 4190 | size_t *plaintext_length, |
| 4191 | const uint8_t *tag, |
| 4192 | size_t tag_length ) |
| 4193 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4194 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4195 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4196 | *plaintext_length = 0; |
| 4197 | |
Paul Elliott | 814fffb | 2021-08-16 18:20:36 +0100 | [diff] [blame] | 4198 | status = psa_aead_final_checks( operation ); |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4199 | if( status != PSA_SUCCESS ) |
| 4200 | goto exit; |
| 4201 | |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4202 | if( operation->is_encrypt ) |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4203 | { |
| 4204 | status = PSA_ERROR_BAD_STATE; |
| 4205 | goto exit; |
| 4206 | } |
| 4207 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4208 | status = psa_driver_wrapper_aead_verify( operation, plaintext, |
| 4209 | plaintext_size, |
| 4210 | plaintext_length, |
| 4211 | tag, tag_length ); |
| 4212 | |
| 4213 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4214 | psa_aead_abort( operation ); |
| 4215 | |
| 4216 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4217 | } |
| 4218 | |
| 4219 | /* Abort an AEAD operation. */ |
Paul Elliott | bbe90b5 | 2021-05-11 22:22:42 +0100 | [diff] [blame] | 4220 | psa_status_t psa_aead_abort( psa_aead_operation_t *operation ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4221 | { |
| 4222 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4223 | |
| 4224 | if( operation->id == 0 ) |
| 4225 | { |
| 4226 | /* The object has (apparently) been initialized but it is not (yet) |
| 4227 | * in use. It's ok to call abort on such an object, and there's |
| 4228 | * nothing to do. */ |
| 4229 | return( PSA_SUCCESS ); |
| 4230 | } |
| 4231 | |
| 4232 | status = psa_driver_wrapper_aead_abort( operation ); |
| 4233 | |
Paul Elliott | 70618b2 | 2021-09-22 17:12:16 +0100 | [diff] [blame] | 4234 | memset( operation, 0, sizeof( *operation ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4235 | |
| 4236 | return( status ); |
| 4237 | } |
| 4238 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4239 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4240 | /* Generators */ |
| 4241 | /****************************************************************/ |
| 4242 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4243 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4244 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4245 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4246 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4247 | #endif /* At least one builtin KDF */ |
| 4248 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4249 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4250 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4251 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4252 | static psa_status_t psa_key_derivation_start_hmac( |
| 4253 | psa_mac_operation_t *operation, |
| 4254 | psa_algorithm_t hash_alg, |
| 4255 | const uint8_t *hmac_key, |
| 4256 | size_t hmac_key_length ) |
| 4257 | { |
| 4258 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4259 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4260 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 4261 | psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) ); |
| 4262 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 4263 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4264 | operation->is_sign = 1; |
| 4265 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
| 4266 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4267 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 4268 | &attributes, |
| 4269 | hmac_key, hmac_key_length, |
| 4270 | PSA_ALG_HMAC( hash_alg ) ); |
| 4271 | |
| 4272 | psa_reset_key_attributes( &attributes ); |
| 4273 | return( status ); |
| 4274 | } |
| 4275 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4276 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4277 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4278 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4279 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4280 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4281 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4282 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4283 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4284 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4285 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4286 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4287 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4288 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4289 | } |
| 4290 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4291 | 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] | 4292 | { |
| 4293 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4294 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4295 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4296 | { |
| 4297 | /* The object has (apparently) been initialized but it is not |
| 4298 | * in use. It's ok to call abort on such an object, and there's |
| 4299 | * nothing to do. */ |
| 4300 | } |
| 4301 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4302 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 4303 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4304 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4305 | mbedtls_free( operation->ctx.hkdf.info ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4306 | status = psa_mac_abort( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4307 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4308 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4309 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4310 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4311 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4312 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4313 | /* 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] | 4314 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4315 | { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4316 | if( operation->ctx.tls12_prf.secret != NULL ) |
| 4317 | { |
| 4318 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret, |
| 4319 | operation->ctx.tls12_prf.secret_length ); |
| 4320 | mbedtls_free( operation->ctx.tls12_prf.secret ); |
| 4321 | } |
| 4322 | |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4323 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4324 | { |
| 4325 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4326 | operation->ctx.tls12_prf.seed_length ); |
| 4327 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4328 | } |
| 4329 | |
| 4330 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4331 | { |
| 4332 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4333 | operation->ctx.tls12_prf.label_length ); |
| 4334 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4335 | } |
| 4336 | |
Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 4337 | if( operation->ctx.tls12_prf.other_secret != NULL ) |
| 4338 | { |
| 4339 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.other_secret, |
| 4340 | operation->ctx.tls12_prf.other_secret_length ); |
| 4341 | mbedtls_free( operation->ctx.tls12_prf.other_secret ); |
| 4342 | } |
| 4343 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4344 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4345 | |
| 4346 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4347 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4348 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4349 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4350 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4351 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4352 | { |
| 4353 | status = PSA_ERROR_BAD_STATE; |
| 4354 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4355 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4356 | return( status ); |
| 4357 | } |
| 4358 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4359 | 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] | 4360 | size_t *capacity) |
| 4361 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4362 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4363 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4364 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4365 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4368 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4369 | return( PSA_SUCCESS ); |
| 4370 | } |
| 4371 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4372 | 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] | 4373 | size_t capacity ) |
| 4374 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4375 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4376 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4377 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4378 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4379 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4380 | return( PSA_SUCCESS ); |
| 4381 | } |
| 4382 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4383 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4384 | /* Read some bytes from an HKDF-based operation. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4385 | 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] | 4386 | psa_algorithm_t kdf_alg, |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4387 | uint8_t *output, |
| 4388 | size_t output_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4389 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4390 | 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] | 4391 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4392 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4393 | psa_status_t status; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4394 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4395 | 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] | 4396 | #else |
| 4397 | const uint8_t last_block = 0xff; |
| 4398 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4399 | |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4400 | if( hkdf->state < HKDF_STATE_KEYED || |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4401 | ( !hkdf->info_set |
| 4402 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 4403 | && !PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) |
| 4404 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
| 4405 | ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4406 | return( PSA_ERROR_BAD_STATE ); |
| 4407 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4408 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4409 | while( output_length != 0 ) |
| 4410 | { |
| 4411 | /* Copy what remains of the current block */ |
| 4412 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4413 | if( n > output_length ) |
| 4414 | n = (uint8_t) output_length; |
| 4415 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4416 | output += n; |
| 4417 | output_length -= n; |
| 4418 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4419 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4420 | break; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4421 | /* We can't be wanting more output after the last block, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4422 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4423 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4424 | * object was corrupted or if this function is called directly |
| 4425 | * inside the library. */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4426 | if( hkdf->block_number == last_block ) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4427 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4428 | |
| 4429 | /* We need a new block */ |
| 4430 | ++hkdf->block_number; |
| 4431 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4432 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4433 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 4434 | hash_alg, |
| 4435 | hkdf->prk, |
| 4436 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4437 | if( status != PSA_SUCCESS ) |
| 4438 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4439 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4440 | if( hkdf->block_number != 1 ) |
| 4441 | { |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4442 | status = psa_mac_update( &hkdf->hmac, |
| 4443 | hkdf->output_block, |
| 4444 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4445 | if( status != PSA_SUCCESS ) |
| 4446 | return( status ); |
| 4447 | } |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4448 | status = psa_mac_update( &hkdf->hmac, |
| 4449 | hkdf->info, |
| 4450 | hkdf->info_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4451 | if( status != PSA_SUCCESS ) |
| 4452 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4453 | status = psa_mac_update( &hkdf->hmac, |
| 4454 | &hkdf->block_number, 1 ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4455 | if( status != PSA_SUCCESS ) |
| 4456 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4457 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 4458 | hkdf->output_block, |
| 4459 | sizeof( hkdf->output_block ), |
| 4460 | &hmac_output_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4461 | if( status != PSA_SUCCESS ) |
| 4462 | return( status ); |
| 4463 | } |
| 4464 | |
| 4465 | return( PSA_SUCCESS ); |
| 4466 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4467 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4468 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4469 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4470 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4471 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4472 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4473 | psa_algorithm_t alg ) |
| 4474 | { |
| 4475 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4476 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4477 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 4478 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4479 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4480 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4481 | /* We can't be wanting more output after block 0xff, otherwise |
| 4482 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4483 | * prevented this call. It could happen only if the operation |
| 4484 | * object was corrupted or if this function is called directly |
| 4485 | * inside the library. */ |
| 4486 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4487 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4488 | |
| 4489 | /* We need a new block */ |
| 4490 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4491 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4492 | |
| 4493 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4494 | * |
| 4495 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4496 | * |
| 4497 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4498 | * HMAC_hash(secret, A(2) + seed) + |
| 4499 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4500 | * |
| 4501 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4502 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4503 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4504 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4505 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4506 | * is currently extracted as `output_block` and where i is |
| 4507 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4508 | */ |
| 4509 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4510 | status = psa_key_derivation_start_hmac( &hmac, |
| 4511 | hash_alg, |
| 4512 | tls12_prf->secret, |
| 4513 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4514 | if( status != PSA_SUCCESS ) |
| 4515 | goto cleanup; |
| 4516 | |
| 4517 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4518 | if( tls12_prf->block_number == 1 ) |
| 4519 | { |
| 4520 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4521 | * the variable seed and in this instance means it in the context of the |
| 4522 | * P_hash function, where seed = label + seed.) */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4523 | status = psa_mac_update( &hmac, |
| 4524 | tls12_prf->label, |
| 4525 | tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4526 | if( status != PSA_SUCCESS ) |
| 4527 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4528 | status = psa_mac_update( &hmac, |
| 4529 | tls12_prf->seed, |
| 4530 | tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4531 | if( status != PSA_SUCCESS ) |
| 4532 | goto cleanup; |
| 4533 | } |
| 4534 | else |
| 4535 | { |
| 4536 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4537 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4538 | if( status != PSA_SUCCESS ) |
| 4539 | goto cleanup; |
| 4540 | } |
| 4541 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4542 | status = psa_mac_sign_finish( &hmac, |
| 4543 | tls12_prf->Ai, hash_length, |
| 4544 | &hmac_output_length ); |
| 4545 | if( hmac_output_length != hash_length ) |
| 4546 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4547 | if( status != PSA_SUCCESS ) |
| 4548 | goto cleanup; |
| 4549 | |
| 4550 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4551 | status = psa_key_derivation_start_hmac( &hmac, |
| 4552 | hash_alg, |
| 4553 | tls12_prf->secret, |
| 4554 | tls12_prf->secret_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_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4558 | if( status != PSA_SUCCESS ) |
| 4559 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4560 | status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4561 | if( status != PSA_SUCCESS ) |
| 4562 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4563 | status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4564 | if( status != PSA_SUCCESS ) |
| 4565 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4566 | status = psa_mac_sign_finish( &hmac, |
| 4567 | tls12_prf->output_block, hash_length, |
| 4568 | &hmac_output_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4569 | if( status != PSA_SUCCESS ) |
| 4570 | goto cleanup; |
| 4571 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4572 | |
| 4573 | cleanup: |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4574 | cleanup_status = psa_mac_abort( &hmac ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4575 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4576 | status = cleanup_status; |
| 4577 | |
| 4578 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4579 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4580 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4581 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4582 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4583 | psa_algorithm_t alg, |
| 4584 | uint8_t *output, |
| 4585 | size_t output_length ) |
| 4586 | { |
| 4587 | 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] | 4588 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4589 | psa_status_t status; |
| 4590 | uint8_t offset, length; |
| 4591 | |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4592 | switch( tls12_prf->state ) |
| 4593 | { |
| 4594 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4595 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4596 | break; |
| 4597 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4598 | break; |
| 4599 | default: |
| 4600 | return( PSA_ERROR_BAD_STATE ); |
| 4601 | } |
| 4602 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4603 | while( output_length != 0 ) |
| 4604 | { |
| 4605 | /* Check if we have fully processed the current block. */ |
| 4606 | if( tls12_prf->left_in_block == 0 ) |
| 4607 | { |
| 4608 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4609 | alg ); |
| 4610 | if( status != PSA_SUCCESS ) |
| 4611 | return( status ); |
| 4612 | |
| 4613 | continue; |
| 4614 | } |
| 4615 | |
| 4616 | if( tls12_prf->left_in_block > output_length ) |
| 4617 | length = (uint8_t) output_length; |
| 4618 | else |
| 4619 | length = tls12_prf->left_in_block; |
| 4620 | |
| 4621 | offset = hash_length - tls12_prf->left_in_block; |
| 4622 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4623 | output += length; |
| 4624 | output_length -= length; |
| 4625 | tls12_prf->left_in_block -= length; |
| 4626 | } |
| 4627 | |
| 4628 | return( PSA_SUCCESS ); |
| 4629 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4630 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4631 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4632 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4633 | psa_status_t psa_key_derivation_output_bytes( |
| 4634 | psa_key_derivation_operation_t *operation, |
| 4635 | uint8_t *output, |
| 4636 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4637 | { |
| 4638 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4639 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4640 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4641 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4642 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4643 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4644 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4647 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4648 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4649 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4650 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4651 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4652 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4653 | goto exit; |
| 4654 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4655 | if( output_length == 0 && operation->capacity == 0 ) |
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 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4658 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4659 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4660 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4661 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4662 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4663 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
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 -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4666 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4667 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 4668 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4669 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4670 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, kdf_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4671 | output, output_length ); |
| 4672 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4673 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4674 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4675 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4676 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4677 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4678 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4679 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4680 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4681 | kdf_alg, output, |
| 4682 | output_length ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4683 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4684 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4685 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4686 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4687 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4688 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4689 | return( PSA_ERROR_BAD_STATE ); |
| 4690 | } |
| 4691 | |
| 4692 | exit: |
| 4693 | if( status != PSA_SUCCESS ) |
| 4694 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4695 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4696 | * This allows us to differentiate between exhausted operations and |
| 4697 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4698 | * operations. */ |
| 4699 | psa_algorithm_t alg = operation->alg; |
| 4700 | psa_key_derivation_abort( operation ); |
| 4701 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4702 | memset( output, '!', output_length ); |
| 4703 | } |
| 4704 | return( status ); |
| 4705 | } |
| 4706 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4707 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4708 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4709 | { |
| 4710 | if( data_size >= 8 ) |
| 4711 | mbedtls_des_key_set_parity( data ); |
| 4712 | if( data_size >= 16 ) |
| 4713 | mbedtls_des_key_set_parity( data + 8 ); |
| 4714 | if( data_size >= 24 ) |
| 4715 | mbedtls_des_key_set_parity( data + 16 ); |
| 4716 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4717 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4718 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4719 | /* |
Przemyslaw Stekiel | 705fb0f | 2021-11-24 08:47:29 +0100 | [diff] [blame] | 4720 | * ECC keys on a Weierstrass elliptic curve require the generation |
| 4721 | * of a private key which is an integer |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4722 | * in the range [1, N - 1], where N is the boundary of the private key domain: |
| 4723 | * N is the prime p for Diffie-Hellman, or the order of the |
| 4724 | * curve’s base point for ECC. |
| 4725 | * |
| 4726 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). |
| 4727 | * This function generates the private key using the following process: |
| 4728 | * |
| 4729 | * 1. Draw a byte string of length ceiling(m/8) bytes. |
| 4730 | * 2. If m is not a multiple of 8, set the most significant |
| 4731 | * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. |
| 4732 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. |
| 4733 | * 4. If k > N - 2, discard the result and return to step 1. |
| 4734 | * 5. Output k + 1 as the private key. |
| 4735 | * |
| 4736 | * This method allows compliance to NIST standards, specifically the methods titled |
| 4737 | * Key-Pair Generation by Testing Candidates in the following publications: |
| 4738 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment |
| 4739 | * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for |
| 4740 | * Diffie-Hellman keys. |
| 4741 | * |
| 4742 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature |
| 4743 | * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4744 | * |
| 4745 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 4746 | * always NULL. |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4747 | */ |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4748 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 4749 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 4750 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4751 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 4752 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4753 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4754 | psa_key_slot_t *slot, |
| 4755 | size_t bits, |
| 4756 | psa_key_derivation_operation_t *operation, |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4757 | uint8_t **data |
| 4758 | ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4759 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4760 | unsigned key_out_of_range = 1; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4761 | mbedtls_mpi k; |
| 4762 | mbedtls_mpi diff_N_2; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4763 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4764 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4765 | |
| 4766 | mbedtls_mpi_init( &k ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4767 | mbedtls_mpi_init( &diff_N_2 ); |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4768 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4769 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 4770 | slot->attr.type ); |
| 4771 | mbedtls_ecp_group_id grp_id = |
| 4772 | mbedtls_ecc_group_of_psa( curve, bits, 0 ); |
| 4773 | |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 4774 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 4775 | { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4776 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 4777 | goto cleanup; |
| 4778 | } |
| 4779 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4780 | mbedtls_ecp_group ecp_group; |
Przemyslaw Stekiel | 6534816 | 2021-11-18 11:57:07 +0100 | [diff] [blame] | 4781 | mbedtls_ecp_group_init( &ecp_group ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4782 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4783 | MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ecp_group, grp_id ) ); |
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 | /* N is the boundary of the private key domain (ecp_group.N). */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4786 | /* Let m be the bit size of N. */ |
| 4787 | size_t m = ecp_group.nbits; |
| 4788 | |
| 4789 | size_t m_bytes = PSA_BITS_TO_BYTES( m ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4790 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4791 | /* Calculate N - 2 - it will be needed later. */ |
| 4792 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &diff_N_2, &ecp_group.N, 2 ) ); |
| 4793 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4794 | /* Note: This function is always called with *data == NULL and it |
| 4795 | * allocates memory for the data buffer. */ |
| 4796 | *data = mbedtls_calloc( 1, m_bytes ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4797 | if( *data == NULL ) |
| 4798 | { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4799 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4800 | goto cleanup; |
| 4801 | } |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4802 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4803 | while( key_out_of_range ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4804 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4805 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4806 | if( ( status = psa_key_derivation_output_bytes( operation, *data, m_bytes ) ) != 0 ) |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4807 | goto cleanup; |
| 4808 | |
| 4809 | /* 2. If m is not a multiple of 8 */ |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4810 | if( m % 8 != 0 ) |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4811 | { |
| 4812 | /* Set the most significant |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4813 | * (8 * ceiling(m/8) - m) bits of the first byte in |
| 4814 | * the string to zero. |
| 4815 | */ |
| 4816 | uint8_t clear_bit_mask = ( 1 << ( m % 8 ) ) - 1; |
| 4817 | (*data)[0] &= clear_bit_mask; |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | /* 3. Convert the string to integer k by decoding it as a |
| 4821 | * big-endian byte string. |
| 4822 | */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4823 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &k, *data, m_bytes ) ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4824 | |
| 4825 | /* 4. If k > N - 2, discard the result and return to step 1. |
| 4826 | * Result of comparison is returned. When it indicates error |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 4827 | * then this function is called again. |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4828 | */ |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4829 | 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] | 4830 | } |
| 4831 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4832 | /* 5. Output k + 1 as the private key. */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4833 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &k, &k, 1 ) ); |
| 4834 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &k, *data, m_bytes ) ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4835 | cleanup: |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4836 | if( ret != 0 ) |
Przemyslaw Stekiel | dc8d7d9 | 2021-12-02 09:15:20 +0100 | [diff] [blame] | 4837 | status = mbedtls_to_psa_error( ret ); |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4838 | if( status != PSA_SUCCESS ) { |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4839 | mbedtls_free( *data ); |
| 4840 | *data = NULL; |
| 4841 | } |
| 4842 | mbedtls_mpi_free( &k ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4843 | mbedtls_mpi_free( &diff_N_2 ); |
Przemyslaw Stekiel | dc8d7d9 | 2021-12-02 09:15:20 +0100 | [diff] [blame] | 4844 | return( status ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4845 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4846 | |
| 4847 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length |
| 4848 | * is determined by the curve, and sets the mandatory bits accordingly. That is: |
| 4849 | * |
| 4850 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): |
| 4851 | * draw a 32-byte string and process it as specified in |
| 4852 | * Elliptic Curves for Security [RFC7748] §5. |
| 4853 | * |
| 4854 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): |
| 4855 | * draw a 56-byte string and process it as specified in [RFC7748] §5. |
| 4856 | * |
| 4857 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 4858 | * always NULL. |
| 4859 | */ |
| 4860 | |
| 4861 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 4862 | size_t bits, |
| 4863 | psa_key_derivation_operation_t *operation, |
| 4864 | uint8_t **data |
| 4865 | ) |
| 4866 | { |
| 4867 | size_t output_length; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4868 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4869 | |
| 4870 | switch( bits ) |
| 4871 | { |
| 4872 | case 255: |
| 4873 | output_length = 32; |
| 4874 | break; |
| 4875 | case 448: |
| 4876 | output_length = 56; |
| 4877 | break; |
| 4878 | default: |
| 4879 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4880 | break; |
| 4881 | } |
| 4882 | |
| 4883 | *data = mbedtls_calloc( 1, output_length ); |
| 4884 | |
| 4885 | if( *data == NULL ) |
| 4886 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4887 | |
| 4888 | status = psa_key_derivation_output_bytes( operation, *data, output_length ); |
| 4889 | |
| 4890 | if( status != PSA_SUCCESS ) |
| 4891 | return status; |
| 4892 | |
| 4893 | switch( bits ) |
| 4894 | { |
| 4895 | case 255: |
| 4896 | (*data)[0] &= 248; |
| 4897 | (*data)[31] &= 127; |
| 4898 | (*data)[31] |= 64; |
| 4899 | break; |
| 4900 | case 448: |
| 4901 | (*data)[0] &= 252; |
| 4902 | (*data)[55] |= 128; |
| 4903 | break; |
| 4904 | default: |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4905 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4906 | break; |
| 4907 | } |
| 4908 | |
| 4909 | return status; |
| 4910 | } |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4911 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 4912 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 4913 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4914 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 4915 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4916 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4917 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4918 | psa_key_slot_t *slot, |
| 4919 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4920 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4921 | { |
| 4922 | uint8_t *data = NULL; |
| 4923 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4924 | size_t storage_size = bytes; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4925 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4926 | |
Przemek Stekiel | dcab6cc | 2022-03-01 14:22:29 +0100 | [diff] [blame] | 4927 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4928 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4929 | |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4930 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 4931 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 4932 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4933 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 4934 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4935 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4936 | { |
Przemyslaw Stekiel | 9248159 | 2022-01-04 10:09:46 +0100 | [diff] [blame] | 4937 | 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] | 4938 | if( PSA_ECC_FAMILY_IS_WEIERSTRASS( curve ) ) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4939 | { |
| 4940 | /* Weierstrass elliptic curve */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4941 | status = psa_generate_derived_ecc_key_weierstrass_helper( slot, bits, operation, &data ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4942 | if( status != PSA_SUCCESS ) |
| 4943 | goto exit; |
Przemyslaw Stekiel | 50fcc53 | 2021-11-26 10:54:52 +0100 | [diff] [blame] | 4944 | } |
| 4945 | else |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4946 | { |
| 4947 | /* Montgomery elliptic curve */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4948 | status = psa_generate_derived_ecc_key_montgomery_helper( bits, operation, &data ); |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4949 | if( status != PSA_SUCCESS ) |
| 4950 | goto exit; |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4951 | } |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4952 | } else |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4953 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 4954 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 4955 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4956 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 4957 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4958 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4959 | { |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4960 | if( bits % 8 != 0 ) |
| 4961 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4962 | data = mbedtls_calloc( 1, bytes ); |
| 4963 | if( data == NULL ) |
| 4964 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4965 | |
| 4966 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
| 4967 | if( status != PSA_SUCCESS ) |
| 4968 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4969 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4970 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
| 4971 | psa_des_set_key_parity( data, bytes ); |
Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 4972 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4973 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4974 | else |
Przemek Stekiel | dcab6cc | 2022-03-01 14:22:29 +0100 | [diff] [blame] | 4975 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4976 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4977 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4978 | psa_key_attributes_t attributes = { |
| 4979 | .core = slot->attr |
| 4980 | }; |
| 4981 | |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4982 | if( psa_key_lifetime_is_external( attributes.core.lifetime ) ) |
| 4983 | { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 4984 | status = psa_driver_wrapper_get_key_buffer_size( &attributes, |
| 4985 | &storage_size ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4986 | if( status != PSA_SUCCESS ) |
| 4987 | goto exit; |
| 4988 | } |
| 4989 | status = psa_allocate_buffer_to_slot( slot, storage_size ); |
| 4990 | if( status != PSA_SUCCESS ) |
| 4991 | goto exit; |
| 4992 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4993 | status = psa_driver_wrapper_import_key( &attributes, |
| 4994 | data, bytes, |
| 4995 | slot->key.data, |
| 4996 | slot->key.bytes, |
| 4997 | &slot->key.bytes, &bits ); |
| 4998 | if( bits != slot->attr.bits ) |
| 4999 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5000 | |
| 5001 | exit: |
| 5002 | mbedtls_free( data ); |
| 5003 | return( status ); |
| 5004 | } |
| 5005 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5006 | 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] | 5007 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5008 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5009 | { |
| 5010 | psa_status_t status; |
| 5011 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5012 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5013 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5014 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5015 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5016 | /* Reject any attempt to create a zero-length key so that we don't |
| 5017 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5018 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5019 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5020 | |
Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 5021 | if( operation->alg == PSA_ALG_NONE ) |
| 5022 | return( PSA_ERROR_BAD_STATE ); |
| 5023 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5024 | if( ! operation->can_output_key ) |
| 5025 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5026 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5027 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5028 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5029 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5030 | if( driver != NULL ) |
| 5031 | { |
| 5032 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5033 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5034 | } |
| 5035 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5036 | if( status == PSA_SUCCESS ) |
| 5037 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5038 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5039 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5040 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5041 | } |
| 5042 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5043 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5044 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5045 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5046 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5047 | return( status ); |
| 5048 | } |
| 5049 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5050 | |
| 5051 | |
| 5052 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5053 | /* Key derivation */ |
| 5054 | /****************************************************************/ |
| 5055 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5056 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5057 | static int is_kdf_alg_supported( psa_algorithm_t kdf_alg ) |
| 5058 | { |
| 5059 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 5060 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5061 | return( 1 ); |
| 5062 | #endif |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5063 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 5064 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5065 | return( 1 ); |
| 5066 | #endif |
| 5067 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
| 5068 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5069 | return( 1 ); |
| 5070 | #endif |
| 5071 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5072 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5073 | return( 1 ); |
| 5074 | #endif |
| 5075 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5076 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5077 | return( 1 ); |
| 5078 | #endif |
| 5079 | return( 0 ); |
| 5080 | } |
| 5081 | |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5082 | static psa_status_t psa_hash_try_support( psa_algorithm_t alg ) |
| 5083 | { |
| 5084 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 5085 | psa_status_t status = psa_hash_setup( &operation, alg ); |
| 5086 | psa_hash_abort( &operation ); |
| 5087 | return( status ); |
| 5088 | } |
| 5089 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5090 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5091 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5092 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5093 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5094 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5095 | * initialisers for this union leave some bytes unspecified.) */ |
| 5096 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5097 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5098 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5099 | if( ! is_kdf_alg_supported( kdf_alg ) ) |
| 5100 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5101 | |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5102 | /* All currently supported key derivation algorithms are based on a |
| 5103 | * hash algorithm. */ |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5104 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
| 5105 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
| 5106 | if( hash_size == 0 ) |
| 5107 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5108 | |
| 5109 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 5110 | * we might fail later, which is somewhat unfriendly and potentially |
| 5111 | * risk-prone. */ |
| 5112 | psa_status_t status = psa_hash_try_support( hash_alg ); |
| 5113 | if( status != PSA_SUCCESS ) |
| 5114 | return( status ); |
| 5115 | |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5116 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5117 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
| 5118 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5119 | { |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5120 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5121 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5122 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5123 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5124 | operation->capacity = hash_size; |
| 5125 | else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5126 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5127 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5128 | return( PSA_SUCCESS ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5129 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5130 | |
| 5131 | static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg ) |
| 5132 | { |
| 5133 | #if defined(PSA_WANT_ALG_ECDH) |
| 5134 | if( alg == PSA_ALG_ECDH ) |
| 5135 | return( PSA_SUCCESS ); |
| 5136 | #endif |
| 5137 | (void) alg; |
| 5138 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5139 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5140 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5141 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5142 | 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] | 5143 | psa_algorithm_t alg ) |
| 5144 | { |
| 5145 | psa_status_t status; |
| 5146 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5147 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5148 | return( PSA_ERROR_BAD_STATE ); |
| 5149 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5150 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5151 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5152 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5153 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5154 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5155 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5156 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ); |
| 5157 | status = psa_key_agreement_try_support( ka_alg ); |
| 5158 | if( status != PSA_SUCCESS ) |
| 5159 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5160 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5161 | #else |
| 5162 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5163 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5164 | } |
| 5165 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5166 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5167 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5168 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5169 | #else |
| 5170 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5171 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5172 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5173 | else |
| 5174 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5175 | |
| 5176 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5177 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5178 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5179 | } |
| 5180 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5181 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5182 | 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] | 5183 | psa_algorithm_t kdf_alg, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5184 | psa_key_derivation_step_t step, |
| 5185 | const uint8_t *data, |
| 5186 | size_t data_length ) |
| 5187 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5188 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5189 | psa_status_t status; |
| 5190 | switch( step ) |
| 5191 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5192 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5193 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5194 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
| 5195 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5196 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5197 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5198 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5199 | else |
| 5200 | { |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5201 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 5202 | hash_alg, |
| 5203 | data, data_length ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5204 | if( status != PSA_SUCCESS ) |
| 5205 | return( status ); |
| 5206 | hkdf->state = HKDF_STATE_STARTED; |
| 5207 | return( PSA_SUCCESS ); |
| 5208 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5209 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5210 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5211 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5212 | { |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 5213 | /* We shouldn't be in different state as HKDF_EXPAND only allows |
| 5214 | * two inputs: SECRET (this case) and INFO which does not modify |
| 5215 | * the state. It could happen only if the hkdf |
| 5216 | * object was corrupted. */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5217 | if( hkdf->state != HKDF_STATE_INIT ) |
| 5218 | return( PSA_ERROR_BAD_STATE ); |
| 5219 | |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 5220 | /* Allow only input that fits expected prk size */ |
| 5221 | if( data_length != PSA_HASH_LENGTH( hash_alg ) ) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5222 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5223 | |
| 5224 | memcpy( hkdf->prk, data, data_length ); |
| 5225 | } |
| 5226 | else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5227 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5228 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5229 | /* HKDF: If no salt was provided, use an empty salt. |
| 5230 | * HKDF-EXTRACT: salt is mandatory. */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5231 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5232 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5233 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 5234 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5235 | return( PSA_ERROR_BAD_STATE ); |
| 5236 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5237 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 5238 | hash_alg, |
| 5239 | NULL, 0 ); |
| 5240 | if( status != PSA_SUCCESS ) |
| 5241 | return( status ); |
| 5242 | hkdf->state = HKDF_STATE_STARTED; |
| 5243 | } |
| 5244 | if( hkdf->state != HKDF_STATE_STARTED ) |
| 5245 | return( PSA_ERROR_BAD_STATE ); |
| 5246 | status = psa_mac_update( &hkdf->hmac, |
| 5247 | data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5248 | if( status != PSA_SUCCESS ) |
| 5249 | return( status ); |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5250 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 5251 | hkdf->prk, |
| 5252 | sizeof( hkdf->prk ), |
| 5253 | &data_length ); |
| 5254 | if( status != PSA_SUCCESS ) |
| 5255 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5256 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5257 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5258 | hkdf->state = HKDF_STATE_KEYED; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5259 | hkdf->block_number = 0; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5260 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5261 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5262 | { |
| 5263 | /* The only block of output is the PRK. */ |
| 5264 | memcpy( hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH( hash_alg ) ); |
| 5265 | hkdf->offset_in_block = 0; |
| 5266 | } |
| 5267 | else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5268 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5269 | { |
| 5270 | /* Block 0 is empty, and the next block will be |
| 5271 | * generated by psa_key_derivation_hkdf_read(). */ |
| 5272 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
| 5273 | } |
| 5274 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5275 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5276 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5277 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5278 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5279 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5280 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 5281 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
| 5282 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) && |
| 5283 | hkdf->state == HKDF_STATE_INIT ) |
| 5284 | return( PSA_ERROR_BAD_STATE ); |
| 5285 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5286 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5287 | return( PSA_ERROR_BAD_STATE ); |
| 5288 | if( hkdf->info_set ) |
| 5289 | return( PSA_ERROR_BAD_STATE ); |
| 5290 | hkdf->info_length = data_length; |
| 5291 | if( data_length != 0 ) |
| 5292 | { |
| 5293 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5294 | if( hkdf->info == NULL ) |
| 5295 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5296 | memcpy( hkdf->info, data, data_length ); |
| 5297 | } |
| 5298 | hkdf->info_set = 1; |
| 5299 | return( PSA_SUCCESS ); |
| 5300 | default: |
| 5301 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5302 | } |
| 5303 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5304 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5305 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5306 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5307 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5308 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5309 | const uint8_t *data, |
| 5310 | size_t data_length ) |
| 5311 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5312 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5313 | return( PSA_ERROR_BAD_STATE ); |
| 5314 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5315 | if( data_length != 0 ) |
| 5316 | { |
| 5317 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5318 | if( prf->seed == NULL ) |
| 5319 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5320 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5321 | memcpy( prf->seed, data, data_length ); |
| 5322 | prf->seed_length = data_length; |
| 5323 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5324 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5325 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5326 | |
| 5327 | return( PSA_SUCCESS ); |
| 5328 | } |
| 5329 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5330 | 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] | 5331 | const uint8_t *data, |
| 5332 | size_t data_length ) |
| 5333 | { |
Przemek Stekiel | d7a2864 | 2022-04-07 14:58:33 +0200 | [diff] [blame] | 5334 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET && |
| 5335 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5336 | return( PSA_ERROR_BAD_STATE ); |
| 5337 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5338 | if( data_length != 0 ) |
| 5339 | { |
| 5340 | prf->secret = mbedtls_calloc( 1, data_length ); |
| 5341 | if( prf->secret == NULL ) |
| 5342 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5343 | |
| 5344 | memcpy( prf->secret, data, data_length ); |
| 5345 | prf->secret_length = data_length; |
| 5346 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5347 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5348 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5349 | |
| 5350 | return( PSA_SUCCESS ); |
| 5351 | } |
| 5352 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5353 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5354 | const uint8_t *data, |
| 5355 | size_t data_length ) |
| 5356 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5357 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5358 | return( PSA_ERROR_BAD_STATE ); |
| 5359 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5360 | if( data_length != 0 ) |
| 5361 | { |
| 5362 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5363 | if( prf->label == NULL ) |
| 5364 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5365 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5366 | memcpy( prf->label, data, data_length ); |
| 5367 | prf->label_length = data_length; |
| 5368 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5369 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5370 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5371 | |
| 5372 | return( PSA_SUCCESS ); |
| 5373 | } |
| 5374 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5375 | 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] | 5376 | psa_key_derivation_step_t step, |
| 5377 | const uint8_t *data, |
| 5378 | size_t data_length ) |
| 5379 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5380 | switch( step ) |
| 5381 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5382 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5383 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5384 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5385 | return( psa_tls12_prf_set_key( prf, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5386 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5387 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5388 | default: |
| 5389 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5390 | } |
| 5391 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5392 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5393 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5394 | |
| 5395 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5396 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5397 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5398 | const uint8_t *data, |
| 5399 | size_t data_length ) |
| 5400 | { |
| 5401 | psa_status_t status; |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5402 | const size_t pms_len = ( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? |
| 5403 | 4 + data_length + prf->other_secret_length : |
| 5404 | 4 + 2 * data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5405 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5406 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5407 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5408 | |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5409 | uint8_t *pms = mbedtls_calloc( 1, pms_len ); |
Przemek Stekiel | 937b90f | 2022-04-20 08:33:13 +0200 | [diff] [blame] | 5410 | if( pms == NULL ) |
| 5411 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5412 | uint8_t *cur = pms; |
| 5413 | |
| 5414 | /* pure-PSK: |
| 5415 | * Quoting RFC 4279, Section 2: |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5416 | * |
| 5417 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5418 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5419 | * uint16 with the value N, and the PSK itself. |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5420 | * |
| 5421 | * mixed-PSK: |
| 5422 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as |
| 5423 | * follows: concatenate a uint16 with the length of the other secret, |
| 5424 | * the other secret itself, uint16 with the length of PSK, and the |
| 5425 | * PSK itself. |
| 5426 | * For details please check: |
| 5427 | * - RFC 4279, Section 4 for the definition of RSA-PSK, |
| 5428 | * - RFC 4279, Section 3 for the definition of DHE-PSK, |
| 5429 | * - RFC 5489 for the definition of ECDHE-PSK. |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5430 | */ |
| 5431 | |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 5432 | if( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ) |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5433 | { |
| 5434 | *cur++ = MBEDTLS_BYTE_1( prf->other_secret_length ); |
| 5435 | *cur++ = MBEDTLS_BYTE_0( prf->other_secret_length ); |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 5436 | if( prf->other_secret_length != 0 ) |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 5437 | { |
| 5438 | memcpy( cur, prf->other_secret, prf->other_secret_length ); |
Przemek Stekiel | 03faf5d2 | 2022-04-20 08:37:43 +0200 | [diff] [blame] | 5439 | mbedtls_platform_zeroize( prf->other_secret, prf->other_secret_length ); |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 5440 | cur += prf->other_secret_length; |
| 5441 | } |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5442 | } |
| 5443 | else |
| 5444 | { |
| 5445 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 5446 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
| 5447 | memset( cur, 0, data_length ); |
| 5448 | cur += data_length; |
| 5449 | } |
| 5450 | |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 5451 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 5452 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5453 | memcpy( cur, data, data_length ); |
| 5454 | cur += data_length; |
| 5455 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5456 | status = psa_tls12_prf_set_key( prf, pms, cur - pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5457 | |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5458 | mbedtls_platform_zeroize( pms, pms_len ); |
| 5459 | mbedtls_free( pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5460 | return( status ); |
| 5461 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5462 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5463 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( |
| 5464 | psa_tls12_prf_key_derivation_t *prf, |
| 5465 | const uint8_t *data, |
| 5466 | size_t data_length ) |
| 5467 | { |
| 5468 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
| 5469 | return( PSA_ERROR_BAD_STATE ); |
| 5470 | |
| 5471 | if( data_length != 0 ) |
| 5472 | { |
| 5473 | prf->other_secret = mbedtls_calloc( 1, data_length ); |
| 5474 | if( prf->other_secret == NULL ) |
| 5475 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5476 | |
| 5477 | memcpy( prf->other_secret, data, data_length ); |
| 5478 | prf->other_secret_length = data_length; |
| 5479 | } |
| 5480 | else |
| 5481 | { |
| 5482 | prf->other_secret_length = 0; |
| 5483 | } |
| 5484 | |
| 5485 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; |
| 5486 | |
| 5487 | return( PSA_SUCCESS ); |
| 5488 | } |
| 5489 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5490 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5491 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5492 | psa_key_derivation_step_t step, |
| 5493 | const uint8_t *data, |
| 5494 | size_t data_length ) |
| 5495 | { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5496 | switch( step ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5497 | { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5498 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5499 | return( psa_tls12_prf_psk_to_ms_set_key( prf, |
| 5500 | data, data_length ) ); |
| 5501 | break; |
| 5502 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
| 5503 | return( psa_tls12_prf_psk_to_ms_set_other_key( prf, |
| 5504 | data, |
| 5505 | data_length ) ); |
| 5506 | break; |
| 5507 | default: |
| 5508 | return( psa_tls12_prf_input( prf, step, data, data_length ) ); |
| 5509 | break; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5510 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5511 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5512 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5513 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5514 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5515 | /** Check whether the given key type is acceptable for the given |
| 5516 | * input step of a key derivation. |
| 5517 | * |
| 5518 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5519 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5520 | * Both secret and non-secret inputs can alternatively have the type |
| 5521 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5522 | * that the input was passed as a buffer rather than via a key object. |
| 5523 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5524 | static int psa_key_derivation_check_input_type( |
| 5525 | psa_key_derivation_step_t step, |
| 5526 | psa_key_type_t key_type ) |
| 5527 | { |
| 5528 | switch( step ) |
| 5529 | { |
| 5530 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5531 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5532 | return( PSA_SUCCESS ); |
| 5533 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5534 | return( PSA_SUCCESS ); |
| 5535 | break; |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 5536 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
| 5537 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5538 | return( PSA_SUCCESS ); |
| 5539 | if( key_type == PSA_KEY_TYPE_NONE ) |
| 5540 | return( PSA_SUCCESS ); |
| 5541 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5542 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5543 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5544 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5545 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5546 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5547 | return( PSA_SUCCESS ); |
| 5548 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5549 | return( PSA_SUCCESS ); |
| 5550 | break; |
| 5551 | } |
| 5552 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5553 | } |
| 5554 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5555 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5556 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5557 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5558 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5559 | const uint8_t *data, |
| 5560 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5561 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5562 | psa_status_t status; |
| 5563 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5564 | |
| 5565 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5566 | if( status != PSA_SUCCESS ) |
| 5567 | goto exit; |
| 5568 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5569 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 5570 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5571 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5572 | status = psa_hkdf_input( &operation->ctx.hkdf, kdf_alg, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5573 | step, data, data_length ); |
| 5574 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5575 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5576 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5577 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5578 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5579 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5580 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5581 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5582 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5583 | else |
| 5584 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 5585 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5586 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5587 | { |
| 5588 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5589 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5590 | } |
| 5591 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5592 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5593 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5594 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5595 | (void) data; |
| 5596 | (void) data_length; |
| 5597 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5598 | return( PSA_ERROR_BAD_STATE ); |
| 5599 | } |
| 5600 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5601 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5602 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5603 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5604 | return( status ); |
| 5605 | } |
| 5606 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5607 | psa_status_t psa_key_derivation_input_bytes( |
| 5608 | psa_key_derivation_operation_t *operation, |
| 5609 | psa_key_derivation_step_t step, |
| 5610 | const uint8_t *data, |
| 5611 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5612 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5613 | return( psa_key_derivation_input_internal( operation, step, |
| 5614 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5615 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5616 | } |
| 5617 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5618 | psa_status_t psa_key_derivation_input_key( |
| 5619 | psa_key_derivation_operation_t *operation, |
| 5620 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5621 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5622 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5623 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5624 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5625 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5626 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5627 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5628 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5629 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5630 | { |
| 5631 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5632 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5633 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5634 | |
| 5635 | /* Passing a key object as a SECRET input unlocks the permission |
| 5636 | * to output to a key object. */ |
| 5637 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5638 | operation->can_output_key = 1; |
| 5639 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5640 | status = psa_key_derivation_input_internal( operation, |
| 5641 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5642 | slot->key.data, |
| 5643 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5644 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5645 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5646 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5647 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5648 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5649 | |
| 5650 | |
| 5651 | |
| 5652 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5653 | /* Key agreement */ |
| 5654 | /****************************************************************/ |
| 5655 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5656 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5657 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5658 | size_t peer_key_length, |
| 5659 | const mbedtls_ecp_keypair *our_key, |
| 5660 | uint8_t *shared_secret, |
| 5661 | size_t shared_secret_size, |
| 5662 | size_t *shared_secret_length ) |
| 5663 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5664 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5665 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5666 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5667 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5668 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5669 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5670 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5671 | status = mbedtls_psa_ecp_load_representation( |
| 5672 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5673 | bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5674 | peer_key, |
| 5675 | peer_key_length, |
| 5676 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5677 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5678 | goto exit; |
| 5679 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5680 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5681 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5682 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5683 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5684 | status = mbedtls_to_psa_error( |
| 5685 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5686 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5687 | goto exit; |
| 5688 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5689 | status = mbedtls_to_psa_error( |
| 5690 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5691 | shared_secret_length, |
| 5692 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5693 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5694 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5695 | if( status != PSA_SUCCESS ) |
| 5696 | goto exit; |
| 5697 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5698 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5699 | |
| 5700 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5701 | if( status != PSA_SUCCESS ) |
| 5702 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5703 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5704 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 5705 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5706 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5707 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5708 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5709 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5710 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5711 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5712 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5713 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5714 | psa_key_slot_t *private_key, |
| 5715 | const uint8_t *peer_key, |
| 5716 | size_t peer_key_length, |
| 5717 | uint8_t *shared_secret, |
| 5718 | size_t shared_secret_size, |
| 5719 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5720 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5721 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5722 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5723 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5724 | case PSA_ALG_ECDH: |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 5725 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5726 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5727 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5728 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
| 5729 | private_key->attr.type, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5730 | private_key->attr.bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5731 | private_key->key.data, |
| 5732 | private_key->key.bytes, |
| 5733 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5734 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5735 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5736 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5737 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5738 | shared_secret, shared_secret_size, |
| 5739 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5740 | mbedtls_ecp_keypair_free( ecp ); |
| 5741 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5742 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5743 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5744 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5745 | (void) private_key; |
| 5746 | (void) peer_key; |
| 5747 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5748 | (void) shared_secret; |
| 5749 | (void) shared_secret_size; |
| 5750 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5751 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5752 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5753 | } |
| 5754 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5755 | /* Note that if this function fails, you must call psa_key_derivation_abort() |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5756 | * to potentially free embedded data structures and wipe confidential data. |
| 5757 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5758 | 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] | 5759 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5760 | psa_key_slot_t *private_key, |
| 5761 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5762 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5763 | { |
| 5764 | psa_status_t status; |
| 5765 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5766 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5767 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5768 | |
| 5769 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5770 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5771 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5772 | private_key, |
| 5773 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5774 | shared_secret, |
| 5775 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5776 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5777 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5778 | goto exit; |
| 5779 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5780 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5781 | * the shared secret. A shared secret is permitted wherever a key |
| 5782 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5783 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5784 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5785 | shared_secret, |
| 5786 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5787 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5788 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5789 | return( status ); |
| 5790 | } |
| 5791 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5792 | 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] | 5793 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5794 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5795 | const uint8_t *peer_key, |
| 5796 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5797 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5798 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5799 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5800 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5801 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5802 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5803 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5804 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5805 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5806 | if( status != PSA_SUCCESS ) |
| 5807 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5808 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5809 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5810 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5811 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5812 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5813 | else |
| 5814 | { |
| 5815 | /* If a private key has been added as SECRET, we allow the derived |
| 5816 | * key material to be used as a key in PSA Crypto. */ |
| 5817 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5818 | operation->can_output_key = 1; |
| 5819 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5820 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5821 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5822 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5823 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5824 | } |
| 5825 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5826 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5827 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5828 | const uint8_t *peer_key, |
| 5829 | size_t peer_key_length, |
| 5830 | uint8_t *output, |
| 5831 | size_t output_size, |
| 5832 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5833 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5834 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5835 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5836 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5837 | |
| 5838 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5839 | { |
| 5840 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5841 | goto exit; |
| 5842 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5843 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5844 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5845 | if( status != PSA_SUCCESS ) |
| 5846 | goto exit; |
| 5847 | |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5848 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 5849 | * for the output size. The PSA specification only guarantees that this |
| 5850 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 5851 | * but it might be nice to allow smaller buffers if the output fits. |
| 5852 | * At the time of writing this comment, with only ECDH implemented, |
| 5853 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 5854 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 5855 | * be exact for it as well. */ |
| 5856 | size_t expected_length = |
| 5857 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits ); |
| 5858 | if( output_size < expected_length ) |
| 5859 | { |
| 5860 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5861 | goto exit; |
| 5862 | } |
| 5863 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5864 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5865 | peer_key, peer_key_length, |
| 5866 | output, output_size, |
| 5867 | output_length ); |
| 5868 | |
| 5869 | exit: |
| 5870 | if( status != PSA_SUCCESS ) |
| 5871 | { |
| 5872 | /* If an error happens and is not handled properly, the output |
| 5873 | * may be used as a key to protect sensitive data. Arrange for such |
| 5874 | * a key to be random, which is likely to result in decryption or |
| 5875 | * verification errors. This is better than filling the buffer with |
| 5876 | * some constant data such as zeros, which would result in the data |
| 5877 | * being protected with a reproducible, easily knowable key. |
| 5878 | */ |
| 5879 | psa_generate_random( output, output_size ); |
| 5880 | *output_length = output_size; |
| 5881 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5882 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5883 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5884 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5885 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5886 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5887 | |
| 5888 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5889 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5890 | /****************************************************************/ |
| 5891 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5892 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5893 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5894 | /** Initialize the PSA random generator. |
| 5895 | */ |
| 5896 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 5897 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5898 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5899 | memset( rng, 0, sizeof( *rng ) ); |
| 5900 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5901 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5902 | /* Set default configuration if |
| 5903 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 5904 | if( rng->entropy_init == NULL ) |
| 5905 | rng->entropy_init = mbedtls_entropy_init; |
| 5906 | if( rng->entropy_free == NULL ) |
| 5907 | rng->entropy_free = mbedtls_entropy_free; |
| 5908 | |
| 5909 | rng->entropy_init( &rng->entropy ); |
| 5910 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5911 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5912 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5913 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 5914 | mbedtls_entropy_add_source( &rng->entropy, |
| 5915 | mbedtls_nv_seed_poll, NULL, |
| 5916 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5917 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 5918 | #endif |
| 5919 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5920 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5921 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5922 | } |
| 5923 | |
| 5924 | /** Deinitialize the PSA random generator. |
| 5925 | */ |
| 5926 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 5927 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5928 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5929 | memset( rng, 0, sizeof( *rng ) ); |
| 5930 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5931 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5932 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5933 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5934 | } |
| 5935 | |
| 5936 | /** Seed the PSA random generator. |
| 5937 | */ |
| 5938 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 5939 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5940 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5941 | /* Do nothing: the external RNG seeds itself. */ |
| 5942 | (void) rng; |
| 5943 | return( PSA_SUCCESS ); |
| 5944 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5945 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5946 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 5947 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5948 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5949 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5950 | } |
| 5951 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5952 | psa_status_t psa_generate_random( uint8_t *output, |
| 5953 | size_t output_size ) |
| 5954 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5955 | GUARD_MODULE_INITIALIZED; |
| 5956 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5957 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5958 | |
| 5959 | size_t output_length = 0; |
| 5960 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 5961 | output, output_size, |
| 5962 | &output_length ); |
| 5963 | if( status != PSA_SUCCESS ) |
| 5964 | return( status ); |
| 5965 | /* Breaking up a request into smaller chunks is currently not supported |
| 5966 | * for the extrernal RNG interface. */ |
| 5967 | if( output_length != output_size ) |
| 5968 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 5969 | return( PSA_SUCCESS ); |
| 5970 | |
| 5971 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5972 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5973 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5974 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5975 | size_t request_size = |
| 5976 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 5977 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 5978 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 5979 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 5980 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5981 | if( ret != 0 ) |
| 5982 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5983 | output_size -= request_size; |
| 5984 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5985 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 5986 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5987 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5988 | } |
| 5989 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5990 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5991 | * |
| 5992 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 5993 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 5994 | * PSA API doesn't support passing an explicit state. |
| 5995 | * |
| 5996 | * In the non-external case, psa_generate_random() calls an |
| 5997 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 5998 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 5999 | * instead of doing this back-and-forth between the PSA API and the |
| 6000 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6001 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6002 | */ |
| 6003 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6004 | int mbedtls_psa_get_random( void *p_rng, |
| 6005 | unsigned char *output, |
| 6006 | size_t output_size ) |
| 6007 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6008 | /* This function takes a pointer to the RNG state because that's what |
| 6009 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6010 | * its own state internally and doesn't let the caller access that state. |
| 6011 | * So we just ignore the state parameter, and in practice we'll pass |
| 6012 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6013 | (void) p_rng; |
| 6014 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6015 | if( status == PSA_SUCCESS ) |
| 6016 | return( 0 ); |
| 6017 | else |
| 6018 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6019 | } |
| 6020 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6021 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6022 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Chris Jones | ea0a865 | 2021-03-09 19:11:19 +0000 | [diff] [blame] | 6023 | #include "entropy_poll.h" |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6024 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6025 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6026 | size_t seed_size ) |
| 6027 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6028 | if( global_data.initialized ) |
| 6029 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6030 | |
| 6031 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6032 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6033 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6034 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6035 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6036 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6037 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6038 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6039 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6040 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6041 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6042 | * \param type The key type |
| 6043 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6044 | * |
| 6045 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6046 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6047 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 6048 | * The size in bits of the key is not valid. |
| 6049 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 6050 | * The type and/or the size in bits of the key or the combination of |
| 6051 | * the two is not supported. |
| 6052 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6053 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 6054 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6055 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6056 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6057 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6058 | if( key_type_is_raw_bytes( type ) ) |
| 6059 | { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 6060 | status = psa_validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6061 | if( status != PSA_SUCCESS ) |
| 6062 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6063 | } |
| 6064 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6065 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6066 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6067 | { |
| 6068 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6069 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6070 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6071 | /* Accept only byte-aligned keys, for the same reasons as |
| 6072 | * in psa_import_rsa_key(). */ |
| 6073 | if( bits % 8 != 0 ) |
| 6074 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6075 | } |
| 6076 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6077 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6078 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6079 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6080 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6081 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 6082 | /* To avoid empty block, return successfully here. */ |
| 6083 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6084 | } |
| 6085 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6086 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6087 | { |
| 6088 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6089 | } |
| 6090 | |
| 6091 | return( PSA_SUCCESS ); |
| 6092 | } |
| 6093 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 6094 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6095 | const psa_key_attributes_t *attributes, |
| 6096 | 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] | 6097 | { |
| 6098 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6099 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6100 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6101 | if( ( attributes->domain_parameters == NULL ) && |
| 6102 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6103 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6104 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6105 | if( key_type_is_raw_bytes( type ) ) |
| 6106 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6107 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6108 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6109 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6110 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6111 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6112 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6113 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 6114 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6115 | } |
| 6116 | else |
| 6117 | |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6118 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 6119 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6120 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6121 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 6122 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 6123 | key_buffer, |
| 6124 | key_buffer_size, |
| 6125 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6126 | } |
| 6127 | else |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6128 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 6129 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6130 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6131 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6132 | 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] | 6133 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 6134 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 6135 | key_buffer, |
| 6136 | key_buffer_size, |
| 6137 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6138 | } |
| 6139 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6140 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6141 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6142 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6143 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6144 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6145 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6146 | return( PSA_SUCCESS ); |
| 6147 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6148 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6149 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6150 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6151 | { |
| 6152 | psa_status_t status; |
| 6153 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6154 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6155 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6156 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6157 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6158 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6159 | /* Reject any attempt to create a zero-length key so that we don't |
| 6160 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6161 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6162 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6163 | |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6164 | /* Reject any attempt to create a public key. */ |
| 6165 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) ) |
| 6166 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6167 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6168 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6169 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6170 | if( status != PSA_SUCCESS ) |
| 6171 | goto exit; |
| 6172 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6173 | /* 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] | 6174 | * storage ( thus not in the case of generating a key in a secure element |
| 6175 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 6176 | * buffer to hold the generated key material. */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6177 | if( slot->key.data == NULL ) |
| 6178 | { |
| 6179 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 6180 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 6181 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6182 | status = psa_validate_key_type_and_size_for_key_generation( |
| 6183 | attributes->core.type, attributes->core.bits ); |
| 6184 | if( status != PSA_SUCCESS ) |
| 6185 | goto exit; |
| 6186 | |
| 6187 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 6188 | attributes->core.type, |
| 6189 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6190 | } |
| 6191 | else |
| 6192 | { |
| 6193 | status = psa_driver_wrapper_get_key_buffer_size( |
| 6194 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6195 | if( status != PSA_SUCCESS ) |
| 6196 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6197 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6198 | |
| 6199 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 6200 | if( status != PSA_SUCCESS ) |
| 6201 | goto exit; |
| 6202 | } |
| 6203 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6204 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6205 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6206 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6207 | if( status != PSA_SUCCESS ) |
| 6208 | psa_remove_key_data_from_memory( slot ); |
| 6209 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6210 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6211 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6212 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6213 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6214 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6215 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6216 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6217 | } |
| 6218 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6219 | /****************************************************************/ |
| 6220 | /* Module setup */ |
| 6221 | /****************************************************************/ |
| 6222 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6223 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6224 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6225 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6226 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6227 | { |
| 6228 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6229 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6230 | global_data.rng.entropy_init = entropy_init; |
| 6231 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6232 | return( PSA_SUCCESS ); |
| 6233 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6234 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6235 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6236 | void mbedtls_psa_crypto_free( void ) |
| 6237 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6238 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6239 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6240 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6241 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6242 | } |
| 6243 | /* Wipe all remaining data, including configuration. |
| 6244 | * In particular, this sets all state indicator to the value |
| 6245 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6246 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 6247 | |
| 6248 | /* Terminate drivers */ |
| 6249 | psa_driver_wrapper_free( ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6250 | } |
| 6251 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6252 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6253 | /** Recover a transaction that was interrupted by a power failure. |
| 6254 | * |
| 6255 | * This function is called during initialization, before psa_crypto_init() |
| 6256 | * returns. If this function returns a failure status, the initialization |
| 6257 | * fails. |
| 6258 | */ |
| 6259 | static psa_status_t psa_crypto_recover_transaction( |
| 6260 | const psa_crypto_transaction_t *transaction ) |
| 6261 | { |
| 6262 | switch( transaction->unknown.type ) |
| 6263 | { |
| 6264 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6265 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6266 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6267 | * is implemented. |
| 6268 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6269 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6270 | default: |
| 6271 | /* We found an unsupported transaction in the storage. |
| 6272 | * 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] | 6273 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6274 | } |
| 6275 | } |
| 6276 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6277 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6278 | psa_status_t psa_crypto_init( void ) |
| 6279 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6280 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6281 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6282 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6283 | if( global_data.initialized != 0 ) |
| 6284 | return( PSA_SUCCESS ); |
| 6285 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6286 | /* Initialize and seed the random generator. */ |
| 6287 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6288 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6289 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6290 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6291 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6292 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6293 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6294 | status = psa_initialize_key_slots( ); |
| 6295 | if( status != PSA_SUCCESS ) |
| 6296 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6297 | |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 6298 | /* Init drivers */ |
| 6299 | status = psa_driver_wrapper_init( ); |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6300 | if( status != PSA_SUCCESS ) |
| 6301 | goto exit; |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6302 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6303 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6304 | status = psa_crypto_load_transaction( ); |
| 6305 | if( status == PSA_SUCCESS ) |
| 6306 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6307 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6308 | if( status != PSA_SUCCESS ) |
| 6309 | goto exit; |
| 6310 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6311 | } |
| 6312 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6313 | { |
| 6314 | /* There's no transaction to complete. It's all good. */ |
| 6315 | status = PSA_SUCCESS; |
| 6316 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6317 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6318 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6319 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6320 | global_data.initialized = 1; |
| 6321 | |
| 6322 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6323 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6324 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6325 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6326 | } |
| 6327 | |
| 6328 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |