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" |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 84 | #include "hash_info.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 85 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array))) |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 87 | |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 88 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 89 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
| 90 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 91 | #define BUILTIN_ALG_ANY_HKDF 1 |
Przemek Stekiel | 75fe3fb | 2022-06-09 14:44:55 +0200 | [diff] [blame] | 92 | #endif |
| 93 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 94 | /****************************************************************/ |
| 95 | /* Global data, support functions and library management */ |
| 96 | /****************************************************************/ |
| 97 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | static int key_type_is_raw_bytes(psa_key_type_t type) |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 99 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | return PSA_KEY_TYPE_IS_UNSTRUCTURED(type); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 103 | /* Values for psa_global_data_t::rng_state */ |
| 104 | #define RNG_NOT_INITIALIZED 0 |
| 105 | #define RNG_INITIALIZED 1 |
| 106 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | typedef struct { |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 109 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 110 | unsigned rng_state : 2; |
Gilles Peskine | 2d8a182 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 111 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 112 | } psa_global_data_t; |
| 113 | |
| 114 | static psa_global_data_t global_data; |
| 115 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 116 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 117 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 118 | &global_data.rng.drbg; |
| 119 | #endif |
| 120 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 121 | #define GUARD_MODULE_INITIALIZED \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 122 | if (global_data.initialized == 0) \ |
| 123 | return PSA_ERROR_BAD_STATE; |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | psa_status_t mbedtls_to_psa_error(int ret) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 126 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 127 | /* Mbed TLS error codes can combine a high-level error code and a |
| 128 | * low-level error code. The low-level error usually reflects the |
| 129 | * root cause better, so dispatch on that preferably. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | int low_level_ret = -(-ret & 0x007f); |
| 131 | switch (low_level_ret != 0 ? low_level_ret : ret) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 132 | case 0: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | return PSA_SUCCESS; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 134 | |
| 135 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 136 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 138 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 139 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 140 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 141 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 142 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 144 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 146 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 148 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 149 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 150 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 151 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 152 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 154 | |
| 155 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 157 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 158 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 159 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 160 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 162 | |
| 163 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 165 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 167 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 168 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 170 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 172 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 174 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 176 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 178 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 180 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | #if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 184 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 185 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 186 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 187 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 189 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 190 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 192 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 194 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 195 | |
| 196 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 198 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 199 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 200 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 201 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 203 | |
| 204 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | return PSA_ERROR_INVALID_SIGNATURE; |
Mateusz Starzyk | f28261f | 2021-09-30 16:39:07 +0200 | [diff] [blame] | 206 | case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 208 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 210 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 211 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 212 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 213 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 214 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 215 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 217 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 218 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 220 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 222 | #endif |
| 223 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 224 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 226 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 228 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 230 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 232 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 233 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 234 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 235 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 237 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 239 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 241 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 243 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 245 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 247 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 249 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 250 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 251 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 252 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 253 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 255 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 257 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 258 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 260 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 262 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 263 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 265 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 267 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 268 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 269 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 271 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 272 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | f9f1bdf | 2021-06-23 20:32:27 +0200 | [diff] [blame] | 273 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 275 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 276 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 278 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 280 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 281 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 283 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 285 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 287 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 289 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 290 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 292 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 294 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 296 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 298 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 299 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 300 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | return PSA_ERROR_INVALID_ARGUMENT; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 302 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | return PSA_ERROR_BUFFER_TOO_SMALL; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 304 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | return PSA_ERROR_NOT_SUPPORTED; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 306 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 307 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | return PSA_ERROR_INVALID_SIGNATURE; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 309 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 311 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 313 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 314 | case MBEDTLS_ERR_ECP_IN_PROGRESS: |
| 315 | return PSA_OPERATION_INCOMPLETE; |
| 316 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 317 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | return PSA_ERROR_CORRUPTION_DETECTED; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 319 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 320 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | return PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 325 | /** |
| 326 | * \brief For output buffers which contain "tags" |
| 327 | * (outputs that may be checked for validity like |
| 328 | * hashes, MACs and signatures), fill the unused |
| 329 | * part of the output buffer (the whole buffer on |
| 330 | * error, the trailing part on success) with |
| 331 | * something that isn't a valid tag (barring an |
| 332 | * attack on the tag and deliberately-crafted |
| 333 | * input), in case the caller doesn't check the |
| 334 | * return status properly. |
| 335 | * |
| 336 | * \param output_buffer Pointer to buffer to wipe. May not be NULL |
| 337 | * unless \p output_buffer_size is zero. |
| 338 | * \param status Status of function called to generate |
| 339 | * output_buffer originally |
| 340 | * \param output_buffer_size Size of output buffer. If zero, \p output_buffer |
| 341 | * could be NULL. |
| 342 | * \param output_buffer_length Length of data written to output_buffer, must be |
| 343 | * less than \p output_buffer_size |
| 344 | */ |
| 345 | static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status, |
Paul Elliott | 7118d17 | 2023-02-26 16:57:05 +0000 | [diff] [blame^] | 346 | size_t output_buffer_size, size_t output_buffer_length) |
| 347 | { |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 348 | size_t offset = 0; |
| 349 | |
| 350 | if (output_buffer_size == 0) { |
| 351 | /* If output_buffer_size is 0 then we have nothing to do. We must not |
| 352 | call memset because output_buffer may be NULL in this case */ |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | if (status == PSA_SUCCESS) { |
| 357 | offset = output_buffer_length; |
| 358 | } |
| 359 | |
| 360 | memset(output_buffer + offset, '!', output_buffer_size - offset); |
| 361 | } |
| 362 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 363 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 364 | |
| 365 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 366 | /****************************************************************/ |
| 367 | /* Key management */ |
| 368 | /****************************************************************/ |
| 369 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 370 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 371 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 372 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 373 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 374 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 375 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, |
| 376 | size_t bits, |
| 377 | int bits_is_sloppy) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 378 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | switch (curve) { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 380 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 382 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 383 | case 192: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | return MBEDTLS_ECP_DP_SECP192R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 385 | #endif |
| 386 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 387 | case 224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | return MBEDTLS_ECP_DP_SECP224R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 389 | #endif |
| 390 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 391 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | return MBEDTLS_ECP_DP_SECP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 393 | #endif |
| 394 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 395 | case 384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | return MBEDTLS_ECP_DP_SECP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 397 | #endif |
| 398 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 399 | case 521: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | return MBEDTLS_ECP_DP_SECP521R1; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 401 | case 528: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | if (bits_is_sloppy) { |
| 403 | return MBEDTLS_ECP_DP_SECP521R1; |
| 404 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 405 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 406 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 407 | } |
| 408 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 409 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 410 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 412 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 413 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 414 | return MBEDTLS_ECP_DP_BP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 415 | #endif |
| 416 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 417 | case 384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | return MBEDTLS_ECP_DP_BP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 419 | #endif |
| 420 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 421 | case 512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 422 | return MBEDTLS_ECP_DP_BP512R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 423 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 424 | } |
| 425 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 426 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 427 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 429 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 430 | case 255: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | return MBEDTLS_ECP_DP_CURVE25519; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 432 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | if (bits_is_sloppy) { |
| 434 | return MBEDTLS_ECP_DP_CURVE25519; |
| 435 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 436 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 437 | #endif |
| 438 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 439 | case 448: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | return MBEDTLS_ECP_DP_CURVE448; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 441 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 442 | } |
| 443 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 444 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 445 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 447 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 448 | case 192: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | return MBEDTLS_ECP_DP_SECP192K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 450 | #endif |
| 451 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 452 | case 224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | return MBEDTLS_ECP_DP_SECP224K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 454 | #endif |
| 455 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 456 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 457 | return MBEDTLS_ECP_DP_SECP256K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 458 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 459 | } |
| 460 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 461 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 462 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 463 | (void) bits_is_sloppy; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | return MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 465 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 466 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 467 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 468 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 469 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 470 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type, |
| 473 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 474 | { |
| 475 | /* Check that the bit size is acceptable for the key type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 477 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 478 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 479 | case PSA_KEY_TYPE_DERIVE: |
Neil Armstrong | 4b5710f | 2022-05-25 11:30:27 +0200 | [diff] [blame] | 480 | case PSA_KEY_TYPE_PASSWORD: |
| 481 | case PSA_KEY_TYPE_PASSWORD_HASH: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 482 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 483 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 484 | case PSA_KEY_TYPE_AES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | if (bits != 128 && bits != 192 && bits != 256) { |
| 486 | return PSA_ERROR_INVALID_ARGUMENT; |
| 487 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 488 | break; |
| 489 | #endif |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 490 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 491 | case PSA_KEY_TYPE_ARIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | if (bits != 128 && bits != 192 && bits != 256) { |
| 493 | return PSA_ERROR_INVALID_ARGUMENT; |
| 494 | } |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 495 | break; |
| 496 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 497 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 498 | case PSA_KEY_TYPE_CAMELLIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 499 | if (bits != 128 && bits != 192 && bits != 256) { |
| 500 | return PSA_ERROR_INVALID_ARGUMENT; |
| 501 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 502 | break; |
| 503 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 504 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 505 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | if (bits != 64 && bits != 128 && bits != 192) { |
| 507 | return PSA_ERROR_INVALID_ARGUMENT; |
| 508 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 509 | break; |
| 510 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 511 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 512 | case PSA_KEY_TYPE_CHACHA20: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | if (bits != 256) { |
| 514 | return PSA_ERROR_INVALID_ARGUMENT; |
| 515 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 516 | break; |
| 517 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 518 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 520 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | if (bits % 8 != 0) { |
| 522 | return PSA_ERROR_INVALID_ARGUMENT; |
| 523 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 528 | /** 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] | 529 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 530 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 531 | * 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] | 532 | * |
| 533 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 534 | * \param[in] key_type The key type of the key to be used with the |
| 535 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 536 | * |
| 537 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 538 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 539 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 540 | * 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] | 541 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 542 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 543 | psa_algorithm_t algorithm, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 545 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 547 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 548 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 549 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 550 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 553 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 554 | * key. */ |
| 555 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 556 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 557 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 558 | * the block length (larger than 1) for block ciphers. */ |
| 559 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 560 | return PSA_SUCCESS; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 566 | } |
| 567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 568 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 569 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 570 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | if (slot->key.data != NULL) { |
| 572 | return PSA_ERROR_ALREADY_EXISTS; |
| 573 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 575 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 576 | if (slot->key.data == NULL) { |
| 577 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 578 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 579 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 580 | slot->key.bytes = buffer_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 581 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 582 | } |
| 583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 585 | const uint8_t *data, |
| 586 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 587 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 589 | data_length); |
| 590 | if (status != PSA_SUCCESS) { |
| 591 | return status; |
| 592 | } |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | memcpy(slot->key.data, data, data_length); |
| 595 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 598 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 599 | const psa_key_attributes_t *attributes, |
| 600 | const uint8_t *data, size_t data_length, |
| 601 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | size_t *key_buffer_length, size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 603 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 604 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 605 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 606 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 607 | /* zero-length keys are never supported. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | if (data_length == 0) { |
| 609 | return PSA_ERROR_NOT_SUPPORTED; |
| 610 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 612 | if (key_type_is_raw_bytes(type)) { |
| 613 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 615 | status = psa_validate_unstructured_key_bit_size(attributes->core.type, |
| 616 | *bits); |
| 617 | if (status != PSA_SUCCESS) { |
| 618 | return status; |
| 619 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 620 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 621 | /* Copy the key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 622 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 623 | *key_buffer_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 624 | (void) key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | return PSA_SUCCESS; |
| 627 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 628 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 630 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 631 | return mbedtls_psa_ecp_import_key(attributes, |
| 632 | data, data_length, |
| 633 | key_buffer, key_buffer_size, |
| 634 | key_buffer_length, |
| 635 | bits); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 636 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 637 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 638 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 639 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 640 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 641 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 642 | return mbedtls_psa_rsa_import_key(attributes, |
| 643 | data, data_length, |
| 644 | key_buffer, key_buffer_size, |
| 645 | key_buffer_length, |
| 646 | bits); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 647 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 648 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 649 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 650 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 653 | } |
| 654 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 655 | /** Calculate the intersection of two algorithm usage policies. |
| 656 | * |
| 657 | * Return 0 (which allows no operation) on incompatibility. |
| 658 | */ |
| 659 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 660 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 661 | psa_algorithm_t alg1, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 662 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 663 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 664 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | if (alg1 == alg2) { |
| 666 | return alg1; |
| 667 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 668 | /* If the policies are from the same hash-and-sign family, check |
| 669 | * 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] | 670 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 671 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 672 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 673 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 674 | return alg2; |
| 675 | } |
| 676 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 677 | return alg1; |
| 678 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 679 | } |
| 680 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 681 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 682 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 684 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 685 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 686 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 687 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 688 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 689 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 690 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 691 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 692 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 693 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 694 | alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 695 | } |
| 696 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 697 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 698 | (alg1_len <= alg2_len)) { |
| 699 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 700 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 701 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 702 | (alg2_len <= alg1_len)) { |
| 703 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | /* If the policies are from the same MAC family, check whether one |
| 707 | * of them is a minimum-MAC-length policy. Calculate the most |
| 708 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 709 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 710 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 711 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 712 | /* Validate the combination of key type and algorithm. Since the base |
| 713 | * 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] | 714 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 715 | return 0; |
| 716 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 717 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 718 | /* Get the (exact or at-least) output lengths for both sides of the |
| 719 | * requested intersection. None of the currently supported algorithms |
| 720 | * have an output length dependent on the actual key size, so setting it |
| 721 | * to a bogus value of 0 is currently OK. |
| 722 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 723 | * Note that for at-least-this-length wildcard algorithms, the output |
| 724 | * length is set to the shortest allowed length, which allows us to |
| 725 | * calculate the most restrictive tag length for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 726 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 727 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 728 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 729 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 730 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 731 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 732 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 733 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 734 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 735 | |
| 736 | /* If only one is an at-least-this-length policy, the intersection would |
| 737 | * be the other (fixed-length) policy as long as said fixed length is |
| 738 | * equal to or larger than the shortest allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 739 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 740 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 741 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 743 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 744 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 745 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 746 | /* If none of them are wildcards, check whether they define the same tag |
| 747 | * length. This is still possible here when one is default-length and |
| 748 | * the other specific-length. Ensure to always return the |
| 749 | * specific-length version for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 750 | if (alg1_len == alg2_len) { |
| 751 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 752 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 753 | } |
| 754 | /* If the policies are incompatible, allow nothing. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 758 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 759 | psa_algorithm_t policy_alg, |
| 760 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 761 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 762 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 763 | if (requested_alg == policy_alg) { |
| 764 | return 1; |
| 765 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 766 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 767 | * and requested_alg is the same hash-and-sign family with any hash, |
| 768 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 770 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 771 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 772 | (requested_alg & ~PSA_ALG_HASH_MASK); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 773 | } |
| 774 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 775 | * the requested algorithm, check the requested tag length to be |
| 776 | * equal-length or longer than the wildcard-specified length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 778 | PSA_ALG_IS_AEAD(requested_alg) && |
| 779 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 780 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 781 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 782 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 783 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 784 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 785 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 786 | * algorithm, check whether their MAC lengths are compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 788 | PSA_ALG_IS_MAC(requested_alg) && |
| 789 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 790 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 791 | /* Validate the combination of key type and algorithm. Since the policy |
| 792 | * and requested algorithms are the same, we only need this once. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 793 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 794 | return 0; |
| 795 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 796 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 797 | /* Get both the requested output length for the algorithm which is to be |
| 798 | * verified, and the default output length for the base algorithm. |
| 799 | * Note that none of the currently supported algorithms have an output |
| 800 | * length dependent on actual key size, so setting it to a bogus value |
| 801 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 802 | size_t requested_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 803 | key_type, 0, requested_alg); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 804 | size_t default_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 805 | key_type, 0, |
| 806 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 807 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 808 | /* If the policy is default-length, only allow an algorithm with |
| 809 | * a declared exact-length matching the default. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 811 | return requested_output_length == default_output_length; |
| 812 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 813 | |
| 814 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 815 | * length exactly matches the default length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 816 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 817 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 818 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 821 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 822 | * check for the requested MAC length to be equal to or longer than the |
| 823 | * minimum allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 824 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 825 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 826 | requested_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 827 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 828 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 829 | /* 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] | 830 | * a key derivation with that key agreement should also be allowed. This |
| 831 | * behaviour is expected to be defined in a future specification version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 832 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 833 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 834 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 835 | policy_alg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 836 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 837 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 838 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 841 | /** Test whether a policy permits an algorithm. |
| 842 | * |
| 843 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 844 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 845 | * \note This function requires providing the key type for which the policy is |
| 846 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 847 | * have different properties depending on what kind of cipher it is |
| 848 | * combined with. |
| 849 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 850 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 851 | * allowed by the \p policy. |
| 852 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 853 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 854 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 855 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 856 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 857 | psa_key_type_t key_type, |
| 858 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 859 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 860 | /* '0' is not a valid algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 861 | if (alg == 0) { |
| 862 | return PSA_ERROR_INVALID_ARGUMENT; |
| 863 | } |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 864 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 865 | /* A requested algorithm cannot be a wildcard. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 867 | return PSA_ERROR_INVALID_ARGUMENT; |
| 868 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 870 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 871 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 872 | return PSA_SUCCESS; |
| 873 | } else { |
| 874 | return PSA_ERROR_NOT_PERMITTED; |
| 875 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 876 | } |
| 877 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 878 | /** Restrict a key policy based on a constraint. |
| 879 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 880 | * \note This function requires providing the key type for which the policy is |
| 881 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 882 | * have different properties depending on what kind of cipher it is |
| 883 | * combined with. |
| 884 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 885 | * \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] | 886 | * \param[in,out] policy The policy to restrict. |
| 887 | * \param[in] constraint The policy constraint to apply. |
| 888 | * |
| 889 | * \retval #PSA_SUCCESS |
| 890 | * \c *policy contains the intersection of the original value of |
| 891 | * \c *policy and \c *constraint. |
| 892 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 893 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 894 | * \c *policy is unchanged. |
| 895 | */ |
| 896 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 897 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 898 | psa_key_policy_t *policy, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 899 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 900 | { |
| 901 | psa_algorithm_t intersection_alg = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 902 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 903 | constraint->alg); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 904 | psa_algorithm_t intersection_alg2 = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 905 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 906 | constraint->alg2); |
| 907 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 908 | return PSA_ERROR_INVALID_ARGUMENT; |
| 909 | } |
| 910 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 911 | return PSA_ERROR_INVALID_ARGUMENT; |
| 912 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 913 | policy->usage &= constraint->usage; |
| 914 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 915 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 916 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 917 | } |
| 918 | |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 919 | psa_status_t psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 920 | mbedtls_svc_key_id_t key, |
| 921 | psa_key_slot_t **p_slot, |
| 922 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 923 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 924 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 925 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 926 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 928 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 929 | if (status != PSA_SUCCESS) { |
| 930 | return status; |
| 931 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 932 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 933 | |
| 934 | /* Enforce that usage policy for the key slot contains all the flags |
| 935 | * required by the usage parameter. There is one exception: public |
| 936 | * keys can always be exported, so we treat public key objects as |
| 937 | * if they had the export flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 938 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 939 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 940 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 942 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 943 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 944 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 945 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 946 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 947 | /* Enforce that the usage policy permits the requested algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 948 | if (alg != 0) { |
| 949 | status = psa_key_policy_permits(&slot->attr.policy, |
| 950 | slot->attr.type, |
| 951 | alg); |
| 952 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 953 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 954 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 955 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 957 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 958 | |
| 959 | error: |
| 960 | *p_slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 963 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 964 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 965 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 966 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 967 | * |
| 968 | * 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] | 969 | * available, as opposed to a key in a secure element and/or to be used |
| 970 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 971 | * |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 972 | * This is a temporary function that may be used instead of |
| 973 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 974 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 975 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 976 | * On success, the returned key slot is locked. It is the responsibility of the |
| 977 | * 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] | 978 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 979 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 980 | mbedtls_svc_key_id_t key, |
| 981 | psa_key_slot_t **p_slot, |
| 982 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 983 | psa_algorithm_t alg) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 984 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 986 | usage, alg); |
| 987 | if (status != PSA_SUCCESS) { |
| 988 | return status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 989 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 991 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
| 992 | psa_unlock_key_slot(*p_slot); |
| 993 | *p_slot = NULL; |
| 994 | return PSA_ERROR_NOT_SUPPORTED; |
| 995 | } |
| 996 | |
| 997 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 998 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1000 | 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] | 1001 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1002 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1003 | * initialized slot, so we can just free it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1004 | if (slot->key.data != NULL) { |
| 1005 | mbedtls_platform_zeroize(slot->key.data, slot->key.bytes); |
| 1006 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1008 | mbedtls_free(slot->key.data); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1009 | slot->key.data = NULL; |
| 1010 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1012 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1015 | /** Completely wipe a slot in memory, including its policy. |
| 1016 | * Persistent storage is not affected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1017 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1018 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1019 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1021 | /* |
| 1022 | * As the return error code may not be handled in case of multiple errors, |
| 1023 | * do our best to report an unexpected lock counter. Assert with |
| 1024 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one: |
| 1025 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 1026 | * function is called as part of the execution of a test suite, the |
| 1027 | * execution of the test suite is stopped in error if the assertion fails. |
| 1028 | */ |
| 1029 | if (slot->lock_count != 1) { |
| 1030 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1031 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1032 | } |
| 1033 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1034 | /* Multipart operations may still be using the key. This is safe |
| 1035 | * because all multipart operation objects are independent from |
| 1036 | * the key slot: if they need to access the key after the setup |
| 1037 | * phase, they have a copy of the key. Note that this means that |
| 1038 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1039 | /* At this point, key material and other type-specific content has |
| 1040 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1041 | * zeroize because the metadata is not particularly sensitive. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1042 | memset(slot, 0, sizeof(*slot)); |
| 1043 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1046 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1047 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1048 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1049 | psa_status_t status; /* status of the last operation */ |
| 1050 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1051 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1052 | psa_se_drv_table_entry_t *driver; |
| 1053 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1055 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1056 | return PSA_SUCCESS; |
| 1057 | } |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1058 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1059 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1060 | * 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] | 1061 | * key, this will load the key description from persistent memory if not |
| 1062 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1063 | * the key is operated by an SE or not and this information is needed by |
| 1064 | * the current implementation. |
| 1065 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1066 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1067 | if (status != PSA_SUCCESS) { |
| 1068 | return status; |
| 1069 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1070 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1071 | /* |
| 1072 | * If the key slot containing the key description is under access by the |
| 1073 | * library (apart from the present access), the key cannot be destroyed |
| 1074 | * yet. For the time being, just return in error. Eventually (to be |
| 1075 | * implemented), the key should be destroyed when all accesses have |
| 1076 | * stopped. |
| 1077 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1078 | if (slot->lock_count > 1) { |
| 1079 | psa_unlock_key_slot(slot); |
| 1080 | return PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1083 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1084 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1085 | * if we attempt it, depending on whether the key is merely read-only |
| 1086 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1087 | * Just do the best we can, which is to wipe the copy in memory |
| 1088 | * (done in this function's cleanup code). */ |
| 1089 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1090 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1093 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1094 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1095 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1096 | /* For a key in a secure element, we need to do three things: |
| 1097 | * remove the key file in internal storage, destroy the |
| 1098 | * key inside the secure element, and update the driver's |
| 1099 | * persistent data. Start a transaction that will encompass these |
| 1100 | * three actions. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1102 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1103 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1104 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1105 | status = psa_crypto_save_transaction(); |
| 1106 | if (status != PSA_SUCCESS) { |
| 1107 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1108 | /* We should still try to destroy the key in the secure |
| 1109 | * element and the key metadata in storage. This is especially |
| 1110 | * important if the error is that the storage is full. |
| 1111 | * But how to do it exactly without risking an inconsistent |
| 1112 | * state after a reset? |
| 1113 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1114 | */ |
| 1115 | overall_status = status; |
| 1116 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1117 | } |
| 1118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1119 | status = psa_destroy_se_key(driver, |
| 1120 | psa_key_slot_get_slot_number(slot)); |
| 1121 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1122 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1124 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1125 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1126 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1127 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1129 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1130 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1131 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | } |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1133 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1134 | /* TODO: other slots may have a copy of the same key. We should |
| 1135 | * invalidate them. |
| 1136 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1137 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1138 | } |
| 1139 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1140 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1141 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1142 | if (driver != NULL) { |
| 1143 | status = psa_save_se_persistent_data(driver); |
| 1144 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1145 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1146 | } |
| 1147 | status = psa_crypto_stop_transaction(); |
| 1148 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1149 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1150 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1151 | } |
| 1152 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1153 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1154 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1155 | status = psa_wipe_key_slot(slot); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1156 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1157 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1158 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | } |
| 1160 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1161 | } |
| 1162 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1163 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [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 | static psa_status_t psa_get_rsa_public_exponent( |
| 1166 | const mbedtls_rsa_context *rsa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1167 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1168 | { |
| 1169 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1170 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1171 | uint8_t *buffer = NULL; |
| 1172 | size_t buflen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1175 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1176 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1177 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1178 | } |
| 1179 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1180 | /* It's the default value, which is reported as an empty string, |
| 1181 | * so there's nothing to do. */ |
| 1182 | goto exit; |
| 1183 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1185 | buflen = mbedtls_mpi_size(&mpi); |
| 1186 | buffer = mbedtls_calloc(1, buflen); |
| 1187 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1188 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1189 | goto exit; |
| 1190 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1191 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1192 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1193 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1194 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1195 | attributes->domain_parameters = buffer; |
| 1196 | attributes->domain_parameters_size = buflen; |
| 1197 | |
| 1198 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1199 | mbedtls_mpi_free(&mpi); |
| 1200 | if (ret != 0) { |
| 1201 | mbedtls_free(buffer); |
| 1202 | } |
| 1203 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1204 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1205 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1206 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1207 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1208 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1209 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1210 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1211 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1212 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1213 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1214 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1215 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1217 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1219 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1220 | if (status != PSA_SUCCESS) { |
| 1221 | return status; |
| 1222 | } |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1223 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1224 | attributes->core = slot->attr; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1225 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1226 | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1227 | |
| 1228 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1229 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1230 | psa_set_key_slot_number(attributes, |
| 1231 | psa_key_slot_get_slot_number(slot)); |
| 1232 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1233 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | switch (slot->attr.type) { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1236 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1237 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1238 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1239 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1240 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1241 | * is not yet implemented. |
| 1242 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1243 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1244 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1245 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1246 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1247 | status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1248 | slot->attr.type, |
| 1249 | slot->key.data, |
| 1250 | slot->key.bytes, |
| 1251 | &rsa); |
| 1252 | if (status != PSA_SUCCESS) { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1253 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1254 | } |
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 | status = psa_get_rsa_public_exponent(rsa, |
| 1257 | attributes); |
| 1258 | mbedtls_rsa_free(rsa); |
| 1259 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1260 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1261 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1262 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1263 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1264 | default: |
| 1265 | /* Nothing else to do. */ |
| 1266 | break; |
| 1267 | } |
| 1268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1269 | if (status != PSA_SUCCESS) { |
| 1270 | psa_reset_key_attributes(attributes); |
| 1271 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1273 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1275 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1278 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1279 | psa_status_t psa_get_key_slot_number( |
| 1280 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1282 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1283 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1284 | *slot_number = attributes->slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1285 | return PSA_SUCCESS; |
| 1286 | } else { |
| 1287 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1288 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1289 | } |
| 1290 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1292 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1293 | size_t key_buffer_size, |
| 1294 | uint8_t *data, |
| 1295 | size_t data_size, |
| 1296 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1297 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1298 | if (key_buffer_size > data_size) { |
| 1299 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1300 | } |
| 1301 | memcpy(data, key_buffer, key_buffer_size); |
| 1302 | memset(data + key_buffer_size, 0, |
| 1303 | data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1304 | *data_length = key_buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1305 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1308 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1309 | const psa_key_attributes_t *attributes, |
| 1310 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1311 | uint8_t *data, size_t data_size, size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1312 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1313 | psa_key_type_t type = attributes->core.type; |
| 1314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1315 | if (key_type_is_raw_bytes(type) || |
| 1316 | PSA_KEY_TYPE_IS_RSA(type) || |
| 1317 | PSA_KEY_TYPE_IS_ECC(type)) { |
| 1318 | return psa_export_key_buffer_internal( |
| 1319 | key_buffer, key_buffer_size, |
| 1320 | data, data_size, data_length); |
| 1321 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1322 | /* This shouldn't happen in the reference implementation, but |
| 1323 | it is valid for a special-purpose implementation to omit |
| 1324 | support for exporting certain key types. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1325 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1329 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
| 1330 | uint8_t *data, |
| 1331 | size_t data_size, |
| 1332 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1333 | { |
| 1334 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1335 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1336 | psa_key_slot_t *slot; |
| 1337 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1338 | /* Reject a zero-length output buffer now, since this can never be a |
| 1339 | * valid key representation. This way we know that data must be a valid |
| 1340 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1341 | if (data_size == 0) { |
| 1342 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1343 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1344 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1345 | /* Set the key to empty now, so that even when there are errors, we always |
| 1346 | * set data_length to a value between 0 and data_size. On error, setting |
| 1347 | * the key to empty is a good choice because an empty key representation is |
| 1348 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1349 | *data_length = 0; |
| 1350 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1351 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1352 | * which don't require any flag, but |
| 1353 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1354 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1355 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1356 | PSA_KEY_USAGE_EXPORT, 0); |
| 1357 | if (status != PSA_SUCCESS) { |
| 1358 | return status; |
| 1359 | } |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1360 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1361 | psa_key_attributes_t attributes = { |
| 1362 | .core = slot->attr |
| 1363 | }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1364 | status = psa_driver_wrapper_export_key(&attributes, |
| 1365 | slot->key.data, slot->key.bytes, |
| 1366 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1368 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1370 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1373 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1374 | const psa_key_attributes_t *attributes, |
| 1375 | const uint8_t *key_buffer, |
| 1376 | size_t key_buffer_size, |
| 1377 | uint8_t *data, |
| 1378 | size_t data_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1379 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1380 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1381 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1383 | if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) { |
| 1384 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1385 | /* Exporting public -> public */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1386 | return psa_export_key_buffer_internal( |
| 1387 | key_buffer, key_buffer_size, |
| 1388 | data, data_size, data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1389 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1391 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1392 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1393 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1394 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1395 | key_buffer, |
| 1396 | key_buffer_size, |
| 1397 | data, |
| 1398 | data_size, |
| 1399 | data_length); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1400 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1401 | /* 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] | 1402 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1403 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1404 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1405 | } else { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1406 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1407 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1408 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1409 | key_buffer, |
| 1410 | key_buffer_size, |
| 1411 | data, |
| 1412 | data_size, |
| 1413 | data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1414 | #else |
| 1415 | /* 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] | 1416 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1417 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1418 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1419 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1420 | } else { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1421 | /* This shouldn't happen in the reference implementation, but |
| 1422 | it is valid for a special-purpose implementation to omit |
| 1423 | support for exporting certain key types. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1424 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1425 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1426 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1428 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, |
| 1429 | uint8_t *data, |
| 1430 | size_t data_size, |
| 1431 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1432 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1433 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1434 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1435 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1436 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1437 | /* Reject a zero-length output buffer now, since this can never be a |
| 1438 | * valid key representation. This way we know that data must be a valid |
| 1439 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1440 | if (data_size == 0) { |
| 1441 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1442 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1443 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1444 | /* Set the key to empty now, so that even when there are errors, we always |
| 1445 | * set data_length to a value between 0 and data_size. On error, setting |
| 1446 | * the key to empty is a good choice because an empty key representation is |
| 1447 | * unlikely to be accepted anywhere. */ |
| 1448 | *data_length = 0; |
| 1449 | |
| 1450 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1451 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1452 | if (status != PSA_SUCCESS) { |
| 1453 | return status; |
| 1454 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1456 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1457 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1458 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1459 | } |
| 1460 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1461 | psa_key_attributes_t attributes = { |
| 1462 | .core = slot->attr |
| 1463 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1464 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1465 | &attributes, slot->key.data, slot->key.bytes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1466 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1467 | |
| 1468 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1469 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1471 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1472 | } |
| 1473 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1474 | #if defined(static_assert) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1475 | static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1476 | "One or more key attribute flag is listed as both external-only and dual-use"); |
| 1477 | static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1478 | "One or more key attribute flag is listed as both internal-only and dual-use"); |
| 1479 | static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1480 | "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] | 1481 | #endif |
| 1482 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1483 | /** Validate that a key policy is internally well-formed. |
| 1484 | * |
| 1485 | * This function only rejects invalid policies. It does not validate the |
| 1486 | * consistency of the policy with respect to other attributes of the key |
| 1487 | * such as the key type. |
| 1488 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1489 | 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] | 1490 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1491 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1492 | PSA_KEY_USAGE_COPY | |
| 1493 | PSA_KEY_USAGE_ENCRYPT | |
| 1494 | PSA_KEY_USAGE_DECRYPT | |
| 1495 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1496 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1497 | PSA_KEY_USAGE_SIGN_HASH | |
| 1498 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1499 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
| 1500 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1501 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1502 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1504 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1505 | } |
| 1506 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1507 | /** Validate the internal consistency of key attributes. |
| 1508 | * |
| 1509 | * This function only rejects invalid attribute values. If does not |
| 1510 | * validate the consistency of the attributes with any key data that may |
| 1511 | * be involved in the creation of the key. |
| 1512 | * |
| 1513 | * Call this function early in the key creation process. |
| 1514 | * |
| 1515 | * \param[in] attributes Key attributes for the new key. |
| 1516 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1517 | * NULL for a transparent key. |
| 1518 | * |
| 1519 | */ |
| 1520 | static psa_status_t psa_validate_key_attributes( |
| 1521 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1522 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1523 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1524 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1525 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1526 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | status = psa_validate_key_location(lifetime, p_drv); |
| 1529 | if (status != PSA_SUCCESS) { |
| 1530 | return status; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1531 | } |
| 1532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1533 | status = psa_validate_key_persistence(lifetime); |
| 1534 | if (status != PSA_SUCCESS) { |
| 1535 | return status; |
| 1536 | } |
| 1537 | |
| 1538 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1539 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1540 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1541 | } |
| 1542 | } else { |
| 1543 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1544 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1549 | if (status != PSA_SUCCESS) { |
| 1550 | return status; |
| 1551 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1552 | |
| 1553 | /* Refuse to create overly large keys. |
| 1554 | * Note that this doesn't trigger on import if the attributes don't |
| 1555 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1556 | * psa_import_key() needs its own checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1557 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1558 | return PSA_ERROR_NOT_SUPPORTED; |
| 1559 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1560 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1561 | /* Reject invalid flags. These should not be reachable through the API. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1562 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1563 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { |
| 1564 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1565 | } |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1567 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1568 | } |
| 1569 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1570 | /** Prepare a key slot to receive key material. |
| 1571 | * |
| 1572 | * This function allocates a key slot and sets its metadata. |
| 1573 | * |
| 1574 | * If this function fails, call psa_fail_key_creation(). |
| 1575 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1576 | * This function is intended to be used as follows: |
| 1577 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1578 | * it with the specified attributes, and in case of a volatile key assign it |
| 1579 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1580 | * -# Populate the slot with the key material. |
| 1581 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1582 | * In case of failure at any step, stop the sequence and call |
| 1583 | * psa_fail_key_creation(). |
| 1584 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1585 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1586 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1587 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1588 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1589 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1590 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1591 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1592 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1593 | * |
| 1594 | * \retval #PSA_SUCCESS |
| 1595 | * The key slot is ready to receive key material. |
| 1596 | * \return If this function fails, the key slot is an invalid state. |
| 1597 | * 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] | 1598 | */ |
| 1599 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1600 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1601 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1602 | psa_key_slot_t **p_slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1603 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1604 | { |
| 1605 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1606 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1607 | psa_key_slot_t *slot; |
| 1608 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1609 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1610 | *p_drv = NULL; |
| 1611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1612 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1613 | if (status != PSA_SUCCESS) { |
| 1614 | return status; |
| 1615 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1617 | status = psa_get_empty_key_slot(&volatile_key_id, p_slot); |
| 1618 | if (status != PSA_SUCCESS) { |
| 1619 | return status; |
| 1620 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1621 | slot = *p_slot; |
| 1622 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1623 | /* We're storing the declared bit-size of the key. It's up to each |
| 1624 | * creation mechanism to verify that this information is correct. |
| 1625 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1626 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1627 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1628 | * volatile key identifier associated to the slot returned to contain its |
| 1629 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1630 | |
| 1631 | slot->attr = attributes->core; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1632 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1633 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1634 | slot->attr.id = volatile_key_id; |
| 1635 | #else |
| 1636 | slot->attr.id.key_id = volatile_key_id; |
| 1637 | #endif |
| 1638 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1639 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1640 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1641 | * external-only flags, query `attributes`. Thanks to the check |
| 1642 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1643 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1644 | * may have set. */ |
| 1645 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1646 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1647 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1648 | /* 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] | 1649 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1650 | * create the key file in internal storage, create the |
| 1651 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1652 | * persistent data. This is done by starting a transaction that will |
| 1653 | * encompass these three actions. |
| 1654 | * For registering a volatile key, we just need to find an appropriate |
| 1655 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1656 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1657 | /* The first thing to do is to find a slot number for the new key. |
| 1658 | * We save the slot number in persistent storage as part of the |
| 1659 | * transaction data. It will be needed to recover if the power |
| 1660 | * fails during the key creation process, to clean up on the secure |
| 1661 | * element side after restarting. Obtaining a slot number from the |
| 1662 | * secure element driver updates its persistent state, but we do not yet |
| 1663 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1664 | * 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] | 1665 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1666 | psa_key_slot_number_t slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1667 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1668 | &slot_number); |
| 1669 | if (status != PSA_SUCCESS) { |
| 1670 | return status; |
| 1671 | } |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1673 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1674 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1675 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1676 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1677 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1678 | status = psa_crypto_save_transaction(); |
| 1679 | if (status != PSA_SUCCESS) { |
| 1680 | (void) psa_crypto_stop_transaction(); |
| 1681 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1682 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1683 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1684 | |
| 1685 | status = psa_copy_key_material_into_slot( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1687 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1689 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1690 | /* Key registration only makes sense with a secure element. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1691 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1692 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1693 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1695 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1696 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1697 | |
| 1698 | /** Finalize the creation of a key once its key material has been set. |
| 1699 | * |
| 1700 | * This entails writing the key to persistent storage. |
| 1701 | * |
| 1702 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1703 | * See the documentation of psa_start_key_creation() for the intended use |
| 1704 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1705 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1706 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1707 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1708 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1709 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1710 | * \param[in,out] slot Pointer to the slot with key material. |
| 1711 | * \param[in] driver The secure element driver for the key, |
| 1712 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1713 | * \param[out] key On success, identifier of the key. Note that the |
| 1714 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1715 | * |
| 1716 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1717 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1718 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1719 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1720 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1721 | * \retval #PSA_ERROR_DATA_INVALID |
| 1722 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1723 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1724 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1725 | * \return If this function fails, the key slot is an invalid state. |
| 1726 | * 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] | 1727 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1728 | static psa_status_t psa_finish_key_creation( |
| 1729 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1730 | psa_se_drv_table_entry_t *driver, |
| 1731 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1732 | { |
| 1733 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1734 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1735 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1736 | |
| 1737 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1738 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1739 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1740 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1741 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1742 | psa_key_slot_number_t slot_number = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1743 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1744 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1745 | #if defined(static_assert) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1746 | static_assert(sizeof(slot_number) == |
| 1747 | sizeof(data.slot_number), |
| 1748 | "Slot number size does not match psa_se_key_data_storage_t"); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1749 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1750 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1751 | status = psa_save_persistent_key(&slot->attr, |
| 1752 | (uint8_t *) &data, |
| 1753 | sizeof(data)); |
| 1754 | } else |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1755 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1756 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1757 | /* Key material is saved in export representation in the slot, so |
| 1758 | * just pass the slot buffer for storage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1759 | status = psa_save_persistent_key(&slot->attr, |
| 1760 | slot->key.data, |
| 1761 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1762 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1763 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1764 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1765 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1766 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1767 | /* Finish the transaction for a key creation. This does not |
| 1768 | * happen when registering an existing key. Detect this case |
| 1769 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1770 | * creation of a persistent key in a secure element requires a transaction, |
| 1771 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1772 | if (driver != NULL && |
| 1773 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1774 | status = psa_save_se_persistent_data(driver); |
| 1775 | if (status != PSA_SUCCESS) { |
| 1776 | psa_destroy_persistent_key(slot->attr.id); |
| 1777 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1778 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1779 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1780 | } |
| 1781 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1783 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1784 | *key = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1785 | status = psa_unlock_key_slot(slot); |
| 1786 | if (status != PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1787 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1788 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1789 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | /** Abort the creation of a key. |
| 1795 | * |
| 1796 | * You may call this function after calling psa_start_key_creation(), |
| 1797 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1798 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1799 | * See the documentation of psa_start_key_creation() for the intended use |
| 1800 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1801 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1802 | * \param[in,out] slot Pointer to the slot with key material. |
| 1803 | * \param[in] driver The secure element driver for the key, |
| 1804 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1805 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1806 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 1807 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1808 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1809 | (void) driver; |
| 1810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1811 | if (slot == NULL) { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1812 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1813 | } |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1814 | |
| 1815 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1816 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1817 | * element, and the failure happened later (when saving metadata |
| 1818 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1819 | * element. |
| 1820 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1821 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1822 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1823 | /* Abort the ongoing transaction if any (there may not be one if |
| 1824 | * the creation process failed before starting one, or if the |
| 1825 | * key creation is a registration of a key in a secure element). |
| 1826 | * Earlier functions must already have done what it takes to undo any |
| 1827 | * partial creation. All that's left is to update the transaction data |
| 1828 | * itself. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1829 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1830 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1832 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1833 | } |
| 1834 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1835 | /** Validate optional attributes during key creation. |
| 1836 | * |
| 1837 | * Some key attributes are optional during key creation. If they are |
| 1838 | * specified in the attributes structure, check that they are consistent |
| 1839 | * with the data in the slot. |
| 1840 | * |
| 1841 | * This function should be called near the end of key creation, after |
| 1842 | * the slot in memory is fully populated but before saving persistent data. |
| 1843 | */ |
| 1844 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1845 | const psa_key_slot_t *slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1847 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | if (attributes->core.type != 0) { |
| 1849 | if (attributes->core.type != slot->attr.type) { |
| 1850 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1851 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1852 | } |
| 1853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1854 | if (attributes->domain_parameters_size != 0) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1855 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1857 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1858 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1859 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1860 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1861 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1862 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1863 | slot->attr.type, |
| 1864 | slot->key.data, |
| 1865 | slot->key.bytes, |
| 1866 | &rsa); |
| 1867 | if (status != PSA_SUCCESS) { |
| 1868 | return status; |
| 1869 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1871 | mbedtls_mpi_init(&actual); |
| 1872 | mbedtls_mpi_init(&required); |
| 1873 | ret = mbedtls_rsa_export(rsa, |
| 1874 | NULL, NULL, NULL, NULL, &actual); |
| 1875 | mbedtls_rsa_free(rsa); |
| 1876 | mbedtls_free(rsa); |
| 1877 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1878 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1879 | } |
| 1880 | ret = mbedtls_mpi_read_binary(&required, |
| 1881 | attributes->domain_parameters, |
| 1882 | attributes->domain_parameters_size); |
| 1883 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1884 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1885 | } |
| 1886 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1887 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1888 | } |
| 1889 | rsa_exit: |
| 1890 | mbedtls_mpi_free(&actual); |
| 1891 | mbedtls_mpi_free(&required); |
| 1892 | if (ret != 0) { |
| 1893 | return mbedtls_to_psa_error(ret); |
| 1894 | } |
| 1895 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1896 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1897 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1898 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1903 | if (attributes->core.bits != 0) { |
| 1904 | if (attributes->core.bits != slot->attr.bits) { |
| 1905 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1906 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1907 | } |
| 1908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1909 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1910 | } |
| 1911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1912 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
| 1913 | const uint8_t *data, |
| 1914 | size_t data_length, |
| 1915 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1916 | { |
| 1917 | psa_status_t status; |
| 1918 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1919 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1920 | size_t bits; |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1921 | size_t storage_size = data_length; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1922 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1923 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1924 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1925 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1926 | * This also rejects any key which might be encoded as an empty string, |
| 1927 | * which is never valid. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1928 | if (data_length == 0) { |
| 1929 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1930 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1931 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1932 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1933 | if (data_length > SIZE_MAX / 8) { |
| 1934 | return PSA_ERROR_NOT_SUPPORTED; |
| 1935 | } |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1937 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 1938 | &slot, &driver); |
| 1939 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1940 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1941 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1942 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1943 | /* 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] | 1944 | * storage ( thus not in the case of importing a key in a secure element |
| 1945 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 1946 | * buffer to hold the imported key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1947 | if (slot->key.data == NULL) { |
| 1948 | if (psa_key_lifetime_is_external(attributes->core.lifetime)) { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1949 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1950 | attributes, data, data_length, &storage_size); |
| 1951 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1952 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1954 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1955 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 1956 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1957 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1958 | } |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1959 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1960 | |
| 1961 | bits = slot->attr.bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1962 | status = psa_driver_wrapper_import_key(attributes, |
| 1963 | data, data_length, |
| 1964 | slot->key.data, |
| 1965 | slot->key.bytes, |
| 1966 | &slot->key.bytes, &bits); |
| 1967 | if (status != PSA_SUCCESS) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1968 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1969 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1971 | if (slot->attr.bits == 0) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1972 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1973 | } else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1974 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1975 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1976 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 1977 | |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1978 | /* Enforce a size limit, and in particular ensure that the bit |
| 1979 | * size fits in its representation type.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1980 | if (bits > PSA_MAX_KEY_BITS) { |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1981 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1982 | goto exit; |
| 1983 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1984 | status = psa_validate_optional_attributes(slot, attributes); |
| 1985 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1986 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1987 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1989 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1990 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1991 | if (status != PSA_SUCCESS) { |
| 1992 | psa_fail_key_creation(slot, driver); |
| 1993 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1995 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1996 | } |
| 1997 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1998 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1999 | psa_status_t mbedtls_psa_register_se_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2000 | const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2001 | { |
| 2002 | psa_status_t status; |
| 2003 | psa_key_slot_t *slot = NULL; |
| 2004 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2005 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2006 | |
| 2007 | /* Leaving attributes unspecified is not currently supported. |
| 2008 | * It could make sense to query the key type and size from the |
| 2009 | * secure element, but not all secure elements support this |
| 2010 | * and the driver HAL doesn't currently support it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2011 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 2012 | return PSA_ERROR_NOT_SUPPORTED; |
| 2013 | } |
| 2014 | if (psa_get_key_bits(attributes) == 0) { |
| 2015 | return PSA_ERROR_NOT_SUPPORTED; |
| 2016 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2018 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 2019 | &slot, &driver); |
| 2020 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2021 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2022 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2024 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2025 | |
| 2026 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2027 | if (status != PSA_SUCCESS) { |
| 2028 | psa_fail_key_creation(slot, driver); |
| 2029 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2030 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2031 | /* Registration doesn't keep the key in RAM. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2032 | psa_close_key(key); |
| 2033 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2034 | } |
| 2035 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2037 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2038 | const psa_key_attributes_t *specified_attributes, |
| 2039 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2040 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2041 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2042 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2043 | psa_key_slot_t *source_slot = NULL; |
| 2044 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2045 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2046 | psa_se_drv_table_entry_t *driver = NULL; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2047 | size_t storage_size = 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2048 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2049 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2050 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2051 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2052 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2053 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2054 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2055 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2057 | status = psa_validate_optional_attributes(source_slot, |
| 2058 | specified_attributes); |
| 2059 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2060 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2061 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2062 | |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2063 | /* The target key type and number of bits have been validated by |
| 2064 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2065 | * equal to the ones of the source key. So it is safe to inherit |
| 2066 | * them from the source key now." |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2067 | * */ |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2068 | actual_attributes.core.bits = source_slot->attr.bits; |
| 2069 | actual_attributes.core.type = source_slot->attr.type; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2070 | |
| 2071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2072 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2073 | &actual_attributes.core.policy, |
| 2074 | &source_slot->attr.policy); |
| 2075 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2076 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2077 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2079 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2080 | &target_slot, &driver); |
| 2081 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2082 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2083 | } |
| 2084 | if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) != |
| 2085 | PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2086 | /* |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2087 | * If the source and target keys are stored in different locations, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2088 | * the source key would need to be exported as plaintext and re-imported |
| 2089 | * in the other location. This has security implications which have not |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2090 | * been fully mapped. For now, this can be achieved through |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2091 | * appropriate API invocations from the application, if needed. |
| 2092 | * */ |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2093 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2094 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2095 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2096 | /* |
| 2097 | * When the source and target keys are within the same location, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2098 | * - For transparent keys it is a blind copy without any driver invocation, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2099 | * - For opaque keys this translates to an invocation of the drivers' |
| 2100 | * copy_key entry point through the dispatch layer. |
| 2101 | * */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2102 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
| 2103 | status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes, |
| 2104 | &storage_size); |
| 2105 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2106 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2107 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2109 | status = psa_allocate_buffer_to_slot(target_slot, storage_size); |
| 2110 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2111 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2112 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2114 | status = psa_driver_wrapper_copy_key(&actual_attributes, |
| 2115 | source_slot->key.data, |
| 2116 | source_slot->key.bytes, |
| 2117 | target_slot->key.data, |
| 2118 | target_slot->key.bytes, |
| 2119 | &target_slot->key.bytes); |
| 2120 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2121 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2122 | } |
| 2123 | } else { |
| 2124 | status = psa_copy_key_material_into_slot(target_slot, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2125 | source_slot->key.data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2126 | source_slot->key.bytes); |
| 2127 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2128 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2129 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2130 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2131 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2132 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2133 | if (status != PSA_SUCCESS) { |
| 2134 | psa_fail_key_creation(target_slot, driver); |
| 2135 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2137 | unlock_status = psa_unlock_key_slot(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2139 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2142 | |
| 2143 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2144 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2145 | /* Message digests */ |
| 2146 | /****************************************************************/ |
| 2147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2148 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2149 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2150 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2151 | if (operation->id == 0) { |
| 2152 | return PSA_SUCCESS; |
| 2153 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2155 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2156 | operation->id = 0; |
| 2157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2158 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2159 | } |
| 2160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2161 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2162 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2163 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2164 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2165 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2166 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2167 | if (operation->id != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2168 | status = PSA_ERROR_BAD_STATE; |
| 2169 | goto exit; |
| 2170 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2172 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2173 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2174 | goto exit; |
| 2175 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2176 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2177 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2178 | * directly zeroes the int-sized dummy member of the context union. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2179 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2181 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2182 | |
| 2183 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2184 | if (status != PSA_SUCCESS) { |
| 2185 | psa_hash_abort(operation); |
| 2186 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2187 | |
| 2188 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2189 | } |
| 2190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2191 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 2192 | const uint8_t *input, |
| 2193 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2194 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2195 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2197 | if (operation->id == 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2198 | status = PSA_ERROR_BAD_STATE; |
| 2199 | goto exit; |
| 2200 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2201 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2202 | /* Don't require hash implementations to behave correctly on a |
| 2203 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2204 | if (input_length == 0) { |
| 2205 | return PSA_SUCCESS; |
| 2206 | } |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2208 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2209 | |
| 2210 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | if (status != PSA_SUCCESS) { |
| 2212 | psa_hash_abort(operation); |
| 2213 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2215 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2216 | } |
| 2217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2218 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2219 | uint8_t *hash, |
| 2220 | size_t hash_size, |
| 2221 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2222 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2223 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2224 | if (operation->id == 0) { |
| 2225 | return PSA_ERROR_BAD_STATE; |
| 2226 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2227 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2228 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2229 | operation, hash, hash_size, hash_length); |
| 2230 | psa_hash_abort(operation); |
| 2231 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2232 | } |
| 2233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2234 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 2235 | const uint8_t *hash, |
| 2236 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2237 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2238 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2239 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2240 | psa_status_t status = psa_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2241 | operation, |
| 2242 | actual_hash, sizeof(actual_hash), |
| 2243 | &actual_hash_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2245 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2246 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2247 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2249 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2250 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2251 | goto exit; |
| 2252 | } |
| 2253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2254 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2255 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2256 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2257 | |
| 2258 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2259 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2260 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2261 | psa_hash_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2264 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2265 | } |
| 2266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2267 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 2268 | const uint8_t *input, size_t input_length, |
| 2269 | uint8_t *hash, size_t hash_size, |
| 2270 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2271 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2272 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2273 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2274 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2275 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2277 | return psa_driver_wrapper_hash_compute(alg, input, input_length, |
| 2278 | hash, hash_size, hash_length); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2279 | } |
| 2280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2281 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 2282 | const uint8_t *input, size_t input_length, |
| 2283 | const uint8_t *hash, size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2284 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2285 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2286 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2288 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2289 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2290 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2291 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2292 | psa_status_t status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2293 | alg, input, input_length, |
| 2294 | actual_hash, sizeof(actual_hash), |
| 2295 | &actual_hash_length); |
| 2296 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2297 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2298 | } |
| 2299 | if (actual_hash_length != hash_length) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2300 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2301 | goto exit; |
| 2302 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2303 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2304 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2305 | } |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2306 | |
| 2307 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2308 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2309 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2312 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2313 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2314 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2315 | if (source_operation->id == 0 || |
| 2316 | target_operation->id != 0) { |
| 2317 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2318 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2320 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2321 | target_operation); |
| 2322 | if (status != PSA_SUCCESS) { |
| 2323 | psa_hash_abort(target_operation); |
| 2324 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2327 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2328 | |
| 2329 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2330 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2331 | /* MAC */ |
| 2332 | /****************************************************************/ |
| 2333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2335 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2336 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2337 | if (operation->id == 0) { |
| 2338 | return PSA_SUCCESS; |
| 2339 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2342 | operation->mac_size = 0; |
| 2343 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2344 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2346 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2347 | } |
| 2348 | |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2349 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2350 | psa_algorithm_t alg, |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2351 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2352 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2353 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2354 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2355 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2356 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2358 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2359 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2360 | } |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2361 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2362 | /* Validate the combination of key type and algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2363 | status = psa_mac_key_can_do(alg, key_type); |
| 2364 | if (status != PSA_SUCCESS) { |
| 2365 | return status; |
| 2366 | } |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2367 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2368 | /* Get the output length for the algorithm and key combination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2369 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2371 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2372 | /* A very short MAC is too short for security since it can be |
| 2373 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2374 | * so we make this our minimum, even though 32 bits is still |
| 2375 | * too small for security. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2376 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2377 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2379 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2380 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2381 | /* It's impossible to "truncate" to a larger length than the full length |
| 2382 | * of the algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2383 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2384 | } |
| 2385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2386 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2387 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2388 | * that is disabled in the compile-time configuration. The result can |
| 2389 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2390 | * configuration into account. In this case, force a return of |
| 2391 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2392 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2393 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2394 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2395 | * systematically generated tests. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2396 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2397 | } |
| 2398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2399 | return PSA_SUCCESS; |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2400 | } |
| 2401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2403 | mbedtls_svc_key_id_t key, |
| 2404 | psa_algorithm_t alg, |
| 2405 | int is_sign) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2406 | { |
| 2407 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2408 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2409 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2410 | |
| 2411 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2412 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2413 | status = PSA_ERROR_BAD_STATE; |
| 2414 | goto exit; |
| 2415 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2416 | |
| 2417 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2418 | key, |
| 2419 | &slot, |
| 2420 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2421 | alg); |
| 2422 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2423 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2424 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2425 | |
| 2426 | psa_key_attributes_t attributes = { |
| 2427 | .core = slot->attr |
| 2428 | }; |
| 2429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2430 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2431 | &operation->mac_size); |
| 2432 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2433 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2434 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2435 | |
| 2436 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2437 | /* Dispatch the MAC setup call with validated input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2438 | if (is_sign) { |
| 2439 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2440 | &attributes, |
| 2441 | slot->key.data, |
| 2442 | slot->key.bytes, |
| 2443 | alg); |
| 2444 | } else { |
| 2445 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2446 | &attributes, |
| 2447 | slot->key.data, |
| 2448 | slot->key.bytes, |
| 2449 | alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2450 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2451 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2452 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2453 | if (status != PSA_SUCCESS) { |
| 2454 | psa_mac_abort(operation); |
| 2455 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2457 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2459 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2460 | } |
| 2461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2462 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2463 | mbedtls_svc_key_id_t key, |
| 2464 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2465 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2466 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2467 | } |
| 2468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2470 | mbedtls_svc_key_id_t key, |
| 2471 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2472 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2476 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 2477 | const uint8_t *input, |
| 2478 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2479 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2480 | if (operation->id == 0) { |
| 2481 | return PSA_ERROR_BAD_STATE; |
| 2482 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2483 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2484 | /* Don't require hash implementations to behave correctly on a |
| 2485 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | if (input_length == 0) { |
| 2487 | return PSA_SUCCESS; |
| 2488 | } |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2490 | psa_status_t status = psa_driver_wrapper_mac_update(operation, |
| 2491 | input, input_length); |
| 2492 | if (status != PSA_SUCCESS) { |
| 2493 | psa_mac_abort(operation); |
| 2494 | } |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2496 | return status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2497 | } |
| 2498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2499 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 2500 | uint8_t *mac, |
| 2501 | size_t mac_size, |
| 2502 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2503 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2504 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2505 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2507 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2508 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2509 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2510 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2512 | if (!operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2513 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2514 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2515 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2516 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2517 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2518 | * once all the error checks are done. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2519 | if (operation->mac_size == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2520 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2521 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2522 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2524 | if (mac_size < operation->mac_size) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2525 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2526 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2527 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2529 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2530 | mac, operation->mac_size, |
| 2531 | mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2532 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2533 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2534 | /* In case of success, set the potential excess room in the output buffer |
| 2535 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2536 | * In case of error, set the output length and content to a safe default, |
| 2537 | * such that in case the caller misses an error check, the output would be |
| 2538 | * an unachievable MAC. |
| 2539 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2540 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2541 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2542 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2543 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2544 | |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 2545 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2547 | abort_status = psa_mac_abort(operation); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2549 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2550 | } |
| 2551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2552 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2553 | const uint8_t *mac, |
| 2554 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2555 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2556 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2557 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2559 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2560 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2561 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2562 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2564 | if (operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2565 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2566 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2567 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2569 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2570 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2571 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2572 | } |
| 2573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2574 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2575 | mac, mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2576 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2577 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2578 | abort_status = psa_mac_abort(operation); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2580 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2581 | } |
| 2582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2583 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2584 | psa_algorithm_t alg, |
| 2585 | const uint8_t *input, |
| 2586 | size_t input_length, |
| 2587 | uint8_t *mac, |
| 2588 | size_t mac_size, |
| 2589 | size_t *mac_length, |
| 2590 | int is_sign) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2591 | { |
| 2592 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2593 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2594 | psa_key_slot_t *slot; |
| 2595 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2596 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2597 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2598 | key, |
| 2599 | &slot, |
| 2600 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2601 | alg); |
| 2602 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2603 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2604 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2605 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2606 | psa_key_attributes_t attributes = { |
| 2607 | .core = slot->attr |
| 2608 | }; |
| 2609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2610 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2611 | &operation_mac_size); |
| 2612 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2613 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2614 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2616 | if (mac_size < operation_mac_size) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2617 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2618 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | status = psa_driver_wrapper_mac_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2622 | &attributes, |
| 2623 | slot->key.data, slot->key.bytes, |
| 2624 | alg, |
| 2625 | input, input_length, |
| 2626 | mac, operation_mac_size, mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2627 | |
| 2628 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2629 | /* In case of success, set the potential excess room in the output buffer |
| 2630 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2631 | * In case of error, set the output length and content to a safe default, |
| 2632 | * such that in case the caller misses an error check, the output would be |
| 2633 | * an unachievable MAC. |
| 2634 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | if (status != PSA_SUCCESS) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2636 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2637 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2638 | } |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 2639 | |
| 2640 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2642 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2644 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2645 | } |
| 2646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | 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] | 2648 | psa_algorithm_t alg, |
| 2649 | const uint8_t *input, |
| 2650 | size_t input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2651 | uint8_t *mac, |
| 2652 | size_t mac_size, |
| 2653 | size_t *mac_length) |
| 2654 | { |
| 2655 | return psa_mac_compute_internal(key, alg, |
| 2656 | input, input_length, |
| 2657 | mac, mac_size, mac_length, 1); |
| 2658 | } |
| 2659 | |
| 2660 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2661 | psa_algorithm_t alg, |
| 2662 | const uint8_t *input, |
| 2663 | size_t input_length, |
| 2664 | const uint8_t *mac, |
| 2665 | size_t mac_length) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2666 | { |
| 2667 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2668 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2669 | size_t actual_mac_length; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2671 | status = psa_mac_compute_internal(key, alg, |
| 2672 | input, input_length, |
| 2673 | actual_mac, sizeof(actual_mac), |
| 2674 | &actual_mac_length, 0); |
| 2675 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2676 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2677 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2679 | if (mac_length != actual_mac_length) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2680 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2681 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2682 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2683 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2684 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2685 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2686 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2687 | |
| 2688 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2689 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2691 | return status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2692 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2693 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2694 | /****************************************************************/ |
| 2695 | /* Asymmetric cryptography */ |
| 2696 | /****************************************************************/ |
| 2697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2698 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2699 | psa_algorithm_t alg) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2700 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2701 | if (input_is_message) { |
| 2702 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 2703 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2704 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2706 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2707 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { |
| 2708 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2709 | } |
| 2710 | } |
| 2711 | } else { |
| 2712 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2713 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2714 | } |
| 2715 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2717 | return PSA_SUCCESS; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2718 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2720 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 2721 | int input_is_message, |
| 2722 | psa_algorithm_t alg, |
| 2723 | const uint8_t *input, |
| 2724 | size_t input_length, |
| 2725 | uint8_t *signature, |
| 2726 | size_t signature_size, |
| 2727 | size_t *signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2728 | { |
| 2729 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2730 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2731 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2732 | |
| 2733 | *signature_length = 0; |
| 2734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2735 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2736 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2737 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2738 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2739 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2740 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2741 | * 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] | 2742 | * buffer can in principle be empty since it doesn't actually have |
| 2743 | * to be a hash.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2744 | if (signature_size == 0) { |
| 2745 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 2746 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2747 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2748 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2749 | key, &slot, |
| 2750 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2751 | PSA_KEY_USAGE_SIGN_HASH, |
| 2752 | alg); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2754 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2755 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2756 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2758 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2759 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2760 | goto exit; |
| 2761 | } |
| 2762 | |
| 2763 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2764 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2765 | }; |
| 2766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2767 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2768 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2769 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2770 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2771 | signature, signature_size, signature_length); |
| 2772 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2773 | |
| 2774 | status = psa_driver_wrapper_sign_hash( |
| 2775 | &attributes, slot->key.data, slot->key.bytes, |
| 2776 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2777 | signature, signature_size, signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2778 | } |
| 2779 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2780 | |
| 2781 | exit: |
Paul Elliott | 59200a2 | 2023-02-21 15:07:40 +0000 | [diff] [blame] | 2782 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 2783 | *signature_length); |
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 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2787 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2788 | } |
| 2789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2790 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 2791 | int input_is_message, |
| 2792 | psa_algorithm_t alg, |
| 2793 | const uint8_t *input, |
| 2794 | size_t input_length, |
| 2795 | const uint8_t *signature, |
| 2796 | size_t signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2797 | { |
| 2798 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2799 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2800 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2802 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2803 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2804 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2805 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2806 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2807 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2808 | key, &slot, |
| 2809 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2810 | PSA_KEY_USAGE_VERIFY_HASH, |
| 2811 | alg); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2813 | if (status != PSA_SUCCESS) { |
| 2814 | return status; |
| 2815 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2816 | |
| 2817 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2818 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2819 | }; |
| 2820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2821 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2822 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2823 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2824 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2825 | signature, signature_length); |
| 2826 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2827 | status = psa_driver_wrapper_verify_hash( |
| 2828 | &attributes, slot->key.data, slot->key.bytes, |
| 2829 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2830 | signature, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2831 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2833 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2835 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2836 | |
| 2837 | } |
| 2838 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2839 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2840 | const psa_key_attributes_t *attributes, |
| 2841 | const uint8_t *key_buffer, |
| 2842 | size_t key_buffer_size, |
| 2843 | psa_algorithm_t alg, |
| 2844 | const uint8_t *input, |
| 2845 | size_t input_length, |
| 2846 | uint8_t *signature, |
| 2847 | size_t signature_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2848 | size_t *signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2849 | { |
| 2850 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2852 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2853 | size_t hash_length; |
| 2854 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2855 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2856 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2857 | PSA_ALG_SIGN_GET_HASH(alg), |
| 2858 | input, input_length, |
| 2859 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2861 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2862 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2863 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2864 | |
| 2865 | return psa_driver_wrapper_sign_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2866 | attributes, key_buffer, key_buffer_size, |
| 2867 | alg, hash, hash_length, |
| 2868 | signature, signature_size, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2869 | } |
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 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2872 | } |
| 2873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2874 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 2875 | psa_algorithm_t alg, |
| 2876 | const uint8_t *input, |
| 2877 | size_t input_length, |
| 2878 | uint8_t *signature, |
| 2879 | size_t signature_size, |
| 2880 | size_t *signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2881 | { |
| 2882 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2883 | key, 1, alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2884 | signature, signature_size, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2885 | } |
| 2886 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2887 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2888 | const psa_key_attributes_t *attributes, |
| 2889 | const uint8_t *key_buffer, |
| 2890 | size_t key_buffer_size, |
| 2891 | psa_algorithm_t alg, |
| 2892 | const uint8_t *input, |
| 2893 | size_t input_length, |
| 2894 | const uint8_t *signature, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2895 | size_t signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2896 | { |
| 2897 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2900 | size_t hash_length; |
| 2901 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2902 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2903 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2904 | PSA_ALG_SIGN_GET_HASH(alg), |
| 2905 | input, input_length, |
| 2906 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2908 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2909 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2910 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2911 | |
| 2912 | return psa_driver_wrapper_verify_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | attributes, key_buffer, key_buffer_size, |
| 2914 | alg, hash, hash_length, |
| 2915 | signature, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2916 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2918 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2919 | } |
| 2920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2921 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 2922 | psa_algorithm_t alg, |
| 2923 | const uint8_t *input, |
| 2924 | size_t input_length, |
| 2925 | const uint8_t *signature, |
| 2926 | size_t signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2927 | { |
| 2928 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2929 | key, 1, alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2930 | signature, signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2931 | } |
| 2932 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2933 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2934 | const psa_key_attributes_t *attributes, |
| 2935 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2936 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2937 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2938 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2939 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 2940 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 2941 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2942 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2943 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2944 | return mbedtls_psa_rsa_sign_hash( |
| 2945 | attributes, |
| 2946 | key_buffer, key_buffer_size, |
| 2947 | alg, hash, hash_length, |
| 2948 | signature, signature_size, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2949 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2950 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2951 | } else { |
| 2952 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2953 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2954 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 2955 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2956 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2957 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2958 | return mbedtls_psa_ecdsa_sign_hash( |
| 2959 | attributes, |
| 2960 | key_buffer, key_buffer_size, |
| 2961 | alg, hash, hash_length, |
| 2962 | signature, signature_size, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2963 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2964 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2965 | } else { |
| 2966 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2967 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2968 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | (void) key_buffer; |
| 2971 | (void) key_buffer_size; |
| 2972 | (void) hash; |
| 2973 | (void) hash_length; |
| 2974 | (void) signature; |
| 2975 | (void) signature_size; |
| 2976 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2979 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2981 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 2982 | psa_algorithm_t alg, |
| 2983 | const uint8_t *hash, |
| 2984 | size_t hash_length, |
| 2985 | uint8_t *signature, |
| 2986 | size_t signature_size, |
| 2987 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2988 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2989 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2990 | key, 0, alg, hash, hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2991 | signature, signature_size, signature_length); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2992 | } |
| 2993 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2994 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2995 | const psa_key_attributes_t *attributes, |
| 2996 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2997 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2998 | const uint8_t *signature, size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2999 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 3001 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3002 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3003 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3004 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3005 | return mbedtls_psa_rsa_verify_hash( |
| 3006 | attributes, |
| 3007 | key_buffer, key_buffer_size, |
| 3008 | alg, hash, hash_length, |
| 3009 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3010 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3011 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3012 | } else { |
| 3013 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3014 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3016 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3017 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3018 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3019 | return mbedtls_psa_ecdsa_verify_hash( |
| 3020 | attributes, |
| 3021 | key_buffer, key_buffer_size, |
| 3022 | alg, hash, hash_length, |
| 3023 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3024 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3025 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3026 | } else { |
| 3027 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3028 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3029 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3031 | (void) key_buffer; |
| 3032 | (void) key_buffer_size; |
| 3033 | (void) hash; |
| 3034 | (void) hash_length; |
| 3035 | (void) signature; |
| 3036 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3038 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3039 | } |
| 3040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3041 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3042 | psa_algorithm_t alg, |
| 3043 | const uint8_t *hash, |
| 3044 | size_t hash_length, |
| 3045 | const uint8_t *signature, |
| 3046 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3047 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3048 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3049 | key, 0, alg, hash, hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3050 | signature, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3051 | } |
| 3052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3053 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3054 | psa_algorithm_t alg, |
| 3055 | const uint8_t *input, |
| 3056 | size_t input_length, |
| 3057 | const uint8_t *salt, |
| 3058 | size_t salt_length, |
| 3059 | uint8_t *output, |
| 3060 | size_t output_size, |
| 3061 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3062 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3063 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3064 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3065 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3066 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3067 | (void) input; |
| 3068 | (void) input_length; |
| 3069 | (void) salt; |
| 3070 | (void) output; |
| 3071 | (void) output_size; |
| 3072 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3073 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3075 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3076 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3077 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3078 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3079 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3080 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3081 | if (status != PSA_SUCCESS) { |
| 3082 | return status; |
| 3083 | } |
| 3084 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3085 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3086 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3087 | goto exit; |
| 3088 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3089 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3090 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | .core = slot->attr |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3092 | }; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3093 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3094 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3095 | &attributes, slot->key.data, slot->key.bytes, |
| 3096 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3097 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3098 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3099 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3101 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3102 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3104 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3105 | psa_algorithm_t alg, |
| 3106 | const uint8_t *input, |
| 3107 | size_t input_length, |
| 3108 | const uint8_t *salt, |
| 3109 | size_t salt_length, |
| 3110 | uint8_t *output, |
| 3111 | size_t output_size, |
| 3112 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3113 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3114 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3115 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3116 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3117 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3118 | (void) input; |
| 3119 | (void) input_length; |
| 3120 | (void) salt; |
| 3121 | (void) output; |
| 3122 | (void) output_size; |
| 3123 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3124 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3127 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3128 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3129 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3130 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3131 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3132 | if (status != PSA_SUCCESS) { |
| 3133 | return status; |
| 3134 | } |
| 3135 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3136 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3137 | goto exit; |
| 3138 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3139 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3140 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3141 | .core = slot->attr |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3142 | }; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3143 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3144 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3145 | &attributes, slot->key.data, slot->key.bytes, |
| 3146 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3147 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3148 | |
| 3149 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3150 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3152 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3153 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3154 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3155 | /****************************************************************/ |
| 3156 | /* Asymmetric interruptible cryptography */ |
| 3157 | /****************************************************************/ |
| 3158 | |
| 3159 | void psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3160 | { |
| 3161 | psa_driver_wrapper_interruptible_set_max_ops(max_ops); |
| 3162 | } |
| 3163 | |
| 3164 | uint32_t psa_interruptible_get_max_ops(void) |
| 3165 | { |
| 3166 | return psa_driver_wrapper_interruptible_get_max_ops(); |
| 3167 | } |
| 3168 | |
| 3169 | |
| 3170 | uint32_t psa_sign_hash_get_num_ops( |
| 3171 | const psa_sign_hash_interruptible_operation_t *operation) |
| 3172 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3173 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | uint32_t psa_verify_hash_get_num_ops( |
| 3177 | const psa_verify_hash_interruptible_operation_t *operation) |
| 3178 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3179 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3182 | static psa_status_t psa_sign_hash_abort_internal( |
| 3183 | psa_sign_hash_interruptible_operation_t *operation) |
| 3184 | { |
| 3185 | if (operation->id == 0) { |
| 3186 | /* The object has (apparently) been initialized but it is not (yet) |
| 3187 | * in use. It's ok to call abort on such an object, and there's |
| 3188 | * nothing to do. */ |
| 3189 | return PSA_SUCCESS; |
| 3190 | } |
| 3191 | |
| 3192 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3193 | |
| 3194 | status = psa_driver_wrapper_sign_hash_abort(operation); |
| 3195 | |
| 3196 | operation->id = 0; |
| 3197 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame] | 3198 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3199 | * only want to be cleared by the application calling abort, not by abort |
| 3200 | * being called at completion of an operation. */ |
| 3201 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3202 | return status; |
| 3203 | } |
| 3204 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3205 | psa_status_t psa_sign_hash_start( |
| 3206 | psa_sign_hash_interruptible_operation_t *operation, |
| 3207 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3208 | const uint8_t *hash, size_t hash_length) |
| 3209 | { |
| 3210 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3211 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3212 | psa_key_slot_t *slot; |
| 3213 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3214 | /* Check that start has not been previously called, or operation has not |
| 3215 | * previously errored. */ |
| 3216 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3217 | return PSA_ERROR_BAD_STATE; |
| 3218 | } |
| 3219 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3220 | status = psa_sign_verify_check_alg(0, alg); |
| 3221 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3222 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3223 | return status; |
| 3224 | } |
| 3225 | |
| 3226 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3227 | PSA_KEY_USAGE_SIGN_HASH, |
| 3228 | alg); |
| 3229 | |
| 3230 | if (status != PSA_SUCCESS) { |
| 3231 | goto exit; |
| 3232 | } |
| 3233 | |
| 3234 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
| 3235 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3236 | goto exit; |
| 3237 | } |
| 3238 | |
| 3239 | psa_key_attributes_t attributes = { |
| 3240 | .core = slot->attr |
| 3241 | }; |
| 3242 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3243 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3244 | operation->num_ops = 0; |
| 3245 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3246 | status = psa_driver_wrapper_sign_hash_start(operation, &attributes, |
| 3247 | slot->key.data, |
| 3248 | slot->key.bytes, alg, |
| 3249 | hash, hash_length); |
| 3250 | exit: |
| 3251 | |
| 3252 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3253 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3254 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | unlock_status = psa_unlock_key_slot(slot); |
| 3258 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3259 | if (unlock_status != PSA_SUCCESS) { |
| 3260 | operation->error_occurred = 1; |
| 3261 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3262 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3263 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | |
| 3267 | psa_status_t psa_sign_hash_complete( |
| 3268 | psa_sign_hash_interruptible_operation_t *operation, |
| 3269 | uint8_t *signature, size_t signature_size, |
| 3270 | size_t *signature_length) |
| 3271 | { |
| 3272 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3273 | |
| 3274 | *signature_length = 0; |
| 3275 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3276 | /* Check that start has been called first, and that operation has not |
| 3277 | * previously errored. */ |
| 3278 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3279 | status = PSA_ERROR_BAD_STATE; |
| 3280 | goto exit; |
| 3281 | } |
| 3282 | |
Paul Elliott | 096abc4 | 2023-02-03 18:33:23 +0000 | [diff] [blame] | 3283 | /* Immediately reject a zero-length signature buffer. This guarantees that |
| 3284 | * signature must be a valid pointer. */ |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3285 | if (signature_size == 0) { |
| 3286 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3287 | goto exit; |
| 3288 | } |
| 3289 | |
| 3290 | status = psa_driver_wrapper_sign_hash_complete(operation, signature, |
| 3291 | signature_size, |
| 3292 | signature_length); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3293 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3294 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3295 | operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation); |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3296 | |
Paul Elliott | ebe225c | 2023-02-10 14:32:53 +0000 | [diff] [blame] | 3297 | exit: |
| 3298 | |
Paul Elliott | 59200a2 | 2023-02-21 15:07:40 +0000 | [diff] [blame] | 3299 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 3300 | *signature_length); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3301 | |
Paul Elliott | 53bb312 | 2023-02-10 14:22:22 +0000 | [diff] [blame] | 3302 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3303 | if (status != PSA_SUCCESS) { |
| 3304 | operation->error_occurred = 1; |
| 3305 | } |
| 3306 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3307 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | return status; |
| 3311 | } |
| 3312 | |
| 3313 | psa_status_t psa_sign_hash_abort( |
| 3314 | psa_sign_hash_interruptible_operation_t *operation) |
| 3315 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3316 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3317 | |
| 3318 | status = psa_sign_hash_abort_internal(operation); |
| 3319 | |
| 3320 | /* We clear the number of ops done here, so that it is not cleared when |
| 3321 | * the operation fails or succeeds, only on manual abort. */ |
| 3322 | operation->num_ops = 0; |
| 3323 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3324 | /* Likewise, failure state. */ |
| 3325 | operation->error_occurred = 0; |
| 3326 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3327 | return status; |
| 3328 | } |
| 3329 | |
| 3330 | static psa_status_t psa_verify_hash_abort_internal( |
| 3331 | psa_verify_hash_interruptible_operation_t *operation) |
| 3332 | { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3333 | if (operation->id == 0) { |
| 3334 | /* The object has (apparently) been initialized but it is not (yet) |
| 3335 | * in use. It's ok to call abort on such an object, and there's |
| 3336 | * nothing to do. */ |
| 3337 | return PSA_SUCCESS; |
| 3338 | } |
| 3339 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3340 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3341 | |
| 3342 | status = psa_driver_wrapper_verify_hash_abort(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3343 | |
| 3344 | operation->id = 0; |
| 3345 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame] | 3346 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3347 | * only want to be cleared by the application calling abort, not by abort |
| 3348 | * being called at completion of an operation. */ |
| 3349 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3350 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | psa_status_t psa_verify_hash_start( |
| 3354 | psa_verify_hash_interruptible_operation_t *operation, |
| 3355 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3356 | const uint8_t *hash, size_t hash_length, |
| 3357 | const uint8_t *signature, size_t signature_length) |
| 3358 | { |
| 3359 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3360 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3361 | psa_key_slot_t *slot; |
| 3362 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3363 | /* Check that start has not been previously called, or operation has not |
| 3364 | * previously errored. */ |
| 3365 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3366 | return PSA_ERROR_BAD_STATE; |
| 3367 | } |
| 3368 | |
| 3369 | status = psa_sign_verify_check_alg(0, alg); |
| 3370 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3371 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3372 | return status; |
| 3373 | } |
| 3374 | |
| 3375 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3376 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3377 | alg); |
| 3378 | |
| 3379 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3380 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3381 | return status; |
| 3382 | } |
| 3383 | |
| 3384 | psa_key_attributes_t attributes = { |
| 3385 | .core = slot->attr |
| 3386 | }; |
| 3387 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3388 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3389 | operation->num_ops = 0; |
| 3390 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3391 | status = psa_driver_wrapper_verify_hash_start(operation, &attributes, |
| 3392 | slot->key.data, |
| 3393 | slot->key.bytes, |
| 3394 | alg, hash, hash_length, |
| 3395 | signature, signature_length); |
| 3396 | |
| 3397 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3398 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3399 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | unlock_status = psa_unlock_key_slot(slot); |
| 3403 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3404 | if (unlock_status != PSA_SUCCESS) { |
| 3405 | operation->error_occurred = 1; |
| 3406 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3407 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3408 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | psa_status_t psa_verify_hash_complete( |
| 3412 | psa_verify_hash_interruptible_operation_t *operation) |
| 3413 | { |
| 3414 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3415 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3416 | /* Check that start has been called first, and that operation has not |
| 3417 | * previously errored. */ |
| 3418 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3419 | status = PSA_ERROR_BAD_STATE; |
| 3420 | goto exit; |
| 3421 | } |
| 3422 | |
| 3423 | status = psa_driver_wrapper_verify_hash_complete(operation); |
| 3424 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3425 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3426 | operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops( |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3427 | operation); |
| 3428 | |
Paul Elliott | ebe225c | 2023-02-10 14:32:53 +0000 | [diff] [blame] | 3429 | exit: |
| 3430 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3431 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3432 | if (status != PSA_SUCCESS) { |
| 3433 | operation->error_occurred = 1; |
| 3434 | } |
| 3435 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3436 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | return status; |
| 3440 | } |
| 3441 | |
| 3442 | psa_status_t psa_verify_hash_abort( |
| 3443 | psa_verify_hash_interruptible_operation_t *operation) |
| 3444 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3445 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3446 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3447 | status = psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3448 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3449 | /* We clear the number of ops done here, so that it is not cleared when |
| 3450 | * the operation fails or succeeds, only on manual abort. */ |
| 3451 | operation->num_ops = 0; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3452 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3453 | /* Likewise, failure state. */ |
| 3454 | operation->error_occurred = 0; |
| 3455 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3456 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3457 | } |
| 3458 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3459 | /****************************************************************/ |
| 3460 | /* Asymmetric interruptible cryptography internal */ |
| 3461 | /* implementations */ |
| 3462 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3463 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3464 | static uint32_t mbedtls_psa_interruptible_max_ops = |
| 3465 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 3466 | |
| 3467 | void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3468 | { |
Paul Elliott | 7cc4e81 | 2023-01-10 17:14:11 +0000 | [diff] [blame] | 3469 | mbedtls_psa_interruptible_max_ops = max_ops; |
| 3470 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3471 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3472 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3473 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3474 | |
| 3475 | /* Internal implementation uses zero to indicate infinite number max ops, |
| 3476 | * therefore avoid this value, and set to minimum possible. */ |
| 3477 | if (max_ops == 0) { |
| 3478 | max_ops = 1; |
| 3479 | } |
| 3480 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3481 | mbedtls_ecp_set_max_ops(max_ops); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3482 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3483 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3484 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3485 | } |
| 3486 | |
| 3487 | uint32_t mbedtls_psa_interruptible_get_max_ops(void) |
| 3488 | { |
| 3489 | return mbedtls_psa_interruptible_max_ops; |
| 3490 | } |
| 3491 | |
| 3492 | uint32_t mbedtls_psa_sign_hash_get_num_ops( |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3493 | const mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3494 | { |
| 3495 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3496 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3497 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3498 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3499 | return operation->num_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3500 | #else |
| 3501 | (void) operation; |
| 3502 | return 0; |
| 3503 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3504 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3505 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3506 | } |
| 3507 | |
| 3508 | uint32_t mbedtls_psa_verify_hash_get_num_ops( |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3509 | const mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3510 | { |
| 3511 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3512 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3513 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3514 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3515 | return operation->num_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3516 | #else |
| 3517 | (void) operation; |
| 3518 | return 0; |
| 3519 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3520 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3521 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3522 | } |
| 3523 | |
| 3524 | psa_status_t mbedtls_psa_sign_hash_start( |
| 3525 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3526 | const psa_key_attributes_t *attributes, const uint8_t *key_buffer, |
| 3527 | size_t key_buffer_size, psa_algorithm_t alg, |
| 3528 | const uint8_t *hash, size_t hash_length) |
| 3529 | { |
| 3530 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3531 | size_t required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3532 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3533 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3534 | return PSA_ERROR_NOT_SUPPORTED; |
| 3535 | } |
| 3536 | |
| 3537 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 3538 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3539 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3540 | |
| 3541 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3542 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3543 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3544 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3545 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
| 3546 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3547 | /* Ensure num_ops is zero'ed in case of context re-use. */ |
| 3548 | operation->num_ops = 0; |
| 3549 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3550 | /* Ensure default is set even if |
| 3551 | * mbedtls_psa_interruptible_set_max_ops() has not been called. */ |
| 3552 | mbedtls_psa_interruptible_set_max_ops( |
| 3553 | mbedtls_psa_interruptible_get_max_ops()); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3554 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3555 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 3556 | attributes->core.bits, |
| 3557 | key_buffer, |
| 3558 | key_buffer_size, |
| 3559 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3560 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3561 | if (status != PSA_SUCCESS) { |
| 3562 | return status; |
| 3563 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3564 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3565 | operation->coordinate_bytes = PSA_BITS_TO_BYTES( |
Paul Elliott | c569fc2 | 2023-02-10 13:02:54 +0000 | [diff] [blame] | 3566 | operation->ctx->grp.nbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3567 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3568 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
| 3569 | operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg); |
| 3570 | operation->alg = alg; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3571 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3572 | /* We only need to store the same length of hash as the private key size |
| 3573 | * here, it would be truncated by the internal implementation anyway. */ |
| 3574 | required_hash_length = (hash_length < operation->coordinate_bytes ? |
| 3575 | hash_length : operation->coordinate_bytes); |
| 3576 | |
Paul Elliott | ba70ad4 | 2023-02-15 18:23:53 +0000 | [diff] [blame] | 3577 | if (required_hash_length > sizeof(operation->hash)) { |
| 3578 | /* Shouldn't happen, but better safe than sorry. */ |
| 3579 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 3580 | } |
| 3581 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3582 | memcpy(operation->hash, hash, required_hash_length); |
| 3583 | operation->hash_length = required_hash_length; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3584 | |
| 3585 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3586 | |
| 3587 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3588 | (void) operation; |
| 3589 | (void) key_buffer; |
| 3590 | (void) key_buffer_size; |
| 3591 | (void) alg; |
| 3592 | (void) hash; |
| 3593 | (void) hash_length; |
| 3594 | (void) status; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3595 | (void) required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3596 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3597 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3598 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3599 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3600 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | psa_status_t mbedtls_psa_sign_hash_complete( |
| 3604 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3605 | uint8_t *signature, size_t signature_size, |
| 3606 | size_t *signature_length) |
| 3607 | { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3608 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3609 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3610 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3611 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3612 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 3613 | mbedtls_mpi r; |
| 3614 | mbedtls_mpi s; |
| 3615 | |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3616 | mbedtls_mpi_init(&r); |
| 3617 | mbedtls_mpi_init(&s); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3618 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3619 | if (signature_size < 2 * operation->coordinate_bytes) { |
| 3620 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3621 | goto exit; |
| 3622 | } |
| 3623 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3624 | if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) { |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3625 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3626 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3627 | status = mbedtls_to_psa_error( |
| 3628 | mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3629 | &r, |
| 3630 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3631 | &operation->ctx->d, |
| 3632 | operation->hash, |
| 3633 | operation->hash_length, |
| 3634 | operation->md_alg, |
| 3635 | mbedtls_psa_get_random, |
| 3636 | MBEDTLS_PSA_RANDOM_STATE, |
| 3637 | &operation->restart_ctx)); |
| 3638 | #else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3639 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3640 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3641 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3642 | } else { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3643 | status = mbedtls_to_psa_error( |
| 3644 | mbedtls_ecdsa_sign_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3645 | &r, |
| 3646 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3647 | &operation->ctx->d, |
| 3648 | operation->hash, |
| 3649 | operation->hash_length, |
| 3650 | mbedtls_psa_get_random, |
| 3651 | MBEDTLS_PSA_RANDOM_STATE, |
| 3652 | mbedtls_psa_get_random, |
| 3653 | MBEDTLS_PSA_RANDOM_STATE, |
| 3654 | &operation->restart_ctx)); |
| 3655 | } |
| 3656 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3657 | /* Hide the fact that the restart context only holds a delta of number of |
| 3658 | * ops done during the last operation, not an absolute value. */ |
| 3659 | operation->num_ops += operation->restart_ctx.ecp.ops_done; |
| 3660 | |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3661 | if (status == PSA_SUCCESS) { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3662 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3663 | mbedtls_mpi_write_binary(&r, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3664 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3665 | operation->coordinate_bytes) |
| 3666 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3667 | |
| 3668 | if (status != PSA_SUCCESS) { |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3669 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3673 | mbedtls_mpi_write_binary(&s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3674 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3675 | operation->coordinate_bytes, |
| 3676 | operation->coordinate_bytes) |
| 3677 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3678 | |
| 3679 | if (status != PSA_SUCCESS) { |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3680 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3683 | *signature_length = operation->coordinate_bytes * 2; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3684 | |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3685 | status = PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3686 | } |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3687 | |
| 3688 | exit: |
| 3689 | |
| 3690 | mbedtls_mpi_free(&r); |
| 3691 | mbedtls_mpi_free(&s); |
| 3692 | return status; |
| 3693 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3694 | #else |
| 3695 | |
| 3696 | (void) operation; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3697 | (void) signature; |
| 3698 | (void) signature_size; |
| 3699 | (void) signature_length; |
| 3700 | |
| 3701 | return PSA_ERROR_NOT_SUPPORTED; |
| 3702 | |
| 3703 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3704 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3705 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3706 | } |
| 3707 | |
| 3708 | psa_status_t mbedtls_psa_sign_hash_abort( |
| 3709 | mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
| 3710 | { |
| 3711 | |
| 3712 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3713 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3714 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3715 | |
| 3716 | if (operation->ctx) { |
| 3717 | mbedtls_ecdsa_free(operation->ctx); |
| 3718 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3719 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 3723 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3724 | operation->num_ops = 0; |
| 3725 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3726 | return PSA_SUCCESS; |
| 3727 | |
| 3728 | #else |
| 3729 | |
| 3730 | (void) operation; |
| 3731 | |
| 3732 | return PSA_ERROR_NOT_SUPPORTED; |
| 3733 | |
| 3734 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3735 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3736 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3737 | } |
| 3738 | |
| 3739 | psa_status_t mbedtls_psa_verify_hash_start( |
| 3740 | mbedtls_psa_verify_hash_interruptible_operation_t *operation, |
| 3741 | const psa_key_attributes_t *attributes, |
| 3742 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3743 | psa_algorithm_t alg, |
| 3744 | const uint8_t *hash, size_t hash_length, |
| 3745 | const uint8_t *signature, size_t signature_length) |
| 3746 | { |
| 3747 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3748 | size_t coordinate_bytes = 0; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3749 | size_t required_hash_length = 0; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3750 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3751 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3752 | return PSA_ERROR_NOT_SUPPORTED; |
| 3753 | } |
| 3754 | |
| 3755 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 3756 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3757 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3758 | |
| 3759 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3760 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3761 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3762 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3763 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
| 3764 | mbedtls_mpi_init(&operation->r); |
| 3765 | mbedtls_mpi_init(&operation->s); |
| 3766 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3767 | /* Ensure num_ops is zero'ed in case of context re-use. */ |
| 3768 | operation->num_ops = 0; |
| 3769 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3770 | /* Ensure default is set even if |
| 3771 | * mbedtls_psa_interruptible_set_max_ops() has not been called. */ |
| 3772 | mbedtls_psa_interruptible_set_max_ops( |
| 3773 | mbedtls_psa_interruptible_get_max_ops()); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3774 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3775 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 3776 | attributes->core.bits, |
| 3777 | key_buffer, |
| 3778 | key_buffer_size, |
| 3779 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3780 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3781 | if (status != PSA_SUCCESS) { |
| 3782 | return status; |
| 3783 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3784 | |
Paul Elliott | c569fc2 | 2023-02-10 13:02:54 +0000 | [diff] [blame] | 3785 | coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3786 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3787 | if (signature_length != 2 * coordinate_bytes) { |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3788 | return PSA_ERROR_INVALID_SIGNATURE; |
| 3789 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3790 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3791 | status = mbedtls_to_psa_error( |
| 3792 | mbedtls_mpi_read_binary(&operation->r, |
| 3793 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3794 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3795 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3796 | if (status != PSA_SUCCESS) { |
| 3797 | return status; |
| 3798 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3799 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3800 | status = mbedtls_to_psa_error( |
| 3801 | mbedtls_mpi_read_binary(&operation->s, |
| 3802 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3803 | coordinate_bytes, |
| 3804 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3805 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3806 | if (status != PSA_SUCCESS) { |
| 3807 | return status; |
| 3808 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3809 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 3810 | status = mbedtls_psa_ecp_load_public_part(operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3811 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 3812 | if (status != PSA_SUCCESS) { |
| 3813 | return status; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3814 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3815 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3816 | /* We only need to store the same length of hash as the private key size |
| 3817 | * here, it would be truncated by the internal implementation anyway. */ |
| 3818 | required_hash_length = (hash_length < coordinate_bytes ? hash_length : |
| 3819 | coordinate_bytes); |
| 3820 | |
Paul Elliott | ba70ad4 | 2023-02-15 18:23:53 +0000 | [diff] [blame] | 3821 | if (required_hash_length > sizeof(operation->hash)) { |
| 3822 | /* Shouldn't happen, but better safe than sorry. */ |
| 3823 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 3824 | } |
| 3825 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3826 | memcpy(operation->hash, hash, required_hash_length); |
| 3827 | operation->hash_length = required_hash_length; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3828 | |
| 3829 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3830 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3831 | (void) operation; |
| 3832 | (void) key_buffer; |
| 3833 | (void) key_buffer_size; |
| 3834 | (void) alg; |
| 3835 | (void) hash; |
| 3836 | (void) hash_length; |
| 3837 | (void) signature; |
| 3838 | (void) signature_length; |
| 3839 | (void) status; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 3840 | (void) coordinate_bytes; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3841 | (void) required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3842 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3843 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3844 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3845 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3846 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | psa_status_t mbedtls_psa_verify_hash_complete( |
| 3850 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 3851 | { |
| 3852 | |
| 3853 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3854 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3855 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3856 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3857 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3858 | |
| 3859 | status = mbedtls_to_psa_error( |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3860 | mbedtls_ecdsa_verify_restartable(&operation->ctx->grp, |
| 3861 | operation->hash, |
| 3862 | operation->hash_length, |
| 3863 | &operation->ctx->Q, |
| 3864 | &operation->r, |
| 3865 | &operation->s, |
| 3866 | &operation->restart_ctx)); |
| 3867 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3868 | /* Hide the fact that the restart context only holds a delta of number of |
| 3869 | * ops done during the last operation, not an absolute value. */ |
| 3870 | operation->num_ops += operation->restart_ctx.ecp.ops_done; |
| 3871 | |
| 3872 | return status; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3873 | #else |
| 3874 | (void) operation; |
| 3875 | |
| 3876 | return PSA_ERROR_NOT_SUPPORTED; |
| 3877 | |
| 3878 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3879 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3880 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3881 | } |
| 3882 | |
| 3883 | psa_status_t mbedtls_psa_verify_hash_abort( |
| 3884 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 3885 | { |
| 3886 | |
| 3887 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3888 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3889 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3890 | |
| 3891 | if (operation->ctx) { |
| 3892 | mbedtls_ecdsa_free(operation->ctx); |
| 3893 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3894 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 3898 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3899 | operation->num_ops = 0; |
| 3900 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3901 | mbedtls_mpi_free(&operation->r); |
| 3902 | mbedtls_mpi_free(&operation->s); |
| 3903 | |
| 3904 | return PSA_SUCCESS; |
| 3905 | |
| 3906 | #else |
| 3907 | (void) operation; |
| 3908 | |
| 3909 | return PSA_ERROR_NOT_SUPPORTED; |
| 3910 | |
| 3911 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3912 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3913 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3914 | } |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3915 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3916 | /****************************************************************/ |
| 3917 | /* Symmetric cryptography */ |
| 3918 | /****************************************************************/ |
| 3919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3920 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 3921 | mbedtls_svc_key_id_t key, |
| 3922 | psa_algorithm_t alg, |
| 3923 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3924 | { |
| 3925 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3926 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3927 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3928 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 3929 | PSA_KEY_USAGE_ENCRYPT : |
| 3930 | PSA_KEY_USAGE_DECRYPT); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3931 | |
| 3932 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3933 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3934 | status = PSA_ERROR_BAD_STATE; |
| 3935 | goto exit; |
| 3936 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3938 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3939 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3940 | goto exit; |
| 3941 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3943 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 3944 | if (status != PSA_SUCCESS) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3945 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3946 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3947 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3948 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3949 | * 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] | 3950 | * so we only set it (in the driver wrapper) after resources have been |
| 3951 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3952 | operation->iv_set = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3953 | if (alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3954 | operation->iv_required = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3955 | } else { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3956 | operation->iv_required = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3957 | } |
| 3958 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3959 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3960 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3961 | .core = slot->attr |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3962 | }; |
| 3963 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3964 | /* Try doing the operation through a driver before using software fallback. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3965 | if (cipher_operation == MBEDTLS_ENCRYPT) { |
| 3966 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, |
| 3967 | &attributes, |
| 3968 | slot->key.data, |
| 3969 | slot->key.bytes, |
| 3970 | alg); |
| 3971 | } else { |
| 3972 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, |
| 3973 | &attributes, |
| 3974 | slot->key.data, |
| 3975 | slot->key.bytes, |
| 3976 | alg); |
| 3977 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3978 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3979 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3980 | if (status != PSA_SUCCESS) { |
| 3981 | psa_cipher_abort(operation); |
| 3982 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3984 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3986 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3987 | } |
| 3988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3989 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 3990 | mbedtls_svc_key_id_t key, |
| 3991 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3992 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3993 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3994 | } |
| 3995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3996 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 3997 | mbedtls_svc_key_id_t key, |
| 3998 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3999 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4000 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4001 | } |
| 4002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4003 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 4004 | uint8_t *iv, |
| 4005 | size_t iv_size, |
| 4006 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4007 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4008 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4009 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 4010 | size_t default_iv_length; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4012 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4013 | status = PSA_ERROR_BAD_STATE; |
| 4014 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4015 | } |
| 4016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4017 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4018 | status = PSA_ERROR_BAD_STATE; |
| 4019 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4020 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4021 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4022 | default_iv_length = operation->default_iv_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4023 | if (iv_size < default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4024 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4025 | goto exit; |
| 4026 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4028 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4029 | status = PSA_ERROR_GENERIC_ERROR; |
| 4030 | goto exit; |
| 4031 | } |
| 4032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4033 | status = psa_generate_random(local_iv, default_iv_length); |
| 4034 | if (status != PSA_SUCCESS) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4035 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4036 | } |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4038 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4039 | local_iv, default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4040 | |
| 4041 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4042 | if (status == PSA_SUCCESS) { |
| 4043 | memcpy(iv, local_iv, default_iv_length); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4044 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4045 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4046 | } else { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4047 | *iv_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4048 | psa_cipher_abort(operation); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4049 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4051 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4052 | } |
| 4053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4054 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 4055 | const uint8_t *iv, |
| 4056 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4057 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4058 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4060 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4061 | status = PSA_ERROR_BAD_STATE; |
| 4062 | goto exit; |
| 4063 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4065 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4066 | status = PSA_ERROR_BAD_STATE; |
| 4067 | goto exit; |
| 4068 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 4069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4070 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4071 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4072 | goto exit; |
| 4073 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4075 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4076 | iv, |
| 4077 | iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4078 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4079 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4080 | if (status == PSA_SUCCESS) { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4081 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4082 | } else { |
| 4083 | psa_cipher_abort(operation); |
| 4084 | } |
| 4085 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4086 | } |
| 4087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4088 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 4089 | const uint8_t *input, |
| 4090 | size_t input_length, |
| 4091 | uint8_t *output, |
| 4092 | size_t output_size, |
| 4093 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4094 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4095 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4097 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4098 | status = PSA_ERROR_BAD_STATE; |
| 4099 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4100 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4102 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4103 | status = PSA_ERROR_BAD_STATE; |
| 4104 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4105 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4107 | status = psa_driver_wrapper_cipher_update(operation, |
| 4108 | input, |
| 4109 | input_length, |
| 4110 | output, |
| 4111 | output_size, |
| 4112 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4113 | |
| 4114 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4115 | if (status != PSA_SUCCESS) { |
| 4116 | psa_cipher_abort(operation); |
| 4117 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4119 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4120 | } |
| 4121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4122 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
| 4123 | uint8_t *output, |
| 4124 | size_t output_size, |
| 4125 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4126 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4127 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4129 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4130 | status = PSA_ERROR_BAD_STATE; |
| 4131 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4132 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4134 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4135 | status = PSA_ERROR_BAD_STATE; |
| 4136 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4137 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4139 | status = psa_driver_wrapper_cipher_finish(operation, |
| 4140 | output, |
| 4141 | output_size, |
| 4142 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4143 | |
| 4144 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4145 | if (status == PSA_SUCCESS) { |
| 4146 | return psa_cipher_abort(operation); |
| 4147 | } else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4148 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4149 | (void) psa_cipher_abort(operation); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4151 | return status; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4152 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4153 | } |
| 4154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4155 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4156 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4157 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4158 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4159 | * in use. It's ok to call abort on such an object, and there's |
| 4160 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4161 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4162 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4164 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4165 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 4166 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4167 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4168 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4171 | } |
| 4172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4173 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 4174 | psa_algorithm_t alg, |
| 4175 | const uint8_t *input, |
| 4176 | size_t input_length, |
| 4177 | uint8_t *output, |
| 4178 | size_t output_size, |
| 4179 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4180 | { |
| 4181 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4182 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4183 | psa_key_slot_t *slot = NULL; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4184 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 4185 | size_t default_iv_length = 0; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4187 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4188 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4189 | goto exit; |
| 4190 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4192 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4193 | PSA_KEY_USAGE_ENCRYPT, |
| 4194 | alg); |
| 4195 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4196 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4197 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4198 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4199 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4200 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4201 | }; |
| 4202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4203 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
| 4204 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4205 | status = PSA_ERROR_GENERIC_ERROR; |
| 4206 | goto exit; |
| 4207 | } |
| 4208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4209 | if (default_iv_length > 0) { |
| 4210 | if (output_size < default_iv_length) { |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4211 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4212 | goto exit; |
| 4213 | } |
| 4214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4215 | status = psa_generate_random(local_iv, default_iv_length); |
| 4216 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4217 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4218 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4219 | } |
| 4220 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4221 | status = psa_driver_wrapper_cipher_encrypt( |
| 4222 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4223 | alg, local_iv, default_iv_length, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4224 | mbedtls_buffer_offset(output, default_iv_length), |
| 4225 | output_size - default_iv_length, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4226 | |
| 4227 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4228 | unlock_status = psa_unlock_key_slot(slot); |
| 4229 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4230 | status = unlock_status; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4231 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4233 | if (status == PSA_SUCCESS) { |
| 4234 | if (default_iv_length > 0) { |
| 4235 | memcpy(output, local_iv, default_iv_length); |
| 4236 | } |
| 4237 | *output_length += default_iv_length; |
| 4238 | } else { |
| 4239 | *output_length = 0; |
| 4240 | } |
| 4241 | |
| 4242 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4243 | } |
| 4244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4245 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 4246 | psa_algorithm_t alg, |
| 4247 | const uint8_t *input, |
| 4248 | size_t input_length, |
| 4249 | uint8_t *output, |
| 4250 | size_t output_size, |
| 4251 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4252 | { |
| 4253 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4254 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4255 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4257 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4258 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4259 | goto exit; |
| 4260 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4262 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4263 | PSA_KEY_USAGE_DECRYPT, |
| 4264 | alg); |
| 4265 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4266 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4267 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4268 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4269 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4270 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4271 | }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4273 | if (alg == PSA_ALG_CCM_STAR_NO_TAG && |
| 4274 | input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 4275 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4276 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4277 | } 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] | 4278 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4279 | goto exit; |
| 4280 | } |
| 4281 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4282 | status = psa_driver_wrapper_cipher_decrypt( |
| 4283 | &attributes, slot->key.data, slot->key.bytes, |
| 4284 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4285 | output, output_size, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4286 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 4287 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4288 | unlock_status = psa_unlock_key_slot(slot); |
| 4289 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4290 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4291 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4293 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4294 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4295 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4297 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4301 | /****************************************************************/ |
| 4302 | /* AEAD */ |
| 4303 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4304 | |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4305 | /* Helper function to get the base algorithm from its variants. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4306 | 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] | 4307 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4308 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4309 | } |
| 4310 | |
| 4311 | /* Helper function to perform common nonce length checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4312 | static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg, |
| 4313 | size_t nonce_length) |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4314 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4315 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4317 | switch (base_alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4318 | #if defined(PSA_WANT_ALG_GCM) |
| 4319 | case PSA_ALG_GCM: |
| 4320 | /* Not checking max nonce size here as GCM spec allows almost |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4321 | * arbitrarily large nonces. Please note that we do not generally |
| 4322 | * recommend the usage of nonces of greater length than |
| 4323 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter |
| 4324 | * size, which can then lead to collisions if you encrypt a very |
| 4325 | * large number of messages.*/ |
| 4326 | if (nonce_length != 0) { |
| 4327 | return PSA_SUCCESS; |
| 4328 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4329 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4330 | #endif /* PSA_WANT_ALG_GCM */ |
| 4331 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4332 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4333 | if (nonce_length >= 7 && nonce_length <= 13) { |
| 4334 | return PSA_SUCCESS; |
| 4335 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4336 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4337 | #endif /* PSA_WANT_ALG_CCM */ |
| 4338 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4339 | case PSA_ALG_CHACHA20_POLY1305: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4340 | if (nonce_length == 12) { |
| 4341 | return PSA_SUCCESS; |
| 4342 | } else if (nonce_length == 8) { |
| 4343 | return PSA_ERROR_NOT_SUPPORTED; |
| 4344 | } |
Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 4345 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4346 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4347 | default: |
Przemek Stekiel | 4c49927 | 2022-09-27 13:55:37 +0200 | [diff] [blame] | 4348 | (void) nonce_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4349 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4350 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4352 | return PSA_ERROR_INVALID_ARGUMENT; |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4353 | } |
| 4354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4355 | 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] | 4356 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4357 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 4358 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4359 | } |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4361 | return PSA_SUCCESS; |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4362 | } |
| 4363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4364 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 4365 | psa_algorithm_t alg, |
| 4366 | const uint8_t *nonce, |
| 4367 | size_t nonce_length, |
| 4368 | const uint8_t *additional_data, |
| 4369 | size_t additional_data_length, |
| 4370 | const uint8_t *plaintext, |
| 4371 | size_t plaintext_length, |
| 4372 | uint8_t *ciphertext, |
| 4373 | size_t ciphertext_size, |
| 4374 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4375 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4376 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4377 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4378 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4379 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4381 | status = psa_aead_check_algorithm(alg); |
| 4382 | if (status != PSA_SUCCESS) { |
| 4383 | return status; |
| 4384 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4385 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4386 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4387 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 4388 | if (status != PSA_SUCCESS) { |
| 4389 | return status; |
| 4390 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4391 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4392 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4393 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4394 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4396 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4397 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4398 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4399 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4400 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4401 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4402 | &attributes, slot->key.data, slot->key.bytes, |
| 4403 | alg, |
| 4404 | nonce, nonce_length, |
| 4405 | additional_data, additional_data_length, |
| 4406 | plaintext, plaintext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4407 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4409 | if (status != PSA_SUCCESS && ciphertext_size != 0) { |
| 4410 | memset(ciphertext, 0, ciphertext_size); |
| 4411 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4412 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4413 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4414 | psa_unlock_key_slot(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4416 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4417 | } |
| 4418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4419 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 4420 | psa_algorithm_t alg, |
| 4421 | const uint8_t *nonce, |
| 4422 | size_t nonce_length, |
| 4423 | const uint8_t *additional_data, |
| 4424 | size_t additional_data_length, |
| 4425 | const uint8_t *ciphertext, |
| 4426 | size_t ciphertext_length, |
| 4427 | uint8_t *plaintext, |
| 4428 | size_t plaintext_size, |
| 4429 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4430 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4431 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4432 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4433 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4434 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4436 | status = psa_aead_check_algorithm(alg); |
| 4437 | if (status != PSA_SUCCESS) { |
| 4438 | return status; |
| 4439 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4440 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4441 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4442 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 4443 | if (status != PSA_SUCCESS) { |
| 4444 | return status; |
| 4445 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4446 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4447 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4448 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4449 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4451 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4452 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4453 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4454 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4455 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4456 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4457 | &attributes, slot->key.data, slot->key.bytes, |
| 4458 | alg, |
| 4459 | nonce, nonce_length, |
| 4460 | additional_data, additional_data_length, |
| 4461 | ciphertext, ciphertext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4462 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4464 | if (status != PSA_SUCCESS && plaintext_size != 0) { |
| 4465 | memset(plaintext, 0, plaintext_size); |
| 4466 | } |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4467 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4468 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4469 | psa_unlock_key_slot(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4471 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4472 | } |
| 4473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4474 | static psa_status_t psa_validate_tag_length(psa_algorithm_t alg) |
| 4475 | { |
| 4476 | const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4478 | switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) { |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4479 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4480 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4481 | /* 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] | 4482 | if (tag_len < 4 || tag_len > 16 || tag_len % 2) { |
| 4483 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4484 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4485 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4486 | #endif /* PSA_WANT_ALG_CCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4487 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4488 | #if defined(PSA_WANT_ALG_GCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4489 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4490 | /* 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] | 4491 | if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) { |
| 4492 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4493 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4494 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4495 | #endif /* PSA_WANT_ALG_GCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4496 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4497 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4498 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4499 | /* We only support the default tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4500 | if (tag_len != 16) { |
| 4501 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4502 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4503 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4504 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4505 | |
| 4506 | default: |
| 4507 | (void) tag_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4508 | return PSA_ERROR_NOT_SUPPORTED; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4509 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4510 | return PSA_SUCCESS; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4511 | } |
| 4512 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4513 | /* Set the key for a multipart authenticated operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4514 | static psa_status_t psa_aead_setup(psa_aead_operation_t *operation, |
| 4515 | int is_encrypt, |
| 4516 | mbedtls_svc_key_id_t key, |
| 4517 | psa_algorithm_t alg) |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4518 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4519 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4520 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4521 | psa_key_slot_t *slot = NULL; |
| 4522 | psa_key_usage_t key_usage = 0; |
| 4523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4524 | status = psa_aead_check_algorithm(alg); |
| 4525 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4526 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4527 | } |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4529 | if (operation->id != 0) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4530 | status = PSA_ERROR_BAD_STATE; |
| 4531 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4532 | } |
| 4533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4534 | if (operation->nonce_set || operation->lengths_set || |
| 4535 | operation->ad_started || operation->body_started) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4536 | status = PSA_ERROR_BAD_STATE; |
| 4537 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4538 | } |
| 4539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4540 | if (is_encrypt) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4541 | key_usage = PSA_KEY_USAGE_ENCRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4542 | } else { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4543 | key_usage = PSA_KEY_USAGE_DECRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4544 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4546 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage, |
| 4547 | alg); |
| 4548 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4549 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4550 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4551 | |
| 4552 | psa_key_attributes_t attributes = { |
| 4553 | .core = slot->attr |
| 4554 | }; |
| 4555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4556 | if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) { |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 4557 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4558 | } |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 4559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4560 | if (is_encrypt) { |
| 4561 | status = psa_driver_wrapper_aead_encrypt_setup(operation, |
| 4562 | &attributes, |
| 4563 | slot->key.data, |
| 4564 | slot->key.bytes, |
| 4565 | alg); |
| 4566 | } else { |
| 4567 | status = psa_driver_wrapper_aead_decrypt_setup(operation, |
| 4568 | &attributes, |
| 4569 | slot->key.data, |
| 4570 | slot->key.bytes, |
| 4571 | alg); |
| 4572 | } |
| 4573 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4574 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4575 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4577 | operation->key_type = psa_get_key_type(&attributes); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4578 | |
| 4579 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4580 | unlock_status = psa_unlock_key_slot(slot); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4582 | if (status == PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4583 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4584 | operation->alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 4585 | operation->is_encrypt = is_encrypt; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4586 | } else { |
| 4587 | psa_aead_abort(operation); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4588 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4590 | return status; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4591 | } |
| 4592 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4593 | /* Set the key for a multipart authenticated encryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4594 | psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation, |
| 4595 | mbedtls_svc_key_id_t key, |
| 4596 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4597 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4598 | return psa_aead_setup(operation, 1, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | /* Set the key for a multipart authenticated decryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4602 | psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation, |
| 4603 | mbedtls_svc_key_id_t key, |
| 4604 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4605 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4606 | return psa_aead_setup(operation, 0, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | /* Generate a random nonce / IV for multipart AEAD operation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4610 | psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation, |
| 4611 | uint8_t *nonce, |
| 4612 | size_t nonce_size, |
| 4613 | size_t *nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4614 | { |
| 4615 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 4616 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; |
Paul Elliott | b91da71 | 2021-05-20 14:43:47 +0100 | [diff] [blame] | 4617 | size_t required_nonce_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4618 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4619 | *nonce_length = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4621 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4622 | status = PSA_ERROR_BAD_STATE; |
| 4623 | goto exit; |
| 4624 | } |
| 4625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4626 | if (operation->nonce_set || !operation->is_encrypt) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4627 | status = PSA_ERROR_BAD_STATE; |
| 4628 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4629 | } |
| 4630 | |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4631 | /* For CCM, this size may not be correct according to the PSA |
| 4632 | * specification. The PSA Crypto 1.0.1 specification states: |
| 4633 | * |
| 4634 | * CCM encodes the plaintext length pLen in L octets, with L the smallest |
| 4635 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. |
| 4636 | * |
| 4637 | * However this restriction that L has to be the smallest integer is not |
| 4638 | * applied in practice, and it is not implementable here since the |
| 4639 | * plaintext length may or may not be known at this time. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type, |
| 4641 | operation->alg); |
| 4642 | if (nonce_size < required_nonce_size) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4643 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4644 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4645 | } |
| 4646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4647 | status = psa_generate_random(local_nonce, required_nonce_size); |
| 4648 | if (status != PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4649 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4650 | } |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4652 | status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4653 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4654 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4655 | if (status == PSA_SUCCESS) { |
| 4656 | memcpy(nonce, local_nonce, required_nonce_size); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4657 | *nonce_length = required_nonce_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4658 | } else { |
| 4659 | psa_aead_abort(operation); |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 4660 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4662 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | /* Set the nonce for a multipart authenticated encryption or decryption |
| 4666 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4667 | psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation, |
| 4668 | const uint8_t *nonce, |
| 4669 | size_t nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4670 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4671 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4673 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4674 | status = PSA_ERROR_BAD_STATE; |
| 4675 | goto exit; |
| 4676 | } |
| 4677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4678 | if (operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4679 | status = PSA_ERROR_BAD_STATE; |
| 4680 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4681 | } |
| 4682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4683 | status = psa_aead_check_nonce_length(operation->alg, nonce_length); |
| 4684 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4685 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4686 | goto exit; |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 4687 | } |
Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 4688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4689 | status = psa_driver_wrapper_aead_set_nonce(operation, nonce, |
| 4690 | nonce_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4691 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4692 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4693 | if (status == PSA_SUCCESS) { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4694 | operation->nonce_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4695 | } else { |
| 4696 | psa_aead_abort(operation); |
| 4697 | } |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4699 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | /* Declare the lengths of the message and additional data for multipart AEAD. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4703 | psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation, |
| 4704 | size_t ad_length, |
| 4705 | size_t plaintext_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4706 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4707 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4709 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4710 | status = PSA_ERROR_BAD_STATE; |
| 4711 | goto exit; |
| 4712 | } |
| 4713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | if (operation->lengths_set || operation->ad_started || |
| 4715 | operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4716 | status = PSA_ERROR_BAD_STATE; |
| 4717 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4718 | } |
| 4719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4720 | switch (operation->alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4721 | #if defined(PSA_WANT_ALG_GCM) |
| 4722 | case PSA_ALG_GCM: |
| 4723 | /* 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] | 4724 | * bits. Without the guard this code will generate warnings on 32bit |
| 4725 | * builds. */ |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 4726 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4727 | if (((uint64_t) ad_length) >> 61 != 0 || |
| 4728 | ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4729 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4730 | goto exit; |
| 4731 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 4732 | #endif |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4733 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4734 | #endif /* PSA_WANT_ALG_GCM */ |
| 4735 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4736 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4737 | if (ad_length > 0xFF00) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4738 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4739 | goto exit; |
| 4740 | } |
| 4741 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4742 | #endif /* PSA_WANT_ALG_CCM */ |
| 4743 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4744 | case PSA_ALG_CHACHA20_POLY1305: |
| 4745 | /* No length restrictions for ChaChaPoly. */ |
| 4746 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4747 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4748 | default: |
| 4749 | break; |
| 4750 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 4751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4752 | status = psa_driver_wrapper_aead_set_lengths(operation, ad_length, |
| 4753 | plaintext_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4754 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4755 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4756 | if (status == PSA_SUCCESS) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4757 | operation->ad_remaining = ad_length; |
| 4758 | operation->body_remaining = plaintext_length; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4759 | operation->lengths_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4760 | } else { |
| 4761 | psa_aead_abort(operation); |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4762 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4764 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4765 | } |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 4766 | |
| 4767 | /* Pass additional data to an active multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation, |
| 4769 | const uint8_t *input, |
| 4770 | size_t input_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4771 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4772 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4774 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4775 | status = PSA_ERROR_BAD_STATE; |
| 4776 | goto exit; |
| 4777 | } |
| 4778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4779 | if (!operation->nonce_set || operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4780 | status = PSA_ERROR_BAD_STATE; |
| 4781 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4782 | } |
| 4783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4784 | if (operation->lengths_set) { |
| 4785 | if (operation->ad_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4786 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4787 | goto exit; |
| 4788 | } |
| 4789 | |
| 4790 | operation->ad_remaining -= input_length; |
| 4791 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4792 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4793 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4794 | status = PSA_ERROR_BAD_STATE; |
| 4795 | goto exit; |
| 4796 | } |
| 4797 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4799 | status = psa_driver_wrapper_aead_update_ad(operation, input, |
| 4800 | input_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4801 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4802 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4803 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4804 | operation->ad_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4805 | } else { |
| 4806 | psa_aead_abort(operation); |
| 4807 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 4813 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | psa_status_t psa_aead_update(psa_aead_operation_t *operation, |
| 4815 | const uint8_t *input, |
| 4816 | size_t input_length, |
| 4817 | uint8_t *output, |
| 4818 | size_t output_size, |
| 4819 | size_t *output_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4820 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4821 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4822 | |
| 4823 | *output_length = 0; |
| 4824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4825 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4826 | status = PSA_ERROR_BAD_STATE; |
| 4827 | goto exit; |
| 4828 | } |
| 4829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4830 | if (!operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4831 | status = PSA_ERROR_BAD_STATE; |
| 4832 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4833 | } |
| 4834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4835 | if (operation->lengths_set) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4836 | /* Additional data length was supplied, but not all the additional |
| 4837 | data was supplied.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4838 | if (operation->ad_remaining != 0) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4839 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4840 | goto exit; |
| 4841 | } |
| 4842 | |
| 4843 | /* Too much data provided. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4844 | if (operation->body_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4845 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4846 | goto exit; |
| 4847 | } |
| 4848 | |
| 4849 | operation->body_remaining -= input_length; |
| 4850 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4851 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4852 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4853 | status = PSA_ERROR_BAD_STATE; |
| 4854 | goto exit; |
| 4855 | } |
| 4856 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4858 | status = psa_driver_wrapper_aead_update(operation, input, input_length, |
| 4859 | output, output_size, |
| 4860 | output_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4861 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4862 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4863 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4864 | operation->body_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4865 | } else { |
| 4866 | psa_aead_abort(operation); |
| 4867 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4869 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4870 | } |
| 4871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4872 | 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] | 4873 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | if (operation->id == 0 || !operation->nonce_set) { |
| 4875 | return PSA_ERROR_BAD_STATE; |
| 4876 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4878 | if (operation->lengths_set && (operation->ad_remaining != 0 || |
| 4879 | operation->body_remaining != 0)) { |
| 4880 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4881 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4883 | return PSA_SUCCESS; |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4884 | } |
| 4885 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4886 | /* Finish encrypting a message in a multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4887 | psa_status_t psa_aead_finish(psa_aead_operation_t *operation, |
| 4888 | uint8_t *ciphertext, |
| 4889 | size_t ciphertext_size, |
| 4890 | size_t *ciphertext_length, |
| 4891 | uint8_t *tag, |
| 4892 | size_t tag_size, |
| 4893 | size_t *tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4894 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4895 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4896 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4897 | *ciphertext_length = 0; |
Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 4898 | *tag_length = tag_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4900 | status = psa_aead_final_checks(operation); |
| 4901 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4902 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4903 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4905 | if (!operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4906 | status = PSA_ERROR_BAD_STATE; |
| 4907 | goto exit; |
| 4908 | } |
| 4909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4910 | status = psa_driver_wrapper_aead_finish(operation, ciphertext, |
| 4911 | ciphertext_size, |
| 4912 | ciphertext_length, |
| 4913 | tag, tag_size, tag_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4914 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4915 | exit: |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 4916 | |
| 4917 | |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4918 | /* 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] | 4919 | * the zero tag size, make sure the tag is set to something implausible. |
| 4920 | * Even if the operation succeeds, make sure we clear the rest of the |
| 4921 | * buffer to prevent potential leakage of anything previously placed in |
| 4922 | * the same buffer.*/ |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame] | 4923 | psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length); |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4925 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4927 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4928 | } |
| 4929 | |
| 4930 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 4931 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4932 | psa_status_t psa_aead_verify(psa_aead_operation_t *operation, |
| 4933 | uint8_t *plaintext, |
| 4934 | size_t plaintext_size, |
| 4935 | size_t *plaintext_length, |
| 4936 | const uint8_t *tag, |
| 4937 | size_t tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4938 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4939 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4940 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4941 | *plaintext_length = 0; |
| 4942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4943 | status = psa_aead_final_checks(operation); |
| 4944 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4945 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4946 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4948 | if (operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4949 | status = PSA_ERROR_BAD_STATE; |
| 4950 | goto exit; |
| 4951 | } |
| 4952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4953 | status = psa_driver_wrapper_aead_verify(operation, plaintext, |
| 4954 | plaintext_size, |
| 4955 | plaintext_length, |
| 4956 | tag, tag_length); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4957 | |
| 4958 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4959 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4961 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | /* Abort an AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4965 | psa_status_t psa_aead_abort(psa_aead_operation_t *operation) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4966 | { |
| 4967 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4969 | if (operation->id == 0) { |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4970 | /* The object has (apparently) been initialized but it is not (yet) |
| 4971 | * in use. It's ok to call abort on such an object, and there's |
| 4972 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4973 | return PSA_SUCCESS; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4974 | } |
| 4975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4976 | status = psa_driver_wrapper_aead_abort(operation); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4978 | memset(operation, 0, sizeof(*operation)); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4980 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4981 | } |
| 4982 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4983 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4984 | /* Generators */ |
| 4985 | /****************************************************************/ |
| 4986 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4987 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4988 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4989 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \ |
| 4990 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4991 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4992 | #endif /* At least one builtin KDF */ |
| 4993 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4994 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4995 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4996 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4997 | static psa_status_t psa_key_derivation_start_hmac( |
| 4998 | psa_mac_operation_t *operation, |
| 4999 | psa_algorithm_t hash_alg, |
| 5000 | const uint8_t *hmac_key, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5001 | size_t hmac_key_length) |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5002 | { |
| 5003 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5004 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5005 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 5006 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 5007 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5008 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 5009 | operation->is_sign = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 5011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5012 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 5013 | &attributes, |
| 5014 | hmac_key, hmac_key_length, |
| 5015 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5017 | psa_reset_key_attributes(&attributes); |
| 5018 | return status; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5019 | } |
| 5020 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5021 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5022 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5023 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5024 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5025 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5026 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5027 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5028 | const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5029 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5030 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 5031 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
| 5032 | } else { |
| 5033 | return operation->alg; |
| 5034 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5035 | } |
| 5036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5037 | 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] | 5038 | { |
| 5039 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5040 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 5041 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5042 | /* The object has (apparently) been initialized but it is not |
| 5043 | * in use. It's ok to call abort on such an object, and there's |
| 5044 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5045 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5046 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5047 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5048 | mbedtls_free(operation->ctx.hkdf.info); |
| 5049 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 5050 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5051 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5052 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5053 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5054 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5055 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 5056 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5057 | if (operation->ctx.tls12_prf.secret != NULL) { |
| 5058 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret, |
| 5059 | operation->ctx.tls12_prf.secret_length); |
| 5060 | mbedtls_free(operation->ctx.tls12_prf.secret); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5061 | } |
| 5062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5063 | if (operation->ctx.tls12_prf.seed != NULL) { |
| 5064 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed, |
| 5065 | operation->ctx.tls12_prf.seed_length); |
| 5066 | mbedtls_free(operation->ctx.tls12_prf.seed); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5067 | } |
| 5068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | if (operation->ctx.tls12_prf.label != NULL) { |
| 5070 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.label, |
| 5071 | operation->ctx.tls12_prf.label_length); |
| 5072 | mbedtls_free(operation->ctx.tls12_prf.label); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5073 | } |
| 5074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5075 | if (operation->ctx.tls12_prf.other_secret != NULL) { |
| 5076 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret, |
| 5077 | operation->ctx.tls12_prf.other_secret_length); |
| 5078 | mbedtls_free(operation->ctx.tls12_prf.other_secret); |
Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 5079 | } |
| 5080 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5081 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5082 | |
| 5083 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5084 | * mbedtls_platform_zeroize() in the end of this function. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5085 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5086 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5087 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5088 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5089 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5090 | mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data, |
| 5091 | sizeof(operation->ctx.tls12_ecjpake_to_pms.data)); |
| 5092 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5093 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5094 | { |
| 5095 | status = PSA_ERROR_BAD_STATE; |
| 5096 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5097 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 5098 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5099 | } |
| 5100 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5101 | 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] | 5102 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5103 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5104 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5105 | /* This is a blank key derivation operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5106 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5107 | } |
| 5108 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5109 | *capacity = operation->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5110 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5111 | } |
| 5112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5113 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 5114 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5115 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5116 | if (operation->alg == 0) { |
| 5117 | return PSA_ERROR_BAD_STATE; |
| 5118 | } |
| 5119 | if (capacity > operation->capacity) { |
| 5120 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5121 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5122 | operation->capacity = capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5123 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5124 | } |
| 5125 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5126 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5127 | /* Read some bytes from an HKDF-based operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5128 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 5129 | psa_algorithm_t kdf_alg, |
| 5130 | uint8_t *output, |
| 5131 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5132 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5133 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 5134 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5135 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5136 | psa_status_t status; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5137 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5138 | 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] | 5139 | #else |
| 5140 | const uint8_t last_block = 0xff; |
| 5141 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5143 | if (hkdf->state < HKDF_STATE_KEYED || |
| 5144 | (!hkdf->info_set |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5145 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5146 | && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5147 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5148 | )) { |
| 5149 | return PSA_ERROR_BAD_STATE; |
| 5150 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5151 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5153 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5154 | /* Copy what remains of the current block */ |
| 5155 | uint8_t n = hash_length - hkdf->offset_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5156 | if (n > output_length) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5157 | n = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5158 | } |
| 5159 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5160 | output += n; |
| 5161 | output_length -= n; |
| 5162 | hkdf->offset_in_block += n; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5163 | if (output_length == 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5164 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5165 | } |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5166 | /* We can't be wanting more output after the last block, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5167 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5168 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5169 | * object was corrupted or if this function is called directly |
| 5170 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5171 | if (hkdf->block_number == last_block) { |
| 5172 | return PSA_ERROR_BAD_STATE; |
| 5173 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5174 | |
| 5175 | /* We need a new block */ |
| 5176 | ++hkdf->block_number; |
| 5177 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5179 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 5180 | hash_alg, |
| 5181 | hkdf->prk, |
| 5182 | hash_length); |
| 5183 | if (status != PSA_SUCCESS) { |
| 5184 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5185 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5186 | |
| 5187 | if (hkdf->block_number != 1) { |
| 5188 | status = psa_mac_update(&hkdf->hmac, |
| 5189 | hkdf->output_block, |
| 5190 | hash_length); |
| 5191 | if (status != PSA_SUCCESS) { |
| 5192 | return status; |
| 5193 | } |
| 5194 | } |
| 5195 | status = psa_mac_update(&hkdf->hmac, |
| 5196 | hkdf->info, |
| 5197 | hkdf->info_length); |
| 5198 | if (status != PSA_SUCCESS) { |
| 5199 | return status; |
| 5200 | } |
| 5201 | status = psa_mac_update(&hkdf->hmac, |
| 5202 | &hkdf->block_number, 1); |
| 5203 | if (status != PSA_SUCCESS) { |
| 5204 | return status; |
| 5205 | } |
| 5206 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 5207 | hkdf->output_block, |
| 5208 | sizeof(hkdf->output_block), |
| 5209 | &hmac_output_length); |
| 5210 | if (status != PSA_SUCCESS) { |
| 5211 | return status; |
| 5212 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5213 | } |
| 5214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5215 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5216 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5217 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5218 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5219 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5220 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5221 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5222 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5223 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5224 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5225 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 5226 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5227 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 5228 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5229 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5230 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5231 | /* We can't be wanting more output after block 0xff, otherwise |
| 5232 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5233 | * prevented this call. It could happen only if the operation |
| 5234 | * object was corrupted or if this function is called directly |
| 5235 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5236 | if (tls12_prf->block_number == 0xff) { |
| 5237 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 5238 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5239 | |
| 5240 | /* We need a new block */ |
| 5241 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5242 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5243 | |
| 5244 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5245 | * |
| 5246 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5247 | * |
| 5248 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5249 | * HMAC_hash(secret, A(2) + seed) + |
| 5250 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5251 | * |
| 5252 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5253 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5254 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5255 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5256 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5257 | * is currently extracted as `output_block` and where i is |
| 5258 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5259 | */ |
| 5260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5261 | status = psa_key_derivation_start_hmac(&hmac, |
| 5262 | hash_alg, |
| 5263 | tls12_prf->secret, |
| 5264 | tls12_prf->secret_length); |
| 5265 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5266 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5267 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5268 | |
| 5269 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5270 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5271 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5272 | * the variable seed and in this instance means it in the context of the |
| 5273 | * P_hash function, where seed = label + seed.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5274 | status = psa_mac_update(&hmac, |
| 5275 | tls12_prf->label, |
| 5276 | tls12_prf->label_length); |
| 5277 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5278 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5279 | } |
| 5280 | status = psa_mac_update(&hmac, |
| 5281 | tls12_prf->seed, |
| 5282 | tls12_prf->seed_length); |
| 5283 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5284 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5285 | } |
| 5286 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5287 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5288 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5289 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5290 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5291 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5292 | } |
| 5293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5294 | status = psa_mac_sign_finish(&hmac, |
| 5295 | tls12_prf->Ai, hash_length, |
| 5296 | &hmac_output_length); |
| 5297 | if (hmac_output_length != hash_length) { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5298 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5299 | } |
| 5300 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5301 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5302 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5303 | |
| 5304 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5305 | status = psa_key_derivation_start_hmac(&hmac, |
| 5306 | hash_alg, |
| 5307 | tls12_prf->secret, |
| 5308 | tls12_prf->secret_length); |
| 5309 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5310 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5311 | } |
| 5312 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5313 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5314 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5315 | } |
| 5316 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 5317 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5318 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5319 | } |
| 5320 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 5321 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5322 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5323 | } |
| 5324 | status = psa_mac_sign_finish(&hmac, |
| 5325 | tls12_prf->output_block, hash_length, |
| 5326 | &hmac_output_length); |
| 5327 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5328 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5329 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5330 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5331 | |
| 5332 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5333 | cleanup_status = psa_mac_abort(&hmac); |
| 5334 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5335 | status = cleanup_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5336 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5338 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5339 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5340 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5341 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5342 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5343 | psa_algorithm_t alg, |
| 5344 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5345 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5346 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5347 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 5348 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5349 | psa_status_t status; |
| 5350 | uint8_t offset, length; |
| 5351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | switch (tls12_prf->state) { |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5353 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 5354 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 5355 | break; |
| 5356 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 5357 | break; |
| 5358 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5359 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5360 | } |
| 5361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5363 | /* Check if we have fully processed the current block. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5364 | if (tls12_prf->left_in_block == 0) { |
| 5365 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 5366 | alg); |
| 5367 | if (status != PSA_SUCCESS) { |
| 5368 | return status; |
| 5369 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5370 | |
| 5371 | continue; |
| 5372 | } |
| 5373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | if (tls12_prf->left_in_block > output_length) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5375 | length = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5376 | } else { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5377 | length = tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5379 | |
| 5380 | offset = hash_length - tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5381 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5382 | output += length; |
| 5383 | output_length -= length; |
| 5384 | tls12_prf->left_in_block -= length; |
| 5385 | } |
| 5386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5387 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5388 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5389 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5390 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5391 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5392 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5393 | static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 5394 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
| 5395 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | size_t output_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5397 | { |
| 5398 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5399 | size_t output_size = 0; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | if (output_length != 32) { |
| 5402 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5403 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5405 | status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data, |
| 5406 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length, |
| 5407 | &output_size); |
| 5408 | if (status != PSA_SUCCESS) { |
| 5409 | return status; |
| 5410 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5412 | if (output_size != output_length) { |
| 5413 | return PSA_ERROR_GENERIC_ERROR; |
| 5414 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5416 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5417 | } |
| 5418 | #endif |
| 5419 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5420 | psa_status_t psa_key_derivation_output_bytes( |
| 5421 | psa_key_derivation_operation_t *operation, |
| 5422 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5424 | { |
| 5425 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5426 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5428 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5429 | /* This is a blank operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5430 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5433 | if (output_length > operation->capacity) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5434 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5435 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5436 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5437 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5438 | goto exit; |
| 5439 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5440 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5441 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5442 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5443 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5444 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5445 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5446 | * output_length > 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5448 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5449 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5450 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5451 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5452 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5453 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg, |
| 5454 | output, output_length); |
| 5455 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5456 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5457 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5458 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5459 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5460 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5461 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, |
| 5462 | kdf_alg, output, |
| 5463 | output_length); |
| 5464 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5465 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5466 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5467 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5468 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5469 | status = psa_key_derivation_tls12_ecjpake_to_pms_read( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5470 | &operation->ctx.tls12_ecjpake_to_pms, output, output_length); |
| 5471 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5472 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
| 5473 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5474 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5475 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5480 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5481 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5482 | * This allows us to differentiate between exhausted operations and |
| 5483 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5484 | * operations. */ |
| 5485 | psa_algorithm_t alg = operation->alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5486 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5487 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5488 | memset(output, '!', output_length); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5489 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5490 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5491 | } |
| 5492 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5493 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5494 | 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] | 5495 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5496 | if (data_size >= 8) { |
| 5497 | mbedtls_des_key_set_parity(data); |
| 5498 | } |
| 5499 | if (data_size >= 16) { |
| 5500 | mbedtls_des_key_set_parity(data + 8); |
| 5501 | } |
| 5502 | if (data_size >= 24) { |
| 5503 | mbedtls_des_key_set_parity(data + 16); |
| 5504 | } |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5505 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5506 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5507 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5508 | /* |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5509 | * ECC keys on a Weierstrass elliptic curve require the generation |
| 5510 | * of a private key which is an integer |
| 5511 | * in the range [1, N - 1], where N is the boundary of the private key domain: |
| 5512 | * N is the prime p for Diffie-Hellman, or the order of the |
| 5513 | * curve’s base point for ECC. |
| 5514 | * |
| 5515 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). |
| 5516 | * This function generates the private key using the following process: |
| 5517 | * |
| 5518 | * 1. Draw a byte string of length ceiling(m/8) bytes. |
| 5519 | * 2. If m is not a multiple of 8, set the most significant |
| 5520 | * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. |
| 5521 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. |
| 5522 | * 4. If k > N - 2, discard the result and return to step 1. |
| 5523 | * 5. Output k + 1 as the private key. |
| 5524 | * |
| 5525 | * This method allows compliance to NIST standards, specifically the methods titled |
| 5526 | * Key-Pair Generation by Testing Candidates in the following publications: |
| 5527 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment |
| 5528 | * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for |
| 5529 | * Diffie-Hellman keys. |
| 5530 | * |
| 5531 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature |
| 5532 | * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. |
| 5533 | * |
| 5534 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 5535 | * always NULL. |
| 5536 | */ |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5537 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 5538 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 5539 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 5540 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 5541 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5542 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5543 | psa_key_slot_t *slot, |
| 5544 | size_t bits, |
| 5545 | psa_key_derivation_operation_t *operation, |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5546 | uint8_t **data |
| 5547 | ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5548 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5549 | unsigned key_out_of_range = 1; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5550 | mbedtls_mpi k; |
| 5551 | mbedtls_mpi diff_N_2; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5552 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 5553 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5555 | mbedtls_mpi_init(&k); |
| 5556 | mbedtls_mpi_init(&diff_N_2); |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 5557 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5558 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | slot->attr.type); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5560 | mbedtls_ecp_group_id grp_id = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5561 | mbedtls_ecc_group_of_psa(curve, bits, 0); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5564 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 5565 | goto cleanup; |
| 5566 | } |
| 5567 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5568 | mbedtls_ecp_group ecp_group; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5569 | mbedtls_ecp_group_init(&ecp_group); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5572 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5573 | /* N is the boundary of the private key domain (ecp_group.N). */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5574 | /* Let m be the bit size of N. */ |
| 5575 | size_t m = ecp_group.nbits; |
| 5576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5577 | size_t m_bytes = PSA_BITS_TO_BYTES(m); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5578 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5579 | /* Calculate N - 2 - it will be needed later. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | 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] | 5581 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5582 | /* Note: This function is always called with *data == NULL and it |
| 5583 | * allocates memory for the data buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5584 | *data = mbedtls_calloc(1, m_bytes); |
| 5585 | if (*data == NULL) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5586 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5587 | goto cleanup; |
| 5588 | } |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5590 | while (key_out_of_range) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5591 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5592 | if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5593 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5594 | } |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5595 | |
| 5596 | /* 2. If m is not a multiple of 8 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5597 | if (m % 8 != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5598 | /* Set the most significant |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5599 | * (8 * ceiling(m/8) - m) bits of the first byte in |
| 5600 | * the string to zero. |
| 5601 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | uint8_t clear_bit_mask = (1 << (m % 8)) - 1; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5603 | (*data)[0] &= clear_bit_mask; |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5604 | } |
| 5605 | |
| 5606 | /* 3. Convert the string to integer k by decoding it as a |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | * big-endian byte string. |
| 5608 | */ |
| 5609 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5610 | |
| 5611 | /* 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] | 5612 | * Result of comparison is returned. When it indicates error |
| 5613 | * then this function is called again. |
| 5614 | */ |
| 5615 | 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] | 5616 | } |
| 5617 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5618 | /* 5. Output k + 1 as the private key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5619 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1)); |
| 5620 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5621 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5622 | if (ret != 0) { |
| 5623 | status = mbedtls_to_psa_error(ret); |
| 5624 | } |
| 5625 | if (status != PSA_SUCCESS) { |
| 5626 | mbedtls_free(*data); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5627 | *data = NULL; |
| 5628 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | mbedtls_mpi_free(&k); |
| 5630 | mbedtls_mpi_free(&diff_N_2); |
| 5631 | return status; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5632 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5633 | |
| 5634 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length |
| 5635 | * is determined by the curve, and sets the mandatory bits accordingly. That is: |
| 5636 | * |
| 5637 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): |
| 5638 | * draw a 32-byte string and process it as specified in |
| 5639 | * Elliptic Curves for Security [RFC7748] §5. |
| 5640 | * |
| 5641 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): |
| 5642 | * draw a 56-byte string and process it as specified in [RFC7748] §5. |
| 5643 | * |
| 5644 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 5645 | * always NULL. |
| 5646 | */ |
| 5647 | |
| 5648 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 5649 | size_t bits, |
| 5650 | psa_key_derivation_operation_t *operation, |
| 5651 | uint8_t **data |
| 5652 | ) |
| 5653 | { |
| 5654 | size_t output_length; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 5655 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5657 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5658 | case 255: |
| 5659 | output_length = 32; |
| 5660 | break; |
| 5661 | case 448: |
| 5662 | output_length = 56; |
| 5663 | break; |
| 5664 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5665 | return PSA_ERROR_INVALID_ARGUMENT; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5666 | break; |
| 5667 | } |
| 5668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5669 | *data = mbedtls_calloc(1, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | if (*data == NULL) { |
| 5672 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5673 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5675 | status = psa_key_derivation_output_bytes(operation, *data, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5677 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5678 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5679 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5681 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5682 | case 255: |
| 5683 | (*data)[0] &= 248; |
| 5684 | (*data)[31] &= 127; |
| 5685 | (*data)[31] |= 64; |
| 5686 | break; |
| 5687 | case 448: |
| 5688 | (*data)[0] &= 252; |
| 5689 | (*data)[55] |= 128; |
| 5690 | break; |
| 5691 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | return PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5693 | break; |
| 5694 | } |
| 5695 | |
| 5696 | return status; |
| 5697 | } |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5698 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 5699 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 5700 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 5701 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 5702 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5703 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5704 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5705 | psa_key_slot_t *slot, |
| 5706 | size_t bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5707 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5708 | { |
| 5709 | uint8_t *data = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5710 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5711 | size_t storage_size = bytes; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 5712 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5714 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
| 5715 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5716 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5717 | |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5718 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 5719 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 5720 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 5721 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 5722 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5723 | if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) { |
| 5724 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type); |
| 5725 | if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5726 | /* Weierstrass elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5727 | status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data); |
| 5728 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5729 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5730 | } |
| 5731 | } else { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5732 | /* Montgomery elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5733 | status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data); |
| 5734 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5735 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5736 | } |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5737 | } |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 5738 | } else |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5739 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 5740 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 5741 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 5742 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 5743 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5744 | if (key_type_is_raw_bytes(slot->attr.type)) { |
| 5745 | if (bits % 8 != 0) { |
| 5746 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5747 | } |
| 5748 | data = mbedtls_calloc(1, bytes); |
| 5749 | if (data == NULL) { |
| 5750 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5751 | } |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5753 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 5754 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5755 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5756 | } |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5757 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5758 | if (slot->attr.type == PSA_KEY_TYPE_DES) { |
| 5759 | psa_des_set_key_parity(data, bytes); |
| 5760 | } |
Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 5761 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5762 | } else { |
| 5763 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5764 | } |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5765 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5766 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5767 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5768 | .core = slot->attr |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5769 | }; |
| 5770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | if (psa_key_lifetime_is_external(attributes.core.lifetime)) { |
| 5772 | status = psa_driver_wrapper_get_key_buffer_size(&attributes, |
| 5773 | &storage_size); |
| 5774 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5775 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5776 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5777 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 5779 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5780 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5781 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5783 | status = psa_driver_wrapper_import_key(&attributes, |
| 5784 | data, bytes, |
| 5785 | slot->key.data, |
| 5786 | slot->key.bytes, |
| 5787 | &slot->key.bytes, &bits); |
| 5788 | if (bits != slot->attr.bits) { |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5789 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5790 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5791 | |
| 5792 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5793 | mbedtls_free(data); |
| 5794 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5795 | } |
| 5796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5797 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 5798 | psa_key_derivation_operation_t *operation, |
| 5799 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5800 | { |
| 5801 | psa_status_t status; |
| 5802 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5803 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5804 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5805 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5806 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5807 | /* Reject any attempt to create a zero-length key so that we don't |
| 5808 | * 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] | 5809 | if (psa_get_key_bits(attributes) == 0) { |
| 5810 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5811 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5813 | if (operation->alg == PSA_ALG_NONE) { |
| 5814 | return PSA_ERROR_BAD_STATE; |
| 5815 | } |
Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 5816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | if (!operation->can_output_key) { |
| 5818 | return PSA_ERROR_NOT_PERMITTED; |
| 5819 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5821 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, |
| 5822 | &slot, &driver); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5823 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5824 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5825 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5826 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5827 | } |
| 5828 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5829 | if (status == PSA_SUCCESS) { |
| 5830 | status = psa_generate_derived_key_internal(slot, |
| 5831 | attributes->core.bits, |
| 5832 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5833 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5834 | if (status == PSA_SUCCESS) { |
| 5835 | status = psa_finish_key_creation(slot, driver, key); |
| 5836 | } |
| 5837 | if (status != PSA_SUCCESS) { |
| 5838 | psa_fail_key_creation(slot, driver); |
| 5839 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5841 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5842 | } |
| 5843 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5844 | |
| 5845 | |
| 5846 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5847 | /* Key derivation */ |
| 5848 | /****************************************************************/ |
| 5849 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5850 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5851 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5852 | { |
| 5853 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5854 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 5855 | return 1; |
| 5856 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5857 | #endif |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5858 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5859 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 5860 | return 1; |
| 5861 | } |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5862 | #endif |
| 5863 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5864 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 5865 | return 1; |
| 5866 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5867 | #endif |
| 5868 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5869 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 5870 | return 1; |
| 5871 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5872 | #endif |
| 5873 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5874 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5875 | return 1; |
| 5876 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5877 | #endif |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5878 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5880 | return 1; |
| 5881 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5882 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5883 | return 0; |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5884 | } |
| 5885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5886 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5887 | { |
| 5888 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5889 | psa_status_t status = psa_hash_setup(&operation, alg); |
| 5890 | psa_hash_abort(&operation); |
| 5891 | return status; |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5892 | } |
| 5893 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5894 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5895 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5896 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5897 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5898 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5899 | * initialisers for this union leave some bytes unspecified.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5900 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5901 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5902 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5903 | if (!is_kdf_alg_supported(kdf_alg)) { |
| 5904 | return PSA_ERROR_NOT_SUPPORTED; |
| 5905 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5906 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5907 | /* All currently supported key derivation algorithms (apart from |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5908 | * ecjpake to pms) are based on a hash algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5909 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 5910 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 5911 | if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5912 | if (hash_size == 0) { |
| 5913 | return PSA_ERROR_NOT_SUPPORTED; |
| 5914 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5915 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5916 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 5917 | * we might fail later, which is somewhat unfriendly and potentially |
| 5918 | * risk-prone. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5919 | psa_status_t status = psa_hash_try_support(hash_alg); |
| 5920 | if (status != PSA_SUCCESS) { |
| 5921 | return status; |
| 5922 | } |
| 5923 | } else { |
| 5924 | hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5925 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5928 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && |
| 5929 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 5930 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5931 | } |
Andrzej Kurek | 7763829 | 2022-09-16 12:24:52 -0400 | [diff] [blame] | 5932 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5933 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5934 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) || |
| 5935 | (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5936 | operation->capacity = hash_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5937 | } else |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5938 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT || |
| 5939 | MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | operation->capacity = 255 * hash_size; |
| 5941 | return PSA_SUCCESS; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5942 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5944 | 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] | 5945 | { |
| 5946 | #if defined(PSA_WANT_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | if (alg == PSA_ALG_ECDH) { |
| 5948 | return PSA_SUCCESS; |
| 5949 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5950 | #endif |
| 5951 | (void) alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5953 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 5954 | |
| 5955 | static int psa_key_derivation_allows_free_form_secret_input( |
| 5956 | psa_algorithm_t kdf_alg) |
| 5957 | { |
| 5958 | #if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5959 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5960 | return 0; |
| 5961 | } |
| 5962 | #endif |
| 5963 | (void) kdf_alg; |
| 5964 | return 1; |
| 5965 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5966 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5968 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 5969 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5970 | { |
| 5971 | psa_status_t status; |
| 5972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5973 | if (operation->alg != 0) { |
| 5974 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5975 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5977 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 5978 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5979 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 5980 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 5981 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 5982 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); |
| 5983 | status = psa_key_agreement_try_support(ka_alg); |
| 5984 | if (status != PSA_SUCCESS) { |
| 5985 | return status; |
| 5986 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 5987 | if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) { |
| 5988 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5989 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5990 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 5991 | #else |
| 5992 | return PSA_ERROR_NOT_SUPPORTED; |
| 5993 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 5994 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 5995 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 5996 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 5997 | #else |
| 5998 | return PSA_ERROR_NOT_SUPPORTED; |
| 5999 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 6000 | } else { |
| 6001 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6002 | } |
| 6003 | |
| 6004 | if (status == PSA_SUCCESS) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6005 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6006 | } |
| 6007 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6008 | } |
| 6009 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6010 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6011 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 6012 | psa_algorithm_t kdf_alg, |
| 6013 | psa_key_derivation_step_t step, |
| 6014 | const uint8_t *data, |
| 6015 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6016 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6017 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6018 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6019 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6020 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6021 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 6023 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6024 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6025 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6026 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6027 | return PSA_ERROR_BAD_STATE; |
| 6028 | } else { |
| 6029 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6030 | hash_alg, |
| 6031 | data, data_length); |
| 6032 | if (status != PSA_SUCCESS) { |
| 6033 | return status; |
| 6034 | } |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6035 | hkdf->state = HKDF_STATE_STARTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6036 | return PSA_SUCCESS; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6037 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6038 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6039 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6040 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6041 | /* We shouldn't be in different state as HKDF_EXPAND only allows |
| 6042 | * two inputs: SECRET (this case) and INFO which does not modify |
| 6043 | * the state. It could happen only if the hkdf |
| 6044 | * object was corrupted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6045 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6046 | return PSA_ERROR_BAD_STATE; |
| 6047 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6048 | |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6049 | /* Allow only input that fits expected prk size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6050 | if (data_length != PSA_HASH_LENGTH(hash_alg)) { |
| 6051 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6052 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6054 | memcpy(hkdf->prk, data, data_length); |
| 6055 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6056 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6057 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6058 | /* HKDF: If no salt was provided, use an empty salt. |
| 6059 | * HKDF-EXTRACT: salt is mandatory. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6060 | if (hkdf->state == HKDF_STATE_INIT) { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6061 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6063 | return PSA_ERROR_BAD_STATE; |
| 6064 | } |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6065 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6066 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6067 | hash_alg, |
| 6068 | NULL, 0); |
| 6069 | if (status != PSA_SUCCESS) { |
| 6070 | return status; |
| 6071 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6072 | hkdf->state = HKDF_STATE_STARTED; |
| 6073 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6074 | if (hkdf->state != HKDF_STATE_STARTED) { |
| 6075 | return PSA_ERROR_BAD_STATE; |
| 6076 | } |
| 6077 | status = psa_mac_update(&hkdf->hmac, |
| 6078 | data, data_length); |
| 6079 | if (status != PSA_SUCCESS) { |
| 6080 | return status; |
| 6081 | } |
| 6082 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 6083 | hkdf->prk, |
| 6084 | sizeof(hkdf->prk), |
| 6085 | &data_length); |
| 6086 | if (status != PSA_SUCCESS) { |
| 6087 | return status; |
| 6088 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6089 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6090 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 6091 | hkdf->state = HKDF_STATE_KEYED; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6092 | hkdf->block_number = 0; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6093 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6095 | /* The only block of output is the PRK. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6096 | memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg)); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6097 | hkdf->offset_in_block = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6098 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6099 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6100 | { |
| 6101 | /* Block 0 is empty, and the next block will be |
| 6102 | * generated by psa_key_derivation_hkdf_read(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6104 | } |
| 6105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6106 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6107 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6108 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6109 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6110 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6111 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6112 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6113 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6114 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) && |
| 6115 | hkdf->state == HKDF_STATE_INIT) { |
| 6116 | return PSA_ERROR_BAD_STATE; |
| 6117 | } |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6118 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6119 | if (hkdf->state == HKDF_STATE_OUTPUT) { |
| 6120 | return PSA_ERROR_BAD_STATE; |
| 6121 | } |
| 6122 | if (hkdf->info_set) { |
| 6123 | return PSA_ERROR_BAD_STATE; |
| 6124 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6125 | hkdf->info_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | if (data_length != 0) { |
| 6127 | hkdf->info = mbedtls_calloc(1, data_length); |
| 6128 | if (hkdf->info == NULL) { |
| 6129 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6130 | } |
| 6131 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6132 | } |
| 6133 | hkdf->info_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6134 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6135 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6136 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6137 | } |
| 6138 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6139 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6140 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6141 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 6142 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6143 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 6144 | const uint8_t *data, |
| 6145 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6146 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6147 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { |
| 6148 | return PSA_ERROR_BAD_STATE; |
| 6149 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6151 | if (data_length != 0) { |
| 6152 | prf->seed = mbedtls_calloc(1, data_length); |
| 6153 | if (prf->seed == NULL) { |
| 6154 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6155 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6157 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6158 | prf->seed_length = data_length; |
| 6159 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6160 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6161 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6163 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6164 | } |
| 6165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6166 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 6167 | const uint8_t *data, |
| 6168 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6169 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6170 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET && |
| 6171 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6172 | return PSA_ERROR_BAD_STATE; |
| 6173 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | if (data_length != 0) { |
| 6176 | prf->secret = mbedtls_calloc(1, data_length); |
| 6177 | if (prf->secret == NULL) { |
| 6178 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6179 | } |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6181 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6182 | prf->secret_length = data_length; |
| 6183 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6184 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6185 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6187 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6188 | } |
| 6189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6190 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 6191 | const uint8_t *data, |
| 6192 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6193 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6194 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { |
| 6195 | return PSA_ERROR_BAD_STATE; |
| 6196 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6198 | if (data_length != 0) { |
| 6199 | prf->label = mbedtls_calloc(1, data_length); |
| 6200 | if (prf->label == NULL) { |
| 6201 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6202 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6204 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6205 | prf->label_length = data_length; |
| 6206 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6207 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6208 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6210 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6211 | } |
| 6212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 6214 | psa_key_derivation_step_t step, |
| 6215 | const uint8_t *data, |
| 6216 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6217 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6218 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6219 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6220 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6221 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6222 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6223 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6224 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6225 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6226 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6227 | } |
| 6228 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6229 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 6230 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 6231 | |
| 6232 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6233 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 6234 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6235 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6236 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6237 | { |
| 6238 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? |
| 6240 | 4 + data_length + prf->other_secret_length : |
| 6241 | 4 + 2 * data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6243 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { |
| 6244 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6245 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6247 | uint8_t *pms = mbedtls_calloc(1, pms_len); |
| 6248 | if (pms == NULL) { |
| 6249 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6250 | } |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6251 | uint8_t *cur = pms; |
| 6252 | |
| 6253 | /* pure-PSK: |
| 6254 | * Quoting RFC 4279, Section 2: |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6255 | * |
| 6256 | * The premaster secret is formed as follows: if the PSK is N octets |
| 6257 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 6258 | * uint16 with the value N, and the PSK itself. |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6259 | * |
| 6260 | * mixed-PSK: |
| 6261 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as |
| 6262 | * follows: concatenate a uint16 with the length of the other secret, |
| 6263 | * the other secret itself, uint16 with the length of PSK, and the |
| 6264 | * PSK itself. |
| 6265 | * For details please check: |
| 6266 | * - RFC 4279, Section 4 for the definition of RSA-PSK, |
| 6267 | * - RFC 4279, Section 3 for the definition of DHE-PSK, |
| 6268 | * - RFC 5489 for the definition of ECDHE-PSK. |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6269 | */ |
| 6270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6271 | if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6272 | *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length); |
| 6273 | *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length); |
| 6274 | if (prf->other_secret_length != 0) { |
| 6275 | memcpy(cur, prf->other_secret, prf->other_secret_length); |
| 6276 | mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length); |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 6277 | cur += prf->other_secret_length; |
| 6278 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6279 | } else { |
| 6280 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 6281 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 6282 | memset(cur, 0, data_length); |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6283 | cur += data_length; |
| 6284 | } |
| 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 6287 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 6288 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6289 | cur += data_length; |
| 6290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | status = psa_tls12_prf_set_key(prf, pms, cur - pms); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | mbedtls_platform_zeroize(pms, pms_len); |
| 6294 | mbedtls_free(pms); |
| 6295 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6296 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6297 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6298 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( |
| 6299 | psa_tls12_prf_key_derivation_t *prf, |
| 6300 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6301 | size_t data_length) |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6302 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6303 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { |
| 6304 | return PSA_ERROR_BAD_STATE; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6305 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6306 | |
| 6307 | if (data_length != 0) { |
| 6308 | prf->other_secret = mbedtls_calloc(1, data_length); |
| 6309 | if (prf->other_secret == NULL) { |
| 6310 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6311 | } |
| 6312 | |
| 6313 | memcpy(prf->other_secret, data, data_length); |
| 6314 | prf->other_secret_length = data_length; |
| 6315 | } else { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6316 | prf->other_secret_length = 0; |
| 6317 | } |
| 6318 | |
| 6319 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; |
| 6320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6321 | return PSA_SUCCESS; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6322 | } |
| 6323 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6324 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 6325 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6326 | psa_key_derivation_step_t step, |
| 6327 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6328 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6329 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | switch (step) { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6331 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6332 | return psa_tls12_prf_psk_to_ms_set_key(prf, |
| 6333 | data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6334 | break; |
| 6335 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6336 | return psa_tls12_prf_psk_to_ms_set_other_key(prf, |
| 6337 | data, |
| 6338 | data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6339 | break; |
| 6340 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | return psa_tls12_prf_input(prf, step, data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6342 | break; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6343 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6344 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6345 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6346 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6347 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6348 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 6349 | static psa_status_t psa_tls12_ecjpake_to_pms_input( |
| 6350 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 6351 | psa_key_derivation_step_t step, |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6352 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6353 | size_t data_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6354 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6355 | if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE || |
| 6356 | step != PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 6357 | return PSA_ERROR_INVALID_ARGUMENT; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 6358 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6359 | |
| 6360 | /* Check if the passed point is in an uncompressed form */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | if (data[0] != 0x04) { |
| 6362 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6363 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6364 | |
| 6365 | /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6366 | memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6368 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6369 | } |
| 6370 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6371 | /** Check whether the given key type is acceptable for the given |
| 6372 | * input step of a key derivation. |
| 6373 | * |
| 6374 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 6375 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 6376 | * Both secret and non-secret inputs can alternatively have the type |
| 6377 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 6378 | * that the input was passed as a buffer rather than via a key object. |
| 6379 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6380 | static int psa_key_derivation_check_input_type( |
| 6381 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6382 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6383 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6384 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6385 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 6387 | return PSA_SUCCESS; |
| 6388 | } |
| 6389 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 6390 | return PSA_SUCCESS; |
| 6391 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6392 | break; |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 6393 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 6395 | return PSA_SUCCESS; |
| 6396 | } |
| 6397 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 6398 | return PSA_SUCCESS; |
| 6399 | } |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 6400 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6401 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 6402 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 6403 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 6404 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6405 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
| 6406 | return PSA_SUCCESS; |
| 6407 | } |
| 6408 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 6409 | return PSA_SUCCESS; |
| 6410 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6411 | break; |
| 6412 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6413 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6414 | } |
| 6415 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6416 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6417 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6418 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6419 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6420 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6421 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6422 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6423 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6424 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6426 | status = psa_key_derivation_check_input_type(step, key_type); |
| 6427 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6428 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6429 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6430 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6431 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6432 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 6433 | status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg, |
| 6434 | step, data, data_length); |
| 6435 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6436 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6437 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6438 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 6439 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, |
| 6440 | step, data, data_length); |
| 6441 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6442 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 6443 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 6445 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, |
| 6446 | step, data, data_length); |
| 6447 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6448 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6449 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6450 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6451 | status = psa_tls12_ecjpake_to_pms_input( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6452 | &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length); |
| 6453 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6454 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6455 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6456 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 6457 | (void) data; |
| 6458 | (void) data_length; |
| 6459 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6460 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6461 | } |
| 6462 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6463 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6464 | if (status != PSA_SUCCESS) { |
| 6465 | psa_key_derivation_abort(operation); |
| 6466 | } |
| 6467 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6468 | } |
| 6469 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6470 | psa_status_t psa_key_derivation_input_bytes( |
| 6471 | psa_key_derivation_operation_t *operation, |
| 6472 | psa_key_derivation_step_t step, |
| 6473 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6474 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6475 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6476 | return psa_key_derivation_input_internal(operation, step, |
| 6477 | PSA_KEY_TYPE_NONE, |
| 6478 | data, data_length); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6479 | } |
| 6480 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6481 | psa_status_t psa_key_derivation_input_key( |
| 6482 | psa_key_derivation_operation_t *operation, |
| 6483 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6484 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6485 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6486 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6487 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6488 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6489 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6490 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6491 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 6492 | if (status != PSA_SUCCESS) { |
| 6493 | psa_key_derivation_abort(operation); |
| 6494 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6495 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6496 | |
| 6497 | /* Passing a key object as a SECRET input unlocks the permission |
| 6498 | * to output to a key object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6499 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6500 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6501 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6503 | status = psa_key_derivation_input_internal(operation, |
| 6504 | step, slot->attr.type, |
| 6505 | slot->key.data, |
| 6506 | slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6508 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6510 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6511 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6512 | |
| 6513 | |
| 6514 | |
| 6515 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6516 | /* Key agreement */ |
| 6517 | /****************************************************************/ |
| 6518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6519 | psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes, |
| 6520 | const uint8_t *key_buffer, |
| 6521 | size_t key_buffer_size, |
| 6522 | psa_algorithm_t alg, |
| 6523 | const uint8_t *peer_key, |
| 6524 | size_t peer_key_length, |
| 6525 | uint8_t *shared_secret, |
| 6526 | size_t shared_secret_size, |
| 6527 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6528 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6529 | switch (alg) { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6530 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6531 | case PSA_ALG_ECDH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6532 | return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer, |
| 6533 | key_buffer_size, alg, |
| 6534 | peer_key, peer_key_length, |
| 6535 | shared_secret, |
| 6536 | shared_secret_size, |
| 6537 | shared_secret_length); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6538 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6539 | default: |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6540 | (void) attributes; |
| 6541 | (void) key_buffer; |
| 6542 | (void) key_buffer_size; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 6543 | (void) peer_key; |
| 6544 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6545 | (void) shared_secret; |
| 6546 | (void) shared_secret_size; |
| 6547 | (void) shared_secret_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6548 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6549 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6550 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6551 | |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6552 | /** Internal function for raw key agreement |
| 6553 | * Calls the driver wrapper which will hand off key agreement task |
Aditya Deshpande | 40c05cc | 2022-10-14 16:41:40 +0100 | [diff] [blame] | 6554 | * to the driver's implementation if a driver is present. |
| 6555 | * Fallback specified in the driver wrapper is built-in raw key agreement |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6556 | * (psa_key_agreement_raw_builtin). |
| 6557 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6558 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 6559 | psa_key_slot_t *private_key, |
| 6560 | const uint8_t *peer_key, |
| 6561 | size_t peer_key_length, |
| 6562 | uint8_t *shared_secret, |
| 6563 | size_t shared_secret_size, |
| 6564 | size_t *shared_secret_length) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6565 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6566 | if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 6567 | return PSA_ERROR_NOT_SUPPORTED; |
| 6568 | } |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6569 | |
| 6570 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6571 | .core = private_key->attr |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6572 | }; |
| 6573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6574 | return psa_driver_wrapper_key_agreement(&attributes, |
| 6575 | private_key->key.data, |
| 6576 | private_key->key.bytes, alg, |
| 6577 | peer_key, peer_key_length, |
| 6578 | shared_secret, |
| 6579 | shared_secret_size, |
| 6580 | shared_secret_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6581 | } |
| 6582 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6583 | /* 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] | 6584 | * to potentially free embedded data structures and wipe confidential data. |
| 6585 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6586 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 6587 | psa_key_derivation_step_t step, |
| 6588 | psa_key_slot_t *private_key, |
| 6589 | const uint8_t *peer_key, |
| 6590 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6591 | { |
| 6592 | psa_status_t status; |
Aditya Deshpande | 2f7fd76 | 2022-11-22 11:10:34 +0000 | [diff] [blame] | 6593 | uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE]; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6594 | size_t shared_secret_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6595 | 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] | 6596 | |
| 6597 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 6598 | * secret. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6599 | status = psa_key_agreement_raw_internal(ka_alg, |
| 6600 | private_key, |
| 6601 | peer_key, peer_key_length, |
| 6602 | shared_secret, |
| 6603 | sizeof(shared_secret), |
| 6604 | &shared_secret_length); |
| 6605 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6606 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6607 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6608 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 6609 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6610 | * the shared secret. A shared secret is permitted wherever a key |
| 6611 | * of type DERIVE is permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6612 | status = psa_key_derivation_input_internal(operation, step, |
| 6613 | PSA_KEY_TYPE_DERIVE, |
| 6614 | shared_secret, |
| 6615 | shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6616 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6617 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 6618 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6619 | } |
| 6620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6621 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 6622 | psa_key_derivation_step_t step, |
| 6623 | mbedtls_svc_key_id_t private_key, |
| 6624 | const uint8_t *peer_key, |
| 6625 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6626 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6627 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6628 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 6629 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6631 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 6632 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6633 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6634 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6635 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 6636 | if (status != PSA_SUCCESS) { |
| 6637 | return status; |
| 6638 | } |
| 6639 | status = psa_key_agreement_internal(operation, step, |
| 6640 | slot, |
| 6641 | peer_key, peer_key_length); |
| 6642 | if (status != PSA_SUCCESS) { |
| 6643 | psa_key_derivation_abort(operation); |
| 6644 | } else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6645 | /* If a private key has been added as SECRET, we allow the derived |
| 6646 | * key material to be used as a key in PSA Crypto. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6647 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6648 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6649 | } |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6650 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6652 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6654 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6655 | } |
| 6656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6657 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 6658 | mbedtls_svc_key_id_t private_key, |
| 6659 | const uint8_t *peer_key, |
| 6660 | size_t peer_key_length, |
| 6661 | uint8_t *output, |
| 6662 | size_t output_size, |
| 6663 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6664 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6665 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6666 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6667 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6669 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6670 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6671 | goto exit; |
| 6672 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6673 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6674 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 6675 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6676 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6677 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6678 | |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 6679 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 6680 | * for the output size. The PSA specification only guarantees that this |
| 6681 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 6682 | * but it might be nice to allow smaller buffers if the output fits. |
| 6683 | * At the time of writing this comment, with only ECDH implemented, |
| 6684 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 6685 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 6686 | * be exact for it as well. */ |
| 6687 | size_t expected_length = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6688 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); |
| 6689 | if (output_size < expected_length) { |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 6690 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 6691 | goto exit; |
| 6692 | } |
| 6693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6694 | status = psa_key_agreement_raw_internal(alg, slot, |
| 6695 | peer_key, peer_key_length, |
| 6696 | output, output_size, |
| 6697 | output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6698 | |
| 6699 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6700 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6701 | /* If an error happens and is not handled properly, the output |
| 6702 | * may be used as a key to protect sensitive data. Arrange for such |
| 6703 | * a key to be random, which is likely to result in decryption or |
| 6704 | * verification errors. This is better than filling the buffer with |
| 6705 | * some constant data such as zeros, which would result in the data |
| 6706 | * being protected with a reproducible, easily knowable key. |
| 6707 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6708 | psa_generate_random(output, output_size); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6709 | *output_length = output_size; |
| 6710 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6712 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6714 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6715 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6716 | |
| 6717 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6718 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6719 | /****************************************************************/ |
| 6720 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6721 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6722 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6723 | /** Initialize the PSA random generator. |
| 6724 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6725 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6726 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6727 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6728 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6729 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6730 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6731 | /* Set default configuration if |
| 6732 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6733 | if (rng->entropy_init == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6734 | rng->entropy_init = mbedtls_entropy_init; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6735 | } |
| 6736 | if (rng->entropy_free == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6737 | rng->entropy_free = mbedtls_entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6738 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6740 | rng->entropy_init(&rng->entropy); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6741 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6742 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6743 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6744 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6745 | mbedtls_entropy_add_source(&rng->entropy, |
| 6746 | mbedtls_nv_seed_poll, NULL, |
| 6747 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6748 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6749 | #endif |
| 6750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6751 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6752 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6753 | } |
| 6754 | |
| 6755 | /** Deinitialize the PSA random generator. |
| 6756 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6757 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6758 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6759 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6760 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6761 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6762 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 6763 | rng->entropy_free(&rng->entropy); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6764 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6765 | } |
| 6766 | |
| 6767 | /** Seed the PSA random generator. |
| 6768 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6769 | 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] | 6770 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6771 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6772 | /* Do nothing: the external RNG seeds itself. */ |
| 6773 | (void) rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6774 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6775 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6776 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6777 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, |
| 6778 | drbg_seed, sizeof(drbg_seed) - 1); |
| 6779 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6780 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6781 | } |
| 6782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6783 | psa_status_t psa_generate_random(uint8_t *output, |
| 6784 | size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6785 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6786 | GUARD_MODULE_INITIALIZED; |
| 6787 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6788 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6789 | |
| 6790 | size_t output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6791 | psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng, |
| 6792 | output, output_size, |
| 6793 | &output_length); |
| 6794 | if (status != PSA_SUCCESS) { |
| 6795 | return status; |
| 6796 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6797 | /* Breaking up a request into smaller chunks is currently not supported |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6798 | * for the external RNG interface. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6799 | if (output_length != output_size) { |
| 6800 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 6801 | } |
| 6802 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6803 | |
| 6804 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6806 | while (output_size > 0) { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6807 | size_t request_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6808 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6809 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6810 | output_size); |
| 6811 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 6812 | output, request_size); |
| 6813 | if (ret != 0) { |
| 6814 | return mbedtls_to_psa_error(ret); |
| 6815 | } |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6816 | output_size -= request_size; |
| 6817 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6818 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6819 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6820 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6821 | } |
| 6822 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6823 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6824 | * |
| 6825 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6826 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6827 | * PSA API doesn't support passing an explicit state. |
| 6828 | * |
| 6829 | * In the non-external case, psa_generate_random() calls an |
| 6830 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6831 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6832 | * instead of doing this back-and-forth between the PSA API and the |
| 6833 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6834 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6835 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6836 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6837 | int mbedtls_psa_get_random(void *p_rng, |
| 6838 | unsigned char *output, |
| 6839 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6840 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6841 | /* This function takes a pointer to the RNG state because that's what |
| 6842 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6843 | * its own state internally and doesn't let the caller access that state. |
| 6844 | * So we just ignore the state parameter, and in practice we'll pass |
| 6845 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6846 | (void) p_rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6847 | psa_status_t status = psa_generate_random(output, output_size); |
| 6848 | if (status == PSA_SUCCESS) { |
| 6849 | return 0; |
| 6850 | } else { |
| 6851 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 6852 | } |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6853 | } |
| 6854 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6855 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6856 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Chris Jones | ea0a865 | 2021-03-09 19:11:19 +0000 | [diff] [blame] | 6857 | #include "entropy_poll.h" |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6859 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
| 6860 | size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6861 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6862 | if (global_data.initialized) { |
| 6863 | return PSA_ERROR_NOT_PERMITTED; |
| 6864 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6866 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 6867 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 6868 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { |
| 6869 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6870 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6872 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6873 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6874 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6875 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6876 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6877 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6878 | * \param type The key type |
| 6879 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6880 | * |
| 6881 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6882 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6883 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 6884 | * The size in bits of the key is not valid. |
| 6885 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 6886 | * The type and/or the size in bits of the key or the combination of |
| 6887 | * the two is not supported. |
| 6888 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6889 | 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] | 6890 | psa_key_type_t type, size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6891 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6892 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6894 | if (key_type_is_raw_bytes(type)) { |
| 6895 | status = psa_validate_unstructured_key_bit_size(type, bits); |
| 6896 | if (status != PSA_SUCCESS) { |
| 6897 | return status; |
| 6898 | } |
| 6899 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6900 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6901 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 6902 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { |
| 6903 | return PSA_ERROR_NOT_SUPPORTED; |
| 6904 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6905 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6906 | /* Accept only byte-aligned keys, for the same reasons as |
| 6907 | * in psa_import_rsa_key(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6908 | if (bits % 8 != 0) { |
| 6909 | return PSA_ERROR_NOT_SUPPORTED; |
| 6910 | } |
| 6911 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6912 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6913 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6914 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6915 | 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] | 6916 | /* To avoid empty block, return successfully here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6917 | return PSA_SUCCESS; |
| 6918 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6919 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6920 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6921 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6922 | } |
| 6923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6924 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6925 | } |
| 6926 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 6927 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6928 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6929 | 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] | 6930 | { |
| 6931 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6932 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6934 | if ((attributes->domain_parameters == NULL) && |
| 6935 | (attributes->domain_parameters_size != 0)) { |
| 6936 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6937 | } |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6939 | if (key_type_is_raw_bytes(type)) { |
| 6940 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 6941 | if (status != PSA_SUCCESS) { |
| 6942 | return status; |
| 6943 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6944 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6945 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6946 | if (type == PSA_KEY_TYPE_DES) { |
| 6947 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 6948 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 6949 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6950 | } else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6951 | |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6952 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 6953 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6954 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 6955 | return mbedtls_psa_rsa_generate_key(attributes, |
| 6956 | key_buffer, |
| 6957 | key_buffer_size, |
| 6958 | key_buffer_length); |
| 6959 | } else |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6960 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 6961 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6962 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6963 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6964 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 6965 | return mbedtls_psa_ecp_generate_key(attributes, |
| 6966 | key_buffer, |
| 6967 | key_buffer_size, |
| 6968 | key_buffer_length); |
| 6969 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6970 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6971 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6972 | (void) key_buffer_length; |
| 6973 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6974 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6976 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6977 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6979 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 6980 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6981 | { |
| 6982 | psa_status_t status; |
| 6983 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6984 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6985 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6986 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6987 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6988 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6989 | /* Reject any attempt to create a zero-length key so that we don't |
| 6990 | * 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] | 6991 | if (psa_get_key_bits(attributes) == 0) { |
| 6992 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6993 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6994 | |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6995 | /* Reject any attempt to create a public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6996 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { |
| 6997 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6998 | } |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7000 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 7001 | &slot, &driver); |
| 7002 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7003 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7004 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7005 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7006 | /* 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] | 7007 | * storage ( thus not in the case of generating a key in a secure element |
| 7008 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 7009 | * buffer to hold the generated key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7010 | if (slot->key.data == NULL) { |
| 7011 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 7012 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7013 | status = psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7014 | attributes->core.type, attributes->core.bits); |
| 7015 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7016 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7017 | } |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7018 | |
| 7019 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7020 | attributes->core.type, |
| 7021 | attributes->core.bits); |
| 7022 | } else { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7023 | status = psa_driver_wrapper_get_key_buffer_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7024 | attributes, &key_buffer_size); |
| 7025 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7026 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7027 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7028 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7030 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 7031 | if (status != PSA_SUCCESS) { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7032 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7033 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7034 | } |
| 7035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7036 | status = psa_driver_wrapper_generate_key(attributes, |
| 7037 | slot->key.data, slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7039 | if (status != PSA_SUCCESS) { |
| 7040 | psa_remove_key_data_from_memory(slot); |
| 7041 | } |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 7042 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7043 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7044 | if (status == PSA_SUCCESS) { |
| 7045 | status = psa_finish_key_creation(slot, driver, key); |
| 7046 | } |
| 7047 | if (status != PSA_SUCCESS) { |
| 7048 | psa_fail_key_creation(slot, driver); |
| 7049 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7051 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7052 | } |
| 7053 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7054 | /****************************************************************/ |
| 7055 | /* Module setup */ |
| 7056 | /****************************************************************/ |
| 7057 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7058 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7059 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7060 | void (* entropy_init)(mbedtls_entropy_context *ctx), |
| 7061 | void (* entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7062 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7063 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 7064 | return PSA_ERROR_BAD_STATE; |
| 7065 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7066 | global_data.rng.entropy_init = entropy_init; |
| 7067 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7068 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7069 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7070 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7072 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7073 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7074 | psa_wipe_all_key_slots(); |
| 7075 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 7076 | mbedtls_psa_random_free(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7077 | } |
| 7078 | /* Wipe all remaining data, including configuration. |
| 7079 | * In particular, this sets all state indicator to the value |
| 7080 | * indicating "uninitialized". */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7081 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 7082 | |
| 7083 | /* Terminate drivers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7084 | psa_driver_wrapper_free(); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7085 | } |
| 7086 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7087 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 7088 | /** Recover a transaction that was interrupted by a power failure. |
| 7089 | * |
| 7090 | * This function is called during initialization, before psa_crypto_init() |
| 7091 | * returns. If this function returns a failure status, the initialization |
| 7092 | * fails. |
| 7093 | */ |
| 7094 | static psa_status_t psa_crypto_recover_transaction( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7095 | const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7096 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7097 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7098 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 7099 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7100 | /* TODO - fall through to the failure case until this |
| 7101 | * is implemented. |
| 7102 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 7103 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7104 | default: |
| 7105 | /* We found an unsupported transaction in the storage. |
| 7106 | * We don't know what state the storage is in. Give up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7107 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7108 | } |
| 7109 | } |
| 7110 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 7111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7112 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7113 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 7114 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7115 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7116 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7117 | if (global_data.initialized != 0) { |
| 7118 | return PSA_SUCCESS; |
| 7119 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7120 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7121 | /* Initialize and seed the random generator. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7122 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7123 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7124 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 7125 | if (status != PSA_SUCCESS) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7126 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7127 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7128 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7130 | status = psa_initialize_key_slots(); |
| 7131 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 7132 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7133 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7134 | |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 7135 | /* Init drivers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7136 | status = psa_driver_wrapper_init(); |
| 7137 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 7138 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7139 | } |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 7140 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 7141 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7142 | status = psa_crypto_load_transaction(); |
| 7143 | if (status == PSA_SUCCESS) { |
| 7144 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 7145 | if (status != PSA_SUCCESS) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7146 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7147 | } |
| 7148 | status = psa_crypto_stop_transaction(); |
| 7149 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 7150 | /* There's no transaction to complete. It's all good. */ |
| 7151 | status = PSA_SUCCESS; |
| 7152 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 7153 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 7154 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7155 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7156 | global_data.initialized = 1; |
| 7157 | |
| 7158 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7159 | if (status != PSA_SUCCESS) { |
| 7160 | mbedtls_psa_crypto_free(); |
| 7161 | } |
| 7162 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7163 | } |
| 7164 | |
| 7165 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |