| 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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array))) | 
| Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | static int key_type_is_raw_bytes(psa_key_type_t type) | 
| Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 98 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | typedef struct { | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 108 | unsigned initialized : 1; | 
| Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 109 | unsigned rng_state : 2; | 
| Gilles Peskine | 2d8a182 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 110 | mbedtls_psa_random_context_t rng; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 111 | } psa_global_data_t; | 
|  | 112 |  | 
|  | 113 | static psa_global_data_t global_data; | 
|  | 114 |  | 
| Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 115 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) | 
|  | 116 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = | 
|  | 117 | &global_data.rng.drbg; | 
|  | 118 | #endif | 
|  | 119 |  | 
| itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 120 | #define GUARD_MODULE_INITIALIZED        \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | if (global_data.initialized == 0)  \ | 
|  | 122 | return PSA_ERROR_BAD_STATE; | 
| itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 123 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | psa_status_t mbedtls_to_psa_error(int ret) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 125 | { | 
| Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 126 | /* Mbed TLS error codes can combine a high-level error code and a | 
|  | 127 | * low-level error code. The low-level error usually reflects the | 
|  | 128 | * root cause better, so dispatch on that preferably. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | int low_level_ret = -(-ret & 0x007f); | 
|  | 130 | switch (low_level_ret != 0 ? low_level_ret : ret) { | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 131 | case 0: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | return PSA_SUCCESS; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 133 |  | 
|  | 134 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: | 
|  | 135 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 137 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: | 
|  | 138 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: | 
|  | 139 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: | 
|  | 140 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: | 
|  | 141 | case MBEDTLS_ERR_ASN1_INVALID_DATA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 143 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
| Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 145 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
| Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 147 |  | 
| Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 148 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) | 
| Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 149 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: | 
| Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 150 | #endif | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 151 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 153 |  | 
|  | 154 | case MBEDTLS_ERR_CCM_BAD_INPUT: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 156 | case MBEDTLS_ERR_CCM_AUTH_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 157 | return PSA_ERROR_INVALID_SIGNATURE; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 158 |  | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 159 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 161 |  | 
|  | 162 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | return PSA_ERROR_BAD_STATE; | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 164 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 165 | return PSA_ERROR_INVALID_SIGNATURE; | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 166 |  | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 167 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 169 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 171 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 173 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | return PSA_ERROR_INVALID_PADDING; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 175 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 177 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | return PSA_ERROR_INVALID_SIGNATURE; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 179 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 180 | return PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 181 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | #if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) ||      \ | 
|  | 183 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) | 
| Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 184 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx | 
|  | 185 | * functions are passed a CTR_DRBG instance. */ | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 186 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 187 | return PSA_ERROR_INSUFFICIENT_ENTROPY; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 188 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: | 
|  | 189 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 191 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | return PSA_ERROR_INSUFFICIENT_ENTROPY; | 
| Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 193 | #endif | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 194 |  | 
|  | 195 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 197 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 198 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: | 
|  | 199 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: | 
|  | 200 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | return PSA_ERROR_INSUFFICIENT_ENTROPY; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 202 |  | 
|  | 203 | case MBEDTLS_ERR_GCM_AUTH_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 204 | return PSA_ERROR_INVALID_SIGNATURE; | 
| Mateusz Starzyk | f28261f | 2021-09-30 16:39:07 +0200 | [diff] [blame] | 205 | case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 207 | case MBEDTLS_ERR_GCM_BAD_INPUT: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 208 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 209 |  | 
| Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 210 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) &&        \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) | 
| Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 212 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx | 
|  | 213 | * functions are passed a HMAC_DRBG instance. */ | 
| Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 214 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | return PSA_ERROR_INSUFFICIENT_ENTROPY; | 
| Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 216 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: | 
|  | 217 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 219 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | return PSA_ERROR_INSUFFICIENT_ENTROPY; | 
| Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 221 | #endif | 
|  | 222 |  | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 223 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 224 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 225 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 227 | case MBEDTLS_ERR_MD_ALLOC_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 229 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | return PSA_ERROR_STORAGE_FAILURE; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 231 |  | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 232 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | return PSA_ERROR_STORAGE_FAILURE; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 234 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 236 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 237 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 238 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 240 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 242 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 243 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 244 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 246 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
| Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 248 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 249 | case MBEDTLS_ERR_PK_ALLOC_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 250 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 251 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: | 
|  | 252 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 254 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | return PSA_ERROR_STORAGE_FAILURE; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 256 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: | 
|  | 257 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 259 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 261 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: | 
|  | 262 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | return PSA_ERROR_NOT_PERMITTED; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 264 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 266 | case MBEDTLS_ERR_PK_INVALID_ALG: | 
|  | 267 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: | 
|  | 268 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 270 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | return PSA_ERROR_INVALID_SIGNATURE; | 
| Gilles Peskine | f9f1bdf | 2021-06-23 20:32:27 +0200 | [diff] [blame] | 272 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 274 |  | 
| Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 275 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | return PSA_ERROR_HARDWARE_FAILURE; | 
| Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 277 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 279 |  | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 280 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 282 | case MBEDTLS_ERR_RSA_INVALID_PADDING: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 283 | return PSA_ERROR_INVALID_PADDING; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 284 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | return PSA_ERROR_HARDWARE_FAILURE; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 286 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 288 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: | 
|  | 289 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | return PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 291 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | return PSA_ERROR_INVALID_SIGNATURE; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 293 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 294 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
| Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 295 | case MBEDTLS_ERR_RSA_RNG_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | return PSA_ERROR_INSUFFICIENT_ENTROPY; | 
| Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 297 |  | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 298 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: | 
| itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 299 | case MBEDTLS_ERR_ECP_INVALID_KEY: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | return PSA_ERROR_INVALID_ARGUMENT; | 
| itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 301 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
| itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 303 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | return PSA_ERROR_NOT_SUPPORTED; | 
| itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 305 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: | 
|  | 306 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | return PSA_ERROR_INVALID_SIGNATURE; | 
| itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 308 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
| Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 310 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | return PSA_ERROR_INSUFFICIENT_ENTROPY; | 
| Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 312 |  | 
| Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 313 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | return PSA_ERROR_CORRUPTION_DETECTED; | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 315 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 316 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | return PSA_ERROR_GENERIC_ERROR; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 318 | } | 
|  | 319 | } | 
|  | 320 |  | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 321 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 322 |  | 
|  | 323 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 324 | /****************************************************************/ | 
|  | 325 | /* Key management */ | 
|  | 326 | /****************************************************************/ | 
|  | 327 |  | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 328 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ | 
| John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 329 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ | 
| Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 330 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ | 
|  | 331 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ | 
|  | 332 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, | 
|  | 334 | size_t bits, | 
|  | 335 | int bits_is_sloppy) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 336 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | switch (curve) { | 
| Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 338 | case PSA_ECC_FAMILY_SECP_R1: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | switch (bits) { | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 340 | #if defined(PSA_WANT_ECC_SECP_R1_192) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 341 | case 192: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 342 | return MBEDTLS_ECP_DP_SECP192R1; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 343 | #endif | 
|  | 344 | #if defined(PSA_WANT_ECC_SECP_R1_224) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 345 | case 224: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 346 | return MBEDTLS_ECP_DP_SECP224R1; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 347 | #endif | 
|  | 348 | #if defined(PSA_WANT_ECC_SECP_R1_256) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 349 | case 256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | return MBEDTLS_ECP_DP_SECP256R1; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 351 | #endif | 
|  | 352 | #if defined(PSA_WANT_ECC_SECP_R1_384) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 353 | case 384: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | return MBEDTLS_ECP_DP_SECP384R1; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 355 | #endif | 
|  | 356 | #if defined(PSA_WANT_ECC_SECP_R1_521) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 357 | case 521: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | return MBEDTLS_ECP_DP_SECP521R1; | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 359 | case 528: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | if (bits_is_sloppy) { | 
|  | 361 | return MBEDTLS_ECP_DP_SECP521R1; | 
|  | 362 | } | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 363 | break; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 364 | #endif | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 365 | } | 
|  | 366 | break; | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 367 |  | 
| Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 368 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | switch (bits) { | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 370 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 371 | case 256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | return MBEDTLS_ECP_DP_BP256R1; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 373 | #endif | 
|  | 374 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 375 | case 384: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | return MBEDTLS_ECP_DP_BP384R1; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 377 | #endif | 
|  | 378 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 379 | case 512: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | return MBEDTLS_ECP_DP_BP512R1; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 381 | #endif | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 382 | } | 
|  | 383 | break; | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 384 |  | 
| Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 385 | case PSA_ECC_FAMILY_MONTGOMERY: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | switch (bits) { | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 387 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 388 | case 255: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | return MBEDTLS_ECP_DP_CURVE25519; | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 390 | case 256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | if (bits_is_sloppy) { | 
|  | 392 | return MBEDTLS_ECP_DP_CURVE25519; | 
|  | 393 | } | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 394 | break; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 395 | #endif | 
|  | 396 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 397 | case 448: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 398 | return MBEDTLS_ECP_DP_CURVE448; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 399 | #endif | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 400 | } | 
|  | 401 | break; | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 402 |  | 
| Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 403 | case PSA_ECC_FAMILY_SECP_K1: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | switch (bits) { | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 405 | #if defined(PSA_WANT_ECC_SECP_K1_192) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 406 | case 192: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 407 | return MBEDTLS_ECP_DP_SECP192K1; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 408 | #endif | 
|  | 409 | #if defined(PSA_WANT_ECC_SECP_K1_224) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 410 | case 224: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | return MBEDTLS_ECP_DP_SECP224K1; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 412 | #endif | 
|  | 413 | #if defined(PSA_WANT_ECC_SECP_K1_256) | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 414 | case 256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | return MBEDTLS_ECP_DP_SECP256K1; | 
| Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 416 | #endif | 
| Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 417 | } | 
|  | 418 | break; | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 419 | } | 
| Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 420 |  | 
| Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 421 | (void) bits_is_sloppy; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 422 | return MBEDTLS_ECP_DP_NONE; | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 423 | } | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 424 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || | 
| Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 425 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || | 
|  | 426 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || | 
|  | 427 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || | 
|  | 428 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 429 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type, | 
|  | 431 | size_t bits) | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 432 | { | 
|  | 433 | /* Check that the bit size is acceptable for the key type */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | switch (type) { | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 435 | case PSA_KEY_TYPE_RAW_DATA: | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 436 | case PSA_KEY_TYPE_HMAC: | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 437 | case PSA_KEY_TYPE_DERIVE: | 
| Neil Armstrong | 4b5710f | 2022-05-25 11:30:27 +0200 | [diff] [blame] | 438 | case PSA_KEY_TYPE_PASSWORD: | 
|  | 439 | case PSA_KEY_TYPE_PASSWORD_HASH: | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 440 | break; | 
| Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 441 | #if defined(PSA_WANT_KEY_TYPE_AES) | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 442 | case PSA_KEY_TYPE_AES: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | if (bits != 128 && bits != 192 && bits != 256) { | 
|  | 444 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 445 | } | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | break; | 
|  | 447 | #endif | 
| Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 448 | #if defined(PSA_WANT_KEY_TYPE_ARIA) | 
|  | 449 | case PSA_KEY_TYPE_ARIA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | if (bits != 128 && bits != 192 && bits != 256) { | 
|  | 451 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 452 | } | 
| Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 453 | break; | 
|  | 454 | #endif | 
| Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 455 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 456 | case PSA_KEY_TYPE_CAMELLIA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 457 | if (bits != 128 && bits != 192 && bits != 256) { | 
|  | 458 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 459 | } | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 460 | break; | 
|  | 461 | #endif | 
| Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 462 | #if defined(PSA_WANT_KEY_TYPE_DES) | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 463 | case PSA_KEY_TYPE_DES: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | if (bits != 64 && bits != 128 && bits != 192) { | 
|  | 465 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 466 | } | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 467 | break; | 
|  | 468 | #endif | 
| Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 469 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 470 | case PSA_KEY_TYPE_CHACHA20: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | if (bits != 256) { | 
|  | 472 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 473 | } | 
| Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 474 | break; | 
|  | 475 | #endif | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 476 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 478 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | if (bits % 8 != 0) { | 
|  | 480 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 481 | } | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 482 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | return PSA_SUCCESS; | 
| Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 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, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | psa_key_type_t key_type) | 
| Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 503 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | if (PSA_ALG_IS_HMAC(algorithm)) { | 
|  | 505 | if (key_type == PSA_KEY_TYPE_HMAC) { | 
|  | 506 | return PSA_SUCCESS; | 
| Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 507 | } | 
| 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 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { | 
|  | 511 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher | 
|  | 512 | * key. */ | 
|  | 513 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == | 
|  | 514 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { | 
|  | 515 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and | 
|  | 516 | * the block length (larger than 1) for block ciphers. */ | 
|  | 517 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { | 
|  | 518 | return PSA_SUCCESS; | 
|  | 519 | } | 
|  | 520 | } | 
|  | 521 | } | 
|  | 522 |  | 
|  | 523 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 524 | } | 
|  | 525 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 526 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, | 
|  | 527 | size_t buffer_length) | 
| Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 528 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | if (slot->key.data != NULL) { | 
|  | 530 | return PSA_ERROR_ALREADY_EXISTS; | 
|  | 531 | } | 
| Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 532 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | slot->key.data = mbedtls_calloc(1, buffer_length); | 
|  | 534 | if (slot->key.data == NULL) { | 
|  | 535 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 536 | } | 
| Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 537 |  | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 538 | slot->key.bytes = buffer_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | return PSA_SUCCESS; | 
| Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 540 | } | 
|  | 541 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, | 
|  | 543 | const uint8_t *data, | 
|  | 544 | size_t data_length) | 
| Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 545 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | psa_status_t status = psa_allocate_buffer_to_slot(slot, | 
|  | 547 | data_length); | 
|  | 548 | if (status != PSA_SUCCESS) { | 
|  | 549 | return status; | 
|  | 550 | } | 
| Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 551 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | memcpy(slot->key.data, data, data_length); | 
|  | 553 | return PSA_SUCCESS; | 
| Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 554 | } | 
|  | 555 |  | 
| Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 556 | psa_status_t psa_import_key_into_slot( | 
| Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 557 | const psa_key_attributes_t *attributes, | 
|  | 558 | const uint8_t *data, size_t data_length, | 
|  | 559 | uint8_t *key_buffer, size_t key_buffer_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | size_t *key_buffer_length, size_t *bits) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 561 | { | 
| Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 562 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 563 | psa_key_type_t type = attributes->core.type; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 564 |  | 
| Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 565 | /* zero-length keys are never supported. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 566 | if (data_length == 0) { | 
|  | 567 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 568 | } | 
| Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 569 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | if (key_type_is_raw_bytes(type)) { | 
|  | 571 | *bits = PSA_BYTES_TO_BITS(data_length); | 
| Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 572 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 573 | status = psa_validate_unstructured_key_bit_size(attributes->core.type, | 
|  | 574 | *bits); | 
|  | 575 | if (status != PSA_SUCCESS) { | 
|  | 576 | return status; | 
|  | 577 | } | 
| Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 578 |  | 
| Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 579 | /* Copy the key material. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 580 | memcpy(key_buffer, data, data_length); | 
| Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 581 | *key_buffer_length = data_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | (void) key_buffer_size; | 
| Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 583 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | return PSA_SUCCESS; | 
|  | 585 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 586 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) | 
|  | 588 | if (PSA_KEY_TYPE_IS_ECC(type)) { | 
|  | 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) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) | 
|  | 599 | if (PSA_KEY_TYPE_IS_RSA(type)) { | 
|  | 600 | return mbedtls_psa_rsa_import_key(attributes, | 
|  | 601 | data, data_length, | 
|  | 602 | key_buffer, key_buffer_size, | 
|  | 603 | key_buffer_length, | 
|  | 604 | bits); | 
| Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 605 | } | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 606 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || | 
|  | 607 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ | 
| Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 608 | } | 
| Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 609 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 610 | return PSA_ERROR_NOT_SUPPORTED; | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 611 | } | 
|  | 612 |  | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 613 | /** Calculate the intersection of two algorithm usage policies. | 
|  | 614 | * | 
|  | 615 | * Return 0 (which allows no operation) on incompatibility. | 
|  | 616 | */ | 
|  | 617 | static psa_algorithm_t psa_key_policy_algorithm_intersection( | 
| Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 618 | psa_key_type_t key_type, | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 619 | psa_algorithm_t alg1, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 620 | psa_algorithm_t alg2) | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 621 | { | 
| Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 622 | /* Common case: both sides actually specify the same policy. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | if (alg1 == alg2) { | 
|  | 624 | return alg1; | 
|  | 625 | } | 
| 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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 628 | if (PSA_ALG_IS_SIGN_HASH(alg1) && | 
|  | 629 | PSA_ALG_IS_SIGN_HASH(alg2) && | 
|  | 630 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { | 
|  | 631 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { | 
|  | 632 | return alg2; | 
|  | 633 | } | 
|  | 634 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { | 
|  | 635 | return alg1; | 
|  | 636 | } | 
| Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 637 | } | 
|  | 638 | /* If the policies are from the same AEAD family, check whether | 
| Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 639 | * one of them is a minimum-tag-length wildcard. Calculate the most | 
| Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 640 | * restrictive tag length. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && | 
|  | 642 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == | 
|  | 643 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { | 
|  | 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 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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)) { | 
|  | 651 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( | 
|  | 652 | alg1, restricted_len); | 
| Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 653 | } | 
|  | 654 | /* If only one is a wildcard, return specific algorithm if compatible. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && | 
|  | 656 | (alg1_len <= alg2_len)) { | 
|  | 657 | return alg2; | 
| Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 658 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 659 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && | 
|  | 660 | (alg2_len <= alg1_len)) { | 
|  | 661 | return alg1; | 
| Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 662 | } | 
|  | 663 | } | 
|  | 664 | /* If the policies are from the same MAC family, check whether one | 
|  | 665 | * of them is a minimum-MAC-length policy. Calculate the most | 
|  | 666 | * restrictive tag length. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && | 
|  | 668 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == | 
|  | 669 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { | 
| Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 670 | /* Validate the combination of key type and algorithm. Since the base | 
|  | 671 | * algorithm of alg1 and alg2 are the same, we only need this once. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { | 
|  | 673 | return 0; | 
|  | 674 | } | 
| Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 675 |  | 
| Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 676 | /* Get the (exact or at-least) output lengths for both sides of the | 
|  | 677 | * requested intersection. None of the currently supported algorithms | 
|  | 678 | * have an output length dependent on the actual key size, so setting it | 
|  | 679 | * to a bogus value of 0 is currently OK. | 
|  | 680 | * | 
| Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 681 | * Note that for at-least-this-length wildcard algorithms, the output | 
|  | 682 | * length is set to the shortest allowed length, which allows us to | 
|  | 683 | * calculate the most restrictive tag length for the intersection. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 684 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); | 
|  | 685 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); | 
| Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 686 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; | 
| Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 687 |  | 
| Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 688 | /* If both are wildcards, return most restrictive wildcard */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && | 
|  | 690 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { | 
|  | 691 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); | 
| Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 692 | } | 
| Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 693 |  | 
|  | 694 | /* If only one is an at-least-this-length policy, the intersection would | 
|  | 695 | * be the other (fixed-length) policy as long as said fixed length is | 
|  | 696 | * equal to or larger than the shortest allowed length. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 697 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { | 
|  | 698 | return (alg1_len <= alg2_len) ? alg2 : 0; | 
| Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 699 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { | 
|  | 701 | return (alg2_len <= alg1_len) ? alg1 : 0; | 
| Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 702 | } | 
| Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 703 |  | 
| Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 704 | /* If none of them are wildcards, check whether they define the same tag | 
|  | 705 | * length. This is still possible here when one is default-length and | 
|  | 706 | * the other specific-length. Ensure to always return the | 
|  | 707 | * specific-length version for the intersection. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 708 | if (alg1_len == alg2_len) { | 
|  | 709 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); | 
|  | 710 | } | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 711 | } | 
|  | 712 | /* If the policies are incompatible, allow nothing. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 713 | return 0; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 714 | } | 
|  | 715 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 716 | static int psa_key_algorithm_permits(psa_key_type_t key_type, | 
|  | 717 | psa_algorithm_t policy_alg, | 
|  | 718 | psa_algorithm_t requested_alg) | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 719 | { | 
| Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 720 | /* Common case: the policy only allows requested_alg. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 721 | if (requested_alg == policy_alg) { | 
|  | 722 | return 1; | 
|  | 723 | } | 
| Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 724 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, | 
|  | 725 | * and requested_alg is the same hash-and-sign family with any hash, | 
|  | 726 | * then requested_alg is compliant with policy_alg. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 727 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && | 
|  | 728 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { | 
|  | 729 | return (policy_alg & ~PSA_ALG_HASH_MASK) == | 
|  | 730 | (requested_alg & ~PSA_ALG_HASH_MASK); | 
| Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 731 | } | 
|  | 732 | /* If policy_alg is a wildcard AEAD algorithm of the same base as | 
|  | 733 | * the requested algorithm, check the requested tag length to be | 
|  | 734 | * equal-length or longer than the wildcard-specified length. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 735 | if (PSA_ALG_IS_AEAD(policy_alg) && | 
|  | 736 | PSA_ALG_IS_AEAD(requested_alg) && | 
|  | 737 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == | 
|  | 738 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && | 
|  | 739 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { | 
|  | 740 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= | 
|  | 741 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); | 
| Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 742 | } | 
| Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 743 | /* If policy_alg is a MAC algorithm of the same base as the requested | 
|  | 744 | * algorithm, check whether their MAC lengths are compatible. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 745 | if (PSA_ALG_IS_MAC(policy_alg) && | 
|  | 746 | PSA_ALG_IS_MAC(requested_alg) && | 
|  | 747 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == | 
|  | 748 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { | 
| Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 749 | /* Validate the combination of key type and algorithm. Since the policy | 
|  | 750 | * and requested algorithms are the same, we only need this once. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 751 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { | 
|  | 752 | return 0; | 
|  | 753 | } | 
| Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 754 |  | 
| Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 755 | /* Get both the requested output length for the algorithm which is to be | 
|  | 756 | * verified, and the default output length for the base algorithm. | 
|  | 757 | * Note that none of the currently supported algorithms have an output | 
|  | 758 | * length dependent on actual key size, so setting it to a bogus value | 
|  | 759 | * of 0 is currently OK. */ | 
| Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 760 | size_t requested_output_length = PSA_MAC_LENGTH( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 761 | key_type, 0, requested_alg); | 
| Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 762 | size_t default_output_length = PSA_MAC_LENGTH( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 763 | key_type, 0, | 
|  | 764 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); | 
| Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 765 |  | 
| Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 766 | /* If the policy is default-length, only allow an algorithm with | 
|  | 767 | * a declared exact-length matching the default. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { | 
|  | 769 | return requested_output_length == default_output_length; | 
|  | 770 | } | 
| Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 771 |  | 
|  | 772 | /* If the requested algorithm is default-length, allow it if the policy | 
| Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 773 | * length exactly matches the default length. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 774 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && | 
|  | 775 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { | 
|  | 776 | return 1; | 
| Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 777 | } | 
|  | 778 |  | 
| Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 779 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, | 
|  | 780 | * check for the requested MAC length to be equal to or longer than the | 
|  | 781 | * minimum allowed length. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 782 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { | 
|  | 783 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= | 
|  | 784 | requested_output_length; | 
| Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 785 | } | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 786 | } | 
| Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 787 | /* 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] | 788 | * a key derivation with that key agreement should also be allowed. This | 
|  | 789 | * behaviour is expected to be defined in a future specification version. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 790 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && | 
|  | 791 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { | 
|  | 792 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == | 
|  | 793 | policy_alg; | 
| Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 794 | } | 
| Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 795 | /* If it isn't explicitly permitted, it's forbidden. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 796 | return 0; | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 797 | } | 
|  | 798 |  | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 799 | /** Test whether a policy permits an algorithm. | 
|  | 800 | * | 
|  | 801 | * The caller must test usage flags separately. | 
| Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 802 | * | 
| Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 803 | * \note This function requires providing the key type for which the policy is | 
|  | 804 | *       being validated, since some algorithm policy definitions (e.g. MAC) | 
|  | 805 | *       have different properties depending on what kind of cipher it is | 
|  | 806 | *       combined with. | 
|  | 807 | * | 
| Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 808 | * \retval PSA_SUCCESS                  When \p alg is a specific algorithm | 
|  | 809 | *                                      allowed by the \p policy. | 
|  | 810 | * \retval PSA_ERROR_INVALID_ARGUMENT   When \p alg is not a specific algorithm | 
|  | 811 | * \retval PSA_ERROR_NOT_PERMITTED      When \p alg is a specific algorithm, but | 
|  | 812 | *                                      the \p policy does not allow it. | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 813 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 814 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, | 
|  | 815 | psa_key_type_t key_type, | 
|  | 816 | psa_algorithm_t alg) | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 817 | { | 
| Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 818 | /* '0' is not a valid algorithm */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 819 | if (alg == 0) { | 
|  | 820 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 821 | } | 
| Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 822 |  | 
| Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 823 | /* A requested algorithm cannot be a wildcard. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 824 | if (PSA_ALG_IS_WILDCARD(alg)) { | 
|  | 825 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 826 | } | 
| Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 827 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 828 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || | 
|  | 829 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { | 
|  | 830 | return PSA_SUCCESS; | 
|  | 831 | } else { | 
|  | 832 | return PSA_ERROR_NOT_PERMITTED; | 
|  | 833 | } | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 834 | } | 
|  | 835 |  | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 836 | /** Restrict a key policy based on a constraint. | 
|  | 837 | * | 
| Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 838 | * \note This function requires providing the key type for which the policy is | 
|  | 839 | *       being restricted, since some algorithm policy definitions (e.g. MAC) | 
|  | 840 | *       have different properties depending on what kind of cipher it is | 
|  | 841 | *       combined with. | 
|  | 842 | * | 
| Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 843 | * \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] | 844 | * \param[in,out] policy    The policy to restrict. | 
|  | 845 | * \param[in] constraint    The policy constraint to apply. | 
|  | 846 | * | 
|  | 847 | * \retval #PSA_SUCCESS | 
|  | 848 | *         \c *policy contains the intersection of the original value of | 
|  | 849 | *         \c *policy and \c *constraint. | 
|  | 850 | * \retval #PSA_ERROR_INVALID_ARGUMENT | 
| Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 851 | *         \c key_type, \c *policy and \c *constraint are incompatible. | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 852 | *         \c *policy is unchanged. | 
|  | 853 | */ | 
|  | 854 | static psa_status_t psa_restrict_key_policy( | 
| Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 855 | psa_key_type_t key_type, | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 856 | psa_key_policy_t *policy, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 857 | const psa_key_policy_t *constraint) | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 858 | { | 
|  | 859 | psa_algorithm_t intersection_alg = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 860 | psa_key_policy_algorithm_intersection(key_type, policy->alg, | 
|  | 861 | constraint->alg); | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 862 | psa_algorithm_t intersection_alg2 = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 863 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, | 
|  | 864 | constraint->alg2); | 
|  | 865 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { | 
|  | 866 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 867 | } | 
|  | 868 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { | 
|  | 869 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 870 | } | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 871 | policy->usage &= constraint->usage; | 
|  | 872 | policy->alg = intersection_alg; | 
| Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 873 | policy->alg2 = intersection_alg2; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 874 | return PSA_SUCCESS; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 875 | } | 
|  | 876 |  | 
| Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 877 | psa_status_t psa_get_and_lock_key_slot_with_policy( | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 878 | mbedtls_svc_key_id_t key, | 
|  | 879 | psa_key_slot_t **p_slot, | 
|  | 880 | psa_key_usage_t usage, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 881 | psa_algorithm_t alg) | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 882 | { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 883 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 884 | psa_key_slot_t *slot; | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 885 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 886 | status = psa_get_and_lock_key_slot(key, p_slot); | 
|  | 887 | if (status != PSA_SUCCESS) { | 
|  | 888 | return status; | 
|  | 889 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 890 | slot = *p_slot; | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 891 |  | 
|  | 892 | /* Enforce that usage policy for the key slot contains all the flags | 
|  | 893 | * required by the usage parameter. There is one exception: public | 
|  | 894 | * keys can always be exported, so we treat public key objects as | 
|  | 895 | * if they had the export flag. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 896 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 897 | usage &= ~PSA_KEY_USAGE_EXPORT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 898 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 899 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 900 | if ((slot->attr.policy.usage & usage) != usage) { | 
| Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 901 | status = PSA_ERROR_NOT_PERMITTED; | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 902 | goto error; | 
| Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 903 | } | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 904 |  | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 905 | /* Enforce that the usage policy permits the requested algorithm. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 906 | if (alg != 0) { | 
|  | 907 | status = psa_key_policy_permits(&slot->attr.policy, | 
|  | 908 | slot->attr.type, | 
|  | 909 | alg); | 
|  | 910 | if (status != PSA_SUCCESS) { | 
| Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 911 | goto error; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 912 | } | 
| Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 913 | } | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 914 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 915 | return PSA_SUCCESS; | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 916 |  | 
|  | 917 | error: | 
|  | 918 | *p_slot = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 919 | psa_unlock_key_slot(slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 920 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 921 | return status; | 
| Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 922 | } | 
| Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 923 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 924 | /** Get a key slot containing a transparent key and lock it. | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 925 | * | 
|  | 926 | * 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] | 927 | * available, as opposed to a key in a secure element and/or to be used | 
|  | 928 | * by a secure element. | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 929 | * | 
| Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 930 | * This is a temporary function that may be used instead of | 
|  | 931 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support | 
|  | 932 | * for a cryptographic operation. | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 933 | * | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 934 | * On success, the returned key slot is locked. It is the responsibility of the | 
|  | 935 | * 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] | 936 | */ | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 937 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( | 
|  | 938 | mbedtls_svc_key_id_t key, | 
|  | 939 | psa_key_slot_t **p_slot, | 
|  | 940 | psa_key_usage_t usage, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 941 | psa_algorithm_t alg) | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 942 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 943 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, | 
|  | 944 | usage, alg); | 
|  | 945 | if (status != PSA_SUCCESS) { | 
|  | 946 | return status; | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 947 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 948 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { | 
|  | 950 | psa_unlock_key_slot(*p_slot); | 
|  | 951 | *p_slot = NULL; | 
|  | 952 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 953 | } | 
|  | 954 |  | 
|  | 955 | return PSA_SUCCESS; | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 956 | } | 
| Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 957 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 958 | 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] | 959 | { | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 960 | /* Data pointer will always be either a valid pointer or NULL in an | 
|  | 961 | * initialized slot, so we can just free it. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | if (slot->key.data != NULL) { | 
|  | 963 | mbedtls_platform_zeroize(slot->key.data, slot->key.bytes); | 
|  | 964 | } | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 965 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 966 | mbedtls_free(slot->key.data); | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 967 | slot->key.data = NULL; | 
|  | 968 | slot->key.bytes = 0; | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 969 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 970 | return PSA_SUCCESS; | 
| Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 971 | } | 
|  | 972 |  | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 973 | /** Completely wipe a slot in memory, including its policy. | 
|  | 974 | * Persistent storage is not affected. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 975 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 976 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | psa_status_t status = psa_remove_key_data_from_memory(slot); | 
| Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 978 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | /* | 
|  | 980 | * As the return error code may not be handled in case of multiple errors, | 
|  | 981 | * do our best to report an unexpected lock counter. Assert with | 
|  | 982 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one: | 
|  | 983 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the | 
|  | 984 | * function is called as part of the execution of a test suite, the | 
|  | 985 | * execution of the test suite is stopped in error if the assertion fails. | 
|  | 986 | */ | 
|  | 987 | if (slot->lock_count != 1) { | 
|  | 988 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1); | 
| Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 989 | status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 990 | } | 
|  | 991 |  | 
| Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 992 | /* Multipart operations may still be using the key. This is safe | 
|  | 993 | * because all multipart operation objects are independent from | 
|  | 994 | * the key slot: if they need to access the key after the setup | 
|  | 995 | * phase, they have a copy of the key. Note that this means that | 
|  | 996 | * key material can linger until all operations are completed. */ | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 997 | /* At this point, key material and other type-specific content has | 
|  | 998 | * been wiped. Clear remaining metadata. We can call memset and not | 
|  | 999 | * zeroize because the metadata is not particularly sensitive. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1000 | memset(slot, 0, sizeof(*slot)); | 
|  | 1001 | return status; | 
| Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1002 | } | 
|  | 1003 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1004 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1005 | { | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1006 | psa_key_slot_t *slot; | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1007 | psa_status_t status; /* status of the last operation */ | 
|  | 1008 | psa_status_t overall_status = PSA_SUCCESS; | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1009 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1010 | psa_se_drv_table_entry_t *driver; | 
|  | 1011 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1012 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1013 | if (mbedtls_svc_key_id_is_null(key)) { | 
|  | 1014 | return PSA_SUCCESS; | 
|  | 1015 | } | 
| Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1016 |  | 
| Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1017 | /* | 
| Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1018 | * 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] | 1019 | * key, this will load the key description from persistent memory if not | 
|  | 1020 | * done yet. We cannot avoid this loading as without it we don't know if | 
|  | 1021 | * the key is operated by an SE or not and this information is needed by | 
|  | 1022 | * the current implementation. | 
|  | 1023 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1024 | status = psa_get_and_lock_key_slot(key, &slot); | 
|  | 1025 | if (status != PSA_SUCCESS) { | 
|  | 1026 | return status; | 
|  | 1027 | } | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1028 |  | 
| Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1029 | /* | 
|  | 1030 | * If the key slot containing the key description is under access by the | 
|  | 1031 | * library (apart from the present access), the key cannot be destroyed | 
|  | 1032 | * yet. For the time being, just return in error. Eventually (to be | 
|  | 1033 | * implemented), the key should be destroyed when all accesses have | 
|  | 1034 | * stopped. | 
|  | 1035 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1036 | if (slot->lock_count > 1) { | 
|  | 1037 | psa_unlock_key_slot(slot); | 
|  | 1038 | return PSA_ERROR_GENERIC_ERROR; | 
| Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1039 | } | 
|  | 1040 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1041 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { | 
| Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1042 | /* Refuse the destruction of a read-only key (which may or may not work | 
|  | 1043 | * if we attempt it, depending on whether the key is merely read-only | 
|  | 1044 | * by policy or actually physically read-only). | 
| Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1045 | * Just do the best we can, which is to wipe the copy in memory | 
|  | 1046 | * (done in this function's cleanup code). */ | 
|  | 1047 | overall_status = PSA_ERROR_NOT_PERMITTED; | 
|  | 1048 | goto exit; | 
| Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1049 | } | 
|  | 1050 |  | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1051 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1052 | driver = psa_get_se_driver_entry(slot->attr.lifetime); | 
|  | 1053 | if (driver != NULL) { | 
| Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1054 | /* For a key in a secure element, we need to do three things: | 
|  | 1055 | * remove the key file in internal storage, destroy the | 
|  | 1056 | * key inside the secure element, and update the driver's | 
|  | 1057 | * persistent data. Start a transaction that will encompass these | 
|  | 1058 | * three actions. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1059 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1060 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1061 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); | 
| Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1062 | psa_crypto_transaction.key.id = slot->attr.id; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | status = psa_crypto_save_transaction(); | 
|  | 1064 | if (status != PSA_SUCCESS) { | 
|  | 1065 | (void) psa_crypto_stop_transaction(); | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1066 | /* We should still try to destroy the key in the secure | 
|  | 1067 | * element and the key metadata in storage. This is especially | 
|  | 1068 | * important if the error is that the storage is full. | 
|  | 1069 | * But how to do it exactly without risking an inconsistent | 
|  | 1070 | * state after a reset? | 
|  | 1071 | * https://github.com/ARMmbed/mbed-crypto/issues/215 | 
|  | 1072 | */ | 
|  | 1073 | overall_status = status; | 
|  | 1074 | goto exit; | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1075 | } | 
|  | 1076 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1077 | status = psa_destroy_se_key(driver, | 
|  | 1078 | psa_key_slot_get_slot_number(slot)); | 
|  | 1079 | if (overall_status == PSA_SUCCESS) { | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1080 | overall_status = status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | } | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1082 | } | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1083 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1084 |  | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1085 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1086 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { | 
|  | 1087 | status = psa_destroy_persistent_key(slot->attr.id); | 
|  | 1088 | if (overall_status == PSA_SUCCESS) { | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1089 | overall_status = status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1090 | } | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1091 |  | 
| Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1092 | /* TODO: other slots may have a copy of the same key. We should | 
|  | 1093 | * invalidate them. | 
|  | 1094 | * https://github.com/ARMmbed/mbed-crypto/issues/214 | 
|  | 1095 | */ | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1096 | } | 
|  | 1097 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ | 
| Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1098 |  | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1099 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1100 | if (driver != NULL) { | 
|  | 1101 | status = psa_save_se_persistent_data(driver); | 
|  | 1102 | if (overall_status == PSA_SUCCESS) { | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1103 | overall_status = status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1104 | } | 
|  | 1105 | status = psa_crypto_stop_transaction(); | 
|  | 1106 | if (overall_status == PSA_SUCCESS) { | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1107 | overall_status = status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1108 | } | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1109 | } | 
|  | 1110 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1111 |  | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1112 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1113 | status = psa_wipe_key_slot(slot); | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1114 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1115 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1116 | overall_status = status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1117 | } | 
|  | 1118 | return overall_status; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1119 | } | 
|  | 1120 |  | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1121 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ | 
| John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1122 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1123 | static psa_status_t psa_get_rsa_public_exponent( | 
|  | 1124 | const mbedtls_rsa_context *rsa, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1125 | psa_key_attributes_t *attributes) | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1126 | { | 
|  | 1127 | mbedtls_mpi mpi; | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1128 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1129 | uint8_t *buffer = NULL; | 
|  | 1130 | size_t buflen; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | mbedtls_mpi_init(&mpi); | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1132 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1133 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); | 
|  | 1134 | if (ret != 0) { | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1135 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1136 | } | 
|  | 1137 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { | 
| Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1138 | /* It's the default value, which is reported as an empty string, | 
|  | 1139 | * so there's nothing to do. */ | 
|  | 1140 | goto exit; | 
|  | 1141 | } | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1142 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1143 | buflen = mbedtls_mpi_size(&mpi); | 
|  | 1144 | buffer = mbedtls_calloc(1, buflen); | 
|  | 1145 | if (buffer == NULL) { | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1146 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; | 
|  | 1147 | goto exit; | 
|  | 1148 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1149 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); | 
|  | 1150 | if (ret != 0) { | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1151 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1152 | } | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1153 | attributes->domain_parameters = buffer; | 
|  | 1154 | attributes->domain_parameters_size = buflen; | 
|  | 1155 |  | 
|  | 1156 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1157 | mbedtls_mpi_free(&mpi); | 
|  | 1158 | if (ret != 0) { | 
|  | 1159 | mbedtls_free(buffer); | 
|  | 1160 | } | 
|  | 1161 | return mbedtls_to_psa_error(ret); | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1162 | } | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1163 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1164 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1165 |  | 
| Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1166 | /** Retrieve all the publicly-accessible attributes of a key. | 
|  | 1167 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1168 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, | 
|  | 1169 | psa_key_attributes_t *attributes) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1170 | { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1171 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1172 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1173 | psa_key_slot_t *slot; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1174 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1175 | psa_reset_key_attributes(attributes); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1176 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1177 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); | 
|  | 1178 | if (status != PSA_SUCCESS) { | 
|  | 1179 | return status; | 
|  | 1180 | } | 
| Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1181 |  | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1182 | attributes->core = slot->attr; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1183 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | | 
|  | 1184 | MBEDTLS_PSA_KA_MASK_DUAL_USE); | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1185 |  | 
|  | 1186 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1187 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { | 
|  | 1188 | psa_set_key_slot_number(attributes, | 
|  | 1189 | psa_key_slot_get_slot_number(slot)); | 
|  | 1190 | } | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1191 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1192 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | switch (slot->attr.type) { | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1194 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ | 
| John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1195 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1196 | case PSA_KEY_TYPE_RSA_KEY_PAIR: | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1197 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: | 
| Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1198 | /* TODO: reporting the public exponent for opaque keys | 
| Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1199 | * is not yet implemented. | 
|  | 1200 | * https://github.com/ARMmbed/mbed-crypto/issues/216 | 
|  | 1201 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1202 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { | 
| Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1203 | mbedtls_rsa_context *rsa = NULL; | 
| Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1204 |  | 
| Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1205 | status = mbedtls_psa_rsa_load_representation( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1206 | slot->attr.type, | 
|  | 1207 | slot->key.data, | 
|  | 1208 | slot->key.bytes, | 
|  | 1209 | &rsa); | 
|  | 1210 | if (status != PSA_SUCCESS) { | 
| Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1211 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | } | 
| Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1213 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1214 | status = psa_get_rsa_public_exponent(rsa, | 
|  | 1215 | attributes); | 
|  | 1216 | mbedtls_rsa_free(rsa); | 
|  | 1217 | mbedtls_free(rsa); | 
| Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1218 | } | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1219 | break; | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1220 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1221 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ | 
| Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1222 | default: | 
|  | 1223 | /* Nothing else to do. */ | 
|  | 1224 | break; | 
|  | 1225 | } | 
|  | 1226 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1227 | if (status != PSA_SUCCESS) { | 
|  | 1228 | psa_reset_key_attributes(attributes); | 
|  | 1229 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1230 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1231 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1232 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1233 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1234 | } | 
|  | 1235 |  | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1236 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1237 | psa_status_t psa_get_key_slot_number( | 
|  | 1238 | const psa_key_attributes_t *attributes, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1239 | psa_key_slot_number_t *slot_number) | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1240 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1241 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1242 | *slot_number = attributes->slot_number; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1243 | return PSA_SUCCESS; | 
|  | 1244 | } else { | 
|  | 1245 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1246 | } | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1247 | } | 
|  | 1248 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1249 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1250 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, | 
|  | 1251 | size_t key_buffer_size, | 
|  | 1252 | uint8_t *data, | 
|  | 1253 | size_t data_size, | 
|  | 1254 | size_t *data_length) | 
| Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1255 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1256 | if (key_buffer_size > data_size) { | 
|  | 1257 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 1258 | } | 
|  | 1259 | memcpy(data, key_buffer, key_buffer_size); | 
|  | 1260 | memset(data + key_buffer_size, 0, | 
|  | 1261 | data_size - key_buffer_size); | 
| Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1262 | *data_length = key_buffer_size; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1263 | return PSA_SUCCESS; | 
| Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1264 | } | 
|  | 1265 |  | 
| Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1266 | psa_status_t psa_export_key_internal( | 
| Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1267 | const psa_key_attributes_t *attributes, | 
|  | 1268 | const uint8_t *key_buffer, size_t key_buffer_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1269 | uint8_t *data, size_t data_size, size_t *data_length) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1270 | { | 
| Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1271 | psa_key_type_t type = attributes->core.type; | 
|  | 1272 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1273 | if (key_type_is_raw_bytes(type) || | 
|  | 1274 | PSA_KEY_TYPE_IS_RSA(type)   || | 
|  | 1275 | PSA_KEY_TYPE_IS_ECC(type)) { | 
|  | 1276 | return psa_export_key_buffer_internal( | 
|  | 1277 | key_buffer, key_buffer_size, | 
|  | 1278 | data, data_size, data_length); | 
|  | 1279 | } else { | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1280 | /* This shouldn't happen in the reference implementation, but | 
|  | 1281 | it is valid for a special-purpose implementation to omit | 
|  | 1282 | support for exporting certain key types. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1283 | return PSA_ERROR_NOT_SUPPORTED; | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1284 | } | 
|  | 1285 | } | 
|  | 1286 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1287 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, | 
|  | 1288 | uint8_t *data, | 
|  | 1289 | size_t data_size, | 
|  | 1290 | size_t *data_length) | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1291 | { | 
|  | 1292 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 1293 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 1294 | psa_key_slot_t *slot; | 
|  | 1295 |  | 
| Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1296 | /* Reject a zero-length output buffer now, since this can never be a | 
|  | 1297 | * valid key representation. This way we know that data must be a valid | 
|  | 1298 | * pointer and we can do things like memset(data, ..., data_size). */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1299 | if (data_size == 0) { | 
|  | 1300 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 1301 | } | 
| Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1302 |  | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1303 | /* Set the key to empty now, so that even when there are errors, we always | 
|  | 1304 | * set data_length to a value between 0 and data_size. On error, setting | 
|  | 1305 | * the key to empty is a good choice because an empty key representation is | 
|  | 1306 | * unlikely to be accepted anywhere. */ | 
| Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1307 | *data_length = 0; | 
|  | 1308 |  | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1309 | /* Export requires the EXPORT flag. There is an exception for public keys, | 
|  | 1310 | * which don't require any flag, but | 
|  | 1311 | * psa_get_and_lock_key_slot_with_policy() takes care of this. | 
|  | 1312 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1313 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, | 
|  | 1314 | PSA_KEY_USAGE_EXPORT, 0); | 
|  | 1315 | if (status != PSA_SUCCESS) { | 
|  | 1316 | return status; | 
|  | 1317 | } | 
| mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1318 |  | 
| Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1319 | psa_key_attributes_t attributes = { | 
|  | 1320 | .core = slot->attr | 
|  | 1321 | }; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1322 | status = psa_driver_wrapper_export_key(&attributes, | 
|  | 1323 | slot->key.data, slot->key.bytes, | 
|  | 1324 | data, data_size, data_length); | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1325 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1326 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1327 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1328 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1329 | } | 
|  | 1330 |  | 
| Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1331 | psa_status_t psa_export_public_key_internal( | 
| Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1332 | const psa_key_attributes_t *attributes, | 
|  | 1333 | const uint8_t *key_buffer, | 
|  | 1334 | size_t key_buffer_size, | 
|  | 1335 | uint8_t *data, | 
|  | 1336 | size_t data_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1337 | size_t *data_length) | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1338 | { | 
| Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1339 | psa_key_type_t type = attributes->core.type; | 
| Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1340 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1341 | if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) { | 
|  | 1342 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { | 
| Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1343 | /* Exporting public -> public */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1344 | return psa_export_key_buffer_internal( | 
|  | 1345 | key_buffer, key_buffer_size, | 
|  | 1346 | data, data_size, data_length); | 
| Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1347 | } | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1348 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1349 | if (PSA_KEY_TYPE_IS_RSA(type)) { | 
| John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1350 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1351 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) | 
|  | 1352 | return mbedtls_psa_rsa_export_public_key(attributes, | 
|  | 1353 | key_buffer, | 
|  | 1354 | key_buffer_size, | 
|  | 1355 | data, | 
|  | 1356 | data_size, | 
|  | 1357 | data_length); | 
| Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1358 | #else | 
| Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1359 | /* We don't know how to convert a private RSA key to public. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1360 | return PSA_ERROR_NOT_SUPPORTED; | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1361 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || | 
|  | 1362 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1363 | } else { | 
| John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1364 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1365 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) | 
|  | 1366 | return mbedtls_psa_ecp_export_public_key(attributes, | 
|  | 1367 | key_buffer, | 
|  | 1368 | key_buffer_size, | 
|  | 1369 | data, | 
|  | 1370 | data_size, | 
|  | 1371 | data_length); | 
| Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1372 | #else | 
|  | 1373 | /* We don't know how to convert a private ECC key to public */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | return PSA_ERROR_NOT_SUPPORTED; | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1375 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || | 
|  | 1376 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ | 
| Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1377 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1378 | } else { | 
| Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1379 | /* This shouldn't happen in the reference implementation, but | 
|  | 1380 | it is valid for a special-purpose implementation to omit | 
|  | 1381 | support for exporting certain key types. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1382 | return PSA_ERROR_NOT_SUPPORTED; | 
| Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1383 | } | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1384 | } | 
| Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1385 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1386 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, | 
|  | 1387 | uint8_t *data, | 
|  | 1388 | size_t data_size, | 
|  | 1389 | size_t *data_length) | 
| Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1390 | { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1391 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1392 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1393 | psa_key_slot_t *slot; | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1394 |  | 
| Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1395 | /* Reject a zero-length output buffer now, since this can never be a | 
|  | 1396 | * valid key representation. This way we know that data must be a valid | 
|  | 1397 | * pointer and we can do things like memset(data, ..., data_size). */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1398 | if (data_size == 0) { | 
|  | 1399 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 1400 | } | 
| Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1401 |  | 
| Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1402 | /* Set the key to empty now, so that even when there are errors, we always | 
|  | 1403 | * set data_length to a value between 0 and data_size. On error, setting | 
|  | 1404 | * the key to empty is a good choice because an empty key representation is | 
|  | 1405 | * unlikely to be accepted anywhere. */ | 
|  | 1406 | *data_length = 0; | 
|  | 1407 |  | 
|  | 1408 | /* Exporting a public key doesn't require a usage flag. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1409 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); | 
|  | 1410 | if (status != PSA_SUCCESS) { | 
|  | 1411 | return status; | 
|  | 1412 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1413 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1414 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { | 
|  | 1415 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1416 | goto exit; | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1417 | } | 
|  | 1418 |  | 
| Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1419 | psa_key_attributes_t attributes = { | 
|  | 1420 | .core = slot->attr | 
|  | 1421 | }; | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1422 | status = psa_driver_wrapper_export_public_key( | 
| Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1423 | &attributes, slot->key.data, slot->key.bytes, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1424 | data, data_size, data_length); | 
| Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1425 |  | 
|  | 1426 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1427 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1428 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1429 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1430 | } | 
|  | 1431 |  | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1432 | #if defined(static_assert) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1433 | static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, | 
|  | 1434 | "One or more key attribute flag is listed as both external-only and dual-use"); | 
|  | 1435 | static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, | 
|  | 1436 | "One or more key attribute flag is listed as both internal-only and dual-use"); | 
|  | 1437 | static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, | 
|  | 1438 | "One or more key attribute flag is listed as both internal-only and external-only"); | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1439 | #endif | 
|  | 1440 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1441 | /** Validate that a key policy is internally well-formed. | 
|  | 1442 | * | 
|  | 1443 | * This function only rejects invalid policies. It does not validate the | 
|  | 1444 | * consistency of the policy with respect to other attributes of the key | 
|  | 1445 | * such as the key type. | 
|  | 1446 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1447 | 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] | 1448 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1449 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | | 
|  | 1450 | PSA_KEY_USAGE_COPY | | 
|  | 1451 | PSA_KEY_USAGE_ENCRYPT | | 
|  | 1452 | PSA_KEY_USAGE_DECRYPT | | 
|  | 1453 | PSA_KEY_USAGE_SIGN_MESSAGE | | 
|  | 1454 | PSA_KEY_USAGE_VERIFY_MESSAGE | | 
|  | 1455 | PSA_KEY_USAGE_SIGN_HASH | | 
|  | 1456 | PSA_KEY_USAGE_VERIFY_HASH | | 
|  | 1457 | PSA_KEY_USAGE_VERIFY_DERIVATION | | 
|  | 1458 | PSA_KEY_USAGE_DERIVE)) != 0) { | 
|  | 1459 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1460 | } | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1461 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1462 | return PSA_SUCCESS; | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1463 | } | 
|  | 1464 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1465 | /** Validate the internal consistency of key attributes. | 
|  | 1466 | * | 
|  | 1467 | * This function only rejects invalid attribute values. If does not | 
|  | 1468 | * validate the consistency of the attributes with any key data that may | 
|  | 1469 | * be involved in the creation of the key. | 
|  | 1470 | * | 
|  | 1471 | * Call this function early in the key creation process. | 
|  | 1472 | * | 
|  | 1473 | * \param[in] attributes    Key attributes for the new key. | 
|  | 1474 | * \param[out] p_drv        On any return, the driver for the key, if any. | 
|  | 1475 | *                          NULL for a transparent key. | 
|  | 1476 | * | 
|  | 1477 | */ | 
|  | 1478 | static psa_status_t psa_validate_key_attributes( | 
|  | 1479 | const psa_key_attributes_t *attributes, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1480 | psa_se_drv_table_entry_t **p_drv) | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1481 | { | 
| Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1482 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1483 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); | 
|  | 1484 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1485 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1486 | status = psa_validate_key_location(lifetime, p_drv); | 
|  | 1487 | if (status != PSA_SUCCESS) { | 
|  | 1488 | return status; | 
| Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1489 | } | 
|  | 1490 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1491 | status = psa_validate_key_persistence(lifetime); | 
|  | 1492 | if (status != PSA_SUCCESS) { | 
|  | 1493 | return status; | 
|  | 1494 | } | 
|  | 1495 |  | 
|  | 1496 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { | 
|  | 1497 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { | 
|  | 1498 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1499 | } | 
|  | 1500 | } else { | 
|  | 1501 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { | 
|  | 1502 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1503 | } | 
|  | 1504 | } | 
|  | 1505 |  | 
|  | 1506 | status = psa_validate_key_policy(&attributes->core.policy); | 
|  | 1507 | if (status != PSA_SUCCESS) { | 
|  | 1508 | return status; | 
|  | 1509 | } | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1510 |  | 
|  | 1511 | /* Refuse to create overly large keys. | 
|  | 1512 | * Note that this doesn't trigger on import if the attributes don't | 
|  | 1513 | * explicitly specify a size (so psa_get_key_bits returns 0), so | 
|  | 1514 | * psa_import_key() needs its own checks. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1515 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { | 
|  | 1516 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 1517 | } | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1518 |  | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1519 | /* Reject invalid flags. These should not be reachable through the API. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1520 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | | 
|  | 1521 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { | 
|  | 1522 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1523 | } | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1524 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1525 | return PSA_SUCCESS; | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1526 | } | 
|  | 1527 |  | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1528 | /** Prepare a key slot to receive key material. | 
|  | 1529 | * | 
|  | 1530 | * This function allocates a key slot and sets its metadata. | 
|  | 1531 | * | 
|  | 1532 | * If this function fails, call psa_fail_key_creation(). | 
|  | 1533 | * | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1534 | * This function is intended to be used as follows: | 
|  | 1535 | * -# Call psa_start_key_creation() to allocate a key slot, prepare | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1536 | *    it with the specified attributes, and in case of a volatile key assign it | 
|  | 1537 | *    a volatile key identifier. | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1538 | * -# Populate the slot with the key material. | 
|  | 1539 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. | 
|  | 1540 | * In case of failure at any step, stop the sequence and call | 
|  | 1541 | * psa_fail_key_creation(). | 
|  | 1542 | * | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1543 | * On success, the key slot is locked. It is the responsibility of the caller | 
|  | 1544 | * to unlock the key slot when it does not access it anymore. | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1545 | * | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1546 | * \param method            An identification of the calling function. | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1547 | * \param[in] attributes    Key attributes for the new key. | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1548 | * \param[out] p_slot       On success, a pointer to the prepared slot. | 
|  | 1549 | * \param[out] p_drv        On any return, the driver for the key, if any. | 
|  | 1550 | *                          NULL for a transparent key. | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1551 | * | 
|  | 1552 | * \retval #PSA_SUCCESS | 
|  | 1553 | *         The key slot is ready to receive key material. | 
|  | 1554 | * \return If this function fails, the key slot is an invalid state. | 
|  | 1555 | *         You must call psa_fail_key_creation() to wipe and free the slot. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1556 | */ | 
|  | 1557 | static psa_status_t psa_start_key_creation( | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1558 | psa_key_creation_method_t method, | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1559 | const psa_key_attributes_t *attributes, | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1560 | psa_key_slot_t **p_slot, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1561 | psa_se_drv_table_entry_t **p_drv) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1562 | { | 
|  | 1563 | psa_status_t status; | 
| Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1564 | psa_key_id_t volatile_key_id; | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1565 | psa_key_slot_t *slot; | 
|  | 1566 |  | 
| Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1567 | (void) method; | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1568 | *p_drv = NULL; | 
|  | 1569 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1570 | status = psa_validate_key_attributes(attributes, p_drv); | 
|  | 1571 | if (status != PSA_SUCCESS) { | 
|  | 1572 | return status; | 
|  | 1573 | } | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1574 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1575 | status = psa_get_empty_key_slot(&volatile_key_id, p_slot); | 
|  | 1576 | if (status != PSA_SUCCESS) { | 
|  | 1577 | return status; | 
|  | 1578 | } | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1579 | slot = *p_slot; | 
|  | 1580 |  | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1581 | /* We're storing the declared bit-size of the key. It's up to each | 
|  | 1582 | * creation mechanism to verify that this information is correct. | 
|  | 1583 | * It's automatically correct for mechanisms that use the bit-size as | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1584 | * an input (generate, device) but not for those where the bit-size | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1585 | * is optional (import, copy). In case of a volatile key, assign it the | 
|  | 1586 | * volatile key identifier associated to the slot returned to contain its | 
|  | 1587 | * definition. */ | 
| Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1588 |  | 
|  | 1589 | slot->attr = attributes->core; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1590 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { | 
| Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1591 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) | 
|  | 1592 | slot->attr.id = volatile_key_id; | 
|  | 1593 | #else | 
|  | 1594 | slot->attr.id.key_id = volatile_key_id; | 
|  | 1595 | #endif | 
|  | 1596 | } | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1597 |  | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1598 | /* Erase external-only flags from the internal copy. To access | 
| Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1599 | * external-only flags, query `attributes`. Thanks to the check | 
|  | 1600 | * in psa_validate_key_attributes(), this leaves the dual-use | 
| Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1601 | * flags and any internal flag that psa_get_empty_key_slot() | 
| Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1602 | * may have set. */ | 
|  | 1603 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; | 
| Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1604 |  | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1605 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1606 | /* 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] | 1607 | * when creating or registering a persistent key: | 
| Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1608 | * create the key file in internal storage, create the | 
|  | 1609 | * key inside the secure element, and update the driver's | 
| Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1610 | * persistent data. This is done by starting a transaction that will | 
|  | 1611 | * encompass these three actions. | 
|  | 1612 | * For registering a volatile key, we just need to find an appropriate | 
|  | 1613 | * slot number inside the SE. Since the key is designated volatile, creating | 
|  | 1614 | * a transaction is not required. */ | 
| Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1615 | /* The first thing to do is to find a slot number for the new key. | 
|  | 1616 | * We save the slot number in persistent storage as part of the | 
|  | 1617 | * transaction data. It will be needed to recover if the power | 
|  | 1618 | * fails during the key creation process, to clean up on the secure | 
|  | 1619 | * element side after restarting. Obtaining a slot number from the | 
|  | 1620 | * secure element driver updates its persistent state, but we do not yet | 
|  | 1621 | * save the driver's persistent state, so that if the power fails, | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1622 | * we can roll back to a state where the key doesn't exist. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1623 | if (*p_drv != NULL) { | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1624 | psa_key_slot_number_t slot_number; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1625 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, | 
|  | 1626 | &slot_number); | 
|  | 1627 | if (status != PSA_SUCCESS) { | 
|  | 1628 | return status; | 
|  | 1629 | } | 
| Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1630 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1631 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { | 
|  | 1632 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); | 
| Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1633 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1634 | psa_crypto_transaction.key.slot = slot_number; | 
| Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1635 | psa_crypto_transaction.key.id = slot->attr.id; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1636 | status = psa_crypto_save_transaction(); | 
|  | 1637 | if (status != PSA_SUCCESS) { | 
|  | 1638 | (void) psa_crypto_stop_transaction(); | 
|  | 1639 | return status; | 
| Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1640 | } | 
| Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1641 | } | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1642 |  | 
|  | 1643 | status = psa_copy_key_material_into_slot( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1644 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1645 | } | 
| Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1646 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1647 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { | 
| Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1648 | /* Key registration only makes sense with a secure element. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1649 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1650 | } | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1651 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1652 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1653 | return PSA_SUCCESS; | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1654 | } | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1655 |  | 
|  | 1656 | /** Finalize the creation of a key once its key material has been set. | 
|  | 1657 | * | 
|  | 1658 | * This entails writing the key to persistent storage. | 
|  | 1659 | * | 
|  | 1660 | * If this function fails, call psa_fail_key_creation(). | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1661 | * See the documentation of psa_start_key_creation() for the intended use | 
|  | 1662 | * of this function. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1663 | * | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1664 | * If the finalization succeeds, the function unlocks the key slot (it was | 
|  | 1665 | * locked by psa_start_key_creation()) and the key slot cannot be accessed | 
|  | 1666 | * anymore as part of the key creation process. | 
| Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1667 | * | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1668 | * \param[in,out] slot  Pointer to the slot with key material. | 
|  | 1669 | * \param[in] driver    The secure element driver for the key, | 
|  | 1670 | *                      or NULL for a transparent key. | 
| Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1671 | * \param[out] key      On success, identifier of the key. Note that the | 
|  | 1672 | *                      key identifier is also stored in the key slot. | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1673 | * | 
|  | 1674 | * \retval #PSA_SUCCESS | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1675 | *         The key was successfully created. | 
| gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1676 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY | 
|  | 1677 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE | 
|  | 1678 | * \retval #PSA_ERROR_ALREADY_EXISTS | 
|  | 1679 | * \retval #PSA_ERROR_DATA_INVALID | 
|  | 1680 | * \retval #PSA_ERROR_DATA_CORRUPT | 
| gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1681 | * \retval #PSA_ERROR_STORAGE_FAILURE | 
| gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1682 | * | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1683 | * \return If this function fails, the key slot is an invalid state. | 
|  | 1684 | *         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] | 1685 | */ | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1686 | static psa_status_t psa_finish_key_creation( | 
|  | 1687 | psa_key_slot_t *slot, | 
| Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1688 | psa_se_drv_table_entry_t *driver, | 
|  | 1689 | mbedtls_svc_key_id_t *key) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1690 | { | 
|  | 1691 | psa_status_t status = PSA_SUCCESS; | 
| Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1692 | (void) slot; | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1693 | (void) driver; | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1694 |  | 
|  | 1695 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1696 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1697 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1698 | if (driver != NULL) { | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1699 | psa_se_key_data_storage_t data; | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1700 | psa_key_slot_number_t slot_number = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1701 | psa_key_slot_get_slot_number(slot); | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1702 |  | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1703 | #if defined(static_assert) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1704 | static_assert(sizeof(slot_number) == | 
|  | 1705 | sizeof(data.slot_number), | 
|  | 1706 | "Slot number size does not match psa_se_key_data_storage_t"); | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1707 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1708 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); | 
|  | 1709 | status = psa_save_persistent_key(&slot->attr, | 
|  | 1710 | (uint8_t *) &data, | 
|  | 1711 | sizeof(data)); | 
|  | 1712 | } else | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1713 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1714 | { | 
| Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1715 | /* Key material is saved in export representation in the slot, so | 
|  | 1716 | * just pass the slot buffer for storage. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1717 | status = psa_save_persistent_key(&slot->attr, | 
|  | 1718 | slot->key.data, | 
|  | 1719 | slot->key.bytes); | 
| Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1720 | } | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1721 | } | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1722 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ | 
|  | 1723 |  | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1724 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1725 | /* Finish the transaction for a key creation. This does not | 
|  | 1726 | * happen when registering an existing key. Detect this case | 
|  | 1727 | * by checking whether a transaction is in progress (actual | 
| Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1728 | * creation of a persistent key in a secure element requires a transaction, | 
|  | 1729 | * but registration or volatile key creation doesn't use one). */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1730 | if (driver != NULL && | 
|  | 1731 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { | 
|  | 1732 | status = psa_save_se_persistent_data(driver); | 
|  | 1733 | if (status != PSA_SUCCESS) { | 
|  | 1734 | psa_destroy_persistent_key(slot->attr.id); | 
|  | 1735 | return status; | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1736 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1737 | status = psa_crypto_stop_transaction(); | 
| Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1738 | } | 
|  | 1739 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1740 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1741 | if (status == PSA_SUCCESS) { | 
| Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1742 | *key = slot->attr.id; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1743 | status = psa_unlock_key_slot(slot); | 
|  | 1744 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1745 | *key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1746 | } | 
| Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1747 | } | 
| Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1748 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1749 | return status; | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1750 | } | 
|  | 1751 |  | 
|  | 1752 | /** Abort the creation of a key. | 
|  | 1753 | * | 
|  | 1754 | * You may call this function after calling psa_start_key_creation(), | 
|  | 1755 | * or after psa_finish_key_creation() fails. In other circumstances, this | 
|  | 1756 | * function may not clean up persistent storage. | 
| Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1757 | * See the documentation of psa_start_key_creation() for the intended use | 
|  | 1758 | * of this function. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1759 | * | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1760 | * \param[in,out] slot  Pointer to the slot with key material. | 
|  | 1761 | * \param[in] driver    The secure element driver for the key, | 
|  | 1762 | *                      or NULL for a transparent key. | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1763 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1764 | static void psa_fail_key_creation(psa_key_slot_t *slot, | 
|  | 1765 | psa_se_drv_table_entry_t *driver) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1766 | { | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1767 | (void) driver; | 
|  | 1768 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | if (slot == NULL) { | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1770 | return; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | } | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1772 |  | 
|  | 1773 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1774 | /* TODO: If the key has already been created in the secure | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1775 | * element, and the failure happened later (when saving metadata | 
|  | 1776 | * to internal storage), we need to destroy the key in the secure | 
| Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1777 | * element. | 
|  | 1778 | * https://github.com/ARMmbed/mbed-crypto/issues/217 | 
|  | 1779 | */ | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1780 |  | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1781 | /* Abort the ongoing transaction if any (there may not be one if | 
|  | 1782 | * the creation process failed before starting one, or if the | 
|  | 1783 | * key creation is a registration of a key in a secure element). | 
|  | 1784 | * Earlier functions must already have done what it takes to undo any | 
|  | 1785 | * partial creation. All that's left is to update the transaction data | 
|  | 1786 | * itself. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1787 | (void) psa_crypto_stop_transaction(); | 
| Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1788 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1789 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1790 | psa_wipe_key_slot(slot); | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1791 | } | 
|  | 1792 |  | 
| Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1793 | /** Validate optional attributes during key creation. | 
|  | 1794 | * | 
|  | 1795 | * Some key attributes are optional during key creation. If they are | 
|  | 1796 | * specified in the attributes structure, check that they are consistent | 
|  | 1797 | * with the data in the slot. | 
|  | 1798 | * | 
|  | 1799 | * This function should be called near the end of key creation, after | 
|  | 1800 | * the slot in memory is fully populated but before saving persistent data. | 
|  | 1801 | */ | 
|  | 1802 | static psa_status_t psa_validate_optional_attributes( | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1803 | const psa_key_slot_t *slot, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1804 | const psa_key_attributes_t *attributes) | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1805 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1806 | if (attributes->core.type != 0) { | 
|  | 1807 | if (attributes->core.type != slot->attr.type) { | 
|  | 1808 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1809 | } | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1810 | } | 
|  | 1811 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1812 | if (attributes->domain_parameters_size != 0) { | 
| John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1813 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1814 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) | 
|  | 1815 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { | 
| Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1816 | mbedtls_rsa_context *rsa = NULL; | 
| Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1817 | mbedtls_mpi actual, required; | 
| Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1818 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1819 |  | 
| Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1820 | psa_status_t status = mbedtls_psa_rsa_load_representation( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1821 | slot->attr.type, | 
|  | 1822 | slot->key.data, | 
|  | 1823 | slot->key.bytes, | 
|  | 1824 | &rsa); | 
|  | 1825 | if (status != PSA_SUCCESS) { | 
|  | 1826 | return status; | 
|  | 1827 | } | 
| Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1828 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1829 | mbedtls_mpi_init(&actual); | 
|  | 1830 | mbedtls_mpi_init(&required); | 
|  | 1831 | ret = mbedtls_rsa_export(rsa, | 
|  | 1832 | NULL, NULL, NULL, NULL, &actual); | 
|  | 1833 | mbedtls_rsa_free(rsa); | 
|  | 1834 | mbedtls_free(rsa); | 
|  | 1835 | if (ret != 0) { | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1836 | goto rsa_exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1837 | } | 
|  | 1838 | ret = mbedtls_mpi_read_binary(&required, | 
|  | 1839 | attributes->domain_parameters, | 
|  | 1840 | attributes->domain_parameters_size); | 
|  | 1841 | if (ret != 0) { | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1842 | goto rsa_exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | } | 
|  | 1844 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1845 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | } | 
|  | 1847 | rsa_exit: | 
|  | 1848 | mbedtls_mpi_free(&actual); | 
|  | 1849 | mbedtls_mpi_free(&required); | 
|  | 1850 | if (ret != 0) { | 
|  | 1851 | return mbedtls_to_psa_error(ret); | 
|  | 1852 | } | 
|  | 1853 | } else | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1854 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || | 
|  | 1855 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1856 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1857 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1858 | } | 
|  | 1859 | } | 
|  | 1860 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1861 | if (attributes->core.bits != 0) { | 
|  | 1862 | if (attributes->core.bits != slot->attr.bits) { | 
|  | 1863 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1864 | } | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1865 | } | 
|  | 1866 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1867 | return PSA_SUCCESS; | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1868 | } | 
|  | 1869 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1870 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, | 
|  | 1871 | const uint8_t *data, | 
|  | 1872 | size_t data_length, | 
|  | 1873 | mbedtls_svc_key_id_t *key) | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1874 | { | 
|  | 1875 | psa_status_t status; | 
|  | 1876 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1877 | psa_se_drv_table_entry_t *driver = NULL; | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1878 | size_t bits; | 
| Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1879 | size_t storage_size = data_length; | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1880 |  | 
| Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1881 | *key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 1882 |  | 
| Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1883 | /* Reject zero-length symmetric keys (including raw data key objects). | 
|  | 1884 | * This also rejects any key which might be encoded as an empty string, | 
|  | 1885 | * which is never valid. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | if (data_length == 0) { | 
|  | 1887 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1888 | } | 
| Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1889 |  | 
| Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1890 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1891 | if (data_length > SIZE_MAX / 8) { | 
|  | 1892 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 1893 | } | 
| Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1894 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1895 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, | 
|  | 1896 | &slot, &driver); | 
|  | 1897 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1898 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | } | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1900 |  | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1901 | /* 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] | 1902 | * storage ( thus not in the case of importing a key in a secure element | 
|  | 1903 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a | 
|  | 1904 | * buffer to hold the imported key material. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1905 | if (slot->key.data == NULL) { | 
|  | 1906 | if (psa_key_lifetime_is_external(attributes->core.lifetime)) { | 
| Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1907 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1908 | attributes, data, data_length, &storage_size); | 
|  | 1909 | if (status != PSA_SUCCESS) { | 
| Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1910 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1911 | } | 
| Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1912 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | status = psa_allocate_buffer_to_slot(slot, storage_size); | 
|  | 1914 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1915 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | } | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1917 | } | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1918 |  | 
|  | 1919 | bits = slot->attr.bits; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1920 | status = psa_driver_wrapper_import_key(attributes, | 
|  | 1921 | data, data_length, | 
|  | 1922 | slot->key.data, | 
|  | 1923 | slot->key.bytes, | 
|  | 1924 | &slot->key.bytes, &bits); | 
|  | 1925 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1926 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | } | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1928 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1929 | if (slot->attr.bits == 0) { | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1930 | slot->attr.bits = (psa_key_bits_t) bits; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1931 | } else if (bits != slot->attr.bits) { | 
| Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1932 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 1933 | goto exit; | 
| Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1934 | } | 
| Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 1935 |  | 
| Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1936 | /* Enforce a size limit, and in particular ensure that the bit | 
|  | 1937 | * size fits in its representation type.*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1938 | if (bits > PSA_MAX_KEY_BITS) { | 
| Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1939 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 1940 | goto exit; | 
|  | 1941 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1942 | status = psa_validate_optional_attributes(slot, attributes); | 
|  | 1943 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1944 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1945 | } | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1946 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1947 | status = psa_finish_key_creation(slot, driver, key); | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1948 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1949 | if (status != PSA_SUCCESS) { | 
|  | 1950 | psa_fail_key_creation(slot, driver); | 
|  | 1951 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1952 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | return status; | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1954 | } | 
|  | 1955 |  | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1956 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 1957 | psa_status_t mbedtls_psa_register_se_key( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1958 | const psa_key_attributes_t *attributes) | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1959 | { | 
|  | 1960 | psa_status_t status; | 
|  | 1961 | psa_key_slot_t *slot = NULL; | 
|  | 1962 | psa_se_drv_table_entry_t *driver = NULL; | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1963 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1964 |  | 
|  | 1965 | /* Leaving attributes unspecified is not currently supported. | 
|  | 1966 | * It could make sense to query the key type and size from the | 
|  | 1967 | * secure element, but not all secure elements support this | 
|  | 1968 | * and the driver HAL doesn't currently support it. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1969 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { | 
|  | 1970 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 1971 | } | 
|  | 1972 | if (psa_get_key_bits(attributes) == 0) { | 
|  | 1973 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 1974 | } | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1975 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1976 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, | 
|  | 1977 | &slot, &driver); | 
|  | 1978 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1979 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1980 | } | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1981 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1982 | status = psa_finish_key_creation(slot, driver, &key); | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1983 |  | 
|  | 1984 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1985 | if (status != PSA_SUCCESS) { | 
|  | 1986 | psa_fail_key_creation(slot, driver); | 
|  | 1987 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1988 |  | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1989 | /* Registration doesn't keep the key in RAM. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1990 | psa_close_key(key); | 
|  | 1991 | return status; | 
| Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1992 | } | 
|  | 1993 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 1994 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1995 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, | 
|  | 1996 | const psa_key_attributes_t *specified_attributes, | 
|  | 1997 | mbedtls_svc_key_id_t *target_key) | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1998 | { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1999 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2000 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2001 | psa_key_slot_t *source_slot = NULL; | 
|  | 2002 | psa_key_slot_t *target_slot = NULL; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2003 | psa_key_attributes_t actual_attributes = *specified_attributes; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2004 | psa_se_drv_table_entry_t *driver = NULL; | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2005 | size_t storage_size = 0; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2006 |  | 
| Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2007 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 2008 |  | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2009 | status = psa_get_and_lock_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2010 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); | 
|  | 2011 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2012 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2013 | } | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2014 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2015 | status = psa_validate_optional_attributes(source_slot, | 
|  | 2016 | specified_attributes); | 
|  | 2017 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2018 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2019 | } | 
| Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2020 |  | 
| Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2021 | /* The target key type and number of bits have been validated by | 
|  | 2022 | * psa_validate_optional_attributes() to be either equal to zero or | 
|  | 2023 | * equal to the ones of the source key. So it is safe to inherit | 
|  | 2024 | * them from the source key now." | 
| Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2025 | * */ | 
| Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2026 | actual_attributes.core.bits = source_slot->attr.bits; | 
|  | 2027 | actual_attributes.core.type = source_slot->attr.type; | 
| Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2028 |  | 
|  | 2029 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2030 | status = psa_restrict_key_policy(source_slot->attr.type, | 
|  | 2031 | &actual_attributes.core.policy, | 
|  | 2032 | &source_slot->attr.policy); | 
|  | 2033 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2034 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2035 | } | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2036 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2037 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, | 
|  | 2038 | &target_slot, &driver); | 
|  | 2039 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2040 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2041 | } | 
|  | 2042 | if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) != | 
|  | 2043 | PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) { | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2044 | /* | 
| Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2045 | * If the source and target keys are stored in different locations, | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2046 | * the source key would need to be exported as plaintext and re-imported | 
|  | 2047 | * in the other location. This has security implications which have not | 
| Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2048 | * been fully mapped. For now, this can be achieved through | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2049 | * appropriate API invocations from the application, if needed. | 
|  | 2050 | * */ | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2051 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 2052 | goto exit; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2053 | } | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2054 | /* | 
|  | 2055 | * When the source and target keys are within the same location, | 
| Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2056 | * - For transparent keys it is a blind copy without any driver invocation, | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2057 | * - For opaque keys this translates to an invocation of the drivers' | 
|  | 2058 | *   copy_key entry point through the dispatch layer. | 
|  | 2059 | * */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2060 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { | 
|  | 2061 | status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes, | 
|  | 2062 | &storage_size); | 
|  | 2063 | if (status != PSA_SUCCESS) { | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2064 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2065 | } | 
| Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2066 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2067 | status = psa_allocate_buffer_to_slot(target_slot, storage_size); | 
|  | 2068 | if (status != PSA_SUCCESS) { | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2069 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2070 | } | 
| Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2071 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2072 | status = psa_driver_wrapper_copy_key(&actual_attributes, | 
|  | 2073 | source_slot->key.data, | 
|  | 2074 | source_slot->key.bytes, | 
|  | 2075 | target_slot->key.data, | 
|  | 2076 | target_slot->key.bytes, | 
|  | 2077 | &target_slot->key.bytes); | 
|  | 2078 | if (status != PSA_SUCCESS) { | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2079 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2080 | } | 
|  | 2081 | } else { | 
|  | 2082 | status = psa_copy_key_material_into_slot(target_slot, | 
| Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2083 | source_slot->key.data, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2084 | source_slot->key.bytes); | 
|  | 2085 | if (status != PSA_SUCCESS) { | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2086 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2087 | } | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2088 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2089 | status = psa_finish_key_creation(target_slot, driver, target_key); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2090 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | if (status != PSA_SUCCESS) { | 
|  | 2092 | psa_fail_key_creation(target_slot, driver); | 
|  | 2093 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2094 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | unlock_status = psa_unlock_key_slot(source_slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2096 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2097 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2098 | } | 
|  | 2099 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2100 |  | 
|  | 2101 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2102 | /****************************************************************/ | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2103 | /* Message digests */ | 
|  | 2104 | /****************************************************************/ | 
|  | 2105 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2106 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2107 | { | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2108 | /* Aborting a non-active operation is allowed */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2109 | if (operation->id == 0) { | 
|  | 2110 | return PSA_SUCCESS; | 
|  | 2111 | } | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2112 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2114 | operation->id = 0; | 
|  | 2115 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2116 | return status; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2117 | } | 
|  | 2118 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2119 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, | 
|  | 2120 | psa_algorithm_t alg) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2121 | { | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2122 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2123 |  | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2124 | /* A context must be freshly initialized before it can be set up. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2125 | if (operation->id != 0) { | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2126 | status = PSA_ERROR_BAD_STATE; | 
|  | 2127 | goto exit; | 
|  | 2128 | } | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2129 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2130 | if (!PSA_ALG_IS_HASH(alg)) { | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2131 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2132 | goto exit; | 
|  | 2133 | } | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2134 |  | 
| Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2135 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only | 
|  | 2136 | * directly zeroes the int-sized dummy member of the context union. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2137 | memset(&operation->ctx, 0, sizeof(operation->ctx)); | 
| Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2138 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2139 | status = psa_driver_wrapper_hash_setup(operation, alg); | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2140 |  | 
|  | 2141 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2142 | if (status != PSA_SUCCESS) { | 
|  | 2143 | psa_hash_abort(operation); | 
|  | 2144 | } | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2145 |  | 
|  | 2146 | return status; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2147 | } | 
|  | 2148 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2149 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, | 
|  | 2150 | const uint8_t *input, | 
|  | 2151 | size_t input_length) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2152 | { | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2153 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2154 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2155 | if (operation->id == 0) { | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2156 | status = PSA_ERROR_BAD_STATE; | 
|  | 2157 | goto exit; | 
|  | 2158 | } | 
| Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2159 |  | 
| Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2160 | /* Don't require hash implementations to behave correctly on a | 
|  | 2161 | * zero-length input, which may have an invalid pointer. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2162 | if (input_length == 0) { | 
|  | 2163 | return PSA_SUCCESS; | 
|  | 2164 | } | 
| Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2165 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2166 | status = psa_driver_wrapper_hash_update(operation, input, input_length); | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2167 |  | 
|  | 2168 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2169 | if (status != PSA_SUCCESS) { | 
|  | 2170 | psa_hash_abort(operation); | 
|  | 2171 | } | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2172 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | return status; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2174 | } | 
|  | 2175 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2176 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, | 
|  | 2177 | uint8_t *hash, | 
|  | 2178 | size_t hash_size, | 
|  | 2179 | size_t *hash_length) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2180 | { | 
| Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2181 | *hash_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2182 | if (operation->id == 0) { | 
|  | 2183 | return PSA_ERROR_BAD_STATE; | 
|  | 2184 | } | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2185 |  | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2186 | psa_status_t status = psa_driver_wrapper_hash_finish( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2187 | operation, hash, hash_size, hash_length); | 
|  | 2188 | psa_hash_abort(operation); | 
|  | 2189 | return status; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2190 | } | 
|  | 2191 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2192 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, | 
|  | 2193 | const uint8_t *hash, | 
|  | 2194 | size_t hash_length) | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2195 | { | 
| Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2196 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2197 | size_t actual_hash_length; | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2198 | psa_status_t status = psa_hash_finish( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2199 | operation, | 
|  | 2200 | actual_hash, sizeof(actual_hash), | 
|  | 2201 | &actual_hash_length); | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2202 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2203 | if (status != PSA_SUCCESS) { | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2204 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2205 | } | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2206 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2207 | if (actual_hash_length != hash_length) { | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2208 | status = PSA_ERROR_INVALID_SIGNATURE; | 
|  | 2209 | goto exit; | 
|  | 2210 | } | 
|  | 2211 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2212 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2213 | status = PSA_ERROR_INVALID_SIGNATURE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2214 | } | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2215 |  | 
|  | 2216 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2217 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); | 
|  | 2218 | if (status != PSA_SUCCESS) { | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2219 | psa_hash_abort(operation); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | } | 
| Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2221 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2222 | return status; | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2223 | } | 
|  | 2224 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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) | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2229 | { | 
| Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2230 | *hash_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2231 | if (!PSA_ALG_IS_HASH(alg)) { | 
|  | 2232 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2233 | } | 
| Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2234 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2235 | return psa_driver_wrapper_hash_compute(alg, input, input_length, | 
|  | 2236 | hash, hash_size, hash_length); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2237 | } | 
|  | 2238 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2239 | psa_status_t psa_hash_compare(psa_algorithm_t alg, | 
|  | 2240 | const uint8_t *input, size_t input_length, | 
|  | 2241 | const uint8_t *hash, size_t hash_length) | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2242 | { | 
| Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2243 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; | 
| Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2244 | size_t actual_hash_length; | 
| Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2245 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2246 | if (!PSA_ALG_IS_HASH(alg)) { | 
|  | 2247 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2248 | } | 
| Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2249 |  | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2250 | psa_status_t status = psa_driver_wrapper_hash_compute( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2251 | alg, input, input_length, | 
|  | 2252 | actual_hash, sizeof(actual_hash), | 
|  | 2253 | &actual_hash_length); | 
|  | 2254 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2255 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2256 | } | 
|  | 2257 | if (actual_hash_length != hash_length) { | 
| Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2258 | status = PSA_ERROR_INVALID_SIGNATURE; | 
|  | 2259 | goto exit; | 
|  | 2260 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2261 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { | 
| Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2262 | status = PSA_ERROR_INVALID_SIGNATURE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2263 | } | 
| Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2264 |  | 
|  | 2265 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2266 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); | 
|  | 2267 | return status; | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2268 | } | 
|  | 2269 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, | 
|  | 2271 | psa_hash_operation_t *target_operation) | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2272 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2273 | if (source_operation->id == 0 || | 
|  | 2274 | target_operation->id != 0) { | 
|  | 2275 | return PSA_ERROR_BAD_STATE; | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2276 | } | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2277 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2278 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, | 
|  | 2279 | target_operation); | 
|  | 2280 | if (status != PSA_SUCCESS) { | 
|  | 2281 | psa_hash_abort(target_operation); | 
|  | 2282 | } | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2283 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2284 | return status; | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2285 | } | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2286 |  | 
|  | 2287 |  | 
| Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2288 | /****************************************************************/ | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2289 | /* MAC */ | 
|  | 2290 | /****************************************************************/ | 
|  | 2291 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2293 | { | 
| Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2294 | /* Aborting a non-active operation is allowed */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2295 | if (operation->id == 0) { | 
|  | 2296 | return PSA_SUCCESS; | 
|  | 2297 | } | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2298 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2299 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); | 
| Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2300 | operation->mac_size = 0; | 
|  | 2301 | operation->is_sign = 0; | 
| Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2302 | operation->id = 0; | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2303 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2304 | return status; | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2305 | } | 
|  | 2306 |  | 
| Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2307 | static psa_status_t psa_mac_finalize_alg_and_key_validation( | 
|  | 2308 | psa_algorithm_t alg, | 
| Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2309 | const psa_key_attributes_t *attributes, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2310 | uint8_t *mac_size) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2311 | { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2312 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2313 | psa_key_type_t key_type = psa_get_key_type(attributes); | 
|  | 2314 | size_t key_bits = psa_get_key_bits(attributes); | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2315 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2316 | if (!PSA_ALG_IS_MAC(alg)) { | 
|  | 2317 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2318 | } | 
| Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2319 |  | 
| Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2320 | /* Validate the combination of key type and algorithm */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2321 | status = psa_mac_key_can_do(alg, key_type); | 
|  | 2322 | if (status != PSA_SUCCESS) { | 
|  | 2323 | return status; | 
|  | 2324 | } | 
| Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2325 |  | 
| Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2326 | /* Get the output length for the algorithm and key combination */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2327 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); | 
| Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2328 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2329 | if (*mac_size < 4) { | 
| Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2330 | /* A very short MAC is too short for security since it can be | 
|  | 2331 | * brute-forced. Ancient protocols with 32-bit MACs do exist, | 
|  | 2332 | * so we make this our minimum, even though 32 bits is still | 
|  | 2333 | * too small for security. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | return PSA_ERROR_NOT_SUPPORTED; | 
| Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2335 | } | 
| Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2336 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2337 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, | 
|  | 2338 | PSA_ALG_FULL_LENGTH_MAC(alg))) { | 
| Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2339 | /* It's impossible to "truncate" to a larger length than the full length | 
|  | 2340 | * of the algorithm. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2342 | } | 
|  | 2343 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2344 | if (*mac_size > PSA_MAC_MAX_SIZE) { | 
| Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2345 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm | 
|  | 2346 | * that is disabled in the compile-time configuration. The result can | 
|  | 2347 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the | 
|  | 2348 | * configuration into account. In this case, force a return of | 
|  | 2349 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or | 
|  | 2350 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return | 
|  | 2351 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size | 
|  | 2352 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks | 
|  | 2353 | * systematically generated tests. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2354 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2355 | } | 
|  | 2356 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2357 | return PSA_SUCCESS; | 
| Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2358 | } | 
|  | 2359 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2360 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, | 
|  | 2361 | mbedtls_svc_key_id_t key, | 
|  | 2362 | psa_algorithm_t alg, | 
|  | 2363 | int is_sign) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2364 | { | 
|  | 2365 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2366 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2367 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2368 |  | 
|  | 2369 | /* A context must be freshly initialized before it can be set up. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2370 | if (operation->id != 0) { | 
| Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2371 | status = PSA_ERROR_BAD_STATE; | 
|  | 2372 | goto exit; | 
|  | 2373 | } | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2374 |  | 
|  | 2375 | status = psa_get_and_lock_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2376 | key, | 
|  | 2377 | &slot, | 
|  | 2378 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, | 
|  | 2379 | alg); | 
|  | 2380 | if (status != PSA_SUCCESS) { | 
| Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2381 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | } | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2383 |  | 
|  | 2384 | psa_key_attributes_t attributes = { | 
|  | 2385 | .core = slot->attr | 
|  | 2386 | }; | 
|  | 2387 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2388 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, | 
|  | 2389 | &operation->mac_size); | 
|  | 2390 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2391 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | } | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2393 |  | 
|  | 2394 | operation->is_sign = is_sign; | 
| Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2395 | /* Dispatch the MAC setup call with validated input */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2396 | if (is_sign) { | 
|  | 2397 | status = psa_driver_wrapper_mac_sign_setup(operation, | 
|  | 2398 | &attributes, | 
|  | 2399 | slot->key.data, | 
|  | 2400 | slot->key.bytes, | 
|  | 2401 | alg); | 
|  | 2402 | } else { | 
|  | 2403 | status = psa_driver_wrapper_mac_verify_setup(operation, | 
|  | 2404 | &attributes, | 
|  | 2405 | slot->key.data, | 
|  | 2406 | slot->key.bytes, | 
|  | 2407 | alg); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2408 | } | 
| Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2409 |  | 
| Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2410 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2411 | if (status != PSA_SUCCESS) { | 
|  | 2412 | psa_mac_abort(operation); | 
|  | 2413 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2414 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2415 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2416 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2417 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2418 | } | 
|  | 2419 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, | 
|  | 2421 | mbedtls_svc_key_id_t key, | 
|  | 2422 | psa_algorithm_t alg) | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2423 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2424 | return psa_mac_setup(operation, key, alg, 1); | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2425 | } | 
|  | 2426 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2427 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, | 
|  | 2428 | mbedtls_svc_key_id_t key, | 
|  | 2429 | psa_algorithm_t alg) | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2430 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2431 | return psa_mac_setup(operation, key, alg, 0); | 
| Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2432 | } | 
|  | 2433 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2434 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, | 
|  | 2435 | const uint8_t *input, | 
|  | 2436 | size_t input_length) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2437 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2438 | if (operation->id == 0) { | 
|  | 2439 | return PSA_ERROR_BAD_STATE; | 
|  | 2440 | } | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2441 |  | 
| Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2442 | /* Don't require hash implementations to behave correctly on a | 
|  | 2443 | * zero-length input, which may have an invalid pointer. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2444 | if (input_length == 0) { | 
|  | 2445 | return PSA_SUCCESS; | 
|  | 2446 | } | 
| Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2447 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | psa_status_t status = psa_driver_wrapper_mac_update(operation, | 
|  | 2449 | input, input_length); | 
|  | 2450 | if (status != PSA_SUCCESS) { | 
|  | 2451 | psa_mac_abort(operation); | 
|  | 2452 | } | 
| Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2453 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2454 | return status; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2455 | } | 
|  | 2456 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2457 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, | 
|  | 2458 | uint8_t *mac, | 
|  | 2459 | size_t mac_size, | 
|  | 2460 | size_t *mac_length) | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2461 | { | 
| Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2462 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2463 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2464 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2465 | if (operation->id == 0) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2466 | status = PSA_ERROR_BAD_STATE; | 
| Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2467 | goto exit; | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2468 | } | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2469 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2470 | if (!operation->is_sign) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2471 | status = PSA_ERROR_BAD_STATE; | 
| Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2472 | goto exit; | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2473 | } | 
| Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2474 |  | 
| Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2475 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) | 
|  | 2476 | * once all the error checks are done. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2477 | if (operation->mac_size == 0) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2478 | status = PSA_ERROR_BAD_STATE; | 
| Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2479 | goto exit; | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2480 | } | 
| Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2481 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2482 | if (mac_size < operation->mac_size) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2483 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
| Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2484 | goto exit; | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2485 | } | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2486 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2487 | status = psa_driver_wrapper_mac_sign_finish(operation, | 
|  | 2488 | mac, operation->mac_size, | 
|  | 2489 | mac_length); | 
| Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2490 |  | 
| Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2491 | exit: | 
| Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2492 | /* In case of success, set the potential excess room in the output buffer | 
|  | 2493 | * to an invalid value, to avoid potentially leaking a longer MAC. | 
|  | 2494 | * In case of error, set the output length and content to a safe default, | 
|  | 2495 | * such that in case the caller misses an error check, the output would be | 
|  | 2496 | * an unachievable MAC. | 
|  | 2497 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2498 | if (status != PSA_SUCCESS) { | 
| 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 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2503 | if (mac_size > operation->mac_size) { | 
|  | 2504 | memset(&mac[operation->mac_size], '!', | 
|  | 2505 | mac_size - operation->mac_size); | 
|  | 2506 | } | 
| Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2507 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2508 | abort_status = psa_mac_abort(operation); | 
| Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2509 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2510 | return status == PSA_SUCCESS ? abort_status : status; | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2511 | } | 
|  | 2512 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2513 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, | 
|  | 2514 | const uint8_t *mac, | 
|  | 2515 | size_t mac_length) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2516 | { | 
| Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2517 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2518 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2519 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2520 | if (operation->id == 0) { | 
| 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 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2525 | if (operation->is_sign) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2526 | status = PSA_ERROR_BAD_STATE; | 
| Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2527 | goto exit; | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2528 | } | 
| Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2529 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2530 | if (operation->mac_size != mac_length) { | 
| Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2531 | status = PSA_ERROR_INVALID_SIGNATURE; | 
| Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2532 | goto exit; | 
| Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2533 | } | 
|  | 2534 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2535 | status = psa_driver_wrapper_mac_verify_finish(operation, | 
|  | 2536 | mac, mac_length); | 
| Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2537 |  | 
| Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2538 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2539 | abort_status = psa_mac_abort(operation); | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2540 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2541 | return status == PSA_SUCCESS ? abort_status : status; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2542 | } | 
|  | 2543 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2544 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, | 
|  | 2545 | psa_algorithm_t alg, | 
|  | 2546 | const uint8_t *input, | 
|  | 2547 | size_t input_length, | 
|  | 2548 | uint8_t *mac, | 
|  | 2549 | size_t mac_size, | 
|  | 2550 | size_t *mac_length, | 
|  | 2551 | int is_sign) | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2552 | { | 
|  | 2553 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2554 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2555 | psa_key_slot_t *slot; | 
|  | 2556 | uint8_t operation_mac_size = 0; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2557 |  | 
| Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2558 | status = psa_get_and_lock_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2559 | key, | 
|  | 2560 | &slot, | 
|  | 2561 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, | 
|  | 2562 | alg); | 
|  | 2563 | if (status != PSA_SUCCESS) { | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2564 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2565 | } | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2566 |  | 
| Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2567 | psa_key_attributes_t attributes = { | 
|  | 2568 | .core = slot->attr | 
|  | 2569 | }; | 
|  | 2570 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2571 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, | 
|  | 2572 | &operation_mac_size); | 
|  | 2573 | if (status != PSA_SUCCESS) { | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2574 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2575 | } | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2576 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2577 | if (mac_size < operation_mac_size) { | 
| Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2578 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2579 | goto exit; | 
| Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2580 | } | 
|  | 2581 |  | 
|  | 2582 | status = psa_driver_wrapper_mac_compute( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2583 | &attributes, | 
|  | 2584 | slot->key.data, slot->key.bytes, | 
|  | 2585 | alg, | 
|  | 2586 | input, input_length, | 
|  | 2587 | mac, operation_mac_size, mac_length); | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2588 |  | 
|  | 2589 | exit: | 
| Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2590 | /* In case of success, set the potential excess room in the output buffer | 
|  | 2591 | * to an invalid value, to avoid potentially leaking a longer MAC. | 
|  | 2592 | * In case of error, set the output length and content to a safe default, | 
|  | 2593 | * such that in case the caller misses an error check, the output would be | 
|  | 2594 | * an unachievable MAC. | 
|  | 2595 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2596 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2597 | *mac_length = mac_size; | 
| Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2598 | operation_mac_size = 0; | 
| Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2599 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2600 | if (mac_size > operation_mac_size) { | 
|  | 2601 | memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size); | 
|  | 2602 | } | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2603 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2604 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2605 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2606 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2607 | } | 
|  | 2608 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2609 | psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key, | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2610 | psa_algorithm_t alg, | 
|  | 2611 | const uint8_t *input, | 
|  | 2612 | size_t input_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2613 | uint8_t *mac, | 
|  | 2614 | size_t mac_size, | 
|  | 2615 | size_t *mac_length) | 
|  | 2616 | { | 
|  | 2617 | return psa_mac_compute_internal(key, alg, | 
|  | 2618 | input, input_length, | 
|  | 2619 | mac, mac_size, mac_length, 1); | 
|  | 2620 | } | 
|  | 2621 |  | 
|  | 2622 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, | 
|  | 2623 | psa_algorithm_t alg, | 
|  | 2624 | const uint8_t *input, | 
|  | 2625 | size_t input_length, | 
|  | 2626 | const uint8_t *mac, | 
|  | 2627 | size_t mac_length) | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2628 | { | 
|  | 2629 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2630 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; | 
|  | 2631 | size_t actual_mac_length; | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2632 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2633 | status = psa_mac_compute_internal(key, alg, | 
|  | 2634 | input, input_length, | 
|  | 2635 | actual_mac, sizeof(actual_mac), | 
|  | 2636 | &actual_mac_length, 0); | 
|  | 2637 | if (status != PSA_SUCCESS) { | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2638 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2639 | } | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2640 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2641 | if (mac_length != actual_mac_length) { | 
| Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2642 | status = PSA_ERROR_INVALID_SIGNATURE; | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2643 | goto exit; | 
| Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2644 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2645 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) { | 
| Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2646 | status = PSA_ERROR_INVALID_SIGNATURE; | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2647 | goto exit; | 
| Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2648 | } | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2649 |  | 
|  | 2650 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2651 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2652 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2653 | return status; | 
| gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2654 | } | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2655 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2656 | /****************************************************************/ | 
|  | 2657 | /* Asymmetric cryptography */ | 
|  | 2658 | /****************************************************************/ | 
|  | 2659 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2660 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, | 
|  | 2661 | psa_algorithm_t alg) | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2662 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2663 | if (input_is_message) { | 
|  | 2664 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { | 
|  | 2665 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2666 | } | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2667 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | if (PSA_ALG_IS_SIGN_HASH(alg)) { | 
|  | 2669 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { | 
|  | 2670 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2671 | } | 
|  | 2672 | } | 
|  | 2673 | } else { | 
|  | 2674 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { | 
|  | 2675 | return PSA_ERROR_INVALID_ARGUMENT; | 
| gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2676 | } | 
|  | 2677 | } | 
| gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2678 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2679 | return PSA_SUCCESS; | 
| gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2680 | } | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2681 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2682 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, | 
|  | 2683 | int input_is_message, | 
|  | 2684 | psa_algorithm_t alg, | 
|  | 2685 | const uint8_t *input, | 
|  | 2686 | size_t input_length, | 
|  | 2687 | uint8_t *signature, | 
|  | 2688 | size_t signature_size, | 
|  | 2689 | size_t *signature_length) | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2690 | { | 
|  | 2691 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2692 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2693 | psa_key_slot_t *slot; | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2694 |  | 
|  | 2695 | *signature_length = 0; | 
|  | 2696 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2697 | status = psa_sign_verify_check_alg(input_is_message, alg); | 
|  | 2698 | if (status != PSA_SUCCESS) { | 
| gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2699 | return status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2700 | } | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2701 |  | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2702 | /* Immediately reject a zero-length signature buffer. This guarantees | 
| gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2703 | * 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] | 2704 | * buffer can in principle be empty since it doesn't actually have | 
|  | 2705 | * to be a hash.) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2706 | if (signature_size == 0) { | 
|  | 2707 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 2708 | } | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2709 |  | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2710 | status = psa_get_and_lock_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2711 | key, &slot, | 
|  | 2712 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : | 
|  | 2713 | PSA_KEY_USAGE_SIGN_HASH, | 
|  | 2714 | alg); | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2715 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2716 | if (status != PSA_SUCCESS) { | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2717 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2718 | } | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2719 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2720 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2721 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 2722 | goto exit; | 
|  | 2723 | } | 
|  | 2724 |  | 
|  | 2725 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2726 | .core = slot->attr | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2727 | }; | 
|  | 2728 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2729 | if (input_is_message) { | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2730 | status = psa_driver_wrapper_sign_message( | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2731 | &attributes, slot->key.data, slot->key.bytes, | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2732 | alg, input, input_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2733 | signature, signature_size, signature_length); | 
|  | 2734 | } else { | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2735 |  | 
|  | 2736 | status = psa_driver_wrapper_sign_hash( | 
|  | 2737 | &attributes, slot->key.data, slot->key.bytes, | 
|  | 2738 | alg, input, input_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2739 | signature, signature_size, signature_length); | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2740 | } | 
|  | 2741 |  | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2742 |  | 
|  | 2743 | exit: | 
|  | 2744 | /* Fill the unused part of the output buffer (the whole buffer on error, | 
|  | 2745 | * the trailing part on success) with something that isn't a valid signature | 
|  | 2746 | * (barring an attack on the signature and deliberately-crafted input), | 
|  | 2747 | * in case the caller doesn't check the return status properly. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2748 | if (status == PSA_SUCCESS) { | 
|  | 2749 | memset(signature + *signature_length, '!', | 
|  | 2750 | signature_size - *signature_length); | 
|  | 2751 | } else { | 
|  | 2752 | memset(signature, '!', signature_size); | 
|  | 2753 | } | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2754 | /* If signature_size is 0 then we have nothing to do. We must not call | 
|  | 2755 | * memset because signature may be NULL in this case. */ | 
|  | 2756 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2757 | unlock_status = psa_unlock_key_slot(slot); | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2758 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2759 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2760 | } | 
|  | 2761 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2762 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, | 
|  | 2763 | int input_is_message, | 
|  | 2764 | psa_algorithm_t alg, | 
|  | 2765 | const uint8_t *input, | 
|  | 2766 | size_t input_length, | 
|  | 2767 | const uint8_t *signature, | 
|  | 2768 | size_t signature_length) | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2769 | { | 
|  | 2770 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2771 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 2772 | psa_key_slot_t *slot; | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2773 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2774 | status = psa_sign_verify_check_alg(input_is_message, alg); | 
|  | 2775 | if (status != PSA_SUCCESS) { | 
| gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2776 | return status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2777 | } | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2778 |  | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2779 | status = psa_get_and_lock_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2780 | key, &slot, | 
|  | 2781 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : | 
|  | 2782 | PSA_KEY_USAGE_VERIFY_HASH, | 
|  | 2783 | alg); | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2784 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2785 | if (status != PSA_SUCCESS) { | 
|  | 2786 | return status; | 
|  | 2787 | } | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2788 |  | 
|  | 2789 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2790 | .core = slot->attr | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2791 | }; | 
|  | 2792 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2793 | if (input_is_message) { | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2794 | status = psa_driver_wrapper_verify_message( | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2795 | &attributes, slot->key.data, slot->key.bytes, | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2796 | alg, input, input_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2797 | signature, signature_length); | 
|  | 2798 | } else { | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2799 | status = psa_driver_wrapper_verify_hash( | 
|  | 2800 | &attributes, slot->key.data, slot->key.bytes, | 
|  | 2801 | alg, input, input_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2802 | signature, signature_length); | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2803 | } | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2804 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2805 | unlock_status = psa_unlock_key_slot(slot); | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2806 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2807 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2808 |  | 
|  | 2809 | } | 
|  | 2810 |  | 
| gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2811 | psa_status_t psa_sign_message_builtin( | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2812 | const psa_key_attributes_t *attributes, | 
|  | 2813 | const uint8_t *key_buffer, | 
|  | 2814 | size_t key_buffer_size, | 
|  | 2815 | psa_algorithm_t alg, | 
|  | 2816 | const uint8_t *input, | 
|  | 2817 | size_t input_length, | 
|  | 2818 | uint8_t *signature, | 
|  | 2819 | size_t signature_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2820 | size_t *signature_length) | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2821 | { | 
|  | 2822 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2823 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2824 | if (PSA_ALG_IS_SIGN_HASH(alg)) { | 
| gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2825 | size_t hash_length; | 
|  | 2826 | uint8_t hash[PSA_HASH_MAX_SIZE]; | 
|  | 2827 |  | 
| gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2828 | status = psa_driver_wrapper_hash_compute( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2829 | PSA_ALG_SIGN_GET_HASH(alg), | 
|  | 2830 | input, input_length, | 
|  | 2831 | hash, sizeof(hash), &hash_length); | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2832 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2833 | if (status != PSA_SUCCESS) { | 
| gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2834 | return status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2835 | } | 
| gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2836 |  | 
|  | 2837 | return psa_driver_wrapper_sign_hash( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2838 | attributes, key_buffer, key_buffer_size, | 
|  | 2839 | alg, hash, hash_length, | 
|  | 2840 | signature, signature_size, signature_length); | 
| gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2841 | } | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2842 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2843 | return PSA_ERROR_NOT_SUPPORTED; | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2844 | } | 
|  | 2845 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2846 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, | 
|  | 2847 | psa_algorithm_t alg, | 
|  | 2848 | const uint8_t *input, | 
|  | 2849 | size_t input_length, | 
|  | 2850 | uint8_t *signature, | 
|  | 2851 | size_t signature_size, | 
|  | 2852 | size_t *signature_length) | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2853 | { | 
|  | 2854 | return psa_sign_internal( | 
| gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2855 | key, 1, alg, input, input_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2856 | signature, signature_size, signature_length); | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2857 | } | 
|  | 2858 |  | 
| gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2859 | psa_status_t psa_verify_message_builtin( | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2860 | const psa_key_attributes_t *attributes, | 
|  | 2861 | const uint8_t *key_buffer, | 
|  | 2862 | size_t key_buffer_size, | 
|  | 2863 | psa_algorithm_t alg, | 
|  | 2864 | const uint8_t *input, | 
|  | 2865 | size_t input_length, | 
|  | 2866 | const uint8_t *signature, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2867 | size_t signature_length) | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2868 | { | 
|  | 2869 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2870 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2871 | if (PSA_ALG_IS_SIGN_HASH(alg)) { | 
| gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2872 | size_t hash_length; | 
|  | 2873 | uint8_t hash[PSA_HASH_MAX_SIZE]; | 
|  | 2874 |  | 
| gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2875 | status = psa_driver_wrapper_hash_compute( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2876 | PSA_ALG_SIGN_GET_HASH(alg), | 
|  | 2877 | input, input_length, | 
|  | 2878 | hash, sizeof(hash), &hash_length); | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2879 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2880 | if (status != PSA_SUCCESS) { | 
| gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2881 | return status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2882 | } | 
| gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2883 |  | 
|  | 2884 | return psa_driver_wrapper_verify_hash( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2885 | attributes, key_buffer, key_buffer_size, | 
|  | 2886 | alg, hash, hash_length, | 
|  | 2887 | signature, signature_length); | 
| gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2888 | } | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2889 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2890 | return PSA_ERROR_NOT_SUPPORTED; | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2891 | } | 
|  | 2892 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2893 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, | 
|  | 2894 | psa_algorithm_t alg, | 
|  | 2895 | const uint8_t *input, | 
|  | 2896 | size_t input_length, | 
|  | 2897 | const uint8_t *signature, | 
|  | 2898 | size_t signature_length) | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2899 | { | 
|  | 2900 | return psa_verify_internal( | 
| gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2901 | key, 1, alg, input, input_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2902 | signature, signature_length); | 
| gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2903 | } | 
|  | 2904 |  | 
| gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2905 | psa_status_t psa_sign_hash_builtin( | 
| Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2906 | const psa_key_attributes_t *attributes, | 
|  | 2907 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 2908 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2909 | uint8_t *signature, size_t signature_size, size_t *signature_length) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2910 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2911 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { | 
|  | 2912 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || | 
|  | 2913 | PSA_ALG_IS_RSA_PSS(alg)) { | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2914 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2915 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) | 
|  | 2916 | return mbedtls_psa_rsa_sign_hash( | 
|  | 2917 | attributes, | 
|  | 2918 | key_buffer, key_buffer_size, | 
|  | 2919 | alg, hash, hash_length, | 
|  | 2920 | signature, signature_size, signature_length); | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2921 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || | 
|  | 2922 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2923 | } else { | 
|  | 2924 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2925 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2926 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { | 
|  | 2927 | if (PSA_ALG_IS_ECDSA(alg)) { | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2928 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2929 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) | 
|  | 2930 | return mbedtls_psa_ecdsa_sign_hash( | 
|  | 2931 | attributes, | 
|  | 2932 | key_buffer, key_buffer_size, | 
|  | 2933 | alg, hash, hash_length, | 
|  | 2934 | signature, signature_size, signature_length); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2935 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || | 
|  | 2936 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2937 | } else { | 
|  | 2938 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2939 | } | 
| Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2940 | } | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2941 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | (void) key_buffer; | 
|  | 2943 | (void) key_buffer_size; | 
|  | 2944 | (void) hash; | 
|  | 2945 | (void) hash_length; | 
|  | 2946 | (void) signature; | 
|  | 2947 | (void) signature_size; | 
|  | 2948 | (void) signature_length; | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2949 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2950 | return PSA_ERROR_NOT_SUPPORTED; | 
| Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2951 | } | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2952 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2953 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, | 
|  | 2954 | psa_algorithm_t alg, | 
|  | 2955 | const uint8_t *hash, | 
|  | 2956 | size_t hash_length, | 
|  | 2957 | uint8_t *signature, | 
|  | 2958 | size_t signature_size, | 
|  | 2959 | size_t *signature_length) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2960 | { | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2961 | return psa_sign_internal( | 
| gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2962 | key, 0, alg, hash, hash_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2963 | signature, signature_size, signature_length); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2964 | } | 
|  | 2965 |  | 
| gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2966 | psa_status_t psa_verify_hash_builtin( | 
| Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2967 | const psa_key_attributes_t *attributes, | 
|  | 2968 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 2969 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | const uint8_t *signature, size_t signature_length) | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2971 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2972 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { | 
|  | 2973 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || | 
|  | 2974 | PSA_ALG_IS_RSA_PSS(alg)) { | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2975 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2976 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) | 
|  | 2977 | return mbedtls_psa_rsa_verify_hash( | 
|  | 2978 | attributes, | 
|  | 2979 | key_buffer, key_buffer_size, | 
|  | 2980 | alg, hash, hash_length, | 
|  | 2981 | signature, signature_length); | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2982 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || | 
|  | 2983 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2984 | } else { | 
|  | 2985 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2986 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2987 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { | 
|  | 2988 | if (PSA_ALG_IS_ECDSA(alg)) { | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2989 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2990 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) | 
|  | 2991 | return mbedtls_psa_ecdsa_verify_hash( | 
|  | 2992 | attributes, | 
|  | 2993 | key_buffer, key_buffer_size, | 
|  | 2994 | alg, hash, hash_length, | 
|  | 2995 | signature, signature_length); | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2996 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || | 
|  | 2997 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2998 | } else { | 
|  | 2999 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3000 | } | 
| Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3001 | } | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3002 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3003 | (void) key_buffer; | 
|  | 3004 | (void) key_buffer_size; | 
|  | 3005 | (void) hash; | 
|  | 3006 | (void) hash_length; | 
|  | 3007 | (void) signature; | 
|  | 3008 | (void) signature_length; | 
| Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3009 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3010 | return PSA_ERROR_NOT_SUPPORTED; | 
| Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3011 | } | 
|  | 3012 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3013 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, | 
|  | 3014 | psa_algorithm_t alg, | 
|  | 3015 | const uint8_t *hash, | 
|  | 3016 | size_t hash_length, | 
|  | 3017 | const uint8_t *signature, | 
|  | 3018 | size_t signature_length) | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3019 | { | 
| gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3020 | return psa_verify_internal( | 
| gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3021 | key, 0, alg, hash, hash_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3022 | signature, signature_length); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3023 | } | 
|  | 3024 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, | 
|  | 3026 | psa_algorithm_t alg, | 
|  | 3027 | const uint8_t *input, | 
|  | 3028 | size_t input_length, | 
|  | 3029 | const uint8_t *salt, | 
|  | 3030 | size_t salt_length, | 
|  | 3031 | uint8_t *output, | 
|  | 3032 | size_t output_size, | 
|  | 3033 | size_t *output_length) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3034 | { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3035 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3036 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3037 | psa_key_slot_t *slot; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3038 |  | 
| Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3039 | (void) input; | 
|  | 3040 | (void) input_length; | 
|  | 3041 | (void) salt; | 
|  | 3042 | (void) output; | 
|  | 3043 | (void) output_size; | 
|  | 3044 |  | 
| Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3045 | *output_length = 0; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3046 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3047 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { | 
|  | 3048 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3049 | } | 
| Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3050 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3051 | status = psa_get_and_lock_transparent_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3052 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); | 
|  | 3053 | if (status != PSA_SUCCESS) { | 
|  | 3054 | return status; | 
|  | 3055 | } | 
|  | 3056 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || | 
|  | 3057 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3058 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3059 | goto exit; | 
|  | 3060 | } | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3061 |  | 
| Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3062 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3063 | .core = slot->attr | 
| Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3064 | }; | 
| Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3065 |  | 
| Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3066 | status = psa_driver_wrapper_asymmetric_encrypt( | 
|  | 3067 | &attributes, slot->key.data, slot->key.bytes, | 
|  | 3068 | alg, input, input_length, salt, salt_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3069 | output, output_size, output_length); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3070 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3071 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3072 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3073 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3074 | } | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3075 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3076 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, | 
|  | 3077 | psa_algorithm_t alg, | 
|  | 3078 | const uint8_t *input, | 
|  | 3079 | size_t input_length, | 
|  | 3080 | const uint8_t *salt, | 
|  | 3081 | size_t salt_length, | 
|  | 3082 | uint8_t *output, | 
|  | 3083 | size_t output_size, | 
|  | 3084 | size_t *output_length) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3085 | { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3086 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3087 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3088 | psa_key_slot_t *slot; | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3089 |  | 
| Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3090 | (void) input; | 
|  | 3091 | (void) input_length; | 
|  | 3092 | (void) salt; | 
|  | 3093 | (void) output; | 
|  | 3094 | (void) output_size; | 
|  | 3095 |  | 
| Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3096 | *output_length = 0; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3097 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { | 
|  | 3099 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3100 | } | 
| Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3101 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3102 | status = psa_get_and_lock_transparent_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3103 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); | 
|  | 3104 | if (status != PSA_SUCCESS) { | 
|  | 3105 | return status; | 
|  | 3106 | } | 
|  | 3107 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3108 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3109 | goto exit; | 
|  | 3110 | } | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3111 |  | 
| Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3112 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3113 | .core = slot->attr | 
| Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3114 | }; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3115 |  | 
| Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3116 | status = psa_driver_wrapper_asymmetric_decrypt( | 
|  | 3117 | &attributes, slot->key.data, slot->key.bytes, | 
|  | 3118 | alg, input, input_length, salt, salt_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3119 | output, output_size, output_length); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3120 |  | 
|  | 3121 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3123 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3124 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3125 | } | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3126 |  | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3127 |  | 
|  | 3128 |  | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3129 | /****************************************************************/ | 
|  | 3130 | /* Symmetric cryptography */ | 
|  | 3131 | /****************************************************************/ | 
|  | 3132 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3133 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, | 
|  | 3134 | mbedtls_svc_key_id_t key, | 
|  | 3135 | psa_algorithm_t alg, | 
|  | 3136 | mbedtls_operation_t cipher_operation) | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3137 | { | 
|  | 3138 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 3139 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3140 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3141 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? | 
|  | 3142 | PSA_KEY_USAGE_ENCRYPT : | 
|  | 3143 | PSA_KEY_USAGE_DECRYPT); | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3144 |  | 
|  | 3145 | /* A context must be freshly initialized before it can be set up. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3146 | if (operation->id != 0) { | 
| Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3147 | status = PSA_ERROR_BAD_STATE; | 
|  | 3148 | goto exit; | 
|  | 3149 | } | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3150 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3151 | if (!PSA_ALG_IS_CIPHER(alg)) { | 
| Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3152 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3153 | goto exit; | 
|  | 3154 | } | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3155 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3156 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); | 
|  | 3157 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3158 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3159 | } | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3160 |  | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3161 | /* Initialize the operation struct members, except for id. The id member | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3162 | * 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] | 3163 | * so we only set it (in the driver wrapper) after resources have been | 
|  | 3164 | * allocated/initialized. */ | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3165 | operation->iv_set = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3166 | if (alg == PSA_ALG_ECB_NO_PADDING) { | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3167 | operation->iv_required = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3168 | } else { | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3169 | operation->iv_required = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3170 | } | 
|  | 3171 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3172 |  | 
| Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3173 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3174 | .core = slot->attr | 
| Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3175 | }; | 
|  | 3176 |  | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3177 | /* Try doing the operation through a driver before using software fallback. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3178 | if (cipher_operation == MBEDTLS_ENCRYPT) { | 
|  | 3179 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, | 
|  | 3180 | &attributes, | 
|  | 3181 | slot->key.data, | 
|  | 3182 | slot->key.bytes, | 
|  | 3183 | alg); | 
|  | 3184 | } else { | 
|  | 3185 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, | 
|  | 3186 | &attributes, | 
|  | 3187 | slot->key.data, | 
|  | 3188 | slot->key.bytes, | 
|  | 3189 | alg); | 
|  | 3190 | } | 
| Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3191 |  | 
| Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3192 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3193 | if (status != PSA_SUCCESS) { | 
|  | 3194 | psa_cipher_abort(operation); | 
|  | 3195 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3196 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3197 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3198 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3199 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3200 | } | 
|  | 3201 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3202 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, | 
|  | 3203 | mbedtls_svc_key_id_t key, | 
|  | 3204 | psa_algorithm_t alg) | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3205 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3206 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3207 | } | 
|  | 3208 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3209 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, | 
|  | 3210 | mbedtls_svc_key_id_t key, | 
|  | 3211 | psa_algorithm_t alg) | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3212 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3213 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); | 
| mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3214 | } | 
|  | 3215 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3216 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, | 
|  | 3217 | uint8_t *iv, | 
|  | 3218 | size_t iv_size, | 
|  | 3219 | size_t *iv_length) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3220 | { | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3221 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3222 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; | 
|  | 3223 | size_t default_iv_length; | 
| Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3224 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3225 | if (operation->id == 0) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3226 | status = PSA_ERROR_BAD_STATE; | 
|  | 3227 | goto exit; | 
| Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3228 | } | 
|  | 3229 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3230 | if (operation->iv_set || !operation->iv_required) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3231 | status = PSA_ERROR_BAD_STATE; | 
|  | 3232 | goto exit; | 
| Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3233 | } | 
| Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3234 |  | 
| Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3235 | default_iv_length = operation->default_iv_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3236 | if (iv_size < default_iv_length) { | 
| Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3237 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 3238 | goto exit; | 
|  | 3239 | } | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3240 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3241 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { | 
| Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3242 | status = PSA_ERROR_GENERIC_ERROR; | 
|  | 3243 | goto exit; | 
|  | 3244 | } | 
|  | 3245 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3246 | status = psa_generate_random(local_iv, default_iv_length); | 
|  | 3247 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3248 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3249 | } | 
| Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3250 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3251 | status = psa_driver_wrapper_cipher_set_iv(operation, | 
|  | 3252 | local_iv, default_iv_length); | 
| Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3253 |  | 
|  | 3254 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3255 | if (status == PSA_SUCCESS) { | 
|  | 3256 | memcpy(iv, local_iv, default_iv_length); | 
| Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3257 | *iv_length = default_iv_length; | 
| Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3258 | operation->iv_set = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3259 | } else { | 
| Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3260 | *iv_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3261 | psa_cipher_abort(operation); | 
| Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3262 | } | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3263 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3264 | return status; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3265 | } | 
|  | 3266 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3267 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, | 
|  | 3268 | const uint8_t *iv, | 
|  | 3269 | size_t iv_length) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3270 | { | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3271 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3272 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3273 | if (operation->id == 0) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3274 | status = PSA_ERROR_BAD_STATE; | 
|  | 3275 | goto exit; | 
|  | 3276 | } | 
| Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3277 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3278 | if (operation->iv_set || !operation->iv_required) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3279 | status = PSA_ERROR_BAD_STATE; | 
|  | 3280 | goto exit; | 
|  | 3281 | } | 
| Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3282 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3283 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3284 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3285 | goto exit; | 
|  | 3286 | } | 
| Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3287 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3288 | status = psa_driver_wrapper_cipher_set_iv(operation, | 
|  | 3289 | iv, | 
|  | 3290 | iv_length); | 
| Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3291 |  | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3292 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3293 | if (status == PSA_SUCCESS) { | 
| itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3294 | operation->iv_set = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3295 | } else { | 
|  | 3296 | psa_cipher_abort(operation); | 
|  | 3297 | } | 
|  | 3298 | return status; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3299 | } | 
|  | 3300 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3301 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, | 
|  | 3302 | const uint8_t *input, | 
|  | 3303 | size_t input_length, | 
|  | 3304 | uint8_t *output, | 
|  | 3305 | size_t output_size, | 
|  | 3306 | size_t *output_length) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3307 | { | 
| Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3308 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3309 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3310 | if (operation->id == 0) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3311 | status = PSA_ERROR_BAD_STATE; | 
|  | 3312 | goto exit; | 
| Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3313 | } | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3314 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3315 | if (operation->iv_required && !operation->iv_set) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3316 | status = PSA_ERROR_BAD_STATE; | 
|  | 3317 | goto exit; | 
| Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3318 | } | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3319 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3320 | status = psa_driver_wrapper_cipher_update(operation, | 
|  | 3321 | input, | 
|  | 3322 | input_length, | 
|  | 3323 | output, | 
|  | 3324 | output_size, | 
|  | 3325 | output_length); | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3326 |  | 
|  | 3327 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3328 | if (status != PSA_SUCCESS) { | 
|  | 3329 | psa_cipher_abort(operation); | 
|  | 3330 | } | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3331 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3332 | return status; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3333 | } | 
|  | 3334 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3335 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, | 
|  | 3336 | uint8_t *output, | 
|  | 3337 | size_t output_size, | 
|  | 3338 | size_t *output_length) | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3339 | { | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3340 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3341 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3342 | if (operation->id == 0) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3343 | status = PSA_ERROR_BAD_STATE; | 
|  | 3344 | goto exit; | 
| Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3345 | } | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3346 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3347 | if (operation->iv_required && !operation->iv_set) { | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3348 | status = PSA_ERROR_BAD_STATE; | 
|  | 3349 | goto exit; | 
| Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3350 | } | 
| Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3351 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3352 | status = psa_driver_wrapper_cipher_finish(operation, | 
|  | 3353 | output, | 
|  | 3354 | output_size, | 
|  | 3355 | output_length); | 
| Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3356 |  | 
|  | 3357 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3358 | if (status == PSA_SUCCESS) { | 
|  | 3359 | return psa_cipher_abort(operation); | 
|  | 3360 | } else { | 
| Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3361 | *output_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3362 | (void) psa_cipher_abort(operation); | 
| Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3363 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | return status; | 
| Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3365 | } | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3366 | } | 
|  | 3367 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3368 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3369 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3370 | if (operation->id == 0) { | 
| Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3371 | /* The object has (apparently) been initialized but it is not (yet) | 
| Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3372 | * in use. It's ok to call abort on such an object, and there's | 
|  | 3373 | * nothing to do. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3374 | return PSA_SUCCESS; | 
| Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3375 | } | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3376 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3377 | psa_driver_wrapper_cipher_abort(operation); | 
| Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3378 |  | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3379 | operation->id = 0; | 
| Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3380 | operation->iv_set = 0; | 
| Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3381 | operation->iv_required = 0; | 
| Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3382 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3383 | return PSA_SUCCESS; | 
| mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3384 | } | 
|  | 3385 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3386 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, | 
|  | 3387 | psa_algorithm_t alg, | 
|  | 3388 | const uint8_t *input, | 
|  | 3389 | size_t input_length, | 
|  | 3390 | uint8_t *output, | 
|  | 3391 | size_t output_size, | 
|  | 3392 | size_t *output_length) | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3393 | { | 
|  | 3394 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3395 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3396 | psa_key_slot_t *slot = NULL; | 
| Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3397 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; | 
|  | 3398 | size_t default_iv_length = 0; | 
| gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3399 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3400 | if (!PSA_ALG_IS_CIPHER(alg)) { | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3401 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3402 | goto exit; | 
|  | 3403 | } | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3404 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3405 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, | 
|  | 3406 | PSA_KEY_USAGE_ENCRYPT, | 
|  | 3407 | alg); | 
|  | 3408 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3409 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3410 | } | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3411 |  | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3412 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3413 | .core = slot->attr | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3414 | }; | 
|  | 3415 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3416 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); | 
|  | 3417 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { | 
| Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3418 | status = PSA_ERROR_GENERIC_ERROR; | 
|  | 3419 | goto exit; | 
|  | 3420 | } | 
|  | 3421 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3422 | if (default_iv_length > 0) { | 
|  | 3423 | if (output_size < default_iv_length) { | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3424 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 3425 | goto exit; | 
|  | 3426 | } | 
|  | 3427 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3428 | status = psa_generate_random(local_iv, default_iv_length); | 
|  | 3429 | if (status != PSA_SUCCESS) { | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3430 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3431 | } | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3432 | } | 
|  | 3433 |  | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3434 | status = psa_driver_wrapper_cipher_encrypt( | 
|  | 3435 | &attributes, slot->key.data, slot->key.bytes, | 
| Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3436 | alg, local_iv, default_iv_length, input, input_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3437 | mbedtls_buffer_offset(output, default_iv_length), | 
|  | 3438 | output_size - default_iv_length, output_length); | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3439 |  | 
|  | 3440 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3441 | unlock_status = psa_unlock_key_slot(slot); | 
|  | 3442 | if (status == PSA_SUCCESS) { | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3443 | status = unlock_status; | 
| Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3444 | } | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3445 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3446 | if (status == PSA_SUCCESS) { | 
|  | 3447 | if (default_iv_length > 0) { | 
|  | 3448 | memcpy(output, local_iv, default_iv_length); | 
|  | 3449 | } | 
|  | 3450 | *output_length += default_iv_length; | 
|  | 3451 | } else { | 
|  | 3452 | *output_length = 0; | 
|  | 3453 | } | 
|  | 3454 |  | 
|  | 3455 | return status; | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3456 | } | 
|  | 3457 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3458 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, | 
|  | 3459 | psa_algorithm_t alg, | 
|  | 3460 | const uint8_t *input, | 
|  | 3461 | size_t input_length, | 
|  | 3462 | uint8_t *output, | 
|  | 3463 | size_t output_size, | 
|  | 3464 | size_t *output_length) | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3465 | { | 
|  | 3466 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3467 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3468 | psa_key_slot_t *slot = NULL; | 
| gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3469 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3470 | if (!PSA_ALG_IS_CIPHER(alg)) { | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3471 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3472 | goto exit; | 
|  | 3473 | } | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3474 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3475 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, | 
|  | 3476 | PSA_KEY_USAGE_DECRYPT, | 
|  | 3477 | alg); | 
|  | 3478 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3479 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3480 | } | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3481 |  | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3482 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3483 | .core = slot->attr | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3484 | }; | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3485 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3486 | if (alg == PSA_ALG_CCM_STAR_NO_TAG && | 
|  | 3487 | input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) { | 
| Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 3488 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3489 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3490 | } 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] | 3491 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3492 | goto exit; | 
|  | 3493 | } | 
|  | 3494 |  | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3495 | status = psa_driver_wrapper_cipher_decrypt( | 
|  | 3496 | &attributes, slot->key.data, slot->key.bytes, | 
|  | 3497 | alg, input, input_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3498 | output, output_size, output_length); | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3499 |  | 
| gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3500 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3501 | unlock_status = psa_unlock_key_slot(slot); | 
|  | 3502 | if (status == PSA_SUCCESS) { | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3503 | status = unlock_status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3504 | } | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3505 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3507 | *output_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3508 | } | 
| Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3509 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3510 | return status; | 
| gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3511 | } | 
|  | 3512 |  | 
|  | 3513 |  | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3514 | /****************************************************************/ | 
|  | 3515 | /* AEAD */ | 
|  | 3516 | /****************************************************************/ | 
| Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3517 |  | 
| Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3518 | /* Helper function to get the base algorithm from its variants. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3519 | static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg) | 
| Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3520 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3521 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg); | 
| Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3522 | } | 
|  | 3523 |  | 
|  | 3524 | /* Helper function to perform common nonce length checks. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3525 | static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg, | 
|  | 3526 | size_t nonce_length) | 
| Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3527 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3528 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg); | 
| Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 3529 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3530 | switch (base_alg) { | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3531 | #if defined(PSA_WANT_ALG_GCM) | 
|  | 3532 | case PSA_ALG_GCM: | 
|  | 3533 | /* Not checking max nonce size here as GCM spec allows almost | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3534 | * arbitrarily large nonces. Please note that we do not generally | 
|  | 3535 | * recommend the usage of nonces of greater length than | 
|  | 3536 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter | 
|  | 3537 | * size, which can then lead to collisions if you encrypt a very | 
|  | 3538 | * large number of messages.*/ | 
|  | 3539 | if (nonce_length != 0) { | 
|  | 3540 | return PSA_SUCCESS; | 
|  | 3541 | } | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3542 | break; | 
| Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3543 | #endif /* PSA_WANT_ALG_GCM */ | 
|  | 3544 | #if defined(PSA_WANT_ALG_CCM) | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3545 | case PSA_ALG_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3546 | if (nonce_length >= 7 && nonce_length <= 13) { | 
|  | 3547 | return PSA_SUCCESS; | 
|  | 3548 | } | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3549 | break; | 
| Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3550 | #endif /* PSA_WANT_ALG_CCM */ | 
|  | 3551 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3552 | case PSA_ALG_CHACHA20_POLY1305: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3553 | if (nonce_length == 12) { | 
|  | 3554 | return PSA_SUCCESS; | 
|  | 3555 | } else if (nonce_length == 8) { | 
|  | 3556 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 3557 | } | 
| Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 3558 | break; | 
| Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3559 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3560 | default: | 
| Przemek Stekiel | 4c49927 | 2022-09-27 13:55:37 +0200 | [diff] [blame] | 3561 | (void) nonce_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3562 | return PSA_ERROR_NOT_SUPPORTED; | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3563 | } | 
| Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3564 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3565 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3566 | } | 
|  | 3567 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3568 | static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg) | 
| Bence Szépkúti | aa3a6e4 | 2022-01-13 16:26:03 +0100 | [diff] [blame] | 3569 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3570 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { | 
|  | 3571 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3572 | } | 
| Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3573 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3574 | return PSA_SUCCESS; | 
| Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 3575 | } | 
|  | 3576 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3577 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, | 
|  | 3578 | psa_algorithm_t alg, | 
|  | 3579 | const uint8_t *nonce, | 
|  | 3580 | size_t nonce_length, | 
|  | 3581 | const uint8_t *additional_data, | 
|  | 3582 | size_t additional_data_length, | 
|  | 3583 | const uint8_t *plaintext, | 
|  | 3584 | size_t plaintext_length, | 
|  | 3585 | uint8_t *ciphertext, | 
|  | 3586 | size_t ciphertext_size, | 
|  | 3587 | size_t *ciphertext_length) | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3588 | { | 
| Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3589 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 3590 | psa_key_slot_t *slot; | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3591 |  | 
| mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3592 | *ciphertext_length = 0; | 
| mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3593 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3594 | status = psa_aead_check_algorithm(alg); | 
|  | 3595 | if (status != PSA_SUCCESS) { | 
|  | 3596 | return status; | 
|  | 3597 | } | 
| Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3598 |  | 
| Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3599 | status = psa_get_and_lock_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3600 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); | 
|  | 3601 | if (status != PSA_SUCCESS) { | 
|  | 3602 | return status; | 
|  | 3603 | } | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3604 |  | 
| Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3605 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3606 | .core = slot->attr | 
| Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3607 | }; | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3608 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3609 | status = psa_aead_check_nonce_length(alg, nonce_length); | 
|  | 3610 | if (status != PSA_SUCCESS) { | 
| Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3611 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3612 | } | 
| Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3613 |  | 
| Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3614 | status = psa_driver_wrapper_aead_encrypt( | 
| Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3615 | &attributes, slot->key.data, slot->key.bytes, | 
|  | 3616 | alg, | 
|  | 3617 | nonce, nonce_length, | 
|  | 3618 | additional_data, additional_data_length, | 
|  | 3619 | plaintext, plaintext_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3620 | ciphertext, ciphertext_size, ciphertext_length); | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3621 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3622 | if (status != PSA_SUCCESS && ciphertext_size != 0) { | 
|  | 3623 | memset(ciphertext, 0, ciphertext_size); | 
|  | 3624 | } | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3625 |  | 
| Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3626 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3627 | psa_unlock_key_slot(slot); | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3628 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3629 | return status; | 
| Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3630 | } | 
|  | 3631 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3632 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, | 
|  | 3633 | psa_algorithm_t alg, | 
|  | 3634 | const uint8_t *nonce, | 
|  | 3635 | size_t nonce_length, | 
|  | 3636 | const uint8_t *additional_data, | 
|  | 3637 | size_t additional_data_length, | 
|  | 3638 | const uint8_t *ciphertext, | 
|  | 3639 | size_t ciphertext_length, | 
|  | 3640 | uint8_t *plaintext, | 
|  | 3641 | size_t plaintext_size, | 
|  | 3642 | size_t *plaintext_length) | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3643 | { | 
| Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3644 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 3645 | psa_key_slot_t *slot; | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3646 |  | 
| Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3647 | *plaintext_length = 0; | 
| mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3648 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3649 | status = psa_aead_check_algorithm(alg); | 
|  | 3650 | if (status != PSA_SUCCESS) { | 
|  | 3651 | return status; | 
|  | 3652 | } | 
| Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3653 |  | 
| Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3654 | status = psa_get_and_lock_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3655 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); | 
|  | 3656 | if (status != PSA_SUCCESS) { | 
|  | 3657 | return status; | 
|  | 3658 | } | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3659 |  | 
| Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3660 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3661 | .core = slot->attr | 
| Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3662 | }; | 
| Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3663 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3664 | status = psa_aead_check_nonce_length(alg, nonce_length); | 
|  | 3665 | if (status != PSA_SUCCESS) { | 
| Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3666 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3667 | } | 
| Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3668 |  | 
| Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3669 | status = psa_driver_wrapper_aead_decrypt( | 
| Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3670 | &attributes, slot->key.data, slot->key.bytes, | 
|  | 3671 | alg, | 
|  | 3672 | nonce, nonce_length, | 
|  | 3673 | additional_data, additional_data_length, | 
|  | 3674 | ciphertext, ciphertext_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3675 | plaintext, plaintext_size, plaintext_length); | 
| Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3676 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3677 | if (status != PSA_SUCCESS && plaintext_size != 0) { | 
|  | 3678 | memset(plaintext, 0, plaintext_size); | 
|  | 3679 | } | 
| mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3680 |  | 
| Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3681 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3682 | psa_unlock_key_slot(slot); | 
| Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3683 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3684 | return status; | 
| mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3685 | } | 
|  | 3686 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3687 | static psa_status_t psa_validate_tag_length(psa_algorithm_t alg) | 
|  | 3688 | { | 
|  | 3689 | const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg); | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3690 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3691 | switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) { | 
| Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3692 | #if defined(PSA_WANT_ALG_CCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3693 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3694 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3695 | if (tag_len < 4 || tag_len > 16 || tag_len % 2) { | 
|  | 3696 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3697 | } | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3698 | break; | 
| Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3699 | #endif /* PSA_WANT_ALG_CCM */ | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3700 |  | 
| Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3701 | #if defined(PSA_WANT_ALG_GCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3702 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3703 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3704 | if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) { | 
|  | 3705 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3706 | } | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3707 | break; | 
| Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3708 | #endif /* PSA_WANT_ALG_GCM */ | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3709 |  | 
| Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3710 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3711 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3712 | /* We only support the default tag length. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3713 | if (tag_len != 16) { | 
|  | 3714 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3715 | } | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3716 | break; | 
| Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 3717 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3718 |  | 
|  | 3719 | default: | 
|  | 3720 | (void) tag_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3721 | return PSA_ERROR_NOT_SUPPORTED; | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3722 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3723 | return PSA_SUCCESS; | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 3724 | } | 
|  | 3725 |  | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3726 | /* Set the key for a multipart authenticated operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3727 | static psa_status_t psa_aead_setup(psa_aead_operation_t *operation, | 
|  | 3728 | int is_encrypt, | 
|  | 3729 | mbedtls_svc_key_id_t key, | 
|  | 3730 | psa_algorithm_t alg) | 
| Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3731 | { | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3732 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 3733 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 3734 | psa_key_slot_t *slot = NULL; | 
|  | 3735 | psa_key_usage_t key_usage = 0; | 
|  | 3736 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3737 | status = psa_aead_check_algorithm(alg); | 
|  | 3738 | if (status != PSA_SUCCESS) { | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3739 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3740 | } | 
| Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3741 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3742 | if (operation->id != 0) { | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3743 | status = PSA_ERROR_BAD_STATE; | 
|  | 3744 | goto exit; | 
| Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3745 | } | 
|  | 3746 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3747 | if (operation->nonce_set || operation->lengths_set || | 
|  | 3748 | operation->ad_started || operation->body_started) { | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3749 | status = PSA_ERROR_BAD_STATE; | 
|  | 3750 | goto exit; | 
| Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3751 | } | 
|  | 3752 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3753 | if (is_encrypt) { | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3754 | key_usage = PSA_KEY_USAGE_ENCRYPT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3755 | } else { | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3756 | key_usage = PSA_KEY_USAGE_DECRYPT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3757 | } | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3758 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3759 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage, | 
|  | 3760 | alg); | 
|  | 3761 | if (status != PSA_SUCCESS) { | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3762 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3763 | } | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3764 |  | 
|  | 3765 | psa_key_attributes_t attributes = { | 
|  | 3766 | .core = slot->attr | 
|  | 3767 | }; | 
|  | 3768 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3769 | if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) { | 
| Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 3770 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3771 | } | 
| Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 3772 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | if (is_encrypt) { | 
|  | 3774 | status = psa_driver_wrapper_aead_encrypt_setup(operation, | 
|  | 3775 | &attributes, | 
|  | 3776 | slot->key.data, | 
|  | 3777 | slot->key.bytes, | 
|  | 3778 | alg); | 
|  | 3779 | } else { | 
|  | 3780 | status = psa_driver_wrapper_aead_decrypt_setup(operation, | 
|  | 3781 | &attributes, | 
|  | 3782 | slot->key.data, | 
|  | 3783 | slot->key.bytes, | 
|  | 3784 | alg); | 
|  | 3785 | } | 
|  | 3786 | if (status != PSA_SUCCESS) { | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3787 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3788 | } | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3789 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3790 | operation->key_type = psa_get_key_type(&attributes); | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3791 |  | 
|  | 3792 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3793 | unlock_status = psa_unlock_key_slot(slot); | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3794 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3795 | if (status == PSA_SUCCESS) { | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3796 | status = unlock_status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3797 | operation->alg = psa_aead_get_base_algorithm(alg); | 
| Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 3798 | operation->is_encrypt = is_encrypt; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3799 | } else { | 
|  | 3800 | psa_aead_abort(operation); | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3801 | } | 
| Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 3802 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3803 | return status; | 
| Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 3804 | } | 
|  | 3805 |  | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3806 | /* Set the key for a multipart authenticated encryption operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3807 | psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation, | 
|  | 3808 | mbedtls_svc_key_id_t key, | 
|  | 3809 | psa_algorithm_t alg) | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3810 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3811 | return psa_aead_setup(operation, 1, key, alg); | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3812 | } | 
|  | 3813 |  | 
|  | 3814 | /* Set the key for a multipart authenticated decryption operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3815 | psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation, | 
|  | 3816 | mbedtls_svc_key_id_t key, | 
|  | 3817 | psa_algorithm_t alg) | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3818 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3819 | return psa_aead_setup(operation, 0, key, alg); | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3820 | } | 
|  | 3821 |  | 
|  | 3822 | /* Generate a random nonce / IV for multipart AEAD operation */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3823 | psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation, | 
|  | 3824 | uint8_t *nonce, | 
|  | 3825 | size_t nonce_size, | 
|  | 3826 | size_t *nonce_length) | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3827 | { | 
|  | 3828 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3829 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; | 
| Paul Elliott | b91da71 | 2021-05-20 14:43:47 +0100 | [diff] [blame] | 3830 | size_t required_nonce_size; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3831 |  | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3832 | *nonce_length = 0; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3833 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3834 | if (operation->id == 0) { | 
| Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3835 | status = PSA_ERROR_BAD_STATE; | 
|  | 3836 | goto exit; | 
|  | 3837 | } | 
|  | 3838 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3839 | if (operation->nonce_set || !operation->is_encrypt) { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3840 | status = PSA_ERROR_BAD_STATE; | 
|  | 3841 | goto exit; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3842 | } | 
|  | 3843 |  | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 3844 | /* For CCM, this size may not be correct according to the PSA | 
|  | 3845 | * specification. The PSA Crypto 1.0.1 specification states: | 
|  | 3846 | * | 
|  | 3847 | * CCM encodes the plaintext length pLen in L octets, with L the smallest | 
|  | 3848 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. | 
|  | 3849 | * | 
|  | 3850 | * However this restriction that L has to be the smallest integer is not | 
|  | 3851 | * applied in practice, and it is not implementable here since the | 
|  | 3852 | * plaintext length may or may not be known at this time. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3853 | required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type, | 
|  | 3854 | operation->alg); | 
|  | 3855 | if (nonce_size < required_nonce_size) { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3856 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 3857 | goto exit; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3858 | } | 
|  | 3859 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3860 | status = psa_generate_random(local_nonce, required_nonce_size); | 
|  | 3861 | if (status != PSA_SUCCESS) { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3862 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3863 | } | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3864 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3865 | status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size); | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3866 |  | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3867 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3868 | if (status == PSA_SUCCESS) { | 
|  | 3869 | memcpy(nonce, local_nonce, required_nonce_size); | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3870 | *nonce_length = required_nonce_size; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3871 | } else { | 
|  | 3872 | psa_aead_abort(operation); | 
| Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 3873 | } | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3874 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3875 | return status; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3876 | } | 
|  | 3877 |  | 
|  | 3878 | /* Set the nonce for a multipart authenticated encryption or decryption | 
|  | 3879 | operation.*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3880 | psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation, | 
|  | 3881 | const uint8_t *nonce, | 
|  | 3882 | size_t nonce_length) | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3883 | { | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3884 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 3885 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3886 | if (operation->id == 0) { | 
| Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3887 | status = PSA_ERROR_BAD_STATE; | 
|  | 3888 | goto exit; | 
|  | 3889 | } | 
|  | 3890 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3891 | if (operation->nonce_set) { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3892 | status = PSA_ERROR_BAD_STATE; | 
|  | 3893 | goto exit; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3894 | } | 
|  | 3895 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3896 | status = psa_aead_check_nonce_length(operation->alg, nonce_length); | 
|  | 3897 | if (status != PSA_SUCCESS) { | 
| Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 3898 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3899 | goto exit; | 
| Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 3900 | } | 
| Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 3901 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3902 | status = psa_driver_wrapper_aead_set_nonce(operation, nonce, | 
|  | 3903 | nonce_length); | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3904 |  | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3905 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3906 | if (status == PSA_SUCCESS) { | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3907 | operation->nonce_set = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3908 | } else { | 
|  | 3909 | psa_aead_abort(operation); | 
|  | 3910 | } | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3911 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3912 | return status; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3913 | } | 
|  | 3914 |  | 
|  | 3915 | /* Declare the lengths of the message and additional data for multipart AEAD. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3916 | psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation, | 
|  | 3917 | size_t ad_length, | 
|  | 3918 | size_t plaintext_length) | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3919 | { | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3920 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 3921 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3922 | if (operation->id == 0) { | 
| Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3923 | status = PSA_ERROR_BAD_STATE; | 
|  | 3924 | goto exit; | 
|  | 3925 | } | 
|  | 3926 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3927 | if (operation->lengths_set || operation->ad_started || | 
|  | 3928 | operation->body_started) { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3929 | status = PSA_ERROR_BAD_STATE; | 
|  | 3930 | goto exit; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3931 | } | 
|  | 3932 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3933 | switch (operation->alg) { | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3934 | #if defined(PSA_WANT_ALG_GCM) | 
|  | 3935 | case PSA_ALG_GCM: | 
|  | 3936 | /* Lengths can only be too large for GCM if size_t is bigger than 32 | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3937 | * bits. Without the guard this code will generate warnings on 32bit | 
|  | 3938 | * builds. */ | 
| Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3939 | #if SIZE_MAX > UINT32_MAX | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3940 | if (((uint64_t) ad_length) >> 61 != 0 || | 
|  | 3941 | ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) { | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3942 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3943 | goto exit; | 
|  | 3944 | } | 
| Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3945 | #endif | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3946 | break; | 
| Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3947 | #endif /* PSA_WANT_ALG_GCM */ | 
|  | 3948 | #if defined(PSA_WANT_ALG_CCM) | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3949 | case PSA_ALG_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3950 | if (ad_length > 0xFF00) { | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3951 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 3952 | goto exit; | 
|  | 3953 | } | 
|  | 3954 | break; | 
| Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3955 | #endif /* PSA_WANT_ALG_CCM */ | 
|  | 3956 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3957 | case PSA_ALG_CHACHA20_POLY1305: | 
|  | 3958 | /* No length restrictions for ChaChaPoly. */ | 
|  | 3959 | break; | 
| Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 3960 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ | 
| Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 3961 | default: | 
|  | 3962 | break; | 
|  | 3963 | } | 
| Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 3964 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3965 | status = psa_driver_wrapper_aead_set_lengths(operation, ad_length, | 
|  | 3966 | plaintext_length); | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3967 |  | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3968 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3969 | if (status == PSA_SUCCESS) { | 
| Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 3970 | operation->ad_remaining = ad_length; | 
|  | 3971 | operation->body_remaining = plaintext_length; | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3972 | operation->lengths_set = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3973 | } else { | 
|  | 3974 | psa_aead_abort(operation); | 
| Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 3975 | } | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3976 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3977 | return status; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3978 | } | 
| Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 3979 |  | 
|  | 3980 | /* Pass additional data to an active multipart AEAD operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3981 | psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation, | 
|  | 3982 | const uint8_t *input, | 
|  | 3983 | size_t input_length) | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3984 | { | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 3985 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 3986 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3987 | if (operation->id == 0) { | 
| Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 3988 | status = PSA_ERROR_BAD_STATE; | 
|  | 3989 | goto exit; | 
|  | 3990 | } | 
|  | 3991 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | if (!operation->nonce_set || operation->body_started) { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 3993 | status = PSA_ERROR_BAD_STATE; | 
|  | 3994 | goto exit; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 3995 | } | 
|  | 3996 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3997 | if (operation->lengths_set) { | 
|  | 3998 | if (operation->ad_remaining < input_length) { | 
| Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 3999 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 4000 | goto exit; | 
|  | 4001 | } | 
|  | 4002 |  | 
|  | 4003 | operation->ad_remaining -= input_length; | 
|  | 4004 | } | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4005 | #if defined(PSA_WANT_ALG_CCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4006 | else if (operation->alg == PSA_ALG_CCM) { | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4007 | status = PSA_ERROR_BAD_STATE; | 
|  | 4008 | goto exit; | 
|  | 4009 | } | 
|  | 4010 | #endif /* PSA_WANT_ALG_CCM */ | 
| Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4011 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4012 | status = psa_driver_wrapper_aead_update_ad(operation, input, | 
|  | 4013 | input_length); | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4014 |  | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4015 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4016 | if (status == PSA_SUCCESS) { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4017 | operation->ad_started = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4018 | } else { | 
|  | 4019 | psa_aead_abort(operation); | 
|  | 4020 | } | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4021 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4022 | return status; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4023 | } | 
|  | 4024 |  | 
|  | 4025 | /* Encrypt or decrypt a message fragment in an active multipart AEAD | 
|  | 4026 | operation.*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4027 | psa_status_t psa_aead_update(psa_aead_operation_t *operation, | 
|  | 4028 | const uint8_t *input, | 
|  | 4029 | size_t input_length, | 
|  | 4030 | uint8_t *output, | 
|  | 4031 | size_t output_size, | 
|  | 4032 | size_t *output_length) | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4033 | { | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4034 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4035 |  | 
|  | 4036 | *output_length = 0; | 
|  | 4037 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4038 | if (operation->id == 0) { | 
| Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4039 | status = PSA_ERROR_BAD_STATE; | 
|  | 4040 | goto exit; | 
|  | 4041 | } | 
|  | 4042 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4043 | if (!operation->nonce_set) { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4044 | status = PSA_ERROR_BAD_STATE; | 
|  | 4045 | goto exit; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4046 | } | 
|  | 4047 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4048 | if (operation->lengths_set) { | 
| Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4049 | /* Additional data length was supplied, but not all the additional | 
|  | 4050 | data was supplied.*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4051 | if (operation->ad_remaining != 0) { | 
| Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4052 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 4053 | goto exit; | 
|  | 4054 | } | 
|  | 4055 |  | 
|  | 4056 | /* Too much data provided. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4057 | if (operation->body_remaining < input_length) { | 
| Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4058 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 4059 | goto exit; | 
|  | 4060 | } | 
|  | 4061 |  | 
|  | 4062 | operation->body_remaining -= input_length; | 
|  | 4063 | } | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4064 | #if defined(PSA_WANT_ALG_CCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4065 | else if (operation->alg == PSA_ALG_CCM) { | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4066 | status = PSA_ERROR_BAD_STATE; | 
|  | 4067 | goto exit; | 
|  | 4068 | } | 
|  | 4069 | #endif /* PSA_WANT_ALG_CCM */ | 
| Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4070 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4071 | status = psa_driver_wrapper_aead_update(operation, input, input_length, | 
|  | 4072 | output, output_size, | 
|  | 4073 | output_length); | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4074 |  | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4075 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4076 | if (status == PSA_SUCCESS) { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4077 | operation->body_started = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4078 | } else { | 
|  | 4079 | psa_aead_abort(operation); | 
|  | 4080 | } | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4081 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4082 | return status; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4083 | } | 
|  | 4084 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4085 | 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] | 4086 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4087 | if (operation->id == 0 || !operation->nonce_set) { | 
|  | 4088 | return PSA_ERROR_BAD_STATE; | 
|  | 4089 | } | 
| Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4090 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4091 | if (operation->lengths_set && (operation->ad_remaining != 0 || | 
|  | 4092 | operation->body_remaining != 0)) { | 
|  | 4093 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 4094 | } | 
| Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4095 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4096 | return PSA_SUCCESS; | 
| Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4097 | } | 
|  | 4098 |  | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4099 | /* Finish encrypting a message in a multipart AEAD operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4100 | psa_status_t psa_aead_finish(psa_aead_operation_t *operation, | 
|  | 4101 | uint8_t *ciphertext, | 
|  | 4102 | size_t ciphertext_size, | 
|  | 4103 | size_t *ciphertext_length, | 
|  | 4104 | uint8_t *tag, | 
|  | 4105 | size_t tag_size, | 
|  | 4106 | size_t *tag_length) | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4107 | { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4108 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 4109 |  | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4110 | *ciphertext_length = 0; | 
| Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 4111 | *tag_length = tag_size; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4112 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4113 | status = psa_aead_final_checks(operation); | 
|  | 4114 | if (status != PSA_SUCCESS) { | 
| Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4115 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4116 | } | 
| Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4117 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4118 | if (!operation->is_encrypt) { | 
| Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4119 | status = PSA_ERROR_BAD_STATE; | 
|  | 4120 | goto exit; | 
|  | 4121 | } | 
|  | 4122 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4123 | status = psa_driver_wrapper_aead_finish(operation, ciphertext, | 
|  | 4124 | ciphertext_size, | 
|  | 4125 | ciphertext_length, | 
|  | 4126 | tag, tag_size, tag_length); | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4127 |  | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4128 | exit: | 
| Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4129 | /* 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] | 4130 | * the zero tag size, make sure the tag is set to something implausible. | 
|  | 4131 | * Even if the operation succeeds, make sure we clear the rest of the | 
|  | 4132 | * buffer to prevent potential leakage of anything previously placed in | 
|  | 4133 | * the same buffer.*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4134 | if (tag != NULL) { | 
|  | 4135 | if (status != PSA_SUCCESS) { | 
|  | 4136 | memset(tag, '!', tag_size); | 
|  | 4137 | } else if (*tag_length < tag_size) { | 
|  | 4138 | memset(tag + *tag_length, '!', (tag_size - *tag_length)); | 
|  | 4139 | } | 
| Paul Elliott | ec95cc9 | 2021-09-19 22:33:09 +0100 | [diff] [blame] | 4140 | } | 
| Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4141 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4142 | psa_aead_abort(operation); | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4143 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4144 | return status; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4145 | } | 
|  | 4146 |  | 
|  | 4147 | /* Finish authenticating and decrypting a message in a multipart AEAD | 
|  | 4148 | operation.*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4149 | psa_status_t psa_aead_verify(psa_aead_operation_t *operation, | 
|  | 4150 | uint8_t *plaintext, | 
|  | 4151 | size_t plaintext_size, | 
|  | 4152 | size_t *plaintext_length, | 
|  | 4153 | const uint8_t *tag, | 
|  | 4154 | size_t tag_length) | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4155 | { | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4156 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 4157 |  | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4158 | *plaintext_length = 0; | 
|  | 4159 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4160 | status = psa_aead_final_checks(operation); | 
|  | 4161 | if (status != PSA_SUCCESS) { | 
| Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4162 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4163 | } | 
| Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4164 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4165 | if (operation->is_encrypt) { | 
| Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4166 | status = PSA_ERROR_BAD_STATE; | 
|  | 4167 | goto exit; | 
|  | 4168 | } | 
|  | 4169 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | status = psa_driver_wrapper_aead_verify(operation, plaintext, | 
|  | 4171 | plaintext_size, | 
|  | 4172 | plaintext_length, | 
|  | 4173 | tag, tag_length); | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4174 |  | 
|  | 4175 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4176 | psa_aead_abort(operation); | 
| Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4177 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4178 | return status; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4179 | } | 
|  | 4180 |  | 
|  | 4181 | /* Abort an AEAD operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4182 | psa_status_t psa_aead_abort(psa_aead_operation_t *operation) | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4183 | { | 
|  | 4184 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 4185 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4186 | if (operation->id == 0) { | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4187 | /* The object has (apparently) been initialized but it is not (yet) | 
|  | 4188 | * in use. It's ok to call abort on such an object, and there's | 
|  | 4189 | * nothing to do. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4190 | return PSA_SUCCESS; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4191 | } | 
|  | 4192 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4193 | status = psa_driver_wrapper_aead_abort(operation); | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4194 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4195 | memset(operation, 0, sizeof(*operation)); | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4196 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4197 | return status; | 
| Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4198 | } | 
|  | 4199 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4200 | /****************************************************************/ | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4201 | /* Generators */ | 
|  | 4202 | /****************************************************************/ | 
|  | 4203 |  | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4204 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4205 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4206 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \ | 
|  | 4207 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4208 | #define AT_LEAST_ONE_BUILTIN_KDF | 
| Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4209 | #endif /* At least one builtin KDF */ | 
|  | 4210 |  | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4211 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ | 
| Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4212 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ | 
|  | 4213 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) | 
|  | 4214 | static psa_status_t psa_key_derivation_start_hmac( | 
|  | 4215 | psa_mac_operation_t *operation, | 
|  | 4216 | psa_algorithm_t hash_alg, | 
|  | 4217 | const uint8_t *hmac_key, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4218 | size_t hmac_key_length) | 
| Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4219 | { | 
|  | 4220 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 4221 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4222 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); | 
|  | 4223 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); | 
|  | 4224 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); | 
| Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4225 |  | 
| Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4226 | operation->is_sign = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4227 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); | 
| Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4228 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4229 | status = psa_driver_wrapper_mac_sign_setup(operation, | 
|  | 4230 | &attributes, | 
|  | 4231 | hmac_key, hmac_key_length, | 
|  | 4232 | PSA_ALG_HMAC(hash_alg)); | 
| Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4233 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4234 | psa_reset_key_attributes(&attributes); | 
|  | 4235 | return status; | 
| Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4236 | } | 
|  | 4237 | #endif /* KDF algorithms reliant on HMAC */ | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4238 |  | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4239 | #define HKDF_STATE_INIT 0 /* no input yet */ | 
|  | 4240 | #define HKDF_STATE_STARTED 1 /* got salt */ | 
|  | 4241 | #define HKDF_STATE_KEYED 2 /* got key */ | 
|  | 4242 | #define HKDF_STATE_OUTPUT 3 /* output started */ | 
|  | 4243 |  | 
| Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4244 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4245 | const psa_key_derivation_operation_t *operation) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4246 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4247 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { | 
|  | 4248 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); | 
|  | 4249 | } else { | 
|  | 4250 | return operation->alg; | 
|  | 4251 | } | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4252 | } | 
|  | 4253 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4254 | 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] | 4255 | { | 
|  | 4256 | psa_status_t status = PSA_SUCCESS; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4257 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); | 
|  | 4258 | if (kdf_alg == 0) { | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4259 | /* The object has (apparently) been initialized but it is not | 
|  | 4260 | * in use. It's ok to call abort on such an object, and there's | 
|  | 4261 | * nothing to do. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4262 | } else | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4263 | #if defined(BUILTIN_ALG_ANY_HKDF) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4264 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { | 
|  | 4265 | mbedtls_free(operation->ctx.hkdf.info); | 
|  | 4266 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); | 
|  | 4267 | } else | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4268 | #endif /* BUILTIN_ALG_ANY_HKDF */ | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4269 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ | 
|  | 4270 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4271 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || | 
|  | 4272 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ | 
|  | 4273 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { | 
|  | 4274 | if (operation->ctx.tls12_prf.secret != NULL) { | 
|  | 4275 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret, | 
|  | 4276 | operation->ctx.tls12_prf.secret_length); | 
|  | 4277 | mbedtls_free(operation->ctx.tls12_prf.secret); | 
| Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4278 | } | 
|  | 4279 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | if (operation->ctx.tls12_prf.seed != NULL) { | 
|  | 4281 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed, | 
|  | 4282 | operation->ctx.tls12_prf.seed_length); | 
|  | 4283 | mbedtls_free(operation->ctx.tls12_prf.seed); | 
| Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4284 | } | 
|  | 4285 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4286 | if (operation->ctx.tls12_prf.label != NULL) { | 
|  | 4287 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.label, | 
|  | 4288 | operation->ctx.tls12_prf.label_length); | 
|  | 4289 | mbedtls_free(operation->ctx.tls12_prf.label); | 
| Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4290 | } | 
|  | 4291 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4292 | if (operation->ctx.tls12_prf.other_secret != NULL) { | 
|  | 4293 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret, | 
|  | 4294 | operation->ctx.tls12_prf.other_secret_length); | 
|  | 4295 | mbedtls_free(operation->ctx.tls12_prf.other_secret); | 
| Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 4296 | } | 
|  | 4297 |  | 
| Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4298 | status = PSA_SUCCESS; | 
| Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 4299 |  | 
|  | 4300 | /* We leave the fields Ai and output_block to be erased safely by the | 
|  | 4301 | * mbedtls_platform_zeroize() in the end of this function. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4302 | } else | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4303 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || | 
|  | 4304 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4305 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4306 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { | 
|  | 4307 | mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data, | 
|  | 4308 | sizeof(operation->ctx.tls12_ecjpake_to_pms.data)); | 
|  | 4309 | } else | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4310 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */ | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4311 | { | 
|  | 4312 | status = PSA_ERROR_BAD_STATE; | 
|  | 4313 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4314 | mbedtls_platform_zeroize(operation, sizeof(*operation)); | 
|  | 4315 | return status; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4316 | } | 
|  | 4317 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4318 | psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4319 | size_t *capacity) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4320 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4321 | if (operation->alg == 0) { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4322 | /* This is a blank key derivation operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4323 | return PSA_ERROR_BAD_STATE; | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4324 | } | 
|  | 4325 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4326 | *capacity = operation->capacity; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4327 | return PSA_SUCCESS; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4328 | } | 
|  | 4329 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4330 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, | 
|  | 4331 | size_t capacity) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4332 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4333 | if (operation->alg == 0) { | 
|  | 4334 | return PSA_ERROR_BAD_STATE; | 
|  | 4335 | } | 
|  | 4336 | if (capacity > operation->capacity) { | 
|  | 4337 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 4338 | } | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4339 | operation->capacity = capacity; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4340 | return PSA_SUCCESS; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4341 | } | 
|  | 4342 |  | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4343 | #if defined(BUILTIN_ALG_ANY_HKDF) | 
| Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4344 | /* Read some bytes from an HKDF-based operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4345 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, | 
|  | 4346 | psa_algorithm_t kdf_alg, | 
|  | 4347 | uint8_t *output, | 
|  | 4348 | size_t output_length) | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4349 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4350 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); | 
|  | 4351 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); | 
| Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4352 | size_t hmac_output_length; | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4353 | psa_status_t status; | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4354 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4355 | 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] | 4356 | #else | 
|  | 4357 | const uint8_t last_block = 0xff; | 
|  | 4358 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4359 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | if (hkdf->state < HKDF_STATE_KEYED || | 
|  | 4361 | (!hkdf->info_set | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4362 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4363 | && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 4364 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4365 | )) { | 
|  | 4366 | return PSA_ERROR_BAD_STATE; | 
|  | 4367 | } | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4368 | hkdf->state = HKDF_STATE_OUTPUT; | 
|  | 4369 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4370 | while (output_length != 0) { | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4371 | /* Copy what remains of the current block */ | 
|  | 4372 | uint8_t n = hash_length - hkdf->offset_in_block; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4373 | if (n > output_length) { | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4374 | n = (uint8_t) output_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4375 | } | 
|  | 4376 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4377 | output += n; | 
|  | 4378 | output_length -= n; | 
|  | 4379 | hkdf->offset_in_block += n; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4380 | if (output_length == 0) { | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4381 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4382 | } | 
| Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 4383 | /* We can't be wanting more output after the last block, otherwise | 
| Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4384 | * the capacity check in psa_key_derivation_output_bytes() would have | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4385 | * prevented this call. It could happen only if the operation | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4386 | * object was corrupted or if this function is called directly | 
|  | 4387 | * inside the library. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4388 | if (hkdf->block_number == last_block) { | 
|  | 4389 | return PSA_ERROR_BAD_STATE; | 
|  | 4390 | } | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4391 |  | 
|  | 4392 | /* We need a new block */ | 
|  | 4393 | ++hkdf->block_number; | 
|  | 4394 | hkdf->offset_in_block = 0; | 
| Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4395 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4396 | status = psa_key_derivation_start_hmac(&hkdf->hmac, | 
|  | 4397 | hash_alg, | 
|  | 4398 | hkdf->prk, | 
|  | 4399 | hash_length); | 
|  | 4400 | if (status != PSA_SUCCESS) { | 
|  | 4401 | return status; | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4402 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4403 |  | 
|  | 4404 | if (hkdf->block_number != 1) { | 
|  | 4405 | status = psa_mac_update(&hkdf->hmac, | 
|  | 4406 | hkdf->output_block, | 
|  | 4407 | hash_length); | 
|  | 4408 | if (status != PSA_SUCCESS) { | 
|  | 4409 | return status; | 
|  | 4410 | } | 
|  | 4411 | } | 
|  | 4412 | status = psa_mac_update(&hkdf->hmac, | 
|  | 4413 | hkdf->info, | 
|  | 4414 | hkdf->info_length); | 
|  | 4415 | if (status != PSA_SUCCESS) { | 
|  | 4416 | return status; | 
|  | 4417 | } | 
|  | 4418 | status = psa_mac_update(&hkdf->hmac, | 
|  | 4419 | &hkdf->block_number, 1); | 
|  | 4420 | if (status != PSA_SUCCESS) { | 
|  | 4421 | return status; | 
|  | 4422 | } | 
|  | 4423 | status = psa_mac_sign_finish(&hkdf->hmac, | 
|  | 4424 | hkdf->output_block, | 
|  | 4425 | sizeof(hkdf->output_block), | 
|  | 4426 | &hmac_output_length); | 
|  | 4427 | if (status != PSA_SUCCESS) { | 
|  | 4428 | return status; | 
|  | 4429 | } | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4430 | } | 
|  | 4431 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4432 | return PSA_SUCCESS; | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4433 | } | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4434 | #endif /* BUILTIN_ALG_ANY_HKDF */ | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4435 |  | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4436 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ | 
|  | 4437 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4438 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( | 
|  | 4439 | psa_tls12_prf_key_derivation_t *tls12_prf, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4440 | psa_algorithm_t alg) | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4441 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4442 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); | 
|  | 4443 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); | 
| Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4444 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; | 
|  | 4445 | size_t hmac_output_length; | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4446 | psa_status_t status, cleanup_status; | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4447 |  | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4448 | /* We can't be wanting more output after block 0xff, otherwise | 
|  | 4449 | * the capacity check in psa_key_derivation_output_bytes() would have | 
|  | 4450 | * prevented this call. It could happen only if the operation | 
|  | 4451 | * object was corrupted or if this function is called directly | 
|  | 4452 | * inside the library. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4453 | if (tls12_prf->block_number == 0xff) { | 
|  | 4454 | return PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 4455 | } | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4456 |  | 
|  | 4457 | /* We need a new block */ | 
|  | 4458 | ++tls12_prf->block_number; | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4459 | tls12_prf->left_in_block = hash_length; | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4460 |  | 
|  | 4461 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: | 
|  | 4462 | * | 
|  | 4463 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) | 
|  | 4464 | * | 
|  | 4465 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + | 
|  | 4466 | *                        HMAC_hash(secret, A(2) + seed) + | 
|  | 4467 | *                        HMAC_hash(secret, A(3) + seed) + ... | 
|  | 4468 | * | 
|  | 4469 | * A(0) = seed | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4470 | * A(i) = HMAC_hash(secret, A(i-1)) | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4471 | * | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4472 | * The `psa_tls12_prf_key_derivation` structure saves the block | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4473 | * `HMAC_hash(secret, A(i) + seed)` from which the output | 
| Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4474 | * is currently extracted as `output_block` and where i is | 
|  | 4475 | * `block_number`. | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4476 | */ | 
|  | 4477 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4478 | status = psa_key_derivation_start_hmac(&hmac, | 
|  | 4479 | hash_alg, | 
|  | 4480 | tls12_prf->secret, | 
|  | 4481 | tls12_prf->secret_length); | 
|  | 4482 | if (status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4483 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | } | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4485 |  | 
|  | 4486 | /* Calculate A(i) where i = tls12_prf->block_number. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4487 | if (tls12_prf->block_number == 1) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4488 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads | 
|  | 4489 | * the variable seed and in this instance means it in the context of the | 
|  | 4490 | * P_hash function, where seed = label + seed.) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4491 | status = psa_mac_update(&hmac, | 
|  | 4492 | tls12_prf->label, | 
|  | 4493 | tls12_prf->label_length); | 
|  | 4494 | if (status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4495 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4496 | } | 
|  | 4497 | status = psa_mac_update(&hmac, | 
|  | 4498 | tls12_prf->seed, | 
|  | 4499 | tls12_prf->seed_length); | 
|  | 4500 | if (status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4501 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4502 | } | 
|  | 4503 | } else { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4504 | /* A(i) = HMAC_hash(secret, A(i-1)) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4505 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); | 
|  | 4506 | if (status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4507 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4508 | } | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4509 | } | 
|  | 4510 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | status = psa_mac_sign_finish(&hmac, | 
|  | 4512 | tls12_prf->Ai, hash_length, | 
|  | 4513 | &hmac_output_length); | 
|  | 4514 | if (hmac_output_length != hash_length) { | 
| Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4515 | status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4516 | } | 
|  | 4517 | if (status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4518 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4519 | } | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4520 |  | 
|  | 4521 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | status = psa_key_derivation_start_hmac(&hmac, | 
|  | 4523 | hash_alg, | 
|  | 4524 | tls12_prf->secret, | 
|  | 4525 | tls12_prf->secret_length); | 
|  | 4526 | if (status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4527 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4528 | } | 
|  | 4529 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); | 
|  | 4530 | if (status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4531 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4532 | } | 
|  | 4533 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); | 
|  | 4534 | if (status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4535 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4536 | } | 
|  | 4537 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); | 
|  | 4538 | if (status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4539 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4540 | } | 
|  | 4541 | status = psa_mac_sign_finish(&hmac, | 
|  | 4542 | tls12_prf->output_block, hash_length, | 
|  | 4543 | &hmac_output_length); | 
|  | 4544 | if (status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4545 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4546 | } | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4547 |  | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4548 |  | 
|  | 4549 | cleanup: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4550 | cleanup_status = psa_mac_abort(&hmac); | 
|  | 4551 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4552 | status = cleanup_status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4553 | } | 
| Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4554 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4555 | return status; | 
| Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4556 | } | 
| Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4557 |  | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4558 | static psa_status_t psa_key_derivation_tls12_prf_read( | 
|  | 4559 | psa_tls12_prf_key_derivation_t *tls12_prf, | 
|  | 4560 | psa_algorithm_t alg, | 
|  | 4561 | uint8_t *output, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4562 | size_t output_length) | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4563 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4564 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); | 
|  | 4565 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4566 | psa_status_t status; | 
|  | 4567 | uint8_t offset, length; | 
|  | 4568 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4569 | switch (tls12_prf->state) { | 
| Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4570 | case PSA_TLS12_PRF_STATE_LABEL_SET: | 
|  | 4571 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; | 
|  | 4572 | break; | 
|  | 4573 | case PSA_TLS12_PRF_STATE_OUTPUT: | 
|  | 4574 | break; | 
|  | 4575 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | return PSA_ERROR_BAD_STATE; | 
| Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4577 | } | 
|  | 4578 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4579 | while (output_length != 0) { | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4580 | /* Check if we have fully processed the current block. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4581 | if (tls12_prf->left_in_block == 0) { | 
|  | 4582 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, | 
|  | 4583 | alg); | 
|  | 4584 | if (status != PSA_SUCCESS) { | 
|  | 4585 | return status; | 
|  | 4586 | } | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4587 |  | 
|  | 4588 | continue; | 
|  | 4589 | } | 
|  | 4590 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4591 | if (tls12_prf->left_in_block > output_length) { | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4592 | length = (uint8_t) output_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | } else { | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4594 | length = tls12_prf->left_in_block; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4595 | } | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4596 |  | 
|  | 4597 | offset = hash_length - tls12_prf->left_in_block; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4598 | memcpy(output, tls12_prf->output_block + offset, length); | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4599 | output += length; | 
|  | 4600 | output_length -= length; | 
|  | 4601 | tls12_prf->left_in_block -= length; | 
|  | 4602 | } | 
|  | 4603 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4604 | return PSA_SUCCESS; | 
| Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4605 | } | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4606 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || | 
|  | 4607 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ | 
| Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4608 |  | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4609 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) | 
|  | 4610 | static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read( | 
|  | 4611 | psa_tls12_ecjpake_to_pms_t *ecjpake, | 
|  | 4612 | uint8_t *output, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4613 | size_t output_length) | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4614 | { | 
|  | 4615 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 4616 | size_t output_size = 0; | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4617 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4618 | if (output_length != 32) { | 
|  | 4619 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 4620 | } | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4621 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4622 | status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data, | 
|  | 4623 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length, | 
|  | 4624 | &output_size); | 
|  | 4625 | if (status != PSA_SUCCESS) { | 
|  | 4626 | return status; | 
|  | 4627 | } | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4628 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4629 | if (output_size != output_length) { | 
|  | 4630 | return PSA_ERROR_GENERIC_ERROR; | 
|  | 4631 | } | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4632 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4633 | return PSA_SUCCESS; | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4634 | } | 
|  | 4635 | #endif | 
|  | 4636 |  | 
| Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4637 | psa_status_t psa_key_derivation_output_bytes( | 
|  | 4638 | psa_key_derivation_operation_t *operation, | 
|  | 4639 | uint8_t *output, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | size_t output_length) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4641 | { | 
|  | 4642 | psa_status_t status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4643 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4644 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4645 | if (operation->alg == 0) { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4646 | /* This is a blank operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4647 | return PSA_ERROR_BAD_STATE; | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4648 | } | 
|  | 4649 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4650 | if (output_length > operation->capacity) { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4651 | operation->capacity = 0; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4652 | /* Go through the error path to wipe all confidential data now | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4653 | * that the operation object is useless. */ | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4654 | status = PSA_ERROR_INSUFFICIENT_DATA; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4655 | goto exit; | 
|  | 4656 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4657 | if (output_length == 0 && operation->capacity == 0) { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4658 | /* Edge case: this is a finished operation, and 0 bytes | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4659 | * were requested. The right error in this case could | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4660 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return | 
|  | 4661 | * INSUFFICIENT_CAPACITY, which is right for a finished | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4662 | * operation, for consistency with the case when | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4663 | * output_length > 0. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4664 | return PSA_ERROR_INSUFFICIENT_DATA; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4665 | } | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4666 | operation->capacity -= output_length; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4667 |  | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4668 | #if defined(BUILTIN_ALG_ANY_HKDF) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4669 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { | 
|  | 4670 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg, | 
|  | 4671 | output, output_length); | 
|  | 4672 | } else | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4673 | #endif /* BUILTIN_ALG_ANY_HKDF */ | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4674 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ | 
|  | 4675 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4676 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || | 
|  | 4677 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { | 
|  | 4678 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, | 
|  | 4679 | kdf_alg, output, | 
|  | 4680 | output_length); | 
|  | 4681 | } else | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4682 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || | 
|  | 4683 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4684 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4685 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4686 | status = psa_key_derivation_tls12_ecjpake_to_pms_read( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4687 | &operation->ctx.tls12_ecjpake_to_pms, output, output_length); | 
|  | 4688 | } else | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4689 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ | 
|  | 4690 |  | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4691 | { | 
| Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4692 | (void) kdf_alg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4693 | return PSA_ERROR_BAD_STATE; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4694 | } | 
|  | 4695 |  | 
|  | 4696 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4697 | if (status != PSA_SUCCESS) { | 
| Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4698 | /* Preserve the algorithm upon errors, but clear all sensitive state. | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4699 | * This allows us to differentiate between exhausted operations and | 
|  | 4700 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank | 
|  | 4701 | * operations. */ | 
|  | 4702 | psa_algorithm_t alg = operation->alg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4703 | psa_key_derivation_abort(operation); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4704 | operation->alg = alg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4705 | memset(output, '!', output_length); | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4706 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4707 | return status; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4708 | } | 
|  | 4709 |  | 
| David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4710 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4711 | static void psa_des_set_key_parity(uint8_t *data, size_t data_size) | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4712 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4713 | if (data_size >= 8) { | 
|  | 4714 | mbedtls_des_key_set_parity(data); | 
|  | 4715 | } | 
|  | 4716 | if (data_size >= 16) { | 
|  | 4717 | mbedtls_des_key_set_parity(data + 8); | 
|  | 4718 | } | 
|  | 4719 | if (data_size >= 24) { | 
|  | 4720 | mbedtls_des_key_set_parity(data + 16); | 
|  | 4721 | } | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4722 | } | 
| David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4723 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ | 
| Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4724 |  | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4725 | /* | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | * ECC keys on a Weierstrass elliptic curve require the generation | 
|  | 4727 | * of a private key which is an integer | 
|  | 4728 | * in the range [1, N - 1], where N is the boundary of the private key domain: | 
|  | 4729 | * N is the prime p for Diffie-Hellman, or the order of the | 
|  | 4730 | * curve’s base point for ECC. | 
|  | 4731 | * | 
|  | 4732 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). | 
|  | 4733 | * This function generates the private key using the following process: | 
|  | 4734 | * | 
|  | 4735 | * 1. Draw a byte string of length ceiling(m/8) bytes. | 
|  | 4736 | * 2. If m is not a multiple of 8, set the most significant | 
|  | 4737 | *    (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. | 
|  | 4738 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. | 
|  | 4739 | * 4. If k > N - 2, discard the result and return to step 1. | 
|  | 4740 | * 5. Output k + 1 as the private key. | 
|  | 4741 | * | 
|  | 4742 | * This method allows compliance to NIST standards, specifically the methods titled | 
|  | 4743 | * Key-Pair Generation by Testing Candidates in the following publications: | 
|  | 4744 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment | 
|  | 4745 | *   Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for | 
|  | 4746 | *   Diffie-Hellman keys. | 
|  | 4747 | * | 
|  | 4748 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature | 
|  | 4749 | *   Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. | 
|  | 4750 | * | 
|  | 4751 | * Note: Function allocates memory for *data buffer, so given *data should be | 
|  | 4752 | *       always NULL. | 
|  | 4753 | */ | 
| Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4754 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ | 
|  | 4755 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ | 
|  | 4756 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ | 
|  | 4757 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ | 
|  | 4758 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) | 
| Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4759 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4760 | psa_key_slot_t *slot, | 
|  | 4761 | size_t bits, | 
|  | 4762 | psa_key_derivation_operation_t *operation, | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4763 | uint8_t **data | 
|  | 4764 | ) | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4765 | { | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4766 | unsigned key_out_of_range = 1; | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4767 | mbedtls_mpi k; | 
|  | 4768 | mbedtls_mpi diff_N_2; | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4769 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4770 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4771 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4772 | mbedtls_mpi_init(&k); | 
|  | 4773 | mbedtls_mpi_init(&diff_N_2); | 
| Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4774 |  | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4775 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4776 | slot->attr.type); | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4777 | mbedtls_ecp_group_id grp_id = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4778 | mbedtls_ecc_group_of_psa(curve, bits, 0); | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4779 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4780 | if (grp_id == MBEDTLS_ECP_DP_NONE) { | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4781 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; | 
| Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 4782 | goto cleanup; | 
|  | 4783 | } | 
|  | 4784 |  | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4785 | mbedtls_ecp_group ecp_group; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4786 | mbedtls_ecp_group_init(&ecp_group); | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4787 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4788 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id)); | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4789 |  | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4790 | /* N is the boundary of the private key domain (ecp_group.N). */ | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4791 | /* Let m be the bit size of N. */ | 
|  | 4792 | size_t m = ecp_group.nbits; | 
|  | 4793 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4794 | size_t m_bytes = PSA_BITS_TO_BYTES(m); | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4795 |  | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4796 | /* Calculate N - 2 - it will be needed later. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4797 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2)); | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4798 |  | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4799 | /* Note: This function is always called with *data == NULL and it | 
|  | 4800 | * allocates memory for the data buffer. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4801 | *data = mbedtls_calloc(1, m_bytes); | 
|  | 4802 | if (*data == NULL) { | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4803 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4804 | goto cleanup; | 
|  | 4805 | } | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4806 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4807 | while (key_out_of_range) { | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4808 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) { | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4810 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4811 | } | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4812 |  | 
|  | 4813 | /* 2. If m is not a multiple of 8 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | if (m % 8 != 0) { | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4815 | /* Set the most significant | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4816 | * (8 * ceiling(m/8) - m) bits of the first byte in | 
|  | 4817 | * the string to zero. | 
|  | 4818 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4819 | uint8_t clear_bit_mask = (1 << (m % 8)) - 1; | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4820 | (*data)[0] &= clear_bit_mask; | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4821 | } | 
|  | 4822 |  | 
|  | 4823 | /* 3. Convert the string to integer k by decoding it as a | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | *    big-endian byte string. | 
|  | 4825 | */ | 
|  | 4826 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes)); | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4827 |  | 
|  | 4828 | /* 4. If k > N - 2, discard the result and return to step 1. | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4829 | *    Result of comparison is returned. When it indicates error | 
|  | 4830 | *    then this function is called again. | 
|  | 4831 | */ | 
|  | 4832 | 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] | 4833 | } | 
|  | 4834 |  | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4835 | /* 5. Output k + 1 as the private key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4836 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1)); | 
|  | 4837 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes)); | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4838 | cleanup: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4839 | if (ret != 0) { | 
|  | 4840 | status = mbedtls_to_psa_error(ret); | 
|  | 4841 | } | 
|  | 4842 | if (status != PSA_SUCCESS) { | 
|  | 4843 | mbedtls_free(*data); | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4844 | *data = NULL; | 
|  | 4845 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4846 | mbedtls_mpi_free(&k); | 
|  | 4847 | mbedtls_mpi_free(&diff_N_2); | 
|  | 4848 | return status; | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4849 | } | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4850 |  | 
|  | 4851 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length | 
|  | 4852 | * is determined by the curve, and sets the mandatory bits accordingly. That is: | 
|  | 4853 | * | 
|  | 4854 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): | 
|  | 4855 | *   draw a 32-byte string and process it as specified in | 
|  | 4856 | *   Elliptic Curves for Security [RFC7748] §5. | 
|  | 4857 | * | 
|  | 4858 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): | 
|  | 4859 | *   draw a 56-byte string and process it as specified in [RFC7748] §5. | 
|  | 4860 | * | 
|  | 4861 | * Note: Function allocates memory for *data buffer, so given *data should be | 
|  | 4862 | *       always NULL. | 
|  | 4863 | */ | 
|  | 4864 |  | 
|  | 4865 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( | 
|  | 4866 | size_t bits, | 
|  | 4867 | psa_key_derivation_operation_t *operation, | 
|  | 4868 | uint8_t **data | 
|  | 4869 | ) | 
|  | 4870 | { | 
|  | 4871 | size_t output_length; | 
| Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4872 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4873 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | switch (bits) { | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4875 | case 255: | 
|  | 4876 | output_length = 32; | 
|  | 4877 | break; | 
|  | 4878 | case 448: | 
|  | 4879 | output_length = 56; | 
|  | 4880 | break; | 
|  | 4881 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4882 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4883 | break; | 
|  | 4884 | } | 
|  | 4885 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4886 | *data = mbedtls_calloc(1, output_length); | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4887 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4888 | if (*data == NULL) { | 
|  | 4889 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 4890 | } | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4891 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4892 | status = psa_key_derivation_output_bytes(operation, *data, output_length); | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4893 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4894 | if (status != PSA_SUCCESS) { | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4895 | return status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4896 | } | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4897 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4898 | switch (bits) { | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4899 | case 255: | 
|  | 4900 | (*data)[0] &= 248; | 
|  | 4901 | (*data)[31] &= 127; | 
|  | 4902 | (*data)[31] |= 64; | 
|  | 4903 | break; | 
|  | 4904 | case 448: | 
|  | 4905 | (*data)[0] &= 252; | 
|  | 4906 | (*data)[55] |= 128; | 
|  | 4907 | break; | 
|  | 4908 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4909 | return PSA_ERROR_CORRUPTION_DETECTED; | 
| Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 4910 | break; | 
|  | 4911 | } | 
|  | 4912 |  | 
|  | 4913 | return status; | 
|  | 4914 | } | 
| Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4915 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || | 
|  | 4916 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || | 
|  | 4917 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || | 
|  | 4918 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || | 
|  | 4919 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ | 
| Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 4920 |  | 
| Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4921 | static psa_status_t psa_generate_derived_key_internal( | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4922 | psa_key_slot_t *slot, | 
|  | 4923 | size_t bits, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4924 | psa_key_derivation_operation_t *operation) | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4925 | { | 
|  | 4926 | uint8_t *data = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4927 | size_t bytes = PSA_BITS_TO_BYTES(bits); | 
| Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4928 | size_t storage_size = bytes; | 
| Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 4929 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4930 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4931 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { | 
|  | 4932 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 4933 | } | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4934 |  | 
| Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4935 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ | 
|  | 4936 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ | 
|  | 4937 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ | 
|  | 4938 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ | 
|  | 4939 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4940 | if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) { | 
|  | 4941 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type); | 
|  | 4942 | if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) { | 
| Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4943 | /* Weierstrass elliptic curve */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4944 | status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data); | 
|  | 4945 | if (status != PSA_SUCCESS) { | 
| Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 4946 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4947 | } | 
|  | 4948 | } else { | 
| Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4949 | /* Montgomery elliptic curve */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4950 | status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data); | 
|  | 4951 | if (status != PSA_SUCCESS) { | 
| Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4952 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4953 | } | 
| Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 4954 | } | 
| Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 4955 | } else | 
| Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 4956 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || | 
|  | 4957 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || | 
|  | 4958 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || | 
|  | 4959 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || | 
|  | 4960 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4961 | if (key_type_is_raw_bytes(slot->attr.type)) { | 
|  | 4962 | if (bits % 8 != 0) { | 
|  | 4963 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 4964 | } | 
|  | 4965 | data = mbedtls_calloc(1, bytes); | 
|  | 4966 | if (data == NULL) { | 
|  | 4967 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 4968 | } | 
| Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4969 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4970 | status = psa_key_derivation_output_bytes(operation, data, bytes); | 
|  | 4971 | if (status != PSA_SUCCESS) { | 
| Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4972 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4973 | } | 
| David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4974 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4975 | if (slot->attr.type == PSA_KEY_TYPE_DES) { | 
|  | 4976 | psa_des_set_key_parity(data, bytes); | 
|  | 4977 | } | 
| Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 4978 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4979 | } else { | 
|  | 4980 | return PSA_ERROR_NOT_SUPPORTED; | 
| Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 4981 | } | 
| Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4982 |  | 
| Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4983 | slot->attr.bits = (psa_key_bits_t) bits; | 
| Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4984 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4985 | .core = slot->attr | 
| Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4986 | }; | 
|  | 4987 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4988 | if (psa_key_lifetime_is_external(attributes.core.lifetime)) { | 
|  | 4989 | status = psa_driver_wrapper_get_key_buffer_size(&attributes, | 
|  | 4990 | &storage_size); | 
|  | 4991 | if (status != PSA_SUCCESS) { | 
| Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4992 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4993 | } | 
| Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4994 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4995 | status = psa_allocate_buffer_to_slot(slot, storage_size); | 
|  | 4996 | if (status != PSA_SUCCESS) { | 
| Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4997 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4998 | } | 
| Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 4999 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5000 | status = psa_driver_wrapper_import_key(&attributes, | 
|  | 5001 | data, bytes, | 
|  | 5002 | slot->key.data, | 
|  | 5003 | slot->key.bytes, | 
|  | 5004 | &slot->key.bytes, &bits); | 
|  | 5005 | if (bits != slot->attr.bits) { | 
| Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5006 | status = PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5007 | } | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5008 |  | 
|  | 5009 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | mbedtls_free(data); | 
|  | 5011 | return status; | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5012 | } | 
|  | 5013 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5014 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, | 
|  | 5015 | psa_key_derivation_operation_t *operation, | 
|  | 5016 | mbedtls_svc_key_id_t *key) | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5017 | { | 
|  | 5018 | psa_status_t status; | 
|  | 5019 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5020 | psa_se_drv_table_entry_t *driver = NULL; | 
| Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5021 |  | 
| Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5022 | *key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 5023 |  | 
| Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5024 | /* Reject any attempt to create a zero-length key so that we don't | 
|  | 5025 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5026 | if (psa_get_key_bits(attributes) == 0) { | 
|  | 5027 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5028 | } | 
| Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5029 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5030 | if (operation->alg == PSA_ALG_NONE) { | 
|  | 5031 | return PSA_ERROR_BAD_STATE; | 
|  | 5032 | } | 
| Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 5033 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5034 | if (!operation->can_output_key) { | 
|  | 5035 | return PSA_ERROR_NOT_PERMITTED; | 
|  | 5036 | } | 
| Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5037 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5038 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, | 
|  | 5039 | &slot, &driver); | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5040 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5041 | if (driver != NULL) { | 
| Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5042 | /* Deriving a key in a secure element is not implemented yet. */ | 
|  | 5043 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 5044 | } | 
|  | 5045 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5046 | if (status == PSA_SUCCESS) { | 
|  | 5047 | status = psa_generate_derived_key_internal(slot, | 
|  | 5048 | attributes->core.bits, | 
|  | 5049 | operation); | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5050 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5051 | if (status == PSA_SUCCESS) { | 
|  | 5052 | status = psa_finish_key_creation(slot, driver, key); | 
|  | 5053 | } | 
|  | 5054 | if (status != PSA_SUCCESS) { | 
|  | 5055 | psa_fail_key_creation(slot, driver); | 
|  | 5056 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5057 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5058 | return status; | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5059 | } | 
|  | 5060 |  | 
| Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5061 |  | 
|  | 5062 |  | 
|  | 5063 | /****************************************************************/ | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5064 | /* Key derivation */ | 
|  | 5065 | /****************************************************************/ | 
|  | 5066 |  | 
| Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5067 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5068 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) | 
| Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5069 | { | 
|  | 5070 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5071 | if (PSA_ALG_IS_HKDF(kdf_alg)) { | 
|  | 5072 | return 1; | 
|  | 5073 | } | 
| Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5074 | #endif | 
| Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5075 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5076 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { | 
|  | 5077 | return 1; | 
|  | 5078 | } | 
| Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5079 | #endif | 
|  | 5080 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5081 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { | 
|  | 5082 | return 1; | 
|  | 5083 | } | 
| Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5084 | #endif | 
|  | 5085 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5086 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { | 
|  | 5087 | return 1; | 
|  | 5088 | } | 
| Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5089 | #endif | 
|  | 5090 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5091 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { | 
|  | 5092 | return 1; | 
|  | 5093 | } | 
| Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5094 | #endif | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5095 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5096 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { | 
|  | 5097 | return 1; | 
|  | 5098 | } | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5099 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5100 | return 0; | 
| Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5101 | } | 
|  | 5102 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5103 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) | 
| Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5104 | { | 
|  | 5105 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5106 | psa_status_t status = psa_hash_setup(&operation, alg); | 
|  | 5107 | psa_hash_abort(&operation); | 
|  | 5108 | return status; | 
| Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5109 | } | 
|  | 5110 |  | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5111 | static psa_status_t psa_key_derivation_setup_kdf( | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5112 | psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5113 | psa_algorithm_t kdf_alg) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5114 | { | 
| Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5115 | /* Make sure that operation->ctx is properly zero-initialised. (Macro | 
|  | 5116 | * initialisers for this union leave some bytes unspecified.) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5117 | memset(&operation->ctx, 0, sizeof(operation->ctx)); | 
| Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5118 |  | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5119 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5120 | if (!is_kdf_alg_supported(kdf_alg)) { | 
|  | 5121 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 5122 | } | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5123 |  | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5124 | /* All currently supported key derivation algorithms (apart from | 
| Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5125 | * ecjpake to pms) are based on a hash algorithm. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5126 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); | 
|  | 5127 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); | 
|  | 5128 | if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) { | 
|  | 5129 | if (hash_size == 0) { | 
|  | 5130 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 5131 | } | 
| Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5132 |  | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5133 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise | 
|  | 5134 | * we might fail later, which is somewhat unfriendly and potentially | 
|  | 5135 | * risk-prone. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5136 | psa_status_t status = psa_hash_try_support(hash_alg); | 
|  | 5137 | if (status != PSA_SUCCESS) { | 
|  | 5138 | return status; | 
|  | 5139 | } | 
|  | 5140 | } else { | 
|  | 5141 | hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256); | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5142 | } | 
| Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5143 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5144 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || | 
|  | 5145 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && | 
|  | 5146 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { | 
|  | 5147 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5148 | } | 
| Andrzej Kurek | 7763829 | 2022-09-16 12:24:52 -0400 | [diff] [blame] | 5149 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ | 
| Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5150 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5151 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) || | 
|  | 5152 | (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) { | 
| Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5153 | operation->capacity = hash_size; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5154 | } else | 
| Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5155 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT || | 
|  | 5156 | MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | operation->capacity = 255 * hash_size; | 
|  | 5158 | return PSA_SUCCESS; | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5159 | } | 
| Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5160 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5161 | static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg) | 
| Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5162 | { | 
|  | 5163 | #if defined(PSA_WANT_ALG_ECDH) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5164 | if (alg == PSA_ALG_ECDH) { | 
|  | 5165 | return PSA_SUCCESS; | 
|  | 5166 | } | 
| Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5167 | #endif | 
|  | 5168 | (void) alg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5169 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5170 | } | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5171 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5172 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, | 
|  | 5174 | psa_algorithm_t alg) | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5175 | { | 
|  | 5176 | psa_status_t status; | 
|  | 5177 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | if (operation->alg != 0) { | 
|  | 5179 | return PSA_ERROR_BAD_STATE; | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5180 | } | 
| Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5181 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5182 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { | 
|  | 5183 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5184 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { | 
|  | 5185 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) | 
|  | 5186 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); | 
|  | 5187 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); | 
|  | 5188 | status = psa_key_agreement_try_support(ka_alg); | 
|  | 5189 | if (status != PSA_SUCCESS) { | 
|  | 5190 | return status; | 
|  | 5191 | } | 
|  | 5192 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); | 
|  | 5193 | #else | 
|  | 5194 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 5195 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ | 
|  | 5196 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { | 
|  | 5197 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) | 
|  | 5198 | status = psa_key_derivation_setup_kdf(operation, alg); | 
|  | 5199 | #else | 
|  | 5200 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 5201 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ | 
|  | 5202 | } else { | 
|  | 5203 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5204 | } | 
|  | 5205 |  | 
|  | 5206 | if (status == PSA_SUCCESS) { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5207 | operation->alg = alg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5208 | } | 
|  | 5209 | return status; | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5210 | } | 
|  | 5211 |  | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5212 | #if defined(BUILTIN_ALG_ANY_HKDF) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5213 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, | 
|  | 5214 | psa_algorithm_t kdf_alg, | 
|  | 5215 | psa_key_derivation_step_t step, | 
|  | 5216 | const uint8_t *data, | 
|  | 5217 | size_t data_length) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5218 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5219 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5220 | psa_status_t status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5221 | switch (step) { | 
| Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5222 | case PSA_KEY_DERIVATION_INPUT_SALT: | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5223 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5224 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { | 
|  | 5225 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5226 | } | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5227 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5228 | if (hkdf->state != HKDF_STATE_INIT) { | 
|  | 5229 | return PSA_ERROR_BAD_STATE; | 
|  | 5230 | } else { | 
|  | 5231 | status = psa_key_derivation_start_hmac(&hkdf->hmac, | 
|  | 5232 | hash_alg, | 
|  | 5233 | data, data_length); | 
|  | 5234 | if (status != PSA_SUCCESS) { | 
|  | 5235 | return status; | 
|  | 5236 | } | 
| Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5237 | hkdf->state = HKDF_STATE_STARTED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5238 | return PSA_SUCCESS; | 
| Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5239 | } | 
| Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5240 | case PSA_KEY_DERIVATION_INPUT_SECRET: | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5241 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5242 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { | 
| Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 5243 | /* We shouldn't be in different state as HKDF_EXPAND only allows | 
|  | 5244 | * two inputs: SECRET (this case) and INFO which does not modify | 
|  | 5245 | * the state. It could happen only if the hkdf | 
|  | 5246 | * object was corrupted. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5247 | if (hkdf->state != HKDF_STATE_INIT) { | 
|  | 5248 | return PSA_ERROR_BAD_STATE; | 
|  | 5249 | } | 
| Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5250 |  | 
| Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 5251 | /* Allow only input that fits expected prk size */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | if (data_length != PSA_HASH_LENGTH(hash_alg)) { | 
|  | 5253 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5254 | } | 
| Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5255 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5256 | memcpy(hkdf->prk, data, data_length); | 
|  | 5257 | } else | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5258 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ | 
| Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5259 | { | 
| Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5260 | /* HKDF: If no salt was provided, use an empty salt. | 
|  | 5261 | * HKDF-EXTRACT: salt is mandatory. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | if (hkdf->state == HKDF_STATE_INIT) { | 
| Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5263 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5264 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { | 
|  | 5265 | return PSA_ERROR_BAD_STATE; | 
|  | 5266 | } | 
| Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 5267 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5268 | status = psa_key_derivation_start_hmac(&hkdf->hmac, | 
|  | 5269 | hash_alg, | 
|  | 5270 | NULL, 0); | 
|  | 5271 | if (status != PSA_SUCCESS) { | 
|  | 5272 | return status; | 
|  | 5273 | } | 
| Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5274 | hkdf->state = HKDF_STATE_STARTED; | 
|  | 5275 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5276 | if (hkdf->state != HKDF_STATE_STARTED) { | 
|  | 5277 | return PSA_ERROR_BAD_STATE; | 
|  | 5278 | } | 
|  | 5279 | status = psa_mac_update(&hkdf->hmac, | 
|  | 5280 | data, data_length); | 
|  | 5281 | if (status != PSA_SUCCESS) { | 
|  | 5282 | return status; | 
|  | 5283 | } | 
|  | 5284 | status = psa_mac_sign_finish(&hkdf->hmac, | 
|  | 5285 | hkdf->prk, | 
|  | 5286 | sizeof(hkdf->prk), | 
|  | 5287 | &data_length); | 
|  | 5288 | if (status != PSA_SUCCESS) { | 
|  | 5289 | return status; | 
|  | 5290 | } | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5291 | } | 
| Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5292 |  | 
| Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 5293 | hkdf->state = HKDF_STATE_KEYED; | 
| Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5294 | hkdf->block_number = 0; | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5295 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5296 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { | 
| Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5297 | /* The only block of output is the PRK. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5298 | memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg)); | 
| Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5299 | hkdf->offset_in_block = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5300 | } else | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5301 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ | 
| Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5302 | { | 
|  | 5303 | /* Block 0 is empty, and the next block will be | 
|  | 5304 | * generated by psa_key_derivation_hkdf_read(). */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5305 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); | 
| Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5306 | } | 
|  | 5307 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5308 | return PSA_SUCCESS; | 
| Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5309 | case PSA_KEY_DERIVATION_INPUT_INFO: | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5310 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5311 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { | 
|  | 5312 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5313 | } | 
| Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5314 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ | 
| Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 5315 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5316 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) && | 
|  | 5317 | hkdf->state == HKDF_STATE_INIT) { | 
|  | 5318 | return PSA_ERROR_BAD_STATE; | 
|  | 5319 | } | 
| Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 5320 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5321 | if (hkdf->state == HKDF_STATE_OUTPUT) { | 
|  | 5322 | return PSA_ERROR_BAD_STATE; | 
|  | 5323 | } | 
|  | 5324 | if (hkdf->info_set) { | 
|  | 5325 | return PSA_ERROR_BAD_STATE; | 
|  | 5326 | } | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5327 | hkdf->info_length = data_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5328 | if (data_length != 0) { | 
|  | 5329 | hkdf->info = mbedtls_calloc(1, data_length); | 
|  | 5330 | if (hkdf->info == NULL) { | 
|  | 5331 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 5332 | } | 
|  | 5333 | memcpy(hkdf->info, data, data_length); | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5334 | } | 
|  | 5335 | hkdf->info_set = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5336 | return PSA_SUCCESS; | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5337 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5338 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5339 | } | 
|  | 5340 | } | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5341 | #endif /* BUILTIN_ALG_ANY_HKDF */ | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5342 |  | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5343 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ | 
|  | 5344 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5345 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, | 
|  | 5346 | const uint8_t *data, | 
|  | 5347 | size_t data_length) | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5348 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5349 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { | 
|  | 5350 | return PSA_ERROR_BAD_STATE; | 
|  | 5351 | } | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5352 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5353 | if (data_length != 0) { | 
|  | 5354 | prf->seed = mbedtls_calloc(1, data_length); | 
|  | 5355 | if (prf->seed == NULL) { | 
|  | 5356 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 5357 | } | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5358 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5359 | memcpy(prf->seed, data, data_length); | 
| Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5360 | prf->seed_length = data_length; | 
|  | 5361 | } | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5362 |  | 
| Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5363 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5364 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5365 | return PSA_SUCCESS; | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5366 | } | 
|  | 5367 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5368 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, | 
|  | 5369 | const uint8_t *data, | 
|  | 5370 | size_t data_length) | 
| Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5371 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5372 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET && | 
|  | 5373 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { | 
|  | 5374 | return PSA_ERROR_BAD_STATE; | 
|  | 5375 | } | 
| Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5376 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5377 | if (data_length != 0) { | 
|  | 5378 | prf->secret = mbedtls_calloc(1, data_length); | 
|  | 5379 | if (prf->secret == NULL) { | 
|  | 5380 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 5381 | } | 
| Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5382 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | memcpy(prf->secret, data, data_length); | 
| Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5384 | prf->secret_length = data_length; | 
|  | 5385 | } | 
| Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5386 |  | 
| Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5387 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; | 
| Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5388 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5389 | return PSA_SUCCESS; | 
| Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5390 | } | 
|  | 5391 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, | 
|  | 5393 | const uint8_t *data, | 
|  | 5394 | size_t data_length) | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5395 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { | 
|  | 5397 | return PSA_ERROR_BAD_STATE; | 
|  | 5398 | } | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5399 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | if (data_length != 0) { | 
|  | 5401 | prf->label = mbedtls_calloc(1, data_length); | 
|  | 5402 | if (prf->label == NULL) { | 
|  | 5403 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 5404 | } | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5405 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5406 | memcpy(prf->label, data, data_length); | 
| Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 5407 | prf->label_length = data_length; | 
|  | 5408 | } | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5409 |  | 
| Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 5410 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5411 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5412 | return PSA_SUCCESS; | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5413 | } | 
|  | 5414 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5415 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, | 
|  | 5416 | psa_key_derivation_step_t step, | 
|  | 5417 | const uint8_t *data, | 
|  | 5418 | size_t data_length) | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5419 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5420 | switch (step) { | 
| Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 5421 | case PSA_KEY_DERIVATION_INPUT_SEED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5422 | return psa_tls12_prf_set_seed(prf, data, data_length); | 
| Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 5423 | case PSA_KEY_DERIVATION_INPUT_SECRET: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5424 | return psa_tls12_prf_set_key(prf, data, data_length); | 
| Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 5425 | case PSA_KEY_DERIVATION_INPUT_LABEL: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5426 | return psa_tls12_prf_set_label(prf, data, data_length); | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5427 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5428 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 5429 | } | 
|  | 5430 | } | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5431 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || | 
|  | 5432 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ | 
|  | 5433 |  | 
|  | 5434 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) | 
|  | 5435 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( | 
|  | 5436 | psa_tls12_prf_key_derivation_t *prf, | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5437 | const uint8_t *data, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5438 | size_t data_length) | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5439 | { | 
|  | 5440 | psa_status_t status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5441 | const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? | 
|  | 5442 | 4 + data_length + prf->other_secret_length : | 
|  | 5443 | 4 + 2 * data_length); | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5444 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5445 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { | 
|  | 5446 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5447 | } | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5448 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5449 | uint8_t *pms = mbedtls_calloc(1, pms_len); | 
|  | 5450 | if (pms == NULL) { | 
|  | 5451 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 5452 | } | 
| Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5453 | uint8_t *cur = pms; | 
|  | 5454 |  | 
|  | 5455 | /* pure-PSK: | 
|  | 5456 | * Quoting RFC 4279, Section 2: | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5457 | * | 
|  | 5458 | * The premaster secret is formed as follows: if the PSK is N octets | 
|  | 5459 | * long, concatenate a uint16 with the value N, N zero octets, a second | 
|  | 5460 | * uint16 with the value N, and the PSK itself. | 
| Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5461 | * | 
|  | 5462 | * mixed-PSK: | 
|  | 5463 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as | 
|  | 5464 | * follows: concatenate a uint16 with the length of the other secret, | 
|  | 5465 | * the other secret itself, uint16 with the length of PSK, and the | 
|  | 5466 | * PSK itself. | 
|  | 5467 | * For details please check: | 
|  | 5468 | * - RFC 4279, Section 4 for the definition of RSA-PSK, | 
|  | 5469 | * - RFC 4279, Section 3 for the definition of DHE-PSK, | 
|  | 5470 | * - RFC 5489 for the definition of ECDHE-PSK. | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5471 | */ | 
|  | 5472 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { | 
|  | 5474 | *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length); | 
|  | 5475 | *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length); | 
|  | 5476 | if (prf->other_secret_length != 0) { | 
|  | 5477 | memcpy(cur, prf->other_secret, prf->other_secret_length); | 
|  | 5478 | mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length); | 
| Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 5479 | cur += prf->other_secret_length; | 
|  | 5480 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5481 | } else { | 
|  | 5482 | *cur++ = MBEDTLS_BYTE_1(data_length); | 
|  | 5483 | *cur++ = MBEDTLS_BYTE_0(data_length); | 
|  | 5484 | memset(cur, 0, data_length); | 
| Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 5485 | cur += data_length; | 
|  | 5486 | } | 
|  | 5487 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5488 | *cur++ = MBEDTLS_BYTE_1(data_length); | 
|  | 5489 | *cur++ = MBEDTLS_BYTE_0(data_length); | 
|  | 5490 | memcpy(cur, data, data_length); | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5491 | cur += data_length; | 
|  | 5492 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | status = psa_tls12_prf_set_key(prf, pms, cur - pms); | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5494 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | mbedtls_platform_zeroize(pms, pms_len); | 
|  | 5496 | mbedtls_free(pms); | 
|  | 5497 | return status; | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5498 | } | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5499 |  | 
| Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5500 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( | 
|  | 5501 | psa_tls12_prf_key_derivation_t *prf, | 
|  | 5502 | const uint8_t *data, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5503 | size_t data_length) | 
| Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5504 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5505 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { | 
|  | 5506 | return PSA_ERROR_BAD_STATE; | 
| Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5507 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5508 |  | 
|  | 5509 | if (data_length != 0) { | 
|  | 5510 | prf->other_secret = mbedtls_calloc(1, data_length); | 
|  | 5511 | if (prf->other_secret == NULL) { | 
|  | 5512 | return PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 5513 | } | 
|  | 5514 |  | 
|  | 5515 | memcpy(prf->other_secret, data, data_length); | 
|  | 5516 | prf->other_secret_length = data_length; | 
|  | 5517 | } else { | 
| Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5518 | prf->other_secret_length = 0; | 
|  | 5519 | } | 
|  | 5520 |  | 
|  | 5521 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; | 
|  | 5522 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5523 | return PSA_SUCCESS; | 
| Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5524 | } | 
|  | 5525 |  | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5526 | static psa_status_t psa_tls12_prf_psk_to_ms_input( | 
|  | 5527 | psa_tls12_prf_key_derivation_t *prf, | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5528 | psa_key_derivation_step_t step, | 
|  | 5529 | const uint8_t *data, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5530 | size_t data_length) | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5531 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5532 | switch (step) { | 
| Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5533 | case PSA_KEY_DERIVATION_INPUT_SECRET: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5534 | return psa_tls12_prf_psk_to_ms_set_key(prf, | 
|  | 5535 | data, data_length); | 
| Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5536 | break; | 
|  | 5537 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5538 | return psa_tls12_prf_psk_to_ms_set_other_key(prf, | 
|  | 5539 | data, | 
|  | 5540 | data_length); | 
| Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5541 | break; | 
|  | 5542 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | return psa_tls12_prf_input(prf, step, data, data_length); | 
| Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5544 | break; | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5545 |  | 
| Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 5546 | } | 
| Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 5547 | } | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5548 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5549 |  | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5550 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) | 
|  | 5551 | static psa_status_t psa_tls12_ecjpake_to_pms_input( | 
|  | 5552 | psa_tls12_ecjpake_to_pms_t *ecjpake, | 
| Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5553 | psa_key_derivation_step_t step, | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5554 | const uint8_t *data, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5555 | size_t data_length) | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5556 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5557 | if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE || | 
|  | 5558 | step != PSA_KEY_DERIVATION_INPUT_SECRET) { | 
|  | 5559 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5560 | } | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5561 |  | 
|  | 5562 | /* Check if the passed point is in an uncompressed form */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | if (data[0] != 0x04) { | 
|  | 5564 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5565 | } | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5566 |  | 
|  | 5567 | /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5568 | memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5569 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5570 | return PSA_SUCCESS; | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5571 | } | 
|  | 5572 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ | 
| Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5573 | /** Check whether the given key type is acceptable for the given | 
|  | 5574 | * input step of a key derivation. | 
|  | 5575 | * | 
|  | 5576 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. | 
|  | 5577 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. | 
|  | 5578 | * Both secret and non-secret inputs can alternatively have the type | 
|  | 5579 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning | 
|  | 5580 | * that the input was passed as a buffer rather than via a key object. | 
|  | 5581 | */ | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5582 | static int psa_key_derivation_check_input_type( | 
|  | 5583 | psa_key_derivation_step_t step, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5584 | psa_key_type_t key_type) | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5585 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5586 | switch (step) { | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5587 | case PSA_KEY_DERIVATION_INPUT_SECRET: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5588 | if (key_type == PSA_KEY_TYPE_DERIVE) { | 
|  | 5589 | return PSA_SUCCESS; | 
|  | 5590 | } | 
|  | 5591 | if (key_type == PSA_KEY_TYPE_NONE) { | 
|  | 5592 | return PSA_SUCCESS; | 
|  | 5593 | } | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5594 | break; | 
| Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 5595 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5596 | if (key_type == PSA_KEY_TYPE_DERIVE) { | 
|  | 5597 | return PSA_SUCCESS; | 
|  | 5598 | } | 
|  | 5599 | if (key_type == PSA_KEY_TYPE_NONE) { | 
|  | 5600 | return PSA_SUCCESS; | 
|  | 5601 | } | 
| Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 5602 | break; | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5603 | case PSA_KEY_DERIVATION_INPUT_LABEL: | 
|  | 5604 | case PSA_KEY_DERIVATION_INPUT_SALT: | 
|  | 5605 | case PSA_KEY_DERIVATION_INPUT_INFO: | 
|  | 5606 | case PSA_KEY_DERIVATION_INPUT_SEED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { | 
|  | 5608 | return PSA_SUCCESS; | 
|  | 5609 | } | 
|  | 5610 | if (key_type == PSA_KEY_TYPE_NONE) { | 
|  | 5611 | return PSA_SUCCESS; | 
|  | 5612 | } | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5613 | break; | 
|  | 5614 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5615 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5616 | } | 
|  | 5617 |  | 
| Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 5618 | static psa_status_t psa_key_derivation_input_internal( | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5619 | psa_key_derivation_operation_t *operation, | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5620 | psa_key_derivation_step_t step, | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5621 | psa_key_type_t key_type, | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5622 | const uint8_t *data, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5623 | size_t data_length) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5624 | { | 
| Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5625 | psa_status_t status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5626 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); | 
| Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 5627 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5628 | status = psa_key_derivation_check_input_type(step, key_type); | 
|  | 5629 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5630 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5631 | } | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5632 |  | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5633 | #if defined(BUILTIN_ALG_ANY_HKDF) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { | 
|  | 5635 | status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg, | 
|  | 5636 | step, data, data_length); | 
|  | 5637 | } else | 
| Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5638 | #endif /* BUILTIN_ALG_ANY_HKDF */ | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5639 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5640 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { | 
|  | 5641 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, | 
|  | 5642 | step, data, data_length); | 
|  | 5643 | } else | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5644 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ | 
|  | 5645 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5646 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { | 
|  | 5647 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, | 
|  | 5648 | step, data, data_length); | 
|  | 5649 | } else | 
| John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5650 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5651 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5652 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5653 | status = psa_tls12_ecjpake_to_pms_input( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5654 | &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length); | 
|  | 5655 | } else | 
| Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5656 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5657 | { | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5658 | /* This can't happen unless the operation object was not initialized */ | 
| Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5659 | (void) data; | 
|  | 5660 | (void) data_length; | 
|  | 5661 | (void) kdf_alg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5662 | return PSA_ERROR_BAD_STATE; | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5663 | } | 
|  | 5664 |  | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5665 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5666 | if (status != PSA_SUCCESS) { | 
|  | 5667 | psa_key_derivation_abort(operation); | 
|  | 5668 | } | 
|  | 5669 | return status; | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5670 | } | 
|  | 5671 |  | 
| Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5672 | psa_status_t psa_key_derivation_input_bytes( | 
|  | 5673 | psa_key_derivation_operation_t *operation, | 
|  | 5674 | psa_key_derivation_step_t step, | 
|  | 5675 | const uint8_t *data, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | size_t data_length) | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5677 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5678 | return psa_key_derivation_input_internal(operation, step, | 
|  | 5679 | PSA_KEY_TYPE_NONE, | 
|  | 5680 | data, data_length); | 
| Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 5681 | } | 
|  | 5682 |  | 
| Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 5683 | psa_status_t psa_key_derivation_input_key( | 
|  | 5684 | psa_key_derivation_operation_t *operation, | 
|  | 5685 | psa_key_derivation_step_t step, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5686 | mbedtls_svc_key_id_t key) | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5687 | { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5688 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5689 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5690 | psa_key_slot_t *slot; | 
| Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5691 |  | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5692 | status = psa_get_and_lock_transparent_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); | 
|  | 5694 | if (status != PSA_SUCCESS) { | 
|  | 5695 | psa_key_derivation_abort(operation); | 
|  | 5696 | return status; | 
| Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 5697 | } | 
| Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5698 |  | 
|  | 5699 | /* Passing a key object as a SECRET input unlocks the permission | 
|  | 5700 | * to output to a key object. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5701 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { | 
| Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5702 | operation->can_output_key = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5703 | } | 
| Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5704 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5705 | status = psa_key_derivation_input_internal(operation, | 
|  | 5706 | step, slot->attr.type, | 
|  | 5707 | slot->key.data, | 
|  | 5708 | slot->key.bytes); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5709 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5710 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5711 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5712 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5713 | } | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5714 |  | 
|  | 5715 |  | 
|  | 5716 |  | 
|  | 5717 | /****************************************************************/ | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5718 | /* Key agreement */ | 
|  | 5719 | /****************************************************************/ | 
|  | 5720 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5721 | psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes, | 
|  | 5722 | const uint8_t *key_buffer, | 
|  | 5723 | size_t key_buffer_size, | 
|  | 5724 | psa_algorithm_t alg, | 
|  | 5725 | const uint8_t *peer_key, | 
|  | 5726 | size_t peer_key_length, | 
|  | 5727 | uint8_t *shared_secret, | 
|  | 5728 | size_t shared_secret_size, | 
|  | 5729 | size_t *shared_secret_length) | 
| Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 5730 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5731 | switch (alg) { | 
| John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 5732 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5733 | case PSA_ALG_ECDH: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5734 | return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer, | 
|  | 5735 | key_buffer_size, alg, | 
|  | 5736 | peer_key, peer_key_length, | 
|  | 5737 | shared_secret, | 
|  | 5738 | shared_secret_size, | 
|  | 5739 | shared_secret_length); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5740 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5741 | default: | 
| Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5742 | (void) attributes; | 
|  | 5743 | (void) key_buffer; | 
|  | 5744 | (void) key_buffer_size; | 
| Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 5745 | (void) peer_key; | 
|  | 5746 | (void) peer_key_length; | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5747 | (void) shared_secret; | 
|  | 5748 | (void) shared_secret_size; | 
|  | 5749 | (void) shared_secret_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5750 | return PSA_ERROR_NOT_SUPPORTED; | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5751 | } | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5752 | } | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5753 |  | 
| Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5754 | /** Internal function for raw key agreement | 
|  | 5755 | *  Calls the driver wrapper which will hand off key agreement task | 
| Aditya Deshpande | 40c05cc | 2022-10-14 16:41:40 +0100 | [diff] [blame] | 5756 | *  to the driver's implementation if a driver is present. | 
|  | 5757 | *  Fallback specified in the driver wrapper is built-in raw key agreement | 
| Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5758 | *  (psa_key_agreement_raw_builtin). | 
|  | 5759 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, | 
|  | 5761 | psa_key_slot_t *private_key, | 
|  | 5762 | const uint8_t *peer_key, | 
|  | 5763 | size_t peer_key_length, | 
|  | 5764 | uint8_t *shared_secret, | 
|  | 5765 | size_t shared_secret_size, | 
|  | 5766 | size_t *shared_secret_length) | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5767 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5768 | if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { | 
|  | 5769 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 5770 | } | 
| Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5771 |  | 
|  | 5772 | psa_key_attributes_t attributes = { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | .core = private_key->attr | 
| Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 5774 | }; | 
|  | 5775 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5776 | return psa_driver_wrapper_key_agreement(&attributes, | 
|  | 5777 | private_key->key.data, | 
|  | 5778 | private_key->key.bytes, alg, | 
|  | 5779 | peer_key, peer_key_length, | 
|  | 5780 | shared_secret, | 
|  | 5781 | shared_secret_size, | 
|  | 5782 | shared_secret_length); | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5783 | } | 
|  | 5784 |  | 
| Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5785 | /* 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] | 5786 | * to potentially free embedded data structures and wipe confidential data. | 
|  | 5787 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5788 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, | 
|  | 5789 | psa_key_derivation_step_t step, | 
|  | 5790 | psa_key_slot_t *private_key, | 
|  | 5791 | const uint8_t *peer_key, | 
|  | 5792 | size_t peer_key_length) | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5793 | { | 
|  | 5794 | psa_status_t status; | 
| Aditya Deshpande | 2f7fd76 | 2022-11-22 11:10:34 +0000 | [diff] [blame] | 5795 | uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE]; | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5796 | size_t shared_secret_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5797 | 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] | 5798 |  | 
|  | 5799 | /* Step 1: run the secret agreement algorithm to generate the shared | 
|  | 5800 | * secret. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5801 | status = psa_key_agreement_raw_internal(ka_alg, | 
|  | 5802 | private_key, | 
|  | 5803 | peer_key, peer_key_length, | 
|  | 5804 | shared_secret, | 
|  | 5805 | sizeof(shared_secret), | 
|  | 5806 | &shared_secret_length); | 
|  | 5807 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5808 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5809 | } | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5810 |  | 
| Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 5811 | /* Step 2: set up the key derivation to generate key material from | 
| Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 5812 | * the shared secret. A shared secret is permitted wherever a key | 
|  | 5813 | * of type DERIVE is permitted. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5814 | status = psa_key_derivation_input_internal(operation, step, | 
|  | 5815 | PSA_KEY_TYPE_DERIVE, | 
|  | 5816 | shared_secret, | 
|  | 5817 | shared_secret_length); | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5818 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5819 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); | 
|  | 5820 | return status; | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5821 | } | 
|  | 5822 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5823 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, | 
|  | 5824 | psa_key_derivation_step_t step, | 
|  | 5825 | mbedtls_svc_key_id_t private_key, | 
|  | 5826 | const uint8_t *peer_key, | 
|  | 5827 | size_t peer_key_length) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5828 | { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5829 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5830 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 5831 | psa_key_slot_t *slot; | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5832 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5833 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { | 
|  | 5834 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5835 | } | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5836 | status = psa_get_and_lock_transparent_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5837 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); | 
|  | 5838 | if (status != PSA_SUCCESS) { | 
|  | 5839 | return status; | 
|  | 5840 | } | 
|  | 5841 | status = psa_key_agreement_internal(operation, step, | 
|  | 5842 | slot, | 
|  | 5843 | peer_key, peer_key_length); | 
|  | 5844 | if (status != PSA_SUCCESS) { | 
|  | 5845 | psa_key_derivation_abort(operation); | 
|  | 5846 | } else { | 
| Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5847 | /* If a private key has been added as SECRET, we allow the derived | 
|  | 5848 | * key material to be used as a key in PSA Crypto. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5849 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { | 
| Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5850 | operation->can_output_key = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5851 | } | 
| Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5852 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5853 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5854 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5855 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5856 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5857 | } | 
|  | 5858 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5859 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, | 
|  | 5860 | mbedtls_svc_key_id_t private_key, | 
|  | 5861 | const uint8_t *peer_key, | 
|  | 5862 | size_t peer_key_length, | 
|  | 5863 | uint8_t *output, | 
|  | 5864 | size_t output_size, | 
|  | 5865 | size_t *output_length) | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5866 | { | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5867 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5868 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5869 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5870 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5872 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 5873 | goto exit; | 
|  | 5874 | } | 
| Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5875 | status = psa_get_and_lock_transparent_key_slot_with_policy( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5876 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); | 
|  | 5877 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5878 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | } | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5880 |  | 
| Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5881 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound | 
|  | 5882 | * for the output size. The PSA specification only guarantees that this | 
|  | 5883 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), | 
|  | 5884 | * but it might be nice to allow smaller buffers if the output fits. | 
|  | 5885 | * At the time of writing this comment, with only ECDH implemented, | 
|  | 5886 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. | 
|  | 5887 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily | 
|  | 5888 | * be exact for it as well. */ | 
|  | 5889 | size_t expected_length = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5890 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); | 
|  | 5891 | if (output_size < expected_length) { | 
| Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5892 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 5893 | goto exit; | 
|  | 5894 | } | 
|  | 5895 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5896 | status = psa_key_agreement_raw_internal(alg, slot, | 
|  | 5897 | peer_key, peer_key_length, | 
|  | 5898 | output, output_size, | 
|  | 5899 | output_length); | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5900 |  | 
|  | 5901 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5902 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5903 | /* If an error happens and is not handled properly, the output | 
|  | 5904 | * may be used as a key to protect sensitive data. Arrange for such | 
|  | 5905 | * a key to be random, which is likely to result in decryption or | 
|  | 5906 | * verification errors. This is better than filling the buffer with | 
|  | 5907 | * some constant data such as zeros, which would result in the data | 
|  | 5908 | * being protected with a reproducible, easily knowable key. | 
|  | 5909 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5910 | psa_generate_random(output, output_size); | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5911 | *output_length = output_size; | 
|  | 5912 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5913 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5914 | unlock_status = psa_unlock_key_slot(slot); | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5915 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | return (status == PSA_SUCCESS) ? unlock_status : status; | 
| Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5917 | } | 
| Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5918 |  | 
|  | 5919 |  | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5920 |  | 
| Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5921 | /****************************************************************/ | 
|  | 5922 | /* Random generation */ | 
| Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5923 | /****************************************************************/ | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5924 |  | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5925 | /** Initialize the PSA random generator. | 
|  | 5926 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5928 | { | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5929 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5930 | memset(rng, 0, sizeof(*rng)); | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5931 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ | 
|  | 5932 |  | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5933 | /* Set default configuration if | 
|  | 5934 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5935 | if (rng->entropy_init == NULL) { | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5936 | rng->entropy_init = mbedtls_entropy_init; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5937 | } | 
|  | 5938 | if (rng->entropy_free == NULL) { | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5939 | rng->entropy_free = mbedtls_entropy_free; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | } | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5941 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5942 | rng->entropy_init(&rng->entropy); | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5943 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ | 
|  | 5944 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) | 
|  | 5945 | /* The PSA entropy injection feature depends on using NV seed as an entropy | 
|  | 5946 | * source. Add NV seed as an entropy source for PSA entropy injection. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | mbedtls_entropy_add_source(&rng->entropy, | 
|  | 5948 | mbedtls_nv_seed_poll, NULL, | 
|  | 5949 | MBEDTLS_ENTROPY_BLOCK_SIZE, | 
|  | 5950 | MBEDTLS_ENTROPY_SOURCE_STRONG); | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5951 | #endif | 
|  | 5952 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5953 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5954 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5955 | } | 
|  | 5956 |  | 
|  | 5957 | /** Deinitialize the PSA random generator. | 
|  | 5958 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5959 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5960 | { | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5961 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5962 | memset(rng, 0, sizeof(*rng)); | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5963 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5964 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); | 
|  | 5965 | rng->entropy_free(&rng->entropy); | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5966 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5967 | } | 
|  | 5968 |  | 
|  | 5969 | /** Seed the PSA random generator. | 
|  | 5970 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng) | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5972 | { | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5973 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) | 
|  | 5974 | /* Do nothing: the external RNG seeds itself. */ | 
|  | 5975 | (void) rng; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5976 | return PSA_SUCCESS; | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5977 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5978 | const unsigned char drbg_seed[] = "PSA"; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5979 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, | 
|  | 5980 | drbg_seed, sizeof(drbg_seed) - 1); | 
|  | 5981 | return mbedtls_to_psa_error(ret); | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5982 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5983 | } | 
|  | 5984 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5985 | psa_status_t psa_generate_random(uint8_t *output, | 
|  | 5986 | size_t output_size) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5987 | { | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5988 | GUARD_MODULE_INITIALIZED; | 
|  | 5989 |  | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5990 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) | 
|  | 5991 |  | 
|  | 5992 | size_t output_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5993 | psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng, | 
|  | 5994 | output, output_size, | 
|  | 5995 | &output_length); | 
|  | 5996 | if (status != PSA_SUCCESS) { | 
|  | 5997 | return status; | 
|  | 5998 | } | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5999 | /* Breaking up a request into smaller chunks is currently not supported | 
| Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6000 | * for the external RNG interface. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6001 | if (output_length != output_size) { | 
|  | 6002 | return PSA_ERROR_INSUFFICIENT_ENTROPY; | 
|  | 6003 | } | 
|  | 6004 | return PSA_SUCCESS; | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6005 |  | 
|  | 6006 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ | 
|  | 6007 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6008 | while (output_size > 0) { | 
| Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6009 | size_t request_size = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6010 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? | 
|  | 6011 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : | 
|  | 6012 | output_size); | 
|  | 6013 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, | 
|  | 6014 | output, request_size); | 
|  | 6015 | if (ret != 0) { | 
|  | 6016 | return mbedtls_to_psa_error(ret); | 
|  | 6017 | } | 
| Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6018 | output_size -= request_size; | 
|  | 6019 | output += request_size; | 
| Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6020 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6021 | return PSA_SUCCESS; | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6022 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6023 | } | 
|  | 6024 |  | 
| Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6025 | /* Wrapper function allowing the classic API to use the PSA RNG. | 
| Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6026 | * | 
|  | 6027 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls | 
|  | 6028 | * `psa_generate_random(...)`. The state parameter is ignored since the | 
|  | 6029 | * PSA API doesn't support passing an explicit state. | 
|  | 6030 | * | 
|  | 6031 | * In the non-external case, psa_generate_random() calls an | 
|  | 6032 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature | 
|  | 6033 | * and semantics as mbedtls_psa_get_random(). As an optimization, | 
|  | 6034 | * instead of doing this back-and-forth between the PSA API and the | 
|  | 6035 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` | 
|  | 6036 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. | 
| Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6037 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6038 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) | 
|  | 6039 | int mbedtls_psa_get_random(void *p_rng, | 
|  | 6040 | unsigned char *output, | 
|  | 6041 | size_t output_size) | 
| Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6042 | { | 
| Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6043 | /* This function takes a pointer to the RNG state because that's what | 
|  | 6044 | * classic mbedtls functions using an RNG expect. The PSA RNG manages | 
|  | 6045 | * its own state internally and doesn't let the caller access that state. | 
|  | 6046 | * So we just ignore the state parameter, and in practice we'll pass | 
|  | 6047 | * NULL. */ | 
| Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6048 | (void) p_rng; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | psa_status_t status = psa_generate_random(output, output_size); | 
|  | 6050 | if (status == PSA_SUCCESS) { | 
|  | 6051 | return 0; | 
|  | 6052 | } else { | 
|  | 6053 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; | 
|  | 6054 | } | 
| Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6055 | } | 
|  | 6056 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ | 
|  | 6057 |  | 
| Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6058 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) | 
| Chris Jones | ea0a865 | 2021-03-09 19:11:19 +0000 | [diff] [blame] | 6059 | #include "entropy_poll.h" | 
| Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6060 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6061 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, | 
|  | 6062 | size_t seed_size) | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6063 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6064 | if (global_data.initialized) { | 
|  | 6065 | return PSA_ERROR_NOT_PERMITTED; | 
|  | 6066 | } | 
| Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6067 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6068 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || | 
|  | 6069 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || | 
|  | 6070 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { | 
|  | 6071 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 6072 | } | 
| Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6073 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6074 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6075 | } | 
| Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6076 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ | 
| Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6077 |  | 
| Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6078 | /** Validate the key type and size for key generation | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6079 | * | 
| Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6080 | * \param  type  The key type | 
|  | 6081 | * \param  bits  The number of bits of the key | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6082 | * | 
|  | 6083 | * \retval #PSA_SUCCESS | 
| Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6084 | *         The key type and size are valid. | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6085 | * \retval #PSA_ERROR_INVALID_ARGUMENT | 
|  | 6086 | *         The size in bits of the key is not valid. | 
|  | 6087 | * \retval #PSA_ERROR_NOT_SUPPORTED | 
|  | 6088 | *         The type and/or the size in bits of the key or the combination of | 
|  | 6089 | *         the two is not supported. | 
|  | 6090 | */ | 
| Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6091 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6092 | psa_key_type_t type, size_t bits) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6093 | { | 
| Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6094 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6095 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6096 | if (key_type_is_raw_bytes(type)) { | 
|  | 6097 | status = psa_validate_unstructured_key_bit_size(type, bits); | 
|  | 6098 | if (status != PSA_SUCCESS) { | 
|  | 6099 | return status; | 
|  | 6100 | } | 
|  | 6101 | } else | 
| Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6102 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { | 
|  | 6104 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { | 
|  | 6105 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 6106 | } | 
| Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6107 |  | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6108 | /* Accept only byte-aligned keys, for the same reasons as | 
|  | 6109 | * in psa_import_rsa_key(). */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6110 | if (bits % 8 != 0) { | 
|  | 6111 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 6112 | } | 
|  | 6113 | } else | 
| Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6114 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6115 |  | 
| Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6116 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6117 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { | 
| Ronald Cron | d81ab56 | 2021-02-16 09:01:16 +0100 | [diff] [blame] | 6118 | /* To avoid empty block, return successfully here. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6119 | return PSA_SUCCESS; | 
|  | 6120 | } else | 
| Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6121 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6122 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6123 | return PSA_ERROR_NOT_SUPPORTED; | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6124 | } | 
|  | 6125 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | return PSA_SUCCESS; | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6127 | } | 
|  | 6128 |  | 
| Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 6129 | psa_status_t psa_generate_key_internal( | 
| Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6130 | const psa_key_attributes_t *attributes, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6131 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length) | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6132 | { | 
|  | 6133 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6134 | psa_key_type_t type = attributes->core.type; | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6135 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6136 | if ((attributes->domain_parameters == NULL) && | 
|  | 6137 | (attributes->domain_parameters_size != 0)) { | 
|  | 6138 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 6139 | } | 
| Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6140 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6141 | if (key_type_is_raw_bytes(type)) { | 
|  | 6142 | status = psa_generate_random(key_buffer, key_buffer_size); | 
|  | 6143 | if (status != PSA_SUCCESS) { | 
|  | 6144 | return status; | 
|  | 6145 | } | 
| Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6146 |  | 
| David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6147 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6148 | if (type == PSA_KEY_TYPE_DES) { | 
|  | 6149 | psa_des_set_key_parity(key_buffer, key_buffer_size); | 
|  | 6150 | } | 
| David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 6151 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6152 | } else | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6153 |  | 
| Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6154 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ | 
|  | 6155 | defined(MBEDTLS_GENPRIME) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6156 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { | 
|  | 6157 | return mbedtls_psa_rsa_generate_key(attributes, | 
|  | 6158 | key_buffer, | 
|  | 6159 | key_buffer_size, | 
|  | 6160 | key_buffer_length); | 
|  | 6161 | } else | 
| Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6162 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) | 
|  | 6163 | * defined(MBEDTLS_GENPRIME) */ | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6164 |  | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6165 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6166 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { | 
|  | 6167 | return mbedtls_psa_ecp_generate_key(attributes, | 
|  | 6168 | key_buffer, | 
|  | 6169 | key_buffer_size, | 
|  | 6170 | key_buffer_length); | 
|  | 6171 | } else | 
| John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6172 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ | 
| Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6173 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6174 | (void) key_buffer_length; | 
|  | 6175 | return PSA_ERROR_NOT_SUPPORTED; | 
| Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6176 | } | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6177 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6178 | return PSA_SUCCESS; | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6179 | } | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6180 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6181 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, | 
|  | 6182 | mbedtls_svc_key_id_t *key) | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6183 | { | 
|  | 6184 | psa_status_t status; | 
|  | 6185 | psa_key_slot_t *slot = NULL; | 
| Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6186 | psa_se_drv_table_entry_t *driver = NULL; | 
| Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6187 | size_t key_buffer_size; | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6188 |  | 
| Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6189 | *key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 6190 |  | 
| Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6191 | /* Reject any attempt to create a zero-length key so that we don't | 
|  | 6192 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | if (psa_get_key_bits(attributes) == 0) { | 
|  | 6194 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 6195 | } | 
| Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6196 |  | 
| Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6197 | /* Reject any attempt to create a public key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6198 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { | 
|  | 6199 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 6200 | } | 
| Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6201 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6202 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, | 
|  | 6203 | &slot, &driver); | 
|  | 6204 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6205 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6206 | } | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6207 |  | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6208 | /* In the case of a transparent key or an opaque key stored in local | 
| Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 6209 | * storage ( thus not in the case of generating a key in a secure element | 
|  | 6210 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a | 
|  | 6211 | * buffer to hold the generated key material. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6212 | if (slot->key.data == NULL) { | 
|  | 6213 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == | 
|  | 6214 | PSA_KEY_LOCATION_LOCAL_STORAGE) { | 
| Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6215 | status = psa_validate_key_type_and_size_for_key_generation( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6216 | attributes->core.type, attributes->core.bits); | 
|  | 6217 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6218 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6219 | } | 
| Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6220 |  | 
|  | 6221 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6222 | attributes->core.type, | 
|  | 6223 | attributes->core.bits); | 
|  | 6224 | } else { | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6225 | status = psa_driver_wrapper_get_key_buffer_size( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6226 | attributes, &key_buffer_size); | 
|  | 6227 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6228 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6229 | } | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6230 | } | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6231 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6232 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); | 
|  | 6233 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6234 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | } | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6236 | } | 
|  | 6237 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6238 | status = psa_driver_wrapper_generate_key(attributes, | 
|  | 6239 | slot->key.data, slot->key.bytes, &slot->key.bytes); | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6240 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6241 | if (status != PSA_SUCCESS) { | 
|  | 6242 | psa_remove_key_data_from_memory(slot); | 
|  | 6243 | } | 
| Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6244 |  | 
| Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6245 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6246 | if (status == PSA_SUCCESS) { | 
|  | 6247 | status = psa_finish_key_creation(slot, driver, key); | 
|  | 6248 | } | 
|  | 6249 | if (status != PSA_SUCCESS) { | 
|  | 6250 | psa_fail_key_creation(slot, driver); | 
|  | 6251 | } | 
| Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6252 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6253 | return status; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6254 | } | 
|  | 6255 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6256 | /****************************************************************/ | 
|  | 6257 | /* Module setup */ | 
|  | 6258 | /****************************************************************/ | 
|  | 6259 |  | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6260 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6261 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6262 | void (* entropy_init)(mbedtls_entropy_context *ctx), | 
|  | 6263 | void (* entropy_free)(mbedtls_entropy_context *ctx)) | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6264 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6265 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { | 
|  | 6266 | return PSA_ERROR_BAD_STATE; | 
|  | 6267 | } | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6268 | global_data.rng.entropy_init = entropy_init; | 
|  | 6269 | global_data.rng.entropy_free = entropy_free; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | return PSA_SUCCESS; | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6271 | } | 
| Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6272 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ | 
| Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 6273 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6274 | void mbedtls_psa_crypto_free(void) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6275 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6276 | psa_wipe_all_key_slots(); | 
|  | 6277 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { | 
|  | 6278 | mbedtls_psa_random_free(&global_data.rng); | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6279 | } | 
|  | 6280 | /* Wipe all remaining data, including configuration. | 
|  | 6281 | * In particular, this sets all state indicator to the value | 
|  | 6282 | * indicating "uninitialized". */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6283 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); | 
| Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 6284 |  | 
|  | 6285 | /* Terminate drivers */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | psa_driver_wrapper_free(); | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6287 | } | 
|  | 6288 |  | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6289 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) | 
|  | 6290 | /** Recover a transaction that was interrupted by a power failure. | 
|  | 6291 | * | 
|  | 6292 | * This function is called during initialization, before psa_crypto_init() | 
|  | 6293 | * returns. If this function returns a failure status, the initialization | 
|  | 6294 | * fails. | 
|  | 6295 | */ | 
|  | 6296 | static psa_status_t psa_crypto_recover_transaction( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6297 | const psa_crypto_transaction_t *transaction) | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6298 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6299 | switch (transaction->unknown.type) { | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6300 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: | 
|  | 6301 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | /* TODO - fall through to the failure case until this | 
|  | 6303 | * is implemented. | 
|  | 6304 | * https://github.com/ARMmbed/mbed-crypto/issues/218 | 
|  | 6305 | */ | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6306 | default: | 
|  | 6307 | /* We found an unsupported transaction in the storage. | 
|  | 6308 | * We don't know what state the storage is in. Give up. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6309 | return PSA_ERROR_DATA_INVALID; | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6310 | } | 
|  | 6311 | } | 
|  | 6312 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ | 
|  | 6313 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | psa_status_t psa_crypto_init(void) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6315 | { | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6316 | psa_status_t status; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6317 |  | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6318 | /* Double initialization is explicitly allowed. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6319 | if (global_data.initialized != 0) { | 
|  | 6320 | return PSA_SUCCESS; | 
|  | 6321 | } | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6322 |  | 
| Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6323 | /* Initialize and seed the random generator. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6324 | mbedtls_psa_random_init(&global_data.rng); | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6325 | global_data.rng_state = RNG_INITIALIZED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6326 | status = mbedtls_psa_random_seed(&global_data.rng); | 
|  | 6327 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6328 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6329 | } | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6330 | global_data.rng_state = RNG_SEEDED; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6331 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6332 | status = psa_initialize_key_slots(); | 
|  | 6333 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 6334 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | } | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6336 |  | 
| Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 6337 | /* Init drivers */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6338 | status = psa_driver_wrapper_init(); | 
|  | 6339 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6340 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | } | 
| Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 6342 |  | 
| Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6343 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6344 | status = psa_crypto_load_transaction(); | 
|  | 6345 | if (status == PSA_SUCCESS) { | 
|  | 6346 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); | 
|  | 6347 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 6348 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6349 | } | 
|  | 6350 | status = psa_crypto_stop_transaction(); | 
|  | 6351 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6352 | /* There's no transaction to complete. It's all good. */ | 
|  | 6353 | status = PSA_SUCCESS; | 
|  | 6354 | } | 
| Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 6355 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ | 
| Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 6356 |  | 
| Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 6357 | /* All done. */ | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6358 | global_data.initialized = 1; | 
|  | 6359 |  | 
|  | 6360 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | if (status != PSA_SUCCESS) { | 
|  | 6362 | mbedtls_psa_crypto_free(); | 
|  | 6363 | } | 
|  | 6364 | return status; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6365 | } | 
|  | 6366 |  | 
|  | 6367 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |