Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 21 | #include "common.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 24 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 25 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 26 | #include "check_crypto_config.h" |
| 27 | #endif |
| 28 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 29 | #include "psa/crypto.h" |
Przemyslaw Stekiel | 705fb0f | 2021-11-24 08:47:29 +0100 | [diff] [blame] | 30 | #include "psa/crypto_values.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 31 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 32 | #include "psa_crypto_cipher.h" |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 33 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 34 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 35 | #include "psa_crypto_driver_wrappers.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 36 | #include "psa_crypto_ecp.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 37 | #include "psa_crypto_hash.h" |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 38 | #include "psa_crypto_mac.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 39 | #include "psa_crypto_rsa.h" |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 40 | #include "psa_crypto_ecp.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 42 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 43 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 44 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 45 | /* Include internal declarations that are useful for implementing persistently |
| 46 | * stored keys. */ |
| 47 | #include "psa_crypto_storage.h" |
| 48 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 49 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 50 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 51 | #include <assert.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 52 | #include <stdlib.h> |
| 53 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 54 | #include "mbedtls/platform.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 55 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 56 | #include "mbedtls/aes.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 57 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 58 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 59 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 60 | #include "mbedtls/camellia.h" |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 61 | #include "mbedtls/chacha20.h" |
| 62 | #include "mbedtls/chachapoly.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 63 | #include "mbedtls/cipher.h" |
| 64 | #include "mbedtls/ccm.h" |
| 65 | #include "mbedtls/cmac.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 66 | #include "mbedtls/des.h" |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 67 | #include "mbedtls/ecdh.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 68 | #include "mbedtls/ecp.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 69 | #include "mbedtls/entropy.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 70 | #include "mbedtls/error.h" |
| 71 | #include "mbedtls/gcm.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 72 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 73 | #include "mbedtls/md.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 74 | #include "md_wrap.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 75 | #include "mbedtls/pk.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 76 | #include "pk_wrap.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 77 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 78 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 79 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 80 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 81 | #include "mbedtls/sha1.h" |
| 82 | #include "mbedtls/sha256.h" |
| 83 | #include "mbedtls/sha512.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 84 | |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 85 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 86 | |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 87 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 88 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
| 89 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 90 | #define BUILTIN_ALG_ANY_HKDF 1 |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 91 | #endif |
| 92 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 93 | /****************************************************************/ |
| 94 | /* Global data, support functions and library management */ |
| 95 | /****************************************************************/ |
| 96 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 97 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 98 | { |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 99 | return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 102 | /* Values for psa_global_data_t::rng_state */ |
| 103 | #define RNG_NOT_INITIALIZED 0 |
| 104 | #define RNG_INITIALIZED 1 |
| 105 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 106 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 107 | typedef struct |
| 108 | { |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 109 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 110 | unsigned rng_state : 2; |
Gilles Peskine | 2d8a182 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 111 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 112 | } psa_global_data_t; |
| 113 | |
| 114 | static psa_global_data_t global_data; |
| 115 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 116 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 117 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 118 | &global_data.rng.drbg; |
| 119 | #endif |
| 120 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 121 | #define GUARD_MODULE_INITIALIZED \ |
| 122 | if( global_data.initialized == 0 ) \ |
| 123 | return( PSA_ERROR_BAD_STATE ); |
| 124 | |
Steven Cooreman | 0116416 | 2020-07-20 15:31:37 +0200 | [diff] [blame] | 125 | psa_status_t mbedtls_to_psa_error( int ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 126 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 127 | /* Mbed TLS error codes can combine a high-level error code and a |
| 128 | * low-level error code. The low-level error usually reflects the |
| 129 | * root cause better, so dispatch on that preferably. */ |
| 130 | int low_level_ret = - ( -ret & 0x007f ); |
| 131 | switch( low_level_ret != 0 ? low_level_ret : ret ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 132 | { |
| 133 | case 0: |
| 134 | return( PSA_SUCCESS ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 135 | |
| 136 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 137 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 138 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 139 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 140 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 141 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 142 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 143 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 144 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 145 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 146 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 147 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 148 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 149 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 150 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 151 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 152 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 153 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 154 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 155 | |
| 156 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 157 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 158 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 159 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 160 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 161 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 162 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 163 | |
| 164 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 165 | return( PSA_ERROR_BAD_STATE ); |
| 166 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 167 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 168 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 169 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 170 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 171 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 172 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 173 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 174 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 175 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 176 | return( PSA_ERROR_INVALID_PADDING ); |
| 177 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Fredrik Strupe | f90e301 | 2020-09-28 16:11:33 +0200 | [diff] [blame] | 178 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 179 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 180 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 181 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 182 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 183 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 184 | #if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 185 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) ) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 186 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 187 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 188 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 189 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 190 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 191 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 192 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 193 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 194 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 195 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 196 | |
| 197 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 198 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 199 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 200 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 201 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 202 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 203 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 204 | |
| 205 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 206 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Mateusz Starzyk | f28261f | 2021-09-30 16:39:07 +0200 | [diff] [blame] | 207 | case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL: |
| 208 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 209 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 8cac2e6 | 2018-08-21 15:07:38 +0200 | [diff] [blame] | 210 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 211 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 212 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 213 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 214 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 215 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 216 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 217 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 218 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 219 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 220 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 221 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 222 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 223 | #endif |
| 224 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 225 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 226 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 227 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 228 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 229 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 230 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 231 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 232 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 233 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 234 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 235 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 236 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 237 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 238 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 239 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 240 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 241 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 242 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 243 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 244 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 245 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 246 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 248 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 249 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 250 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 251 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 252 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 253 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 254 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 255 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 256 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 257 | return( PSA_ERROR_STORAGE_FAILURE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 258 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 259 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 260 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 261 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 262 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 263 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 264 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 265 | return( PSA_ERROR_NOT_PERMITTED ); |
| 266 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 268 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 269 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 270 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 271 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 272 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 273 | return( PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | f9f1bdf | 2021-06-23 20:32:27 +0200 | [diff] [blame] | 274 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: |
| 275 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 276 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 277 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 278 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 279 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 280 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 281 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 282 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 283 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 284 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 285 | return( PSA_ERROR_INVALID_PADDING ); |
| 286 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 287 | return( PSA_ERROR_HARDWARE_FAILURE ); |
| 288 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 289 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 290 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 291 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 292 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 293 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 294 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 295 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 296 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 297 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 298 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 299 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 300 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 301 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 302 | return( PSA_ERROR_INVALID_ARGUMENT ); |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 303 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 304 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 305 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 306 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 307 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 308 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 309 | return( PSA_ERROR_INVALID_SIGNATURE ); |
| 310 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 311 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 312 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 313 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 314 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 315 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 316 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 317 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 318 | default: |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 319 | return( PSA_ERROR_GENERIC_ERROR ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 323 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 324 | |
| 325 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 326 | /****************************************************************/ |
| 327 | /* Key management */ |
| 328 | /****************************************************************/ |
| 329 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 330 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 331 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 332 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 333 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 334 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 335 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve, |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 336 | size_t bits, |
| 337 | int bits_is_sloppy ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 338 | { |
| 339 | switch( curve ) |
| 340 | { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 341 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 342 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 343 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 344 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 345 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 346 | return( MBEDTLS_ECP_DP_SECP192R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 347 | #endif |
| 348 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 349 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 350 | return( MBEDTLS_ECP_DP_SECP224R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 351 | #endif |
| 352 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 353 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 354 | return( MBEDTLS_ECP_DP_SECP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 355 | #endif |
| 356 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 357 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 358 | return( MBEDTLS_ECP_DP_SECP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 359 | #endif |
| 360 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 361 | case 521: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 362 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 363 | case 528: |
| 364 | if( bits_is_sloppy ) |
| 365 | return( MBEDTLS_ECP_DP_SECP521R1 ); |
| 366 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 367 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 368 | } |
| 369 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 370 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 371 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 372 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 373 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 374 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 375 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 376 | return( MBEDTLS_ECP_DP_BP256R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 377 | #endif |
| 378 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 379 | case 384: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 380 | return( MBEDTLS_ECP_DP_BP384R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 381 | #endif |
| 382 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 383 | case 512: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 384 | return( MBEDTLS_ECP_DP_BP512R1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 385 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 386 | } |
| 387 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 388 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 389 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 390 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 391 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 392 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 393 | case 255: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 394 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 395 | case 256: |
| 396 | if( bits_is_sloppy ) |
| 397 | return( MBEDTLS_ECP_DP_CURVE25519 ); |
| 398 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 399 | #endif |
| 400 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 401 | case 448: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 402 | return( MBEDTLS_ECP_DP_CURVE448 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 403 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 404 | } |
| 405 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 406 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 407 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 408 | switch( bits ) |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 409 | { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 410 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 411 | case 192: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 412 | return( MBEDTLS_ECP_DP_SECP192K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 413 | #endif |
| 414 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 415 | case 224: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 416 | return( MBEDTLS_ECP_DP_SECP224K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 417 | #endif |
| 418 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 419 | case 256: |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 420 | return( MBEDTLS_ECP_DP_SECP256K1 ); |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 421 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 422 | } |
| 423 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 424 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 425 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 426 | (void) bits_is_sloppy; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 427 | return( MBEDTLS_ECP_DP_NONE ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 428 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 429 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 430 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 431 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 432 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 433 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 434 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 435 | psa_status_t psa_validate_unstructured_key_bit_size( psa_key_type_t type, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 436 | size_t bits ) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 437 | { |
| 438 | /* Check that the bit size is acceptable for the key type */ |
| 439 | switch( type ) |
| 440 | { |
| 441 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 442 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 443 | case PSA_KEY_TYPE_DERIVE: |
Neil Armstrong | 4b5710f | 2022-05-25 11:30:27 +0200 | [diff] [blame] | 444 | case PSA_KEY_TYPE_PASSWORD: |
| 445 | case PSA_KEY_TYPE_PASSWORD_HASH: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 446 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 447 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 448 | case PSA_KEY_TYPE_AES: |
| 449 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 450 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 451 | break; |
| 452 | #endif |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 453 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 454 | case PSA_KEY_TYPE_ARIA: |
| 455 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 456 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 457 | break; |
| 458 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 459 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 460 | case PSA_KEY_TYPE_CAMELLIA: |
| 461 | if( bits != 128 && bits != 192 && bits != 256 ) |
| 462 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 463 | break; |
| 464 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 465 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 466 | case PSA_KEY_TYPE_DES: |
| 467 | if( bits != 64 && bits != 128 && bits != 192 ) |
| 468 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 469 | break; |
| 470 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 471 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 472 | case PSA_KEY_TYPE_CHACHA20: |
| 473 | if( bits != 256 ) |
| 474 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 475 | break; |
| 476 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 477 | default: |
| 478 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 479 | } |
Gilles Peskine | b54979a | 2018-06-21 09:32:47 +0200 | [diff] [blame] | 480 | if( bits % 8 != 0 ) |
| 481 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 482 | |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 483 | return( PSA_SUCCESS ); |
| 484 | } |
| 485 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 486 | /** Check whether a given key type is valid for use with a given MAC algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 487 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 488 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 489 | * when called with the validated \p algorithm and \p key_type is well-defined. |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 490 | * |
| 491 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 492 | * \param[in] key_type The key type of the key to be used with the |
| 493 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 494 | * |
| 495 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 496 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 497 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 498 | * The \p key_type is not valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 499 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 500 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 501 | psa_algorithm_t algorithm, |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 502 | psa_key_type_t key_type ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 503 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 504 | if( PSA_ALG_IS_HMAC( algorithm ) ) |
| 505 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 506 | if( key_type == PSA_KEY_TYPE_HMAC ) |
| 507 | return( PSA_SUCCESS ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 508 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 509 | |
| 510 | if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 511 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 512 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 513 | * key. */ |
| 514 | if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) == |
| 515 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC ) |
| 516 | { |
| 517 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 518 | * the block length (larger than 1) for block ciphers. */ |
| 519 | if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 ) |
| 520 | return( PSA_SUCCESS ); |
| 521 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 522 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 523 | |
| 524 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Steven Cooreman | 7609b1f | 2021-04-06 16:45:06 +0200 | [diff] [blame] | 527 | psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot, |
| 528 | size_t buffer_length ) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 529 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 530 | if( slot->key.data != NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 531 | return( PSA_ERROR_ALREADY_EXISTS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 532 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 533 | slot->key.data = mbedtls_calloc( 1, buffer_length ); |
| 534 | if( slot->key.data == NULL ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 535 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 536 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 537 | slot->key.bytes = buffer_length; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 538 | return( PSA_SUCCESS ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 539 | } |
| 540 | |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 541 | psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot, |
| 542 | const uint8_t* data, |
| 543 | size_t data_length ) |
| 544 | { |
| 545 | psa_status_t status = psa_allocate_buffer_to_slot( slot, |
| 546 | data_length ); |
| 547 | if( status != PSA_SUCCESS ) |
| 548 | return( status ); |
| 549 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 550 | memcpy( slot->key.data, data, data_length ); |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 551 | return( PSA_SUCCESS ); |
| 552 | } |
| 553 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 554 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 555 | const psa_key_attributes_t *attributes, |
| 556 | const uint8_t *data, size_t data_length, |
| 557 | uint8_t *key_buffer, size_t key_buffer_size, |
| 558 | size_t *key_buffer_length, size_t *bits ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 559 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 560 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 561 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 562 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 563 | /* zero-length keys are never supported. */ |
| 564 | if( data_length == 0 ) |
| 565 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 566 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 567 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 568 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 569 | *bits = PSA_BYTES_TO_BITS( data_length ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 570 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 571 | status = psa_validate_unstructured_key_bit_size( attributes->core.type, |
| 572 | *bits ); |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 573 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 574 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 575 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 576 | /* Copy the key material. */ |
| 577 | memcpy( key_buffer, data, data_length ); |
| 578 | *key_buffer_length = data_length; |
| 579 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 580 | |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 581 | return( PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 582 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 583 | else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
Steven Cooreman | 19fd574 | 2020-07-24 23:31:01 +0200 | [diff] [blame] | 584 | { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 585 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 586 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 587 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 588 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 589 | return( mbedtls_psa_ecp_import_key( attributes, |
| 590 | data, data_length, |
| 591 | key_buffer, key_buffer_size, |
| 592 | key_buffer_length, |
| 593 | bits ) ); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 594 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 595 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 596 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 597 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 598 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 599 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 600 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 601 | return( mbedtls_psa_rsa_import_key( attributes, |
| 602 | data, data_length, |
| 603 | key_buffer, key_buffer_size, |
| 604 | key_buffer_length, |
| 605 | bits ) ); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 606 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 607 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 608 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 609 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 610 | |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 611 | return( PSA_ERROR_NOT_SUPPORTED ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 614 | /** Calculate the intersection of two algorithm usage policies. |
| 615 | * |
| 616 | * Return 0 (which allows no operation) on incompatibility. |
| 617 | */ |
| 618 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 619 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 620 | psa_algorithm_t alg1, |
| 621 | psa_algorithm_t alg2 ) |
| 622 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 623 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 624 | if( alg1 == alg2 ) |
| 625 | return( alg1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 626 | /* If the policies are from the same hash-and-sign family, check |
| 627 | * if one is a wildcard. If so the other has the specific algorithm. */ |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 628 | if( PSA_ALG_IS_SIGN_HASH( alg1 ) && |
| 629 | PSA_ALG_IS_SIGN_HASH( alg2 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 630 | ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 631 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 632 | if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH ) |
| 633 | return( alg2 ); |
| 634 | if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH ) |
| 635 | return( alg1 ); |
| 636 | } |
| 637 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 638 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 639 | * restrictive tag length. */ |
| 640 | if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) && |
| 641 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) == |
| 642 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) ) |
| 643 | { |
| 644 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 ); |
| 645 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 646 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 647 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 648 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 649 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 650 | ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 651 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 652 | return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 653 | alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 654 | } |
| 655 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 656 | if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 657 | ( alg1_len <= alg2_len ) ) |
| 658 | { |
| 659 | return( alg2 ); |
| 660 | } |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 661 | if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 662 | ( alg2_len <= alg1_len ) ) |
| 663 | { |
| 664 | return( alg1 ); |
| 665 | } |
| 666 | } |
| 667 | /* If the policies are from the same MAC family, check whether one |
| 668 | * of them is a minimum-MAC-length policy. Calculate the most |
| 669 | * restrictive tag length. */ |
| 670 | if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) && |
| 671 | ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) == |
| 672 | PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) |
| 673 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 674 | /* Validate the combination of key type and algorithm. Since the base |
| 675 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
| 676 | if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) ) |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 677 | return( 0 ); |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 678 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 679 | /* Get the (exact or at-least) output lengths for both sides of the |
| 680 | * requested intersection. None of the currently supported algorithms |
| 681 | * have an output length dependent on the actual key size, so setting it |
| 682 | * to a bogus value of 0 is currently OK. |
| 683 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 684 | * Note that for at-least-this-length wildcard algorithms, the output |
| 685 | * length is set to the shortest allowed length, which allows us to |
| 686 | * calculate the most restrictive tag length for the intersection. */ |
| 687 | size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 ); |
| 688 | size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 ); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 689 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 690 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 691 | /* If both are wildcards, return most restrictive wildcard */ |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 692 | if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) && |
| 693 | ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 694 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 695 | return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 696 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 697 | |
| 698 | /* If only one is an at-least-this-length policy, the intersection would |
| 699 | * be the other (fixed-length) policy as long as said fixed length is |
| 700 | * equal to or larger than the shortest allowed length. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 701 | if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 702 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 703 | return( ( alg1_len <= alg2_len ) ? alg2 : 0 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 704 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 705 | if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 706 | { |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 707 | return( ( alg2_len <= alg1_len ) ? alg1 : 0 ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 708 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 709 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 710 | /* If none of them are wildcards, check whether they define the same tag |
| 711 | * length. This is still possible here when one is default-length and |
| 712 | * the other specific-length. Ensure to always return the |
| 713 | * specific-length version for the intersection. */ |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 714 | if( alg1_len == alg2_len ) |
| 715 | return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 716 | } |
| 717 | /* If the policies are incompatible, allow nothing. */ |
| 718 | return( 0 ); |
| 719 | } |
| 720 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 721 | static int psa_key_algorithm_permits( psa_key_type_t key_type, |
| 722 | psa_algorithm_t policy_alg, |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 723 | psa_algorithm_t requested_alg ) |
| 724 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 725 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 726 | if( requested_alg == policy_alg ) |
| 727 | return( 1 ); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 728 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 729 | * and requested_alg is the same hash-and-sign family with any hash, |
| 730 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 731 | if( PSA_ALG_IS_SIGN_HASH( requested_alg ) && |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 732 | PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH ) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 733 | { |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 734 | return( ( policy_alg & ~PSA_ALG_HASH_MASK ) == |
| 735 | ( requested_alg & ~PSA_ALG_HASH_MASK ) ); |
| 736 | } |
| 737 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 738 | * the requested algorithm, check the requested tag length to be |
| 739 | * equal-length or longer than the wildcard-specified length. */ |
| 740 | if( PSA_ALG_IS_AEAD( policy_alg ) && |
| 741 | PSA_ALG_IS_AEAD( requested_alg ) && |
| 742 | ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) == |
| 743 | PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) && |
Steven Cooreman | d927ed7 | 2021-02-22 19:59:35 +0100 | [diff] [blame] | 744 | ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 745 | { |
| 746 | return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <= |
| 747 | PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) ); |
| 748 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 749 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 750 | * algorithm, check whether their MAC lengths are compatible. */ |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 751 | if( PSA_ALG_IS_MAC( policy_alg ) && |
| 752 | PSA_ALG_IS_MAC( requested_alg ) && |
| 753 | ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) == |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 754 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) ) |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 755 | { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 756 | /* Validate the combination of key type and algorithm. Since the policy |
| 757 | * and requested algorithms are the same, we only need this once. */ |
| 758 | if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) ) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 759 | return( 0 ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 760 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 761 | /* Get both the requested output length for the algorithm which is to be |
| 762 | * verified, and the default output length for the base algorithm. |
| 763 | * Note that none of the currently supported algorithms have an output |
| 764 | * length dependent on actual key size, so setting it to a bogus value |
| 765 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 766 | size_t requested_output_length = PSA_MAC_LENGTH( |
| 767 | key_type, 0, requested_alg ); |
| 768 | size_t default_output_length = PSA_MAC_LENGTH( |
| 769 | key_type, 0, |
| 770 | PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 771 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 772 | /* If the policy is default-length, only allow an algorithm with |
| 773 | * a declared exact-length matching the default. */ |
| 774 | if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 775 | return( requested_output_length == default_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 776 | |
| 777 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 778 | * length exactly matches the default length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 779 | if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 && |
| 780 | PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length ) |
| 781 | { |
| 782 | return( 1 ); |
| 783 | } |
| 784 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 785 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 786 | * check for the requested MAC length to be equal to or longer than the |
| 787 | * minimum allowed length. */ |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 788 | if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) |
| 789 | { |
| 790 | return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <= |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 791 | requested_output_length ); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 792 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 793 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 794 | /* If policy_alg is a generic key agreement operation, then using it for |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 795 | * a key derivation with that key agreement should also be allowed. This |
| 796 | * behaviour is expected to be defined in a future specification version. */ |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 797 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) && |
| 798 | PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) ) |
| 799 | { |
| 800 | return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) == |
| 801 | policy_alg ); |
| 802 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 803 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 804 | return( 0 ); |
| 805 | } |
| 806 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 807 | /** Test whether a policy permits an algorithm. |
| 808 | * |
| 809 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 810 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 811 | * \note This function requires providing the key type for which the policy is |
| 812 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 813 | * have different properties depending on what kind of cipher it is |
| 814 | * combined with. |
| 815 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 816 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 817 | * allowed by the \p policy. |
| 818 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 819 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 820 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 821 | */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 822 | static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy, |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 823 | psa_key_type_t key_type, |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 824 | psa_algorithm_t alg ) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 825 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 826 | /* '0' is not a valid algorithm */ |
| 827 | if( alg == 0 ) |
| 828 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 829 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 830 | /* A requested algorithm cannot be a wildcard. */ |
| 831 | if( PSA_ALG_IS_WILDCARD( alg ) ) |
| 832 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 833 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 834 | if( psa_key_algorithm_permits( key_type, policy->alg, alg ) || |
| 835 | psa_key_algorithm_permits( key_type, policy->alg2, alg ) ) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 836 | return( PSA_SUCCESS ); |
| 837 | else |
| 838 | return( PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 839 | } |
| 840 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 841 | /** Restrict a key policy based on a constraint. |
| 842 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 843 | * \note This function requires providing the key type for which the policy is |
| 844 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 845 | * have different properties depending on what kind of cipher it is |
| 846 | * combined with. |
| 847 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 848 | * \param[in] key_type The key type for which to restrict the policy |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 849 | * \param[in,out] policy The policy to restrict. |
| 850 | * \param[in] constraint The policy constraint to apply. |
| 851 | * |
| 852 | * \retval #PSA_SUCCESS |
| 853 | * \c *policy contains the intersection of the original value of |
| 854 | * \c *policy and \c *constraint. |
| 855 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 856 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 857 | * \c *policy is unchanged. |
| 858 | */ |
| 859 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 860 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 861 | psa_key_policy_t *policy, |
| 862 | const psa_key_policy_t *constraint ) |
| 863 | { |
| 864 | psa_algorithm_t intersection_alg = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 865 | psa_key_policy_algorithm_intersection( key_type, policy->alg, |
| 866 | constraint->alg ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 867 | psa_algorithm_t intersection_alg2 = |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 868 | psa_key_policy_algorithm_intersection( key_type, policy->alg2, |
| 869 | constraint->alg2 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 870 | if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 ) |
| 871 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 872 | if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 ) |
| 873 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 874 | policy->usage &= constraint->usage; |
| 875 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 876 | policy->alg2 = intersection_alg2; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 877 | return( PSA_SUCCESS ); |
| 878 | } |
| 879 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 880 | /** Get the description of a key given its identifier and policy constraints |
| 881 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 882 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 883 | * 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] | 884 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 885 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 886 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 887 | * In case of a persistent key, the function loads the description of the key |
| 888 | * into a key slot if not already done. |
| 889 | * |
| 890 | * On success, the returned key slot is locked. It is the responsibility of |
| 891 | * 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] | 892 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 893 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 894 | mbedtls_svc_key_id_t key, |
| 895 | psa_key_slot_t **p_slot, |
| 896 | psa_key_usage_t usage, |
| 897 | psa_algorithm_t alg ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 898 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 899 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 900 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 901 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 902 | status = psa_get_and_lock_key_slot( key, p_slot ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 903 | if( status != PSA_SUCCESS ) |
| 904 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 905 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 906 | |
| 907 | /* Enforce that usage policy for the key slot contains all the flags |
| 908 | * required by the usage parameter. There is one exception: public |
| 909 | * keys can always be exported, so we treat public key objects as |
| 910 | * if they had the export flag. */ |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 911 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 912 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 913 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 914 | if( ( slot->attr.policy.usage & usage ) != usage ) |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 915 | { |
| 916 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 917 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 918 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 919 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 920 | /* Enforce that the usage policy permits the requested algorithm. */ |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 921 | if( alg != 0 ) |
| 922 | { |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 923 | status = psa_key_policy_permits( &slot->attr.policy, |
| 924 | slot->attr.type, |
| 925 | alg ); |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 926 | if( status != PSA_SUCCESS ) |
| 927 | goto error; |
| 928 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 929 | |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 930 | return( PSA_SUCCESS ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 931 | |
| 932 | error: |
| 933 | *p_slot = NULL; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 934 | psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 935 | |
| 936 | return( status ); |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 937 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 938 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 939 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 940 | * |
| 941 | * 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] | 942 | * available, as opposed to a key in a secure element and/or to be used |
| 943 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 944 | * |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 945 | * This is a temporary function that may be used instead of |
| 946 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 947 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 948 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 949 | * On success, the returned key slot is locked. It is the responsibility of the |
| 950 | * 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] | 951 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 952 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 953 | mbedtls_svc_key_id_t key, |
| 954 | psa_key_slot_t **p_slot, |
| 955 | psa_key_usage_t usage, |
| 956 | psa_algorithm_t alg ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 957 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 958 | psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot, |
| 959 | usage, alg ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 960 | if( status != PSA_SUCCESS ) |
| 961 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 962 | |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 963 | if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) ) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 964 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 965 | psa_unlock_key_slot( *p_slot ); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 966 | *p_slot = NULL; |
| 967 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 968 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 969 | |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 970 | return( PSA_SUCCESS ); |
| 971 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 972 | |
Steven Cooreman | 7ddee7f | 2021-04-07 18:08:30 +0200 | [diff] [blame] | 973 | 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] | 974 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 975 | /* Data pointer will always be either a valid pointer or NULL in an |
| 976 | * initialized slot, so we can just free it. */ |
| 977 | if( slot->key.data != NULL ) |
| 978 | mbedtls_platform_zeroize( slot->key.data, slot->key.bytes); |
| 979 | |
| 980 | mbedtls_free( slot->key.data ); |
| 981 | slot->key.data = NULL; |
| 982 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 983 | |
| 984 | return( PSA_SUCCESS ); |
| 985 | } |
| 986 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 987 | /** Completely wipe a slot in memory, including its policy. |
| 988 | * Persistent storage is not affected. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 989 | psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot ) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 990 | { |
| 991 | psa_status_t status = psa_remove_key_data_from_memory( slot ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 992 | |
TRodziewicz | 18cddc0 | 2021-07-13 12:19:15 +0200 | [diff] [blame] | 993 | /* |
| 994 | * 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] | 995 | * do our best to report an unexpected lock counter. Assert with |
| 996 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one: |
| 997 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 998 | * function is called as part of the execution of a test suite, the |
| 999 | * execution of the test suite is stopped in error if the assertion fails. |
TRodziewicz | 18cddc0 | 2021-07-13 12:19:15 +0200 | [diff] [blame] | 1000 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1001 | if( slot->lock_count != 1 ) |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1002 | { |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 1003 | MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count == 1 ); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1004 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1005 | } |
| 1006 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1007 | /* Multipart operations may still be using the key. This is safe |
| 1008 | * because all multipart operation objects are independent from |
| 1009 | * the key slot: if they need to access the key after the setup |
| 1010 | * phase, they have a copy of the key. Note that this means that |
| 1011 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1012 | /* At this point, key material and other type-specific content has |
| 1013 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1014 | * zeroize because the metadata is not particularly sensitive. */ |
| 1015 | memset( slot, 0, sizeof( *slot ) ); |
| 1016 | return( status ); |
| 1017 | } |
| 1018 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1019 | psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1020 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1021 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1022 | psa_status_t status; /* status of the last operation */ |
| 1023 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1024 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1025 | psa_se_drv_table_entry_t *driver; |
| 1026 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1027 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1028 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1029 | return( PSA_SUCCESS ); |
| 1030 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1031 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1032 | * 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] | 1033 | * key, this will load the key description from persistent memory if not |
| 1034 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1035 | * the key is operated by an SE or not and this information is needed by |
| 1036 | * the current implementation. |
| 1037 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1038 | status = psa_get_and_lock_key_slot( key, &slot ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 1039 | if( status != PSA_SUCCESS ) |
| 1040 | return( status ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1041 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1042 | /* |
| 1043 | * If the key slot containing the key description is under access by the |
| 1044 | * library (apart from the present access), the key cannot be destroyed |
| 1045 | * yet. For the time being, just return in error. Eventually (to be |
| 1046 | * implemented), the key should be destroyed when all accesses have |
| 1047 | * stopped. |
| 1048 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1049 | if( slot->lock_count > 1 ) |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1050 | { |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1051 | psa_unlock_key_slot( slot ); |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1052 | return( PSA_ERROR_GENERIC_ERROR ); |
| 1053 | } |
| 1054 | |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1055 | if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) ) |
| 1056 | { |
| 1057 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1058 | * if we attempt it, depending on whether the key is merely read-only |
| 1059 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1060 | * Just do the best we can, which is to wipe the copy in memory |
| 1061 | * (done in this function's cleanup code). */ |
| 1062 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1063 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1064 | } |
| 1065 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1066 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1067 | driver = psa_get_se_driver_entry( slot->attr.lifetime ); |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1068 | if( driver != NULL ) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1069 | { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1070 | /* For a key in a secure element, we need to do three things: |
| 1071 | * remove the key file in internal storage, destroy the |
| 1072 | * key inside the secure element, and update the driver's |
| 1073 | * persistent data. Start a transaction that will encompass these |
| 1074 | * three actions. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1075 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1076 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1077 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1078 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1079 | status = psa_crypto_save_transaction( ); |
| 1080 | if( status != PSA_SUCCESS ) |
| 1081 | { |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1082 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1083 | /* We should still try to destroy the key in the secure |
| 1084 | * element and the key metadata in storage. This is especially |
| 1085 | * important if the error is that the storage is full. |
| 1086 | * But how to do it exactly without risking an inconsistent |
| 1087 | * state after a reset? |
| 1088 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1089 | */ |
| 1090 | overall_status = status; |
| 1091 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1094 | status = psa_destroy_se_key( driver, |
| 1095 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1096 | if( overall_status == PSA_SUCCESS ) |
| 1097 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1098 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1099 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1100 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1101 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ronald Cron | d98059d | 2020-10-23 18:00:55 +0200 | [diff] [blame] | 1102 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1103 | { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1104 | status = psa_destroy_persistent_key( slot->attr.id ); |
| 1105 | if( overall_status == PSA_SUCCESS ) |
| 1106 | overall_status = status; |
| 1107 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1108 | /* TODO: other slots may have a copy of the same key. We should |
| 1109 | * invalidate them. |
| 1110 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1111 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1112 | } |
| 1113 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1114 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1115 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1116 | if( driver != NULL ) |
| 1117 | { |
Gilles Peskine | 725f22a | 2019-07-25 11:31:48 +0200 | [diff] [blame] | 1118 | status = psa_save_se_persistent_data( driver ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1119 | if( overall_status == PSA_SUCCESS ) |
| 1120 | overall_status = status; |
| 1121 | status = psa_crypto_stop_transaction( ); |
| 1122 | if( overall_status == PSA_SUCCESS ) |
| 1123 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1124 | } |
| 1125 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1126 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1127 | exit: |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1128 | status = psa_wipe_key_slot( slot ); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1129 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1130 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1131 | overall_status = status; |
| 1132 | return( overall_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1133 | } |
| 1134 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1135 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1136 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1137 | static psa_status_t psa_get_rsa_public_exponent( |
| 1138 | const mbedtls_rsa_context *rsa, |
| 1139 | psa_key_attributes_t *attributes ) |
| 1140 | { |
| 1141 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1142 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1143 | uint8_t *buffer = NULL; |
| 1144 | size_t buflen; |
| 1145 | mbedtls_mpi_init( &mpi ); |
| 1146 | |
| 1147 | ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi ); |
| 1148 | if( ret != 0 ) |
| 1149 | goto exit; |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1150 | if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 ) |
| 1151 | { |
| 1152 | /* It's the default value, which is reported as an empty string, |
| 1153 | * so there's nothing to do. */ |
| 1154 | goto exit; |
| 1155 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1156 | |
| 1157 | buflen = mbedtls_mpi_size( &mpi ); |
| 1158 | buffer = mbedtls_calloc( 1, buflen ); |
| 1159 | if( buffer == NULL ) |
| 1160 | { |
| 1161 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1162 | goto exit; |
| 1163 | } |
| 1164 | ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen ); |
| 1165 | if( ret != 0 ) |
| 1166 | goto exit; |
| 1167 | attributes->domain_parameters = buffer; |
| 1168 | attributes->domain_parameters_size = buflen; |
| 1169 | |
| 1170 | exit: |
| 1171 | mbedtls_mpi_free( &mpi ); |
| 1172 | if( ret != 0 ) |
| 1173 | mbedtls_free( buffer ); |
| 1174 | return( mbedtls_to_psa_error( ret ) ); |
| 1175 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1176 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1177 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1178 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1179 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1180 | */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1181 | 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] | 1182 | psa_key_attributes_t *attributes ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1183 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1184 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1185 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1186 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1187 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1188 | psa_reset_key_attributes( attributes ); |
| 1189 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1190 | 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] | 1191 | if( status != PSA_SUCCESS ) |
| 1192 | return( status ); |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1193 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1194 | attributes->core = slot->attr; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1195 | attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1196 | MBEDTLS_PSA_KA_MASK_DUAL_USE ); |
| 1197 | |
| 1198 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 512ad81 | 2021-08-24 15:50:05 +0200 | [diff] [blame] | 1199 | if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL ) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1200 | psa_set_key_slot_number( attributes, |
| 1201 | psa_key_slot_get_slot_number( slot ) ); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1202 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1203 | |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1204 | switch( slot->attr.type ) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1205 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1206 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1207 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1208 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1209 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1210 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1211 | * is not yet implemented. |
| 1212 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1213 | */ |
Ronald Cron | 9b8b69c | 2021-08-24 16:00:51 +0200 | [diff] [blame] | 1214 | if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1215 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1216 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1217 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1218 | status = mbedtls_psa_rsa_load_representation( |
| 1219 | slot->attr.type, |
| 1220 | slot->key.data, |
| 1221 | slot->key.bytes, |
| 1222 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1223 | if( status != PSA_SUCCESS ) |
| 1224 | break; |
| 1225 | |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1226 | status = psa_get_rsa_public_exponent( rsa, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1227 | attributes ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1228 | mbedtls_rsa_free( rsa ); |
| 1229 | mbedtls_free( rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1230 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1231 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1232 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1233 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1234 | default: |
| 1235 | /* Nothing else to do. */ |
| 1236 | break; |
| 1237 | } |
| 1238 | |
| 1239 | if( status != PSA_SUCCESS ) |
| 1240 | psa_reset_key_attributes( attributes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1241 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1242 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1243 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1244 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1245 | } |
| 1246 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1247 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1248 | psa_status_t psa_get_key_slot_number( |
| 1249 | const psa_key_attributes_t *attributes, |
| 1250 | psa_key_slot_number_t *slot_number ) |
| 1251 | { |
| 1252 | if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER ) |
| 1253 | { |
| 1254 | *slot_number = attributes->slot_number; |
| 1255 | return( PSA_SUCCESS ); |
| 1256 | } |
| 1257 | else |
| 1258 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1259 | } |
| 1260 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1261 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1262 | static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer, |
| 1263 | size_t key_buffer_size, |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1264 | uint8_t *data, |
| 1265 | size_t data_size, |
| 1266 | size_t *data_length ) |
| 1267 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1268 | if( key_buffer_size > data_size ) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1269 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1270 | memcpy( data, key_buffer, key_buffer_size ); |
| 1271 | memset( data + key_buffer_size, 0, |
| 1272 | data_size - key_buffer_size ); |
| 1273 | *data_length = key_buffer_size; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1274 | return( PSA_SUCCESS ); |
| 1275 | } |
| 1276 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1277 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1278 | const psa_key_attributes_t *attributes, |
| 1279 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1280 | uint8_t *data, size_t data_size, size_t *data_length ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1281 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1282 | psa_key_type_t type = attributes->core.type; |
| 1283 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1284 | if( key_type_is_raw_bytes( type ) || |
| 1285 | PSA_KEY_TYPE_IS_RSA( type ) || |
| 1286 | PSA_KEY_TYPE_IS_ECC( type ) ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1287 | { |
| 1288 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1289 | key_buffer, key_buffer_size, |
| 1290 | data, data_size, data_length ) ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1291 | } |
| 1292 | else |
| 1293 | { |
| 1294 | /* This shouldn't happen in the reference implementation, but |
| 1295 | it is valid for a special-purpose implementation to omit |
| 1296 | support for exporting certain key types. */ |
| 1297 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | psa_status_t psa_export_key( mbedtls_svc_key_id_t key, |
| 1302 | uint8_t *data, |
| 1303 | size_t data_size, |
| 1304 | size_t *data_length ) |
| 1305 | { |
| 1306 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1307 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1308 | psa_key_slot_t *slot; |
| 1309 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1310 | /* Reject a zero-length output buffer now, since this can never be a |
| 1311 | * valid key representation. This way we know that data must be a valid |
| 1312 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1313 | if( data_size == 0 ) |
| 1314 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1315 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1316 | /* Set the key to empty now, so that even when there are errors, we always |
| 1317 | * set data_length to a value between 0 and data_size. On error, setting |
| 1318 | * the key to empty is a good choice because an empty key representation is |
| 1319 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1320 | *data_length = 0; |
| 1321 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1322 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1323 | * which don't require any flag, but |
| 1324 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1325 | */ |
| 1326 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 1327 | PSA_KEY_USAGE_EXPORT, 0 ); |
| 1328 | if( status != PSA_SUCCESS ) |
| 1329 | return( status ); |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1330 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1331 | psa_key_attributes_t attributes = { |
| 1332 | .core = slot->attr |
| 1333 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1334 | status = psa_driver_wrapper_export_key( &attributes, |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1335 | slot->key.data, slot->key.bytes, |
| 1336 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1337 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1338 | unlock_status = psa_unlock_key_slot( slot ); |
| 1339 | |
| 1340 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 1341 | } |
| 1342 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1343 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1344 | const psa_key_attributes_t *attributes, |
| 1345 | const uint8_t *key_buffer, |
| 1346 | size_t key_buffer_size, |
| 1347 | uint8_t *data, |
| 1348 | size_t data_size, |
| 1349 | size_t *data_length ) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1350 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1351 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1352 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1353 | 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] | 1354 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1355 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1356 | { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1357 | /* Exporting public -> public */ |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1358 | return( psa_export_key_buffer_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1359 | key_buffer, key_buffer_size, |
| 1360 | data, data_size, data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1361 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1362 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1363 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1364 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1365 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1366 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1367 | return( mbedtls_psa_rsa_export_public_key( attributes, |
| 1368 | key_buffer, |
| 1369 | key_buffer_size, |
| 1370 | data, |
| 1371 | data_size, |
| 1372 | data_length ) ); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1373 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1374 | /* We don't know how to convert a private RSA key to public. */ |
| 1375 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1376 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1377 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 1378 | } |
| 1379 | else |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1380 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1381 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1382 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 1383 | return( mbedtls_psa_ecp_export_public_key( attributes, |
| 1384 | key_buffer, |
| 1385 | key_buffer_size, |
| 1386 | data, |
| 1387 | data_size, |
| 1388 | data_length ) ); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1389 | #else |
| 1390 | /* 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] | 1391 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1392 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1393 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1394 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1395 | } |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1396 | else |
| 1397 | { |
| 1398 | /* This shouldn't happen in the reference implementation, but |
| 1399 | it is valid for a special-purpose implementation to omit |
| 1400 | support for exporting certain key types. */ |
| 1401 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1402 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1403 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1404 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1405 | 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] | 1406 | uint8_t *data, |
| 1407 | size_t data_size, |
| 1408 | size_t *data_length ) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1409 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1410 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1411 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1412 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1413 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1414 | /* Reject a zero-length output buffer now, since this can never be a |
| 1415 | * valid key representation. This way we know that data must be a valid |
| 1416 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1417 | if( data_size == 0 ) |
| 1418 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1419 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1420 | /* Set the key to empty now, so that even when there are errors, we always |
| 1421 | * set data_length to a value between 0 and data_size. On error, setting |
| 1422 | * the key to empty is a good choice because an empty key representation is |
| 1423 | * unlikely to be accepted anywhere. */ |
| 1424 | *data_length = 0; |
| 1425 | |
| 1426 | /* Exporting a public key doesn't require a usage flag. */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1427 | 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] | 1428 | if( status != PSA_SUCCESS ) |
| 1429 | return( status ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1430 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1431 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 1432 | { |
| 1433 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1434 | goto exit; |
| 1435 | } |
| 1436 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1437 | psa_key_attributes_t attributes = { |
| 1438 | .core = slot->attr |
| 1439 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1440 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1441 | &attributes, slot->key.data, slot->key.bytes, |
| 1442 | data, data_size, data_length ); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1443 | |
| 1444 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1445 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1446 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1447 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1448 | } |
| 1449 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1450 | #if defined(static_assert) |
| 1451 | static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0, |
| 1452 | "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] | 1453 | 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] | 1454 | "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] | 1455 | 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] | 1456 | "One or more key attribute flag is listed as both internal-only and external-only" ); |
| 1457 | #endif |
| 1458 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1459 | /** Validate that a key policy is internally well-formed. |
| 1460 | * |
| 1461 | * This function only rejects invalid policies. It does not validate the |
| 1462 | * consistency of the policy with respect to other attributes of the key |
| 1463 | * such as the key type. |
| 1464 | */ |
| 1465 | 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] | 1466 | { |
| 1467 | if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | |
Gilles Peskine | 8e0206a | 2019-05-14 14:24:28 +0200 | [diff] [blame] | 1468 | PSA_KEY_USAGE_COPY | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1469 | PSA_KEY_USAGE_ENCRYPT | |
| 1470 | PSA_KEY_USAGE_DECRYPT | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 1471 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1472 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1473 | PSA_KEY_USAGE_SIGN_HASH | |
| 1474 | PSA_KEY_USAGE_VERIFY_HASH | |
Manuel Pégourié-Gonnard | 805251b | 2021-05-04 09:49:59 +0200 | [diff] [blame] | 1475 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1476 | PSA_KEY_USAGE_DERIVE ) ) != 0 ) |
| 1477 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1478 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1479 | return( PSA_SUCCESS ); |
| 1480 | } |
| 1481 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1482 | /** Validate the internal consistency of key attributes. |
| 1483 | * |
| 1484 | * This function only rejects invalid attribute values. If does not |
| 1485 | * validate the consistency of the attributes with any key data that may |
| 1486 | * be involved in the creation of the key. |
| 1487 | * |
| 1488 | * Call this function early in the key creation process. |
| 1489 | * |
| 1490 | * \param[in] attributes Key attributes for the new key. |
| 1491 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1492 | * NULL for a transparent key. |
| 1493 | * |
| 1494 | */ |
| 1495 | static psa_status_t psa_validate_key_attributes( |
| 1496 | const psa_key_attributes_t *attributes, |
| 1497 | psa_se_drv_table_entry_t **p_drv ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1498 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1499 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1500 | psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes ); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1501 | mbedtls_svc_key_id_t key = psa_get_key_id( attributes ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1502 | |
Ronald Cron | 54b9008 | 2020-10-29 15:26:43 +0100 | [diff] [blame] | 1503 | status = psa_validate_key_location( lifetime, p_drv ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1504 | if( status != PSA_SUCCESS ) |
| 1505 | return( status ); |
| 1506 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1507 | status = psa_validate_key_persistence( lifetime ); |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1508 | if( status != PSA_SUCCESS ) |
| 1509 | return( status ); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1510 | |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1511 | if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
| 1512 | { |
| 1513 | if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 ) |
| 1514 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1515 | } |
| 1516 | else |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1517 | { |
Ronald Cron | 77e412c | 2021-03-31 17:36:31 +0200 | [diff] [blame] | 1518 | if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) ) |
| 1519 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1520 | } |
| 1521 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1522 | status = psa_validate_key_policy( &attributes->core.policy ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1523 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1524 | return( status ); |
| 1525 | |
| 1526 | /* Refuse to create overly large keys. |
| 1527 | * Note that this doesn't trigger on import if the attributes don't |
| 1528 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1529 | * psa_import_key() needs its own checks. */ |
| 1530 | if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS ) |
| 1531 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1532 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1533 | /* Reject invalid flags. These should not be reachable through the API. */ |
| 1534 | if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1535 | MBEDTLS_PSA_KA_MASK_DUAL_USE ) ) |
| 1536 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1537 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1538 | return( PSA_SUCCESS ); |
| 1539 | } |
| 1540 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1541 | /** Prepare a key slot to receive key material. |
| 1542 | * |
| 1543 | * This function allocates a key slot and sets its metadata. |
| 1544 | * |
| 1545 | * If this function fails, call psa_fail_key_creation(). |
| 1546 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1547 | * This function is intended to be used as follows: |
| 1548 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1549 | * it with the specified attributes, and in case of a volatile key assign it |
| 1550 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1551 | * -# Populate the slot with the key material. |
| 1552 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1553 | * In case of failure at any step, stop the sequence and call |
| 1554 | * psa_fail_key_creation(). |
| 1555 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1556 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1557 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1558 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1559 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1560 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1561 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1562 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1563 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1564 | * |
| 1565 | * \retval #PSA_SUCCESS |
| 1566 | * The key slot is ready to receive key material. |
| 1567 | * \return If this function fails, the key slot is an invalid state. |
| 1568 | * 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] | 1569 | */ |
| 1570 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1571 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1572 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1573 | psa_key_slot_t **p_slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1574 | psa_se_drv_table_entry_t **p_drv ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1575 | { |
| 1576 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1577 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1578 | psa_key_slot_t *slot; |
| 1579 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1580 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1581 | *p_drv = NULL; |
| 1582 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1583 | status = psa_validate_key_attributes( attributes, p_drv ); |
| 1584 | if( status != PSA_SUCCESS ) |
| 1585 | return( status ); |
| 1586 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1587 | status = psa_get_empty_key_slot( &volatile_key_id, p_slot ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1588 | if( status != PSA_SUCCESS ) |
| 1589 | return( status ); |
| 1590 | slot = *p_slot; |
| 1591 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1592 | /* We're storing the declared bit-size of the key. It's up to each |
| 1593 | * creation mechanism to verify that this information is correct. |
| 1594 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1595 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1596 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1597 | * volatile key identifier associated to the slot returned to contain its |
| 1598 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1599 | |
| 1600 | slot->attr = attributes->core; |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1601 | if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
| 1602 | { |
| 1603 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1604 | slot->attr.id = volatile_key_id; |
| 1605 | #else |
| 1606 | slot->attr.id.key_id = volatile_key_id; |
| 1607 | #endif |
| 1608 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1609 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1610 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1611 | * external-only flags, query `attributes`. Thanks to the check |
| 1612 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1613 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1614 | * may have set. */ |
| 1615 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1616 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1617 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1618 | /* 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] | 1619 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1620 | * create the key file in internal storage, create the |
| 1621 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1622 | * persistent data. This is done by starting a transaction that will |
| 1623 | * encompass these three actions. |
| 1624 | * For registering a volatile key, we just need to find an appropriate |
| 1625 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1626 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1627 | /* The first thing to do is to find a slot number for the new key. |
| 1628 | * We save the slot number in persistent storage as part of the |
| 1629 | * transaction data. It will be needed to recover if the power |
| 1630 | * fails during the key creation process, to clean up on the secure |
| 1631 | * element side after restarting. Obtaining a slot number from the |
| 1632 | * secure element driver updates its persistent state, but we do not yet |
| 1633 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1634 | * 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] | 1635 | if( *p_drv != NULL ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1636 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1637 | psa_key_slot_number_t slot_number; |
Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 1638 | status = psa_find_se_slot_for_key( attributes, method, *p_drv, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1639 | &slot_number ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1640 | if( status != PSA_SUCCESS ) |
| 1641 | return( status ); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1642 | |
| 1643 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) ) |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1644 | { |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1645 | psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY ); |
| 1646 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1647 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1648 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1649 | status = psa_crypto_save_transaction( ); |
| 1650 | if( status != PSA_SUCCESS ) |
| 1651 | { |
| 1652 | (void) psa_crypto_stop_transaction( ); |
| 1653 | return( status ); |
| 1654 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1655 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1656 | |
| 1657 | status = psa_copy_key_material_into_slot( |
| 1658 | slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1659 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1660 | |
| 1661 | if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER ) |
| 1662 | { |
| 1663 | /* Key registration only makes sense with a secure element. */ |
| 1664 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1665 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1666 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1667 | |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1668 | return( PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1669 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1670 | |
| 1671 | /** Finalize the creation of a key once its key material has been set. |
| 1672 | * |
| 1673 | * This entails writing the key to persistent storage. |
| 1674 | * |
| 1675 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1676 | * See the documentation of psa_start_key_creation() for the intended use |
| 1677 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1678 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1679 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1680 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1681 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1682 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1683 | * \param[in,out] slot Pointer to the slot with key material. |
| 1684 | * \param[in] driver The secure element driver for the key, |
| 1685 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1686 | * \param[out] key On success, identifier of the key. Note that the |
| 1687 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1688 | * |
| 1689 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1690 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1691 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1692 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1693 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1694 | * \retval #PSA_ERROR_DATA_INVALID |
| 1695 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1696 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1697 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1698 | * \return If this function fails, the key slot is an invalid state. |
| 1699 | * 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] | 1700 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1701 | static psa_status_t psa_finish_key_creation( |
| 1702 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1703 | psa_se_drv_table_entry_t *driver, |
| 1704 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1705 | { |
| 1706 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1707 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1708 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1709 | |
| 1710 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 1711 | if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1712 | { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1713 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1714 | if( driver != NULL ) |
| 1715 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1716 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1717 | psa_key_slot_number_t slot_number = |
| 1718 | psa_key_slot_get_slot_number( slot ) ; |
| 1719 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1720 | #if defined(static_assert) |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1721 | static_assert( sizeof( slot_number ) == |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1722 | sizeof( data.slot_number ), |
| 1723 | "Slot number size does not match psa_se_key_data_storage_t" ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1724 | #endif |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1725 | memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) ); |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1726 | status = psa_save_persistent_key( &slot->attr, |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1727 | (uint8_t*) &data, |
| 1728 | sizeof( data ) ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1729 | } |
| 1730 | else |
| 1731 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1732 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1733 | /* Key material is saved in export representation in the slot, so |
| 1734 | * just pass the slot buffer for storage. */ |
| 1735 | status = psa_save_persistent_key( &slot->attr, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1736 | slot->key.data, |
| 1737 | slot->key.bytes ); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1738 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1739 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1740 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1741 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1742 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1743 | /* Finish the transaction for a key creation. This does not |
| 1744 | * happen when registering an existing key. Detect this case |
| 1745 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1746 | * creation of a persistent key in a secure element requires a transaction, |
| 1747 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1748 | if( driver != NULL && |
| 1749 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY ) |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1750 | { |
| 1751 | status = psa_save_se_persistent_data( driver ); |
| 1752 | if( status != PSA_SUCCESS ) |
| 1753 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1754 | psa_destroy_persistent_key( slot->attr.id ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1755 | return( status ); |
| 1756 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1757 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1758 | } |
| 1759 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1760 | |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1761 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1762 | { |
| 1763 | *key = slot->attr.id; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1764 | status = psa_unlock_key_slot( slot ); |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1765 | if( status != PSA_SUCCESS ) |
| 1766 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1767 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1768 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1769 | return( status ); |
| 1770 | } |
| 1771 | |
| 1772 | /** Abort the creation of a key. |
| 1773 | * |
| 1774 | * You may call this function after calling psa_start_key_creation(), |
| 1775 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1776 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1777 | * See the documentation of psa_start_key_creation() for the intended use |
| 1778 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1779 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1780 | * \param[in,out] slot Pointer to the slot with key material. |
| 1781 | * \param[in] driver The secure element driver for the key, |
| 1782 | * or NULL for a transparent key. |
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 | static void psa_fail_key_creation( psa_key_slot_t *slot, |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1785 | psa_se_drv_table_entry_t *driver ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1786 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1787 | (void) driver; |
| 1788 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1789 | if( slot == NULL ) |
| 1790 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1791 | |
| 1792 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1793 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1794 | * element, and the failure happened later (when saving metadata |
| 1795 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1796 | * element. |
| 1797 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1798 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1799 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1800 | /* Abort the ongoing transaction if any (there may not be one if |
| 1801 | * the creation process failed before starting one, or if the |
| 1802 | * key creation is a registration of a key in a secure element). |
| 1803 | * Earlier functions must already have done what it takes to undo any |
| 1804 | * partial creation. All that's left is to update the transaction data |
| 1805 | * itself. */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1806 | (void) psa_crypto_stop_transaction( ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1807 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1808 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1809 | psa_wipe_key_slot( slot ); |
| 1810 | } |
| 1811 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1812 | /** Validate optional attributes during key creation. |
| 1813 | * |
| 1814 | * Some key attributes are optional during key creation. If they are |
| 1815 | * specified in the attributes structure, check that they are consistent |
| 1816 | * with the data in the slot. |
| 1817 | * |
| 1818 | * This function should be called near the end of key creation, after |
| 1819 | * the slot in memory is fully populated but before saving persistent data. |
| 1820 | */ |
| 1821 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1822 | const psa_key_slot_t *slot, |
| 1823 | const psa_key_attributes_t *attributes ) |
| 1824 | { |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1825 | if( attributes->core.type != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1826 | { |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1827 | if( attributes->core.type != slot->attr.type ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1828 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1829 | } |
| 1830 | |
| 1831 | if( attributes->domain_parameters_size != 0 ) |
| 1832 | { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1833 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1834 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1835 | if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1836 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1837 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1838 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1839 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1840 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1841 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
| 1842 | slot->attr.type, |
| 1843 | slot->key.data, |
| 1844 | slot->key.bytes, |
| 1845 | &rsa ); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1846 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 1847 | return( status ); |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1848 | |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1849 | mbedtls_mpi_init( &actual ); |
| 1850 | mbedtls_mpi_init( &required ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1851 | ret = mbedtls_rsa_export( rsa, |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1852 | NULL, NULL, NULL, NULL, &actual ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1853 | mbedtls_rsa_free( rsa ); |
| 1854 | mbedtls_free( rsa ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1855 | if( ret != 0 ) |
| 1856 | goto rsa_exit; |
| 1857 | ret = mbedtls_mpi_read_binary( &required, |
| 1858 | attributes->domain_parameters, |
| 1859 | attributes->domain_parameters_size ); |
| 1860 | if( ret != 0 ) |
| 1861 | goto rsa_exit; |
| 1862 | if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 ) |
| 1863 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1864 | rsa_exit: |
| 1865 | mbedtls_mpi_free( &actual ); |
| 1866 | mbedtls_mpi_free( &required ); |
| 1867 | if( ret != 0) |
| 1868 | return( mbedtls_to_psa_error( ret ) ); |
| 1869 | } |
| 1870 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1871 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1872 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1873 | { |
| 1874 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1875 | } |
| 1876 | } |
| 1877 | |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1878 | if( attributes->core.bits != 0 ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1879 | { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1880 | if( attributes->core.bits != slot->attr.bits ) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1881 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1882 | } |
| 1883 | |
| 1884 | return( PSA_SUCCESS ); |
| 1885 | } |
| 1886 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1887 | psa_status_t psa_import_key( const psa_key_attributes_t *attributes, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1888 | const uint8_t *data, |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 1889 | size_t data_length, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1890 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1891 | { |
| 1892 | psa_status_t status; |
| 1893 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1894 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1895 | size_t bits; |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1896 | size_t storage_size = data_length; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1897 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1898 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1899 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1900 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1901 | * This also rejects any key which might be encoded as an empty string, |
| 1902 | * which is never valid. */ |
| 1903 | if( data_length == 0 ) |
| 1904 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1905 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1906 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1907 | if( data_length > SIZE_MAX / 8 ) |
| 1908 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1909 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1910 | status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1911 | &slot, &driver ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1912 | if( status != PSA_SUCCESS ) |
| 1913 | goto exit; |
| 1914 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1915 | /* 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] | 1916 | * storage ( thus not in the case of importing a key in a secure element |
| 1917 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 1918 | * buffer to hold the imported key material. */ |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1919 | if( slot->key.data == NULL ) |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1920 | { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1921 | if( psa_key_lifetime_is_external( attributes->core.lifetime ) ) |
| 1922 | { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1923 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
| 1924 | attributes, data, data_length, &storage_size ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1925 | if( status != PSA_SUCCESS ) |
| 1926 | goto exit; |
| 1927 | } |
| 1928 | status = psa_allocate_buffer_to_slot( slot, storage_size ); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1929 | if( status != PSA_SUCCESS ) |
| 1930 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1931 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1932 | |
| 1933 | bits = slot->attr.bits; |
| 1934 | status = psa_driver_wrapper_import_key( attributes, |
| 1935 | data, data_length, |
| 1936 | slot->key.data, |
| 1937 | slot->key.bytes, |
| 1938 | &slot->key.bytes, &bits ); |
| 1939 | if( status != PSA_SUCCESS ) |
| 1940 | goto exit; |
| 1941 | |
| 1942 | if( slot->attr.bits == 0 ) |
| 1943 | slot->attr.bits = (psa_key_bits_t) bits; |
| 1944 | else if( bits != slot->attr.bits ) |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1945 | { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1946 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1947 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1948 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 1949 | |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1950 | /* Enforce a size limit, and in particular ensure that the bit |
| 1951 | * size fits in its representation type.*/ |
| 1952 | if( bits > PSA_MAX_KEY_BITS ) |
| 1953 | { |
| 1954 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1955 | goto exit; |
| 1956 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1957 | status = psa_validate_optional_attributes( slot, attributes ); |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1958 | if( status != PSA_SUCCESS ) |
| 1959 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1960 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1961 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1962 | exit: |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1963 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1964 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1965 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1966 | return( status ); |
| 1967 | } |
| 1968 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1969 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1970 | psa_status_t mbedtls_psa_register_se_key( |
| 1971 | const psa_key_attributes_t *attributes ) |
| 1972 | { |
| 1973 | psa_status_t status; |
| 1974 | psa_key_slot_t *slot = NULL; |
| 1975 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1976 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1977 | |
| 1978 | /* Leaving attributes unspecified is not currently supported. |
| 1979 | * It could make sense to query the key type and size from the |
| 1980 | * secure element, but not all secure elements support this |
| 1981 | * and the driver HAL doesn't currently support it. */ |
| 1982 | if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE ) |
| 1983 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1984 | if( psa_get_key_bits( attributes ) == 0 ) |
| 1985 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1986 | |
| 1987 | status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1988 | &slot, &driver ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1989 | if( status != PSA_SUCCESS ) |
| 1990 | goto exit; |
| 1991 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1992 | status = psa_finish_key_creation( slot, driver, &key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1993 | |
| 1994 | exit: |
| 1995 | if( status != PSA_SUCCESS ) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1996 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1997 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1998 | /* Registration doesn't keep the key in RAM. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1999 | psa_close_key( key ); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2000 | return( status ); |
| 2001 | } |
| 2002 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2003 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2004 | 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] | 2005 | const psa_key_attributes_t *specified_attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2006 | mbedtls_svc_key_id_t *target_key ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2007 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2008 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2009 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2010 | psa_key_slot_t *source_slot = NULL; |
| 2011 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2012 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2013 | psa_se_drv_table_entry_t *driver = NULL; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2014 | size_t storage_size = 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2015 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2016 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2017 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2018 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2019 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2020 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2021 | goto exit; |
| 2022 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 2023 | status = psa_validate_optional_attributes( source_slot, |
| 2024 | specified_attributes ); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2025 | if( status != PSA_SUCCESS ) |
| 2026 | goto exit; |
| 2027 | |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2028 | /* The target key type and number of bits have been validated by |
| 2029 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2030 | * equal to the ones of the source key. So it is safe to inherit |
| 2031 | * them from the source key now." |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2032 | * */ |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2033 | actual_attributes.core.bits = source_slot->attr.bits; |
| 2034 | actual_attributes.core.type = source_slot->attr.type; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2035 | |
| 2036 | |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 2037 | status = psa_restrict_key_policy( source_slot->attr.type, |
| 2038 | &actual_attributes.core.policy, |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 2039 | &source_slot->attr.policy ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2040 | if( status != PSA_SUCCESS ) |
| 2041 | goto exit; |
| 2042 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2043 | status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2044 | &target_slot, &driver ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2045 | if( status != PSA_SUCCESS ) |
| 2046 | goto exit; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2047 | if( PSA_KEY_LIFETIME_GET_LOCATION( target_slot->attr.lifetime ) != |
| 2048 | PSA_KEY_LIFETIME_GET_LOCATION( source_slot->attr.lifetime ) ) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2049 | { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2050 | /* |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2051 | * If the source and target keys are stored in different locations, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2052 | * the source key would need to be exported as plaintext and re-imported |
| 2053 | * in the other location. This has security implications which have not |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2054 | * been fully mapped. For now, this can be achieved through |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2055 | * appropriate API invocations from the application, if needed. |
| 2056 | * */ |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2057 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2058 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2059 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2060 | /* |
| 2061 | * When the source and target keys are within the same location, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2062 | * - For transparent keys it is a blind copy without any driver invocation, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2063 | * - For opaque keys this translates to an invocation of the drivers' |
| 2064 | * copy_key entry point through the dispatch layer. |
| 2065 | * */ |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2066 | if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) ) |
| 2067 | { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2068 | status = psa_driver_wrapper_get_key_buffer_size( &actual_attributes, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2069 | &storage_size ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2070 | if( status != PSA_SUCCESS ) |
| 2071 | goto exit; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2072 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2073 | status = psa_allocate_buffer_to_slot( target_slot, storage_size ); |
| 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_driver_wrapper_copy_key( &actual_attributes, |
| 2078 | source_slot->key.data, |
| 2079 | source_slot->key.bytes, |
| 2080 | target_slot->key.data, |
| 2081 | target_slot->key.bytes, |
| 2082 | &target_slot->key.bytes ); |
| 2083 | if( status != PSA_SUCCESS ) |
| 2084 | goto exit; |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2085 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2086 | else |
| 2087 | { |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2088 | status = psa_copy_key_material_into_slot( target_slot, |
| 2089 | source_slot->key.data, |
| 2090 | source_slot->key.bytes ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2091 | if( status != PSA_SUCCESS ) |
| 2092 | goto exit; |
| 2093 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2094 | status = psa_finish_key_creation( target_slot, driver, target_key ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2095 | exit: |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2096 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 2097 | psa_fail_key_creation( target_slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2098 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2099 | unlock_status = psa_unlock_key_slot( source_slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2100 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2101 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2102 | } |
| 2103 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2104 | |
| 2105 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2106 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2107 | /* Message digests */ |
| 2108 | /****************************************************************/ |
| 2109 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2110 | psa_status_t psa_hash_abort( psa_hash_operation_t *operation ) |
| 2111 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2112 | /* Aborting a non-active operation is allowed */ |
| 2113 | if( operation->id == 0 ) |
| 2114 | return( PSA_SUCCESS ); |
| 2115 | |
| 2116 | psa_status_t status = psa_driver_wrapper_hash_abort( operation ); |
| 2117 | operation->id = 0; |
| 2118 | |
| 2119 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2120 | } |
| 2121 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2122 | psa_status_t psa_hash_setup( psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2123 | psa_algorithm_t alg ) |
| 2124 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2125 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2126 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2127 | /* A context must be freshly initialized before it can be set up. */ |
| 2128 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2129 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2130 | status = PSA_ERROR_BAD_STATE; |
| 2131 | goto exit; |
| 2132 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2133 | |
| 2134 | if( !PSA_ALG_IS_HASH( alg ) ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2135 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2136 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2137 | goto exit; |
| 2138 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2139 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2140 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2141 | * directly zeroes the int-sized dummy member of the context union. */ |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2142 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 2143 | |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2144 | status = psa_driver_wrapper_hash_setup( operation, alg ); |
| 2145 | |
| 2146 | exit: |
| 2147 | if( status != PSA_SUCCESS ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2148 | psa_hash_abort( operation ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2149 | |
| 2150 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | psa_status_t psa_hash_update( psa_hash_operation_t *operation, |
| 2154 | const uint8_t *input, |
| 2155 | size_t input_length ) |
| 2156 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2157 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2158 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2159 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2160 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2161 | status = PSA_ERROR_BAD_STATE; |
| 2162 | goto exit; |
| 2163 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2164 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2165 | /* Don't require hash implementations to behave correctly on a |
| 2166 | * zero-length input, which may have an invalid pointer. */ |
| 2167 | if( input_length == 0 ) |
| 2168 | return( PSA_SUCCESS ); |
| 2169 | |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2170 | status = psa_driver_wrapper_hash_update( operation, input, input_length ); |
| 2171 | |
| 2172 | exit: |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2173 | if( status != PSA_SUCCESS ) |
| 2174 | psa_hash_abort( operation ); |
| 2175 | |
| 2176 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | psa_status_t psa_hash_finish( psa_hash_operation_t *operation, |
| 2180 | uint8_t *hash, |
| 2181 | size_t hash_size, |
| 2182 | size_t *hash_length ) |
| 2183 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2184 | *hash_length = 0; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2185 | if( operation->id == 0 ) |
| 2186 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2187 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2188 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2189 | operation, hash, hash_size, hash_length ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 2190 | psa_hash_abort( operation ); |
| 2191 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2192 | } |
| 2193 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2194 | psa_status_t psa_hash_verify( psa_hash_operation_t *operation, |
| 2195 | const uint8_t *hash, |
| 2196 | size_t hash_length ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2197 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2198 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2199 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2200 | psa_status_t status = psa_hash_finish( |
| 2201 | operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2202 | actual_hash, sizeof( actual_hash ), |
| 2203 | &actual_hash_length ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2204 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2205 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2206 | goto exit; |
| 2207 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2208 | if( actual_hash_length != hash_length ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2209 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2210 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2211 | goto exit; |
| 2212 | } |
| 2213 | |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2214 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2215 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2216 | |
| 2217 | exit: |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2218 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2219 | if( status != PSA_SUCCESS ) |
| 2220 | psa_hash_abort(operation); |
| 2221 | |
| 2222 | return( status ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2223 | } |
| 2224 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2225 | psa_status_t psa_hash_compute( psa_algorithm_t alg, |
| 2226 | const uint8_t *input, size_t input_length, |
| 2227 | uint8_t *hash, size_t hash_size, |
| 2228 | size_t *hash_length ) |
| 2229 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2230 | *hash_length = 0; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2231 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2232 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2233 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2234 | return( psa_driver_wrapper_hash_compute( alg, input, input_length, |
| 2235 | hash, hash_size, hash_length ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | psa_status_t psa_hash_compare( psa_algorithm_t alg, |
| 2239 | const uint8_t *input, size_t input_length, |
| 2240 | const uint8_t *hash, size_t hash_length ) |
| 2241 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2242 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2243 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2244 | |
| 2245 | if( !PSA_ALG_IS_HASH( alg ) ) |
| 2246 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2247 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2248 | psa_status_t status = psa_driver_wrapper_hash_compute( |
| 2249 | alg, input, input_length, |
| 2250 | actual_hash, sizeof(actual_hash), |
| 2251 | &actual_hash_length ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2252 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2253 | goto exit; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2254 | if( actual_hash_length != hash_length ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2255 | { |
| 2256 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2257 | goto exit; |
| 2258 | } |
Steven Cooreman | 36876a0 | 2021-04-29 16:37:59 +0200 | [diff] [blame] | 2259 | if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 ) |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2260 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2261 | |
| 2262 | exit: |
| 2263 | mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) ); |
| 2264 | return( status ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2265 | } |
| 2266 | |
Gilles Peskine | eb35d78 | 2019-01-22 17:56:16 +0100 | [diff] [blame] | 2267 | psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation, |
| 2268 | psa_hash_operation_t *target_operation ) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2269 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2270 | if( source_operation->id == 0 || |
| 2271 | target_operation->id != 0 ) |
| 2272 | { |
| 2273 | return( PSA_ERROR_BAD_STATE ); |
| 2274 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2275 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2276 | psa_status_t status = psa_driver_wrapper_hash_clone( source_operation, |
| 2277 | target_operation ); |
| 2278 | if( status != PSA_SUCCESS ) |
| 2279 | psa_hash_abort( target_operation ); |
| 2280 | |
| 2281 | return( status ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2282 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2283 | |
| 2284 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2285 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2286 | /* MAC */ |
| 2287 | /****************************************************************/ |
| 2288 | |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2289 | psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2290 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2291 | /* Aborting a non-active operation is allowed */ |
| 2292 | if( operation->id == 0 ) |
| 2293 | return( PSA_SUCCESS ); |
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 | psa_status_t status = psa_driver_wrapper_mac_abort( operation ); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2296 | operation->mac_size = 0; |
| 2297 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2298 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2299 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2300 | return( status ); |
| 2301 | } |
| 2302 | |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2303 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2304 | psa_algorithm_t alg, |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2305 | const psa_key_attributes_t *attributes, |
| 2306 | uint8_t *mac_size ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2307 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2308 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2309 | psa_key_type_t key_type = psa_get_key_type( attributes ); |
| 2310 | size_t key_bits = psa_get_key_bits( attributes ); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2311 | |
Ronald Cron | 28ea050 | 2021-06-17 16:10:24 +0200 | [diff] [blame] | 2312 | if( ! PSA_ALG_IS_MAC( alg ) ) |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2313 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2314 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2315 | /* Validate the combination of key type and algorithm */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2316 | status = psa_mac_key_can_do( alg, key_type ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2317 | if( status != PSA_SUCCESS ) |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2318 | return( status ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2319 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2320 | /* Get the output length for the algorithm and key combination */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2321 | *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2322 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2323 | if( *mac_size < 4 ) |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2324 | { |
| 2325 | /* A very short MAC is too short for security since it can be |
| 2326 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2327 | * so we make this our minimum, even though 32 bits is still |
| 2328 | * too small for security. */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2329 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2330 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2331 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2332 | if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits, |
| 2333 | PSA_ALG_FULL_LENGTH_MAC( alg ) ) ) |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2334 | { |
| 2335 | /* It's impossible to "truncate" to a larger length than the full length |
| 2336 | * of the algorithm. */ |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2337 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2338 | } |
| 2339 | |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2340 | if( *mac_size > PSA_MAC_MAX_SIZE ) |
| 2341 | { |
| 2342 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2343 | * that is disabled in the compile-time configuration. The result can |
| 2344 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2345 | * configuration into account. In this case, force a return of |
| 2346 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2347 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2348 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2349 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2350 | * systematically generated tests. */ |
| 2351 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 2352 | } |
| 2353 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2354 | return( PSA_SUCCESS ); |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2355 | } |
| 2356 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2357 | static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, |
| 2358 | mbedtls_svc_key_id_t key, |
| 2359 | psa_algorithm_t alg, |
| 2360 | int is_sign ) |
| 2361 | { |
| 2362 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2363 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2364 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2365 | |
| 2366 | /* A context must be freshly initialized before it can be set up. */ |
| 2367 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2368 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2369 | status = PSA_ERROR_BAD_STATE; |
| 2370 | goto exit; |
| 2371 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2372 | |
| 2373 | status = psa_get_and_lock_key_slot_with_policy( |
| 2374 | key, |
| 2375 | &slot, |
Mateusz Starzyk | ce0e6a9 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2376 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2377 | alg ); |
| 2378 | if( status != PSA_SUCCESS ) |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2379 | goto exit; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2380 | |
| 2381 | psa_key_attributes_t attributes = { |
| 2382 | .core = slot->attr |
| 2383 | }; |
| 2384 | |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2385 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2386 | &operation->mac_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2387 | if( status != PSA_SUCCESS ) |
| 2388 | goto exit; |
| 2389 | |
| 2390 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2391 | /* Dispatch the MAC setup call with validated input */ |
| 2392 | if( is_sign ) |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2393 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2394 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 2395 | &attributes, |
| 2396 | slot->key.data, |
| 2397 | slot->key.bytes, |
| 2398 | alg ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2399 | } |
| 2400 | else |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2401 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2402 | status = psa_driver_wrapper_mac_verify_setup( operation, |
| 2403 | &attributes, |
| 2404 | slot->key.data, |
| 2405 | slot->key.bytes, |
| 2406 | alg ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2407 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2408 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2409 | exit: |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2410 | if( status != PSA_SUCCESS ) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2411 | psa_mac_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2412 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2413 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2414 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2415 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2416 | } |
| 2417 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2418 | psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2419 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2420 | psa_algorithm_t alg ) |
| 2421 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2422 | return( psa_mac_setup( operation, key, alg, 1 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2426 | mbedtls_svc_key_id_t key, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2427 | psa_algorithm_t alg ) |
| 2428 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2429 | return( psa_mac_setup( operation, key, alg, 0 ) ); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2430 | } |
| 2431 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2432 | psa_status_t psa_mac_update( psa_mac_operation_t *operation, |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2433 | const uint8_t *input, |
| 2434 | size_t input_length ) |
| 2435 | { |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2436 | if( operation->id == 0 ) |
Jaeden Amero | e236c2a | 2019-02-20 15:37:15 +0000 | [diff] [blame] | 2437 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2438 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2439 | /* Don't require hash implementations to behave correctly on a |
| 2440 | * zero-length input, which may have an invalid pointer. */ |
| 2441 | if( input_length == 0 ) |
| 2442 | return( PSA_SUCCESS ); |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2443 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2444 | psa_status_t status = psa_driver_wrapper_mac_update( operation, |
| 2445 | input, input_length ); |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2446 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2447 | psa_mac_abort( operation ); |
| 2448 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2449 | return( status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2450 | } |
| 2451 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2452 | psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2453 | uint8_t *mac, |
| 2454 | size_t mac_size, |
| 2455 | size_t *mac_length ) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2456 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2457 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2458 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2459 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2460 | if( operation->id == 0 ) |
Dave Rodgman | 8036bdd | 2021-06-24 16:19:08 +0100 | [diff] [blame] | 2461 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2462 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2463 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2464 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2465 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2466 | if( ! operation->is_sign ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2467 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2468 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2469 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2470 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2471 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2472 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2473 | * once all the error checks are done. */ |
| 2474 | if( operation->mac_size == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2475 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2476 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2477 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2478 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2479 | |
| 2480 | if( mac_size < operation->mac_size ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2481 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2482 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2483 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2484 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2485 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2486 | status = psa_driver_wrapper_mac_sign_finish( operation, |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2487 | mac, operation->mac_size, |
| 2488 | mac_length ); |
| 2489 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2490 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2491 | /* In case of success, set the potential excess room in the output buffer |
| 2492 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2493 | * In case of error, set the output length and content to a safe default, |
| 2494 | * such that in case the caller misses an error check, the output would be |
| 2495 | * an unachievable MAC. |
| 2496 | */ |
| 2497 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2498 | { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2499 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2500 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2501 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2502 | |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2503 | if( mac_size > operation->mac_size ) |
| 2504 | memset( &mac[operation->mac_size], '!', |
| 2505 | mac_size - operation->mac_size ); |
| 2506 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2507 | abort_status = psa_mac_abort( operation ); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2508 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2509 | return( status == PSA_SUCCESS ? abort_status : status ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2510 | } |
| 2511 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2512 | psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation, |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2513 | const uint8_t *mac, |
| 2514 | size_t mac_length ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2515 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2516 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2517 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2518 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2519 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2520 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2521 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2522 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2523 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2524 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2525 | if( operation->is_sign ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 2526 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2527 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2528 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2529 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2530 | |
| 2531 | if( operation->mac_size != mac_length ) |
| 2532 | { |
| 2533 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2534 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2535 | } |
| 2536 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2537 | status = psa_driver_wrapper_mac_verify_finish( operation, |
| 2538 | mac, mac_length ); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2539 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2540 | exit: |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2541 | abort_status = psa_mac_abort( operation ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2542 | |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2543 | return( status == PSA_SUCCESS ? abort_status : status ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2544 | } |
| 2545 | |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2546 | static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key, |
| 2547 | psa_algorithm_t alg, |
| 2548 | const uint8_t *input, |
| 2549 | size_t input_length, |
| 2550 | uint8_t *mac, |
| 2551 | size_t mac_size, |
| 2552 | size_t *mac_length, |
| 2553 | int is_sign ) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2554 | { |
| 2555 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2556 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2557 | psa_key_slot_t *slot; |
| 2558 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2559 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2560 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | ce0e6a9 | 2021-08-17 15:24:32 +0200 | [diff] [blame] | 2561 | key, |
| 2562 | &slot, |
| 2563 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2564 | alg ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2565 | if( status != PSA_SUCCESS ) |
| 2566 | goto exit; |
| 2567 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2568 | psa_key_attributes_t attributes = { |
| 2569 | .core = slot->attr |
| 2570 | }; |
| 2571 | |
| 2572 | status = psa_mac_finalize_alg_and_key_validation( alg, &attributes, |
| 2573 | &operation_mac_size ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2574 | if( status != PSA_SUCCESS ) |
| 2575 | goto exit; |
| 2576 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2577 | if( mac_size < operation_mac_size ) |
| 2578 | { |
| 2579 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2580 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | status = psa_driver_wrapper_mac_compute( |
| 2584 | &attributes, |
| 2585 | slot->key.data, slot->key.bytes, |
| 2586 | alg, |
| 2587 | input, input_length, |
| 2588 | mac, operation_mac_size, mac_length ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2589 | |
| 2590 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2591 | /* In case of success, set the potential excess room in the output buffer |
| 2592 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2593 | * In case of error, set the output length and content to a safe default, |
| 2594 | * such that in case the caller misses an error check, the output would be |
| 2595 | * an unachievable MAC. |
| 2596 | */ |
| 2597 | if( status != PSA_SUCCESS ) |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2598 | { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2599 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2600 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2601 | } |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2602 | if( mac_size > operation_mac_size ) |
| 2603 | memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2604 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2605 | unlock_status = psa_unlock_key_slot( slot ); |
| 2606 | |
| 2607 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2608 | } |
| 2609 | |
Ronald Cron | cd989b5 | 2021-06-18 14:23:33 +0200 | [diff] [blame] | 2610 | psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key, |
| 2611 | psa_algorithm_t alg, |
| 2612 | const uint8_t *input, |
| 2613 | size_t input_length, |
| 2614 | uint8_t *mac, |
| 2615 | size_t mac_size, |
| 2616 | size_t *mac_length) |
| 2617 | { |
| 2618 | return( psa_mac_compute_internal( key, alg, |
| 2619 | input, input_length, |
| 2620 | mac, mac_size, mac_length, 1 ) ); |
| 2621 | } |
| 2622 | |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2623 | psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key, |
| 2624 | psa_algorithm_t alg, |
| 2625 | const uint8_t *input, |
| 2626 | size_t input_length, |
| 2627 | const uint8_t *mac, |
| 2628 | size_t mac_length) |
| 2629 | { |
| 2630 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2631 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2632 | size_t actual_mac_length; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2633 | |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2634 | status = psa_mac_compute_internal( key, alg, |
| 2635 | input, input_length, |
| 2636 | actual_mac, sizeof( actual_mac ), |
| 2637 | &actual_mac_length, 0 ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2638 | if( status != PSA_SUCCESS ) |
| 2639 | goto exit; |
| 2640 | |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2641 | if( mac_length != actual_mac_length ) |
| 2642 | { |
| 2643 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2644 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2645 | } |
| 2646 | if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 ) |
| 2647 | { |
| 2648 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2649 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2650 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2651 | |
| 2652 | exit: |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2653 | mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) ); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2654 | |
| 2655 | return ( status ); |
| 2656 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2657 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2658 | /****************************************************************/ |
| 2659 | /* Asymmetric cryptography */ |
| 2660 | /****************************************************************/ |
| 2661 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2662 | 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] | 2663 | psa_algorithm_t alg ) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2664 | { |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2665 | if( input_is_message ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2666 | { |
| 2667 | if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) ) |
| 2668 | return( PSA_ERROR_INVALID_ARGUMENT ); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2669 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2670 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2671 | { |
| 2672 | if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) ) |
| 2673 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2674 | } |
| 2675 | } |
| 2676 | else |
| 2677 | { |
Mateusz Starzyk | e6d3eda | 2021-08-26 11:46:14 +0200 | [diff] [blame] | 2678 | if( ! PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2679 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2680 | } |
| 2681 | |
| 2682 | return( PSA_SUCCESS ); |
| 2683 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2684 | |
| 2685 | 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] | 2686 | int input_is_message, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2687 | psa_algorithm_t alg, |
| 2688 | const uint8_t * input, |
| 2689 | size_t input_length, |
| 2690 | uint8_t * signature, |
| 2691 | size_t signature_size, |
| 2692 | size_t * signature_length ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2693 | { |
| 2694 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2695 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2696 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2697 | |
| 2698 | *signature_length = 0; |
| 2699 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2700 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2701 | if( status != PSA_SUCCESS ) |
| 2702 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2703 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2704 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2705 | * 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] | 2706 | * buffer can in principle be empty since it doesn't actually have |
| 2707 | * to be a hash.) */ |
| 2708 | if( signature_size == 0 ) |
| 2709 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 2710 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2711 | status = psa_get_and_lock_key_slot_with_policy( |
| 2712 | key, &slot, |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2713 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2714 | PSA_KEY_USAGE_SIGN_HASH, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2715 | alg ); |
| 2716 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2717 | if( status != PSA_SUCCESS ) |
| 2718 | goto exit; |
| 2719 | |
| 2720 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
| 2721 | { |
| 2722 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2723 | goto exit; |
| 2724 | } |
| 2725 | |
| 2726 | psa_key_attributes_t attributes = { |
| 2727 | .core = slot->attr |
| 2728 | }; |
| 2729 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2730 | if( input_is_message ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2731 | { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2732 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2733 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2734 | alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2735 | signature, signature_size, signature_length ); |
| 2736 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2737 | else |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2738 | { |
| 2739 | |
| 2740 | status = psa_driver_wrapper_sign_hash( |
| 2741 | &attributes, slot->key.data, slot->key.bytes, |
| 2742 | alg, input, input_length, |
| 2743 | signature, signature_size, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2744 | } |
| 2745 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2746 | |
| 2747 | exit: |
| 2748 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2749 | * the trailing part on success) with something that isn't a valid signature |
| 2750 | * (barring an attack on the signature and deliberately-crafted input), |
| 2751 | * in case the caller doesn't check the return status properly. */ |
| 2752 | if( status == PSA_SUCCESS ) |
| 2753 | memset( signature + *signature_length, '!', |
| 2754 | signature_size - *signature_length ); |
| 2755 | else |
| 2756 | memset( signature, '!', signature_size ); |
| 2757 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2758 | * memset because signature may be NULL in this case. */ |
| 2759 | |
| 2760 | unlock_status = psa_unlock_key_slot( slot ); |
| 2761 | |
| 2762 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
| 2763 | } |
| 2764 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2765 | 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] | 2766 | int input_is_message, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2767 | psa_algorithm_t alg, |
| 2768 | const uint8_t * input, |
| 2769 | size_t input_length, |
| 2770 | const uint8_t * signature, |
| 2771 | size_t signature_length ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2772 | { |
| 2773 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2774 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2775 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2776 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2777 | status = psa_sign_verify_check_alg( input_is_message, alg ); |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2778 | if( status != PSA_SUCCESS ) |
| 2779 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2780 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2781 | status = psa_get_and_lock_key_slot_with_policy( |
| 2782 | key, &slot, |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2783 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2784 | PSA_KEY_USAGE_VERIFY_HASH, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2785 | alg ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2786 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2787 | if( status != PSA_SUCCESS ) |
| 2788 | return( status ); |
| 2789 | |
| 2790 | psa_key_attributes_t attributes = { |
| 2791 | .core = slot->attr |
| 2792 | }; |
| 2793 | |
gabor-mezei-arm | f048618 | 2021-05-12 11:03:09 +0200 | [diff] [blame] | 2794 | if( input_is_message ) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2795 | { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2796 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2797 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2798 | alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2799 | signature, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2800 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2801 | else |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2802 | { |
| 2803 | status = psa_driver_wrapper_verify_hash( |
| 2804 | &attributes, slot->key.data, slot->key.bytes, |
| 2805 | alg, input, input_length, |
| 2806 | signature, signature_length ); |
| 2807 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2808 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2809 | unlock_status = psa_unlock_key_slot( slot ); |
| 2810 | |
| 2811 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2812 | |
| 2813 | } |
| 2814 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2815 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2816 | const psa_key_attributes_t *attributes, |
| 2817 | const uint8_t *key_buffer, |
| 2818 | size_t key_buffer_size, |
| 2819 | psa_algorithm_t alg, |
| 2820 | const uint8_t *input, |
| 2821 | size_t input_length, |
| 2822 | uint8_t *signature, |
| 2823 | size_t signature_size, |
| 2824 | size_t *signature_length ) |
| 2825 | { |
| 2826 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2827 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2828 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2829 | { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2830 | size_t hash_length; |
| 2831 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2832 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2833 | status = psa_driver_wrapper_hash_compute( |
| 2834 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2835 | input, input_length, |
| 2836 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2837 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2838 | if( status != PSA_SUCCESS ) |
| 2839 | return status; |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2840 | |
| 2841 | return psa_driver_wrapper_sign_hash( |
| 2842 | attributes, key_buffer, key_buffer_size, |
| 2843 | alg, hash, hash_length, |
| 2844 | signature, signature_size, signature_length ); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2845 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2846 | |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2847 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2848 | } |
| 2849 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2850 | psa_status_t psa_sign_message( mbedtls_svc_key_id_t key, |
| 2851 | psa_algorithm_t alg, |
| 2852 | const uint8_t * input, |
| 2853 | size_t input_length, |
| 2854 | uint8_t * signature, |
| 2855 | size_t signature_size, |
| 2856 | size_t * signature_length ) |
| 2857 | { |
| 2858 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2859 | key, 1, alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2860 | signature, signature_size, signature_length ); |
| 2861 | } |
| 2862 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2863 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2864 | const psa_key_attributes_t *attributes, |
| 2865 | const uint8_t *key_buffer, |
| 2866 | size_t key_buffer_size, |
| 2867 | psa_algorithm_t alg, |
| 2868 | const uint8_t *input, |
| 2869 | size_t input_length, |
| 2870 | const uint8_t *signature, |
| 2871 | size_t signature_length ) |
| 2872 | { |
| 2873 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2874 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2875 | if ( PSA_ALG_IS_SIGN_HASH( alg ) ) |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2876 | { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2877 | size_t hash_length; |
| 2878 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2879 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2880 | status = psa_driver_wrapper_hash_compute( |
| 2881 | PSA_ALG_SIGN_GET_HASH( alg ), |
| 2882 | input, input_length, |
| 2883 | hash, sizeof( hash ), &hash_length ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2884 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2885 | if( status != PSA_SUCCESS ) |
| 2886 | return status; |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2887 | |
| 2888 | return psa_driver_wrapper_verify_hash( |
| 2889 | attributes, key_buffer, key_buffer_size, |
| 2890 | alg, hash, hash_length, |
| 2891 | signature, signature_length ); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2892 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2893 | |
gabor-mezei-arm | 474a35f | 2021-05-05 13:59:01 +0200 | [diff] [blame] | 2894 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2895 | } |
| 2896 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2897 | psa_status_t psa_verify_message( mbedtls_svc_key_id_t key, |
| 2898 | psa_algorithm_t alg, |
| 2899 | const uint8_t * input, |
| 2900 | size_t input_length, |
| 2901 | const uint8_t * signature, |
| 2902 | size_t signature_length ) |
| 2903 | { |
| 2904 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2905 | key, 1, alg, input, input_length, |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2906 | signature, signature_length ); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2907 | } |
| 2908 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2909 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2910 | const psa_key_attributes_t *attributes, |
| 2911 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2912 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2913 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2914 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2915 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2916 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2917 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2918 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2919 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2920 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2921 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2922 | return( mbedtls_psa_rsa_sign_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2923 | attributes, |
| 2924 | key_buffer, key_buffer_size, |
| 2925 | alg, hash, hash_length, |
| 2926 | signature, signature_size, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2927 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2928 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2929 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2930 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2931 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2932 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 2933 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2934 | } |
Ronald Cron | 81ca97e | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 2935 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2936 | { |
| 2937 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 2938 | { |
| 2939 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2940 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2941 | return( mbedtls_psa_ecdsa_sign_hash( |
| 2942 | attributes, |
| 2943 | key_buffer, key_buffer_size, |
| 2944 | alg, hash, hash_length, |
| 2945 | signature, signature_size, signature_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2946 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2947 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2948 | } |
| 2949 | else |
| 2950 | { |
| 2951 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 2952 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2953 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2954 | |
| 2955 | (void)key_buffer; |
| 2956 | (void)key_buffer_size; |
| 2957 | (void)hash; |
| 2958 | (void)hash_length; |
| 2959 | (void)signature; |
| 2960 | (void)signature_size; |
| 2961 | (void)signature_length; |
| 2962 | |
| 2963 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2964 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2965 | |
| 2966 | psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key, |
| 2967 | psa_algorithm_t alg, |
| 2968 | const uint8_t *hash, |
| 2969 | size_t hash_length, |
| 2970 | uint8_t *signature, |
| 2971 | size_t signature_size, |
| 2972 | size_t *signature_length ) |
| 2973 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2974 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2975 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 2976 | signature, signature_size, signature_length ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2977 | } |
| 2978 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2979 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2980 | const psa_key_attributes_t *attributes, |
| 2981 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2982 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 2983 | const uint8_t *signature, size_t signature_length ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2984 | { |
Ronald Cron | 99b8ed7 | 2021-02-17 10:33:32 +0100 | [diff] [blame] | 2985 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2986 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2987 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) || |
| 2988 | PSA_ALG_IS_RSA_PSS( alg) ) |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2989 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2990 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2991 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2992 | return( mbedtls_psa_rsa_verify_hash( |
Ronald Cron | 072722c | 2020-12-09 16:36:19 +0100 | [diff] [blame] | 2993 | attributes, |
| 2994 | key_buffer, key_buffer_size, |
| 2995 | alg, hash, hash_length, |
| 2996 | signature, signature_length ) ); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2997 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2998 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2999 | } |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3000 | else |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3001 | { |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3002 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 3003 | } |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3004 | } |
Ronald Cron | 81ca97e | 2021-04-09 15:32:03 +0200 | [diff] [blame] | 3005 | else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3006 | { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3007 | if( PSA_ALG_IS_ECDSA( alg ) ) |
| 3008 | { |
| 3009 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3010 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3011 | return( mbedtls_psa_ecdsa_verify_hash( |
| 3012 | attributes, |
| 3013 | key_buffer, key_buffer_size, |
| 3014 | alg, hash, hash_length, |
| 3015 | signature, signature_length ) ); |
| 3016 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3017 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3018 | } |
| 3019 | else |
| 3020 | { |
| 3021 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3022 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3023 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3024 | |
| 3025 | (void)key_buffer; |
| 3026 | (void)key_buffer_size; |
| 3027 | (void)hash; |
| 3028 | (void)hash_length; |
| 3029 | (void)signature; |
| 3030 | (void)signature_length; |
| 3031 | |
| 3032 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3033 | } |
| 3034 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3035 | psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key, |
| 3036 | psa_algorithm_t alg, |
| 3037 | const uint8_t *hash, |
| 3038 | size_t hash_length, |
| 3039 | const uint8_t *signature, |
| 3040 | size_t signature_length ) |
| 3041 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3042 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3043 | key, 0, alg, hash, hash_length, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 3044 | signature, signature_length ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3045 | } |
| 3046 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3047 | psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key, |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3048 | psa_algorithm_t alg, |
| 3049 | const uint8_t *input, |
| 3050 | size_t input_length, |
| 3051 | const uint8_t *salt, |
| 3052 | size_t salt_length, |
| 3053 | uint8_t *output, |
| 3054 | size_t output_size, |
| 3055 | size_t *output_length ) |
| 3056 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3057 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3058 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3059 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3060 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3061 | (void) input; |
| 3062 | (void) input_length; |
| 3063 | (void) salt; |
| 3064 | (void) output; |
| 3065 | (void) output_size; |
| 3066 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3067 | *output_length = 0; |
| 3068 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3069 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3070 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3071 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3072 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3073 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3074 | if( status != PSA_SUCCESS ) |
| 3075 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3076 | if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) || |
| 3077 | PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3078 | { |
| 3079 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3080 | goto exit; |
| 3081 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3082 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3083 | psa_key_attributes_t attributes = { |
| 3084 | .core = slot->attr |
| 3085 | }; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3086 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3087 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3088 | &attributes, slot->key.data, slot->key.bytes, |
| 3089 | alg, input, input_length, salt, salt_length, |
| 3090 | output, output_size, output_length ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3091 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3092 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3093 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3094 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3095 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3096 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3097 | psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key, |
Gilles Peskine | 61b91d4 | 2018-06-08 16:09:36 +0200 | [diff] [blame] | 3098 | psa_algorithm_t alg, |
| 3099 | const uint8_t *input, |
| 3100 | size_t input_length, |
| 3101 | const uint8_t *salt, |
| 3102 | size_t salt_length, |
| 3103 | uint8_t *output, |
| 3104 | size_t output_size, |
| 3105 | size_t *output_length ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3106 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3107 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3108 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3109 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3110 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3111 | (void) input; |
| 3112 | (void) input_length; |
| 3113 | (void) salt; |
| 3114 | (void) output; |
| 3115 | (void) output_size; |
| 3116 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3117 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3118 | |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3119 | if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 ) |
| 3120 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3121 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3122 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 3123 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3124 | if( status != PSA_SUCCESS ) |
| 3125 | return( status ); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 3126 | if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3127 | { |
| 3128 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3129 | goto exit; |
| 3130 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3131 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3132 | psa_key_attributes_t attributes = { |
| 3133 | .core = slot->attr |
| 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 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3137 | &attributes, slot->key.data, slot->key.bytes, |
| 3138 | alg, input, input_length, salt, salt_length, |
| 3139 | output, output_size, output_length ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3140 | |
| 3141 | exit: |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3142 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3143 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3144 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3145 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3146 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3147 | |
| 3148 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3149 | /****************************************************************/ |
| 3150 | /* Symmetric cryptography */ |
| 3151 | /****************************************************************/ |
| 3152 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3153 | static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, |
| 3154 | mbedtls_svc_key_id_t key, |
| 3155 | psa_algorithm_t alg, |
| 3156 | mbedtls_operation_t cipher_operation ) |
| 3157 | { |
| 3158 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3159 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3160 | psa_key_slot_t *slot = NULL; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3161 | psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ? |
| 3162 | PSA_KEY_USAGE_ENCRYPT : |
| 3163 | PSA_KEY_USAGE_DECRYPT ); |
| 3164 | |
| 3165 | /* A context must be freshly initialized before it can be set up. */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3166 | if( operation->id != 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3167 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3168 | status = PSA_ERROR_BAD_STATE; |
| 3169 | goto exit; |
| 3170 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3171 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3172 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3173 | { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3174 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3175 | goto exit; |
| 3176 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3177 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3178 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg ); |
| 3179 | if( status != PSA_SUCCESS ) |
| 3180 | goto exit; |
| 3181 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3182 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3183 | * 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] | 3184 | * so we only set it (in the driver wrapper) after resources have been |
| 3185 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3186 | operation->iv_set = 0; |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3187 | if( alg == PSA_ALG_ECB_NO_PADDING ) |
| 3188 | operation->iv_required = 0; |
| 3189 | else |
| 3190 | operation->iv_required = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3191 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3192 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3193 | psa_key_attributes_t attributes = { |
| 3194 | .core = slot->attr |
| 3195 | }; |
| 3196 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3197 | /* Try doing the operation through a driver before using software fallback. */ |
| 3198 | if( cipher_operation == MBEDTLS_ENCRYPT ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3199 | status = psa_driver_wrapper_cipher_encrypt_setup( operation, |
| 3200 | &attributes, |
| 3201 | slot->key.data, |
| 3202 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3203 | alg ); |
| 3204 | else |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3205 | status = psa_driver_wrapper_cipher_decrypt_setup( operation, |
| 3206 | &attributes, |
| 3207 | slot->key.data, |
| 3208 | slot->key.bytes, |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3209 | alg ); |
| 3210 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3211 | exit: |
Ronald Cron | 06aa442 | 2021-03-09 17:32:57 +0100 | [diff] [blame] | 3212 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3213 | psa_cipher_abort( operation ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3214 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3215 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3216 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3217 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3218 | } |
| 3219 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3220 | psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3221 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3222 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3223 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3224 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3225 | } |
| 3226 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3227 | psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3228 | mbedtls_svc_key_id_t key, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3229 | psa_algorithm_t alg ) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3230 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3231 | return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) ); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3232 | } |
| 3233 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3234 | psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3235 | uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3236 | size_t iv_size, |
| 3237 | size_t *iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3238 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3239 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3240 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3241 | size_t default_iv_length; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3242 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3243 | if( operation->id == 0 ) |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3244 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3245 | status = PSA_ERROR_BAD_STATE; |
| 3246 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3247 | } |
| 3248 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3249 | if( operation->iv_set || ! operation->iv_required ) |
| 3250 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3251 | status = PSA_ERROR_BAD_STATE; |
| 3252 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3253 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3254 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3255 | default_iv_length = operation->default_iv_length; |
| 3256 | if( iv_size < default_iv_length ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3257 | { |
| 3258 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3259 | goto exit; |
| 3260 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3261 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3262 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
| 3263 | { |
| 3264 | status = PSA_ERROR_GENERIC_ERROR; |
| 3265 | goto exit; |
| 3266 | } |
| 3267 | |
| 3268 | status = psa_generate_random( local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3269 | if( status != PSA_SUCCESS ) |
| 3270 | goto exit; |
| 3271 | |
| 3272 | status = psa_driver_wrapper_cipher_set_iv( operation, |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3273 | local_iv, default_iv_length ); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3274 | |
| 3275 | exit: |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3276 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3277 | { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3278 | memcpy( iv, local_iv, default_iv_length ); |
| 3279 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3280 | operation->iv_set = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3281 | } |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3282 | else |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3283 | { |
| 3284 | *iv_length = 0; |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3285 | psa_cipher_abort( operation ); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3286 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3287 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3288 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3289 | } |
| 3290 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3291 | psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3292 | const uint8_t *iv, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 3293 | size_t iv_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3294 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3295 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3296 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3297 | if( operation->id == 0 ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3298 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3299 | status = PSA_ERROR_BAD_STATE; |
| 3300 | goto exit; |
| 3301 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3302 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3303 | if( operation->iv_set || ! operation->iv_required ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3304 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3305 | status = PSA_ERROR_BAD_STATE; |
| 3306 | goto exit; |
| 3307 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3308 | |
| 3309 | if( iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
Dave Rodgman | b5dd7c7 | 2021-06-24 16:17:43 +0100 | [diff] [blame] | 3310 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3311 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3312 | goto exit; |
| 3313 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3314 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3315 | status = psa_driver_wrapper_cipher_set_iv( operation, |
| 3316 | iv, |
| 3317 | iv_length ); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3318 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3319 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3320 | if( status == PSA_SUCCESS ) |
| 3321 | operation->iv_set = 1; |
| 3322 | else |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3323 | psa_cipher_abort( operation ); |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3324 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3325 | } |
| 3326 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3327 | psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, |
| 3328 | const uint8_t *input, |
| 3329 | size_t input_length, |
Andrew Thoelke | 163639b | 2019-05-15 12:33:23 +0100 | [diff] [blame] | 3330 | uint8_t *output, |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3331 | size_t output_size, |
| 3332 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3333 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3334 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3335 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3336 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3337 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3338 | status = PSA_ERROR_BAD_STATE; |
| 3339 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3340 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3341 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3342 | if( operation->iv_required && ! operation->iv_set ) |
| 3343 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3344 | status = PSA_ERROR_BAD_STATE; |
| 3345 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3346 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3347 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3348 | status = psa_driver_wrapper_cipher_update( operation, |
| 3349 | input, |
| 3350 | input_length, |
| 3351 | output, |
| 3352 | output_size, |
| 3353 | output_length ); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3354 | |
| 3355 | exit: |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3356 | if( status != PSA_SUCCESS ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3357 | psa_cipher_abort( operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3358 | |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3359 | return( status ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3360 | } |
| 3361 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3362 | psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, |
| 3363 | uint8_t *output, |
| 3364 | size_t output_size, |
| 3365 | size_t *output_length ) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3366 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3367 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3368 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3369 | if( operation->id == 0 ) |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3370 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3371 | status = PSA_ERROR_BAD_STATE; |
| 3372 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3373 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3374 | |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3375 | if( operation->iv_required && ! operation->iv_set ) |
| 3376 | { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3377 | status = PSA_ERROR_BAD_STATE; |
| 3378 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3379 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3380 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3381 | status = psa_driver_wrapper_cipher_finish( operation, |
| 3382 | output, |
| 3383 | output_size, |
| 3384 | output_length ); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3385 | |
| 3386 | exit: |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3387 | if( status == PSA_SUCCESS ) |
| 3388 | return( psa_cipher_abort( operation ) ); |
| 3389 | else |
| 3390 | { |
| 3391 | *output_length = 0; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3392 | (void) psa_cipher_abort( operation ); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3393 | |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3394 | return( status ); |
| 3395 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3396 | } |
| 3397 | |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3398 | psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) |
| 3399 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3400 | if( operation->id == 0 ) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3401 | { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3402 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3403 | * in use. It's ok to call abort on such an object, and there's |
| 3404 | * nothing to do. */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3405 | return( PSA_SUCCESS ); |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3406 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3407 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 3408 | psa_driver_wrapper_cipher_abort( operation ); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3409 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3410 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3411 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3412 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3413 | |
Moran Peker | 395db87 | 2018-05-31 14:07:14 +0300 | [diff] [blame] | 3414 | return( PSA_SUCCESS ); |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3415 | } |
| 3416 | |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3417 | psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key, |
| 3418 | psa_algorithm_t alg, |
| 3419 | const uint8_t *input, |
| 3420 | size_t input_length, |
| 3421 | uint8_t *output, |
| 3422 | size_t output_size, |
| 3423 | size_t *output_length ) |
| 3424 | { |
| 3425 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3426 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3427 | psa_key_slot_t *slot = NULL; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3428 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3429 | size_t default_iv_length = 0; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3430 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3431 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3432 | { |
| 3433 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3434 | goto exit; |
| 3435 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3436 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3437 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3438 | PSA_KEY_USAGE_ENCRYPT, |
| 3439 | alg ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3440 | if( status != PSA_SUCCESS ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3441 | goto exit; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3442 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3443 | psa_key_attributes_t attributes = { |
| 3444 | .core = slot->attr |
| 3445 | }; |
| 3446 | |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3447 | default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ); |
| 3448 | if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE ) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3449 | { |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3450 | status = PSA_ERROR_GENERIC_ERROR; |
| 3451 | goto exit; |
| 3452 | } |
| 3453 | |
| 3454 | if( default_iv_length > 0 ) |
| 3455 | { |
| 3456 | if( output_size < default_iv_length ) |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3457 | { |
| 3458 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3459 | goto exit; |
| 3460 | } |
| 3461 | |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3462 | status = psa_generate_random( local_iv, default_iv_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3463 | if( status != PSA_SUCCESS ) |
| 3464 | goto exit; |
| 3465 | } |
| 3466 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3467 | status = psa_driver_wrapper_cipher_encrypt( |
| 3468 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3469 | alg, local_iv, default_iv_length, input, input_length, |
| 3470 | output + default_iv_length, output_size - default_iv_length, |
| 3471 | output_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3472 | |
| 3473 | exit: |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3474 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3475 | if( status == PSA_SUCCESS ) |
| 3476 | status = unlock_status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3477 | |
Ronald Cron | 9b67428 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3478 | if( status == PSA_SUCCESS ) |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3479 | { |
| 3480 | if( default_iv_length > 0 ) |
| 3481 | memcpy( output, local_iv, default_iv_length ); |
| 3482 | *output_length += default_iv_length; |
| 3483 | } |
Ronald Cron | 9b67428 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 3484 | else |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3485 | *output_length = 0; |
| 3486 | |
| 3487 | return( status ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key, |
| 3491 | psa_algorithm_t alg, |
| 3492 | const uint8_t *input, |
| 3493 | size_t input_length, |
| 3494 | uint8_t *output, |
| 3495 | size_t output_size, |
| 3496 | size_t *output_length ) |
| 3497 | { |
| 3498 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3499 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3500 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3501 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3502 | if( ! PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3503 | { |
| 3504 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3505 | goto exit; |
| 3506 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3507 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3508 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, |
| 3509 | PSA_KEY_USAGE_DECRYPT, |
| 3510 | alg ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3511 | if( status != PSA_SUCCESS ) |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3512 | goto exit; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3513 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3514 | psa_key_attributes_t attributes = { |
| 3515 | .core = slot->attr |
| 3516 | }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3517 | |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 3518 | if( alg == PSA_ALG_CCM_STAR_NO_TAG && input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) ) |
| 3519 | { |
| 3520 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3521 | goto exit; |
| 3522 | } |
| 3523 | 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] | 3524 | { |
| 3525 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3526 | goto exit; |
| 3527 | } |
| 3528 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3529 | status = psa_driver_wrapper_cipher_decrypt( |
| 3530 | &attributes, slot->key.data, slot->key.bytes, |
| 3531 | alg, input, input_length, |
| 3532 | output, output_size, output_length ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3533 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3534 | exit: |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3535 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3536 | if( status == PSA_SUCCESS ) |
| 3537 | status = unlock_status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3538 | |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3539 | if( status != PSA_SUCCESS ) |
| 3540 | *output_length = 0; |
| 3541 | |
| 3542 | return( status ); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3546 | /****************************************************************/ |
| 3547 | /* AEAD */ |
| 3548 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3549 | |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3550 | /* Helper function to get the base algorithm from its variants. */ |
| 3551 | static psa_algorithm_t psa_aead_get_base_algorithm( psa_algorithm_t alg ) |
| 3552 | { |
| 3553 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg ); |
| 3554 | } |
| 3555 | |
| 3556 | /* Helper function to perform common nonce length checks. */ |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3557 | static psa_status_t psa_aead_check_nonce_length( psa_algorithm_t alg, |
| 3558 | size_t nonce_length ) |
| 3559 | { |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3560 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm( alg ); |
| 3561 | |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3562 | switch(base_alg) |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3563 | { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3564 | #if defined(PSA_WANT_ALG_GCM) |
| 3565 | case PSA_ALG_GCM: |
| 3566 | /* Not checking max nonce size here as GCM spec allows almost |
| 3567 | * arbitrarily large nonces. Please note that we do not generally |
| 3568 | * recommend the usage of nonces of greater length than |
| 3569 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter |
| 3570 | * size, which can then lead to collisions if you encrypt a very |
| 3571 | * large number of messages.*/ |
| 3572 | if( nonce_length != 0 ) |
| 3573 | return( PSA_SUCCESS ); |
| 3574 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3575 | #endif /* PSA_WANT_ALG_GCM */ |
| 3576 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3577 | case PSA_ALG_CCM: |
| 3578 | if( nonce_length >= 7 && nonce_length <= 13 ) |
| 3579 | return( PSA_SUCCESS ); |
| 3580 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3581 | #endif /* PSA_WANT_ALG_CCM */ |
| 3582 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3583 | case PSA_ALG_CHACHA20_POLY1305: |
| 3584 | if( nonce_length == 12 ) |
| 3585 | return( PSA_SUCCESS ); |
Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 3586 | else if( nonce_length == 8 ) |
| 3587 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3588 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3589 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3590 | default: |
Przemek Stekiel | 4c49927 | 2022-09-27 13:55:37 +0200 | [diff] [blame] | 3591 | (void) nonce_length; |
Bence Szépkúti | 357b78e | 2021-11-09 13:17:17 +0100 | [diff] [blame] | 3592 | return( PSA_ERROR_NOT_SUPPORTED ); |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3593 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3594 | |
Bence Szépkúti | 357b78e | 2021-11-09 13:17:17 +0100 | [diff] [blame] | 3595 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3596 | } |
| 3597 | |
Bence Szépkúti | aa3a6e4 | 2022-01-13 16:26:03 +0100 | [diff] [blame] | 3598 | static psa_status_t psa_aead_check_algorithm( psa_algorithm_t alg ) |
| 3599 | { |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3600 | if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) ) |
| 3601 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3602 | |
| 3603 | return( PSA_SUCCESS ); |
| 3604 | } |
| 3605 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3606 | psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3607 | psa_algorithm_t alg, |
| 3608 | const uint8_t *nonce, |
| 3609 | size_t nonce_length, |
| 3610 | const uint8_t *additional_data, |
| 3611 | size_t additional_data_length, |
| 3612 | const uint8_t *plaintext, |
| 3613 | size_t plaintext_length, |
| 3614 | uint8_t *ciphertext, |
| 3615 | size_t ciphertext_size, |
| 3616 | size_t *ciphertext_length ) |
| 3617 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3618 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3619 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3620 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3621 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3622 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3623 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3624 | if( status != PSA_SUCCESS ) |
| 3625 | return( status ); |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3626 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3627 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3628 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3629 | if( status != PSA_SUCCESS ) |
| 3630 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3631 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3632 | psa_key_attributes_t attributes = { |
| 3633 | .core = slot->attr |
| 3634 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3635 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3636 | status = psa_aead_check_nonce_length( alg, nonce_length ); |
| 3637 | if( status != PSA_SUCCESS ) |
| 3638 | goto exit; |
| 3639 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3640 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3641 | &attributes, slot->key.data, slot->key.bytes, |
| 3642 | alg, |
| 3643 | nonce, nonce_length, |
| 3644 | additional_data, additional_data_length, |
| 3645 | plaintext, plaintext_length, |
| 3646 | ciphertext, ciphertext_size, ciphertext_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3647 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3648 | if( status != PSA_SUCCESS && ciphertext_size != 0 ) |
| 3649 | memset( ciphertext, 0, ciphertext_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3650 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3651 | exit: |
Ronald Cron | 9f31017 | 2021-03-16 16:36:37 +0100 | [diff] [blame] | 3652 | psa_unlock_key_slot( slot ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3653 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3654 | return( status ); |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3655 | } |
| 3656 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3657 | psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key, |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3658 | psa_algorithm_t alg, |
| 3659 | const uint8_t *nonce, |
| 3660 | size_t nonce_length, |
| 3661 | const uint8_t *additional_data, |
| 3662 | size_t additional_data_length, |
| 3663 | const uint8_t *ciphertext, |
| 3664 | size_t ciphertext_length, |
| 3665 | uint8_t *plaintext, |
| 3666 | size_t plaintext_size, |
| 3667 | size_t *plaintext_length ) |
| 3668 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3669 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3670 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3671 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3672 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3673 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3674 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3675 | if( status != PSA_SUCCESS ) |
| 3676 | return( status ); |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3677 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3678 | status = psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3679 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3680 | if( status != PSA_SUCCESS ) |
| 3681 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3682 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3683 | psa_key_attributes_t attributes = { |
| 3684 | .core = slot->attr |
| 3685 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3686 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3687 | status = psa_aead_check_nonce_length( alg, nonce_length ); |
| 3688 | if( status != PSA_SUCCESS ) |
| 3689 | goto exit; |
| 3690 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3691 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3692 | &attributes, slot->key.data, slot->key.bytes, |
| 3693 | alg, |
| 3694 | nonce, nonce_length, |
| 3695 | additional_data, additional_data_length, |
| 3696 | ciphertext, ciphertext_length, |
| 3697 | plaintext, plaintext_size, plaintext_length ); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3698 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3699 | if( status != PSA_SUCCESS && plaintext_size != 0 ) |
| 3700 | memset( plaintext, 0, plaintext_size ); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3701 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3702 | exit: |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3703 | psa_unlock_key_slot( slot ); |
| 3704 | |
Gilles Peskine | edf9a65 | 2018-08-17 18:11:56 +0200 | [diff] [blame] | 3705 | return( status ); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3706 | } |
| 3707 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3708 | static psa_status_t psa_validate_tag_length( psa_algorithm_t alg ) { |
Przemek Stekiel | 8a05a64 | 2022-10-06 17:01:58 +0200 | [diff] [blame] | 3709 | const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3710 | |
| 3711 | switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ) |
| 3712 | { |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3713 | #if defined(PSA_WANT_ALG_CCM) |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3714 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
| 3715 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/ |
| 3716 | if( tag_len < 4 || tag_len > 16 || tag_len % 2 ) |
| 3717 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3718 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3719 | #endif /* PSA_WANT_ALG_CCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3720 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3721 | #if defined(PSA_WANT_ALG_GCM) |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3722 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
| 3723 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */ |
| 3724 | if( tag_len != 4 && tag_len != 8 && ( tag_len < 12 || tag_len > 16 ) ) |
| 3725 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3726 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3727 | #endif /* PSA_WANT_ALG_GCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3728 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3729 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3730 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 3731 | /* We only support the default tag length. */ |
| 3732 | if( tag_len != 16 ) |
| 3733 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 3734 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3735 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3736 | |
| 3737 | default: |
| 3738 | (void) tag_len; |
| 3739 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 3740 | } |
| 3741 | return( PSA_SUCCESS ); |
| 3742 | } |
| 3743 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3744 | /* Set the key for a multipart authenticated operation. */ |
| 3745 | static psa_status_t psa_aead_setup( psa_aead_operation_t *operation, |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3746 | int is_encrypt, |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3747 | mbedtls_svc_key_id_t key, |
| 3748 | psa_algorithm_t alg ) |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3749 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3750 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3751 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3752 | psa_key_slot_t *slot = NULL; |
| 3753 | psa_key_usage_t key_usage = 0; |
| 3754 | |
Bence Szépkúti | 39fb9d1 | 2022-01-13 14:33:45 +0100 | [diff] [blame] | 3755 | status = psa_aead_check_algorithm( alg ); |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3756 | if( status != PSA_SUCCESS ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3757 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3758 | |
| 3759 | if( operation->id != 0 ) |
| 3760 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3761 | status = PSA_ERROR_BAD_STATE; |
| 3762 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | if( operation->nonce_set || operation->lengths_set || |
| 3766 | operation->ad_started || operation->body_started ) |
| 3767 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3768 | status = PSA_ERROR_BAD_STATE; |
| 3769 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3770 | } |
| 3771 | |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3772 | if( is_encrypt ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3773 | key_usage = PSA_KEY_USAGE_ENCRYPT; |
| 3774 | else |
| 3775 | key_usage = PSA_KEY_USAGE_DECRYPT; |
| 3776 | |
| 3777 | status = psa_get_and_lock_key_slot_with_policy( key, &slot, key_usage, |
| 3778 | alg ); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3779 | if( status != PSA_SUCCESS ) |
| 3780 | goto exit; |
| 3781 | |
| 3782 | psa_key_attributes_t attributes = { |
| 3783 | .core = slot->attr |
| 3784 | }; |
| 3785 | |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 3786 | if( ( status = psa_validate_tag_length( alg ) ) != PSA_SUCCESS ) |
| 3787 | goto exit; |
| 3788 | |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3789 | if( is_encrypt ) |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3790 | status = psa_driver_wrapper_aead_encrypt_setup( operation, |
| 3791 | &attributes, |
| 3792 | slot->key.data, |
| 3793 | slot->key.bytes, |
| 3794 | alg ); |
| 3795 | else |
| 3796 | status = psa_driver_wrapper_aead_decrypt_setup( operation, |
| 3797 | &attributes, |
| 3798 | slot->key.data, |
| 3799 | slot->key.bytes, |
| 3800 | alg ); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3801 | if( status != PSA_SUCCESS ) |
| 3802 | goto exit; |
| 3803 | |
| 3804 | operation->key_type = psa_get_key_type( &attributes ); |
| 3805 | |
| 3806 | exit: |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3807 | unlock_status = psa_unlock_key_slot( slot ); |
| 3808 | |
| 3809 | if( status == PSA_SUCCESS ) |
| 3810 | { |
| 3811 | status = unlock_status; |
| 3812 | operation->alg = psa_aead_get_base_algorithm( alg ); |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3813 | operation->is_encrypt = is_encrypt; |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3814 | } |
| 3815 | else |
| 3816 | psa_aead_abort( operation ); |
| 3817 | |
| 3818 | return( status ); |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3819 | } |
| 3820 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3821 | /* Set the key for a multipart authenticated encryption operation. */ |
| 3822 | psa_status_t psa_aead_encrypt_setup( psa_aead_operation_t *operation, |
| 3823 | mbedtls_svc_key_id_t key, |
| 3824 | psa_algorithm_t alg ) |
| 3825 | { |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3826 | return( psa_aead_setup( operation, 1, key, alg ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | /* Set the key for a multipart authenticated decryption operation. */ |
| 3830 | psa_status_t psa_aead_decrypt_setup( psa_aead_operation_t *operation, |
| 3831 | mbedtls_svc_key_id_t key, |
| 3832 | psa_algorithm_t alg ) |
| 3833 | { |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3834 | return( psa_aead_setup( operation, 0, key, alg ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | /* Generate a random nonce / IV for multipart AEAD operation */ |
| 3838 | psa_status_t psa_aead_generate_nonce( psa_aead_operation_t *operation, |
| 3839 | uint8_t *nonce, |
| 3840 | size_t nonce_size, |
| 3841 | size_t *nonce_length ) |
| 3842 | { |
| 3843 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3844 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; |
Paul Elliott | b91da71 | 2021-05-20 14:43:47 +0100 | [diff] [blame] | 3845 | size_t required_nonce_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3846 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3847 | *nonce_length = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3848 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3849 | if( operation->id == 0 ) |
| 3850 | { |
| 3851 | status = PSA_ERROR_BAD_STATE; |
| 3852 | goto exit; |
| 3853 | } |
| 3854 | |
Paul Elliott | daf5c89 | 2021-08-25 16:24:58 +0100 | [diff] [blame] | 3855 | if( operation->nonce_set || !operation->is_encrypt ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3856 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3857 | status = PSA_ERROR_BAD_STATE; |
| 3858 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3859 | } |
| 3860 | |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 3861 | /* For CCM, this size may not be correct according to the PSA |
| 3862 | * specification. The PSA Crypto 1.0.1 specification states: |
| 3863 | * |
| 3864 | * CCM encodes the plaintext length pLen in L octets, with L the smallest |
| 3865 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. |
| 3866 | * |
| 3867 | * However this restriction that L has to be the smallest integer is not |
| 3868 | * applied in practice, and it is not implementable here since the |
| 3869 | * plaintext length may or may not be known at this time. */ |
Paul Elliott | bbe90b5 | 2021-05-11 22:22:42 +0100 | [diff] [blame] | 3870 | required_nonce_size = PSA_AEAD_NONCE_LENGTH( operation->key_type, |
Paul Elliott | 3242f6c | 2021-08-25 16:33:47 +0100 | [diff] [blame] | 3871 | operation->alg ); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3872 | if( nonce_size < required_nonce_size ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3873 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3874 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3875 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3876 | } |
| 3877 | |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3878 | status = psa_generate_random( local_nonce, required_nonce_size ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3879 | if( status != PSA_SUCCESS ) |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3880 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3881 | |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3882 | status = psa_aead_set_nonce( operation, local_nonce, required_nonce_size ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3883 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3884 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3885 | if( status == PSA_SUCCESS ) |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3886 | { |
| 3887 | memcpy( nonce, local_nonce, required_nonce_size ); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3888 | *nonce_length = required_nonce_size; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3889 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3890 | else |
| 3891 | psa_aead_abort( operation ); |
| 3892 | |
| 3893 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | /* Set the nonce for a multipart authenticated encryption or decryption |
| 3897 | operation.*/ |
| 3898 | psa_status_t psa_aead_set_nonce( psa_aead_operation_t *operation, |
| 3899 | const uint8_t *nonce, |
| 3900 | size_t nonce_length ) |
| 3901 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3902 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3903 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3904 | if( operation->id == 0 ) |
| 3905 | { |
| 3906 | status = PSA_ERROR_BAD_STATE; |
| 3907 | goto exit; |
| 3908 | } |
| 3909 | |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 3910 | if( operation->nonce_set ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3911 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3912 | status = PSA_ERROR_BAD_STATE; |
| 3913 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3914 | } |
| 3915 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3916 | status = psa_aead_check_nonce_length( operation->alg, nonce_length ); |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3917 | if( status != PSA_SUCCESS ) |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 3918 | { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3919 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3920 | goto exit; |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 3921 | } |
Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 3922 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3923 | status = psa_driver_wrapper_aead_set_nonce( operation, nonce, |
| 3924 | nonce_length ); |
| 3925 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3926 | exit: |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3927 | if( status == PSA_SUCCESS ) |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3928 | operation->nonce_set = 1; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3929 | else |
| 3930 | psa_aead_abort( operation ); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3931 | |
| 3932 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3933 | } |
| 3934 | |
| 3935 | /* Declare the lengths of the message and additional data for multipart AEAD. */ |
| 3936 | psa_status_t psa_aead_set_lengths( psa_aead_operation_t *operation, |
| 3937 | size_t ad_length, |
| 3938 | size_t plaintext_length ) |
| 3939 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3940 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3941 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3942 | if( operation->id == 0 ) |
| 3943 | { |
| 3944 | status = PSA_ERROR_BAD_STATE; |
| 3945 | goto exit; |
| 3946 | } |
| 3947 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 3948 | if( operation->lengths_set || operation->ad_started || |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 3949 | operation->body_started ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3950 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3951 | status = PSA_ERROR_BAD_STATE; |
| 3952 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3953 | } |
| 3954 | |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3955 | switch(operation->alg) |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3956 | { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3957 | #if defined(PSA_WANT_ALG_GCM) |
| 3958 | case PSA_ALG_GCM: |
| 3959 | /* Lengths can only be too large for GCM if size_t is bigger than 32 |
| 3960 | * bits. Without the guard this code will generate warnings on 32bit |
| 3961 | * builds. */ |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3962 | #if SIZE_MAX > UINT32_MAX |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3963 | if( (( uint64_t ) ad_length ) >> 61 != 0 || |
| 3964 | (( uint64_t ) plaintext_length ) > 0xFFFFFFFE0ull ) |
| 3965 | { |
| 3966 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3967 | goto exit; |
| 3968 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3969 | #endif |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3970 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3971 | #endif /* PSA_WANT_ALG_GCM */ |
| 3972 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3973 | case PSA_ALG_CCM: |
| 3974 | if( ad_length > 0xFF00 ) |
| 3975 | { |
| 3976 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3977 | goto exit; |
| 3978 | } |
| 3979 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3980 | #endif /* PSA_WANT_ALG_CCM */ |
| 3981 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3982 | case PSA_ALG_CHACHA20_POLY1305: |
| 3983 | /* No length restrictions for ChaChaPoly. */ |
| 3984 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3985 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3986 | default: |
| 3987 | break; |
| 3988 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3989 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3990 | status = psa_driver_wrapper_aead_set_lengths( operation, ad_length, |
| 3991 | plaintext_length ); |
| 3992 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3993 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3994 | if( status == PSA_SUCCESS ) |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 3995 | { |
| 3996 | operation->ad_remaining = ad_length; |
| 3997 | operation->body_remaining = plaintext_length; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3998 | operation->lengths_set = 1; |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 3999 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4000 | else |
| 4001 | psa_aead_abort( operation ); |
| 4002 | |
| 4003 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4004 | } |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 4005 | |
| 4006 | /* Pass additional data to an active multipart AEAD operation. */ |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4007 | psa_status_t psa_aead_update_ad( psa_aead_operation_t *operation, |
| 4008 | const uint8_t *input, |
| 4009 | size_t input_length ) |
| 4010 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4011 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4012 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4013 | if( operation->id == 0 ) |
| 4014 | { |
| 4015 | status = PSA_ERROR_BAD_STATE; |
| 4016 | goto exit; |
| 4017 | } |
| 4018 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4019 | if( !operation->nonce_set || operation->body_started ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4020 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4021 | status = PSA_ERROR_BAD_STATE; |
| 4022 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4023 | } |
| 4024 | |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4025 | if( operation->lengths_set ) |
| 4026 | { |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4027 | if( operation->ad_remaining < input_length ) |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4028 | { |
| 4029 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4030 | goto exit; |
| 4031 | } |
| 4032 | |
| 4033 | operation->ad_remaining -= input_length; |
| 4034 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4035 | #if defined(PSA_WANT_ALG_CCM) |
| 4036 | else if( operation->alg == PSA_ALG_CCM ) |
| 4037 | { |
| 4038 | status = PSA_ERROR_BAD_STATE; |
| 4039 | goto exit; |
| 4040 | } |
| 4041 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4042 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4043 | status = psa_driver_wrapper_aead_update_ad( operation, input, |
| 4044 | input_length ); |
| 4045 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4046 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4047 | if( status == PSA_SUCCESS ) |
| 4048 | operation->ad_started = 1; |
| 4049 | else |
| 4050 | psa_aead_abort( operation ); |
| 4051 | |
| 4052 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4053 | } |
| 4054 | |
| 4055 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 4056 | operation.*/ |
| 4057 | psa_status_t psa_aead_update( psa_aead_operation_t *operation, |
| 4058 | const uint8_t *input, |
| 4059 | size_t input_length, |
| 4060 | uint8_t *output, |
| 4061 | size_t output_size, |
| 4062 | size_t *output_length ) |
| 4063 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4064 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4065 | |
| 4066 | *output_length = 0; |
| 4067 | |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4068 | if( operation->id == 0 ) |
| 4069 | { |
| 4070 | status = PSA_ERROR_BAD_STATE; |
| 4071 | goto exit; |
| 4072 | } |
| 4073 | |
Paul Elliott | 6eb9598 | 2021-05-21 17:41:41 +0100 | [diff] [blame] | 4074 | if( !operation->nonce_set ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4075 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4076 | status = PSA_ERROR_BAD_STATE; |
| 4077 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4078 | } |
| 4079 | |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4080 | if( operation->lengths_set ) |
| 4081 | { |
| 4082 | /* Additional data length was supplied, but not all the additional |
| 4083 | data was supplied.*/ |
| 4084 | if( operation->ad_remaining != 0 ) |
| 4085 | { |
| 4086 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4087 | goto exit; |
| 4088 | } |
| 4089 | |
| 4090 | /* Too much data provided. */ |
| 4091 | if( operation->body_remaining < input_length ) |
| 4092 | { |
| 4093 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4094 | goto exit; |
| 4095 | } |
| 4096 | |
| 4097 | operation->body_remaining -= input_length; |
| 4098 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4099 | #if defined(PSA_WANT_ALG_CCM) |
| 4100 | else if( operation->alg == PSA_ALG_CCM ) |
| 4101 | { |
| 4102 | status = PSA_ERROR_BAD_STATE; |
| 4103 | goto exit; |
| 4104 | } |
| 4105 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4106 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4107 | status = psa_driver_wrapper_aead_update( operation, input, input_length, |
| 4108 | output, output_size, |
| 4109 | output_length ); |
| 4110 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4111 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4112 | if( status == PSA_SUCCESS ) |
| 4113 | operation->body_started = 1; |
| 4114 | else |
| 4115 | psa_aead_abort( operation ); |
| 4116 | |
| 4117 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4118 | } |
| 4119 | |
Paul Elliott | 69bf5fc | 2021-09-19 18:26:37 +0100 | [diff] [blame] | 4120 | 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] | 4121 | { |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4122 | if( operation->id == 0 || !operation->nonce_set ) |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4123 | return( PSA_ERROR_BAD_STATE ); |
| 4124 | |
Paul Elliott | a561444 | 2021-07-14 14:54:11 +0100 | [diff] [blame] | 4125 | if( operation->lengths_set && ( operation->ad_remaining != 0 || |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4126 | operation->body_remaining != 0 ) ) |
| 4127 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4128 | |
| 4129 | return( PSA_SUCCESS ); |
| 4130 | } |
| 4131 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4132 | /* Finish encrypting a message in a multipart AEAD operation. */ |
| 4133 | psa_status_t psa_aead_finish( psa_aead_operation_t *operation, |
| 4134 | uint8_t *ciphertext, |
| 4135 | size_t ciphertext_size, |
| 4136 | size_t *ciphertext_length, |
| 4137 | uint8_t *tag, |
| 4138 | size_t tag_size, |
| 4139 | size_t *tag_length ) |
| 4140 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4141 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4142 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4143 | *ciphertext_length = 0; |
Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 4144 | *tag_length = tag_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4145 | |
Paul Elliott | 814fffb | 2021-08-16 18:20:36 +0100 | [diff] [blame] | 4146 | status = psa_aead_final_checks( operation ); |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4147 | if( status != PSA_SUCCESS ) |
| 4148 | goto exit; |
| 4149 | |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4150 | if( !operation->is_encrypt ) |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4151 | { |
| 4152 | status = PSA_ERROR_BAD_STATE; |
| 4153 | goto exit; |
| 4154 | } |
| 4155 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4156 | status = psa_driver_wrapper_aead_finish( operation, ciphertext, |
| 4157 | ciphertext_size, |
| 4158 | ciphertext_length, |
| 4159 | tag, tag_size, tag_length ); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4160 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4161 | exit: |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4162 | /* 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] | 4163 | * the zero tag size, make sure the tag is set to something implausible. |
| 4164 | * Even if the operation succeeds, make sure we clear the rest of the |
| 4165 | * buffer to prevent potential leakage of anything previously placed in |
| 4166 | * the same buffer.*/ |
Paul Elliott | 90fdc11 | 2021-09-22 17:15:48 +0100 | [diff] [blame] | 4167 | if( tag != NULL ) |
Paul Elliott | ec95cc9 | 2021-09-19 22:33:09 +0100 | [diff] [blame] | 4168 | { |
| 4169 | if( status != PSA_SUCCESS ) |
| 4170 | memset( tag, '!', tag_size ); |
| 4171 | else if( *tag_length < tag_size ) |
| 4172 | memset( tag + *tag_length, '!', ( tag_size - *tag_length ) ); |
| 4173 | } |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4174 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4175 | psa_aead_abort( operation ); |
| 4176 | |
| 4177 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 4181 | operation.*/ |
| 4182 | psa_status_t psa_aead_verify( psa_aead_operation_t *operation, |
| 4183 | uint8_t *plaintext, |
| 4184 | size_t plaintext_size, |
| 4185 | size_t *plaintext_length, |
| 4186 | const uint8_t *tag, |
| 4187 | size_t tag_length ) |
| 4188 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4189 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4190 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4191 | *plaintext_length = 0; |
| 4192 | |
Paul Elliott | 814fffb | 2021-08-16 18:20:36 +0100 | [diff] [blame] | 4193 | status = psa_aead_final_checks( operation ); |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4194 | if( status != PSA_SUCCESS ) |
| 4195 | goto exit; |
| 4196 | |
Paul Elliott | 7f429b7 | 2021-06-24 18:08:54 +0100 | [diff] [blame] | 4197 | if( operation->is_encrypt ) |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4198 | { |
| 4199 | status = PSA_ERROR_BAD_STATE; |
| 4200 | goto exit; |
| 4201 | } |
| 4202 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4203 | status = psa_driver_wrapper_aead_verify( operation, plaintext, |
| 4204 | plaintext_size, |
| 4205 | plaintext_length, |
| 4206 | tag, tag_length ); |
| 4207 | |
| 4208 | exit: |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4209 | psa_aead_abort( operation ); |
| 4210 | |
| 4211 | return( status ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | /* Abort an AEAD operation. */ |
Paul Elliott | bbe90b5 | 2021-05-11 22:22:42 +0100 | [diff] [blame] | 4215 | psa_status_t psa_aead_abort( psa_aead_operation_t *operation ) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4216 | { |
| 4217 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4218 | |
| 4219 | if( operation->id == 0 ) |
| 4220 | { |
| 4221 | /* The object has (apparently) been initialized but it is not (yet) |
| 4222 | * in use. It's ok to call abort on such an object, and there's |
| 4223 | * nothing to do. */ |
| 4224 | return( PSA_SUCCESS ); |
| 4225 | } |
| 4226 | |
| 4227 | status = psa_driver_wrapper_aead_abort( operation ); |
| 4228 | |
Paul Elliott | 70618b2 | 2021-09-22 17:12:16 +0100 | [diff] [blame] | 4229 | memset( operation, 0, sizeof( *operation ) ); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4230 | |
| 4231 | return( status ); |
| 4232 | } |
| 4233 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4234 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4235 | /* Generators */ |
| 4236 | /****************************************************************/ |
| 4237 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4238 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4239 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4240 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \ |
| 4241 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4242 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4243 | #endif /* At least one builtin KDF */ |
| 4244 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4245 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4246 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4247 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4248 | static psa_status_t psa_key_derivation_start_hmac( |
| 4249 | psa_mac_operation_t *operation, |
| 4250 | psa_algorithm_t hash_alg, |
| 4251 | const uint8_t *hmac_key, |
| 4252 | size_t hmac_key_length ) |
| 4253 | { |
| 4254 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4255 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4256 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 4257 | psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) ); |
| 4258 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 4259 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4260 | operation->is_sign = 1; |
| 4261 | operation->mac_size = PSA_HASH_LENGTH( hash_alg ); |
| 4262 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4263 | status = psa_driver_wrapper_mac_sign_setup( operation, |
| 4264 | &attributes, |
| 4265 | hmac_key, hmac_key_length, |
| 4266 | PSA_ALG_HMAC( hash_alg ) ); |
| 4267 | |
| 4268 | psa_reset_key_attributes( &attributes ); |
| 4269 | return( status ); |
| 4270 | } |
| 4271 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4272 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4273 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4274 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4275 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4276 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4277 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4278 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4279 | const psa_key_derivation_operation_t *operation ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4280 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4281 | if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
| 4282 | return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4283 | else |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4284 | return( operation->alg ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4285 | } |
| 4286 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4287 | 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] | 4288 | { |
| 4289 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4290 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4291 | if( kdf_alg == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4292 | { |
| 4293 | /* The object has (apparently) been initialized but it is not |
| 4294 | * in use. It's ok to call abort on such an object, and there's |
| 4295 | * nothing to do. */ |
| 4296 | } |
| 4297 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4298 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 4299 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4300 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4301 | mbedtls_free( operation->ctx.hkdf.info ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4302 | status = psa_mac_abort( &operation->ctx.hkdf.hmac ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4303 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4304 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4305 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4306 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4307 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4308 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4309 | /* 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] | 4310 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4311 | { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4312 | if( operation->ctx.tls12_prf.secret != NULL ) |
| 4313 | { |
| 4314 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret, |
| 4315 | operation->ctx.tls12_prf.secret_length ); |
| 4316 | mbedtls_free( operation->ctx.tls12_prf.secret ); |
| 4317 | } |
| 4318 | |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4319 | if( operation->ctx.tls12_prf.seed != NULL ) |
| 4320 | { |
| 4321 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed, |
| 4322 | operation->ctx.tls12_prf.seed_length ); |
| 4323 | mbedtls_free( operation->ctx.tls12_prf.seed ); |
| 4324 | } |
| 4325 | |
| 4326 | if( operation->ctx.tls12_prf.label != NULL ) |
| 4327 | { |
| 4328 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.label, |
| 4329 | operation->ctx.tls12_prf.label_length ); |
| 4330 | mbedtls_free( operation->ctx.tls12_prf.label ); |
| 4331 | } |
| 4332 | |
Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 4333 | if( operation->ctx.tls12_prf.other_secret != NULL ) |
| 4334 | { |
| 4335 | mbedtls_platform_zeroize( operation->ctx.tls12_prf.other_secret, |
| 4336 | operation->ctx.tls12_prf.other_secret_length ); |
| 4337 | mbedtls_free( operation->ctx.tls12_prf.other_secret ); |
| 4338 | } |
| 4339 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4340 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4341 | |
| 4342 | /* We leave the fields Ai and output_block to be erased safely by the |
| 4343 | * mbedtls_platform_zeroize() in the end of this function. */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4344 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4345 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4346 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4347 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4348 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 4349 | if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4350 | { |
| 4351 | mbedtls_platform_zeroize( operation->ctx.tls12_ecjpake_to_pms.data, |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 4352 | sizeof( operation->ctx.tls12_ecjpake_to_pms.data ) ); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4353 | } |
| 4354 | else |
| 4355 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4356 | { |
| 4357 | status = PSA_ERROR_BAD_STATE; |
| 4358 | } |
Janos Follath | 083036a | 2019-06-11 10:22:26 +0100 | [diff] [blame] | 4359 | mbedtls_platform_zeroize( operation, sizeof( *operation ) ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4360 | return( status ); |
| 4361 | } |
| 4362 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4363 | 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] | 4364 | size_t *capacity) |
| 4365 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4366 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4367 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4368 | /* This is a blank key derivation operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4369 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4372 | *capacity = operation->capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4373 | return( PSA_SUCCESS ); |
| 4374 | } |
| 4375 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4376 | 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] | 4377 | size_t capacity ) |
| 4378 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4379 | if( operation->alg == 0 ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4380 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4381 | if( capacity > operation->capacity ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4382 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4383 | operation->capacity = capacity; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4384 | return( PSA_SUCCESS ); |
| 4385 | } |
| 4386 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4387 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4388 | /* Read some bytes from an HKDF-based operation. */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4389 | 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] | 4390 | psa_algorithm_t kdf_alg, |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4391 | uint8_t *output, |
| 4392 | size_t output_length ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4393 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4394 | 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] | 4395 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4396 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4397 | psa_status_t status; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4398 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4399 | 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] | 4400 | #else |
| 4401 | const uint8_t last_block = 0xff; |
| 4402 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4403 | |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4404 | if( hkdf->state < HKDF_STATE_KEYED || |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4405 | ( !hkdf->info_set |
| 4406 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 4407 | && !PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) |
| 4408 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
| 4409 | ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4410 | return( PSA_ERROR_BAD_STATE ); |
| 4411 | hkdf->state = HKDF_STATE_OUTPUT; |
| 4412 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4413 | while( output_length != 0 ) |
| 4414 | { |
| 4415 | /* Copy what remains of the current block */ |
| 4416 | uint8_t n = hash_length - hkdf->offset_in_block; |
| 4417 | if( n > output_length ) |
| 4418 | n = (uint8_t) output_length; |
| 4419 | memcpy( output, hkdf->output_block + hkdf->offset_in_block, n ); |
| 4420 | output += n; |
| 4421 | output_length -= n; |
| 4422 | hkdf->offset_in_block += n; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4423 | if( output_length == 0 ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4424 | break; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4425 | /* We can't be wanting more output after the last block, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4426 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4427 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4428 | * object was corrupted or if this function is called directly |
| 4429 | * inside the library. */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4430 | if( hkdf->block_number == last_block ) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4431 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4432 | |
| 4433 | /* We need a new block */ |
| 4434 | ++hkdf->block_number; |
| 4435 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4436 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4437 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 4438 | hash_alg, |
| 4439 | hkdf->prk, |
| 4440 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4441 | if( status != PSA_SUCCESS ) |
| 4442 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4443 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4444 | if( hkdf->block_number != 1 ) |
| 4445 | { |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4446 | status = psa_mac_update( &hkdf->hmac, |
| 4447 | hkdf->output_block, |
| 4448 | hash_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4449 | if( status != PSA_SUCCESS ) |
| 4450 | return( status ); |
| 4451 | } |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4452 | status = psa_mac_update( &hkdf->hmac, |
| 4453 | hkdf->info, |
| 4454 | hkdf->info_length ); |
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_update( &hkdf->hmac, |
| 4458 | &hkdf->block_number, 1 ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4459 | if( status != PSA_SUCCESS ) |
| 4460 | return( status ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4461 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 4462 | hkdf->output_block, |
| 4463 | sizeof( hkdf->output_block ), |
| 4464 | &hmac_output_length ); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4465 | if( status != PSA_SUCCESS ) |
| 4466 | return( status ); |
| 4467 | } |
| 4468 | |
| 4469 | return( PSA_SUCCESS ); |
| 4470 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4471 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4472 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4473 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4474 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4475 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 4476 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4477 | psa_algorithm_t alg ) |
| 4478 | { |
| 4479 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 4480 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4481 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 4482 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4483 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4484 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4485 | /* We can't be wanting more output after block 0xff, otherwise |
| 4486 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4487 | * prevented this call. It could happen only if the operation |
| 4488 | * object was corrupted or if this function is called directly |
| 4489 | * inside the library. */ |
| 4490 | if( tls12_prf->block_number == 0xff ) |
Janos Follath | 30090bc | 2019-06-25 10:15:04 +0100 | [diff] [blame] | 4491 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4492 | |
| 4493 | /* We need a new block */ |
| 4494 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4495 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4496 | |
| 4497 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4498 | * |
| 4499 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4500 | * |
| 4501 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4502 | * HMAC_hash(secret, A(2) + seed) + |
| 4503 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4504 | * |
| 4505 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4506 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4507 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4508 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4509 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4510 | * is currently extracted as `output_block` and where i is |
| 4511 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4512 | */ |
| 4513 | |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4514 | status = psa_key_derivation_start_hmac( &hmac, |
| 4515 | hash_alg, |
| 4516 | tls12_prf->secret, |
| 4517 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4518 | if( status != PSA_SUCCESS ) |
| 4519 | goto cleanup; |
| 4520 | |
| 4521 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
| 4522 | if( tls12_prf->block_number == 1 ) |
| 4523 | { |
| 4524 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4525 | * the variable seed and in this instance means it in the context of the |
| 4526 | * P_hash function, where seed = label + seed.) */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4527 | status = psa_mac_update( &hmac, |
| 4528 | tls12_prf->label, |
| 4529 | tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4530 | if( status != PSA_SUCCESS ) |
| 4531 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4532 | status = psa_mac_update( &hmac, |
| 4533 | tls12_prf->seed, |
| 4534 | tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4535 | if( status != PSA_SUCCESS ) |
| 4536 | goto cleanup; |
| 4537 | } |
| 4538 | else |
| 4539 | { |
| 4540 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4541 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4542 | if( status != PSA_SUCCESS ) |
| 4543 | goto cleanup; |
| 4544 | } |
| 4545 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4546 | status = psa_mac_sign_finish( &hmac, |
| 4547 | tls12_prf->Ai, hash_length, |
| 4548 | &hmac_output_length ); |
| 4549 | if( hmac_output_length != hash_length ) |
| 4550 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4551 | if( status != PSA_SUCCESS ) |
| 4552 | goto cleanup; |
| 4553 | |
| 4554 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4555 | status = psa_key_derivation_start_hmac( &hmac, |
| 4556 | hash_alg, |
| 4557 | tls12_prf->secret, |
| 4558 | tls12_prf->secret_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4559 | if( status != PSA_SUCCESS ) |
| 4560 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4561 | status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4562 | if( status != PSA_SUCCESS ) |
| 4563 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4564 | status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4565 | if( status != PSA_SUCCESS ) |
| 4566 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4567 | status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4568 | if( status != PSA_SUCCESS ) |
| 4569 | goto cleanup; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4570 | status = psa_mac_sign_finish( &hmac, |
| 4571 | tls12_prf->output_block, hash_length, |
| 4572 | &hmac_output_length ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4573 | if( status != PSA_SUCCESS ) |
| 4574 | goto cleanup; |
| 4575 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4576 | |
| 4577 | cleanup: |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4578 | cleanup_status = psa_mac_abort( &hmac ); |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4579 | if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS ) |
| 4580 | status = cleanup_status; |
| 4581 | |
| 4582 | return( status ); |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4583 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4584 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4585 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4586 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4587 | psa_algorithm_t alg, |
| 4588 | uint8_t *output, |
| 4589 | size_t output_length ) |
| 4590 | { |
| 4591 | 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] | 4592 | uint8_t hash_length = PSA_HASH_LENGTH( hash_alg ); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4593 | psa_status_t status; |
| 4594 | uint8_t offset, length; |
| 4595 | |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4596 | switch( tls12_prf->state ) |
| 4597 | { |
| 4598 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4599 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4600 | break; |
| 4601 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4602 | break; |
| 4603 | default: |
| 4604 | return( PSA_ERROR_BAD_STATE ); |
| 4605 | } |
| 4606 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4607 | while( output_length != 0 ) |
| 4608 | { |
| 4609 | /* Check if we have fully processed the current block. */ |
| 4610 | if( tls12_prf->left_in_block == 0 ) |
| 4611 | { |
| 4612 | status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf, |
| 4613 | alg ); |
| 4614 | if( status != PSA_SUCCESS ) |
| 4615 | return( status ); |
| 4616 | |
| 4617 | continue; |
| 4618 | } |
| 4619 | |
| 4620 | if( tls12_prf->left_in_block > output_length ) |
| 4621 | length = (uint8_t) output_length; |
| 4622 | else |
| 4623 | length = tls12_prf->left_in_block; |
| 4624 | |
| 4625 | offset = hash_length - tls12_prf->left_in_block; |
| 4626 | memcpy( output, tls12_prf->output_block + offset, length ); |
| 4627 | output += length; |
| 4628 | output_length -= length; |
| 4629 | tls12_prf->left_in_block -= length; |
| 4630 | } |
| 4631 | |
| 4632 | return( PSA_SUCCESS ); |
| 4633 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4634 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4635 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4636 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4637 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 4638 | static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 4639 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
| 4640 | uint8_t *output, |
| 4641 | size_t output_length ) |
| 4642 | { |
| 4643 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 4644 | size_t output_size = 0; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4645 | |
| 4646 | if( output_length != 32 ) |
| 4647 | return ( PSA_ERROR_INVALID_ARGUMENT ); |
| 4648 | |
| 4649 | status = psa_hash_compute( PSA_ALG_SHA_256, ecjpake->data, |
| 4650 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length, |
| 4651 | &output_size ); |
| 4652 | if( status != PSA_SUCCESS ) |
| 4653 | return ( status ); |
| 4654 | |
| 4655 | if( output_size != output_length ) |
| 4656 | return ( PSA_ERROR_GENERIC_ERROR ); |
| 4657 | |
| 4658 | return ( PSA_SUCCESS ); |
| 4659 | } |
| 4660 | #endif |
| 4661 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4662 | psa_status_t psa_key_derivation_output_bytes( |
| 4663 | psa_key_derivation_operation_t *operation, |
| 4664 | uint8_t *output, |
| 4665 | size_t output_length ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4666 | { |
| 4667 | psa_status_t status; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4668 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4669 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4670 | if( operation->alg == 0 ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4671 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4672 | /* This is a blank operation. */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4673 | return( PSA_ERROR_BAD_STATE ); |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4674 | } |
| 4675 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4676 | if( output_length > operation->capacity ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4677 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4678 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4679 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4680 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4681 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4682 | goto exit; |
| 4683 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4684 | if( output_length == 0 && operation->capacity == 0 ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4685 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4686 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4687 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4688 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4689 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4690 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4691 | * output_length > 0. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4692 | return( PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4693 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4694 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4695 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4696 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 4697 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4698 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 4699 | status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, kdf_alg, |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4700 | output, output_length ); |
| 4701 | } |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4702 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4703 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4704 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4705 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | adbec81 | 2019-06-14 11:05:39 +0100 | [diff] [blame] | 4706 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4707 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4708 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4709 | status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf, |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4710 | kdf_alg, output, |
| 4711 | output_length ); |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4712 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4713 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4714 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4715 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4716 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 4717 | if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4718 | { |
| 4719 | status = psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 4720 | &operation->ctx.tls12_ecjpake_to_pms, output, output_length ); |
| 4721 | } |
| 4722 | else |
| 4723 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
| 4724 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4725 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4726 | (void) kdf_alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4727 | return( PSA_ERROR_BAD_STATE ); |
| 4728 | } |
| 4729 | |
| 4730 | exit: |
| 4731 | if( status != PSA_SUCCESS ) |
| 4732 | { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4733 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4734 | * This allows us to differentiate between exhausted operations and |
| 4735 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4736 | * operations. */ |
| 4737 | psa_algorithm_t alg = operation->alg; |
| 4738 | psa_key_derivation_abort( operation ); |
| 4739 | operation->alg = alg; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4740 | memset( output, '!', output_length ); |
| 4741 | } |
| 4742 | return( status ); |
| 4743 | } |
| 4744 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4745 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4746 | static void psa_des_set_key_parity( uint8_t *data, size_t data_size ) |
| 4747 | { |
| 4748 | if( data_size >= 8 ) |
| 4749 | mbedtls_des_key_set_parity( data ); |
| 4750 | if( data_size >= 16 ) |
| 4751 | mbedtls_des_key_set_parity( data + 8 ); |
| 4752 | if( data_size >= 24 ) |
| 4753 | mbedtls_des_key_set_parity( data + 16 ); |
| 4754 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4755 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4756 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4757 | /* |
Przemyslaw Stekiel | 705fb0f | 2021-11-24 08:47:29 +0100 | [diff] [blame] | 4758 | * ECC keys on a Weierstrass elliptic curve require the generation |
| 4759 | * of a private key which is an integer |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4760 | * in the range [1, N - 1], where N is the boundary of the private key domain: |
| 4761 | * N is the prime p for Diffie-Hellman, or the order of the |
| 4762 | * curve’s base point for ECC. |
| 4763 | * |
| 4764 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). |
| 4765 | * This function generates the private key using the following process: |
| 4766 | * |
| 4767 | * 1. Draw a byte string of length ceiling(m/8) bytes. |
| 4768 | * 2. If m is not a multiple of 8, set the most significant |
| 4769 | * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. |
| 4770 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. |
| 4771 | * 4. If k > N - 2, discard the result and return to step 1. |
| 4772 | * 5. Output k + 1 as the private key. |
| 4773 | * |
| 4774 | * This method allows compliance to NIST standards, specifically the methods titled |
| 4775 | * Key-Pair Generation by Testing Candidates in the following publications: |
| 4776 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment |
| 4777 | * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for |
| 4778 | * Diffie-Hellman keys. |
| 4779 | * |
| 4780 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature |
| 4781 | * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4782 | * |
| 4783 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 4784 | * always NULL. |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4785 | */ |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4786 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 4787 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 4788 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4789 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 4790 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4791 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4792 | psa_key_slot_t *slot, |
| 4793 | size_t bits, |
| 4794 | psa_key_derivation_operation_t *operation, |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4795 | uint8_t **data |
| 4796 | ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4797 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4798 | unsigned key_out_of_range = 1; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4799 | mbedtls_mpi k; |
| 4800 | mbedtls_mpi diff_N_2; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4801 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4802 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4803 | |
| 4804 | mbedtls_mpi_init( &k ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4805 | mbedtls_mpi_init( &diff_N_2 ); |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4806 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4807 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
| 4808 | slot->attr.type ); |
| 4809 | mbedtls_ecp_group_id grp_id = |
| 4810 | mbedtls_ecc_group_of_psa( curve, bits, 0 ); |
| 4811 | |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 4812 | if( grp_id == MBEDTLS_ECP_DP_NONE ) |
| 4813 | { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4814 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 4815 | goto cleanup; |
| 4816 | } |
| 4817 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4818 | mbedtls_ecp_group ecp_group; |
Przemyslaw Stekiel | 6534816 | 2021-11-18 11:57:07 +0100 | [diff] [blame] | 4819 | mbedtls_ecp_group_init( &ecp_group ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4820 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4821 | MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ecp_group, grp_id ) ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4822 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4823 | /* N is the boundary of the private key domain (ecp_group.N). */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4824 | /* Let m be the bit size of N. */ |
| 4825 | size_t m = ecp_group.nbits; |
| 4826 | |
| 4827 | size_t m_bytes = PSA_BITS_TO_BYTES( m ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4828 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4829 | /* Calculate N - 2 - it will be needed later. */ |
| 4830 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &diff_N_2, &ecp_group.N, 2 ) ); |
| 4831 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4832 | /* Note: This function is always called with *data == NULL and it |
| 4833 | * allocates memory for the data buffer. */ |
| 4834 | *data = mbedtls_calloc( 1, m_bytes ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4835 | if( *data == NULL ) |
| 4836 | { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4837 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4838 | goto cleanup; |
| 4839 | } |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4840 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4841 | while( key_out_of_range ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4842 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4843 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4844 | if( ( status = psa_key_derivation_output_bytes( operation, *data, m_bytes ) ) != 0 ) |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4845 | goto cleanup; |
| 4846 | |
| 4847 | /* 2. If m is not a multiple of 8 */ |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4848 | if( m % 8 != 0 ) |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4849 | { |
| 4850 | /* Set the most significant |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4851 | * (8 * ceiling(m/8) - m) bits of the first byte in |
| 4852 | * the string to zero. |
| 4853 | */ |
| 4854 | uint8_t clear_bit_mask = ( 1 << ( m % 8 ) ) - 1; |
| 4855 | (*data)[0] &= clear_bit_mask; |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | /* 3. Convert the string to integer k by decoding it as a |
| 4859 | * big-endian byte string. |
| 4860 | */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4861 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &k, *data, m_bytes ) ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4862 | |
| 4863 | /* 4. If k > N - 2, discard the result and return to step 1. |
| 4864 | * Result of comparison is returned. When it indicates error |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 4865 | * then this function is called again. |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4866 | */ |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4867 | 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] | 4868 | } |
| 4869 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4870 | /* 5. Output k + 1 as the private key. */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4871 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &k, &k, 1 ) ); |
| 4872 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &k, *data, m_bytes ) ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4873 | cleanup: |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4874 | if( ret != 0 ) |
Przemyslaw Stekiel | dc8d7d9 | 2021-12-02 09:15:20 +0100 | [diff] [blame] | 4875 | status = mbedtls_to_psa_error( ret ); |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4876 | if( status != PSA_SUCCESS ) { |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4877 | mbedtls_free( *data ); |
| 4878 | *data = NULL; |
| 4879 | } |
| 4880 | mbedtls_mpi_free( &k ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4881 | mbedtls_mpi_free( &diff_N_2 ); |
Przemyslaw Stekiel | dc8d7d9 | 2021-12-02 09:15:20 +0100 | [diff] [blame] | 4882 | return( status ); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4883 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4884 | |
| 4885 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length |
| 4886 | * is determined by the curve, and sets the mandatory bits accordingly. That is: |
| 4887 | * |
| 4888 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): |
| 4889 | * draw a 32-byte string and process it as specified in |
| 4890 | * Elliptic Curves for Security [RFC7748] §5. |
| 4891 | * |
| 4892 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): |
| 4893 | * draw a 56-byte string and process it as specified in [RFC7748] §5. |
| 4894 | * |
| 4895 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 4896 | * always NULL. |
| 4897 | */ |
| 4898 | |
| 4899 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 4900 | size_t bits, |
| 4901 | psa_key_derivation_operation_t *operation, |
| 4902 | uint8_t **data |
| 4903 | ) |
| 4904 | { |
| 4905 | size_t output_length; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4906 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4907 | |
| 4908 | switch( bits ) |
| 4909 | { |
| 4910 | case 255: |
| 4911 | output_length = 32; |
| 4912 | break; |
| 4913 | case 448: |
| 4914 | output_length = 56; |
| 4915 | break; |
| 4916 | default: |
| 4917 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 4918 | break; |
| 4919 | } |
| 4920 | |
| 4921 | *data = mbedtls_calloc( 1, output_length ); |
| 4922 | |
| 4923 | if( *data == NULL ) |
| 4924 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4925 | |
| 4926 | status = psa_key_derivation_output_bytes( operation, *data, output_length ); |
| 4927 | |
| 4928 | if( status != PSA_SUCCESS ) |
| 4929 | return status; |
| 4930 | |
| 4931 | switch( bits ) |
| 4932 | { |
| 4933 | case 255: |
| 4934 | (*data)[0] &= 248; |
| 4935 | (*data)[31] &= 127; |
| 4936 | (*data)[31] |= 64; |
| 4937 | break; |
| 4938 | case 448: |
| 4939 | (*data)[0] &= 252; |
| 4940 | (*data)[55] |= 128; |
| 4941 | break; |
| 4942 | default: |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4943 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4944 | break; |
| 4945 | } |
| 4946 | |
| 4947 | return status; |
| 4948 | } |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4949 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 4950 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 4951 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4952 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 4953 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4954 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4955 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4956 | psa_key_slot_t *slot, |
| 4957 | size_t bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4958 | psa_key_derivation_operation_t *operation ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4959 | { |
| 4960 | uint8_t *data = NULL; |
| 4961 | size_t bytes = PSA_BITS_TO_BYTES( bits ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4962 | size_t storage_size = bytes; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4963 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4964 | |
Przemek Stekiel | dcab6cc | 2022-03-01 14:22:29 +0100 | [diff] [blame] | 4965 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4966 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4967 | |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4968 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 4969 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 4970 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 4971 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 4972 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 4973 | if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) ) |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4974 | { |
Przemyslaw Stekiel | 9248159 | 2022-01-04 10:09:46 +0100 | [diff] [blame] | 4975 | 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] | 4976 | if( PSA_ECC_FAMILY_IS_WEIERSTRASS( curve ) ) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4977 | { |
| 4978 | /* Weierstrass elliptic curve */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4979 | status = psa_generate_derived_ecc_key_weierstrass_helper( slot, bits, operation, &data ); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4980 | if( status != PSA_SUCCESS ) |
| 4981 | goto exit; |
Przemyslaw Stekiel | 50fcc53 | 2021-11-26 10:54:52 +0100 | [diff] [blame] | 4982 | } |
| 4983 | else |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4984 | { |
| 4985 | /* Montgomery elliptic curve */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4986 | status = psa_generate_derived_ecc_key_montgomery_helper( bits, operation, &data ); |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4987 | if( status != PSA_SUCCESS ) |
| 4988 | goto exit; |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4989 | } |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4990 | } else |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4991 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 4992 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 4993 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 4994 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 4995 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4996 | if( key_type_is_raw_bytes( slot->attr.type ) ) |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4997 | { |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4998 | if( bits % 8 != 0 ) |
| 4999 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5000 | data = mbedtls_calloc( 1, bytes ); |
| 5001 | if( data == NULL ) |
| 5002 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5003 | |
| 5004 | status = psa_key_derivation_output_bytes( operation, data, bytes ); |
| 5005 | if( status != PSA_SUCCESS ) |
| 5006 | goto exit; |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5007 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5008 | if( slot->attr.type == PSA_KEY_TYPE_DES ) |
| 5009 | psa_des_set_key_parity( data, bytes ); |
Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 5010 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5011 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5012 | else |
Przemek Stekiel | dcab6cc | 2022-03-01 14:22:29 +0100 | [diff] [blame] | 5013 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5014 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5015 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5016 | psa_key_attributes_t attributes = { |
| 5017 | .core = slot->attr |
| 5018 | }; |
| 5019 | |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5020 | if( psa_key_lifetime_is_external( attributes.core.lifetime ) ) |
| 5021 | { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 5022 | status = psa_driver_wrapper_get_key_buffer_size( &attributes, |
| 5023 | &storage_size ); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5024 | if( status != PSA_SUCCESS ) |
| 5025 | goto exit; |
| 5026 | } |
| 5027 | status = psa_allocate_buffer_to_slot( slot, storage_size ); |
| 5028 | if( status != PSA_SUCCESS ) |
| 5029 | goto exit; |
| 5030 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5031 | status = psa_driver_wrapper_import_key( &attributes, |
| 5032 | data, bytes, |
| 5033 | slot->key.data, |
| 5034 | slot->key.bytes, |
| 5035 | &slot->key.bytes, &bits ); |
| 5036 | if( bits != slot->attr.bits ) |
| 5037 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5038 | |
| 5039 | exit: |
| 5040 | mbedtls_free( data ); |
| 5041 | return( status ); |
| 5042 | } |
| 5043 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5044 | 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] | 5045 | psa_key_derivation_operation_t *operation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5046 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5047 | { |
| 5048 | psa_status_t status; |
| 5049 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5050 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5051 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5052 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5053 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5054 | /* Reject any attempt to create a zero-length key so that we don't |
| 5055 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 5056 | if( psa_get_key_bits( attributes ) == 0 ) |
| 5057 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5058 | |
Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 5059 | if( operation->alg == PSA_ALG_NONE ) |
| 5060 | return( PSA_ERROR_BAD_STATE ); |
| 5061 | |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5062 | if( ! operation->can_output_key ) |
| 5063 | return( PSA_ERROR_NOT_PERMITTED ); |
| 5064 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5065 | status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes, |
| 5066 | &slot, &driver ); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5067 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 5068 | if( driver != NULL ) |
| 5069 | { |
| 5070 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5071 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5072 | } |
| 5073 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5074 | if( status == PSA_SUCCESS ) |
| 5075 | { |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5076 | status = psa_generate_derived_key_internal( slot, |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 5077 | attributes->core.bits, |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5078 | operation ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5079 | } |
| 5080 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5081 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5082 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 5083 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5084 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5085 | return( status ); |
| 5086 | } |
| 5087 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5088 | |
| 5089 | |
| 5090 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5091 | /* Key derivation */ |
| 5092 | /****************************************************************/ |
| 5093 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5094 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5095 | static int is_kdf_alg_supported( psa_algorithm_t kdf_alg ) |
| 5096 | { |
| 5097 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 5098 | if( PSA_ALG_IS_HKDF( kdf_alg ) ) |
| 5099 | return( 1 ); |
| 5100 | #endif |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5101 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 5102 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5103 | return( 1 ); |
| 5104 | #endif |
| 5105 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
| 5106 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5107 | return( 1 ); |
| 5108 | #endif |
| 5109 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5110 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
| 5111 | return( 1 ); |
| 5112 | #endif |
| 5113 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5114 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
| 5115 | return( 1 ); |
| 5116 | #endif |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5117 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 5118 | if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5119 | return( 1 ); |
| 5120 | #endif |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5121 | return( 0 ); |
| 5122 | } |
| 5123 | |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5124 | static psa_status_t psa_hash_try_support( psa_algorithm_t alg ) |
| 5125 | { |
| 5126 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 5127 | psa_status_t status = psa_hash_setup( &operation, alg ); |
| 5128 | psa_hash_abort( &operation ); |
| 5129 | return( status ); |
| 5130 | } |
| 5131 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5132 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5133 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5134 | psa_algorithm_t kdf_alg ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5135 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5136 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5137 | * initialisers for this union leave some bytes unspecified.) */ |
| 5138 | memset( &operation->ctx, 0, sizeof( operation->ctx ) ); |
| 5139 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5140 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5141 | if( ! is_kdf_alg_supported( kdf_alg ) ) |
| 5142 | return( PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5143 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5144 | /* All currently supported key derivation algorithms (apart from |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5145 | * ecjpake to pms) are based on a hash algorithm. */ |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5146 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
| 5147 | size_t hash_size = PSA_HASH_LENGTH( hash_alg ); |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 5148 | if( kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5149 | { |
| 5150 | if( hash_size == 0 ) |
| 5151 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5152 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5153 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 5154 | * we might fail later, which is somewhat unfriendly and potentially |
| 5155 | * risk-prone. */ |
| 5156 | psa_status_t status = psa_hash_try_support( hash_alg ); |
| 5157 | if( status != PSA_SUCCESS ) |
| 5158 | return( status ); |
| 5159 | } |
| 5160 | else |
| 5161 | { |
| 5162 | hash_size = PSA_HASH_LENGTH( PSA_ALG_SHA_256 ); |
| 5163 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5164 | |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5165 | if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) || |
| 5166 | PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) && |
| 5167 | ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5168 | { |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5169 | return( PSA_ERROR_NOT_SUPPORTED ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5170 | } |
Andrzej Kurek | 7763829 | 2022-09-16 12:24:52 -0400 | [diff] [blame] | 5171 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5172 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Andrzej Kurek | b093650 | 2022-09-16 07:13:00 -0400 | [diff] [blame] | 5173 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) || |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 5174 | ( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) ) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5175 | operation->capacity = hash_size; |
| 5176 | else |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5177 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT || |
| 5178 | MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5179 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5180 | return( PSA_SUCCESS ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5181 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5182 | |
| 5183 | static psa_status_t psa_key_agreement_try_support( psa_algorithm_t alg ) |
| 5184 | { |
| 5185 | #if defined(PSA_WANT_ALG_ECDH) |
| 5186 | if( alg == PSA_ALG_ECDH ) |
| 5187 | return( PSA_SUCCESS ); |
| 5188 | #endif |
| 5189 | (void) alg; |
| 5190 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5191 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5192 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5193 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5194 | 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] | 5195 | psa_algorithm_t alg ) |
| 5196 | { |
| 5197 | psa_status_t status; |
| 5198 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5199 | if( operation->alg != 0 ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5200 | return( PSA_ERROR_BAD_STATE ); |
| 5201 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 5202 | if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
| 5203 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5204 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5205 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5206 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5207 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ); |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5208 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ); |
| 5209 | status = psa_key_agreement_try_support( ka_alg ); |
| 5210 | if( status != PSA_SUCCESS ) |
| 5211 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5212 | status = psa_key_derivation_setup_kdf( operation, kdf_alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5213 | #else |
| 5214 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5215 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5216 | } |
| 5217 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
| 5218 | { |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5219 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5220 | status = psa_key_derivation_setup_kdf( operation, alg ); |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5221 | #else |
| 5222 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5223 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5224 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5225 | else |
| 5226 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5227 | |
| 5228 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5229 | operation->alg = alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5230 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5231 | } |
| 5232 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5233 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5234 | 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] | 5235 | psa_algorithm_t kdf_alg, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5236 | psa_key_derivation_step_t step, |
| 5237 | const uint8_t *data, |
| 5238 | size_t data_length ) |
| 5239 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5240 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5241 | psa_status_t status; |
| 5242 | switch( step ) |
| 5243 | { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5244 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5245 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5246 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
| 5247 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5248 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5249 | if( hkdf->state != HKDF_STATE_INIT ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5250 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5251 | else |
| 5252 | { |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5253 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 5254 | hash_alg, |
| 5255 | data, data_length ); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5256 | if( status != PSA_SUCCESS ) |
| 5257 | return( status ); |
| 5258 | hkdf->state = HKDF_STATE_STARTED; |
| 5259 | return( PSA_SUCCESS ); |
| 5260 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5261 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5262 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5263 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5264 | { |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 5265 | /* We shouldn't be in different state as HKDF_EXPAND only allows |
| 5266 | * two inputs: SECRET (this case) and INFO which does not modify |
| 5267 | * the state. It could happen only if the hkdf |
| 5268 | * object was corrupted. */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5269 | if( hkdf->state != HKDF_STATE_INIT ) |
| 5270 | return( PSA_ERROR_BAD_STATE ); |
| 5271 | |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 5272 | /* Allow only input that fits expected prk size */ |
| 5273 | if( data_length != PSA_HASH_LENGTH( hash_alg ) ) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5274 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5275 | |
| 5276 | memcpy( hkdf->prk, data, data_length ); |
| 5277 | } |
| 5278 | else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5279 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5280 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5281 | /* HKDF: If no salt was provided, use an empty salt. |
| 5282 | * HKDF-EXTRACT: salt is mandatory. */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5283 | if( hkdf->state == HKDF_STATE_INIT ) |
| 5284 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5285 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
| 5286 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5287 | return( PSA_ERROR_BAD_STATE ); |
| 5288 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5289 | status = psa_key_derivation_start_hmac( &hkdf->hmac, |
| 5290 | hash_alg, |
| 5291 | NULL, 0 ); |
| 5292 | if( status != PSA_SUCCESS ) |
| 5293 | return( status ); |
| 5294 | hkdf->state = HKDF_STATE_STARTED; |
| 5295 | } |
| 5296 | if( hkdf->state != HKDF_STATE_STARTED ) |
| 5297 | return( PSA_ERROR_BAD_STATE ); |
| 5298 | status = psa_mac_update( &hkdf->hmac, |
| 5299 | data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5300 | if( status != PSA_SUCCESS ) |
| 5301 | return( status ); |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5302 | status = psa_mac_sign_finish( &hkdf->hmac, |
| 5303 | hkdf->prk, |
| 5304 | sizeof( hkdf->prk ), |
| 5305 | &data_length ); |
| 5306 | if( status != PSA_SUCCESS ) |
| 5307 | return( status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5308 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5309 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5310 | hkdf->state = HKDF_STATE_KEYED; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5311 | hkdf->block_number = 0; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5312 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5313 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5314 | { |
| 5315 | /* The only block of output is the PRK. */ |
| 5316 | memcpy( hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH( hash_alg ) ); |
| 5317 | hkdf->offset_in_block = 0; |
| 5318 | } |
| 5319 | else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5320 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5321 | { |
| 5322 | /* Block 0 is empty, and the next block will be |
| 5323 | * generated by psa_key_derivation_hkdf_read(). */ |
| 5324 | hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg ); |
| 5325 | } |
| 5326 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5327 | return( PSA_SUCCESS ); |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5328 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5329 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5330 | if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) |
| 5331 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5332 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 5333 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
| 5334 | if( PSA_ALG_IS_HKDF_EXPAND( kdf_alg ) && |
| 5335 | hkdf->state == HKDF_STATE_INIT ) |
| 5336 | return( PSA_ERROR_BAD_STATE ); |
| 5337 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5338 | if( hkdf->state == HKDF_STATE_OUTPUT ) |
| 5339 | return( PSA_ERROR_BAD_STATE ); |
| 5340 | if( hkdf->info_set ) |
| 5341 | return( PSA_ERROR_BAD_STATE ); |
| 5342 | hkdf->info_length = data_length; |
| 5343 | if( data_length != 0 ) |
| 5344 | { |
| 5345 | hkdf->info = mbedtls_calloc( 1, data_length ); |
| 5346 | if( hkdf->info == NULL ) |
| 5347 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5348 | memcpy( hkdf->info, data, data_length ); |
| 5349 | } |
| 5350 | hkdf->info_set = 1; |
| 5351 | return( PSA_SUCCESS ); |
| 5352 | default: |
| 5353 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5354 | } |
| 5355 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5356 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5357 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5358 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5359 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5360 | static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf, |
| 5361 | const uint8_t *data, |
| 5362 | size_t data_length ) |
| 5363 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5364 | if( prf->state != PSA_TLS12_PRF_STATE_INIT ) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5365 | return( PSA_ERROR_BAD_STATE ); |
| 5366 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5367 | if( data_length != 0 ) |
| 5368 | { |
| 5369 | prf->seed = mbedtls_calloc( 1, data_length ); |
| 5370 | if( prf->seed == NULL ) |
| 5371 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5372 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5373 | memcpy( prf->seed, data, data_length ); |
| 5374 | prf->seed_length = data_length; |
| 5375 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5376 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5377 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5378 | |
| 5379 | return( PSA_SUCCESS ); |
| 5380 | } |
| 5381 | |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5382 | 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] | 5383 | const uint8_t *data, |
| 5384 | size_t data_length ) |
| 5385 | { |
Przemek Stekiel | d7a2864 | 2022-04-07 14:58:33 +0200 | [diff] [blame] | 5386 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET && |
| 5387 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET ) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5388 | return( PSA_ERROR_BAD_STATE ); |
| 5389 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5390 | if( data_length != 0 ) |
| 5391 | { |
| 5392 | prf->secret = mbedtls_calloc( 1, data_length ); |
| 5393 | if( prf->secret == NULL ) |
| 5394 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5395 | |
| 5396 | memcpy( prf->secret, data, data_length ); |
| 5397 | prf->secret_length = data_length; |
| 5398 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5399 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5400 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5401 | |
| 5402 | return( PSA_SUCCESS ); |
| 5403 | } |
| 5404 | |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5405 | static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf, |
| 5406 | const uint8_t *data, |
| 5407 | size_t data_length ) |
| 5408 | { |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5409 | if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET ) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5410 | return( PSA_ERROR_BAD_STATE ); |
| 5411 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5412 | if( data_length != 0 ) |
| 5413 | { |
| 5414 | prf->label = mbedtls_calloc( 1, data_length ); |
| 5415 | if( prf->label == NULL ) |
| 5416 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5417 | |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5418 | memcpy( prf->label, data, data_length ); |
| 5419 | prf->label_length = data_length; |
| 5420 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5421 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5422 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5423 | |
| 5424 | return( PSA_SUCCESS ); |
| 5425 | } |
| 5426 | |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5427 | 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] | 5428 | psa_key_derivation_step_t step, |
| 5429 | const uint8_t *data, |
| 5430 | size_t data_length ) |
| 5431 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5432 | switch( step ) |
| 5433 | { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5434 | case PSA_KEY_DERIVATION_INPUT_SEED: |
| 5435 | return( psa_tls12_prf_set_seed( prf, data, data_length ) ); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5436 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5437 | return( psa_tls12_prf_set_key( prf, data, data_length ) ); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5438 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5439 | return( psa_tls12_prf_set_label( prf, data, data_length ) ); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5440 | default: |
| 5441 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5442 | } |
| 5443 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5444 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5445 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 5446 | |
| 5447 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5448 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 5449 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5450 | const uint8_t *data, |
| 5451 | size_t data_length ) |
| 5452 | { |
| 5453 | psa_status_t status; |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5454 | const size_t pms_len = ( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? |
| 5455 | 4 + data_length + prf->other_secret_length : |
| 5456 | 4 + 2 * data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5457 | |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5458 | if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5459 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5460 | |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5461 | uint8_t *pms = mbedtls_calloc( 1, pms_len ); |
Przemek Stekiel | 937b90f | 2022-04-20 08:33:13 +0200 | [diff] [blame] | 5462 | if( pms == NULL ) |
| 5463 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5464 | uint8_t *cur = pms; |
| 5465 | |
| 5466 | /* pure-PSK: |
| 5467 | * Quoting RFC 4279, Section 2: |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5468 | * |
| 5469 | * The premaster secret is formed as follows: if the PSK is N octets |
| 5470 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 5471 | * uint16 with the value N, and the PSK itself. |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5472 | * |
| 5473 | * mixed-PSK: |
| 5474 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as |
| 5475 | * follows: concatenate a uint16 with the length of the other secret, |
| 5476 | * the other secret itself, uint16 with the length of PSK, and the |
| 5477 | * PSK itself. |
| 5478 | * For details please check: |
| 5479 | * - RFC 4279, Section 4 for the definition of RSA-PSK, |
| 5480 | * - RFC 4279, Section 3 for the definition of DHE-PSK, |
| 5481 | * - RFC 5489 for the definition of ECDHE-PSK. |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5482 | */ |
| 5483 | |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 5484 | if( prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ) |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5485 | { |
| 5486 | *cur++ = MBEDTLS_BYTE_1( prf->other_secret_length ); |
| 5487 | *cur++ = MBEDTLS_BYTE_0( prf->other_secret_length ); |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 5488 | if( prf->other_secret_length != 0 ) |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 5489 | { |
| 5490 | memcpy( cur, prf->other_secret, prf->other_secret_length ); |
Przemek Stekiel | 03faf5d2 | 2022-04-20 08:37:43 +0200 | [diff] [blame] | 5491 | mbedtls_platform_zeroize( prf->other_secret, prf->other_secret_length ); |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 5492 | cur += prf->other_secret_length; |
| 5493 | } |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5494 | } |
| 5495 | else |
| 5496 | { |
| 5497 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 5498 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
| 5499 | memset( cur, 0, data_length ); |
| 5500 | cur += data_length; |
| 5501 | } |
| 5502 | |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 5503 | *cur++ = MBEDTLS_BYTE_1( data_length ); |
| 5504 | *cur++ = MBEDTLS_BYTE_0( data_length ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5505 | memcpy( cur, data, data_length ); |
| 5506 | cur += data_length; |
| 5507 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5508 | status = psa_tls12_prf_set_key( prf, pms, cur - pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5509 | |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5510 | mbedtls_platform_zeroize( pms, pms_len ); |
| 5511 | mbedtls_free( pms ); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5512 | return( status ); |
| 5513 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5514 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5515 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( |
| 5516 | psa_tls12_prf_key_derivation_t *prf, |
| 5517 | const uint8_t *data, |
| 5518 | size_t data_length ) |
| 5519 | { |
| 5520 | if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET ) |
| 5521 | return( PSA_ERROR_BAD_STATE ); |
| 5522 | |
| 5523 | if( data_length != 0 ) |
| 5524 | { |
| 5525 | prf->other_secret = mbedtls_calloc( 1, data_length ); |
| 5526 | if( prf->other_secret == NULL ) |
| 5527 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5528 | |
| 5529 | memcpy( prf->other_secret, data, data_length ); |
| 5530 | prf->other_secret_length = data_length; |
| 5531 | } |
| 5532 | else |
| 5533 | { |
| 5534 | prf->other_secret_length = 0; |
| 5535 | } |
| 5536 | |
| 5537 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; |
| 5538 | |
| 5539 | return( PSA_SUCCESS ); |
| 5540 | } |
| 5541 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5542 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 5543 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5544 | psa_key_derivation_step_t step, |
| 5545 | const uint8_t *data, |
| 5546 | size_t data_length ) |
| 5547 | { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5548 | switch( step ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 5549 | { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5550 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5551 | return( psa_tls12_prf_psk_to_ms_set_key( prf, |
| 5552 | data, data_length ) ); |
| 5553 | break; |
| 5554 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
| 5555 | return( psa_tls12_prf_psk_to_ms_set_other_key( prf, |
| 5556 | data, |
| 5557 | data_length ) ); |
| 5558 | break; |
| 5559 | default: |
| 5560 | return( psa_tls12_prf_input( prf, step, data, data_length ) ); |
| 5561 | break; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5562 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5563 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5564 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5565 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5566 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5567 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5568 | static psa_status_t psa_tls12_ecjpake_to_pms_input( |
| 5569 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5570 | psa_key_derivation_step_t step, |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5571 | const uint8_t *data, |
| 5572 | size_t data_length ) |
| 5573 | { |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5574 | if( data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE || |
| 5575 | step != PSA_KEY_DERIVATION_INPUT_SECRET ) |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5576 | { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5577 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5578 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5579 | |
| 5580 | /* Check if the passed point is in an uncompressed form */ |
| 5581 | if( data[0] != 0x04 ) |
| 5582 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5583 | |
| 5584 | /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */ |
| 5585 | memcpy( ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE ); |
| 5586 | |
| 5587 | return( PSA_SUCCESS ); |
| 5588 | } |
| 5589 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5590 | /** Check whether the given key type is acceptable for the given |
| 5591 | * input step of a key derivation. |
| 5592 | * |
| 5593 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 5594 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 5595 | * Both secret and non-secret inputs can alternatively have the type |
| 5596 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 5597 | * that the input was passed as a buffer rather than via a key object. |
| 5598 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5599 | static int psa_key_derivation_check_input_type( |
| 5600 | psa_key_derivation_step_t step, |
| 5601 | psa_key_type_t key_type ) |
| 5602 | { |
| 5603 | switch( step ) |
| 5604 | { |
| 5605 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5606 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5607 | return( PSA_SUCCESS ); |
| 5608 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5609 | return( PSA_SUCCESS ); |
| 5610 | break; |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 5611 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
| 5612 | if( key_type == PSA_KEY_TYPE_DERIVE ) |
| 5613 | return( PSA_SUCCESS ); |
| 5614 | if( key_type == PSA_KEY_TYPE_NONE ) |
| 5615 | return( PSA_SUCCESS ); |
| 5616 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5617 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 5618 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 5619 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 5620 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5621 | if( key_type == PSA_KEY_TYPE_RAW_DATA ) |
| 5622 | return( PSA_SUCCESS ); |
| 5623 | if( key_type == PSA_KEY_TYPE_NONE ) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5624 | return( PSA_SUCCESS ); |
| 5625 | break; |
| 5626 | } |
| 5627 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 5628 | } |
| 5629 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5630 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5631 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5632 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5633 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5634 | const uint8_t *data, |
| 5635 | size_t data_length ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5636 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5637 | psa_status_t status; |
| 5638 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation ); |
| 5639 | |
| 5640 | status = psa_key_derivation_check_input_type( step, key_type ); |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5641 | if( status != PSA_SUCCESS ) |
| 5642 | goto exit; |
| 5643 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5644 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | a29b488 | 2022-06-02 11:37:03 +0200 | [diff] [blame] | 5645 | if( PSA_ALG_IS_ANY_HKDF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5646 | { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5647 | status = psa_hkdf_input( &operation->ctx.hkdf, kdf_alg, |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5648 | step, data, data_length ); |
| 5649 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5650 | else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5651 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5652 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 5653 | if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5654 | { |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5655 | status = psa_tls12_prf_input( &operation->ctx.tls12_prf, |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5656 | step, data, data_length ); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5657 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5658 | else |
| 5659 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 5660 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 5661 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5662 | { |
| 5663 | status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5664 | step, data, data_length ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5665 | } |
| 5666 | else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5667 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5668 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Andrzej Kurek | 3c4c514 | 2022-09-16 07:24:14 -0400 | [diff] [blame] | 5669 | if( kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS ) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5670 | { |
| 5671 | status = psa_tls12_ecjpake_to_pms_input( |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5672 | &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length ); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5673 | } |
| 5674 | else |
| 5675 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5676 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5677 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5678 | (void) data; |
| 5679 | (void) data_length; |
| 5680 | (void) kdf_alg; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5681 | return( PSA_ERROR_BAD_STATE ); |
| 5682 | } |
| 5683 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5684 | exit: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5685 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5686 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5687 | return( status ); |
| 5688 | } |
| 5689 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5690 | psa_status_t psa_key_derivation_input_bytes( |
| 5691 | psa_key_derivation_operation_t *operation, |
| 5692 | psa_key_derivation_step_t step, |
| 5693 | const uint8_t *data, |
| 5694 | size_t data_length ) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5695 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5696 | return( psa_key_derivation_input_internal( operation, step, |
| 5697 | PSA_KEY_TYPE_NONE, |
Janos Follath | c562151 | 2019-06-14 11:27:57 +0100 | [diff] [blame] | 5698 | data, data_length ) ); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5699 | } |
| 5700 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5701 | psa_status_t psa_key_derivation_input_key( |
| 5702 | psa_key_derivation_operation_t *operation, |
| 5703 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5704 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5705 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5706 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5707 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5708 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5709 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5710 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5711 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5712 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5713 | { |
| 5714 | psa_key_derivation_abort( operation ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5715 | return( status ); |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5716 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5717 | |
| 5718 | /* Passing a key object as a SECRET input unlocks the permission |
| 5719 | * to output to a key object. */ |
| 5720 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5721 | operation->can_output_key = 1; |
| 5722 | |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5723 | status = psa_key_derivation_input_internal( operation, |
| 5724 | step, slot->attr.type, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 5725 | slot->key.data, |
| 5726 | slot->key.bytes ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5727 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5728 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5729 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5730 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5731 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5732 | |
| 5733 | |
| 5734 | |
| 5735 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5736 | /* Key agreement */ |
| 5737 | /****************************************************************/ |
| 5738 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5739 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5740 | static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key, |
| 5741 | size_t peer_key_length, |
| 5742 | const mbedtls_ecp_keypair *our_key, |
| 5743 | uint8_t *shared_secret, |
| 5744 | size_t shared_secret_size, |
| 5745 | size_t *shared_secret_length ) |
| 5746 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 5747 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5748 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5749 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5750 | size_t bits = 0; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 5751 | 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] | 5752 | mbedtls_ecdh_init( &ecdh ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5753 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5754 | status = mbedtls_psa_ecp_load_representation( |
| 5755 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 5756 | bits, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5757 | peer_key, |
| 5758 | peer_key_length, |
| 5759 | &their_key ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5760 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5761 | goto exit; |
| 5762 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5763 | status = mbedtls_to_psa_error( |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5764 | mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) ); |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5765 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5766 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5767 | status = mbedtls_to_psa_error( |
| 5768 | mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) ); |
| 5769 | if( status != PSA_SUCCESS ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5770 | goto exit; |
| 5771 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5772 | status = mbedtls_to_psa_error( |
| 5773 | mbedtls_ecdh_calc_secret( &ecdh, |
| 5774 | shared_secret_length, |
| 5775 | shared_secret, shared_secret_size, |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5776 | mbedtls_psa_get_random, |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 5777 | MBEDTLS_PSA_RANDOM_STATE ) ); |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 5778 | if( status != PSA_SUCCESS ) |
| 5779 | goto exit; |
| 5780 | if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length ) |
| 5781 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5782 | |
| 5783 | exit: |
Gilles Peskine | 3e819b7 | 2019-12-20 14:09:55 +0100 | [diff] [blame] | 5784 | if( status != PSA_SUCCESS ) |
| 5785 | mbedtls_platform_zeroize( shared_secret, shared_secret_size ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5786 | mbedtls_ecdh_free( &ecdh ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5787 | mbedtls_ecp_keypair_free( their_key ); |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 5788 | mbedtls_free( their_key ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5789 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 5790 | return( status ); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5791 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5792 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5793 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5794 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 5795 | |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5796 | psa_status_t psa_key_agreement_raw_builtin( const psa_key_attributes_t *attributes, |
| 5797 | const uint8_t *key_buffer, |
| 5798 | size_t key_buffer_size, |
| 5799 | psa_algorithm_t alg, |
| 5800 | const uint8_t *peer_key, |
| 5801 | size_t peer_key_length, |
| 5802 | uint8_t *shared_secret, |
| 5803 | size_t shared_secret_size, |
| 5804 | size_t *shared_secret_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5805 | { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5806 | switch( alg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5807 | { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5808 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5809 | case PSA_ALG_ECDH: |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5810 | if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( attributes->core.type ) ) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5811 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5812 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5813 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5814 | attributes->core.type, |
| 5815 | attributes->core.bits, |
| 5816 | key_buffer, |
| 5817 | key_buffer_size, |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 5818 | &ecp ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5819 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5820 | return( status ); |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5821 | status = psa_key_agreement_ecdh( peer_key, peer_key_length, |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5822 | ecp, |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 5823 | shared_secret, shared_secret_size, |
| 5824 | shared_secret_length ); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 5825 | mbedtls_ecp_keypair_free( ecp ); |
| 5826 | mbedtls_free( ecp ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5827 | return( status ); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5828 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5829 | default: |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5830 | (void) attributes; |
| 5831 | (void) key_buffer; |
| 5832 | (void) key_buffer_size; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5833 | (void) peer_key; |
| 5834 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5835 | (void) shared_secret; |
| 5836 | (void) shared_secret_size; |
| 5837 | (void) shared_secret_length; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5838 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5839 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5840 | } |
| 5841 | |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5842 | /** Internal function for raw key agreement |
| 5843 | * Calls the driver wrapper which will hand off key agreement task |
Aditya Deshpande | 40c05cc | 2022-10-14 16:41:40 +0100 | [diff] [blame^] | 5844 | * to the driver's implementation if a driver is present. |
| 5845 | * Fallback specified in the driver wrapper is built-in raw key agreement |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5846 | * (psa_key_agreement_raw_builtin). |
| 5847 | */ |
| 5848 | static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, |
| 5849 | psa_key_slot_t *private_key, |
| 5850 | const uint8_t *peer_key, |
| 5851 | size_t peer_key_length, |
| 5852 | uint8_t *shared_secret, |
| 5853 | size_t shared_secret_size, |
| 5854 | size_t *shared_secret_length ) |
| 5855 | { |
| 5856 | if( !PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) ) |
| 5857 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5858 | |
| 5859 | psa_key_attributes_t attributes = { |
| 5860 | .core = private_key->attr |
| 5861 | }; |
| 5862 | |
| 5863 | return( psa_driver_wrapper_key_agreement( &attributes, private_key->key.data, |
Aditya Deshpande | 40c05cc | 2022-10-14 16:41:40 +0100 | [diff] [blame^] | 5864 | private_key->key.bytes, |
| 5865 | alg, peer_key, peer_key_length, |
| 5866 | shared_secret, shared_secret_size, |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5867 | shared_secret_length ) ); |
| 5868 | } |
| 5869 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5870 | /* 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] | 5871 | * to potentially free embedded data structures and wipe confidential data. |
| 5872 | */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5873 | 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] | 5874 | psa_key_derivation_step_t step, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5875 | psa_key_slot_t *private_key, |
| 5876 | const uint8_t *peer_key, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5877 | size_t peer_key_length ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5878 | { |
| 5879 | psa_status_t status; |
| 5880 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 5881 | size_t shared_secret_length = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5882 | 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] | 5883 | |
| 5884 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 5885 | * secret. */ |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5886 | status = psa_key_agreement_raw_internal( ka_alg, |
| 5887 | private_key, |
| 5888 | peer_key, peer_key_length, |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5889 | shared_secret, |
| 5890 | sizeof( shared_secret ), |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5891 | &shared_secret_length ); |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5892 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5893 | goto exit; |
| 5894 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5895 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5896 | * the shared secret. A shared secret is permitted wherever a key |
| 5897 | * of type DERIVE is permitted. */ |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5898 | status = psa_key_derivation_input_internal( operation, step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5899 | PSA_KEY_TYPE_DERIVE, |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5900 | shared_secret, |
| 5901 | shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5902 | exit: |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 5903 | mbedtls_platform_zeroize( shared_secret, shared_secret_length ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5904 | return( status ); |
| 5905 | } |
| 5906 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5907 | 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] | 5908 | psa_key_derivation_step_t step, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5909 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5910 | const uint8_t *peer_key, |
| 5911 | size_t peer_key_length ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5912 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5913 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5914 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5915 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5916 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5917 | if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5918 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5919 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5920 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5921 | if( status != PSA_SUCCESS ) |
| 5922 | return( status ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5923 | status = psa_key_agreement_internal( operation, step, |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5924 | slot, |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5925 | peer_key, peer_key_length ); |
Gilles Peskine | 346797d | 2018-11-16 16:05:06 +0100 | [diff] [blame] | 5926 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5927 | psa_key_derivation_abort( operation ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5928 | else |
| 5929 | { |
| 5930 | /* If a private key has been added as SECRET, we allow the derived |
| 5931 | * key material to be used as a key in PSA Crypto. */ |
| 5932 | if( step == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5933 | operation->can_output_key = 1; |
| 5934 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5935 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5936 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5937 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5938 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5939 | } |
| 5940 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5941 | psa_status_t psa_raw_key_agreement( psa_algorithm_t alg, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 5942 | mbedtls_svc_key_id_t private_key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5943 | const uint8_t *peer_key, |
| 5944 | size_t peer_key_length, |
| 5945 | uint8_t *output, |
| 5946 | size_t output_size, |
| 5947 | size_t *output_length ) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5948 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5949 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5950 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5951 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5952 | |
| 5953 | if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 5954 | { |
| 5955 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5956 | goto exit; |
| 5957 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5958 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
| 5959 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5960 | if( status != PSA_SUCCESS ) |
| 5961 | goto exit; |
| 5962 | |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5963 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 5964 | * for the output size. The PSA specification only guarantees that this |
| 5965 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 5966 | * but it might be nice to allow smaller buffers if the output fits. |
| 5967 | * At the time of writing this comment, with only ECDH implemented, |
| 5968 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 5969 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 5970 | * be exact for it as well. */ |
| 5971 | size_t expected_length = |
| 5972 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( slot->attr.type, slot->attr.bits ); |
| 5973 | if( output_size < expected_length ) |
| 5974 | { |
| 5975 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5976 | goto exit; |
| 5977 | } |
| 5978 | |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5979 | status = psa_key_agreement_raw_internal( alg, slot, |
| 5980 | peer_key, peer_key_length, |
| 5981 | output, output_size, |
| 5982 | output_length ); |
| 5983 | |
| 5984 | exit: |
| 5985 | if( status != PSA_SUCCESS ) |
| 5986 | { |
| 5987 | /* If an error happens and is not handled properly, the output |
| 5988 | * may be used as a key to protect sensitive data. Arrange for such |
| 5989 | * a key to be random, which is likely to result in decryption or |
| 5990 | * verification errors. This is better than filling the buffer with |
| 5991 | * some constant data such as zeros, which would result in the data |
| 5992 | * being protected with a reproducible, easily knowable key. |
| 5993 | */ |
| 5994 | psa_generate_random( output, output_size ); |
| 5995 | *output_length = output_size; |
| 5996 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5997 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5998 | unlock_status = psa_unlock_key_slot( slot ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5999 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6000 | return( ( status == PSA_SUCCESS ) ? unlock_status : status ); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6001 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6002 | |
| 6003 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6004 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6005 | /****************************************************************/ |
| 6006 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6007 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6008 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6009 | /** Initialize the PSA random generator. |
| 6010 | */ |
| 6011 | static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng ) |
| 6012 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6013 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6014 | memset( rng, 0, sizeof( *rng ) ); |
| 6015 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6016 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6017 | /* Set default configuration if |
| 6018 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
| 6019 | if( rng->entropy_init == NULL ) |
| 6020 | rng->entropy_init = mbedtls_entropy_init; |
| 6021 | if( rng->entropy_free == NULL ) |
| 6022 | rng->entropy_free = mbedtls_entropy_free; |
| 6023 | |
| 6024 | rng->entropy_init( &rng->entropy ); |
| 6025 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6026 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6027 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6028 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
| 6029 | mbedtls_entropy_add_source( &rng->entropy, |
| 6030 | mbedtls_nv_seed_poll, NULL, |
| 6031 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6032 | MBEDTLS_ENTROPY_SOURCE_STRONG ); |
| 6033 | #endif |
| 6034 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6035 | mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6036 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6037 | } |
| 6038 | |
| 6039 | /** Deinitialize the PSA random generator. |
| 6040 | */ |
| 6041 | static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng ) |
| 6042 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6043 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6044 | memset( rng, 0, sizeof( *rng ) ); |
| 6045 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6046 | mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6047 | rng->entropy_free( &rng->entropy ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6048 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6049 | } |
| 6050 | |
| 6051 | /** Seed the PSA random generator. |
| 6052 | */ |
| 6053 | static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng ) |
| 6054 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6055 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6056 | /* Do nothing: the external RNG seeds itself. */ |
| 6057 | (void) rng; |
| 6058 | return( PSA_SUCCESS ); |
| 6059 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6060 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 6061 | int ret = mbedtls_psa_drbg_seed( &rng->entropy, |
| 6062 | drbg_seed, sizeof( drbg_seed ) - 1 ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6063 | return mbedtls_to_psa_error( ret ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6064 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6065 | } |
| 6066 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6067 | psa_status_t psa_generate_random( uint8_t *output, |
| 6068 | size_t output_size ) |
| 6069 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6070 | GUARD_MODULE_INITIALIZED; |
| 6071 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6072 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6073 | |
| 6074 | size_t output_length = 0; |
| 6075 | psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng, |
| 6076 | output, output_size, |
| 6077 | &output_length ); |
| 6078 | if( status != PSA_SUCCESS ) |
| 6079 | return( status ); |
| 6080 | /* Breaking up a request into smaller chunks is currently not supported |
| 6081 | * for the extrernal RNG interface. */ |
| 6082 | if( output_length != output_size ) |
| 6083 | return( PSA_ERROR_INSUFFICIENT_ENTROPY ); |
| 6084 | return( PSA_SUCCESS ); |
| 6085 | |
| 6086 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6087 | |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6088 | while( output_size > 0 ) |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6089 | { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6090 | size_t request_size = |
| 6091 | ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6092 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6093 | output_size ); |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6094 | int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 6095 | output, request_size ); |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6096 | if( ret != 0 ) |
| 6097 | return( mbedtls_to_psa_error( ret ) ); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6098 | output_size -= request_size; |
| 6099 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6100 | } |
Gilles Peskine | 0c59ba8 | 2021-01-05 14:10:59 +0100 | [diff] [blame] | 6101 | return( PSA_SUCCESS ); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6102 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6103 | } |
| 6104 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6105 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6106 | * |
| 6107 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6108 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6109 | * PSA API doesn't support passing an explicit state. |
| 6110 | * |
| 6111 | * In the non-external case, psa_generate_random() calls an |
| 6112 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6113 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6114 | * instead of doing this back-and-forth between the PSA API and the |
| 6115 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6116 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6117 | */ |
| 6118 | #if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6119 | int mbedtls_psa_get_random( void *p_rng, |
| 6120 | unsigned char *output, |
| 6121 | size_t output_size ) |
| 6122 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6123 | /* This function takes a pointer to the RNG state because that's what |
| 6124 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6125 | * its own state internally and doesn't let the caller access that state. |
| 6126 | * So we just ignore the state parameter, and in practice we'll pass |
| 6127 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6128 | (void) p_rng; |
| 6129 | psa_status_t status = psa_generate_random( output, output_size ); |
| 6130 | if( status == PSA_SUCCESS ) |
| 6131 | return( 0 ); |
| 6132 | else |
| 6133 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 6134 | } |
| 6135 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6136 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6137 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Chris Jones | ea0a865 | 2021-03-09 19:11:19 +0000 | [diff] [blame] | 6138 | #include "entropy_poll.h" |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6139 | |
Gilles Peskine | 7228da2 | 2019-07-15 11:06:15 +0200 | [diff] [blame] | 6140 | psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed, |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6141 | size_t seed_size ) |
| 6142 | { |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6143 | if( global_data.initialized ) |
| 6144 | return( PSA_ERROR_NOT_PERMITTED ); |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6145 | |
| 6146 | if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || |
| 6147 | ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) || |
| 6148 | ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) ) |
| 6149 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6150 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6151 | return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) ); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6152 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6153 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6154 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6155 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6156 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6157 | * \param type The key type |
| 6158 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6159 | * |
| 6160 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6161 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6162 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 6163 | * The size in bits of the key is not valid. |
| 6164 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 6165 | * The type and/or the size in bits of the key or the combination of |
| 6166 | * the two is not supported. |
| 6167 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6168 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
| 6169 | psa_key_type_t type, size_t bits ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6170 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6171 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6172 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6173 | if( key_type_is_raw_bytes( type ) ) |
| 6174 | { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 6175 | status = psa_validate_unstructured_key_bit_size( type, bits ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6176 | if( status != PSA_SUCCESS ) |
| 6177 | return( status ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6178 | } |
| 6179 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6180 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6181 | if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6182 | { |
| 6183 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 6184 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6185 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6186 | /* Accept only byte-aligned keys, for the same reasons as |
| 6187 | * in psa_import_rsa_key(). */ |
| 6188 | if( bits % 8 != 0 ) |
| 6189 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6190 | } |
| 6191 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6192 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6193 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6194 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6195 | if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 6196 | { |
Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 6197 | /* To avoid empty block, return successfully here. */ |
| 6198 | return( PSA_SUCCESS ); |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6199 | } |
| 6200 | else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6201 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6202 | { |
| 6203 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 6204 | } |
| 6205 | |
| 6206 | return( PSA_SUCCESS ); |
| 6207 | } |
| 6208 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 6209 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6210 | const psa_key_attributes_t *attributes, |
| 6211 | 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] | 6212 | { |
| 6213 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6214 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6215 | |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6216 | if( ( attributes->domain_parameters == NULL ) && |
| 6217 | ( attributes->domain_parameters_size != 0 ) ) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6218 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6219 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6220 | if( key_type_is_raw_bytes( type ) ) |
| 6221 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6222 | status = psa_generate_random( key_buffer, key_buffer_size ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6223 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6224 | return( status ); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6225 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6226 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6227 | if( type == PSA_KEY_TYPE_DES ) |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6228 | psa_des_set_key_parity( key_buffer, key_buffer_size ); |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 6229 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6230 | } |
| 6231 | else |
| 6232 | |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6233 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 6234 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6235 | if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6236 | { |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 6237 | return( mbedtls_psa_rsa_generate_key( attributes, |
| 6238 | key_buffer, |
| 6239 | key_buffer_size, |
| 6240 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6241 | } |
| 6242 | else |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6243 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 6244 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6245 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6246 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6247 | 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] | 6248 | { |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 6249 | return( mbedtls_psa_ecp_generate_key( attributes, |
| 6250 | key_buffer, |
| 6251 | key_buffer_size, |
| 6252 | key_buffer_length ) ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6253 | } |
| 6254 | else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6255 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6256 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6257 | (void)key_buffer_length; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6258 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6259 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6260 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6261 | return( PSA_SUCCESS ); |
| 6262 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6263 | |
Gilles Peskine | 35ef36b | 2019-05-16 19:42:05 +0200 | [diff] [blame] | 6264 | psa_status_t psa_generate_key( const psa_key_attributes_t *attributes, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 6265 | mbedtls_svc_key_id_t *key ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6266 | { |
| 6267 | psa_status_t status; |
| 6268 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6269 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6270 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6271 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6272 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6273 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6274 | /* Reject any attempt to create a zero-length key so that we don't |
| 6275 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
| 6276 | if( psa_get_key_bits( attributes ) == 0 ) |
| 6277 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6278 | |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6279 | /* Reject any attempt to create a public key. */ |
| 6280 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) ) |
| 6281 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 6282 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6283 | status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes, |
| 6284 | &slot, &driver ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6285 | if( status != PSA_SUCCESS ) |
| 6286 | goto exit; |
| 6287 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6288 | /* 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] | 6289 | * storage ( thus not in the case of generating a key in a secure element |
| 6290 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 6291 | * buffer to hold the generated key material. */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6292 | if( slot->key.data == NULL ) |
| 6293 | { |
| 6294 | if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == |
| 6295 | PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 6296 | { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6297 | status = psa_validate_key_type_and_size_for_key_generation( |
| 6298 | attributes->core.type, attributes->core.bits ); |
| 6299 | if( status != PSA_SUCCESS ) |
| 6300 | goto exit; |
| 6301 | |
| 6302 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 6303 | attributes->core.type, |
| 6304 | attributes->core.bits ); |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6305 | } |
| 6306 | else |
| 6307 | { |
| 6308 | status = psa_driver_wrapper_get_key_buffer_size( |
| 6309 | attributes, &key_buffer_size ); |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6310 | if( status != PSA_SUCCESS ) |
| 6311 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6312 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6313 | |
| 6314 | status = psa_allocate_buffer_to_slot( slot, key_buffer_size ); |
| 6315 | if( status != PSA_SUCCESS ) |
| 6316 | goto exit; |
| 6317 | } |
| 6318 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 6319 | status = psa_driver_wrapper_generate_key( attributes, |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6320 | slot->key.data, slot->key.bytes, &slot->key.bytes ); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6321 | |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6322 | if( status != PSA_SUCCESS ) |
| 6323 | psa_remove_key_data_from_memory( slot ); |
| 6324 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6325 | exit: |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6326 | if( status == PSA_SUCCESS ) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6327 | status = psa_finish_key_creation( slot, driver, key ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6328 | if( status != PSA_SUCCESS ) |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 6329 | psa_fail_key_creation( slot, driver ); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6330 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6331 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6332 | } |
| 6333 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6334 | /****************************************************************/ |
| 6335 | /* Module setup */ |
| 6336 | /****************************************************************/ |
| 6337 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6338 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6339 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
| 6340 | void (* entropy_init )( mbedtls_entropy_context *ctx ), |
| 6341 | void (* entropy_free )( mbedtls_entropy_context *ctx ) ) |
| 6342 | { |
| 6343 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6344 | return( PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6345 | global_data.rng.entropy_init = entropy_init; |
| 6346 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6347 | return( PSA_SUCCESS ); |
| 6348 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6349 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6350 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6351 | void mbedtls_psa_crypto_free( void ) |
| 6352 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6353 | psa_wipe_all_key_slots( ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6354 | if( global_data.rng_state != RNG_NOT_INITIALIZED ) |
| 6355 | { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6356 | mbedtls_psa_random_free( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6357 | } |
| 6358 | /* Wipe all remaining data, including configuration. |
| 6359 | * In particular, this sets all state indicator to the value |
| 6360 | * indicating "uninitialized". */ |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 6361 | mbedtls_platform_zeroize( &global_data, sizeof( global_data ) ); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 6362 | |
| 6363 | /* Terminate drivers */ |
| 6364 | psa_driver_wrapper_free( ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6365 | } |
| 6366 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6367 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 6368 | /** Recover a transaction that was interrupted by a power failure. |
| 6369 | * |
| 6370 | * This function is called during initialization, before psa_crypto_init() |
| 6371 | * returns. If this function returns a failure status, the initialization |
| 6372 | * fails. |
| 6373 | */ |
| 6374 | static psa_status_t psa_crypto_recover_transaction( |
| 6375 | const psa_crypto_transaction_t *transaction ) |
| 6376 | { |
| 6377 | switch( transaction->unknown.type ) |
| 6378 | { |
| 6379 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 6380 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 6381 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 6382 | * is implemented. |
| 6383 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 6384 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6385 | default: |
| 6386 | /* We found an unsupported transaction in the storage. |
| 6387 | * 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] | 6388 | return( PSA_ERROR_DATA_INVALID ); |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6389 | } |
| 6390 | } |
| 6391 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 6392 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6393 | psa_status_t psa_crypto_init( void ) |
| 6394 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6395 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6396 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6397 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6398 | if( global_data.initialized != 0 ) |
| 6399 | return( PSA_SUCCESS ); |
| 6400 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6401 | /* Initialize and seed the random generator. */ |
| 6402 | mbedtls_psa_random_init( &global_data.rng ); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6403 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6404 | status = mbedtls_psa_random_seed( &global_data.rng ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6405 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6406 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6407 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6408 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6409 | status = psa_initialize_key_slots( ); |
| 6410 | if( status != PSA_SUCCESS ) |
| 6411 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6412 | |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 6413 | /* Init drivers */ |
| 6414 | status = psa_driver_wrapper_init( ); |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6415 | if( status != PSA_SUCCESS ) |
| 6416 | goto exit; |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6417 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6418 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6419 | status = psa_crypto_load_transaction( ); |
| 6420 | if( status == PSA_SUCCESS ) |
| 6421 | { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6422 | status = psa_crypto_recover_transaction( &psa_crypto_transaction ); |
| 6423 | if( status != PSA_SUCCESS ) |
| 6424 | goto exit; |
| 6425 | status = psa_crypto_stop_transaction( ); |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6426 | } |
| 6427 | else if( status == PSA_ERROR_DOES_NOT_EXIST ) |
| 6428 | { |
| 6429 | /* There's no transaction to complete. It's all good. */ |
| 6430 | status = PSA_SUCCESS; |
| 6431 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6432 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6433 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6434 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6435 | global_data.initialized = 1; |
| 6436 | |
| 6437 | exit: |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6438 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6439 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6440 | return( status ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6441 | } |
| 6442 | |
| 6443 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |