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, |
| 346 | size_t output_buffer_size, size_t output_buffer_length) { |
| 347 | size_t offset = 0; |
| 348 | |
| 349 | if (output_buffer_size == 0) { |
| 350 | /* If output_buffer_size is 0 then we have nothing to do. We must not |
| 351 | call memset because output_buffer may be NULL in this case */ |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | if (status == PSA_SUCCESS) { |
| 356 | offset = output_buffer_length; |
| 357 | } |
| 358 | |
| 359 | memset(output_buffer + offset, '!', output_buffer_size - offset); |
| 360 | } |
| 361 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 362 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 363 | |
| 364 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 365 | /****************************************************************/ |
| 366 | /* Key management */ |
| 367 | /****************************************************************/ |
| 368 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 369 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 370 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 371 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 372 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 373 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, |
| 375 | size_t bits, |
| 376 | int bits_is_sloppy) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 377 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | switch (curve) { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 379 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 381 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 382 | case 192: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | return MBEDTLS_ECP_DP_SECP192R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 384 | #endif |
| 385 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 386 | case 224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | return MBEDTLS_ECP_DP_SECP224R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 388 | #endif |
| 389 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 390 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | return MBEDTLS_ECP_DP_SECP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 392 | #endif |
| 393 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 394 | case 384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | return MBEDTLS_ECP_DP_SECP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 396 | #endif |
| 397 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 398 | case 521: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 399 | return MBEDTLS_ECP_DP_SECP521R1; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 400 | case 528: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | if (bits_is_sloppy) { |
| 402 | return MBEDTLS_ECP_DP_SECP521R1; |
| 403 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 404 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 405 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 406 | } |
| 407 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 408 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 409 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 411 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 412 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | return MBEDTLS_ECP_DP_BP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 414 | #endif |
| 415 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 416 | case 384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | return MBEDTLS_ECP_DP_BP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 418 | #endif |
| 419 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 420 | case 512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | return MBEDTLS_ECP_DP_BP512R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 422 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 423 | } |
| 424 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 425 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 426 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 427 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 428 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 429 | case 255: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | return MBEDTLS_ECP_DP_CURVE25519; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 431 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | if (bits_is_sloppy) { |
| 433 | return MBEDTLS_ECP_DP_CURVE25519; |
| 434 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 435 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 436 | #endif |
| 437 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 438 | case 448: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | return MBEDTLS_ECP_DP_CURVE448; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 440 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 441 | } |
| 442 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 443 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 444 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 446 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 447 | case 192: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | return MBEDTLS_ECP_DP_SECP192K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 449 | #endif |
| 450 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 451 | case 224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | return MBEDTLS_ECP_DP_SECP224K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 453 | #endif |
| 454 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 455 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | return MBEDTLS_ECP_DP_SECP256K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 457 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 458 | } |
| 459 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 460 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 461 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 462 | (void) bits_is_sloppy; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | return MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 464 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 465 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 466 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 467 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 468 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 469 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type, |
| 472 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 473 | { |
| 474 | /* Check that the bit size is acceptable for the key type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 476 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 477 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 478 | case PSA_KEY_TYPE_DERIVE: |
Neil Armstrong | 4b5710f | 2022-05-25 11:30:27 +0200 | [diff] [blame] | 479 | case PSA_KEY_TYPE_PASSWORD: |
| 480 | case PSA_KEY_TYPE_PASSWORD_HASH: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 481 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 482 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 483 | case PSA_KEY_TYPE_AES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | if (bits != 128 && bits != 192 && bits != 256) { |
| 485 | return PSA_ERROR_INVALID_ARGUMENT; |
| 486 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 487 | break; |
| 488 | #endif |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 489 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 490 | case PSA_KEY_TYPE_ARIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | if (bits != 128 && bits != 192 && bits != 256) { |
| 492 | return PSA_ERROR_INVALID_ARGUMENT; |
| 493 | } |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 494 | break; |
| 495 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 496 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 497 | case PSA_KEY_TYPE_CAMELLIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | if (bits != 128 && bits != 192 && bits != 256) { |
| 499 | return PSA_ERROR_INVALID_ARGUMENT; |
| 500 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 501 | break; |
| 502 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 503 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 504 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | if (bits != 64 && bits != 128 && bits != 192) { |
| 506 | return PSA_ERROR_INVALID_ARGUMENT; |
| 507 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 508 | break; |
| 509 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 510 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 511 | case PSA_KEY_TYPE_CHACHA20: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | if (bits != 256) { |
| 513 | return PSA_ERROR_INVALID_ARGUMENT; |
| 514 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 515 | break; |
| 516 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 517 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 519 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 520 | if (bits % 8 != 0) { |
| 521 | return PSA_ERROR_INVALID_ARGUMENT; |
| 522 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 527 | /** 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] | 528 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 529 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 530 | * 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] | 531 | * |
| 532 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 533 | * \param[in] key_type The key type of the key to be used with the |
| 534 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 535 | * |
| 536 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 537 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 538 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 539 | * 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] | 540 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 541 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 542 | psa_algorithm_t algorithm, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 544 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 545 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 546 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 547 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 548 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 549 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 552 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 553 | * key. */ |
| 554 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 555 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 556 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 557 | * the block length (larger than 1) for block ciphers. */ |
| 558 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 559 | return PSA_SUCCESS; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 568 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 569 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | if (slot->key.data != NULL) { |
| 571 | return PSA_ERROR_ALREADY_EXISTS; |
| 572 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 575 | if (slot->key.data == NULL) { |
| 576 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 577 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 578 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 579 | slot->key.bytes = buffer_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 580 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 583 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 584 | const uint8_t *data, |
| 585 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 586 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 588 | data_length); |
| 589 | if (status != PSA_SUCCESS) { |
| 590 | return status; |
| 591 | } |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | memcpy(slot->key.data, data, data_length); |
| 594 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 597 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 598 | const psa_key_attributes_t *attributes, |
| 599 | const uint8_t *data, size_t data_length, |
| 600 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 601 | size_t *key_buffer_length, size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 602 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 603 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 604 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 605 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 606 | /* zero-length keys are never supported. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | if (data_length == 0) { |
| 608 | return PSA_ERROR_NOT_SUPPORTED; |
| 609 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 611 | if (key_type_is_raw_bytes(type)) { |
| 612 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 614 | status = psa_validate_unstructured_key_bit_size(attributes->core.type, |
| 615 | *bits); |
| 616 | if (status != PSA_SUCCESS) { |
| 617 | return status; |
| 618 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 619 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 620 | /* Copy the key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 621 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 622 | *key_buffer_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | (void) key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 625 | return PSA_SUCCESS; |
| 626 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 627 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 628 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 629 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 630 | return mbedtls_psa_ecp_import_key(attributes, |
| 631 | data, data_length, |
| 632 | key_buffer, key_buffer_size, |
| 633 | key_buffer_length, |
| 634 | bits); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 635 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 636 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 637 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 638 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 639 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 640 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 641 | return mbedtls_psa_rsa_import_key(attributes, |
| 642 | data, data_length, |
| 643 | key_buffer, key_buffer_size, |
| 644 | key_buffer_length, |
| 645 | bits); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 646 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 647 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 648 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 649 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 651 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 654 | /** Calculate the intersection of two algorithm usage policies. |
| 655 | * |
| 656 | * Return 0 (which allows no operation) on incompatibility. |
| 657 | */ |
| 658 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 659 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 660 | psa_algorithm_t alg1, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 661 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 662 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 663 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 664 | if (alg1 == alg2) { |
| 665 | return alg1; |
| 666 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 667 | /* If the policies are from the same hash-and-sign family, check |
| 668 | * 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] | 669 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 670 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 671 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 672 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 673 | return alg2; |
| 674 | } |
| 675 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 676 | return alg1; |
| 677 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 678 | } |
| 679 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 680 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 681 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 682 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 683 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 684 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 685 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 686 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 687 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 688 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 689 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 691 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 692 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 693 | alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 694 | } |
| 695 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 697 | (alg1_len <= alg2_len)) { |
| 698 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 699 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 701 | (alg2_len <= alg1_len)) { |
| 702 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | /* If the policies are from the same MAC family, check whether one |
| 706 | * of them is a minimum-MAC-length policy. Calculate the most |
| 707 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 708 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 709 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 710 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 711 | /* Validate the combination of key type and algorithm. Since the base |
| 712 | * 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] | 713 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 714 | return 0; |
| 715 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 716 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 717 | /* Get the (exact or at-least) output lengths for both sides of the |
| 718 | * requested intersection. None of the currently supported algorithms |
| 719 | * have an output length dependent on the actual key size, so setting it |
| 720 | * to a bogus value of 0 is currently OK. |
| 721 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 722 | * Note that for at-least-this-length wildcard algorithms, the output |
| 723 | * length is set to the shortest allowed length, which allows us to |
| 724 | * calculate the most restrictive tag length for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 725 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 726 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 727 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 728 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 729 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 730 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 731 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 732 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 733 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 734 | |
| 735 | /* If only one is an at-least-this-length policy, the intersection would |
| 736 | * be the other (fixed-length) policy as long as said fixed length is |
| 737 | * equal to or larger than the shortest allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 738 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 739 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 740 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 741 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 742 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 743 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 744 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 745 | /* If none of them are wildcards, check whether they define the same tag |
| 746 | * length. This is still possible here when one is default-length and |
| 747 | * the other specific-length. Ensure to always return the |
| 748 | * specific-length version for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | if (alg1_len == alg2_len) { |
| 750 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 751 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 752 | } |
| 753 | /* If the policies are incompatible, allow nothing. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 754 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 755 | } |
| 756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 757 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 758 | psa_algorithm_t policy_alg, |
| 759 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 760 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 761 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | if (requested_alg == policy_alg) { |
| 763 | return 1; |
| 764 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 765 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 766 | * and requested_alg is the same hash-and-sign family with any hash, |
| 767 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 769 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 770 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 771 | (requested_alg & ~PSA_ALG_HASH_MASK); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 772 | } |
| 773 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 774 | * the requested algorithm, check the requested tag length to be |
| 775 | * equal-length or longer than the wildcard-specified length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 777 | PSA_ALG_IS_AEAD(requested_alg) && |
| 778 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 779 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 780 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 781 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 782 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 783 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 784 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 785 | * algorithm, check whether their MAC lengths are compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 786 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 787 | PSA_ALG_IS_MAC(requested_alg) && |
| 788 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 789 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 790 | /* Validate the combination of key type and algorithm. Since the policy |
| 791 | * and requested algorithms are the same, we only need this once. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 793 | return 0; |
| 794 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 795 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 796 | /* Get both the requested output length for the algorithm which is to be |
| 797 | * verified, and the default output length for the base algorithm. |
| 798 | * Note that none of the currently supported algorithms have an output |
| 799 | * length dependent on actual key size, so setting it to a bogus value |
| 800 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 801 | size_t requested_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 802 | key_type, 0, requested_alg); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 803 | size_t default_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 804 | key_type, 0, |
| 805 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 806 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 807 | /* If the policy is default-length, only allow an algorithm with |
| 808 | * a declared exact-length matching the default. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 810 | return requested_output_length == default_output_length; |
| 811 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 812 | |
| 813 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 814 | * length exactly matches the default length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 815 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 816 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 817 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 820 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 821 | * check for the requested MAC length to be equal to or longer than the |
| 822 | * minimum allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 823 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 824 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 825 | requested_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 826 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 827 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 828 | /* 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] | 829 | * a key derivation with that key agreement should also be allowed. This |
| 830 | * behaviour is expected to be defined in a future specification version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 832 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 833 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 834 | policy_alg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 835 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 836 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 837 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 840 | /** Test whether a policy permits an algorithm. |
| 841 | * |
| 842 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 843 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 844 | * \note This function requires providing the key type for which the policy is |
| 845 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 846 | * have different properties depending on what kind of cipher it is |
| 847 | * combined with. |
| 848 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 849 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 850 | * allowed by the \p policy. |
| 851 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 852 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 853 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 854 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 855 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 856 | psa_key_type_t key_type, |
| 857 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 858 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 859 | /* '0' is not a valid algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 860 | if (alg == 0) { |
| 861 | return PSA_ERROR_INVALID_ARGUMENT; |
| 862 | } |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 863 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 864 | /* A requested algorithm cannot be a wildcard. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 865 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 866 | return PSA_ERROR_INVALID_ARGUMENT; |
| 867 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 869 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 870 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 871 | return PSA_SUCCESS; |
| 872 | } else { |
| 873 | return PSA_ERROR_NOT_PERMITTED; |
| 874 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 877 | /** Restrict a key policy based on a constraint. |
| 878 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 879 | * \note This function requires providing the key type for which the policy is |
| 880 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 881 | * have different properties depending on what kind of cipher it is |
| 882 | * combined with. |
| 883 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 884 | * \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] | 885 | * \param[in,out] policy The policy to restrict. |
| 886 | * \param[in] constraint The policy constraint to apply. |
| 887 | * |
| 888 | * \retval #PSA_SUCCESS |
| 889 | * \c *policy contains the intersection of the original value of |
| 890 | * \c *policy and \c *constraint. |
| 891 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 892 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 893 | * \c *policy is unchanged. |
| 894 | */ |
| 895 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 896 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 897 | psa_key_policy_t *policy, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 898 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 899 | { |
| 900 | psa_algorithm_t intersection_alg = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 901 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 902 | constraint->alg); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 903 | psa_algorithm_t intersection_alg2 = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 904 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 905 | constraint->alg2); |
| 906 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 907 | return PSA_ERROR_INVALID_ARGUMENT; |
| 908 | } |
| 909 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 910 | return PSA_ERROR_INVALID_ARGUMENT; |
| 911 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 912 | policy->usage &= constraint->usage; |
| 913 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 914 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 915 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 918 | psa_status_t psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 919 | mbedtls_svc_key_id_t key, |
| 920 | psa_key_slot_t **p_slot, |
| 921 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 922 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 923 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 924 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 925 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 927 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 928 | if (status != PSA_SUCCESS) { |
| 929 | return status; |
| 930 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 931 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 932 | |
| 933 | /* Enforce that usage policy for the key slot contains all the flags |
| 934 | * required by the usage parameter. There is one exception: public |
| 935 | * keys can always be exported, so we treat public key objects as |
| 936 | * if they had the export flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 937 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 938 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 939 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 941 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 942 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 943 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 944 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 945 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 946 | /* Enforce that the usage policy permits the requested algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | if (alg != 0) { |
| 948 | status = psa_key_policy_permits(&slot->attr.policy, |
| 949 | slot->attr.type, |
| 950 | alg); |
| 951 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 952 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 953 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 954 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 956 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 957 | |
| 958 | error: |
| 959 | *p_slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 960 | psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 963 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 964 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 965 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 966 | * |
| 967 | * 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] | 968 | * available, as opposed to a key in a secure element and/or to be used |
| 969 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 970 | * |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 971 | * This is a temporary function that may be used instead of |
| 972 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 973 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 974 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 975 | * On success, the returned key slot is locked. It is the responsibility of the |
| 976 | * 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] | 977 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 978 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 979 | mbedtls_svc_key_id_t key, |
| 980 | psa_key_slot_t **p_slot, |
| 981 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 982 | psa_algorithm_t alg) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 983 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 985 | usage, alg); |
| 986 | if (status != PSA_SUCCESS) { |
| 987 | return status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 988 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 990 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
| 991 | psa_unlock_key_slot(*p_slot); |
| 992 | *p_slot = NULL; |
| 993 | return PSA_ERROR_NOT_SUPPORTED; |
| 994 | } |
| 995 | |
| 996 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 997 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 999 | 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] | 1000 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1001 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1002 | * initialized slot, so we can just free it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | if (slot->key.data != NULL) { |
| 1004 | mbedtls_platform_zeroize(slot->key.data, slot->key.bytes); |
| 1005 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1007 | mbedtls_free(slot->key.data); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1008 | slot->key.data = NULL; |
| 1009 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1011 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1014 | /** Completely wipe a slot in memory, including its policy. |
| 1015 | * Persistent storage is not affected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1016 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1017 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1018 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1020 | /* |
| 1021 | * As the return error code may not be handled in case of multiple errors, |
| 1022 | * do our best to report an unexpected lock counter. Assert with |
| 1023 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one: |
| 1024 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 1025 | * function is called as part of the execution of a test suite, the |
| 1026 | * execution of the test suite is stopped in error if the assertion fails. |
| 1027 | */ |
| 1028 | if (slot->lock_count != 1) { |
| 1029 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1030 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1031 | } |
| 1032 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1033 | /* Multipart operations may still be using the key. This is safe |
| 1034 | * because all multipart operation objects are independent from |
| 1035 | * the key slot: if they need to access the key after the setup |
| 1036 | * phase, they have a copy of the key. Note that this means that |
| 1037 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1038 | /* At this point, key material and other type-specific content has |
| 1039 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1040 | * zeroize because the metadata is not particularly sensitive. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1041 | memset(slot, 0, sizeof(*slot)); |
| 1042 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1043 | } |
| 1044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1045 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1046 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1047 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1048 | psa_status_t status; /* status of the last operation */ |
| 1049 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1050 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1051 | psa_se_drv_table_entry_t *driver; |
| 1052 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1055 | return PSA_SUCCESS; |
| 1056 | } |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1057 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1058 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1059 | * 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] | 1060 | * key, this will load the key description from persistent memory if not |
| 1061 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1062 | * the key is operated by an SE or not and this information is needed by |
| 1063 | * the current implementation. |
| 1064 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1065 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1066 | if (status != PSA_SUCCESS) { |
| 1067 | return status; |
| 1068 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1069 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1070 | /* |
| 1071 | * If the key slot containing the key description is under access by the |
| 1072 | * library (apart from the present access), the key cannot be destroyed |
| 1073 | * yet. For the time being, just return in error. Eventually (to be |
| 1074 | * implemented), the key should be destroyed when all accesses have |
| 1075 | * stopped. |
| 1076 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1077 | if (slot->lock_count > 1) { |
| 1078 | psa_unlock_key_slot(slot); |
| 1079 | return PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1080 | } |
| 1081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1082 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1083 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1084 | * if we attempt it, depending on whether the key is merely read-only |
| 1085 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1086 | * Just do the best we can, which is to wipe the copy in memory |
| 1087 | * (done in this function's cleanup code). */ |
| 1088 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1089 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1092 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1093 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1094 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1095 | /* For a key in a secure element, we need to do three things: |
| 1096 | * remove the key file in internal storage, destroy the |
| 1097 | * key inside the secure element, and update the driver's |
| 1098 | * persistent data. Start a transaction that will encompass these |
| 1099 | * three actions. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1100 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1101 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1102 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1103 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1104 | status = psa_crypto_save_transaction(); |
| 1105 | if (status != PSA_SUCCESS) { |
| 1106 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1107 | /* We should still try to destroy the key in the secure |
| 1108 | * element and the key metadata in storage. This is especially |
| 1109 | * important if the error is that the storage is full. |
| 1110 | * But how to do it exactly without risking an inconsistent |
| 1111 | * state after a reset? |
| 1112 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1113 | */ |
| 1114 | overall_status = status; |
| 1115 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1118 | status = psa_destroy_se_key(driver, |
| 1119 | psa_key_slot_get_slot_number(slot)); |
| 1120 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1121 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1122 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1123 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1124 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1125 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1126 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1127 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1128 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1129 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1130 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | } |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1132 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1133 | /* TODO: other slots may have a copy of the same key. We should |
| 1134 | * invalidate them. |
| 1135 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1136 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1137 | } |
| 1138 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1139 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1140 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1141 | if (driver != NULL) { |
| 1142 | status = psa_save_se_persistent_data(driver); |
| 1143 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1144 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1145 | } |
| 1146 | status = psa_crypto_stop_transaction(); |
| 1147 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1148 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1149 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1150 | } |
| 1151 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1152 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1153 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1154 | status = psa_wipe_key_slot(slot); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1155 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1156 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1157 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1158 | } |
| 1159 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1160 | } |
| 1161 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1162 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1163 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1164 | static psa_status_t psa_get_rsa_public_exponent( |
| 1165 | const mbedtls_rsa_context *rsa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1166 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1167 | { |
| 1168 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1169 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1170 | uint8_t *buffer = NULL; |
| 1171 | size_t buflen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1172 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1174 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1175 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1176 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1177 | } |
| 1178 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1179 | /* It's the default value, which is reported as an empty string, |
| 1180 | * so there's nothing to do. */ |
| 1181 | goto exit; |
| 1182 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1184 | buflen = mbedtls_mpi_size(&mpi); |
| 1185 | buffer = mbedtls_calloc(1, buflen); |
| 1186 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1187 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1188 | goto exit; |
| 1189 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1190 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1191 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1192 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1194 | attributes->domain_parameters = buffer; |
| 1195 | attributes->domain_parameters_size = buflen; |
| 1196 | |
| 1197 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1198 | mbedtls_mpi_free(&mpi); |
| 1199 | if (ret != 0) { |
| 1200 | mbedtls_free(buffer); |
| 1201 | } |
| 1202 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1203 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1204 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1205 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1206 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1207 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1208 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1209 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1210 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1211 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1212 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1213 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1214 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1216 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1218 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1219 | if (status != PSA_SUCCESS) { |
| 1220 | return status; |
| 1221 | } |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1222 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1223 | attributes->core = slot->attr; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1224 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1225 | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1226 | |
| 1227 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1228 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1229 | psa_set_key_slot_number(attributes, |
| 1230 | psa_key_slot_get_slot_number(slot)); |
| 1231 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1232 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1234 | switch (slot->attr.type) { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1235 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1236 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1237 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1238 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1239 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1240 | * is not yet implemented. |
| 1241 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1242 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1243 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1244 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1245 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1246 | status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1247 | slot->attr.type, |
| 1248 | slot->key.data, |
| 1249 | slot->key.bytes, |
| 1250 | &rsa); |
| 1251 | if (status != PSA_SUCCESS) { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1252 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1253 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1255 | status = psa_get_rsa_public_exponent(rsa, |
| 1256 | attributes); |
| 1257 | mbedtls_rsa_free(rsa); |
| 1258 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1259 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1260 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1261 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1262 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1263 | default: |
| 1264 | /* Nothing else to do. */ |
| 1265 | break; |
| 1266 | } |
| 1267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1268 | if (status != PSA_SUCCESS) { |
| 1269 | psa_reset_key_attributes(attributes); |
| 1270 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1272 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1274 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1275 | } |
| 1276 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1277 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1278 | psa_status_t psa_get_key_slot_number( |
| 1279 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1280 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1281 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1282 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1283 | *slot_number = attributes->slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1284 | return PSA_SUCCESS; |
| 1285 | } else { |
| 1286 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1287 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1288 | } |
| 1289 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1291 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1292 | size_t key_buffer_size, |
| 1293 | uint8_t *data, |
| 1294 | size_t data_size, |
| 1295 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1296 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1297 | if (key_buffer_size > data_size) { |
| 1298 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1299 | } |
| 1300 | memcpy(data, key_buffer, key_buffer_size); |
| 1301 | memset(data + key_buffer_size, 0, |
| 1302 | data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1303 | *data_length = key_buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1304 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1305 | } |
| 1306 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1307 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1308 | const psa_key_attributes_t *attributes, |
| 1309 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1310 | uint8_t *data, size_t data_size, size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1311 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1312 | psa_key_type_t type = attributes->core.type; |
| 1313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | if (key_type_is_raw_bytes(type) || |
| 1315 | PSA_KEY_TYPE_IS_RSA(type) || |
| 1316 | PSA_KEY_TYPE_IS_ECC(type)) { |
| 1317 | return psa_export_key_buffer_internal( |
| 1318 | key_buffer, key_buffer_size, |
| 1319 | data, data_size, data_length); |
| 1320 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1321 | /* This shouldn't happen in the reference implementation, but |
| 1322 | it is valid for a special-purpose implementation to omit |
| 1323 | support for exporting certain key types. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1324 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1328 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
| 1329 | uint8_t *data, |
| 1330 | size_t data_size, |
| 1331 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1332 | { |
| 1333 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1334 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1335 | psa_key_slot_t *slot; |
| 1336 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1337 | /* Reject a zero-length output buffer now, since this can never be a |
| 1338 | * valid key representation. This way we know that data must be a valid |
| 1339 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1340 | if (data_size == 0) { |
| 1341 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1342 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1343 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1344 | /* Set the key to empty now, so that even when there are errors, we always |
| 1345 | * set data_length to a value between 0 and data_size. On error, setting |
| 1346 | * the key to empty is a good choice because an empty key representation is |
| 1347 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1348 | *data_length = 0; |
| 1349 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1350 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1351 | * which don't require any flag, but |
| 1352 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1353 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1355 | PSA_KEY_USAGE_EXPORT, 0); |
| 1356 | if (status != PSA_SUCCESS) { |
| 1357 | return status; |
| 1358 | } |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1359 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1360 | psa_key_attributes_t attributes = { |
| 1361 | .core = slot->attr |
| 1362 | }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1363 | status = psa_driver_wrapper_export_key(&attributes, |
| 1364 | slot->key.data, slot->key.bytes, |
| 1365 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1367 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1369 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1370 | } |
| 1371 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1372 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1373 | const psa_key_attributes_t *attributes, |
| 1374 | const uint8_t *key_buffer, |
| 1375 | size_t key_buffer_size, |
| 1376 | uint8_t *data, |
| 1377 | size_t data_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1378 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1379 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1380 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1382 | if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) { |
| 1383 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1384 | /* Exporting public -> public */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1385 | return psa_export_key_buffer_internal( |
| 1386 | key_buffer, key_buffer_size, |
| 1387 | data, data_size, data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1388 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1390 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1391 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1392 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1393 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1394 | key_buffer, |
| 1395 | key_buffer_size, |
| 1396 | data, |
| 1397 | data_size, |
| 1398 | data_length); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1399 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1400 | /* 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] | 1401 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1402 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1403 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1404 | } else { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1405 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1406 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1407 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1408 | key_buffer, |
| 1409 | key_buffer_size, |
| 1410 | data, |
| 1411 | data_size, |
| 1412 | data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1413 | #else |
| 1414 | /* 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] | 1415 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1416 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1417 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1418 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | } else { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1420 | /* This shouldn't happen in the reference implementation, but |
| 1421 | it is valid for a special-purpose implementation to omit |
| 1422 | support for exporting certain key types. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1423 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1424 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1425 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1427 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, |
| 1428 | uint8_t *data, |
| 1429 | size_t data_size, |
| 1430 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1431 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1432 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1433 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1434 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1435 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1436 | /* Reject a zero-length output buffer now, since this can never be a |
| 1437 | * valid key representation. This way we know that data must be a valid |
| 1438 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1439 | if (data_size == 0) { |
| 1440 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1441 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1442 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1443 | /* Set the key to empty now, so that even when there are errors, we always |
| 1444 | * set data_length to a value between 0 and data_size. On error, setting |
| 1445 | * the key to empty is a good choice because an empty key representation is |
| 1446 | * unlikely to be accepted anywhere. */ |
| 1447 | *data_length = 0; |
| 1448 | |
| 1449 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1450 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1451 | if (status != PSA_SUCCESS) { |
| 1452 | return status; |
| 1453 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1455 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1456 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1457 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1458 | } |
| 1459 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1460 | psa_key_attributes_t attributes = { |
| 1461 | .core = slot->attr |
| 1462 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1463 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1464 | &attributes, slot->key.data, slot->key.bytes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1465 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1466 | |
| 1467 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1468 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1470 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1471 | } |
| 1472 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1473 | #if defined(static_assert) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1474 | static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1475 | "One or more key attribute flag is listed as both external-only and dual-use"); |
| 1476 | static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1477 | "One or more key attribute flag is listed as both internal-only and dual-use"); |
| 1478 | static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1479 | "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] | 1480 | #endif |
| 1481 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1482 | /** Validate that a key policy is internally well-formed. |
| 1483 | * |
| 1484 | * This function only rejects invalid policies. It does not validate the |
| 1485 | * consistency of the policy with respect to other attributes of the key |
| 1486 | * such as the key type. |
| 1487 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1488 | 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] | 1489 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1490 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1491 | PSA_KEY_USAGE_COPY | |
| 1492 | PSA_KEY_USAGE_ENCRYPT | |
| 1493 | PSA_KEY_USAGE_DECRYPT | |
| 1494 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1495 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1496 | PSA_KEY_USAGE_SIGN_HASH | |
| 1497 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1498 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
| 1499 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1500 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1501 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1503 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1504 | } |
| 1505 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1506 | /** Validate the internal consistency of key attributes. |
| 1507 | * |
| 1508 | * This function only rejects invalid attribute values. If does not |
| 1509 | * validate the consistency of the attributes with any key data that may |
| 1510 | * be involved in the creation of the key. |
| 1511 | * |
| 1512 | * Call this function early in the key creation process. |
| 1513 | * |
| 1514 | * \param[in] attributes Key attributes for the new key. |
| 1515 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1516 | * NULL for a transparent key. |
| 1517 | * |
| 1518 | */ |
| 1519 | static psa_status_t psa_validate_key_attributes( |
| 1520 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1521 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1522 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1523 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1524 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1525 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | status = psa_validate_key_location(lifetime, p_drv); |
| 1528 | if (status != PSA_SUCCESS) { |
| 1529 | return status; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1530 | } |
| 1531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1532 | status = psa_validate_key_persistence(lifetime); |
| 1533 | if (status != PSA_SUCCESS) { |
| 1534 | return status; |
| 1535 | } |
| 1536 | |
| 1537 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1538 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1539 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1540 | } |
| 1541 | } else { |
| 1542 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1543 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1548 | if (status != PSA_SUCCESS) { |
| 1549 | return status; |
| 1550 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1551 | |
| 1552 | /* Refuse to create overly large keys. |
| 1553 | * Note that this doesn't trigger on import if the attributes don't |
| 1554 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1555 | * psa_import_key() needs its own checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1556 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1557 | return PSA_ERROR_NOT_SUPPORTED; |
| 1558 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1559 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1560 | /* Reject invalid flags. These should not be reachable through the API. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1561 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1562 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { |
| 1563 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1564 | } |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1566 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1567 | } |
| 1568 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1569 | /** Prepare a key slot to receive key material. |
| 1570 | * |
| 1571 | * This function allocates a key slot and sets its metadata. |
| 1572 | * |
| 1573 | * If this function fails, call psa_fail_key_creation(). |
| 1574 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1575 | * This function is intended to be used as follows: |
| 1576 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1577 | * it with the specified attributes, and in case of a volatile key assign it |
| 1578 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1579 | * -# Populate the slot with the key material. |
| 1580 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1581 | * In case of failure at any step, stop the sequence and call |
| 1582 | * psa_fail_key_creation(). |
| 1583 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1584 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1585 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1586 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1587 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1588 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1589 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1590 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1591 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1592 | * |
| 1593 | * \retval #PSA_SUCCESS |
| 1594 | * The key slot is ready to receive key material. |
| 1595 | * \return If this function fails, the key slot is an invalid state. |
| 1596 | * 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] | 1597 | */ |
| 1598 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1599 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1600 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1601 | psa_key_slot_t **p_slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1602 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1603 | { |
| 1604 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1605 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1606 | psa_key_slot_t *slot; |
| 1607 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1608 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1609 | *p_drv = NULL; |
| 1610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1611 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1612 | if (status != PSA_SUCCESS) { |
| 1613 | return status; |
| 1614 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1616 | status = psa_get_empty_key_slot(&volatile_key_id, p_slot); |
| 1617 | if (status != PSA_SUCCESS) { |
| 1618 | return status; |
| 1619 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1620 | slot = *p_slot; |
| 1621 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1622 | /* We're storing the declared bit-size of the key. It's up to each |
| 1623 | * creation mechanism to verify that this information is correct. |
| 1624 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1625 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1626 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1627 | * volatile key identifier associated to the slot returned to contain its |
| 1628 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1629 | |
| 1630 | slot->attr = attributes->core; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1631 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1632 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1633 | slot->attr.id = volatile_key_id; |
| 1634 | #else |
| 1635 | slot->attr.id.key_id = volatile_key_id; |
| 1636 | #endif |
| 1637 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1638 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1639 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1640 | * external-only flags, query `attributes`. Thanks to the check |
| 1641 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1642 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1643 | * may have set. */ |
| 1644 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1645 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1646 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1647 | /* 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] | 1648 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1649 | * create the key file in internal storage, create the |
| 1650 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1651 | * persistent data. This is done by starting a transaction that will |
| 1652 | * encompass these three actions. |
| 1653 | * For registering a volatile key, we just need to find an appropriate |
| 1654 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1655 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1656 | /* The first thing to do is to find a slot number for the new key. |
| 1657 | * We save the slot number in persistent storage as part of the |
| 1658 | * transaction data. It will be needed to recover if the power |
| 1659 | * fails during the key creation process, to clean up on the secure |
| 1660 | * element side after restarting. Obtaining a slot number from the |
| 1661 | * secure element driver updates its persistent state, but we do not yet |
| 1662 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1663 | * 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] | 1664 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1665 | psa_key_slot_number_t slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1666 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1667 | &slot_number); |
| 1668 | if (status != PSA_SUCCESS) { |
| 1669 | return status; |
| 1670 | } |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1672 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1673 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1674 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1675 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1676 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1677 | status = psa_crypto_save_transaction(); |
| 1678 | if (status != PSA_SUCCESS) { |
| 1679 | (void) psa_crypto_stop_transaction(); |
| 1680 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1681 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1682 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1683 | |
| 1684 | status = psa_copy_key_material_into_slot( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1685 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1686 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1688 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1689 | /* Key registration only makes sense with a secure element. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1690 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1691 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1692 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1694 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1695 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1696 | |
| 1697 | /** Finalize the creation of a key once its key material has been set. |
| 1698 | * |
| 1699 | * This entails writing the key to persistent storage. |
| 1700 | * |
| 1701 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1702 | * See the documentation of psa_start_key_creation() for the intended use |
| 1703 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1704 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1705 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1706 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1707 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1708 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1709 | * \param[in,out] slot Pointer to the slot with key material. |
| 1710 | * \param[in] driver The secure element driver for the key, |
| 1711 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1712 | * \param[out] key On success, identifier of the key. Note that the |
| 1713 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1714 | * |
| 1715 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1716 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1717 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1718 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1719 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1720 | * \retval #PSA_ERROR_DATA_INVALID |
| 1721 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1722 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1723 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1724 | * \return If this function fails, the key slot is an invalid state. |
| 1725 | * 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] | 1726 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1727 | static psa_status_t psa_finish_key_creation( |
| 1728 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1729 | psa_se_drv_table_entry_t *driver, |
| 1730 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1731 | { |
| 1732 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1733 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1734 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1735 | |
| 1736 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1737 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1738 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1739 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1740 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1741 | psa_key_slot_number_t slot_number = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1742 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1743 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1744 | #if defined(static_assert) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1745 | static_assert(sizeof(slot_number) == |
| 1746 | sizeof(data.slot_number), |
| 1747 | "Slot number size does not match psa_se_key_data_storage_t"); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1748 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1749 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1750 | status = psa_save_persistent_key(&slot->attr, |
| 1751 | (uint8_t *) &data, |
| 1752 | sizeof(data)); |
| 1753 | } else |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1754 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1755 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1756 | /* Key material is saved in export representation in the slot, so |
| 1757 | * just pass the slot buffer for storage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | status = psa_save_persistent_key(&slot->attr, |
| 1759 | slot->key.data, |
| 1760 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1761 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1762 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1763 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1764 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1765 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1766 | /* Finish the transaction for a key creation. This does not |
| 1767 | * happen when registering an existing key. Detect this case |
| 1768 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1769 | * creation of a persistent key in a secure element requires a transaction, |
| 1770 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | if (driver != NULL && |
| 1772 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1773 | status = psa_save_se_persistent_data(driver); |
| 1774 | if (status != PSA_SUCCESS) { |
| 1775 | psa_destroy_persistent_key(slot->attr.id); |
| 1776 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1777 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1778 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1779 | } |
| 1780 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1782 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1783 | *key = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1784 | status = psa_unlock_key_slot(slot); |
| 1785 | if (status != PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1786 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1787 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1788 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1790 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | /** Abort the creation of a key. |
| 1794 | * |
| 1795 | * You may call this function after calling psa_start_key_creation(), |
| 1796 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1797 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1798 | * See the documentation of psa_start_key_creation() for the intended use |
| 1799 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1800 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1801 | * \param[in,out] slot Pointer to the slot with key material. |
| 1802 | * \param[in] driver The secure element driver for the key, |
| 1803 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1804 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1805 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 1806 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1807 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1808 | (void) driver; |
| 1809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | if (slot == NULL) { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1811 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1812 | } |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1813 | |
| 1814 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1815 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1816 | * element, and the failure happened later (when saving metadata |
| 1817 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1818 | * element. |
| 1819 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1820 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1821 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1822 | /* Abort the ongoing transaction if any (there may not be one if |
| 1823 | * the creation process failed before starting one, or if the |
| 1824 | * key creation is a registration of a key in a secure element). |
| 1825 | * Earlier functions must already have done what it takes to undo any |
| 1826 | * partial creation. All that's left is to update the transaction data |
| 1827 | * itself. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1828 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1829 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1831 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1832 | } |
| 1833 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1834 | /** Validate optional attributes during key creation. |
| 1835 | * |
| 1836 | * Some key attributes are optional during key creation. If they are |
| 1837 | * specified in the attributes structure, check that they are consistent |
| 1838 | * with the data in the slot. |
| 1839 | * |
| 1840 | * This function should be called near the end of key creation, after |
| 1841 | * the slot in memory is fully populated but before saving persistent data. |
| 1842 | */ |
| 1843 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1844 | const psa_key_slot_t *slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1845 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1846 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | if (attributes->core.type != 0) { |
| 1848 | if (attributes->core.type != slot->attr.type) { |
| 1849 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1850 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1851 | } |
| 1852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1853 | if (attributes->domain_parameters_size != 0) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1854 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1855 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1856 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1857 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1858 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1859 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1860 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1861 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1862 | slot->attr.type, |
| 1863 | slot->key.data, |
| 1864 | slot->key.bytes, |
| 1865 | &rsa); |
| 1866 | if (status != PSA_SUCCESS) { |
| 1867 | return status; |
| 1868 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1870 | mbedtls_mpi_init(&actual); |
| 1871 | mbedtls_mpi_init(&required); |
| 1872 | ret = mbedtls_rsa_export(rsa, |
| 1873 | NULL, NULL, NULL, NULL, &actual); |
| 1874 | mbedtls_rsa_free(rsa); |
| 1875 | mbedtls_free(rsa); |
| 1876 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1877 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1878 | } |
| 1879 | ret = mbedtls_mpi_read_binary(&required, |
| 1880 | attributes->domain_parameters, |
| 1881 | attributes->domain_parameters_size); |
| 1882 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1883 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1884 | } |
| 1885 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1886 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1887 | } |
| 1888 | rsa_exit: |
| 1889 | mbedtls_mpi_free(&actual); |
| 1890 | mbedtls_mpi_free(&required); |
| 1891 | if (ret != 0) { |
| 1892 | return mbedtls_to_psa_error(ret); |
| 1893 | } |
| 1894 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1895 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1896 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1897 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1898 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1899 | } |
| 1900 | } |
| 1901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1902 | if (attributes->core.bits != 0) { |
| 1903 | if (attributes->core.bits != slot->attr.bits) { |
| 1904 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1905 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1906 | } |
| 1907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1908 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1909 | } |
| 1910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1911 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
| 1912 | const uint8_t *data, |
| 1913 | size_t data_length, |
| 1914 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1915 | { |
| 1916 | psa_status_t status; |
| 1917 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1918 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1919 | size_t bits; |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1920 | size_t storage_size = data_length; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1921 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1922 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1923 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1924 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1925 | * This also rejects any key which might be encoded as an empty string, |
| 1926 | * which is never valid. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | if (data_length == 0) { |
| 1928 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1929 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1930 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1931 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1932 | if (data_length > SIZE_MAX / 8) { |
| 1933 | return PSA_ERROR_NOT_SUPPORTED; |
| 1934 | } |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1936 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 1937 | &slot, &driver); |
| 1938 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1939 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1940 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1941 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1942 | /* 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] | 1943 | * storage ( thus not in the case of importing a key in a secure element |
| 1944 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 1945 | * buffer to hold the imported key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1946 | if (slot->key.data == NULL) { |
| 1947 | if (psa_key_lifetime_is_external(attributes->core.lifetime)) { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1948 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1949 | attributes, data, data_length, &storage_size); |
| 1950 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1951 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1952 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1953 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1954 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 1955 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1956 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1957 | } |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1958 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1959 | |
| 1960 | bits = slot->attr.bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1961 | status = psa_driver_wrapper_import_key(attributes, |
| 1962 | data, data_length, |
| 1963 | slot->key.data, |
| 1964 | slot->key.bytes, |
| 1965 | &slot->key.bytes, &bits); |
| 1966 | if (status != PSA_SUCCESS) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1967 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1968 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1970 | if (slot->attr.bits == 0) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1971 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1972 | } else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1973 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1974 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1975 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 1976 | |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1977 | /* Enforce a size limit, and in particular ensure that the bit |
| 1978 | * size fits in its representation type.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1979 | if (bits > PSA_MAX_KEY_BITS) { |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1980 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1981 | goto exit; |
| 1982 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1983 | status = psa_validate_optional_attributes(slot, attributes); |
| 1984 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1985 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1986 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1988 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1989 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1990 | if (status != PSA_SUCCESS) { |
| 1991 | psa_fail_key_creation(slot, driver); |
| 1992 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1994 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1995 | } |
| 1996 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1997 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1998 | psa_status_t mbedtls_psa_register_se_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1999 | const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2000 | { |
| 2001 | psa_status_t status; |
| 2002 | psa_key_slot_t *slot = NULL; |
| 2003 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2004 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2005 | |
| 2006 | /* Leaving attributes unspecified is not currently supported. |
| 2007 | * It could make sense to query the key type and size from the |
| 2008 | * secure element, but not all secure elements support this |
| 2009 | * and the driver HAL doesn't currently support it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2010 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 2011 | return PSA_ERROR_NOT_SUPPORTED; |
| 2012 | } |
| 2013 | if (psa_get_key_bits(attributes) == 0) { |
| 2014 | return PSA_ERROR_NOT_SUPPORTED; |
| 2015 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2017 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 2018 | &slot, &driver); |
| 2019 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2020 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2021 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2023 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2024 | |
| 2025 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2026 | if (status != PSA_SUCCESS) { |
| 2027 | psa_fail_key_creation(slot, driver); |
| 2028 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2029 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2030 | /* Registration doesn't keep the key in RAM. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2031 | psa_close_key(key); |
| 2032 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2033 | } |
| 2034 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2036 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2037 | const psa_key_attributes_t *specified_attributes, |
| 2038 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2039 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2040 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2041 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2042 | psa_key_slot_t *source_slot = NULL; |
| 2043 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2044 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2045 | psa_se_drv_table_entry_t *driver = NULL; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2046 | size_t storage_size = 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2047 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2048 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2049 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2050 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2051 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2052 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2053 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2054 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2056 | status = psa_validate_optional_attributes(source_slot, |
| 2057 | specified_attributes); |
| 2058 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2059 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2060 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2061 | |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2062 | /* The target key type and number of bits have been validated by |
| 2063 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2064 | * equal to the ones of the source key. So it is safe to inherit |
| 2065 | * them from the source key now." |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2066 | * */ |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2067 | actual_attributes.core.bits = source_slot->attr.bits; |
| 2068 | actual_attributes.core.type = source_slot->attr.type; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2069 | |
| 2070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2071 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2072 | &actual_attributes.core.policy, |
| 2073 | &source_slot->attr.policy); |
| 2074 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2075 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2076 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2078 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2079 | &target_slot, &driver); |
| 2080 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2081 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2082 | } |
| 2083 | if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) != |
| 2084 | PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2085 | /* |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2086 | * If the source and target keys are stored in different locations, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2087 | * the source key would need to be exported as plaintext and re-imported |
| 2088 | * in the other location. This has security implications which have not |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2089 | * been fully mapped. For now, this can be achieved through |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2090 | * appropriate API invocations from the application, if needed. |
| 2091 | * */ |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2092 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2093 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2094 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2095 | /* |
| 2096 | * When the source and target keys are within the same location, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2097 | * - For transparent keys it is a blind copy without any driver invocation, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2098 | * - For opaque keys this translates to an invocation of the drivers' |
| 2099 | * copy_key entry point through the dispatch layer. |
| 2100 | * */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2101 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
| 2102 | status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes, |
| 2103 | &storage_size); |
| 2104 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2105 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2106 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2108 | status = psa_allocate_buffer_to_slot(target_slot, storage_size); |
| 2109 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2110 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2111 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | status = psa_driver_wrapper_copy_key(&actual_attributes, |
| 2114 | source_slot->key.data, |
| 2115 | source_slot->key.bytes, |
| 2116 | target_slot->key.data, |
| 2117 | target_slot->key.bytes, |
| 2118 | &target_slot->key.bytes); |
| 2119 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2120 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2121 | } |
| 2122 | } else { |
| 2123 | status = psa_copy_key_material_into_slot(target_slot, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2124 | source_slot->key.data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2125 | source_slot->key.bytes); |
| 2126 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2127 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2128 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2129 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2130 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2131 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2132 | if (status != PSA_SUCCESS) { |
| 2133 | psa_fail_key_creation(target_slot, driver); |
| 2134 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2136 | unlock_status = psa_unlock_key_slot(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2138 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2139 | } |
| 2140 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2141 | |
| 2142 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2143 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2144 | /* Message digests */ |
| 2145 | /****************************************************************/ |
| 2146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2147 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2148 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2149 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2150 | if (operation->id == 0) { |
| 2151 | return PSA_SUCCESS; |
| 2152 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2154 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2155 | operation->id = 0; |
| 2156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2157 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2158 | } |
| 2159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2160 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2161 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2162 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2163 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2164 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2165 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2166 | if (operation->id != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2167 | status = PSA_ERROR_BAD_STATE; |
| 2168 | goto exit; |
| 2169 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2171 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2172 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2173 | goto exit; |
| 2174 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2175 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2176 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2177 | * directly zeroes the int-sized dummy member of the context union. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2178 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2181 | |
| 2182 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2183 | if (status != PSA_SUCCESS) { |
| 2184 | psa_hash_abort(operation); |
| 2185 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2186 | |
| 2187 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2188 | } |
| 2189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2190 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 2191 | const uint8_t *input, |
| 2192 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2193 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2194 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2196 | if (operation->id == 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2197 | status = PSA_ERROR_BAD_STATE; |
| 2198 | goto exit; |
| 2199 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2200 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2201 | /* Don't require hash implementations to behave correctly on a |
| 2202 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2203 | if (input_length == 0) { |
| 2204 | return PSA_SUCCESS; |
| 2205 | } |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2207 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2208 | |
| 2209 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2210 | if (status != PSA_SUCCESS) { |
| 2211 | psa_hash_abort(operation); |
| 2212 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2214 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2215 | } |
| 2216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2217 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2218 | uint8_t *hash, |
| 2219 | size_t hash_size, |
| 2220 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2221 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2222 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2223 | if (operation->id == 0) { |
| 2224 | return PSA_ERROR_BAD_STATE; |
| 2225 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2226 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2227 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2228 | operation, hash, hash_size, hash_length); |
| 2229 | psa_hash_abort(operation); |
| 2230 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2231 | } |
| 2232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2233 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 2234 | const uint8_t *hash, |
| 2235 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2236 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2237 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2238 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2239 | psa_status_t status = psa_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2240 | operation, |
| 2241 | actual_hash, sizeof(actual_hash), |
| 2242 | &actual_hash_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2244 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2245 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2246 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2248 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2249 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2250 | goto exit; |
| 2251 | } |
| 2252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2253 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2254 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2255 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2256 | |
| 2257 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2258 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2259 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2260 | psa_hash_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2261 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2263 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2266 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 2267 | const uint8_t *input, size_t input_length, |
| 2268 | uint8_t *hash, size_t hash_size, |
| 2269 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2270 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2271 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2272 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2273 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2274 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2276 | return psa_driver_wrapper_hash_compute(alg, input, input_length, |
| 2277 | hash, hash_size, hash_length); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2278 | } |
| 2279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 2281 | const uint8_t *input, size_t input_length, |
| 2282 | const uint8_t *hash, size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2283 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2284 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2285 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2287 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2288 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2289 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2290 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2291 | psa_status_t status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | alg, input, input_length, |
| 2293 | actual_hash, sizeof(actual_hash), |
| 2294 | &actual_hash_length); |
| 2295 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2296 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2297 | } |
| 2298 | if (actual_hash_length != hash_length) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2299 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2300 | goto exit; |
| 2301 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2302 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2303 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2304 | } |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2305 | |
| 2306 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2307 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2308 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2309 | } |
| 2310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2311 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2312 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2313 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2314 | if (source_operation->id == 0 || |
| 2315 | target_operation->id != 0) { |
| 2316 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2317 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2319 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2320 | target_operation); |
| 2321 | if (status != PSA_SUCCESS) { |
| 2322 | psa_hash_abort(target_operation); |
| 2323 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2325 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2326 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2327 | |
| 2328 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2329 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2330 | /* MAC */ |
| 2331 | /****************************************************************/ |
| 2332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2333 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2334 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2335 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2336 | if (operation->id == 0) { |
| 2337 | return PSA_SUCCESS; |
| 2338 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2340 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2341 | operation->mac_size = 0; |
| 2342 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2343 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2345 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2346 | } |
| 2347 | |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2348 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2349 | psa_algorithm_t alg, |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2350 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2351 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2352 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2353 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2354 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2355 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2357 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2358 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2359 | } |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2360 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2361 | /* Validate the combination of key type and algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2362 | status = psa_mac_key_can_do(alg, key_type); |
| 2363 | if (status != PSA_SUCCESS) { |
| 2364 | return status; |
| 2365 | } |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2366 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2367 | /* Get the output length for the algorithm and key combination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2370 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2371 | /* A very short MAC is too short for security since it can be |
| 2372 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2373 | * so we make this our minimum, even though 32 bits is still |
| 2374 | * too small for security. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2375 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2376 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2378 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2379 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2380 | /* It's impossible to "truncate" to a larger length than the full length |
| 2381 | * of the algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2383 | } |
| 2384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2385 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2386 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2387 | * that is disabled in the compile-time configuration. The result can |
| 2388 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2389 | * configuration into account. In this case, force a return of |
| 2390 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2391 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2392 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2393 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2394 | * systematically generated tests. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2395 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2396 | } |
| 2397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2398 | return PSA_SUCCESS; |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2399 | } |
| 2400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2401 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2402 | mbedtls_svc_key_id_t key, |
| 2403 | psa_algorithm_t alg, |
| 2404 | int is_sign) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2405 | { |
| 2406 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2407 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2408 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2409 | |
| 2410 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2411 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2412 | status = PSA_ERROR_BAD_STATE; |
| 2413 | goto exit; |
| 2414 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2415 | |
| 2416 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2417 | key, |
| 2418 | &slot, |
| 2419 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2420 | alg); |
| 2421 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2422 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2423 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2424 | |
| 2425 | psa_key_attributes_t attributes = { |
| 2426 | .core = slot->attr |
| 2427 | }; |
| 2428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2429 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2430 | &operation->mac_size); |
| 2431 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2432 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2433 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2434 | |
| 2435 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2436 | /* Dispatch the MAC setup call with validated input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | if (is_sign) { |
| 2438 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2439 | &attributes, |
| 2440 | slot->key.data, |
| 2441 | slot->key.bytes, |
| 2442 | alg); |
| 2443 | } else { |
| 2444 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2445 | &attributes, |
| 2446 | slot->key.data, |
| 2447 | slot->key.bytes, |
| 2448 | alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2449 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2450 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2451 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | if (status != PSA_SUCCESS) { |
| 2453 | psa_mac_abort(operation); |
| 2454 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2456 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2458 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2459 | } |
| 2460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2461 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2462 | mbedtls_svc_key_id_t key, |
| 2463 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2464 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2465 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2466 | } |
| 2467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2468 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2469 | mbedtls_svc_key_id_t key, |
| 2470 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2471 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2472 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2473 | } |
| 2474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2475 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 2476 | const uint8_t *input, |
| 2477 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2478 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2479 | if (operation->id == 0) { |
| 2480 | return PSA_ERROR_BAD_STATE; |
| 2481 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2482 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2483 | /* Don't require hash implementations to behave correctly on a |
| 2484 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2485 | if (input_length == 0) { |
| 2486 | return PSA_SUCCESS; |
| 2487 | } |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2489 | psa_status_t status = psa_driver_wrapper_mac_update(operation, |
| 2490 | input, input_length); |
| 2491 | if (status != PSA_SUCCESS) { |
| 2492 | psa_mac_abort(operation); |
| 2493 | } |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2495 | return status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2496 | } |
| 2497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2498 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 2499 | uint8_t *mac, |
| 2500 | size_t mac_size, |
| 2501 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2502 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2503 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2504 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2506 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2507 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2508 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2509 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2511 | if (!operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2512 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2513 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2514 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2515 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2516 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2517 | * once all the error checks are done. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2518 | if (operation->mac_size == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2519 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2520 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2521 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | if (mac_size < operation->mac_size) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2524 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2525 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2526 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2528 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2529 | mac, operation->mac_size, |
| 2530 | mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2531 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2532 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2533 | /* In case of success, set the potential excess room in the output buffer |
| 2534 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2535 | * In case of error, set the output length and content to a safe default, |
| 2536 | * such that in case the caller misses an error check, the output would be |
| 2537 | * an unachievable MAC. |
| 2538 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2539 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2540 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2541 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2542 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2543 | |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame^] | 2544 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2546 | abort_status = psa_mac_abort(operation); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2549 | } |
| 2550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2551 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2552 | const uint8_t *mac, |
| 2553 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2554 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2555 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2556 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2558 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2559 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2560 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2561 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2563 | if (operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2564 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2565 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2566 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2568 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2569 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2570 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2571 | } |
| 2572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2573 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2574 | mac, mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2575 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2576 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2577 | abort_status = psa_mac_abort(operation); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2579 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2580 | } |
| 2581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2582 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2583 | psa_algorithm_t alg, |
| 2584 | const uint8_t *input, |
| 2585 | size_t input_length, |
| 2586 | uint8_t *mac, |
| 2587 | size_t mac_size, |
| 2588 | size_t *mac_length, |
| 2589 | int is_sign) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2590 | { |
| 2591 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2592 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2593 | psa_key_slot_t *slot; |
| 2594 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2595 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2596 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2597 | key, |
| 2598 | &slot, |
| 2599 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2600 | alg); |
| 2601 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2602 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2603 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2604 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2605 | psa_key_attributes_t attributes = { |
| 2606 | .core = slot->attr |
| 2607 | }; |
| 2608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2609 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2610 | &operation_mac_size); |
| 2611 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2612 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2613 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2615 | if (mac_size < operation_mac_size) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2616 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2617 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | status = psa_driver_wrapper_mac_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2621 | &attributes, |
| 2622 | slot->key.data, slot->key.bytes, |
| 2623 | alg, |
| 2624 | input, input_length, |
| 2625 | mac, operation_mac_size, mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2626 | |
| 2627 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2628 | /* In case of success, set the potential excess room in the output buffer |
| 2629 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2630 | * In case of error, set the output length and content to a safe default, |
| 2631 | * such that in case the caller misses an error check, the output would be |
| 2632 | * an unachievable MAC. |
| 2633 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2634 | if (status != PSA_SUCCESS) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2635 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2636 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2637 | } |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame^] | 2638 | |
| 2639 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2641 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2643 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2644 | } |
| 2645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2646 | 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] | 2647 | psa_algorithm_t alg, |
| 2648 | const uint8_t *input, |
| 2649 | size_t input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2650 | uint8_t *mac, |
| 2651 | size_t mac_size, |
| 2652 | size_t *mac_length) |
| 2653 | { |
| 2654 | return psa_mac_compute_internal(key, alg, |
| 2655 | input, input_length, |
| 2656 | mac, mac_size, mac_length, 1); |
| 2657 | } |
| 2658 | |
| 2659 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2660 | psa_algorithm_t alg, |
| 2661 | const uint8_t *input, |
| 2662 | size_t input_length, |
| 2663 | const uint8_t *mac, |
| 2664 | size_t mac_length) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2665 | { |
| 2666 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2667 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2668 | size_t actual_mac_length; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2670 | status = psa_mac_compute_internal(key, alg, |
| 2671 | input, input_length, |
| 2672 | actual_mac, sizeof(actual_mac), |
| 2673 | &actual_mac_length, 0); |
| 2674 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2675 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2676 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2678 | if (mac_length != actual_mac_length) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2679 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2680 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2681 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2682 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2683 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2684 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2685 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2686 | |
| 2687 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2690 | return status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2691 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2692 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2693 | /****************************************************************/ |
| 2694 | /* Asymmetric cryptography */ |
| 2695 | /****************************************************************/ |
| 2696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2697 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2698 | psa_algorithm_t alg) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2699 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2700 | if (input_is_message) { |
| 2701 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 2702 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2703 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2705 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2706 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { |
| 2707 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2708 | } |
| 2709 | } |
| 2710 | } else { |
| 2711 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2712 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2713 | } |
| 2714 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2716 | return PSA_SUCCESS; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2717 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2719 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 2720 | int input_is_message, |
| 2721 | psa_algorithm_t alg, |
| 2722 | const uint8_t *input, |
| 2723 | size_t input_length, |
| 2724 | uint8_t *signature, |
| 2725 | size_t signature_size, |
| 2726 | size_t *signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2727 | { |
| 2728 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2729 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2730 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2731 | |
| 2732 | *signature_length = 0; |
| 2733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2734 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2735 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2736 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2737 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2738 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2739 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2740 | * 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] | 2741 | * buffer can in principle be empty since it doesn't actually have |
| 2742 | * to be a hash.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | if (signature_size == 0) { |
| 2744 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 2745 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2746 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2747 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2748 | key, &slot, |
| 2749 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2750 | PSA_KEY_USAGE_SIGN_HASH, |
| 2751 | alg); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2753 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2754 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2755 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2757 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2758 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2759 | goto exit; |
| 2760 | } |
| 2761 | |
| 2762 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2764 | }; |
| 2765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2766 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2767 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2768 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2769 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2770 | signature, signature_size, signature_length); |
| 2771 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2772 | |
| 2773 | status = psa_driver_wrapper_sign_hash( |
| 2774 | &attributes, slot->key.data, slot->key.bytes, |
| 2775 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2776 | signature, signature_size, signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2779 | |
| 2780 | exit: |
Paul Elliott | 59200a2 | 2023-02-21 15:07:40 +0000 | [diff] [blame] | 2781 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 2782 | *signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2784 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2786 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2787 | } |
| 2788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2789 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 2790 | int input_is_message, |
| 2791 | psa_algorithm_t alg, |
| 2792 | const uint8_t *input, |
| 2793 | size_t input_length, |
| 2794 | const uint8_t *signature, |
| 2795 | size_t signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2796 | { |
| 2797 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2798 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2799 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2801 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2802 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2803 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2804 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2805 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2806 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2807 | key, &slot, |
| 2808 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2809 | PSA_KEY_USAGE_VERIFY_HASH, |
| 2810 | alg); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2812 | if (status != PSA_SUCCESS) { |
| 2813 | return status; |
| 2814 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2815 | |
| 2816 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2817 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2818 | }; |
| 2819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2820 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2821 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2822 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2823 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2824 | signature, signature_length); |
| 2825 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2826 | status = psa_driver_wrapper_verify_hash( |
| 2827 | &attributes, slot->key.data, slot->key.bytes, |
| 2828 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2829 | signature, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2830 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2832 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2834 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2835 | |
| 2836 | } |
| 2837 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2838 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2839 | const psa_key_attributes_t *attributes, |
| 2840 | const uint8_t *key_buffer, |
| 2841 | size_t key_buffer_size, |
| 2842 | psa_algorithm_t alg, |
| 2843 | const uint8_t *input, |
| 2844 | size_t input_length, |
| 2845 | uint8_t *signature, |
| 2846 | size_t signature_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2847 | size_t *signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2848 | { |
| 2849 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2851 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2852 | size_t hash_length; |
| 2853 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2854 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2855 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2856 | PSA_ALG_SIGN_GET_HASH(alg), |
| 2857 | input, input_length, |
| 2858 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2860 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2861 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2862 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2863 | |
| 2864 | return psa_driver_wrapper_sign_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2865 | attributes, key_buffer, key_buffer_size, |
| 2866 | alg, hash, hash_length, |
| 2867 | signature, signature_size, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2868 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2870 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2871 | } |
| 2872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2873 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 2874 | psa_algorithm_t alg, |
| 2875 | const uint8_t *input, |
| 2876 | size_t input_length, |
| 2877 | uint8_t *signature, |
| 2878 | size_t signature_size, |
| 2879 | size_t *signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2880 | { |
| 2881 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2882 | key, 1, alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2883 | signature, signature_size, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2884 | } |
| 2885 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2886 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2887 | const psa_key_attributes_t *attributes, |
| 2888 | const uint8_t *key_buffer, |
| 2889 | size_t key_buffer_size, |
| 2890 | psa_algorithm_t alg, |
| 2891 | const uint8_t *input, |
| 2892 | size_t input_length, |
| 2893 | const uint8_t *signature, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2894 | size_t signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2895 | { |
| 2896 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2898 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2899 | size_t hash_length; |
| 2900 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2901 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2902 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2903 | PSA_ALG_SIGN_GET_HASH(alg), |
| 2904 | input, input_length, |
| 2905 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2907 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2908 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2909 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2910 | |
| 2911 | return psa_driver_wrapper_verify_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2912 | attributes, key_buffer, key_buffer_size, |
| 2913 | alg, hash, hash_length, |
| 2914 | signature, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2915 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2917 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2918 | } |
| 2919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2920 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 2921 | psa_algorithm_t alg, |
| 2922 | const uint8_t *input, |
| 2923 | size_t input_length, |
| 2924 | const uint8_t *signature, |
| 2925 | size_t signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2926 | { |
| 2927 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2928 | key, 1, alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2929 | signature, signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2930 | } |
| 2931 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2932 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2933 | const psa_key_attributes_t *attributes, |
| 2934 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2935 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2936 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2937 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2938 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 2939 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 2940 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2941 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2943 | return mbedtls_psa_rsa_sign_hash( |
| 2944 | attributes, |
| 2945 | key_buffer, key_buffer_size, |
| 2946 | alg, hash, hash_length, |
| 2947 | signature, signature_size, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2948 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2949 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2950 | } else { |
| 2951 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2952 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2953 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 2954 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2955 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2956 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2957 | return mbedtls_psa_ecdsa_sign_hash( |
| 2958 | attributes, |
| 2959 | key_buffer, key_buffer_size, |
| 2960 | alg, hash, hash_length, |
| 2961 | signature, signature_size, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2962 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2963 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2964 | } else { |
| 2965 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2966 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2967 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2969 | (void) key_buffer; |
| 2970 | (void) key_buffer_size; |
| 2971 | (void) hash; |
| 2972 | (void) hash_length; |
| 2973 | (void) signature; |
| 2974 | (void) signature_size; |
| 2975 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2977 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2978 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2980 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 2981 | psa_algorithm_t alg, |
| 2982 | const uint8_t *hash, |
| 2983 | size_t hash_length, |
| 2984 | uint8_t *signature, |
| 2985 | size_t signature_size, |
| 2986 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2987 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2988 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2989 | key, 0, alg, hash, hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2990 | signature, signature_size, signature_length); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2991 | } |
| 2992 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2993 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2994 | const psa_key_attributes_t *attributes, |
| 2995 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2996 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2997 | const uint8_t *signature, size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2998 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2999 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 3000 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3001 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3002 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3003 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3004 | return mbedtls_psa_rsa_verify_hash( |
| 3005 | attributes, |
| 3006 | key_buffer, key_buffer_size, |
| 3007 | alg, hash, hash_length, |
| 3008 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3009 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3010 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | } else { |
| 3012 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3013 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3014 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3015 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3016 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3017 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3018 | return mbedtls_psa_ecdsa_verify_hash( |
| 3019 | attributes, |
| 3020 | key_buffer, key_buffer_size, |
| 3021 | alg, hash, hash_length, |
| 3022 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3023 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3024 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | } else { |
| 3026 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3027 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3028 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3030 | (void) key_buffer; |
| 3031 | (void) key_buffer_size; |
| 3032 | (void) hash; |
| 3033 | (void) hash_length; |
| 3034 | (void) signature; |
| 3035 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3037 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3038 | } |
| 3039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3040 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3041 | psa_algorithm_t alg, |
| 3042 | const uint8_t *hash, |
| 3043 | size_t hash_length, |
| 3044 | const uint8_t *signature, |
| 3045 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3046 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3047 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3048 | key, 0, alg, hash, hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | signature, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3050 | } |
| 3051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3052 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3053 | psa_algorithm_t alg, |
| 3054 | const uint8_t *input, |
| 3055 | size_t input_length, |
| 3056 | const uint8_t *salt, |
| 3057 | size_t salt_length, |
| 3058 | uint8_t *output, |
| 3059 | size_t output_size, |
| 3060 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3061 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3062 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3063 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3064 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3065 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3066 | (void) input; |
| 3067 | (void) input_length; |
| 3068 | (void) salt; |
| 3069 | (void) output; |
| 3070 | (void) output_size; |
| 3071 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3072 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3074 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3075 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3076 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3077 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3078 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3079 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3080 | if (status != PSA_SUCCESS) { |
| 3081 | return status; |
| 3082 | } |
| 3083 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3084 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3085 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3086 | goto exit; |
| 3087 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3088 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3089 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | .core = slot->attr |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3091 | }; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3092 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3093 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3094 | &attributes, slot->key.data, slot->key.bytes, |
| 3095 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3096 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3097 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3100 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3101 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3103 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3104 | psa_algorithm_t alg, |
| 3105 | const uint8_t *input, |
| 3106 | size_t input_length, |
| 3107 | const uint8_t *salt, |
| 3108 | size_t salt_length, |
| 3109 | uint8_t *output, |
| 3110 | size_t output_size, |
| 3111 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3112 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3113 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3114 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3115 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3116 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3117 | (void) input; |
| 3118 | (void) input_length; |
| 3119 | (void) salt; |
| 3120 | (void) output; |
| 3121 | (void) output_size; |
| 3122 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3123 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3125 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3126 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3127 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3128 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3129 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3130 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3131 | if (status != PSA_SUCCESS) { |
| 3132 | return status; |
| 3133 | } |
| 3134 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3135 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3136 | goto exit; |
| 3137 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3138 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3139 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3140 | .core = slot->attr |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3141 | }; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3142 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3143 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3144 | &attributes, slot->key.data, slot->key.bytes, |
| 3145 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3146 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3147 | |
| 3148 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3149 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3151 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3152 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3153 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3154 | /****************************************************************/ |
| 3155 | /* Asymmetric interruptible cryptography */ |
| 3156 | /****************************************************************/ |
| 3157 | |
| 3158 | void psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3159 | { |
| 3160 | psa_driver_wrapper_interruptible_set_max_ops(max_ops); |
| 3161 | } |
| 3162 | |
| 3163 | uint32_t psa_interruptible_get_max_ops(void) |
| 3164 | { |
| 3165 | return psa_driver_wrapper_interruptible_get_max_ops(); |
| 3166 | } |
| 3167 | |
| 3168 | |
| 3169 | uint32_t psa_sign_hash_get_num_ops( |
| 3170 | const psa_sign_hash_interruptible_operation_t *operation) |
| 3171 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3172 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | uint32_t psa_verify_hash_get_num_ops( |
| 3176 | const psa_verify_hash_interruptible_operation_t *operation) |
| 3177 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3178 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3181 | static psa_status_t psa_sign_hash_abort_internal( |
| 3182 | psa_sign_hash_interruptible_operation_t *operation) |
| 3183 | { |
| 3184 | if (operation->id == 0) { |
| 3185 | /* The object has (apparently) been initialized but it is not (yet) |
| 3186 | * in use. It's ok to call abort on such an object, and there's |
| 3187 | * nothing to do. */ |
| 3188 | return PSA_SUCCESS; |
| 3189 | } |
| 3190 | |
| 3191 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3192 | |
| 3193 | status = psa_driver_wrapper_sign_hash_abort(operation); |
| 3194 | |
| 3195 | operation->id = 0; |
| 3196 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame] | 3197 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3198 | * only want to be cleared by the application calling abort, not by abort |
| 3199 | * being called at completion of an operation. */ |
| 3200 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3201 | return status; |
| 3202 | } |
| 3203 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3204 | psa_status_t psa_sign_hash_start( |
| 3205 | psa_sign_hash_interruptible_operation_t *operation, |
| 3206 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3207 | const uint8_t *hash, size_t hash_length) |
| 3208 | { |
| 3209 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3210 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3211 | psa_key_slot_t *slot; |
| 3212 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3213 | /* Check that start has not been previously called, or operation has not |
| 3214 | * previously errored. */ |
| 3215 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3216 | return PSA_ERROR_BAD_STATE; |
| 3217 | } |
| 3218 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3219 | status = psa_sign_verify_check_alg(0, alg); |
| 3220 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3221 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3222 | return status; |
| 3223 | } |
| 3224 | |
| 3225 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3226 | PSA_KEY_USAGE_SIGN_HASH, |
| 3227 | alg); |
| 3228 | |
| 3229 | if (status != PSA_SUCCESS) { |
| 3230 | goto exit; |
| 3231 | } |
| 3232 | |
| 3233 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
| 3234 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3235 | goto exit; |
| 3236 | } |
| 3237 | |
| 3238 | psa_key_attributes_t attributes = { |
| 3239 | .core = slot->attr |
| 3240 | }; |
| 3241 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3242 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3243 | operation->num_ops = 0; |
| 3244 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3245 | status = psa_driver_wrapper_sign_hash_start(operation, &attributes, |
| 3246 | slot->key.data, |
| 3247 | slot->key.bytes, alg, |
| 3248 | hash, hash_length); |
| 3249 | exit: |
| 3250 | |
| 3251 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3252 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3253 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | unlock_status = psa_unlock_key_slot(slot); |
| 3257 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3258 | if (unlock_status != PSA_SUCCESS) { |
| 3259 | operation->error_occurred = 1; |
| 3260 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3261 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3262 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | |
| 3266 | psa_status_t psa_sign_hash_complete( |
| 3267 | psa_sign_hash_interruptible_operation_t *operation, |
| 3268 | uint8_t *signature, size_t signature_size, |
| 3269 | size_t *signature_length) |
| 3270 | { |
| 3271 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3272 | |
| 3273 | *signature_length = 0; |
| 3274 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3275 | /* Check that start has been called first, and that operation has not |
| 3276 | * previously errored. */ |
| 3277 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3278 | status = PSA_ERROR_BAD_STATE; |
| 3279 | goto exit; |
| 3280 | } |
| 3281 | |
Paul Elliott | 096abc4 | 2023-02-03 18:33:23 +0000 | [diff] [blame] | 3282 | /* Immediately reject a zero-length signature buffer. This guarantees that |
| 3283 | * signature must be a valid pointer. */ |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3284 | if (signature_size == 0) { |
| 3285 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3286 | goto exit; |
| 3287 | } |
| 3288 | |
| 3289 | status = psa_driver_wrapper_sign_hash_complete(operation, signature, |
| 3290 | signature_size, |
| 3291 | signature_length); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3292 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3293 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3294 | operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation); |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3295 | |
Paul Elliott | ebe225c | 2023-02-10 14:32:53 +0000 | [diff] [blame] | 3296 | exit: |
| 3297 | |
Paul Elliott | 59200a2 | 2023-02-21 15:07:40 +0000 | [diff] [blame] | 3298 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 3299 | *signature_length); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3300 | |
Paul Elliott | 53bb312 | 2023-02-10 14:22:22 +0000 | [diff] [blame] | 3301 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3302 | if (status != PSA_SUCCESS) { |
| 3303 | operation->error_occurred = 1; |
| 3304 | } |
| 3305 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3306 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | return status; |
| 3310 | } |
| 3311 | |
| 3312 | psa_status_t psa_sign_hash_abort( |
| 3313 | psa_sign_hash_interruptible_operation_t *operation) |
| 3314 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3315 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3316 | |
| 3317 | status = psa_sign_hash_abort_internal(operation); |
| 3318 | |
| 3319 | /* We clear the number of ops done here, so that it is not cleared when |
| 3320 | * the operation fails or succeeds, only on manual abort. */ |
| 3321 | operation->num_ops = 0; |
| 3322 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3323 | /* Likewise, failure state. */ |
| 3324 | operation->error_occurred = 0; |
| 3325 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3326 | return status; |
| 3327 | } |
| 3328 | |
| 3329 | static psa_status_t psa_verify_hash_abort_internal( |
| 3330 | psa_verify_hash_interruptible_operation_t *operation) |
| 3331 | { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3332 | if (operation->id == 0) { |
| 3333 | /* The object has (apparently) been initialized but it is not (yet) |
| 3334 | * in use. It's ok to call abort on such an object, and there's |
| 3335 | * nothing to do. */ |
| 3336 | return PSA_SUCCESS; |
| 3337 | } |
| 3338 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3339 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3340 | |
| 3341 | status = psa_driver_wrapper_verify_hash_abort(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3342 | |
| 3343 | operation->id = 0; |
| 3344 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame] | 3345 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3346 | * only want to be cleared by the application calling abort, not by abort |
| 3347 | * being called at completion of an operation. */ |
| 3348 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3349 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | psa_status_t psa_verify_hash_start( |
| 3353 | psa_verify_hash_interruptible_operation_t *operation, |
| 3354 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3355 | const uint8_t *hash, size_t hash_length, |
| 3356 | const uint8_t *signature, size_t signature_length) |
| 3357 | { |
| 3358 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3359 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3360 | psa_key_slot_t *slot; |
| 3361 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3362 | /* Check that start has not been previously called, or operation has not |
| 3363 | * previously errored. */ |
| 3364 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3365 | return PSA_ERROR_BAD_STATE; |
| 3366 | } |
| 3367 | |
| 3368 | status = psa_sign_verify_check_alg(0, alg); |
| 3369 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3370 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3371 | return status; |
| 3372 | } |
| 3373 | |
| 3374 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3375 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3376 | alg); |
| 3377 | |
| 3378 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3379 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3380 | return status; |
| 3381 | } |
| 3382 | |
| 3383 | psa_key_attributes_t attributes = { |
| 3384 | .core = slot->attr |
| 3385 | }; |
| 3386 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3387 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3388 | operation->num_ops = 0; |
| 3389 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3390 | status = psa_driver_wrapper_verify_hash_start(operation, &attributes, |
| 3391 | slot->key.data, |
| 3392 | slot->key.bytes, |
| 3393 | alg, hash, hash_length, |
| 3394 | signature, signature_length); |
| 3395 | |
| 3396 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3397 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3398 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | unlock_status = psa_unlock_key_slot(slot); |
| 3402 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3403 | if (unlock_status != PSA_SUCCESS) { |
| 3404 | operation->error_occurred = 1; |
| 3405 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3406 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3407 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | psa_status_t psa_verify_hash_complete( |
| 3411 | psa_verify_hash_interruptible_operation_t *operation) |
| 3412 | { |
| 3413 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3414 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3415 | /* Check that start has been called first, and that operation has not |
| 3416 | * previously errored. */ |
| 3417 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3418 | status = PSA_ERROR_BAD_STATE; |
| 3419 | goto exit; |
| 3420 | } |
| 3421 | |
| 3422 | status = psa_driver_wrapper_verify_hash_complete(operation); |
| 3423 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3424 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3425 | operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops( |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3426 | operation); |
| 3427 | |
Paul Elliott | ebe225c | 2023-02-10 14:32:53 +0000 | [diff] [blame] | 3428 | exit: |
| 3429 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3430 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3431 | if (status != PSA_SUCCESS) { |
| 3432 | operation->error_occurred = 1; |
| 3433 | } |
| 3434 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3435 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | return status; |
| 3439 | } |
| 3440 | |
| 3441 | psa_status_t psa_verify_hash_abort( |
| 3442 | psa_verify_hash_interruptible_operation_t *operation) |
| 3443 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3444 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3445 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3446 | status = psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3447 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3448 | /* We clear the number of ops done here, so that it is not cleared when |
| 3449 | * the operation fails or succeeds, only on manual abort. */ |
| 3450 | operation->num_ops = 0; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3451 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3452 | /* Likewise, failure state. */ |
| 3453 | operation->error_occurred = 0; |
| 3454 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3455 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3458 | /****************************************************************/ |
| 3459 | /* Asymmetric interruptible cryptography internal */ |
| 3460 | /* implementations */ |
| 3461 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3462 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3463 | static uint32_t mbedtls_psa_interruptible_max_ops = |
| 3464 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 3465 | |
| 3466 | void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3467 | { |
Paul Elliott | 7cc4e81 | 2023-01-10 17:14:11 +0000 | [diff] [blame] | 3468 | mbedtls_psa_interruptible_max_ops = max_ops; |
| 3469 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3470 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3471 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3472 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3473 | |
| 3474 | /* Internal implementation uses zero to indicate infinite number max ops, |
| 3475 | * therefore avoid this value, and set to minimum possible. */ |
| 3476 | if (max_ops == 0) { |
| 3477 | max_ops = 1; |
| 3478 | } |
| 3479 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3480 | mbedtls_ecp_set_max_ops(max_ops); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3481 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3482 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3483 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3484 | } |
| 3485 | |
| 3486 | uint32_t mbedtls_psa_interruptible_get_max_ops(void) |
| 3487 | { |
| 3488 | return mbedtls_psa_interruptible_max_ops; |
| 3489 | } |
| 3490 | |
| 3491 | uint32_t mbedtls_psa_sign_hash_get_num_ops( |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3492 | const mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3493 | { |
| 3494 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3495 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3496 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3497 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3498 | return operation->num_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3499 | #else |
| 3500 | (void) operation; |
| 3501 | return 0; |
| 3502 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3503 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3504 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3505 | } |
| 3506 | |
| 3507 | uint32_t mbedtls_psa_verify_hash_get_num_ops( |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3508 | const mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3509 | { |
| 3510 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3511 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3512 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3513 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3514 | return operation->num_ops; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3515 | #else |
| 3516 | (void) operation; |
| 3517 | return 0; |
| 3518 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3519 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3520 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3521 | } |
| 3522 | |
| 3523 | psa_status_t mbedtls_psa_sign_hash_start( |
| 3524 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3525 | const psa_key_attributes_t *attributes, const uint8_t *key_buffer, |
| 3526 | size_t key_buffer_size, psa_algorithm_t alg, |
| 3527 | const uint8_t *hash, size_t hash_length) |
| 3528 | { |
| 3529 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3530 | size_t required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3531 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3532 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3533 | return PSA_ERROR_NOT_SUPPORTED; |
| 3534 | } |
| 3535 | |
| 3536 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 3537 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3538 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3539 | |
| 3540 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3541 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3542 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3543 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3544 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
| 3545 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3546 | /* Ensure num_ops is zero'ed in case of context re-use. */ |
| 3547 | operation->num_ops = 0; |
| 3548 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3549 | /* Ensure default is set even if |
| 3550 | * mbedtls_psa_interruptible_set_max_ops() has not been called. */ |
| 3551 | mbedtls_psa_interruptible_set_max_ops( |
| 3552 | mbedtls_psa_interruptible_get_max_ops()); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3553 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3554 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 3555 | attributes->core.bits, |
| 3556 | key_buffer, |
| 3557 | key_buffer_size, |
| 3558 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3559 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3560 | if (status != PSA_SUCCESS) { |
| 3561 | return status; |
| 3562 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3563 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3564 | operation->coordinate_bytes = PSA_BITS_TO_BYTES( |
Paul Elliott | c569fc2 | 2023-02-10 13:02:54 +0000 | [diff] [blame] | 3565 | operation->ctx->grp.nbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3566 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3567 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
| 3568 | operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg); |
| 3569 | operation->alg = alg; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3570 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3571 | /* We only need to store the same length of hash as the private key size |
| 3572 | * here, it would be truncated by the internal implementation anyway. */ |
| 3573 | required_hash_length = (hash_length < operation->coordinate_bytes ? |
| 3574 | hash_length : operation->coordinate_bytes); |
| 3575 | |
Paul Elliott | ba70ad4 | 2023-02-15 18:23:53 +0000 | [diff] [blame] | 3576 | if (required_hash_length > sizeof(operation->hash)) { |
| 3577 | /* Shouldn't happen, but better safe than sorry. */ |
| 3578 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 3579 | } |
| 3580 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3581 | memcpy(operation->hash, hash, required_hash_length); |
| 3582 | operation->hash_length = required_hash_length; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3583 | |
| 3584 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3585 | |
| 3586 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3587 | (void) operation; |
| 3588 | (void) key_buffer; |
| 3589 | (void) key_buffer_size; |
| 3590 | (void) alg; |
| 3591 | (void) hash; |
| 3592 | (void) hash_length; |
| 3593 | (void) status; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3594 | (void) required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3595 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3596 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3597 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3598 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3599 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | psa_status_t mbedtls_psa_sign_hash_complete( |
| 3603 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3604 | uint8_t *signature, size_t signature_size, |
| 3605 | size_t *signature_length) |
| 3606 | { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3607 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3608 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3609 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3610 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3611 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 3612 | mbedtls_mpi r; |
| 3613 | mbedtls_mpi s; |
| 3614 | |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3615 | mbedtls_mpi_init(&r); |
| 3616 | mbedtls_mpi_init(&s); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3617 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3618 | if (signature_size < 2 * operation->coordinate_bytes) { |
| 3619 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3620 | goto exit; |
| 3621 | } |
| 3622 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3623 | if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) { |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3624 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3625 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3626 | status = mbedtls_to_psa_error( |
| 3627 | mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3628 | &r, |
| 3629 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3630 | &operation->ctx->d, |
| 3631 | operation->hash, |
| 3632 | operation->hash_length, |
| 3633 | operation->md_alg, |
| 3634 | mbedtls_psa_get_random, |
| 3635 | MBEDTLS_PSA_RANDOM_STATE, |
| 3636 | &operation->restart_ctx)); |
| 3637 | #else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3638 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3639 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3640 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3641 | } else { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3642 | status = mbedtls_to_psa_error( |
| 3643 | mbedtls_ecdsa_sign_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3644 | &r, |
| 3645 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3646 | &operation->ctx->d, |
| 3647 | operation->hash, |
| 3648 | operation->hash_length, |
| 3649 | mbedtls_psa_get_random, |
| 3650 | MBEDTLS_PSA_RANDOM_STATE, |
| 3651 | mbedtls_psa_get_random, |
| 3652 | MBEDTLS_PSA_RANDOM_STATE, |
| 3653 | &operation->restart_ctx)); |
| 3654 | } |
| 3655 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3656 | /* Hide the fact that the restart context only holds a delta of number of |
| 3657 | * ops done during the last operation, not an absolute value. */ |
| 3658 | operation->num_ops += operation->restart_ctx.ecp.ops_done; |
| 3659 | |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3660 | if (status == PSA_SUCCESS) { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3661 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3662 | mbedtls_mpi_write_binary(&r, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3663 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3664 | operation->coordinate_bytes) |
| 3665 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3666 | |
| 3667 | if (status != PSA_SUCCESS) { |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3668 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3672 | mbedtls_mpi_write_binary(&s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3673 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3674 | operation->coordinate_bytes, |
| 3675 | operation->coordinate_bytes) |
| 3676 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3677 | |
| 3678 | if (status != PSA_SUCCESS) { |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3679 | goto exit; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3682 | *signature_length = operation->coordinate_bytes * 2; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3683 | |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3684 | status = PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3685 | } |
Paul Elliott | 724bd25 | 2023-02-08 12:35:08 +0000 | [diff] [blame] | 3686 | |
| 3687 | exit: |
| 3688 | |
| 3689 | mbedtls_mpi_free(&r); |
| 3690 | mbedtls_mpi_free(&s); |
| 3691 | return status; |
| 3692 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3693 | #else |
| 3694 | |
| 3695 | (void) operation; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3696 | (void) signature; |
| 3697 | (void) signature_size; |
| 3698 | (void) signature_length; |
| 3699 | |
| 3700 | return PSA_ERROR_NOT_SUPPORTED; |
| 3701 | |
| 3702 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3703 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3704 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3705 | } |
| 3706 | |
| 3707 | psa_status_t mbedtls_psa_sign_hash_abort( |
| 3708 | mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
| 3709 | { |
| 3710 | |
| 3711 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3712 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3713 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3714 | |
| 3715 | if (operation->ctx) { |
| 3716 | mbedtls_ecdsa_free(operation->ctx); |
| 3717 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3718 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 3722 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3723 | operation->num_ops = 0; |
| 3724 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3725 | return PSA_SUCCESS; |
| 3726 | |
| 3727 | #else |
| 3728 | |
| 3729 | (void) operation; |
| 3730 | |
| 3731 | return PSA_ERROR_NOT_SUPPORTED; |
| 3732 | |
| 3733 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3734 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3735 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3736 | } |
| 3737 | |
| 3738 | psa_status_t mbedtls_psa_verify_hash_start( |
| 3739 | mbedtls_psa_verify_hash_interruptible_operation_t *operation, |
| 3740 | const psa_key_attributes_t *attributes, |
| 3741 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3742 | psa_algorithm_t alg, |
| 3743 | const uint8_t *hash, size_t hash_length, |
| 3744 | const uint8_t *signature, size_t signature_length) |
| 3745 | { |
| 3746 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3747 | size_t coordinate_bytes = 0; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3748 | size_t required_hash_length = 0; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3749 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3750 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3751 | return PSA_ERROR_NOT_SUPPORTED; |
| 3752 | } |
| 3753 | |
| 3754 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 3755 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3756 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3757 | |
| 3758 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3759 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3760 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3761 | |
Paul Elliott | a1c9409 | 2023-02-15 16:38:04 +0000 | [diff] [blame] | 3762 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
| 3763 | mbedtls_mpi_init(&operation->r); |
| 3764 | mbedtls_mpi_init(&operation->s); |
| 3765 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3766 | /* Ensure num_ops is zero'ed in case of context re-use. */ |
| 3767 | operation->num_ops = 0; |
| 3768 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3769 | /* Ensure default is set even if |
| 3770 | * mbedtls_psa_interruptible_set_max_ops() has not been called. */ |
| 3771 | mbedtls_psa_interruptible_set_max_ops( |
| 3772 | mbedtls_psa_interruptible_get_max_ops()); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3773 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3774 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 3775 | attributes->core.bits, |
| 3776 | key_buffer, |
| 3777 | key_buffer_size, |
| 3778 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3779 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3780 | if (status != PSA_SUCCESS) { |
| 3781 | return status; |
| 3782 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3783 | |
Paul Elliott | c569fc2 | 2023-02-10 13:02:54 +0000 | [diff] [blame] | 3784 | coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3785 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3786 | if (signature_length != 2 * coordinate_bytes) { |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3787 | return PSA_ERROR_INVALID_SIGNATURE; |
| 3788 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3789 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3790 | status = mbedtls_to_psa_error( |
| 3791 | mbedtls_mpi_read_binary(&operation->r, |
| 3792 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3793 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3794 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3795 | if (status != PSA_SUCCESS) { |
| 3796 | return status; |
| 3797 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3798 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3799 | status = mbedtls_to_psa_error( |
| 3800 | mbedtls_mpi_read_binary(&operation->s, |
| 3801 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3802 | coordinate_bytes, |
| 3803 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3804 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3805 | if (status != PSA_SUCCESS) { |
| 3806 | return status; |
| 3807 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3808 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 3809 | status = mbedtls_psa_ecp_load_public_part(operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3810 | |
Paul Elliott | 2c9843f | 2023-02-15 17:32:42 +0000 | [diff] [blame] | 3811 | if (status != PSA_SUCCESS) { |
| 3812 | return status; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3813 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3814 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3815 | /* We only need to store the same length of hash as the private key size |
| 3816 | * here, it would be truncated by the internal implementation anyway. */ |
| 3817 | required_hash_length = (hash_length < coordinate_bytes ? hash_length : |
| 3818 | coordinate_bytes); |
| 3819 | |
Paul Elliott | ba70ad4 | 2023-02-15 18:23:53 +0000 | [diff] [blame] | 3820 | if (required_hash_length > sizeof(operation->hash)) { |
| 3821 | /* Shouldn't happen, but better safe than sorry. */ |
| 3822 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 3823 | } |
| 3824 | |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3825 | memcpy(operation->hash, hash, required_hash_length); |
| 3826 | operation->hash_length = required_hash_length; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3827 | |
| 3828 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3829 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3830 | (void) operation; |
| 3831 | (void) key_buffer; |
| 3832 | (void) key_buffer_size; |
| 3833 | (void) alg; |
| 3834 | (void) hash; |
| 3835 | (void) hash_length; |
| 3836 | (void) signature; |
| 3837 | (void) signature_length; |
| 3838 | (void) status; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 3839 | (void) coordinate_bytes; |
Paul Elliott | 0290a76 | 2023-02-09 14:30:24 +0000 | [diff] [blame] | 3840 | (void) required_hash_length; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3841 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3842 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3843 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3844 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3845 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | psa_status_t mbedtls_psa_verify_hash_complete( |
| 3849 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 3850 | { |
| 3851 | |
| 3852 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3853 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3854 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3855 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3856 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3857 | |
| 3858 | status = mbedtls_to_psa_error( |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3859 | mbedtls_ecdsa_verify_restartable(&operation->ctx->grp, |
| 3860 | operation->hash, |
| 3861 | operation->hash_length, |
| 3862 | &operation->ctx->Q, |
| 3863 | &operation->r, |
| 3864 | &operation->s, |
| 3865 | &operation->restart_ctx)); |
| 3866 | |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 3867 | /* Hide the fact that the restart context only holds a delta of number of |
| 3868 | * ops done during the last operation, not an absolute value. */ |
| 3869 | operation->num_ops += operation->restart_ctx.ecp.ops_done; |
| 3870 | |
| 3871 | return status; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3872 | #else |
| 3873 | (void) operation; |
| 3874 | |
| 3875 | return PSA_ERROR_NOT_SUPPORTED; |
| 3876 | |
| 3877 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3878 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3879 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3880 | } |
| 3881 | |
| 3882 | psa_status_t mbedtls_psa_verify_hash_abort( |
| 3883 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 3884 | { |
| 3885 | |
| 3886 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3887 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3888 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3889 | |
| 3890 | if (operation->ctx) { |
| 3891 | mbedtls_ecdsa_free(operation->ctx); |
| 3892 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3893 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 3897 | |
Paul Elliott | 93d9ca8 | 2023-02-15 18:14:21 +0000 | [diff] [blame] | 3898 | operation->num_ops = 0; |
| 3899 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3900 | mbedtls_mpi_free(&operation->r); |
| 3901 | mbedtls_mpi_free(&operation->s); |
| 3902 | |
| 3903 | return PSA_SUCCESS; |
| 3904 | |
| 3905 | #else |
| 3906 | (void) operation; |
| 3907 | |
| 3908 | return PSA_ERROR_NOT_SUPPORTED; |
| 3909 | |
| 3910 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3911 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3912 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3913 | } |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3914 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3915 | /****************************************************************/ |
| 3916 | /* Symmetric cryptography */ |
| 3917 | /****************************************************************/ |
| 3918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3919 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 3920 | mbedtls_svc_key_id_t key, |
| 3921 | psa_algorithm_t alg, |
| 3922 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3923 | { |
| 3924 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3925 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3926 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3927 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 3928 | PSA_KEY_USAGE_ENCRYPT : |
| 3929 | PSA_KEY_USAGE_DECRYPT); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3930 | |
| 3931 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3932 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3933 | status = PSA_ERROR_BAD_STATE; |
| 3934 | goto exit; |
| 3935 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3937 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3938 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3939 | goto exit; |
| 3940 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3942 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 3943 | if (status != PSA_SUCCESS) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3944 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3945 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3946 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3947 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3948 | * 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] | 3949 | * so we only set it (in the driver wrapper) after resources have been |
| 3950 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3951 | operation->iv_set = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3952 | if (alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3953 | operation->iv_required = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3954 | } else { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3955 | operation->iv_required = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3956 | } |
| 3957 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3958 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3959 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3960 | .core = slot->attr |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3961 | }; |
| 3962 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3963 | /* Try doing the operation through a driver before using software fallback. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3964 | if (cipher_operation == MBEDTLS_ENCRYPT) { |
| 3965 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, |
| 3966 | &attributes, |
| 3967 | slot->key.data, |
| 3968 | slot->key.bytes, |
| 3969 | alg); |
| 3970 | } else { |
| 3971 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, |
| 3972 | &attributes, |
| 3973 | slot->key.data, |
| 3974 | slot->key.bytes, |
| 3975 | alg); |
| 3976 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3977 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3978 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3979 | if (status != PSA_SUCCESS) { |
| 3980 | psa_cipher_abort(operation); |
| 3981 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3983 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3985 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3986 | } |
| 3987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3988 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 3989 | mbedtls_svc_key_id_t key, |
| 3990 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3991 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3993 | } |
| 3994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3995 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 3996 | mbedtls_svc_key_id_t key, |
| 3997 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3998 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3999 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 4000 | } |
| 4001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4002 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 4003 | uint8_t *iv, |
| 4004 | size_t iv_size, |
| 4005 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4006 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4007 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4008 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 4009 | size_t default_iv_length; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4011 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4012 | status = PSA_ERROR_BAD_STATE; |
| 4013 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4014 | } |
| 4015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4016 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4017 | status = PSA_ERROR_BAD_STATE; |
| 4018 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4019 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4020 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4021 | default_iv_length = operation->default_iv_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4022 | if (iv_size < default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4023 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4024 | goto exit; |
| 4025 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4027 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4028 | status = PSA_ERROR_GENERIC_ERROR; |
| 4029 | goto exit; |
| 4030 | } |
| 4031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4032 | status = psa_generate_random(local_iv, default_iv_length); |
| 4033 | if (status != PSA_SUCCESS) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4034 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | } |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4037 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4038 | local_iv, default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4039 | |
| 4040 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4041 | if (status == PSA_SUCCESS) { |
| 4042 | memcpy(iv, local_iv, default_iv_length); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4043 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4044 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4045 | } else { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4046 | *iv_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4047 | psa_cipher_abort(operation); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4048 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4050 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4051 | } |
| 4052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4053 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 4054 | const uint8_t *iv, |
| 4055 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4056 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4057 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4059 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4060 | status = PSA_ERROR_BAD_STATE; |
| 4061 | goto exit; |
| 4062 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4064 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4065 | status = PSA_ERROR_BAD_STATE; |
| 4066 | goto exit; |
| 4067 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 4068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4069 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4070 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4071 | goto exit; |
| 4072 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4074 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4075 | iv, |
| 4076 | iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4077 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4078 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4079 | if (status == PSA_SUCCESS) { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4080 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4081 | } else { |
| 4082 | psa_cipher_abort(operation); |
| 4083 | } |
| 4084 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4085 | } |
| 4086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4087 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 4088 | const uint8_t *input, |
| 4089 | size_t input_length, |
| 4090 | uint8_t *output, |
| 4091 | size_t output_size, |
| 4092 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4093 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4094 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4096 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4097 | status = PSA_ERROR_BAD_STATE; |
| 4098 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4099 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4101 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4102 | status = PSA_ERROR_BAD_STATE; |
| 4103 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4104 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4106 | status = psa_driver_wrapper_cipher_update(operation, |
| 4107 | input, |
| 4108 | input_length, |
| 4109 | output, |
| 4110 | output_size, |
| 4111 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4112 | |
| 4113 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4114 | if (status != PSA_SUCCESS) { |
| 4115 | psa_cipher_abort(operation); |
| 4116 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4118 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4119 | } |
| 4120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4121 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
| 4122 | uint8_t *output, |
| 4123 | size_t output_size, |
| 4124 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4125 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4126 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4128 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4129 | status = PSA_ERROR_BAD_STATE; |
| 4130 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4131 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4133 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4134 | status = PSA_ERROR_BAD_STATE; |
| 4135 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4136 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4138 | status = psa_driver_wrapper_cipher_finish(operation, |
| 4139 | output, |
| 4140 | output_size, |
| 4141 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4142 | |
| 4143 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4144 | if (status == PSA_SUCCESS) { |
| 4145 | return psa_cipher_abort(operation); |
| 4146 | } else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4147 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4148 | (void) psa_cipher_abort(operation); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4150 | return status; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4151 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4152 | } |
| 4153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4154 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4155 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4156 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4157 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4158 | * in use. It's ok to call abort on such an object, and there's |
| 4159 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4160 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4161 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4163 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4164 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 4165 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4166 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4167 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4169 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4170 | } |
| 4171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4172 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 4173 | psa_algorithm_t alg, |
| 4174 | const uint8_t *input, |
| 4175 | size_t input_length, |
| 4176 | uint8_t *output, |
| 4177 | size_t output_size, |
| 4178 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4179 | { |
| 4180 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4181 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4182 | psa_key_slot_t *slot = NULL; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4183 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 4184 | size_t default_iv_length = 0; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4186 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4187 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4188 | goto exit; |
| 4189 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4191 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4192 | PSA_KEY_USAGE_ENCRYPT, |
| 4193 | alg); |
| 4194 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4195 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4196 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4197 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4198 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4199 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4200 | }; |
| 4201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4202 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
| 4203 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4204 | status = PSA_ERROR_GENERIC_ERROR; |
| 4205 | goto exit; |
| 4206 | } |
| 4207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4208 | if (default_iv_length > 0) { |
| 4209 | if (output_size < default_iv_length) { |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4210 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4211 | goto exit; |
| 4212 | } |
| 4213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4214 | status = psa_generate_random(local_iv, default_iv_length); |
| 4215 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4216 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4217 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4218 | } |
| 4219 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4220 | status = psa_driver_wrapper_cipher_encrypt( |
| 4221 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4222 | alg, local_iv, default_iv_length, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4223 | mbedtls_buffer_offset(output, default_iv_length), |
| 4224 | output_size - default_iv_length, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4225 | |
| 4226 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4227 | unlock_status = psa_unlock_key_slot(slot); |
| 4228 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4229 | status = unlock_status; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4230 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4232 | if (status == PSA_SUCCESS) { |
| 4233 | if (default_iv_length > 0) { |
| 4234 | memcpy(output, local_iv, default_iv_length); |
| 4235 | } |
| 4236 | *output_length += default_iv_length; |
| 4237 | } else { |
| 4238 | *output_length = 0; |
| 4239 | } |
| 4240 | |
| 4241 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4242 | } |
| 4243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4244 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 4245 | psa_algorithm_t alg, |
| 4246 | const uint8_t *input, |
| 4247 | size_t input_length, |
| 4248 | uint8_t *output, |
| 4249 | size_t output_size, |
| 4250 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4251 | { |
| 4252 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4253 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4254 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4256 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4257 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4258 | goto exit; |
| 4259 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4261 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4262 | PSA_KEY_USAGE_DECRYPT, |
| 4263 | alg); |
| 4264 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4265 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4266 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4267 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4268 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4269 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4270 | }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4272 | if (alg == PSA_ALG_CCM_STAR_NO_TAG && |
| 4273 | input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 4274 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4275 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4276 | } 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] | 4277 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4278 | goto exit; |
| 4279 | } |
| 4280 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4281 | status = psa_driver_wrapper_cipher_decrypt( |
| 4282 | &attributes, slot->key.data, slot->key.bytes, |
| 4283 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4284 | output, output_size, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4285 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 4286 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4287 | unlock_status = psa_unlock_key_slot(slot); |
| 4288 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4289 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4290 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4292 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4293 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4294 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4296 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4300 | /****************************************************************/ |
| 4301 | /* AEAD */ |
| 4302 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4303 | |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4304 | /* Helper function to get the base algorithm from its variants. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4305 | 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] | 4306 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4307 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | /* Helper function to perform common nonce length checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4311 | static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg, |
| 4312 | size_t nonce_length) |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4313 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4314 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4316 | switch (base_alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4317 | #if defined(PSA_WANT_ALG_GCM) |
| 4318 | case PSA_ALG_GCM: |
| 4319 | /* Not checking max nonce size here as GCM spec allows almost |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4320 | * arbitrarily large nonces. Please note that we do not generally |
| 4321 | * recommend the usage of nonces of greater length than |
| 4322 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter |
| 4323 | * size, which can then lead to collisions if you encrypt a very |
| 4324 | * large number of messages.*/ |
| 4325 | if (nonce_length != 0) { |
| 4326 | return PSA_SUCCESS; |
| 4327 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4328 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4329 | #endif /* PSA_WANT_ALG_GCM */ |
| 4330 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4331 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4332 | if (nonce_length >= 7 && nonce_length <= 13) { |
| 4333 | return PSA_SUCCESS; |
| 4334 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4335 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4336 | #endif /* PSA_WANT_ALG_CCM */ |
| 4337 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4338 | case PSA_ALG_CHACHA20_POLY1305: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4339 | if (nonce_length == 12) { |
| 4340 | return PSA_SUCCESS; |
| 4341 | } else if (nonce_length == 8) { |
| 4342 | return PSA_ERROR_NOT_SUPPORTED; |
| 4343 | } |
Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 4344 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4345 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4346 | default: |
Przemek Stekiel | 4c49927 | 2022-09-27 13:55:37 +0200 | [diff] [blame] | 4347 | (void) nonce_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4348 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4349 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4351 | return PSA_ERROR_INVALID_ARGUMENT; |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4352 | } |
| 4353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4354 | 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] | 4355 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4356 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 4357 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4358 | } |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | return PSA_SUCCESS; |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4361 | } |
| 4362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4363 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 4364 | psa_algorithm_t alg, |
| 4365 | const uint8_t *nonce, |
| 4366 | size_t nonce_length, |
| 4367 | const uint8_t *additional_data, |
| 4368 | size_t additional_data_length, |
| 4369 | const uint8_t *plaintext, |
| 4370 | size_t plaintext_length, |
| 4371 | uint8_t *ciphertext, |
| 4372 | size_t ciphertext_size, |
| 4373 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4374 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4375 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4376 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4377 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4378 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4380 | status = psa_aead_check_algorithm(alg); |
| 4381 | if (status != PSA_SUCCESS) { |
| 4382 | return status; |
| 4383 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4384 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4385 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4386 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 4387 | if (status != PSA_SUCCESS) { |
| 4388 | return status; |
| 4389 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4390 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4391 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4392 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4393 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4395 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4396 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4397 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4398 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4399 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4400 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4401 | &attributes, slot->key.data, slot->key.bytes, |
| 4402 | alg, |
| 4403 | nonce, nonce_length, |
| 4404 | additional_data, additional_data_length, |
| 4405 | plaintext, plaintext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4406 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4408 | if (status != PSA_SUCCESS && ciphertext_size != 0) { |
| 4409 | memset(ciphertext, 0, ciphertext_size); |
| 4410 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4411 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4412 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4413 | psa_unlock_key_slot(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4415 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4416 | } |
| 4417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4418 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 4419 | psa_algorithm_t alg, |
| 4420 | const uint8_t *nonce, |
| 4421 | size_t nonce_length, |
| 4422 | const uint8_t *additional_data, |
| 4423 | size_t additional_data_length, |
| 4424 | const uint8_t *ciphertext, |
| 4425 | size_t ciphertext_length, |
| 4426 | uint8_t *plaintext, |
| 4427 | size_t plaintext_size, |
| 4428 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4429 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4430 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4431 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4432 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4433 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4435 | status = psa_aead_check_algorithm(alg); |
| 4436 | if (status != PSA_SUCCESS) { |
| 4437 | return status; |
| 4438 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4439 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4440 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4441 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 4442 | if (status != PSA_SUCCESS) { |
| 4443 | return status; |
| 4444 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4445 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4446 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4447 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4448 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4450 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4451 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4452 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4453 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4454 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4455 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4456 | &attributes, slot->key.data, slot->key.bytes, |
| 4457 | alg, |
| 4458 | nonce, nonce_length, |
| 4459 | additional_data, additional_data_length, |
| 4460 | ciphertext, ciphertext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4463 | if (status != PSA_SUCCESS && plaintext_size != 0) { |
| 4464 | memset(plaintext, 0, plaintext_size); |
| 4465 | } |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4466 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4467 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4468 | psa_unlock_key_slot(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4470 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4471 | } |
| 4472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4473 | static psa_status_t psa_validate_tag_length(psa_algorithm_t alg) |
| 4474 | { |
| 4475 | const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4477 | switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) { |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4478 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4479 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4480 | /* 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] | 4481 | if (tag_len < 4 || tag_len > 16 || tag_len % 2) { |
| 4482 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4483 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4484 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4485 | #endif /* PSA_WANT_ALG_CCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4486 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4487 | #if defined(PSA_WANT_ALG_GCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4488 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4489 | /* 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] | 4490 | if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) { |
| 4491 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4492 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4493 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4494 | #endif /* PSA_WANT_ALG_GCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4495 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4496 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4497 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4498 | /* We only support the default tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4499 | if (tag_len != 16) { |
| 4500 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4501 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4502 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4503 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4504 | |
| 4505 | default: |
| 4506 | (void) tag_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4507 | return PSA_ERROR_NOT_SUPPORTED; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4508 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4509 | return PSA_SUCCESS; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4510 | } |
| 4511 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4512 | /* Set the key for a multipart authenticated operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4513 | static psa_status_t psa_aead_setup(psa_aead_operation_t *operation, |
| 4514 | int is_encrypt, |
| 4515 | mbedtls_svc_key_id_t key, |
| 4516 | psa_algorithm_t alg) |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4517 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4518 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4519 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4520 | psa_key_slot_t *slot = NULL; |
| 4521 | psa_key_usage_t key_usage = 0; |
| 4522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4523 | status = psa_aead_check_algorithm(alg); |
| 4524 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4525 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4526 | } |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4528 | if (operation->id != 0) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4529 | status = PSA_ERROR_BAD_STATE; |
| 4530 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4531 | } |
| 4532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4533 | if (operation->nonce_set || operation->lengths_set || |
| 4534 | operation->ad_started || operation->body_started) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4535 | status = PSA_ERROR_BAD_STATE; |
| 4536 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4537 | } |
| 4538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4539 | if (is_encrypt) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4540 | key_usage = PSA_KEY_USAGE_ENCRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4541 | } else { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4542 | key_usage = PSA_KEY_USAGE_DECRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4543 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4545 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage, |
| 4546 | alg); |
| 4547 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4548 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4549 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4550 | |
| 4551 | psa_key_attributes_t attributes = { |
| 4552 | .core = slot->attr |
| 4553 | }; |
| 4554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4555 | if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) { |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 4556 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4557 | } |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 4558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4559 | if (is_encrypt) { |
| 4560 | status = psa_driver_wrapper_aead_encrypt_setup(operation, |
| 4561 | &attributes, |
| 4562 | slot->key.data, |
| 4563 | slot->key.bytes, |
| 4564 | alg); |
| 4565 | } else { |
| 4566 | status = psa_driver_wrapper_aead_decrypt_setup(operation, |
| 4567 | &attributes, |
| 4568 | slot->key.data, |
| 4569 | slot->key.bytes, |
| 4570 | alg); |
| 4571 | } |
| 4572 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4573 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4574 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | operation->key_type = psa_get_key_type(&attributes); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4577 | |
| 4578 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4579 | unlock_status = psa_unlock_key_slot(slot); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4581 | if (status == PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4582 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4583 | operation->alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 4584 | operation->is_encrypt = is_encrypt; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4585 | } else { |
| 4586 | psa_aead_abort(operation); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4587 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4589 | return status; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4590 | } |
| 4591 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4592 | /* Set the key for a multipart authenticated encryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation, |
| 4594 | mbedtls_svc_key_id_t key, |
| 4595 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4596 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4597 | return psa_aead_setup(operation, 1, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | /* Set the key for a multipart authenticated decryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4601 | psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation, |
| 4602 | mbedtls_svc_key_id_t key, |
| 4603 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4604 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4605 | return psa_aead_setup(operation, 0, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | /* Generate a random nonce / IV for multipart AEAD operation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4609 | psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation, |
| 4610 | uint8_t *nonce, |
| 4611 | size_t nonce_size, |
| 4612 | size_t *nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4613 | { |
| 4614 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 4615 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; |
Paul Elliott | b91da71 | 2021-05-20 14:43:47 +0100 | [diff] [blame] | 4616 | size_t required_nonce_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4617 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4618 | *nonce_length = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4620 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4621 | status = PSA_ERROR_BAD_STATE; |
| 4622 | goto exit; |
| 4623 | } |
| 4624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4625 | if (operation->nonce_set || !operation->is_encrypt) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4626 | status = PSA_ERROR_BAD_STATE; |
| 4627 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4628 | } |
| 4629 | |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4630 | /* For CCM, this size may not be correct according to the PSA |
| 4631 | * specification. The PSA Crypto 1.0.1 specification states: |
| 4632 | * |
| 4633 | * CCM encodes the plaintext length pLen in L octets, with L the smallest |
| 4634 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. |
| 4635 | * |
| 4636 | * However this restriction that L has to be the smallest integer is not |
| 4637 | * applied in practice, and it is not implementable here since the |
| 4638 | * plaintext length may or may not be known at this time. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4639 | required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type, |
| 4640 | operation->alg); |
| 4641 | if (nonce_size < required_nonce_size) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4642 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4643 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4644 | } |
| 4645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4646 | status = psa_generate_random(local_nonce, required_nonce_size); |
| 4647 | if (status != PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4648 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4649 | } |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4651 | status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4652 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4653 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4654 | if (status == PSA_SUCCESS) { |
| 4655 | memcpy(nonce, local_nonce, required_nonce_size); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4656 | *nonce_length = required_nonce_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4657 | } else { |
| 4658 | psa_aead_abort(operation); |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 4659 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4661 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4662 | } |
| 4663 | |
| 4664 | /* Set the nonce for a multipart authenticated encryption or decryption |
| 4665 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4666 | psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation, |
| 4667 | const uint8_t *nonce, |
| 4668 | size_t nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4669 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4670 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4672 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4673 | status = PSA_ERROR_BAD_STATE; |
| 4674 | goto exit; |
| 4675 | } |
| 4676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4677 | if (operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4678 | status = PSA_ERROR_BAD_STATE; |
| 4679 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4680 | } |
| 4681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4682 | status = psa_aead_check_nonce_length(operation->alg, nonce_length); |
| 4683 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4684 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4685 | goto exit; |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 4686 | } |
Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 4687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4688 | status = psa_driver_wrapper_aead_set_nonce(operation, nonce, |
| 4689 | nonce_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4690 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4691 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4692 | if (status == PSA_SUCCESS) { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4693 | operation->nonce_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4694 | } else { |
| 4695 | psa_aead_abort(operation); |
| 4696 | } |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4698 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | /* Declare the lengths of the message and additional data for multipart AEAD. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4702 | psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation, |
| 4703 | size_t ad_length, |
| 4704 | size_t plaintext_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4705 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4706 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4708 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4709 | status = PSA_ERROR_BAD_STATE; |
| 4710 | goto exit; |
| 4711 | } |
| 4712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4713 | if (operation->lengths_set || operation->ad_started || |
| 4714 | operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4715 | status = PSA_ERROR_BAD_STATE; |
| 4716 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4717 | } |
| 4718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4719 | switch (operation->alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4720 | #if defined(PSA_WANT_ALG_GCM) |
| 4721 | case PSA_ALG_GCM: |
| 4722 | /* 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] | 4723 | * bits. Without the guard this code will generate warnings on 32bit |
| 4724 | * builds. */ |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 4725 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | if (((uint64_t) ad_length) >> 61 != 0 || |
| 4727 | ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4728 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4729 | goto exit; |
| 4730 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 4731 | #endif |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4732 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4733 | #endif /* PSA_WANT_ALG_GCM */ |
| 4734 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4735 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | if (ad_length > 0xFF00) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4737 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4738 | goto exit; |
| 4739 | } |
| 4740 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4741 | #endif /* PSA_WANT_ALG_CCM */ |
| 4742 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4743 | case PSA_ALG_CHACHA20_POLY1305: |
| 4744 | /* No length restrictions for ChaChaPoly. */ |
| 4745 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4746 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4747 | default: |
| 4748 | break; |
| 4749 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 4750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4751 | status = psa_driver_wrapper_aead_set_lengths(operation, ad_length, |
| 4752 | plaintext_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4753 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4754 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4755 | if (status == PSA_SUCCESS) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4756 | operation->ad_remaining = ad_length; |
| 4757 | operation->body_remaining = plaintext_length; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4758 | operation->lengths_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | } else { |
| 4760 | psa_aead_abort(operation); |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4761 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4763 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4764 | } |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 4765 | |
| 4766 | /* Pass additional data to an active multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4767 | psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation, |
| 4768 | const uint8_t *input, |
| 4769 | size_t input_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4770 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4771 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4773 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4774 | status = PSA_ERROR_BAD_STATE; |
| 4775 | goto exit; |
| 4776 | } |
| 4777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4778 | if (!operation->nonce_set || operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4779 | status = PSA_ERROR_BAD_STATE; |
| 4780 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4781 | } |
| 4782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4783 | if (operation->lengths_set) { |
| 4784 | if (operation->ad_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4785 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4786 | goto exit; |
| 4787 | } |
| 4788 | |
| 4789 | operation->ad_remaining -= input_length; |
| 4790 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4791 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4792 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4793 | status = PSA_ERROR_BAD_STATE; |
| 4794 | goto exit; |
| 4795 | } |
| 4796 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4798 | status = psa_driver_wrapper_aead_update_ad(operation, input, |
| 4799 | input_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4800 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4801 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4802 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4803 | operation->ad_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | } else { |
| 4805 | psa_aead_abort(operation); |
| 4806 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4808 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 4812 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4813 | psa_status_t psa_aead_update(psa_aead_operation_t *operation, |
| 4814 | const uint8_t *input, |
| 4815 | size_t input_length, |
| 4816 | uint8_t *output, |
| 4817 | size_t output_size, |
| 4818 | size_t *output_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4819 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4820 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4821 | |
| 4822 | *output_length = 0; |
| 4823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4825 | status = PSA_ERROR_BAD_STATE; |
| 4826 | goto exit; |
| 4827 | } |
| 4828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4829 | if (!operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4830 | status = PSA_ERROR_BAD_STATE; |
| 4831 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4832 | } |
| 4833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | if (operation->lengths_set) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4835 | /* Additional data length was supplied, but not all the additional |
| 4836 | data was supplied.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4837 | if (operation->ad_remaining != 0) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4838 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4839 | goto exit; |
| 4840 | } |
| 4841 | |
| 4842 | /* Too much data provided. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | if (operation->body_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4844 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4845 | goto exit; |
| 4846 | } |
| 4847 | |
| 4848 | operation->body_remaining -= input_length; |
| 4849 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4850 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4851 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4852 | status = PSA_ERROR_BAD_STATE; |
| 4853 | goto exit; |
| 4854 | } |
| 4855 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4857 | status = psa_driver_wrapper_aead_update(operation, input, input_length, |
| 4858 | output, output_size, |
| 4859 | output_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4860 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4861 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4862 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4863 | operation->body_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4864 | } else { |
| 4865 | psa_aead_abort(operation); |
| 4866 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4869 | } |
| 4870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4871 | 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] | 4872 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4873 | if (operation->id == 0 || !operation->nonce_set) { |
| 4874 | return PSA_ERROR_BAD_STATE; |
| 4875 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4877 | if (operation->lengths_set && (operation->ad_remaining != 0 || |
| 4878 | operation->body_remaining != 0)) { |
| 4879 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4880 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4882 | return PSA_SUCCESS; |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4883 | } |
| 4884 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4885 | /* Finish encrypting a message in a multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4886 | psa_status_t psa_aead_finish(psa_aead_operation_t *operation, |
| 4887 | uint8_t *ciphertext, |
| 4888 | size_t ciphertext_size, |
| 4889 | size_t *ciphertext_length, |
| 4890 | uint8_t *tag, |
| 4891 | size_t tag_size, |
| 4892 | size_t *tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4893 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4894 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4895 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4896 | *ciphertext_length = 0; |
Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 4897 | *tag_length = tag_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | status = psa_aead_final_checks(operation); |
| 4900 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4901 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4902 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4904 | if (!operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4905 | status = PSA_ERROR_BAD_STATE; |
| 4906 | goto exit; |
| 4907 | } |
| 4908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4909 | status = psa_driver_wrapper_aead_finish(operation, ciphertext, |
| 4910 | ciphertext_size, |
| 4911 | ciphertext_length, |
| 4912 | tag, tag_size, tag_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4913 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4914 | exit: |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame^] | 4915 | |
| 4916 | |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4917 | /* 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] | 4918 | * the zero tag size, make sure the tag is set to something implausible. |
| 4919 | * Even if the operation succeeds, make sure we clear the rest of the |
| 4920 | * buffer to prevent potential leakage of anything previously placed in |
| 4921 | * the same buffer.*/ |
Paul Elliott | dc42ca8 | 2023-02-24 18:11:59 +0000 | [diff] [blame^] | 4922 | psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length); |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4924 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4926 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 4930 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4931 | psa_status_t psa_aead_verify(psa_aead_operation_t *operation, |
| 4932 | uint8_t *plaintext, |
| 4933 | size_t plaintext_size, |
| 4934 | size_t *plaintext_length, |
| 4935 | const uint8_t *tag, |
| 4936 | size_t tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4937 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4938 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4939 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4940 | *plaintext_length = 0; |
| 4941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4942 | status = psa_aead_final_checks(operation); |
| 4943 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4944 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4945 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4947 | if (operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4948 | status = PSA_ERROR_BAD_STATE; |
| 4949 | goto exit; |
| 4950 | } |
| 4951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4952 | status = psa_driver_wrapper_aead_verify(operation, plaintext, |
| 4953 | plaintext_size, |
| 4954 | plaintext_length, |
| 4955 | tag, tag_length); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4956 | |
| 4957 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4958 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4960 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4961 | } |
| 4962 | |
| 4963 | /* Abort an AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4964 | psa_status_t psa_aead_abort(psa_aead_operation_t *operation) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4965 | { |
| 4966 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4968 | if (operation->id == 0) { |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4969 | /* The object has (apparently) been initialized but it is not (yet) |
| 4970 | * in use. It's ok to call abort on such an object, and there's |
| 4971 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4972 | return PSA_SUCCESS; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4973 | } |
| 4974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4975 | status = psa_driver_wrapper_aead_abort(operation); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4977 | memset(operation, 0, sizeof(*operation)); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4979 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4980 | } |
| 4981 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4982 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4983 | /* Generators */ |
| 4984 | /****************************************************************/ |
| 4985 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4986 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4987 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4988 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \ |
| 4989 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4990 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4991 | #endif /* At least one builtin KDF */ |
| 4992 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4993 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4994 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4995 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4996 | static psa_status_t psa_key_derivation_start_hmac( |
| 4997 | psa_mac_operation_t *operation, |
| 4998 | psa_algorithm_t hash_alg, |
| 4999 | const uint8_t *hmac_key, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5000 | size_t hmac_key_length) |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5001 | { |
| 5002 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5003 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5004 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 5005 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 5006 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5007 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 5008 | operation->is_sign = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5009 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 5010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5011 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 5012 | &attributes, |
| 5013 | hmac_key, hmac_key_length, |
| 5014 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5016 | psa_reset_key_attributes(&attributes); |
| 5017 | return status; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 5018 | } |
| 5019 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5020 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5021 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 5022 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 5023 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 5024 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 5025 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5026 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5027 | const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5028 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5029 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 5030 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
| 5031 | } else { |
| 5032 | return operation->alg; |
| 5033 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5034 | } |
| 5035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5036 | 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] | 5037 | { |
| 5038 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5039 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 5040 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5041 | /* The object has (apparently) been initialized but it is not |
| 5042 | * in use. It's ok to call abort on such an object, and there's |
| 5043 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5044 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5045 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5046 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5047 | mbedtls_free(operation->ctx.hkdf.info); |
| 5048 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 5049 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5050 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5051 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5052 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5053 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5054 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 5055 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5056 | if (operation->ctx.tls12_prf.secret != NULL) { |
| 5057 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret, |
| 5058 | operation->ctx.tls12_prf.secret_length); |
| 5059 | mbedtls_free(operation->ctx.tls12_prf.secret); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5060 | } |
| 5061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5062 | if (operation->ctx.tls12_prf.seed != NULL) { |
| 5063 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed, |
| 5064 | operation->ctx.tls12_prf.seed_length); |
| 5065 | mbedtls_free(operation->ctx.tls12_prf.seed); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5066 | } |
| 5067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5068 | if (operation->ctx.tls12_prf.label != NULL) { |
| 5069 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.label, |
| 5070 | operation->ctx.tls12_prf.label_length); |
| 5071 | mbedtls_free(operation->ctx.tls12_prf.label); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5072 | } |
| 5073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5074 | if (operation->ctx.tls12_prf.other_secret != NULL) { |
| 5075 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret, |
| 5076 | operation->ctx.tls12_prf.other_secret_length); |
| 5077 | mbedtls_free(operation->ctx.tls12_prf.other_secret); |
Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 5078 | } |
| 5079 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5080 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5081 | |
| 5082 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5083 | * mbedtls_platform_zeroize() in the end of this function. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5084 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5085 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5086 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5087 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5088 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5089 | mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data, |
| 5090 | sizeof(operation->ctx.tls12_ecjpake_to_pms.data)); |
| 5091 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5092 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5093 | { |
| 5094 | status = PSA_ERROR_BAD_STATE; |
| 5095 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5096 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 5097 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5098 | } |
| 5099 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5100 | 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] | 5101 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5102 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5103 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5104 | /* This is a blank key derivation operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5105 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5108 | *capacity = operation->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5109 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5110 | } |
| 5111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5112 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 5113 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5114 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5115 | if (operation->alg == 0) { |
| 5116 | return PSA_ERROR_BAD_STATE; |
| 5117 | } |
| 5118 | if (capacity > operation->capacity) { |
| 5119 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5120 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5121 | operation->capacity = capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5122 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5123 | } |
| 5124 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5125 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5126 | /* Read some bytes from an HKDF-based operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5127 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 5128 | psa_algorithm_t kdf_alg, |
| 5129 | uint8_t *output, |
| 5130 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5131 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5132 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 5133 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5134 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5135 | psa_status_t status; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5136 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5137 | 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] | 5138 | #else |
| 5139 | const uint8_t last_block = 0xff; |
| 5140 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | if (hkdf->state < HKDF_STATE_KEYED || |
| 5143 | (!hkdf->info_set |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5144 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5145 | && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5146 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | )) { |
| 5148 | return PSA_ERROR_BAD_STATE; |
| 5149 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5150 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5152 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5153 | /* Copy what remains of the current block */ |
| 5154 | uint8_t n = hash_length - hkdf->offset_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5155 | if (n > output_length) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5156 | n = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | } |
| 5158 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5159 | output += n; |
| 5160 | output_length -= n; |
| 5161 | hkdf->offset_in_block += n; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | if (output_length == 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5163 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5164 | } |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5165 | /* We can't be wanting more output after the last block, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5166 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5167 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5168 | * object was corrupted or if this function is called directly |
| 5169 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5170 | if (hkdf->block_number == last_block) { |
| 5171 | return PSA_ERROR_BAD_STATE; |
| 5172 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5173 | |
| 5174 | /* We need a new block */ |
| 5175 | ++hkdf->block_number; |
| 5176 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 5179 | hash_alg, |
| 5180 | hkdf->prk, |
| 5181 | hash_length); |
| 5182 | if (status != PSA_SUCCESS) { |
| 5183 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5184 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5185 | |
| 5186 | if (hkdf->block_number != 1) { |
| 5187 | status = psa_mac_update(&hkdf->hmac, |
| 5188 | hkdf->output_block, |
| 5189 | hash_length); |
| 5190 | if (status != PSA_SUCCESS) { |
| 5191 | return status; |
| 5192 | } |
| 5193 | } |
| 5194 | status = psa_mac_update(&hkdf->hmac, |
| 5195 | hkdf->info, |
| 5196 | hkdf->info_length); |
| 5197 | if (status != PSA_SUCCESS) { |
| 5198 | return status; |
| 5199 | } |
| 5200 | status = psa_mac_update(&hkdf->hmac, |
| 5201 | &hkdf->block_number, 1); |
| 5202 | if (status != PSA_SUCCESS) { |
| 5203 | return status; |
| 5204 | } |
| 5205 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 5206 | hkdf->output_block, |
| 5207 | sizeof(hkdf->output_block), |
| 5208 | &hmac_output_length); |
| 5209 | if (status != PSA_SUCCESS) { |
| 5210 | return status; |
| 5211 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5212 | } |
| 5213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5214 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5215 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5216 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5217 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5218 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5219 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5220 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5221 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5222 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5223 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5224 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 5225 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5226 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 5227 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5228 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5229 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5230 | /* We can't be wanting more output after block 0xff, otherwise |
| 5231 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5232 | * prevented this call. It could happen only if the operation |
| 5233 | * object was corrupted or if this function is called directly |
| 5234 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5235 | if (tls12_prf->block_number == 0xff) { |
| 5236 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 5237 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5238 | |
| 5239 | /* We need a new block */ |
| 5240 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5241 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5242 | |
| 5243 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5244 | * |
| 5245 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5246 | * |
| 5247 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5248 | * HMAC_hash(secret, A(2) + seed) + |
| 5249 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5250 | * |
| 5251 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5252 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5253 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5254 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5255 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5256 | * is currently extracted as `output_block` and where i is |
| 5257 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5258 | */ |
| 5259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5260 | status = psa_key_derivation_start_hmac(&hmac, |
| 5261 | hash_alg, |
| 5262 | tls12_prf->secret, |
| 5263 | tls12_prf->secret_length); |
| 5264 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5265 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5267 | |
| 5268 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5269 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5270 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5271 | * the variable seed and in this instance means it in the context of the |
| 5272 | * P_hash function, where seed = label + seed.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | status = psa_mac_update(&hmac, |
| 5274 | tls12_prf->label, |
| 5275 | tls12_prf->label_length); |
| 5276 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5277 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5278 | } |
| 5279 | status = psa_mac_update(&hmac, |
| 5280 | tls12_prf->seed, |
| 5281 | tls12_prf->seed_length); |
| 5282 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5283 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5284 | } |
| 5285 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5286 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5288 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5289 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5290 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5291 | } |
| 5292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5293 | status = psa_mac_sign_finish(&hmac, |
| 5294 | tls12_prf->Ai, hash_length, |
| 5295 | &hmac_output_length); |
| 5296 | if (hmac_output_length != hash_length) { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5297 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5298 | } |
| 5299 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5300 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5301 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5302 | |
| 5303 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5304 | status = psa_key_derivation_start_hmac(&hmac, |
| 5305 | hash_alg, |
| 5306 | tls12_prf->secret, |
| 5307 | tls12_prf->secret_length); |
| 5308 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5309 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5310 | } |
| 5311 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5312 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5313 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5314 | } |
| 5315 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 5316 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5317 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5318 | } |
| 5319 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 5320 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5321 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5322 | } |
| 5323 | status = psa_mac_sign_finish(&hmac, |
| 5324 | tls12_prf->output_block, hash_length, |
| 5325 | &hmac_output_length); |
| 5326 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5327 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5328 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5329 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5330 | |
| 5331 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5332 | cleanup_status = psa_mac_abort(&hmac); |
| 5333 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5334 | status = cleanup_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5335 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5337 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5338 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5339 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5340 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5341 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5342 | psa_algorithm_t alg, |
| 5343 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5344 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5345 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5346 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 5347 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5348 | psa_status_t status; |
| 5349 | uint8_t offset, length; |
| 5350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5351 | switch (tls12_prf->state) { |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5352 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 5353 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 5354 | break; |
| 5355 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 5356 | break; |
| 5357 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5358 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5359 | } |
| 5360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5361 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5362 | /* Check if we have fully processed the current block. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | if (tls12_prf->left_in_block == 0) { |
| 5364 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 5365 | alg); |
| 5366 | if (status != PSA_SUCCESS) { |
| 5367 | return status; |
| 5368 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5369 | |
| 5370 | continue; |
| 5371 | } |
| 5372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5373 | if (tls12_prf->left_in_block > output_length) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5374 | length = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5375 | } else { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5376 | length = tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5377 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5378 | |
| 5379 | offset = hash_length - tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5380 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5381 | output += length; |
| 5382 | output_length -= length; |
| 5383 | tls12_prf->left_in_block -= length; |
| 5384 | } |
| 5385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5386 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5387 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5388 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5389 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5390 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5391 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5392 | static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 5393 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
| 5394 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5395 | size_t output_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5396 | { |
| 5397 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5398 | size_t output_size = 0; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | if (output_length != 32) { |
| 5401 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5402 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data, |
| 5405 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length, |
| 5406 | &output_size); |
| 5407 | if (status != PSA_SUCCESS) { |
| 5408 | return status; |
| 5409 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5411 | if (output_size != output_length) { |
| 5412 | return PSA_ERROR_GENERIC_ERROR; |
| 5413 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5415 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5416 | } |
| 5417 | #endif |
| 5418 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5419 | psa_status_t psa_key_derivation_output_bytes( |
| 5420 | psa_key_derivation_operation_t *operation, |
| 5421 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5422 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5423 | { |
| 5424 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5425 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5427 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5428 | /* This is a blank operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5429 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5432 | if (output_length > operation->capacity) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5433 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5434 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5435 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5436 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5437 | goto exit; |
| 5438 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5439 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5440 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5441 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5442 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5443 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5444 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5445 | * output_length > 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5446 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5447 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5448 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5449 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5450 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5451 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5452 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg, |
| 5453 | output, output_length); |
| 5454 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5455 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5456 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5457 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5458 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5459 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5460 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, |
| 5461 | kdf_alg, output, |
| 5462 | output_length); |
| 5463 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5464 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5465 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5466 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5467 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5468 | status = psa_key_derivation_tls12_ecjpake_to_pms_read( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5469 | &operation->ctx.tls12_ecjpake_to_pms, output, output_length); |
| 5470 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5471 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
| 5472 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5473 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5474 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5475 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5480 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5481 | * This allows us to differentiate between exhausted operations and |
| 5482 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5483 | * operations. */ |
| 5484 | psa_algorithm_t alg = operation->alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5485 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5486 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5487 | memset(output, '!', output_length); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5488 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5489 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5490 | } |
| 5491 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5492 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | 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] | 5494 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | if (data_size >= 8) { |
| 5496 | mbedtls_des_key_set_parity(data); |
| 5497 | } |
| 5498 | if (data_size >= 16) { |
| 5499 | mbedtls_des_key_set_parity(data + 8); |
| 5500 | } |
| 5501 | if (data_size >= 24) { |
| 5502 | mbedtls_des_key_set_parity(data + 16); |
| 5503 | } |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5504 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5505 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5506 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5507 | /* |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5508 | * ECC keys on a Weierstrass elliptic curve require the generation |
| 5509 | * of a private key which is an integer |
| 5510 | * in the range [1, N - 1], where N is the boundary of the private key domain: |
| 5511 | * N is the prime p for Diffie-Hellman, or the order of the |
| 5512 | * curve’s base point for ECC. |
| 5513 | * |
| 5514 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). |
| 5515 | * This function generates the private key using the following process: |
| 5516 | * |
| 5517 | * 1. Draw a byte string of length ceiling(m/8) bytes. |
| 5518 | * 2. If m is not a multiple of 8, set the most significant |
| 5519 | * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. |
| 5520 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. |
| 5521 | * 4. If k > N - 2, discard the result and return to step 1. |
| 5522 | * 5. Output k + 1 as the private key. |
| 5523 | * |
| 5524 | * This method allows compliance to NIST standards, specifically the methods titled |
| 5525 | * Key-Pair Generation by Testing Candidates in the following publications: |
| 5526 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment |
| 5527 | * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for |
| 5528 | * Diffie-Hellman keys. |
| 5529 | * |
| 5530 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature |
| 5531 | * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. |
| 5532 | * |
| 5533 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 5534 | * always NULL. |
| 5535 | */ |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5536 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 5537 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 5538 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 5539 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 5540 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5541 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5542 | psa_key_slot_t *slot, |
| 5543 | size_t bits, |
| 5544 | psa_key_derivation_operation_t *operation, |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5545 | uint8_t **data |
| 5546 | ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5547 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5548 | unsigned key_out_of_range = 1; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5549 | mbedtls_mpi k; |
| 5550 | mbedtls_mpi diff_N_2; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5551 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 5552 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5554 | mbedtls_mpi_init(&k); |
| 5555 | mbedtls_mpi_init(&diff_N_2); |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 5556 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5557 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5558 | slot->attr.type); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5559 | mbedtls_ecp_group_id grp_id = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5560 | mbedtls_ecc_group_of_psa(curve, bits, 0); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5563 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 5564 | goto cleanup; |
| 5565 | } |
| 5566 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5567 | mbedtls_ecp_group ecp_group; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5568 | mbedtls_ecp_group_init(&ecp_group); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5570 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5571 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5572 | /* N is the boundary of the private key domain (ecp_group.N). */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5573 | /* Let m be the bit size of N. */ |
| 5574 | size_t m = ecp_group.nbits; |
| 5575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | size_t m_bytes = PSA_BITS_TO_BYTES(m); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5577 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5578 | /* Calculate N - 2 - it will be needed later. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5579 | 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] | 5580 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5581 | /* Note: This function is always called with *data == NULL and it |
| 5582 | * allocates memory for the data buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5583 | *data = mbedtls_calloc(1, m_bytes); |
| 5584 | if (*data == NULL) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5585 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5586 | goto cleanup; |
| 5587 | } |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5589 | while (key_out_of_range) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5590 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5592 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5593 | } |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5594 | |
| 5595 | /* 2. If m is not a multiple of 8 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5596 | if (m % 8 != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5597 | /* Set the most significant |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5598 | * (8 * ceiling(m/8) - m) bits of the first byte in |
| 5599 | * the string to zero. |
| 5600 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5601 | uint8_t clear_bit_mask = (1 << (m % 8)) - 1; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5602 | (*data)[0] &= clear_bit_mask; |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5603 | } |
| 5604 | |
| 5605 | /* 3. Convert the string to integer k by decoding it as a |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5606 | * big-endian byte string. |
| 5607 | */ |
| 5608 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5609 | |
| 5610 | /* 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] | 5611 | * Result of comparison is returned. When it indicates error |
| 5612 | * then this function is called again. |
| 5613 | */ |
| 5614 | 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] | 5615 | } |
| 5616 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5617 | /* 5. Output k + 1 as the private key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1)); |
| 5619 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5620 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5621 | if (ret != 0) { |
| 5622 | status = mbedtls_to_psa_error(ret); |
| 5623 | } |
| 5624 | if (status != PSA_SUCCESS) { |
| 5625 | mbedtls_free(*data); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5626 | *data = NULL; |
| 5627 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5628 | mbedtls_mpi_free(&k); |
| 5629 | mbedtls_mpi_free(&diff_N_2); |
| 5630 | return status; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5631 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5632 | |
| 5633 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length |
| 5634 | * is determined by the curve, and sets the mandatory bits accordingly. That is: |
| 5635 | * |
| 5636 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): |
| 5637 | * draw a 32-byte string and process it as specified in |
| 5638 | * Elliptic Curves for Security [RFC7748] §5. |
| 5639 | * |
| 5640 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): |
| 5641 | * draw a 56-byte string and process it as specified in [RFC7748] §5. |
| 5642 | * |
| 5643 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 5644 | * always NULL. |
| 5645 | */ |
| 5646 | |
| 5647 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 5648 | size_t bits, |
| 5649 | psa_key_derivation_operation_t *operation, |
| 5650 | uint8_t **data |
| 5651 | ) |
| 5652 | { |
| 5653 | size_t output_length; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 5654 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5656 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5657 | case 255: |
| 5658 | output_length = 32; |
| 5659 | break; |
| 5660 | case 448: |
| 5661 | output_length = 56; |
| 5662 | break; |
| 5663 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5664 | return PSA_ERROR_INVALID_ARGUMENT; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5665 | break; |
| 5666 | } |
| 5667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5668 | *data = mbedtls_calloc(1, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5670 | if (*data == NULL) { |
| 5671 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5672 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5674 | status = psa_key_derivation_output_bytes(operation, *data, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5677 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5678 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5680 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5681 | case 255: |
| 5682 | (*data)[0] &= 248; |
| 5683 | (*data)[31] &= 127; |
| 5684 | (*data)[31] |= 64; |
| 5685 | break; |
| 5686 | case 448: |
| 5687 | (*data)[0] &= 252; |
| 5688 | (*data)[55] |= 128; |
| 5689 | break; |
| 5690 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5691 | return PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5692 | break; |
| 5693 | } |
| 5694 | |
| 5695 | return status; |
| 5696 | } |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5697 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 5698 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 5699 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 5700 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 5701 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5702 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5703 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5704 | psa_key_slot_t *slot, |
| 5705 | size_t bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5706 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5707 | { |
| 5708 | uint8_t *data = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5709 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5710 | size_t storage_size = bytes; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 5711 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
| 5714 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5715 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5716 | |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5717 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 5718 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 5719 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 5720 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 5721 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5722 | if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) { |
| 5723 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type); |
| 5724 | if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5725 | /* Weierstrass elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5726 | status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data); |
| 5727 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5728 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5729 | } |
| 5730 | } else { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5731 | /* Montgomery elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5732 | status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data); |
| 5733 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5734 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | } |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5736 | } |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 5737 | } else |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5738 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 5739 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 5740 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 5741 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 5742 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5743 | if (key_type_is_raw_bytes(slot->attr.type)) { |
| 5744 | if (bits % 8 != 0) { |
| 5745 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5746 | } |
| 5747 | data = mbedtls_calloc(1, bytes); |
| 5748 | if (data == NULL) { |
| 5749 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5750 | } |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5752 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 5753 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5754 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5755 | } |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5756 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5757 | if (slot->attr.type == PSA_KEY_TYPE_DES) { |
| 5758 | psa_des_set_key_parity(data, bytes); |
| 5759 | } |
Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 5760 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5761 | } else { |
| 5762 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5763 | } |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5764 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5765 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5766 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5767 | .core = slot->attr |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5768 | }; |
| 5769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5770 | if (psa_key_lifetime_is_external(attributes.core.lifetime)) { |
| 5771 | status = psa_driver_wrapper_get_key_buffer_size(&attributes, |
| 5772 | &storage_size); |
| 5773 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5774 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5775 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5776 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 5778 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5779 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5782 | status = psa_driver_wrapper_import_key(&attributes, |
| 5783 | data, bytes, |
| 5784 | slot->key.data, |
| 5785 | slot->key.bytes, |
| 5786 | &slot->key.bytes, &bits); |
| 5787 | if (bits != slot->attr.bits) { |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5788 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5790 | |
| 5791 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | mbedtls_free(data); |
| 5793 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5794 | } |
| 5795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5796 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 5797 | psa_key_derivation_operation_t *operation, |
| 5798 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5799 | { |
| 5800 | psa_status_t status; |
| 5801 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5802 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5803 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5804 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5805 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5806 | /* Reject any attempt to create a zero-length key so that we don't |
| 5807 | * 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] | 5808 | if (psa_get_key_bits(attributes) == 0) { |
| 5809 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5810 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5812 | if (operation->alg == PSA_ALG_NONE) { |
| 5813 | return PSA_ERROR_BAD_STATE; |
| 5814 | } |
Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 5815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5816 | if (!operation->can_output_key) { |
| 5817 | return PSA_ERROR_NOT_PERMITTED; |
| 5818 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5820 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, |
| 5821 | &slot, &driver); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5822 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5823 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5824 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5825 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5826 | } |
| 5827 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5828 | if (status == PSA_SUCCESS) { |
| 5829 | status = psa_generate_derived_key_internal(slot, |
| 5830 | attributes->core.bits, |
| 5831 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5832 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5833 | if (status == PSA_SUCCESS) { |
| 5834 | status = psa_finish_key_creation(slot, driver, key); |
| 5835 | } |
| 5836 | if (status != PSA_SUCCESS) { |
| 5837 | psa_fail_key_creation(slot, driver); |
| 5838 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5840 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5841 | } |
| 5842 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5843 | |
| 5844 | |
| 5845 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5846 | /* Key derivation */ |
| 5847 | /****************************************************************/ |
| 5848 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5849 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5850 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5851 | { |
| 5852 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5853 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 5854 | return 1; |
| 5855 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5856 | #endif |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5857 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 5859 | return 1; |
| 5860 | } |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5861 | #endif |
| 5862 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5863 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 5864 | return 1; |
| 5865 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5866 | #endif |
| 5867 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5868 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 5869 | return 1; |
| 5870 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5871 | #endif |
| 5872 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5873 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5874 | return 1; |
| 5875 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5876 | #endif |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5877 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5878 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5879 | return 1; |
| 5880 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5881 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5882 | return 0; |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5883 | } |
| 5884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5885 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5886 | { |
| 5887 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | psa_status_t status = psa_hash_setup(&operation, alg); |
| 5889 | psa_hash_abort(&operation); |
| 5890 | return status; |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5891 | } |
| 5892 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5893 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5894 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5895 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5896 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5897 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5898 | * initialisers for this union leave some bytes unspecified.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5899 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5900 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5901 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5902 | if (!is_kdf_alg_supported(kdf_alg)) { |
| 5903 | return PSA_ERROR_NOT_SUPPORTED; |
| 5904 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5905 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5906 | /* All currently supported key derivation algorithms (apart from |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5907 | * ecjpake to pms) are based on a hash algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5908 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 5909 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 5910 | if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5911 | if (hash_size == 0) { |
| 5912 | return PSA_ERROR_NOT_SUPPORTED; |
| 5913 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5914 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5915 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 5916 | * we might fail later, which is somewhat unfriendly and potentially |
| 5917 | * risk-prone. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5918 | psa_status_t status = psa_hash_try_support(hash_alg); |
| 5919 | if (status != PSA_SUCCESS) { |
| 5920 | return status; |
| 5921 | } |
| 5922 | } else { |
| 5923 | hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5924 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5926 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5927 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && |
| 5928 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 5929 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5930 | } |
Andrzej Kurek | 7763829 | 2022-09-16 12:24:52 -0400 | [diff] [blame] | 5931 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5932 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) || |
| 5934 | (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5935 | operation->capacity = hash_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5936 | } else |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5937 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT || |
| 5938 | MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5939 | operation->capacity = 255 * hash_size; |
| 5940 | return PSA_SUCCESS; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5941 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5943 | 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] | 5944 | { |
| 5945 | #if defined(PSA_WANT_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5946 | if (alg == PSA_ALG_ECDH) { |
| 5947 | return PSA_SUCCESS; |
| 5948 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5949 | #endif |
| 5950 | (void) alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5951 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5952 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 5953 | |
| 5954 | static int psa_key_derivation_allows_free_form_secret_input( |
| 5955 | psa_algorithm_t kdf_alg) |
| 5956 | { |
| 5957 | #if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5958 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5959 | return 0; |
| 5960 | } |
| 5961 | #endif |
| 5962 | (void) kdf_alg; |
| 5963 | return 1; |
| 5964 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5965 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5967 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 5968 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5969 | { |
| 5970 | psa_status_t status; |
| 5971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5972 | if (operation->alg != 0) { |
| 5973 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5974 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5976 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 5977 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5978 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 5979 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 5980 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 5981 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); |
| 5982 | status = psa_key_agreement_try_support(ka_alg); |
| 5983 | if (status != PSA_SUCCESS) { |
| 5984 | return status; |
| 5985 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 5986 | if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) { |
| 5987 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5988 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5989 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 5990 | #else |
| 5991 | return PSA_ERROR_NOT_SUPPORTED; |
| 5992 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 5993 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 5994 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 5995 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 5996 | #else |
| 5997 | return PSA_ERROR_NOT_SUPPORTED; |
| 5998 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 5999 | } else { |
| 6000 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6001 | } |
| 6002 | |
| 6003 | if (status == PSA_SUCCESS) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6004 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6005 | } |
| 6006 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6007 | } |
| 6008 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6009 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6010 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 6011 | psa_algorithm_t kdf_alg, |
| 6012 | psa_key_derivation_step_t step, |
| 6013 | const uint8_t *data, |
| 6014 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6015 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6016 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6017 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6018 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6019 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6020 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6021 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 6022 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6023 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6024 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6025 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6026 | return PSA_ERROR_BAD_STATE; |
| 6027 | } else { |
| 6028 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6029 | hash_alg, |
| 6030 | data, data_length); |
| 6031 | if (status != PSA_SUCCESS) { |
| 6032 | return status; |
| 6033 | } |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6034 | hkdf->state = HKDF_STATE_STARTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | return PSA_SUCCESS; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6036 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6037 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6038 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6039 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6040 | /* We shouldn't be in different state as HKDF_EXPAND only allows |
| 6041 | * two inputs: SECRET (this case) and INFO which does not modify |
| 6042 | * the state. It could happen only if the hkdf |
| 6043 | * object was corrupted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6044 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6045 | return PSA_ERROR_BAD_STATE; |
| 6046 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6047 | |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6048 | /* Allow only input that fits expected prk size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | if (data_length != PSA_HASH_LENGTH(hash_alg)) { |
| 6050 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6051 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6053 | memcpy(hkdf->prk, data, data_length); |
| 6054 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6055 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6056 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6057 | /* HKDF: If no salt was provided, use an empty salt. |
| 6058 | * HKDF-EXTRACT: salt is mandatory. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6059 | if (hkdf->state == HKDF_STATE_INIT) { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6060 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6061 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6062 | return PSA_ERROR_BAD_STATE; |
| 6063 | } |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6064 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6065 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6066 | hash_alg, |
| 6067 | NULL, 0); |
| 6068 | if (status != PSA_SUCCESS) { |
| 6069 | return status; |
| 6070 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6071 | hkdf->state = HKDF_STATE_STARTED; |
| 6072 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6073 | if (hkdf->state != HKDF_STATE_STARTED) { |
| 6074 | return PSA_ERROR_BAD_STATE; |
| 6075 | } |
| 6076 | status = psa_mac_update(&hkdf->hmac, |
| 6077 | data, data_length); |
| 6078 | if (status != PSA_SUCCESS) { |
| 6079 | return status; |
| 6080 | } |
| 6081 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 6082 | hkdf->prk, |
| 6083 | sizeof(hkdf->prk), |
| 6084 | &data_length); |
| 6085 | if (status != PSA_SUCCESS) { |
| 6086 | return status; |
| 6087 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6088 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6089 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 6090 | hkdf->state = HKDF_STATE_KEYED; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6091 | hkdf->block_number = 0; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6092 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6093 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6094 | /* The only block of output is the PRK. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6095 | memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg)); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6096 | hkdf->offset_in_block = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6098 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6099 | { |
| 6100 | /* Block 0 is empty, and the next block will be |
| 6101 | * generated by psa_key_derivation_hkdf_read(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6102 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6103 | } |
| 6104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6106 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6107 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6109 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6110 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6111 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6112 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6113 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) && |
| 6114 | hkdf->state == HKDF_STATE_INIT) { |
| 6115 | return PSA_ERROR_BAD_STATE; |
| 6116 | } |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6117 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6118 | if (hkdf->state == HKDF_STATE_OUTPUT) { |
| 6119 | return PSA_ERROR_BAD_STATE; |
| 6120 | } |
| 6121 | if (hkdf->info_set) { |
| 6122 | return PSA_ERROR_BAD_STATE; |
| 6123 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6124 | hkdf->info_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6125 | if (data_length != 0) { |
| 6126 | hkdf->info = mbedtls_calloc(1, data_length); |
| 6127 | if (hkdf->info == NULL) { |
| 6128 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6129 | } |
| 6130 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6131 | } |
| 6132 | hkdf->info_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6134 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6135 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6136 | } |
| 6137 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6138 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6139 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6140 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 6141 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6142 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 6143 | const uint8_t *data, |
| 6144 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6145 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6146 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { |
| 6147 | return PSA_ERROR_BAD_STATE; |
| 6148 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | if (data_length != 0) { |
| 6151 | prf->seed = mbedtls_calloc(1, data_length); |
| 6152 | if (prf->seed == NULL) { |
| 6153 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6154 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6156 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6157 | prf->seed_length = data_length; |
| 6158 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6159 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6160 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6163 | } |
| 6164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6165 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 6166 | const uint8_t *data, |
| 6167 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6168 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6169 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET && |
| 6170 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6171 | return PSA_ERROR_BAD_STATE; |
| 6172 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6174 | if (data_length != 0) { |
| 6175 | prf->secret = mbedtls_calloc(1, data_length); |
| 6176 | if (prf->secret == NULL) { |
| 6177 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6178 | } |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6180 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6181 | prf->secret_length = data_length; |
| 6182 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6183 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6184 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6186 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6187 | } |
| 6188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6189 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 6190 | const uint8_t *data, |
| 6191 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6192 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { |
| 6194 | return PSA_ERROR_BAD_STATE; |
| 6195 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | if (data_length != 0) { |
| 6198 | prf->label = mbedtls_calloc(1, data_length); |
| 6199 | if (prf->label == NULL) { |
| 6200 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6201 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6203 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6204 | prf->label_length = data_length; |
| 6205 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6206 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6207 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6209 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6210 | } |
| 6211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6212 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 6213 | psa_key_derivation_step_t step, |
| 6214 | const uint8_t *data, |
| 6215 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6216 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6217 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6218 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6219 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6220 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6221 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6222 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6223 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6224 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6225 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6226 | } |
| 6227 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6228 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 6229 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 6230 | |
| 6231 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6232 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 6233 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6234 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6236 | { |
| 6237 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6238 | const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? |
| 6239 | 4 + data_length + prf->other_secret_length : |
| 6240 | 4 + 2 * data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6242 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { |
| 6243 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6244 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6246 | uint8_t *pms = mbedtls_calloc(1, pms_len); |
| 6247 | if (pms == NULL) { |
| 6248 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6249 | } |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6250 | uint8_t *cur = pms; |
| 6251 | |
| 6252 | /* pure-PSK: |
| 6253 | * Quoting RFC 4279, Section 2: |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6254 | * |
| 6255 | * The premaster secret is formed as follows: if the PSK is N octets |
| 6256 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 6257 | * uint16 with the value N, and the PSK itself. |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6258 | * |
| 6259 | * mixed-PSK: |
| 6260 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as |
| 6261 | * follows: concatenate a uint16 with the length of the other secret, |
| 6262 | * the other secret itself, uint16 with the length of PSK, and the |
| 6263 | * PSK itself. |
| 6264 | * For details please check: |
| 6265 | * - RFC 4279, Section 4 for the definition of RSA-PSK, |
| 6266 | * - RFC 4279, Section 3 for the definition of DHE-PSK, |
| 6267 | * - RFC 5489 for the definition of ECDHE-PSK. |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6268 | */ |
| 6269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6271 | *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length); |
| 6272 | *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length); |
| 6273 | if (prf->other_secret_length != 0) { |
| 6274 | memcpy(cur, prf->other_secret, prf->other_secret_length); |
| 6275 | mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length); |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 6276 | cur += prf->other_secret_length; |
| 6277 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6278 | } else { |
| 6279 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 6280 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 6281 | memset(cur, 0, data_length); |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6282 | cur += data_length; |
| 6283 | } |
| 6284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6285 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 6286 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 6287 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6288 | cur += data_length; |
| 6289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6290 | status = psa_tls12_prf_set_key(prf, pms, cur - pms); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6292 | mbedtls_platform_zeroize(pms, pms_len); |
| 6293 | mbedtls_free(pms); |
| 6294 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6295 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6296 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6297 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( |
| 6298 | psa_tls12_prf_key_derivation_t *prf, |
| 6299 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6300 | size_t data_length) |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6301 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { |
| 6303 | return PSA_ERROR_BAD_STATE; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6304 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6305 | |
| 6306 | if (data_length != 0) { |
| 6307 | prf->other_secret = mbedtls_calloc(1, data_length); |
| 6308 | if (prf->other_secret == NULL) { |
| 6309 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6310 | } |
| 6311 | |
| 6312 | memcpy(prf->other_secret, data, data_length); |
| 6313 | prf->other_secret_length = data_length; |
| 6314 | } else { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6315 | prf->other_secret_length = 0; |
| 6316 | } |
| 6317 | |
| 6318 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; |
| 6319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6320 | return PSA_SUCCESS; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6321 | } |
| 6322 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6323 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 6324 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6325 | psa_key_derivation_step_t step, |
| 6326 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6327 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6328 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6329 | switch (step) { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6330 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6331 | return psa_tls12_prf_psk_to_ms_set_key(prf, |
| 6332 | data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6333 | break; |
| 6334 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | return psa_tls12_prf_psk_to_ms_set_other_key(prf, |
| 6336 | data, |
| 6337 | data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6338 | break; |
| 6339 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6340 | return psa_tls12_prf_input(prf, step, data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6341 | break; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6342 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6343 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6344 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6345 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6346 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6347 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 6348 | static psa_status_t psa_tls12_ecjpake_to_pms_input( |
| 6349 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 6350 | psa_key_derivation_step_t step, |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6351 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6352 | size_t data_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6353 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE || |
| 6355 | step != PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 6356 | return PSA_ERROR_INVALID_ARGUMENT; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 6357 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6358 | |
| 6359 | /* Check if the passed point is in an uncompressed form */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6360 | if (data[0] != 0x04) { |
| 6361 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6362 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6363 | |
| 6364 | /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6365 | memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6368 | } |
| 6369 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6370 | /** Check whether the given key type is acceptable for the given |
| 6371 | * input step of a key derivation. |
| 6372 | * |
| 6373 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 6374 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 6375 | * Both secret and non-secret inputs can alternatively have the type |
| 6376 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 6377 | * that the input was passed as a buffer rather than via a key object. |
| 6378 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6379 | static int psa_key_derivation_check_input_type( |
| 6380 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6382 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6384 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6385 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 6386 | return PSA_SUCCESS; |
| 6387 | } |
| 6388 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 6389 | return PSA_SUCCESS; |
| 6390 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6391 | break; |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 6392 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6393 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 6394 | return PSA_SUCCESS; |
| 6395 | } |
| 6396 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 6397 | return PSA_SUCCESS; |
| 6398 | } |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 6399 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6400 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 6401 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 6402 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 6403 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6404 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
| 6405 | return PSA_SUCCESS; |
| 6406 | } |
| 6407 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 6408 | return PSA_SUCCESS; |
| 6409 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6410 | break; |
| 6411 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6412 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6413 | } |
| 6414 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6415 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6416 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6417 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6418 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6419 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6420 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6421 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6422 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6423 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6425 | status = psa_key_derivation_check_input_type(step, key_type); |
| 6426 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6427 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6428 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6429 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6430 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6431 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 6432 | status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg, |
| 6433 | step, data, data_length); |
| 6434 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6435 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6436 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6437 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 6438 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, |
| 6439 | step, data, data_length); |
| 6440 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6441 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 6442 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6443 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 6444 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, |
| 6445 | step, data, data_length); |
| 6446 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6447 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6448 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6449 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6450 | status = psa_tls12_ecjpake_to_pms_input( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6451 | &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length); |
| 6452 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6453 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6454 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6455 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 6456 | (void) data; |
| 6457 | (void) data_length; |
| 6458 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6459 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6460 | } |
| 6461 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6462 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6463 | if (status != PSA_SUCCESS) { |
| 6464 | psa_key_derivation_abort(operation); |
| 6465 | } |
| 6466 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6467 | } |
| 6468 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6469 | psa_status_t psa_key_derivation_input_bytes( |
| 6470 | psa_key_derivation_operation_t *operation, |
| 6471 | psa_key_derivation_step_t step, |
| 6472 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6473 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6474 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6475 | return psa_key_derivation_input_internal(operation, step, |
| 6476 | PSA_KEY_TYPE_NONE, |
| 6477 | data, data_length); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6478 | } |
| 6479 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6480 | psa_status_t psa_key_derivation_input_key( |
| 6481 | psa_key_derivation_operation_t *operation, |
| 6482 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6483 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6484 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6485 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6486 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6487 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6488 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6489 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6490 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 6491 | if (status != PSA_SUCCESS) { |
| 6492 | psa_key_derivation_abort(operation); |
| 6493 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6494 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6495 | |
| 6496 | /* Passing a key object as a SECRET input unlocks the permission |
| 6497 | * to output to a key object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6498 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6499 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6500 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6502 | status = psa_key_derivation_input_internal(operation, |
| 6503 | step, slot->attr.type, |
| 6504 | slot->key.data, |
| 6505 | slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6507 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6509 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6510 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6511 | |
| 6512 | |
| 6513 | |
| 6514 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6515 | /* Key agreement */ |
| 6516 | /****************************************************************/ |
| 6517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6518 | psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes, |
| 6519 | const uint8_t *key_buffer, |
| 6520 | size_t key_buffer_size, |
| 6521 | psa_algorithm_t alg, |
| 6522 | const uint8_t *peer_key, |
| 6523 | size_t peer_key_length, |
| 6524 | uint8_t *shared_secret, |
| 6525 | size_t shared_secret_size, |
| 6526 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6527 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6528 | switch (alg) { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6529 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6530 | case PSA_ALG_ECDH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6531 | return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer, |
| 6532 | key_buffer_size, alg, |
| 6533 | peer_key, peer_key_length, |
| 6534 | shared_secret, |
| 6535 | shared_secret_size, |
| 6536 | shared_secret_length); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6537 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6538 | default: |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6539 | (void) attributes; |
| 6540 | (void) key_buffer; |
| 6541 | (void) key_buffer_size; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 6542 | (void) peer_key; |
| 6543 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6544 | (void) shared_secret; |
| 6545 | (void) shared_secret_size; |
| 6546 | (void) shared_secret_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6547 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6548 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6549 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6550 | |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6551 | /** Internal function for raw key agreement |
| 6552 | * Calls the driver wrapper which will hand off key agreement task |
Aditya Deshpande | 40c05cc | 2022-10-14 16:41:40 +0100 | [diff] [blame] | 6553 | * to the driver's implementation if a driver is present. |
| 6554 | * Fallback specified in the driver wrapper is built-in raw key agreement |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6555 | * (psa_key_agreement_raw_builtin). |
| 6556 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6557 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 6558 | psa_key_slot_t *private_key, |
| 6559 | const uint8_t *peer_key, |
| 6560 | size_t peer_key_length, |
| 6561 | uint8_t *shared_secret, |
| 6562 | size_t shared_secret_size, |
| 6563 | size_t *shared_secret_length) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6564 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6565 | if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 6566 | return PSA_ERROR_NOT_SUPPORTED; |
| 6567 | } |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6568 | |
| 6569 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6570 | .core = private_key->attr |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6571 | }; |
| 6572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6573 | return psa_driver_wrapper_key_agreement(&attributes, |
| 6574 | private_key->key.data, |
| 6575 | private_key->key.bytes, alg, |
| 6576 | peer_key, peer_key_length, |
| 6577 | shared_secret, |
| 6578 | shared_secret_size, |
| 6579 | shared_secret_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6580 | } |
| 6581 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6582 | /* 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] | 6583 | * to potentially free embedded data structures and wipe confidential data. |
| 6584 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6585 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 6586 | psa_key_derivation_step_t step, |
| 6587 | psa_key_slot_t *private_key, |
| 6588 | const uint8_t *peer_key, |
| 6589 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6590 | { |
| 6591 | psa_status_t status; |
Aditya Deshpande | 2f7fd76 | 2022-11-22 11:10:34 +0000 | [diff] [blame] | 6592 | uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE]; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6593 | size_t shared_secret_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6594 | 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] | 6595 | |
| 6596 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 6597 | * secret. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6598 | status = psa_key_agreement_raw_internal(ka_alg, |
| 6599 | private_key, |
| 6600 | peer_key, peer_key_length, |
| 6601 | shared_secret, |
| 6602 | sizeof(shared_secret), |
| 6603 | &shared_secret_length); |
| 6604 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6605 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6606 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6607 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 6608 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6609 | * the shared secret. A shared secret is permitted wherever a key |
| 6610 | * of type DERIVE is permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6611 | status = psa_key_derivation_input_internal(operation, step, |
| 6612 | PSA_KEY_TYPE_DERIVE, |
| 6613 | shared_secret, |
| 6614 | shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6615 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6616 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 6617 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6618 | } |
| 6619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6620 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 6621 | psa_key_derivation_step_t step, |
| 6622 | mbedtls_svc_key_id_t private_key, |
| 6623 | const uint8_t *peer_key, |
| 6624 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6625 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6626 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6627 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 6628 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6630 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 6631 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6632 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6633 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6634 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 6635 | if (status != PSA_SUCCESS) { |
| 6636 | return status; |
| 6637 | } |
| 6638 | status = psa_key_agreement_internal(operation, step, |
| 6639 | slot, |
| 6640 | peer_key, peer_key_length); |
| 6641 | if (status != PSA_SUCCESS) { |
| 6642 | psa_key_derivation_abort(operation); |
| 6643 | } else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6644 | /* If a private key has been added as SECRET, we allow the derived |
| 6645 | * key material to be used as a key in PSA Crypto. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6646 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6647 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6648 | } |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6649 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6651 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6653 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6654 | } |
| 6655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6656 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 6657 | mbedtls_svc_key_id_t private_key, |
| 6658 | const uint8_t *peer_key, |
| 6659 | size_t peer_key_length, |
| 6660 | uint8_t *output, |
| 6661 | size_t output_size, |
| 6662 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6663 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6664 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6665 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6666 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6668 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6669 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6670 | goto exit; |
| 6671 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6672 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6673 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 6674 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6675 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6676 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6677 | |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 6678 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 6679 | * for the output size. The PSA specification only guarantees that this |
| 6680 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 6681 | * but it might be nice to allow smaller buffers if the output fits. |
| 6682 | * At the time of writing this comment, with only ECDH implemented, |
| 6683 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 6684 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 6685 | * be exact for it as well. */ |
| 6686 | size_t expected_length = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6687 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); |
| 6688 | if (output_size < expected_length) { |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 6689 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 6690 | goto exit; |
| 6691 | } |
| 6692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6693 | status = psa_key_agreement_raw_internal(alg, slot, |
| 6694 | peer_key, peer_key_length, |
| 6695 | output, output_size, |
| 6696 | output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6697 | |
| 6698 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6699 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6700 | /* If an error happens and is not handled properly, the output |
| 6701 | * may be used as a key to protect sensitive data. Arrange for such |
| 6702 | * a key to be random, which is likely to result in decryption or |
| 6703 | * verification errors. This is better than filling the buffer with |
| 6704 | * some constant data such as zeros, which would result in the data |
| 6705 | * being protected with a reproducible, easily knowable key. |
| 6706 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6707 | psa_generate_random(output, output_size); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6708 | *output_length = output_size; |
| 6709 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6711 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6713 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6714 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6715 | |
| 6716 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6717 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6718 | /****************************************************************/ |
| 6719 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6720 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6721 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6722 | /** Initialize the PSA random generator. |
| 6723 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6724 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6725 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6726 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6727 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6728 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6729 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6730 | /* Set default configuration if |
| 6731 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6732 | if (rng->entropy_init == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6733 | rng->entropy_init = mbedtls_entropy_init; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6734 | } |
| 6735 | if (rng->entropy_free == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6736 | rng->entropy_free = mbedtls_entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6737 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6739 | rng->entropy_init(&rng->entropy); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6740 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6741 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6742 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6743 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6744 | mbedtls_entropy_add_source(&rng->entropy, |
| 6745 | mbedtls_nv_seed_poll, NULL, |
| 6746 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6747 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6748 | #endif |
| 6749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6750 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6751 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | /** Deinitialize the PSA random generator. |
| 6755 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6756 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6757 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6758 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6759 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6760 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6761 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 6762 | rng->entropy_free(&rng->entropy); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6763 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | /** Seed the PSA random generator. |
| 6767 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6768 | 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] | 6769 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6770 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6771 | /* Do nothing: the external RNG seeds itself. */ |
| 6772 | (void) rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6773 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6774 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6775 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6776 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, |
| 6777 | drbg_seed, sizeof(drbg_seed) - 1); |
| 6778 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6779 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6780 | } |
| 6781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6782 | psa_status_t psa_generate_random(uint8_t *output, |
| 6783 | size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6784 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6785 | GUARD_MODULE_INITIALIZED; |
| 6786 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6787 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6788 | |
| 6789 | size_t output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6790 | psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng, |
| 6791 | output, output_size, |
| 6792 | &output_length); |
| 6793 | if (status != PSA_SUCCESS) { |
| 6794 | return status; |
| 6795 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6796 | /* Breaking up a request into smaller chunks is currently not supported |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6797 | * for the external RNG interface. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6798 | if (output_length != output_size) { |
| 6799 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 6800 | } |
| 6801 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6802 | |
| 6803 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6805 | while (output_size > 0) { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6806 | size_t request_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6807 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6808 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6809 | output_size); |
| 6810 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 6811 | output, request_size); |
| 6812 | if (ret != 0) { |
| 6813 | return mbedtls_to_psa_error(ret); |
| 6814 | } |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6815 | output_size -= request_size; |
| 6816 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6817 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6818 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6819 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6820 | } |
| 6821 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6822 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6823 | * |
| 6824 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6825 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6826 | * PSA API doesn't support passing an explicit state. |
| 6827 | * |
| 6828 | * In the non-external case, psa_generate_random() calls an |
| 6829 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6830 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6831 | * instead of doing this back-and-forth between the PSA API and the |
| 6832 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6833 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6834 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6835 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6836 | int mbedtls_psa_get_random(void *p_rng, |
| 6837 | unsigned char *output, |
| 6838 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6839 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6840 | /* This function takes a pointer to the RNG state because that's what |
| 6841 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6842 | * its own state internally and doesn't let the caller access that state. |
| 6843 | * So we just ignore the state parameter, and in practice we'll pass |
| 6844 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6845 | (void) p_rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6846 | psa_status_t status = psa_generate_random(output, output_size); |
| 6847 | if (status == PSA_SUCCESS) { |
| 6848 | return 0; |
| 6849 | } else { |
| 6850 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 6851 | } |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6852 | } |
| 6853 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6854 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6855 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Chris Jones | ea0a865 | 2021-03-09 19:11:19 +0000 | [diff] [blame] | 6856 | #include "entropy_poll.h" |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6858 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
| 6859 | size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6860 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6861 | if (global_data.initialized) { |
| 6862 | return PSA_ERROR_NOT_PERMITTED; |
| 6863 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6865 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 6866 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 6867 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { |
| 6868 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6869 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6871 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6872 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6873 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6874 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6875 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6876 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6877 | * \param type The key type |
| 6878 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6879 | * |
| 6880 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6881 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6882 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 6883 | * The size in bits of the key is not valid. |
| 6884 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 6885 | * The type and/or the size in bits of the key or the combination of |
| 6886 | * the two is not supported. |
| 6887 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6888 | 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] | 6889 | psa_key_type_t type, size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6890 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6891 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6893 | if (key_type_is_raw_bytes(type)) { |
| 6894 | status = psa_validate_unstructured_key_bit_size(type, bits); |
| 6895 | if (status != PSA_SUCCESS) { |
| 6896 | return status; |
| 6897 | } |
| 6898 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6899 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6900 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 6901 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { |
| 6902 | return PSA_ERROR_NOT_SUPPORTED; |
| 6903 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6904 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6905 | /* Accept only byte-aligned keys, for the same reasons as |
| 6906 | * in psa_import_rsa_key(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6907 | if (bits % 8 != 0) { |
| 6908 | return PSA_ERROR_NOT_SUPPORTED; |
| 6909 | } |
| 6910 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6911 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6912 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6913 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6914 | 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] | 6915 | /* To avoid empty block, return successfully here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6916 | return PSA_SUCCESS; |
| 6917 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6918 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6919 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6920 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6921 | } |
| 6922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6923 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6924 | } |
| 6925 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 6926 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6927 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6928 | 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] | 6929 | { |
| 6930 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6931 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6933 | if ((attributes->domain_parameters == NULL) && |
| 6934 | (attributes->domain_parameters_size != 0)) { |
| 6935 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6936 | } |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6938 | if (key_type_is_raw_bytes(type)) { |
| 6939 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 6940 | if (status != PSA_SUCCESS) { |
| 6941 | return status; |
| 6942 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6943 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6944 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6945 | if (type == PSA_KEY_TYPE_DES) { |
| 6946 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 6947 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 6948 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6949 | } else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6950 | |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6951 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 6952 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6953 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 6954 | return mbedtls_psa_rsa_generate_key(attributes, |
| 6955 | key_buffer, |
| 6956 | key_buffer_size, |
| 6957 | key_buffer_length); |
| 6958 | } else |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6959 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 6960 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6961 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6962 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6963 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 6964 | return mbedtls_psa_ecp_generate_key(attributes, |
| 6965 | key_buffer, |
| 6966 | key_buffer_size, |
| 6967 | key_buffer_length); |
| 6968 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6969 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6970 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6971 | (void) key_buffer_length; |
| 6972 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6973 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6975 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6976 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6978 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 6979 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6980 | { |
| 6981 | psa_status_t status; |
| 6982 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6983 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6984 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6985 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6986 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6987 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6988 | /* Reject any attempt to create a zero-length key so that we don't |
| 6989 | * 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] | 6990 | if (psa_get_key_bits(attributes) == 0) { |
| 6991 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6992 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6993 | |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6994 | /* Reject any attempt to create a public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6995 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { |
| 6996 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6997 | } |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6999 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 7000 | &slot, &driver); |
| 7001 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7002 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7003 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7004 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7005 | /* 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] | 7006 | * storage ( thus not in the case of generating a key in a secure element |
| 7007 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 7008 | * buffer to hold the generated key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7009 | if (slot->key.data == NULL) { |
| 7010 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 7011 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7012 | status = psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7013 | attributes->core.type, attributes->core.bits); |
| 7014 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7015 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7016 | } |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7017 | |
| 7018 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7019 | attributes->core.type, |
| 7020 | attributes->core.bits); |
| 7021 | } else { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7022 | status = psa_driver_wrapper_get_key_buffer_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7023 | attributes, &key_buffer_size); |
| 7024 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 7025 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7026 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7027 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7029 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 7030 | if (status != PSA_SUCCESS) { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7031 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7032 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7033 | } |
| 7034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7035 | status = psa_driver_wrapper_generate_key(attributes, |
| 7036 | slot->key.data, slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7038 | if (status != PSA_SUCCESS) { |
| 7039 | psa_remove_key_data_from_memory(slot); |
| 7040 | } |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 7041 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7042 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7043 | if (status == PSA_SUCCESS) { |
| 7044 | status = psa_finish_key_creation(slot, driver, key); |
| 7045 | } |
| 7046 | if (status != PSA_SUCCESS) { |
| 7047 | psa_fail_key_creation(slot, driver); |
| 7048 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7050 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7051 | } |
| 7052 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7053 | /****************************************************************/ |
| 7054 | /* Module setup */ |
| 7055 | /****************************************************************/ |
| 7056 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7057 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7058 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7059 | void (* entropy_init)(mbedtls_entropy_context *ctx), |
| 7060 | void (* entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7061 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7062 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 7063 | return PSA_ERROR_BAD_STATE; |
| 7064 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7065 | global_data.rng.entropy_init = entropy_init; |
| 7066 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7067 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7068 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7069 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7071 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7072 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7073 | psa_wipe_all_key_slots(); |
| 7074 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 7075 | mbedtls_psa_random_free(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7076 | } |
| 7077 | /* Wipe all remaining data, including configuration. |
| 7078 | * In particular, this sets all state indicator to the value |
| 7079 | * indicating "uninitialized". */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7080 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 7081 | |
| 7082 | /* Terminate drivers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7083 | psa_driver_wrapper_free(); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7084 | } |
| 7085 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7086 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 7087 | /** Recover a transaction that was interrupted by a power failure. |
| 7088 | * |
| 7089 | * This function is called during initialization, before psa_crypto_init() |
| 7090 | * returns. If this function returns a failure status, the initialization |
| 7091 | * fails. |
| 7092 | */ |
| 7093 | static psa_status_t psa_crypto_recover_transaction( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7094 | const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7095 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7096 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7097 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 7098 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7099 | /* TODO - fall through to the failure case until this |
| 7100 | * is implemented. |
| 7101 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 7102 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7103 | default: |
| 7104 | /* We found an unsupported transaction in the storage. |
| 7105 | * We don't know what state the storage is in. Give up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7106 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7107 | } |
| 7108 | } |
| 7109 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 7110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7111 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7112 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 7113 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7114 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7115 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7116 | if (global_data.initialized != 0) { |
| 7117 | return PSA_SUCCESS; |
| 7118 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7119 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7120 | /* Initialize and seed the random generator. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7121 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7122 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7123 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 7124 | if (status != PSA_SUCCESS) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7125 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7126 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7127 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7129 | status = psa_initialize_key_slots(); |
| 7130 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 7131 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7132 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7133 | |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 7134 | /* Init drivers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7135 | status = psa_driver_wrapper_init(); |
| 7136 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 7137 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7138 | } |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 7139 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 7140 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7141 | status = psa_crypto_load_transaction(); |
| 7142 | if (status == PSA_SUCCESS) { |
| 7143 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 7144 | if (status != PSA_SUCCESS) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7145 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7146 | } |
| 7147 | status = psa_crypto_stop_transaction(); |
| 7148 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 7149 | /* There's no transaction to complete. It's all good. */ |
| 7150 | status = PSA_SUCCESS; |
| 7151 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 7152 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 7153 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7154 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7155 | global_data.initialized = 1; |
| 7156 | |
| 7157 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7158 | if (status != PSA_SUCCESS) { |
| 7159 | mbedtls_psa_crypto_free(); |
| 7160 | } |
| 7161 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7162 | } |
| 7163 | |
| 7164 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |