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 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 325 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 326 | |
| 327 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 328 | /****************************************************************/ |
| 329 | /* Key management */ |
| 330 | /****************************************************************/ |
| 331 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 332 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 333 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 334 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 335 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 336 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, |
| 338 | size_t bits, |
| 339 | int bits_is_sloppy) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 340 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 341 | switch (curve) { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 342 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 344 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 345 | case 192: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 346 | return MBEDTLS_ECP_DP_SECP192R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 347 | #endif |
| 348 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 349 | case 224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | return MBEDTLS_ECP_DP_SECP224R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 351 | #endif |
| 352 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 353 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | return MBEDTLS_ECP_DP_SECP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 355 | #endif |
| 356 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 357 | case 384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | return MBEDTLS_ECP_DP_SECP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 359 | #endif |
| 360 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 361 | case 521: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 362 | return MBEDTLS_ECP_DP_SECP521R1; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 363 | case 528: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | if (bits_is_sloppy) { |
| 365 | return MBEDTLS_ECP_DP_SECP521R1; |
| 366 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 367 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 368 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 369 | } |
| 370 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 371 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 372 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 374 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 375 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | return MBEDTLS_ECP_DP_BP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 377 | #endif |
| 378 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 379 | case 384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | return MBEDTLS_ECP_DP_BP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 381 | #endif |
| 382 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 383 | case 512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | return MBEDTLS_ECP_DP_BP512R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 385 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 386 | } |
| 387 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 388 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 389 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 391 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 392 | case 255: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | return MBEDTLS_ECP_DP_CURVE25519; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 394 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | if (bits_is_sloppy) { |
| 396 | return MBEDTLS_ECP_DP_CURVE25519; |
| 397 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 398 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 399 | #endif |
| 400 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 401 | case 448: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | return MBEDTLS_ECP_DP_CURVE448; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 403 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 404 | } |
| 405 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 406 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 407 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 408 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 409 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 410 | case 192: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | return MBEDTLS_ECP_DP_SECP192K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 412 | #endif |
| 413 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 414 | case 224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | return MBEDTLS_ECP_DP_SECP224K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 416 | #endif |
| 417 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 418 | case 256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 419 | return MBEDTLS_ECP_DP_SECP256K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 420 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 421 | } |
| 422 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 423 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 424 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 425 | (void) bits_is_sloppy; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | return MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 427 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 428 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | f467d63 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 429 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 430 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 431 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 432 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type, |
| 435 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 436 | { |
| 437 | /* Check that the bit size is acceptable for the key type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 439 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 440 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 441 | case PSA_KEY_TYPE_DERIVE: |
Neil Armstrong | 4b5710f | 2022-05-25 11:30:27 +0200 | [diff] [blame] | 442 | case PSA_KEY_TYPE_PASSWORD: |
| 443 | case PSA_KEY_TYPE_PASSWORD_HASH: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 444 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 445 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | case PSA_KEY_TYPE_AES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | if (bits != 128 && bits != 192 && bits != 256) { |
| 448 | return PSA_ERROR_INVALID_ARGUMENT; |
| 449 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 450 | break; |
| 451 | #endif |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 452 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 453 | case PSA_KEY_TYPE_ARIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | if (bits != 128 && bits != 192 && bits != 256) { |
| 455 | return PSA_ERROR_INVALID_ARGUMENT; |
| 456 | } |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 457 | break; |
| 458 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 459 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 460 | case PSA_KEY_TYPE_CAMELLIA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 461 | if (bits != 128 && bits != 192 && bits != 256) { |
| 462 | return PSA_ERROR_INVALID_ARGUMENT; |
| 463 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 464 | break; |
| 465 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 466 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 467 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | if (bits != 64 && bits != 128 && bits != 192) { |
| 469 | return PSA_ERROR_INVALID_ARGUMENT; |
| 470 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 471 | break; |
| 472 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 473 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 474 | case PSA_KEY_TYPE_CHACHA20: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | if (bits != 256) { |
| 476 | return PSA_ERROR_INVALID_ARGUMENT; |
| 477 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 478 | break; |
| 479 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 480 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 482 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | if (bits % 8 != 0) { |
| 484 | return PSA_ERROR_INVALID_ARGUMENT; |
| 485 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 490 | /** Check whether a given key type is valid for use with a given MAC algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 491 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 492 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 493 | * when called with the validated \p algorithm and \p key_type is well-defined. |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 494 | * |
| 495 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 496 | * \param[in] key_type The key type of the key to be used with the |
| 497 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 498 | * |
| 499 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 500 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 501 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 502 | * The \p key_type is not valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 503 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 504 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 505 | psa_algorithm_t algorithm, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 507 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 508 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 509 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 510 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 511 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 512 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 515 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 516 | * key. */ |
| 517 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 518 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 519 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 520 | * the block length (larger than 1) for block ciphers. */ |
| 521 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 522 | return PSA_SUCCESS; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 528 | } |
| 529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 531 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 532 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | if (slot->key.data != NULL) { |
| 534 | return PSA_ERROR_ALREADY_EXISTS; |
| 535 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 538 | if (slot->key.data == NULL) { |
| 539 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 540 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 541 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 542 | slot->key.bytes = buffer_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 547 | const uint8_t *data, |
| 548 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 549 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 550 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 551 | data_length); |
| 552 | if (status != PSA_SUCCESS) { |
| 553 | return status; |
| 554 | } |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | memcpy(slot->key.data, data, data_length); |
| 557 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 558 | } |
| 559 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 560 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 561 | const psa_key_attributes_t *attributes, |
| 562 | const uint8_t *data, size_t data_length, |
| 563 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | size_t *key_buffer_length, size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 565 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 566 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 567 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 568 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 569 | /* zero-length keys are never supported. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | if (data_length == 0) { |
| 571 | return PSA_ERROR_NOT_SUPPORTED; |
| 572 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | if (key_type_is_raw_bytes(type)) { |
| 575 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | status = psa_validate_unstructured_key_bit_size(attributes->core.type, |
| 578 | *bits); |
| 579 | if (status != PSA_SUCCESS) { |
| 580 | return status; |
| 581 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 582 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 583 | /* Copy the key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 585 | *key_buffer_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 586 | (void) key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | return PSA_SUCCESS; |
| 589 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 590 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 591 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 592 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 593 | return mbedtls_psa_ecp_import_key(attributes, |
| 594 | data, data_length, |
| 595 | key_buffer, key_buffer_size, |
| 596 | key_buffer_length, |
| 597 | bits); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 598 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 599 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 600 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 601 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 603 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 604 | return mbedtls_psa_rsa_import_key(attributes, |
| 605 | data, data_length, |
| 606 | key_buffer, key_buffer_size, |
| 607 | key_buffer_length, |
| 608 | bits); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 609 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 610 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 611 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 612 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 614 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 617 | /** Calculate the intersection of two algorithm usage policies. |
| 618 | * |
| 619 | * Return 0 (which allows no operation) on incompatibility. |
| 620 | */ |
| 621 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 622 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 623 | psa_algorithm_t alg1, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 624 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 625 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 626 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | if (alg1 == alg2) { |
| 628 | return alg1; |
| 629 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 630 | /* If the policies are from the same hash-and-sign family, check |
| 631 | * if one is a wildcard. If so the other has the specific algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 632 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 633 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 634 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 635 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 636 | return alg2; |
| 637 | } |
| 638 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 639 | return alg1; |
| 640 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 641 | } |
| 642 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 643 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 644 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 645 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 646 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 647 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 648 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 649 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 650 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 651 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 652 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 653 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 654 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 655 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 656 | alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 657 | } |
| 658 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 659 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 660 | (alg1_len <= alg2_len)) { |
| 661 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 662 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 663 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 664 | (alg2_len <= alg1_len)) { |
| 665 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | /* If the policies are from the same MAC family, check whether one |
| 669 | * of them is a minimum-MAC-length policy. Calculate the most |
| 670 | * restrictive tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 671 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 672 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 673 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 674 | /* Validate the combination of key type and algorithm. Since the base |
| 675 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 677 | return 0; |
| 678 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 679 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 680 | /* Get the (exact or at-least) output lengths for both sides of the |
| 681 | * requested intersection. None of the currently supported algorithms |
| 682 | * have an output length dependent on the actual key size, so setting it |
| 683 | * to a bogus value of 0 is currently OK. |
| 684 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 685 | * Note that for at-least-this-length wildcard algorithms, the output |
| 686 | * length is set to the shortest allowed length, which allows us to |
| 687 | * calculate the most restrictive tag length for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 688 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 689 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 690 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 691 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 692 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 694 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 695 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 696 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 697 | |
| 698 | /* If only one is an at-least-this-length policy, the intersection would |
| 699 | * be the other (fixed-length) policy as long as said fixed length is |
| 700 | * equal to or larger than the shortest allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 701 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 702 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 703 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 704 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 705 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 706 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 707 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 708 | /* If none of them are wildcards, check whether they define the same tag |
| 709 | * length. This is still possible here when one is default-length and |
| 710 | * the other specific-length. Ensure to always return the |
| 711 | * specific-length version for the intersection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 712 | if (alg1_len == alg2_len) { |
| 713 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 714 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 715 | } |
| 716 | /* If the policies are incompatible, allow nothing. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 717 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 718 | } |
| 719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 720 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 721 | psa_algorithm_t policy_alg, |
| 722 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 723 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 724 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 725 | if (requested_alg == policy_alg) { |
| 726 | return 1; |
| 727 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 728 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 729 | * and requested_alg is the same hash-and-sign family with any hash, |
| 730 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 731 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 732 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 733 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 734 | (requested_alg & ~PSA_ALG_HASH_MASK); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 735 | } |
| 736 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 737 | * the requested algorithm, check the requested tag length to be |
| 738 | * equal-length or longer than the wildcard-specified length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 739 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 740 | PSA_ALG_IS_AEAD(requested_alg) && |
| 741 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 742 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 743 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 744 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 745 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 746 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 747 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 748 | * algorithm, check whether their MAC lengths are compatible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 750 | PSA_ALG_IS_MAC(requested_alg) && |
| 751 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 752 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 753 | /* Validate the combination of key type and algorithm. Since the policy |
| 754 | * and requested algorithms are the same, we only need this once. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 756 | return 0; |
| 757 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 758 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 759 | /* Get both the requested output length for the algorithm which is to be |
| 760 | * verified, and the default output length for the base algorithm. |
| 761 | * Note that none of the currently supported algorithms have an output |
| 762 | * length dependent on actual key size, so setting it to a bogus value |
| 763 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 764 | size_t requested_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 765 | key_type, 0, requested_alg); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 766 | size_t default_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 767 | key_type, 0, |
| 768 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 769 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 770 | /* If the policy is default-length, only allow an algorithm with |
| 771 | * a declared exact-length matching the default. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 773 | return requested_output_length == default_output_length; |
| 774 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 775 | |
| 776 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 777 | * length exactly matches the default length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 778 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 779 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 780 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 781 | } |
| 782 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 783 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 784 | * check for the requested MAC length to be equal to or longer than the |
| 785 | * minimum allowed length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 786 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 787 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 788 | requested_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 789 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 790 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 791 | /* 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] | 792 | * a key derivation with that key agreement should also be allowed. This |
| 793 | * behaviour is expected to be defined in a future specification version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 794 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 795 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 796 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 797 | policy_alg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 798 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 799 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 803 | /** Test whether a policy permits an algorithm. |
| 804 | * |
| 805 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 806 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 807 | * \note This function requires providing the key type for which the policy is |
| 808 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 809 | * have different properties depending on what kind of cipher it is |
| 810 | * combined with. |
| 811 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 812 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 813 | * allowed by the \p policy. |
| 814 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 815 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 816 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 817 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 818 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 819 | psa_key_type_t key_type, |
| 820 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 821 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 822 | /* '0' is not a valid algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 823 | if (alg == 0) { |
| 824 | return PSA_ERROR_INVALID_ARGUMENT; |
| 825 | } |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 826 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 827 | /* A requested algorithm cannot be a wildcard. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 828 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 829 | return PSA_ERROR_INVALID_ARGUMENT; |
| 830 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 832 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 833 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 834 | return PSA_SUCCESS; |
| 835 | } else { |
| 836 | return PSA_ERROR_NOT_PERMITTED; |
| 837 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 838 | } |
| 839 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 840 | /** Restrict a key policy based on a constraint. |
| 841 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 842 | * \note This function requires providing the key type for which the policy is |
| 843 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 844 | * have different properties depending on what kind of cipher it is |
| 845 | * combined with. |
| 846 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 847 | * \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] | 848 | * \param[in,out] policy The policy to restrict. |
| 849 | * \param[in] constraint The policy constraint to apply. |
| 850 | * |
| 851 | * \retval #PSA_SUCCESS |
| 852 | * \c *policy contains the intersection of the original value of |
| 853 | * \c *policy and \c *constraint. |
| 854 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 855 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 856 | * \c *policy is unchanged. |
| 857 | */ |
| 858 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 859 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 860 | psa_key_policy_t *policy, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 861 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 862 | { |
| 863 | psa_algorithm_t intersection_alg = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 864 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 865 | constraint->alg); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 866 | psa_algorithm_t intersection_alg2 = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 867 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 868 | constraint->alg2); |
| 869 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 870 | return PSA_ERROR_INVALID_ARGUMENT; |
| 871 | } |
| 872 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 873 | return PSA_ERROR_INVALID_ARGUMENT; |
| 874 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 875 | policy->usage &= constraint->usage; |
| 876 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 877 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 878 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 881 | psa_status_t psa_get_and_lock_key_slot_with_policy( |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 882 | mbedtls_svc_key_id_t key, |
| 883 | psa_key_slot_t **p_slot, |
| 884 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 886 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 887 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 888 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 890 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 891 | if (status != PSA_SUCCESS) { |
| 892 | return status; |
| 893 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 894 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 895 | |
| 896 | /* Enforce that usage policy for the key slot contains all the flags |
| 897 | * required by the usage parameter. There is one exception: public |
| 898 | * keys can always be exported, so we treat public key objects as |
| 899 | * if they had the export flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 900 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 901 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 902 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 904 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 905 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 906 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 907 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 908 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 909 | /* Enforce that the usage policy permits the requested algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 910 | if (alg != 0) { |
| 911 | status = psa_key_policy_permits(&slot->attr.policy, |
| 912 | slot->attr.type, |
| 913 | alg); |
| 914 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 915 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 916 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 917 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 919 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 920 | |
| 921 | error: |
| 922 | *p_slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 923 | psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 926 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 927 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 928 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 929 | * |
| 930 | * 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] | 931 | * available, as opposed to a key in a secure element and/or to be used |
| 932 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 933 | * |
Ronald Cron | ddae0f5 | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 934 | * This is a temporary function that may be used instead of |
| 935 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 936 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 937 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 938 | * On success, the returned key slot is locked. It is the responsibility of the |
| 939 | * 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] | 940 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 941 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 942 | mbedtls_svc_key_id_t key, |
| 943 | psa_key_slot_t **p_slot, |
| 944 | psa_key_usage_t usage, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 945 | psa_algorithm_t alg) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 946 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 948 | usage, alg); |
| 949 | if (status != PSA_SUCCESS) { |
| 950 | return status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 951 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 953 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
| 954 | psa_unlock_key_slot(*p_slot); |
| 955 | *p_slot = NULL; |
| 956 | return PSA_ERROR_NOT_SUPPORTED; |
| 957 | } |
| 958 | |
| 959 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 960 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | 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] | 963 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 964 | /* Data pointer will always be either a valid pointer or NULL in an |
| 965 | * initialized slot, so we can just free it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 966 | if (slot->key.data != NULL) { |
| 967 | mbedtls_platform_zeroize(slot->key.data, slot->key.bytes); |
| 968 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 970 | mbedtls_free(slot->key.data); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 971 | slot->key.data = NULL; |
| 972 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 974 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 977 | /** Completely wipe a slot in memory, including its policy. |
| 978 | * Persistent storage is not affected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 980 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 981 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 983 | /* |
| 984 | * As the return error code may not be handled in case of multiple errors, |
| 985 | * do our best to report an unexpected lock counter. Assert with |
| 986 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one: |
| 987 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 988 | * function is called as part of the execution of a test suite, the |
| 989 | * execution of the test suite is stopped in error if the assertion fails. |
| 990 | */ |
| 991 | if (slot->lock_count != 1) { |
| 992 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 993 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 994 | } |
| 995 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 996 | /* Multipart operations may still be using the key. This is safe |
| 997 | * because all multipart operation objects are independent from |
| 998 | * the key slot: if they need to access the key after the setup |
| 999 | * phase, they have a copy of the key. Note that this means that |
| 1000 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1001 | /* At this point, key material and other type-specific content has |
| 1002 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1003 | * zeroize because the metadata is not particularly sensitive. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1004 | memset(slot, 0, sizeof(*slot)); |
| 1005 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1008 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1009 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1010 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1011 | psa_status_t status; /* status of the last operation */ |
| 1012 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1013 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1014 | psa_se_drv_table_entry_t *driver; |
| 1015 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1017 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1018 | return PSA_SUCCESS; |
| 1019 | } |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1020 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1021 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1022 | * 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] | 1023 | * key, this will load the key description from persistent memory if not |
| 1024 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1025 | * the key is operated by an SE or not and this information is needed by |
| 1026 | * the current implementation. |
| 1027 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1028 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1029 | if (status != PSA_SUCCESS) { |
| 1030 | return status; |
| 1031 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1032 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1033 | /* |
| 1034 | * If the key slot containing the key description is under access by the |
| 1035 | * library (apart from the present access), the key cannot be destroyed |
| 1036 | * yet. For the time being, just return in error. Eventually (to be |
| 1037 | * implemented), the key should be destroyed when all accesses have |
| 1038 | * stopped. |
| 1039 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1040 | if (slot->lock_count > 1) { |
| 1041 | psa_unlock_key_slot(slot); |
| 1042 | return PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1043 | } |
| 1044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1045 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1046 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1047 | * if we attempt it, depending on whether the key is merely read-only |
| 1048 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1049 | * Just do the best we can, which is to wipe the copy in memory |
| 1050 | * (done in this function's cleanup code). */ |
| 1051 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1052 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1053 | } |
| 1054 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1055 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1056 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1057 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1058 | /* For a key in a secure element, we need to do three things: |
| 1059 | * remove the key file in internal storage, destroy the |
| 1060 | * key inside the secure element, and update the driver's |
| 1061 | * persistent data. Start a transaction that will encompass these |
| 1062 | * three actions. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1064 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1065 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1066 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1067 | status = psa_crypto_save_transaction(); |
| 1068 | if (status != PSA_SUCCESS) { |
| 1069 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1070 | /* We should still try to destroy the key in the secure |
| 1071 | * element and the key metadata in storage. This is especially |
| 1072 | * important if the error is that the storage is full. |
| 1073 | * But how to do it exactly without risking an inconsistent |
| 1074 | * state after a reset? |
| 1075 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1076 | */ |
| 1077 | overall_status = status; |
| 1078 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | status = psa_destroy_se_key(driver, |
| 1082 | psa_key_slot_get_slot_number(slot)); |
| 1083 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1084 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1086 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1087 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1088 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1089 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1090 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1091 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1092 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1093 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1094 | } |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1095 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1096 | /* TODO: other slots may have a copy of the same key. We should |
| 1097 | * invalidate them. |
| 1098 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1099 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1100 | } |
| 1101 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1102 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1103 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1104 | if (driver != NULL) { |
| 1105 | status = psa_save_se_persistent_data(driver); |
| 1106 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1107 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1108 | } |
| 1109 | status = psa_crypto_stop_transaction(); |
| 1110 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1111 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1112 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1113 | } |
| 1114 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1115 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1116 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1117 | status = psa_wipe_key_slot(slot); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1118 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1119 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1120 | overall_status = status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1121 | } |
| 1122 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1125 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1126 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1127 | static psa_status_t psa_get_rsa_public_exponent( |
| 1128 | const mbedtls_rsa_context *rsa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1129 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1130 | { |
| 1131 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1132 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1133 | uint8_t *buffer = NULL; |
| 1134 | size_t buflen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1135 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1137 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1138 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1139 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1140 | } |
| 1141 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1142 | /* It's the default value, which is reported as an empty string, |
| 1143 | * so there's nothing to do. */ |
| 1144 | goto exit; |
| 1145 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1147 | buflen = mbedtls_mpi_size(&mpi); |
| 1148 | buffer = mbedtls_calloc(1, buflen); |
| 1149 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1150 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1151 | goto exit; |
| 1152 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1154 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1155 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1156 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1157 | attributes->domain_parameters = buffer; |
| 1158 | attributes->domain_parameters_size = buflen; |
| 1159 | |
| 1160 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1161 | mbedtls_mpi_free(&mpi); |
| 1162 | if (ret != 0) { |
| 1163 | mbedtls_free(buffer); |
| 1164 | } |
| 1165 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1166 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1167 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1168 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1169 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1170 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1171 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1172 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1173 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1174 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1175 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1176 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1177 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1181 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1182 | if (status != PSA_SUCCESS) { |
| 1183 | return status; |
| 1184 | } |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1185 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1186 | attributes->core = slot->attr; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1187 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1188 | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1189 | |
| 1190 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1191 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1192 | psa_set_key_slot_number(attributes, |
| 1193 | psa_key_slot_get_slot_number(slot)); |
| 1194 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1195 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1197 | switch (slot->attr.type) { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1198 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1199 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1200 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1201 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1202 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1203 | * is not yet implemented. |
| 1204 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1205 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1206 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1207 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1208 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1209 | status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1210 | slot->attr.type, |
| 1211 | slot->key.data, |
| 1212 | slot->key.bytes, |
| 1213 | &rsa); |
| 1214 | if (status != PSA_SUCCESS) { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1215 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1216 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1218 | status = psa_get_rsa_public_exponent(rsa, |
| 1219 | attributes); |
| 1220 | mbedtls_rsa_free(rsa); |
| 1221 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1222 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1223 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1224 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1225 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1226 | default: |
| 1227 | /* Nothing else to do. */ |
| 1228 | break; |
| 1229 | } |
| 1230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1231 | if (status != PSA_SUCCESS) { |
| 1232 | psa_reset_key_attributes(attributes); |
| 1233 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1237 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1240 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1241 | psa_status_t psa_get_key_slot_number( |
| 1242 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1243 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1244 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1245 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1246 | *slot_number = attributes->slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1247 | return PSA_SUCCESS; |
| 1248 | } else { |
| 1249 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1250 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1251 | } |
| 1252 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1254 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1255 | size_t key_buffer_size, |
| 1256 | uint8_t *data, |
| 1257 | size_t data_size, |
| 1258 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1259 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1260 | if (key_buffer_size > data_size) { |
| 1261 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1262 | } |
| 1263 | memcpy(data, key_buffer, key_buffer_size); |
| 1264 | memset(data + key_buffer_size, 0, |
| 1265 | data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1266 | *data_length = key_buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1267 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1268 | } |
| 1269 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1270 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1271 | const psa_key_attributes_t *attributes, |
| 1272 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1273 | uint8_t *data, size_t data_size, size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1274 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1275 | psa_key_type_t type = attributes->core.type; |
| 1276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1277 | if (key_type_is_raw_bytes(type) || |
| 1278 | PSA_KEY_TYPE_IS_RSA(type) || |
| 1279 | PSA_KEY_TYPE_IS_ECC(type)) { |
| 1280 | return psa_export_key_buffer_internal( |
| 1281 | key_buffer, key_buffer_size, |
| 1282 | data, data_size, data_length); |
| 1283 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1284 | /* This shouldn't happen in the reference implementation, but |
| 1285 | it is valid for a special-purpose implementation to omit |
| 1286 | support for exporting certain key types. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1287 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1291 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
| 1292 | uint8_t *data, |
| 1293 | size_t data_size, |
| 1294 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1295 | { |
| 1296 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1297 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1298 | psa_key_slot_t *slot; |
| 1299 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1300 | /* Reject a zero-length output buffer now, since this can never be a |
| 1301 | * valid key representation. This way we know that data must be a valid |
| 1302 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1303 | if (data_size == 0) { |
| 1304 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1305 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1306 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1307 | /* Set the key to empty now, so that even when there are errors, we always |
| 1308 | * set data_length to a value between 0 and data_size. On error, setting |
| 1309 | * the key to empty is a good choice because an empty key representation is |
| 1310 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1311 | *data_length = 0; |
| 1312 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1313 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1314 | * which don't require any flag, but |
| 1315 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1316 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1317 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1318 | PSA_KEY_USAGE_EXPORT, 0); |
| 1319 | if (status != PSA_SUCCESS) { |
| 1320 | return status; |
| 1321 | } |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1322 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1323 | psa_key_attributes_t attributes = { |
| 1324 | .core = slot->attr |
| 1325 | }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1326 | status = psa_driver_wrapper_export_key(&attributes, |
| 1327 | slot->key.data, slot->key.bytes, |
| 1328 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1330 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1332 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1333 | } |
| 1334 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1335 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1336 | const psa_key_attributes_t *attributes, |
| 1337 | const uint8_t *key_buffer, |
| 1338 | size_t key_buffer_size, |
| 1339 | uint8_t *data, |
| 1340 | size_t data_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1341 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1342 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1343 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1345 | if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) { |
| 1346 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1347 | /* Exporting public -> public */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1348 | return psa_export_key_buffer_internal( |
| 1349 | key_buffer, key_buffer_size, |
| 1350 | data, data_size, data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1351 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1353 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1354 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1355 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1356 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1357 | key_buffer, |
| 1358 | key_buffer_size, |
| 1359 | data, |
| 1360 | data_size, |
| 1361 | data_length); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1362 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1363 | /* 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] | 1364 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1365 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1366 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1367 | } else { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1368 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1369 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1370 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1371 | key_buffer, |
| 1372 | key_buffer_size, |
| 1373 | data, |
| 1374 | data_size, |
| 1375 | data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1376 | #else |
| 1377 | /* 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] | 1378 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1379 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1380 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1381 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1382 | } else { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1383 | /* This shouldn't happen in the reference implementation, but |
| 1384 | it is valid for a special-purpose implementation to omit |
| 1385 | support for exporting certain key types. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1386 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1387 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1388 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1390 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, |
| 1391 | uint8_t *data, |
| 1392 | size_t data_size, |
| 1393 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1394 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1395 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1396 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1397 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1398 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1399 | /* Reject a zero-length output buffer now, since this can never be a |
| 1400 | * valid key representation. This way we know that data must be a valid |
| 1401 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1402 | if (data_size == 0) { |
| 1403 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1404 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1405 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1406 | /* Set the key to empty now, so that even when there are errors, we always |
| 1407 | * set data_length to a value between 0 and data_size. On error, setting |
| 1408 | * the key to empty is a good choice because an empty key representation is |
| 1409 | * unlikely to be accepted anywhere. */ |
| 1410 | *data_length = 0; |
| 1411 | |
| 1412 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1413 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1414 | if (status != PSA_SUCCESS) { |
| 1415 | return status; |
| 1416 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1419 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1420 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1421 | } |
| 1422 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1423 | psa_key_attributes_t attributes = { |
| 1424 | .core = slot->attr |
| 1425 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1426 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1427 | &attributes, slot->key.data, slot->key.bytes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1428 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1429 | |
| 1430 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1431 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1433 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1434 | } |
| 1435 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1436 | #if defined(static_assert) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1437 | static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1438 | "One or more key attribute flag is listed as both external-only and dual-use"); |
| 1439 | static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1440 | "One or more key attribute flag is listed as both internal-only and dual-use"); |
| 1441 | static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1442 | "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] | 1443 | #endif |
| 1444 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1445 | /** Validate that a key policy is internally well-formed. |
| 1446 | * |
| 1447 | * This function only rejects invalid policies. It does not validate the |
| 1448 | * consistency of the policy with respect to other attributes of the key |
| 1449 | * such as the key type. |
| 1450 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1451 | 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] | 1452 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1453 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1454 | PSA_KEY_USAGE_COPY | |
| 1455 | PSA_KEY_USAGE_ENCRYPT | |
| 1456 | PSA_KEY_USAGE_DECRYPT | |
| 1457 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1458 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1459 | PSA_KEY_USAGE_SIGN_HASH | |
| 1460 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1461 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
| 1462 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1463 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1464 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1466 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1467 | } |
| 1468 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1469 | /** Validate the internal consistency of key attributes. |
| 1470 | * |
| 1471 | * This function only rejects invalid attribute values. If does not |
| 1472 | * validate the consistency of the attributes with any key data that may |
| 1473 | * be involved in the creation of the key. |
| 1474 | * |
| 1475 | * Call this function early in the key creation process. |
| 1476 | * |
| 1477 | * \param[in] attributes Key attributes for the new key. |
| 1478 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1479 | * NULL for a transparent key. |
| 1480 | * |
| 1481 | */ |
| 1482 | static psa_status_t psa_validate_key_attributes( |
| 1483 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1484 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1485 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1486 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1487 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1488 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1490 | status = psa_validate_key_location(lifetime, p_drv); |
| 1491 | if (status != PSA_SUCCESS) { |
| 1492 | return status; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1493 | } |
| 1494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1495 | status = psa_validate_key_persistence(lifetime); |
| 1496 | if (status != PSA_SUCCESS) { |
| 1497 | return status; |
| 1498 | } |
| 1499 | |
| 1500 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1501 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1502 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1503 | } |
| 1504 | } else { |
| 1505 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1506 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1511 | if (status != PSA_SUCCESS) { |
| 1512 | return status; |
| 1513 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1514 | |
| 1515 | /* Refuse to create overly large keys. |
| 1516 | * Note that this doesn't trigger on import if the attributes don't |
| 1517 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1518 | * psa_import_key() needs its own checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1519 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1520 | return PSA_ERROR_NOT_SUPPORTED; |
| 1521 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1522 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1523 | /* Reject invalid flags. These should not be reachable through the API. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1524 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1525 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { |
| 1526 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1527 | } |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1529 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1530 | } |
| 1531 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1532 | /** Prepare a key slot to receive key material. |
| 1533 | * |
| 1534 | * This function allocates a key slot and sets its metadata. |
| 1535 | * |
| 1536 | * If this function fails, call psa_fail_key_creation(). |
| 1537 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1538 | * This function is intended to be used as follows: |
| 1539 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1540 | * it with the specified attributes, and in case of a volatile key assign it |
| 1541 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1542 | * -# Populate the slot with the key material. |
| 1543 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1544 | * In case of failure at any step, stop the sequence and call |
| 1545 | * psa_fail_key_creation(). |
| 1546 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1547 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1548 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1549 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1550 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1551 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1552 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1553 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1554 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1555 | * |
| 1556 | * \retval #PSA_SUCCESS |
| 1557 | * The key slot is ready to receive key material. |
| 1558 | * \return If this function fails, the key slot is an invalid state. |
| 1559 | * 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] | 1560 | */ |
| 1561 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1562 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1563 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1564 | psa_key_slot_t **p_slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1565 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1566 | { |
| 1567 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1568 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1569 | psa_key_slot_t *slot; |
| 1570 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1571 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1572 | *p_drv = NULL; |
| 1573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1574 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1575 | if (status != PSA_SUCCESS) { |
| 1576 | return status; |
| 1577 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1579 | status = psa_get_empty_key_slot(&volatile_key_id, p_slot); |
| 1580 | if (status != PSA_SUCCESS) { |
| 1581 | return status; |
| 1582 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1583 | slot = *p_slot; |
| 1584 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1585 | /* We're storing the declared bit-size of the key. It's up to each |
| 1586 | * creation mechanism to verify that this information is correct. |
| 1587 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1588 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1589 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1590 | * volatile key identifier associated to the slot returned to contain its |
| 1591 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1592 | |
| 1593 | slot->attr = attributes->core; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1594 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1595 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1596 | slot->attr.id = volatile_key_id; |
| 1597 | #else |
| 1598 | slot->attr.id.key_id = volatile_key_id; |
| 1599 | #endif |
| 1600 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1601 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1602 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1603 | * external-only flags, query `attributes`. Thanks to the check |
| 1604 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1605 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1606 | * may have set. */ |
| 1607 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1608 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1609 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1610 | /* 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] | 1611 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1612 | * create the key file in internal storage, create the |
| 1613 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1614 | * persistent data. This is done by starting a transaction that will |
| 1615 | * encompass these three actions. |
| 1616 | * For registering a volatile key, we just need to find an appropriate |
| 1617 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1618 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1619 | /* The first thing to do is to find a slot number for the new key. |
| 1620 | * We save the slot number in persistent storage as part of the |
| 1621 | * transaction data. It will be needed to recover if the power |
| 1622 | * fails during the key creation process, to clean up on the secure |
| 1623 | * element side after restarting. Obtaining a slot number from the |
| 1624 | * secure element driver updates its persistent state, but we do not yet |
| 1625 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1626 | * 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] | 1627 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1628 | psa_key_slot_number_t slot_number; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1629 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1630 | &slot_number); |
| 1631 | if (status != PSA_SUCCESS) { |
| 1632 | return status; |
| 1633 | } |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1635 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1636 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1637 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1638 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1639 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | status = psa_crypto_save_transaction(); |
| 1641 | if (status != PSA_SUCCESS) { |
| 1642 | (void) psa_crypto_stop_transaction(); |
| 1643 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1644 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1645 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1646 | |
| 1647 | status = psa_copy_key_material_into_slot( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1648 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1649 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1651 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1652 | /* Key registration only makes sense with a secure element. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1653 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1654 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1655 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1657 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1658 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1659 | |
| 1660 | /** Finalize the creation of a key once its key material has been set. |
| 1661 | * |
| 1662 | * This entails writing the key to persistent storage. |
| 1663 | * |
| 1664 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1665 | * See the documentation of psa_start_key_creation() for the intended use |
| 1666 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1667 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1668 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1669 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1670 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1671 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1672 | * \param[in,out] slot Pointer to the slot with key material. |
| 1673 | * \param[in] driver The secure element driver for the key, |
| 1674 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1675 | * \param[out] key On success, identifier of the key. Note that the |
| 1676 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1677 | * |
| 1678 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1679 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1680 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1681 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1682 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1683 | * \retval #PSA_ERROR_DATA_INVALID |
| 1684 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1685 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1686 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1687 | * \return If this function fails, the key slot is an invalid state. |
| 1688 | * 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] | 1689 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1690 | static psa_status_t psa_finish_key_creation( |
| 1691 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1692 | psa_se_drv_table_entry_t *driver, |
| 1693 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1694 | { |
| 1695 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1696 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1697 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1698 | |
| 1699 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1700 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1701 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1702 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1703 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1704 | psa_key_slot_number_t slot_number = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1705 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1706 | |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1707 | #if defined(static_assert) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1708 | static_assert(sizeof(slot_number) == |
| 1709 | sizeof(data.slot_number), |
| 1710 | "Slot number size does not match psa_se_key_data_storage_t"); |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1711 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1712 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1713 | status = psa_save_persistent_key(&slot->attr, |
| 1714 | (uint8_t *) &data, |
| 1715 | sizeof(data)); |
| 1716 | } else |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1717 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1718 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1719 | /* Key material is saved in export representation in the slot, so |
| 1720 | * just pass the slot buffer for storage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1721 | status = psa_save_persistent_key(&slot->attr, |
| 1722 | slot->key.data, |
| 1723 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1724 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1725 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1726 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1727 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1728 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1729 | /* Finish the transaction for a key creation. This does not |
| 1730 | * happen when registering an existing key. Detect this case |
| 1731 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1732 | * creation of a persistent key in a secure element requires a transaction, |
| 1733 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1734 | if (driver != NULL && |
| 1735 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1736 | status = psa_save_se_persistent_data(driver); |
| 1737 | if (status != PSA_SUCCESS) { |
| 1738 | psa_destroy_persistent_key(slot->attr.id); |
| 1739 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1740 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1741 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1742 | } |
| 1743 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1745 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1746 | *key = slot->attr.id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1747 | status = psa_unlock_key_slot(slot); |
| 1748 | if (status != PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1749 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1750 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1751 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1753 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | /** Abort the creation of a key. |
| 1757 | * |
| 1758 | * You may call this function after calling psa_start_key_creation(), |
| 1759 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1760 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1761 | * See the documentation of psa_start_key_creation() for the intended use |
| 1762 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1763 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1764 | * \param[in,out] slot Pointer to the slot with key material. |
| 1765 | * \param[in] driver The secure element driver for the key, |
| 1766 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1767 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1768 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 1769 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1770 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1771 | (void) driver; |
| 1772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1773 | if (slot == NULL) { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1774 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1775 | } |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1776 | |
| 1777 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1778 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1779 | * element, and the failure happened later (when saving metadata |
| 1780 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1781 | * element. |
| 1782 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1783 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1784 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1785 | /* Abort the ongoing transaction if any (there may not be one if |
| 1786 | * the creation process failed before starting one, or if the |
| 1787 | * key creation is a registration of a key in a secure element). |
| 1788 | * Earlier functions must already have done what it takes to undo any |
| 1789 | * partial creation. All that's left is to update the transaction data |
| 1790 | * itself. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1792 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1794 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1795 | } |
| 1796 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1797 | /** Validate optional attributes during key creation. |
| 1798 | * |
| 1799 | * Some key attributes are optional during key creation. If they are |
| 1800 | * specified in the attributes structure, check that they are consistent |
| 1801 | * with the data in the slot. |
| 1802 | * |
| 1803 | * This function should be called near the end of key creation, after |
| 1804 | * the slot in memory is fully populated but before saving persistent data. |
| 1805 | */ |
| 1806 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1807 | const psa_key_slot_t *slot, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1808 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1809 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | if (attributes->core.type != 0) { |
| 1811 | if (attributes->core.type != slot->attr.type) { |
| 1812 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1813 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1814 | } |
| 1815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | if (attributes->domain_parameters_size != 0) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1817 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1818 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1819 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1820 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1821 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1822 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1823 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1824 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1825 | slot->attr.type, |
| 1826 | slot->key.data, |
| 1827 | slot->key.bytes, |
| 1828 | &rsa); |
| 1829 | if (status != PSA_SUCCESS) { |
| 1830 | return status; |
| 1831 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1833 | mbedtls_mpi_init(&actual); |
| 1834 | mbedtls_mpi_init(&required); |
| 1835 | ret = mbedtls_rsa_export(rsa, |
| 1836 | NULL, NULL, NULL, NULL, &actual); |
| 1837 | mbedtls_rsa_free(rsa); |
| 1838 | mbedtls_free(rsa); |
| 1839 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1840 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1841 | } |
| 1842 | ret = mbedtls_mpi_read_binary(&required, |
| 1843 | attributes->domain_parameters, |
| 1844 | attributes->domain_parameters_size); |
| 1845 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1846 | goto rsa_exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | } |
| 1848 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1849 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | } |
| 1851 | rsa_exit: |
| 1852 | mbedtls_mpi_free(&actual); |
| 1853 | mbedtls_mpi_free(&required); |
| 1854 | if (ret != 0) { |
| 1855 | return mbedtls_to_psa_error(ret); |
| 1856 | } |
| 1857 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1858 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1859 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1860 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1861 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1862 | } |
| 1863 | } |
| 1864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1865 | if (attributes->core.bits != 0) { |
| 1866 | if (attributes->core.bits != slot->attr.bits) { |
| 1867 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1868 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1869 | } |
| 1870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1871 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1872 | } |
| 1873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1874 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
| 1875 | const uint8_t *data, |
| 1876 | size_t data_length, |
| 1877 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1878 | { |
| 1879 | psa_status_t status; |
| 1880 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1881 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1882 | size_t bits; |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1883 | size_t storage_size = data_length; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1884 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1885 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1886 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1887 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1888 | * This also rejects any key which might be encoded as an empty string, |
| 1889 | * which is never valid. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1890 | if (data_length == 0) { |
| 1891 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1892 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1893 | |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1894 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1895 | if (data_length > SIZE_MAX / 8) { |
| 1896 | return PSA_ERROR_NOT_SUPPORTED; |
| 1897 | } |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 1900 | &slot, &driver); |
| 1901 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1902 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1903 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1904 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1905 | /* 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] | 1906 | * storage ( thus not in the case of importing a key in a secure element |
| 1907 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 1908 | * buffer to hold the imported key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1909 | if (slot->key.data == NULL) { |
| 1910 | if (psa_key_lifetime_is_external(attributes->core.lifetime)) { |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 1911 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1912 | attributes, data, data_length, &storage_size); |
| 1913 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1914 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1915 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 1916 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1917 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 1918 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1919 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1920 | } |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1921 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1922 | |
| 1923 | bits = slot->attr.bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1924 | status = psa_driver_wrapper_import_key(attributes, |
| 1925 | data, data_length, |
| 1926 | slot->key.data, |
| 1927 | slot->key.bytes, |
| 1928 | &slot->key.bytes, &bits); |
| 1929 | if (status != PSA_SUCCESS) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1930 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1931 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1933 | if (slot->attr.bits == 0) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1934 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1935 | } else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1936 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1937 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1938 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 1939 | |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1940 | /* Enforce a size limit, and in particular ensure that the bit |
| 1941 | * size fits in its representation type.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1942 | if (bits > PSA_MAX_KEY_BITS) { |
Archana | 6ed4bda | 2021-08-04 10:47:15 +0530 | [diff] [blame] | 1943 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1944 | goto exit; |
| 1945 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1946 | status = psa_validate_optional_attributes(slot, attributes); |
| 1947 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1948 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1949 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1951 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1952 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | if (status != PSA_SUCCESS) { |
| 1954 | psa_fail_key_creation(slot, driver); |
| 1955 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1957 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1958 | } |
| 1959 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1960 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1961 | psa_status_t mbedtls_psa_register_se_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1962 | const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1963 | { |
| 1964 | psa_status_t status; |
| 1965 | psa_key_slot_t *slot = NULL; |
| 1966 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1967 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1968 | |
| 1969 | /* Leaving attributes unspecified is not currently supported. |
| 1970 | * It could make sense to query the key type and size from the |
| 1971 | * secure element, but not all secure elements support this |
| 1972 | * and the driver HAL doesn't currently support it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1973 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 1974 | return PSA_ERROR_NOT_SUPPORTED; |
| 1975 | } |
| 1976 | if (psa_get_key_bits(attributes) == 0) { |
| 1977 | return PSA_ERROR_NOT_SUPPORTED; |
| 1978 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1980 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 1981 | &slot, &driver); |
| 1982 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1983 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1984 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1986 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1987 | |
| 1988 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1989 | if (status != PSA_SUCCESS) { |
| 1990 | psa_fail_key_creation(slot, driver); |
| 1991 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1992 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1993 | /* Registration doesn't keep the key in RAM. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1994 | psa_close_key(key); |
| 1995 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1996 | } |
| 1997 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1999 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2000 | const psa_key_attributes_t *specified_attributes, |
| 2001 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2002 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2003 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2004 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2005 | psa_key_slot_t *source_slot = NULL; |
| 2006 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2007 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2008 | psa_se_drv_table_entry_t *driver = NULL; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2009 | size_t storage_size = 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2010 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2011 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2012 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2013 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2014 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2015 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2016 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2017 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2019 | status = psa_validate_optional_attributes(source_slot, |
| 2020 | specified_attributes); |
| 2021 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2022 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2023 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2024 | |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2025 | /* The target key type and number of bits have been validated by |
| 2026 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2027 | * equal to the ones of the source key. So it is safe to inherit |
| 2028 | * them from the source key now." |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2029 | * */ |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2030 | actual_attributes.core.bits = source_slot->attr.bits; |
| 2031 | actual_attributes.core.type = source_slot->attr.type; |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2032 | |
| 2033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2034 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2035 | &actual_attributes.core.policy, |
| 2036 | &source_slot->attr.policy); |
| 2037 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2038 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2041 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2042 | &target_slot, &driver); |
| 2043 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2044 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2045 | } |
| 2046 | if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) != |
| 2047 | PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2048 | /* |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2049 | * If the source and target keys are stored in different locations, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2050 | * the source key would need to be exported as plaintext and re-imported |
| 2051 | * in the other location. This has security implications which have not |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2052 | * been fully mapped. For now, this can be achieved through |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2053 | * appropriate API invocations from the application, if needed. |
| 2054 | * */ |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2055 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2056 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2057 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2058 | /* |
| 2059 | * When the source and target keys are within the same location, |
Archana | 449608b | 2021-09-08 15:36:05 +0530 | [diff] [blame] | 2060 | * - For transparent keys it is a blind copy without any driver invocation, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2061 | * - For opaque keys this translates to an invocation of the drivers' |
| 2062 | * copy_key entry point through the dispatch layer. |
| 2063 | * */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2064 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
| 2065 | status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes, |
| 2066 | &storage_size); |
| 2067 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2068 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2069 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2071 | status = psa_allocate_buffer_to_slot(target_slot, storage_size); |
| 2072 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2073 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2074 | } |
Archana | 374fe5b | 2021-09-08 15:50:28 +0530 | [diff] [blame] | 2075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2076 | status = psa_driver_wrapper_copy_key(&actual_attributes, |
| 2077 | source_slot->key.data, |
| 2078 | source_slot->key.bytes, |
| 2079 | target_slot->key.data, |
| 2080 | target_slot->key.bytes, |
| 2081 | &target_slot->key.bytes); |
| 2082 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2083 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2084 | } |
| 2085 | } else { |
| 2086 | status = psa_copy_key_material_into_slot(target_slot, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 2087 | source_slot->key.data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2088 | source_slot->key.bytes); |
| 2089 | if (status != PSA_SUCCESS) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2090 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2092 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2093 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2094 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | if (status != PSA_SUCCESS) { |
| 2096 | psa_fail_key_creation(target_slot, driver); |
| 2097 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2099 | unlock_status = psa_unlock_key_slot(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2101 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2102 | } |
| 2103 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2104 | |
| 2105 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2106 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2107 | /* Message digests */ |
| 2108 | /****************************************************************/ |
| 2109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2110 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2111 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2112 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | if (operation->id == 0) { |
| 2114 | return PSA_SUCCESS; |
| 2115 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2117 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2118 | operation->id = 0; |
| 2119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2120 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2121 | } |
| 2122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2123 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2124 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2125 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2126 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2127 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2128 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2129 | if (operation->id != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2130 | status = PSA_ERROR_BAD_STATE; |
| 2131 | goto exit; |
| 2132 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2134 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2135 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2136 | goto exit; |
| 2137 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2138 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2139 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2140 | * directly zeroes the int-sized dummy member of the context union. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2143 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2144 | |
| 2145 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2146 | if (status != PSA_SUCCESS) { |
| 2147 | psa_hash_abort(operation); |
| 2148 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2149 | |
| 2150 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2151 | } |
| 2152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2153 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 2154 | const uint8_t *input, |
| 2155 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2156 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2157 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2159 | if (operation->id == 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2160 | status = PSA_ERROR_BAD_STATE; |
| 2161 | goto exit; |
| 2162 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2163 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2164 | /* Don't require hash implementations to behave correctly on a |
| 2165 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2166 | if (input_length == 0) { |
| 2167 | return PSA_SUCCESS; |
| 2168 | } |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2170 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2171 | |
| 2172 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | if (status != PSA_SUCCESS) { |
| 2174 | psa_hash_abort(operation); |
| 2175 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2177 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2178 | } |
| 2179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2181 | uint8_t *hash, |
| 2182 | size_t hash_size, |
| 2183 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2184 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2185 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2186 | if (operation->id == 0) { |
| 2187 | return PSA_ERROR_BAD_STATE; |
| 2188 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2189 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2190 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2191 | operation, hash, hash_size, hash_length); |
| 2192 | psa_hash_abort(operation); |
| 2193 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2194 | } |
| 2195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2196 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 2197 | const uint8_t *hash, |
| 2198 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2199 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2200 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2201 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2202 | psa_status_t status = psa_hash_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2203 | operation, |
| 2204 | actual_hash, sizeof(actual_hash), |
| 2205 | &actual_hash_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2207 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2208 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2212 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2213 | goto exit; |
| 2214 | } |
| 2215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2216 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2217 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2218 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2219 | |
| 2220 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2221 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2222 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2223 | psa_hash_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2224 | } |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2226 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2227 | } |
| 2228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2229 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 2230 | const uint8_t *input, size_t input_length, |
| 2231 | uint8_t *hash, size_t hash_size, |
| 2232 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2233 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2234 | *hash_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2235 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2236 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2237 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2239 | return psa_driver_wrapper_hash_compute(alg, input, input_length, |
| 2240 | hash, hash_size, hash_length); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2241 | } |
| 2242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2243 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 2244 | const uint8_t *input, size_t input_length, |
| 2245 | const uint8_t *hash, size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2246 | { |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2247 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2248 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2250 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2251 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2252 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2253 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2254 | psa_status_t status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2255 | alg, input, input_length, |
| 2256 | actual_hash, sizeof(actual_hash), |
| 2257 | &actual_hash_length); |
| 2258 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2259 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | } |
| 2261 | if (actual_hash_length != hash_length) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2262 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2263 | goto exit; |
| 2264 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2265 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2266 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2267 | } |
Gilles Peskine | 60aebec | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2268 | |
| 2269 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2271 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2272 | } |
| 2273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2274 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2275 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2276 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2277 | if (source_operation->id == 0 || |
| 2278 | target_operation->id != 0) { |
| 2279 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2280 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2282 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2283 | target_operation); |
| 2284 | if (status != PSA_SUCCESS) { |
| 2285 | psa_hash_abort(target_operation); |
| 2286 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2288 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2289 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2290 | |
| 2291 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2292 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2293 | /* MAC */ |
| 2294 | /****************************************************************/ |
| 2295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2296 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2297 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2298 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2299 | if (operation->id == 0) { |
| 2300 | return PSA_SUCCESS; |
| 2301 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2303 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2304 | operation->mac_size = 0; |
| 2305 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2306 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2308 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2309 | } |
| 2310 | |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2311 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2312 | psa_algorithm_t alg, |
Ronald Cron | 79bdd82 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2313 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2314 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2315 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2316 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2317 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2318 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2320 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2321 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2322 | } |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2323 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2324 | /* Validate the combination of key type and algorithm */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2325 | status = psa_mac_key_can_do(alg, key_type); |
| 2326 | if (status != PSA_SUCCESS) { |
| 2327 | return status; |
| 2328 | } |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2329 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2330 | /* Get the output length for the algorithm and key combination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2331 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2333 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2334 | /* A very short MAC is too short for security since it can be |
| 2335 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2336 | * so we make this our minimum, even though 32 bits is still |
| 2337 | * too small for security. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2338 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2339 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2342 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2343 | /* It's impossible to "truncate" to a larger length than the full length |
| 2344 | * of the algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2345 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2346 | } |
| 2347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2348 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2349 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2350 | * that is disabled in the compile-time configuration. The result can |
| 2351 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2352 | * configuration into account. In this case, force a return of |
| 2353 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2354 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2355 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2356 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2357 | * systematically generated tests. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2358 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a9b6c80 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2359 | } |
| 2360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2361 | return PSA_SUCCESS; |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2364 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2365 | mbedtls_svc_key_id_t key, |
| 2366 | psa_algorithm_t alg, |
| 2367 | int is_sign) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2368 | { |
| 2369 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2370 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2371 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2372 | |
| 2373 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2375 | status = PSA_ERROR_BAD_STATE; |
| 2376 | goto exit; |
| 2377 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2378 | |
| 2379 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2380 | key, |
| 2381 | &slot, |
| 2382 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2383 | alg); |
| 2384 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2385 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2386 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2387 | |
| 2388 | psa_key_attributes_t attributes = { |
| 2389 | .core = slot->attr |
| 2390 | }; |
| 2391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2393 | &operation->mac_size); |
| 2394 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2395 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2396 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2397 | |
| 2398 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2399 | /* Dispatch the MAC setup call with validated input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2400 | if (is_sign) { |
| 2401 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2402 | &attributes, |
| 2403 | slot->key.data, |
| 2404 | slot->key.bytes, |
| 2405 | alg); |
| 2406 | } else { |
| 2407 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2408 | &attributes, |
| 2409 | slot->key.data, |
| 2410 | slot->key.bytes, |
| 2411 | alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2412 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2413 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2414 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2415 | if (status != PSA_SUCCESS) { |
| 2416 | psa_mac_abort(operation); |
| 2417 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2419 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2421 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2422 | } |
| 2423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2424 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2425 | mbedtls_svc_key_id_t key, |
| 2426 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2427 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2428 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2429 | } |
| 2430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2431 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2432 | mbedtls_svc_key_id_t key, |
| 2433 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2434 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2435 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2436 | } |
| 2437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2438 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 2439 | const uint8_t *input, |
| 2440 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2441 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2442 | if (operation->id == 0) { |
| 2443 | return PSA_ERROR_BAD_STATE; |
| 2444 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2445 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2446 | /* Don't require hash implementations to behave correctly on a |
| 2447 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | if (input_length == 0) { |
| 2449 | return PSA_SUCCESS; |
| 2450 | } |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | psa_status_t status = psa_driver_wrapper_mac_update(operation, |
| 2453 | input, input_length); |
| 2454 | if (status != PSA_SUCCESS) { |
| 2455 | psa_mac_abort(operation); |
| 2456 | } |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2458 | return 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_finish(psa_mac_operation_t *operation, |
| 2462 | uint8_t *mac, |
| 2463 | size_t mac_size, |
| 2464 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2465 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2466 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2467 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2470 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2471 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2472 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2474 | if (!operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2475 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2476 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2477 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2478 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2479 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2480 | * once all the error checks are done. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2481 | if (operation->mac_size == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2482 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2483 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2484 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | if (mac_size < operation->mac_size) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2487 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2488 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2489 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2491 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2492 | mac, operation->mac_size, |
| 2493 | mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2494 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2495 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2496 | /* In case of success, set the potential excess room in the output buffer |
| 2497 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2498 | * In case of error, set the output length and content to a safe default, |
| 2499 | * such that in case the caller misses an error check, the output would be |
| 2500 | * an unachievable MAC. |
| 2501 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2503 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2504 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2505 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2507 | if (mac_size > operation->mac_size) { |
| 2508 | memset(&mac[operation->mac_size], '!', |
| 2509 | mac_size - operation->mac_size); |
| 2510 | } |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2512 | abort_status = psa_mac_abort(operation); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2517 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2518 | const uint8_t *mac, |
| 2519 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2520 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2521 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2522 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2524 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2525 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2526 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2527 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2529 | if (operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2530 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2531 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2532 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2534 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2535 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2536 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2537 | } |
| 2538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2539 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2540 | mac, mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2541 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2542 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2543 | abort_status = psa_mac_abort(operation); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2545 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2546 | } |
| 2547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2549 | psa_algorithm_t alg, |
| 2550 | const uint8_t *input, |
| 2551 | size_t input_length, |
| 2552 | uint8_t *mac, |
| 2553 | size_t mac_size, |
| 2554 | size_t *mac_length, |
| 2555 | int is_sign) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2556 | { |
| 2557 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2558 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2559 | psa_key_slot_t *slot; |
| 2560 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2561 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2562 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2563 | key, |
| 2564 | &slot, |
| 2565 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2566 | alg); |
| 2567 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2568 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2569 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2570 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2571 | psa_key_attributes_t attributes = { |
| 2572 | .core = slot->attr |
| 2573 | }; |
| 2574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2575 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2576 | &operation_mac_size); |
| 2577 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2578 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2579 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2581 | if (mac_size < operation_mac_size) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2582 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2583 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | status = psa_driver_wrapper_mac_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2587 | &attributes, |
| 2588 | slot->key.data, slot->key.bytes, |
| 2589 | alg, |
| 2590 | input, input_length, |
| 2591 | mac, operation_mac_size, mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2592 | |
| 2593 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2594 | /* In case of success, set the potential excess room in the output buffer |
| 2595 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2596 | * In case of error, set the output length and content to a safe default, |
| 2597 | * such that in case the caller misses an error check, the output would be |
| 2598 | * an unachievable MAC. |
| 2599 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2600 | if (status != PSA_SUCCESS) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2601 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2602 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2603 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2604 | if (mac_size > operation_mac_size) { |
| 2605 | memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size); |
| 2606 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2608 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2610 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2611 | } |
| 2612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2613 | 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] | 2614 | psa_algorithm_t alg, |
| 2615 | const uint8_t *input, |
| 2616 | size_t input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2617 | uint8_t *mac, |
| 2618 | size_t mac_size, |
| 2619 | size_t *mac_length) |
| 2620 | { |
| 2621 | return psa_mac_compute_internal(key, alg, |
| 2622 | input, input_length, |
| 2623 | mac, mac_size, mac_length, 1); |
| 2624 | } |
| 2625 | |
| 2626 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2627 | psa_algorithm_t alg, |
| 2628 | const uint8_t *input, |
| 2629 | size_t input_length, |
| 2630 | const uint8_t *mac, |
| 2631 | size_t mac_length) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2632 | { |
| 2633 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2634 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2635 | size_t actual_mac_length; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2637 | status = psa_mac_compute_internal(key, alg, |
| 2638 | input, input_length, |
| 2639 | actual_mac, sizeof(actual_mac), |
| 2640 | &actual_mac_length, 0); |
| 2641 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2642 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2643 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2645 | if (mac_length != actual_mac_length) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2646 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2647 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2648 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2649 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2650 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2651 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2652 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2653 | |
| 2654 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2657 | return status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2658 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2659 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2660 | /****************************************************************/ |
| 2661 | /* Asymmetric cryptography */ |
| 2662 | /****************************************************************/ |
| 2663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2664 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2665 | psa_algorithm_t alg) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2666 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2667 | if (input_is_message) { |
| 2668 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 2669 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2670 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2672 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2673 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { |
| 2674 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2675 | } |
| 2676 | } |
| 2677 | } else { |
| 2678 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2679 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2680 | } |
| 2681 | } |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2683 | return PSA_SUCCESS; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2684 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2685 | |
Paul Elliott | 939bd94 | 2023-02-07 12:15:24 +0000 | [diff] [blame] | 2686 | /** |
| 2687 | * \brief Fill the unused part of the output buffer(the |
| 2688 | * whole buffer on error, the trailing part on |
| 2689 | * success) with something that isn't a valid |
| 2690 | * signature (barring an attack on the signature |
| 2691 | * and deliberately-crafted input), in case the |
| 2692 | * caller doesn't check the return status properly. |
| 2693 | * |
| 2694 | * \param output_buffer pointer to buffer to wipe. May not be NULL |
| 2695 | * unless /p output_buffer_size is zero. |
| 2696 | * \param status status of function called to generate |
| 2697 | * output_buffer originally |
| 2698 | * \param output_buffer_size Size of output buffer. If zero, /p output_buffer |
| 2699 | * could be NULL |
| 2700 | * \param output_buffer_length Length of data written to output_buffer, must be |
| 2701 | * less than /p output_buffer_size |
| 2702 | */ |
| 2703 | static void psa_wipe_output_buffer(uint8_t *output_buffer, psa_status_t status, |
| 2704 | size_t output_buffer_size, size_t output_buffer_length) |
| 2705 | { |
| 2706 | if (status == PSA_SUCCESS) { |
| 2707 | memset(output_buffer + output_buffer_length, '!', |
| 2708 | output_buffer_size - output_buffer_length); |
| 2709 | } else if (output_buffer_size > 0) { |
| 2710 | memset(output_buffer, '!', output_buffer_size); |
| 2711 | } |
| 2712 | /* If output_buffer_size is 0 then we have nothing to do. We must |
| 2713 | * not call memset because output_buffer may be NULL in this |
| 2714 | * case.*/ |
| 2715 | } |
| 2716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2717 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 2718 | int input_is_message, |
| 2719 | psa_algorithm_t alg, |
| 2720 | const uint8_t *input, |
| 2721 | size_t input_length, |
| 2722 | uint8_t *signature, |
| 2723 | size_t signature_size, |
| 2724 | size_t *signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2725 | { |
| 2726 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2727 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2728 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2729 | |
| 2730 | *signature_length = 0; |
| 2731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2732 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2733 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2734 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2735 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2736 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2737 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2738 | * 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] | 2739 | * buffer can in principle be empty since it doesn't actually have |
| 2740 | * to be a hash.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2741 | if (signature_size == 0) { |
| 2742 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 2743 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2744 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2745 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2746 | key, &slot, |
| 2747 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2748 | PSA_KEY_USAGE_SIGN_HASH, |
| 2749 | alg); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2751 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2752 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2753 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2755 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2756 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2757 | goto exit; |
| 2758 | } |
| 2759 | |
| 2760 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2761 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2762 | }; |
| 2763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2764 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2765 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2766 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2767 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2768 | signature, signature_size, signature_length); |
| 2769 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2770 | |
| 2771 | status = psa_driver_wrapper_sign_hash( |
| 2772 | &attributes, slot->key.data, slot->key.bytes, |
| 2773 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2774 | signature, signature_size, signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2775 | } |
| 2776 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2777 | |
| 2778 | exit: |
Paul Elliott | 939bd94 | 2023-02-07 12:15:24 +0000 | [diff] [blame] | 2779 | psa_wipe_output_buffer(signature, status, signature_size, |
| 2780 | *signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2782 | unlock_status = psa_unlock_key_slot(slot); |
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 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2785 | } |
| 2786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2787 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 2788 | int input_is_message, |
| 2789 | psa_algorithm_t alg, |
| 2790 | const uint8_t *input, |
| 2791 | size_t input_length, |
| 2792 | const uint8_t *signature, |
| 2793 | size_t signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2794 | { |
| 2795 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2796 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2797 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2799 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2800 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2801 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2802 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2803 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2804 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2805 | key, &slot, |
| 2806 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2807 | PSA_KEY_USAGE_VERIFY_HASH, |
| 2808 | alg); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2810 | if (status != PSA_SUCCESS) { |
| 2811 | return status; |
| 2812 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2813 | |
| 2814 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2815 | .core = slot->attr |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2816 | }; |
| 2817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2818 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2819 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2820 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2821 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2822 | signature, signature_length); |
| 2823 | } else { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2824 | status = psa_driver_wrapper_verify_hash( |
| 2825 | &attributes, slot->key.data, slot->key.bytes, |
| 2826 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2827 | signature, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2828 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2830 | unlock_status = psa_unlock_key_slot(slot); |
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 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2833 | |
| 2834 | } |
| 2835 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2836 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2837 | const psa_key_attributes_t *attributes, |
| 2838 | const uint8_t *key_buffer, |
| 2839 | size_t key_buffer_size, |
| 2840 | psa_algorithm_t alg, |
| 2841 | const uint8_t *input, |
| 2842 | size_t input_length, |
| 2843 | uint8_t *signature, |
| 2844 | size_t signature_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2845 | size_t *signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2846 | { |
| 2847 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2849 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2850 | size_t hash_length; |
| 2851 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2852 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2853 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2854 | PSA_ALG_SIGN_GET_HASH(alg), |
| 2855 | input, input_length, |
| 2856 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2858 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2859 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2860 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2861 | |
| 2862 | return psa_driver_wrapper_sign_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2863 | attributes, key_buffer, key_buffer_size, |
| 2864 | alg, hash, hash_length, |
| 2865 | signature, signature_size, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2866 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2868 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2869 | } |
| 2870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2871 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 2872 | psa_algorithm_t alg, |
| 2873 | const uint8_t *input, |
| 2874 | size_t input_length, |
| 2875 | uint8_t *signature, |
| 2876 | size_t signature_size, |
| 2877 | size_t *signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2878 | { |
| 2879 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2880 | key, 1, alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2881 | signature, signature_size, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2882 | } |
| 2883 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2884 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2885 | const psa_key_attributes_t *attributes, |
| 2886 | const uint8_t *key_buffer, |
| 2887 | size_t key_buffer_size, |
| 2888 | psa_algorithm_t alg, |
| 2889 | const uint8_t *input, |
| 2890 | size_t input_length, |
| 2891 | const uint8_t *signature, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2892 | size_t signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2893 | { |
| 2894 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2896 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2897 | size_t hash_length; |
| 2898 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2899 | |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2900 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2901 | PSA_ALG_SIGN_GET_HASH(alg), |
| 2902 | input, input_length, |
| 2903 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2905 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2906 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2907 | } |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2908 | |
| 2909 | return psa_driver_wrapper_verify_hash( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2910 | attributes, key_buffer, key_buffer_size, |
| 2911 | alg, hash, hash_length, |
| 2912 | signature, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2913 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2915 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2916 | } |
| 2917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2918 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 2919 | psa_algorithm_t alg, |
| 2920 | const uint8_t *input, |
| 2921 | size_t input_length, |
| 2922 | const uint8_t *signature, |
| 2923 | size_t signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2924 | { |
| 2925 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2926 | key, 1, alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2927 | signature, signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2928 | } |
| 2929 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2930 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2931 | const psa_key_attributes_t *attributes, |
| 2932 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2933 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2934 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2935 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2936 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 2937 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 2938 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2939 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2940 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2941 | return mbedtls_psa_rsa_sign_hash( |
| 2942 | attributes, |
| 2943 | key_buffer, key_buffer_size, |
| 2944 | alg, hash, hash_length, |
| 2945 | signature, signature_size, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2946 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2947 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2948 | } else { |
| 2949 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2950 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2951 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 2952 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2953 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2954 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2955 | return mbedtls_psa_ecdsa_sign_hash( |
| 2956 | attributes, |
| 2957 | key_buffer, key_buffer_size, |
| 2958 | alg, hash, hash_length, |
| 2959 | signature, signature_size, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2960 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2961 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | } else { |
| 2963 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2964 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2965 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2967 | (void) key_buffer; |
| 2968 | (void) key_buffer_size; |
| 2969 | (void) hash; |
| 2970 | (void) hash_length; |
| 2971 | (void) signature; |
| 2972 | (void) signature_size; |
| 2973 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2975 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2976 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 2979 | psa_algorithm_t alg, |
| 2980 | const uint8_t *hash, |
| 2981 | size_t hash_length, |
| 2982 | uint8_t *signature, |
| 2983 | size_t signature_size, |
| 2984 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2985 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2986 | return psa_sign_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2987 | key, 0, alg, hash, hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2988 | signature, signature_size, signature_length); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2989 | } |
| 2990 | |
gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2991 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2992 | const psa_key_attributes_t *attributes, |
| 2993 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2994 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2995 | const uint8_t *signature, size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2996 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2997 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 2998 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 2999 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3000 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3001 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3002 | return mbedtls_psa_rsa_verify_hash( |
| 3003 | attributes, |
| 3004 | key_buffer, key_buffer_size, |
| 3005 | alg, hash, hash_length, |
| 3006 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3007 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3008 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3009 | } else { |
| 3010 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3011 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3012 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3013 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3014 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3016 | return mbedtls_psa_ecdsa_verify_hash( |
| 3017 | attributes, |
| 3018 | key_buffer, key_buffer_size, |
| 3019 | alg, hash, hash_length, |
| 3020 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3021 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3022 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3023 | } else { |
| 3024 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3025 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3026 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3028 | (void) key_buffer; |
| 3029 | (void) key_buffer_size; |
| 3030 | (void) hash; |
| 3031 | (void) hash_length; |
| 3032 | (void) signature; |
| 3033 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3035 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3036 | } |
| 3037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3038 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3039 | psa_algorithm_t alg, |
| 3040 | const uint8_t *hash, |
| 3041 | size_t hash_length, |
| 3042 | const uint8_t *signature, |
| 3043 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3044 | { |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3045 | return psa_verify_internal( |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3046 | key, 0, alg, hash, hash_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3047 | signature, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3048 | } |
| 3049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3050 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3051 | psa_algorithm_t alg, |
| 3052 | const uint8_t *input, |
| 3053 | size_t input_length, |
| 3054 | const uint8_t *salt, |
| 3055 | size_t salt_length, |
| 3056 | uint8_t *output, |
| 3057 | size_t output_size, |
| 3058 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3059 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3060 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3061 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3062 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3063 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3064 | (void) input; |
| 3065 | (void) input_length; |
| 3066 | (void) salt; |
| 3067 | (void) output; |
| 3068 | (void) output_size; |
| 3069 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3070 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3072 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3073 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3074 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3075 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3076 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3077 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3078 | if (status != PSA_SUCCESS) { |
| 3079 | return status; |
| 3080 | } |
| 3081 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3082 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3083 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3084 | goto exit; |
| 3085 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3086 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3087 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3088 | .core = slot->attr |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3089 | }; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3090 | |
Przemyslaw Stekiel | 19e6142 | 2021-12-09 11:09:11 +0100 | [diff] [blame] | 3091 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3092 | &attributes, slot->key.data, slot->key.bytes, |
| 3093 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3094 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3095 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3096 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3099 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3101 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3102 | psa_algorithm_t alg, |
| 3103 | const uint8_t *input, |
| 3104 | size_t input_length, |
| 3105 | const uint8_t *salt, |
| 3106 | size_t salt_length, |
| 3107 | uint8_t *output, |
| 3108 | size_t output_size, |
| 3109 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3110 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3111 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3112 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3113 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3114 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3115 | (void) input; |
| 3116 | (void) input_length; |
| 3117 | (void) salt; |
| 3118 | (void) output; |
| 3119 | (void) output_size; |
| 3120 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3121 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3123 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3124 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3125 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3126 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3127 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3128 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3129 | if (status != PSA_SUCCESS) { |
| 3130 | return status; |
| 3131 | } |
| 3132 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3133 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3134 | goto exit; |
| 3135 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3136 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3137 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3138 | .core = slot->attr |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3139 | }; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3140 | |
Przemyslaw Stekiel | 8d45c00 | 2021-12-13 08:58:19 +0100 | [diff] [blame] | 3141 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3142 | &attributes, slot->key.data, slot->key.bytes, |
| 3143 | alg, input, input_length, salt, salt_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3144 | output, output_size, output_length); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3145 | |
| 3146 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3147 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3149 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3150 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3151 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3152 | /****************************************************************/ |
| 3153 | /* Asymmetric interruptible cryptography */ |
| 3154 | /****************************************************************/ |
| 3155 | |
| 3156 | void psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3157 | { |
| 3158 | psa_driver_wrapper_interruptible_set_max_ops(max_ops); |
| 3159 | } |
| 3160 | |
| 3161 | uint32_t psa_interruptible_get_max_ops(void) |
| 3162 | { |
| 3163 | return psa_driver_wrapper_interruptible_get_max_ops(); |
| 3164 | } |
| 3165 | |
| 3166 | |
| 3167 | uint32_t psa_sign_hash_get_num_ops( |
| 3168 | const psa_sign_hash_interruptible_operation_t *operation) |
| 3169 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3170 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | uint32_t psa_verify_hash_get_num_ops( |
| 3174 | const psa_verify_hash_interruptible_operation_t *operation) |
| 3175 | { |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3176 | return operation->num_ops; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3179 | static psa_status_t psa_sign_hash_abort_internal( |
| 3180 | psa_sign_hash_interruptible_operation_t *operation) |
| 3181 | { |
| 3182 | if (operation->id == 0) { |
| 3183 | /* The object has (apparently) been initialized but it is not (yet) |
| 3184 | * in use. It's ok to call abort on such an object, and there's |
| 3185 | * nothing to do. */ |
| 3186 | return PSA_SUCCESS; |
| 3187 | } |
| 3188 | |
| 3189 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3190 | |
| 3191 | status = psa_driver_wrapper_sign_hash_abort(operation); |
| 3192 | |
| 3193 | operation->id = 0; |
| 3194 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame^] | 3195 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3196 | * only want to be cleared by the application calling abort, not by abort |
| 3197 | * being called at completion of an operation. */ |
| 3198 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3199 | return status; |
| 3200 | } |
| 3201 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3202 | psa_status_t psa_sign_hash_start( |
| 3203 | psa_sign_hash_interruptible_operation_t *operation, |
| 3204 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3205 | const uint8_t *hash, size_t hash_length) |
| 3206 | { |
| 3207 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3208 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3209 | psa_key_slot_t *slot; |
| 3210 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3211 | /* Check that start has not been previously called, or operation has not |
| 3212 | * previously errored. */ |
| 3213 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3214 | return PSA_ERROR_BAD_STATE; |
| 3215 | } |
| 3216 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3217 | status = psa_sign_verify_check_alg(0, alg); |
| 3218 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3219 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3220 | return status; |
| 3221 | } |
| 3222 | |
| 3223 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3224 | PSA_KEY_USAGE_SIGN_HASH, |
| 3225 | alg); |
| 3226 | |
| 3227 | if (status != PSA_SUCCESS) { |
| 3228 | goto exit; |
| 3229 | } |
| 3230 | |
| 3231 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
| 3232 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3233 | goto exit; |
| 3234 | } |
| 3235 | |
| 3236 | psa_key_attributes_t attributes = { |
| 3237 | .core = slot->attr |
| 3238 | }; |
| 3239 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3240 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3241 | operation->num_ops = 0; |
| 3242 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3243 | status = psa_driver_wrapper_sign_hash_start(operation, &attributes, |
| 3244 | slot->key.data, |
| 3245 | slot->key.bytes, alg, |
| 3246 | hash, hash_length); |
| 3247 | exit: |
| 3248 | |
| 3249 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3250 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3251 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | unlock_status = psa_unlock_key_slot(slot); |
| 3255 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3256 | if (unlock_status != PSA_SUCCESS) { |
| 3257 | operation->error_occurred = 1; |
| 3258 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3259 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3260 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | |
| 3264 | psa_status_t psa_sign_hash_complete( |
| 3265 | psa_sign_hash_interruptible_operation_t *operation, |
| 3266 | uint8_t *signature, size_t signature_size, |
| 3267 | size_t *signature_length) |
| 3268 | { |
| 3269 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3270 | |
| 3271 | *signature_length = 0; |
| 3272 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3273 | /* Check that start has been called first, and that operation has not |
| 3274 | * previously errored. */ |
| 3275 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3276 | status = PSA_ERROR_BAD_STATE; |
| 3277 | goto exit; |
| 3278 | } |
| 3279 | |
Paul Elliott | 096abc4 | 2023-02-03 18:33:23 +0000 | [diff] [blame] | 3280 | /* Immediately reject a zero-length signature buffer. This guarantees that |
| 3281 | * signature must be a valid pointer. */ |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3282 | if (signature_size == 0) { |
| 3283 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3284 | goto exit; |
| 3285 | } |
| 3286 | |
| 3287 | status = psa_driver_wrapper_sign_hash_complete(operation, signature, |
| 3288 | signature_size, |
| 3289 | signature_length); |
| 3290 | exit: |
| 3291 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3292 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3293 | operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation); |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3294 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3295 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 939bd94 | 2023-02-07 12:15:24 +0000 | [diff] [blame] | 3296 | psa_wipe_output_buffer(signature, status, signature_size, |
| 3297 | *signature_length); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3298 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3299 | if (status != PSA_SUCCESS) { |
| 3300 | operation->error_occurred = 1; |
| 3301 | } |
| 3302 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3303 | psa_sign_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | return status; |
| 3307 | } |
| 3308 | |
| 3309 | psa_status_t psa_sign_hash_abort( |
| 3310 | psa_sign_hash_interruptible_operation_t *operation) |
| 3311 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3312 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3313 | |
| 3314 | status = psa_sign_hash_abort_internal(operation); |
| 3315 | |
| 3316 | /* We clear the number of ops done here, so that it is not cleared when |
| 3317 | * the operation fails or succeeds, only on manual abort. */ |
| 3318 | operation->num_ops = 0; |
| 3319 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3320 | /* Likewise, failure state. */ |
| 3321 | operation->error_occurred = 0; |
| 3322 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3323 | return status; |
| 3324 | } |
| 3325 | |
| 3326 | static psa_status_t psa_verify_hash_abort_internal( |
| 3327 | psa_verify_hash_interruptible_operation_t *operation) |
| 3328 | { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3329 | if (operation->id == 0) { |
| 3330 | /* The object has (apparently) been initialized but it is not (yet) |
| 3331 | * in use. It's ok to call abort on such an object, and there's |
| 3332 | * nothing to do. */ |
| 3333 | return PSA_SUCCESS; |
| 3334 | } |
| 3335 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3336 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3337 | |
| 3338 | status = psa_driver_wrapper_verify_hash_abort(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3339 | |
| 3340 | operation->id = 0; |
| 3341 | |
Paul Elliott | e6145dc | 2023-02-07 12:51:21 +0000 | [diff] [blame^] | 3342 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3343 | * only want to be cleared by the application calling abort, not by abort |
| 3344 | * being called at completion of an operation. */ |
| 3345 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3346 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | psa_status_t psa_verify_hash_start( |
| 3350 | psa_verify_hash_interruptible_operation_t *operation, |
| 3351 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3352 | const uint8_t *hash, size_t hash_length, |
| 3353 | const uint8_t *signature, size_t signature_length) |
| 3354 | { |
| 3355 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3356 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3357 | psa_key_slot_t *slot; |
| 3358 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3359 | /* Check that start has not been previously called, or operation has not |
| 3360 | * previously errored. */ |
| 3361 | if (operation->id != 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3362 | return PSA_ERROR_BAD_STATE; |
| 3363 | } |
| 3364 | |
| 3365 | status = psa_sign_verify_check_alg(0, alg); |
| 3366 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3367 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3368 | return status; |
| 3369 | } |
| 3370 | |
| 3371 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3372 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3373 | alg); |
| 3374 | |
| 3375 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3376 | operation->error_occurred = 1; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3377 | return status; |
| 3378 | } |
| 3379 | |
| 3380 | psa_key_attributes_t attributes = { |
| 3381 | .core = slot->attr |
| 3382 | }; |
| 3383 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3384 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3385 | operation->num_ops = 0; |
| 3386 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3387 | status = psa_driver_wrapper_verify_hash_start(operation, &attributes, |
| 3388 | slot->key.data, |
| 3389 | slot->key.bytes, |
| 3390 | alg, hash, hash_length, |
| 3391 | signature, signature_length); |
| 3392 | |
| 3393 | if (status != PSA_SUCCESS) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3394 | operation->error_occurred = 1; |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3395 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | unlock_status = psa_unlock_key_slot(slot); |
| 3399 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3400 | if (unlock_status != PSA_SUCCESS) { |
| 3401 | operation->error_occurred = 1; |
| 3402 | } |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3403 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3404 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | psa_status_t psa_verify_hash_complete( |
| 3408 | psa_verify_hash_interruptible_operation_t *operation) |
| 3409 | { |
| 3410 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3411 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3412 | /* Check that start has been called first, and that operation has not |
| 3413 | * previously errored. */ |
| 3414 | if (operation->id == 0 || operation->error_occurred) { |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3415 | status = PSA_ERROR_BAD_STATE; |
| 3416 | goto exit; |
| 3417 | } |
| 3418 | |
| 3419 | status = psa_driver_wrapper_verify_hash_complete(operation); |
| 3420 | |
| 3421 | exit: |
| 3422 | |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3423 | /* Update ops count with work done. */ |
Paul Elliott | de1114c | 2023-02-07 12:43:11 +0000 | [diff] [blame] | 3424 | operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops( |
Paul Elliott | 296ede9 | 2022-12-15 17:00:30 +0000 | [diff] [blame] | 3425 | operation); |
| 3426 | |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3427 | if (status != PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3428 | if (status != PSA_SUCCESS) { |
| 3429 | operation->error_occurred = 1; |
| 3430 | } |
| 3431 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3432 | psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
| 3435 | return status; |
| 3436 | } |
| 3437 | |
| 3438 | psa_status_t psa_verify_hash_abort( |
| 3439 | psa_verify_hash_interruptible_operation_t *operation) |
| 3440 | { |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3441 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3442 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3443 | status = psa_verify_hash_abort_internal(operation); |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3444 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3445 | /* We clear the number of ops done here, so that it is not cleared when |
| 3446 | * the operation fails or succeeds, only on manual abort. */ |
| 3447 | operation->num_ops = 0; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3448 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 3449 | /* Likewise, failure state. */ |
| 3450 | operation->error_occurred = 0; |
| 3451 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 3452 | return status; |
Paul Elliott | 9fe12f6 | 2022-11-30 19:16:02 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3455 | /****************************************************************/ |
| 3456 | /* Asymmetric interruptible cryptography internal */ |
| 3457 | /* implementations */ |
| 3458 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3459 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3460 | static uint32_t mbedtls_psa_interruptible_max_ops = |
| 3461 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 3462 | |
| 3463 | void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3464 | { |
Paul Elliott | 7cc4e81 | 2023-01-10 17:14:11 +0000 | [diff] [blame] | 3465 | mbedtls_psa_interruptible_max_ops = max_ops; |
| 3466 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3467 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3468 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3469 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3470 | |
| 3471 | /* Internal implementation uses zero to indicate infinite number max ops, |
| 3472 | * therefore avoid this value, and set to minimum possible. */ |
| 3473 | if (max_ops == 0) { |
| 3474 | max_ops = 1; |
| 3475 | } |
| 3476 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3477 | mbedtls_ecp_set_max_ops(max_ops); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3478 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3479 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3480 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3481 | } |
| 3482 | |
| 3483 | uint32_t mbedtls_psa_interruptible_get_max_ops(void) |
| 3484 | { |
| 3485 | return mbedtls_psa_interruptible_max_ops; |
| 3486 | } |
| 3487 | |
| 3488 | uint32_t mbedtls_psa_sign_hash_get_num_ops( |
| 3489 | const mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
| 3490 | { |
| 3491 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3492 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3493 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3494 | |
| 3495 | return operation->restart_ctx.ecp.ops_done; |
| 3496 | #else |
| 3497 | (void) operation; |
| 3498 | return 0; |
| 3499 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3500 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3501 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3502 | } |
| 3503 | |
| 3504 | uint32_t mbedtls_psa_verify_hash_get_num_ops( |
| 3505 | const mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 3506 | { |
| 3507 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3508 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3509 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3510 | |
| 3511 | return operation->restart_ctx.ecp.ops_done; |
| 3512 | #else |
| 3513 | (void) operation; |
| 3514 | return 0; |
| 3515 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3516 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3517 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3518 | } |
| 3519 | |
| 3520 | psa_status_t mbedtls_psa_sign_hash_start( |
| 3521 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3522 | const psa_key_attributes_t *attributes, const uint8_t *key_buffer, |
| 3523 | size_t key_buffer_size, psa_algorithm_t alg, |
| 3524 | const uint8_t *hash, size_t hash_length) |
| 3525 | { |
| 3526 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3527 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3528 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3529 | return PSA_ERROR_NOT_SUPPORTED; |
| 3530 | } |
| 3531 | |
| 3532 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 3533 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3534 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3535 | |
| 3536 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3537 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3538 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3539 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3540 | /* Ensure default is set even if |
| 3541 | * mbedtls_psa_interruptible_set_max_ops() has not been called. */ |
| 3542 | mbedtls_psa_interruptible_set_max_ops( |
| 3543 | mbedtls_psa_interruptible_get_max_ops()); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3544 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3545 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 3546 | attributes->core.bits, |
| 3547 | key_buffer, |
| 3548 | key_buffer_size, |
| 3549 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3550 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3551 | if (status != PSA_SUCCESS) { |
| 3552 | return status; |
| 3553 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3554 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3555 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3556 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3557 | operation->coordinate_bytes = PSA_BITS_TO_BYTES( |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3558 | operation->ctx->grp.pbits); |
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 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
| 3561 | operation->md_alg = mbedtls_hash_info_md_from_psa(hash_alg); |
| 3562 | operation->alg = alg; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3563 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3564 | operation->hash = mbedtls_calloc(1, hash_length); |
| 3565 | |
| 3566 | if (operation->hash == NULL) { |
| 3567 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 3568 | } |
| 3569 | |
| 3570 | memcpy(operation->hash, hash, hash_length); |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3571 | operation->hash_length = hash_length; |
| 3572 | |
| 3573 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3574 | |
| 3575 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3576 | (void) operation; |
| 3577 | (void) key_buffer; |
| 3578 | (void) key_buffer_size; |
| 3579 | (void) alg; |
| 3580 | (void) hash; |
| 3581 | (void) hash_length; |
| 3582 | (void) status; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3583 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3584 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3585 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3586 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3587 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | psa_status_t mbedtls_psa_sign_hash_complete( |
| 3591 | mbedtls_psa_sign_hash_interruptible_operation_t *operation, |
| 3592 | uint8_t *signature, size_t signature_size, |
| 3593 | size_t *signature_length) |
| 3594 | { |
| 3595 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3596 | |
| 3597 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3598 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3599 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3600 | |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 3601 | mbedtls_mpi r; |
| 3602 | mbedtls_mpi s; |
| 3603 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3604 | if (signature_size < 2 * operation->coordinate_bytes) { |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3605 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 3606 | } |
| 3607 | |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3608 | mbedtls_mpi_init(&r); |
| 3609 | mbedtls_mpi_init(&s); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3610 | |
| 3611 | if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) { |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3612 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3613 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3614 | status = mbedtls_to_psa_error( |
| 3615 | mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3616 | &r, |
| 3617 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3618 | &operation->ctx->d, |
| 3619 | operation->hash, |
| 3620 | operation->hash_length, |
| 3621 | operation->md_alg, |
| 3622 | mbedtls_psa_get_random, |
| 3623 | MBEDTLS_PSA_RANDOM_STATE, |
| 3624 | &operation->restart_ctx)); |
| 3625 | #else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3626 | return PSA_ERROR_NOT_SUPPORTED; |
| 3627 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3628 | } else { |
| 3629 | |
| 3630 | status = mbedtls_to_psa_error( |
| 3631 | mbedtls_ecdsa_sign_restartable(&operation->ctx->grp, |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3632 | &r, |
| 3633 | &s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3634 | &operation->ctx->d, |
| 3635 | operation->hash, |
| 3636 | operation->hash_length, |
| 3637 | mbedtls_psa_get_random, |
| 3638 | MBEDTLS_PSA_RANDOM_STATE, |
| 3639 | mbedtls_psa_get_random, |
| 3640 | MBEDTLS_PSA_RANDOM_STATE, |
| 3641 | &operation->restart_ctx)); |
| 3642 | } |
| 3643 | |
| 3644 | if (status != PSA_SUCCESS) { |
| 3645 | return status; |
| 3646 | } else { |
| 3647 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3648 | mbedtls_mpi_write_binary(&r, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3649 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3650 | operation->coordinate_bytes) |
| 3651 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3652 | |
| 3653 | if (status != PSA_SUCCESS) { |
| 3654 | return status; |
| 3655 | } |
| 3656 | |
| 3657 | status = mbedtls_to_psa_error( |
Paul Elliott | 4684525 | 2023-02-03 14:59:11 +0000 | [diff] [blame] | 3658 | mbedtls_mpi_write_binary(&s, |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3659 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3660 | operation->coordinate_bytes, |
| 3661 | operation->coordinate_bytes) |
| 3662 | ); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3663 | |
| 3664 | if (status != PSA_SUCCESS) { |
| 3665 | return status; |
| 3666 | } |
| 3667 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3668 | *signature_length = operation->coordinate_bytes * 2; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3669 | |
| 3670 | return PSA_SUCCESS; |
| 3671 | } |
| 3672 | #else |
| 3673 | |
| 3674 | (void) operation; |
| 3675 | (void) status; |
| 3676 | (void) signature; |
| 3677 | (void) signature_size; |
| 3678 | (void) signature_length; |
| 3679 | |
| 3680 | return PSA_ERROR_NOT_SUPPORTED; |
| 3681 | |
| 3682 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3683 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3684 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3685 | } |
| 3686 | |
| 3687 | psa_status_t mbedtls_psa_sign_hash_abort( |
| 3688 | mbedtls_psa_sign_hash_interruptible_operation_t *operation) |
| 3689 | { |
| 3690 | |
| 3691 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3692 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3693 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3694 | |
| 3695 | if (operation->ctx) { |
| 3696 | mbedtls_ecdsa_free(operation->ctx); |
| 3697 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3698 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3701 | mbedtls_free(operation->hash); |
| 3702 | operation->hash = NULL; |
| 3703 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3704 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 3705 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3706 | return PSA_SUCCESS; |
| 3707 | |
| 3708 | #else |
| 3709 | |
| 3710 | (void) operation; |
| 3711 | |
| 3712 | return PSA_ERROR_NOT_SUPPORTED; |
| 3713 | |
| 3714 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3715 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3716 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3717 | } |
| 3718 | |
| 3719 | psa_status_t mbedtls_psa_verify_hash_start( |
| 3720 | mbedtls_psa_verify_hash_interruptible_operation_t *operation, |
| 3721 | const psa_key_attributes_t *attributes, |
| 3722 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3723 | psa_algorithm_t alg, |
| 3724 | const uint8_t *hash, size_t hash_length, |
| 3725 | const uint8_t *signature, size_t signature_length) |
| 3726 | { |
| 3727 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3728 | size_t coordinate_bytes = 0; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3729 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3730 | if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3731 | return PSA_ERROR_NOT_SUPPORTED; |
| 3732 | } |
| 3733 | |
| 3734 | if (!PSA_ALG_IS_ECDSA(alg)) { |
Paul Elliott | 813f9cd | 2023-02-05 15:28:46 +0000 | [diff] [blame] | 3735 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3736 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3737 | |
| 3738 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3739 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3740 | defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3741 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3742 | /* Ensure default is set even if |
| 3743 | * mbedtls_psa_interruptible_set_max_ops() has not been called. */ |
| 3744 | mbedtls_psa_interruptible_set_max_ops( |
| 3745 | mbedtls_psa_interruptible_get_max_ops()); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3746 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3747 | status = mbedtls_psa_ecp_load_representation(attributes->core.type, |
| 3748 | attributes->core.bits, |
| 3749 | key_buffer, |
| 3750 | key_buffer_size, |
| 3751 | &operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3752 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3753 | if (status != PSA_SUCCESS) { |
| 3754 | return status; |
| 3755 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3756 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3757 | coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.pbits); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3758 | |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3759 | if (signature_length != 2 * coordinate_bytes) { |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3760 | return PSA_ERROR_INVALID_SIGNATURE; |
| 3761 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3762 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3763 | mbedtls_mpi_init(&operation->r); |
| 3764 | status = mbedtls_to_psa_error( |
| 3765 | mbedtls_mpi_read_binary(&operation->r, |
| 3766 | signature, |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3767 | coordinate_bytes)); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3768 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3769 | if (status != PSA_SUCCESS) { |
| 3770 | return status; |
| 3771 | } |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3772 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3773 | mbedtls_mpi_init(&operation->s); |
| 3774 | status = mbedtls_to_psa_error( |
| 3775 | mbedtls_mpi_read_binary(&operation->s, |
| 3776 | signature + |
Paul Elliott | 1bc59df | 2023-02-05 13:41:57 +0000 | [diff] [blame] | 3777 | coordinate_bytes, |
| 3778 | coordinate_bytes)); |
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 | eefe472 | 2023-02-06 15:59:09 +0000 | [diff] [blame] | 3784 | int ret = mbedtls_psa_ecp_load_public_part(operation->ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3785 | |
Paul Elliott | eefe472 | 2023-02-06 15:59:09 +0000 | [diff] [blame] | 3786 | if (ret != 0) { |
| 3787 | return mbedtls_to_psa_error(ret); |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 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 | mbedtls_ecdsa_restart_init(&operation->restart_ctx); |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3791 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3792 | operation->hash = mbedtls_calloc(1, hash_length); |
| 3793 | |
| 3794 | if (operation->hash == NULL) { |
| 3795 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 3796 | } |
| 3797 | |
| 3798 | memcpy(operation->hash, hash, hash_length); |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3799 | operation->hash_length = hash_length; |
| 3800 | |
| 3801 | return PSA_SUCCESS; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3802 | #else |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3803 | (void) operation; |
| 3804 | (void) key_buffer; |
| 3805 | (void) key_buffer_size; |
| 3806 | (void) alg; |
| 3807 | (void) hash; |
| 3808 | (void) hash_length; |
| 3809 | (void) signature; |
| 3810 | (void) signature_length; |
| 3811 | (void) status; |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 3812 | (void) coordinate_bytes; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3813 | |
Paul Elliott | 068fe07 | 2023-01-16 13:59:15 +0000 | [diff] [blame] | 3814 | return PSA_ERROR_NOT_SUPPORTED; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3815 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3816 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3817 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | psa_status_t mbedtls_psa_verify_hash_complete( |
| 3821 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 3822 | { |
| 3823 | |
| 3824 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3825 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3826 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3827 | |
| 3828 | return mbedtls_to_psa_error( |
| 3829 | mbedtls_ecdsa_verify_restartable(&operation->ctx->grp, |
| 3830 | operation->hash, |
| 3831 | operation->hash_length, |
| 3832 | &operation->ctx->Q, |
| 3833 | &operation->r, |
| 3834 | &operation->s, |
| 3835 | &operation->restart_ctx)); |
| 3836 | |
| 3837 | #else |
| 3838 | (void) operation; |
| 3839 | |
| 3840 | return PSA_ERROR_NOT_SUPPORTED; |
| 3841 | |
| 3842 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3843 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3844 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3845 | } |
| 3846 | |
| 3847 | psa_status_t mbedtls_psa_verify_hash_abort( |
| 3848 | mbedtls_psa_verify_hash_interruptible_operation_t *operation) |
| 3849 | { |
| 3850 | |
| 3851 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3852 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 3853 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 3854 | |
| 3855 | if (operation->ctx) { |
| 3856 | mbedtls_ecdsa_free(operation->ctx); |
| 3857 | mbedtls_free(operation->ctx); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3858 | operation->ctx = NULL; |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3859 | } |
| 3860 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 3861 | mbedtls_free(operation->hash); |
| 3862 | operation->hash = NULL; |
| 3863 | |
Paul Elliott | 588f8ed | 2022-12-02 18:10:26 +0000 | [diff] [blame] | 3864 | mbedtls_ecdsa_restart_free(&operation->restart_ctx); |
| 3865 | |
| 3866 | mbedtls_mpi_free(&operation->r); |
| 3867 | mbedtls_mpi_free(&operation->s); |
| 3868 | |
| 3869 | return PSA_SUCCESS; |
| 3870 | |
| 3871 | #else |
| 3872 | (void) operation; |
| 3873 | |
| 3874 | return PSA_ERROR_NOT_SUPPORTED; |
| 3875 | |
| 3876 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3877 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 3878 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
| 3879 | } |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3880 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3881 | /****************************************************************/ |
| 3882 | /* Symmetric cryptography */ |
| 3883 | /****************************************************************/ |
| 3884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3885 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 3886 | mbedtls_svc_key_id_t key, |
| 3887 | psa_algorithm_t alg, |
| 3888 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3889 | { |
| 3890 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3891 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3892 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3893 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 3894 | PSA_KEY_USAGE_ENCRYPT : |
| 3895 | PSA_KEY_USAGE_DECRYPT); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3896 | |
| 3897 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3898 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3899 | status = PSA_ERROR_BAD_STATE; |
| 3900 | goto exit; |
| 3901 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3903 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 3904 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3905 | goto exit; |
| 3906 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3908 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 3909 | if (status != PSA_SUCCESS) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3910 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3911 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3912 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3913 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3914 | * 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] | 3915 | * so we only set it (in the driver wrapper) after resources have been |
| 3916 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3917 | operation->iv_set = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3918 | if (alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3919 | operation->iv_required = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3920 | } else { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3921 | operation->iv_required = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3922 | } |
| 3923 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3924 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3925 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3926 | .core = slot->attr |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3927 | }; |
| 3928 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3929 | /* Try doing the operation through a driver before using software fallback. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3930 | if (cipher_operation == MBEDTLS_ENCRYPT) { |
| 3931 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, |
| 3932 | &attributes, |
| 3933 | slot->key.data, |
| 3934 | slot->key.bytes, |
| 3935 | alg); |
| 3936 | } else { |
| 3937 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, |
| 3938 | &attributes, |
| 3939 | slot->key.data, |
| 3940 | slot->key.bytes, |
| 3941 | alg); |
| 3942 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3943 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3944 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3945 | if (status != PSA_SUCCESS) { |
| 3946 | psa_cipher_abort(operation); |
| 3947 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3949 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3951 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3952 | } |
| 3953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3954 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 3955 | mbedtls_svc_key_id_t key, |
| 3956 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3957 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3958 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3959 | } |
| 3960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3961 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 3962 | mbedtls_svc_key_id_t key, |
| 3963 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3964 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3965 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3966 | } |
| 3967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3968 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 3969 | uint8_t *iv, |
| 3970 | size_t iv_size, |
| 3971 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3972 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3973 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3974 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3975 | size_t default_iv_length; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3977 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3978 | status = PSA_ERROR_BAD_STATE; |
| 3979 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3980 | } |
| 3981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3982 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3983 | status = PSA_ERROR_BAD_STATE; |
| 3984 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3985 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3986 | |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3987 | default_iv_length = operation->default_iv_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3988 | if (iv_size < default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3989 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3990 | goto exit; |
| 3991 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3993 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3994 | status = PSA_ERROR_GENERIC_ERROR; |
| 3995 | goto exit; |
| 3996 | } |
| 3997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3998 | status = psa_generate_random(local_iv, default_iv_length); |
| 3999 | if (status != PSA_SUCCESS) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4000 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4001 | } |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4003 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4004 | local_iv, default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 4005 | |
| 4006 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4007 | if (status == PSA_SUCCESS) { |
| 4008 | memcpy(iv, local_iv, default_iv_length); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4009 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4010 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4011 | } else { |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4012 | *iv_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4013 | psa_cipher_abort(operation); |
Ronald Cron | 2fb9052 | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 4014 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4016 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4017 | } |
| 4018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4019 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 4020 | const uint8_t *iv, |
| 4021 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4022 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4023 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4025 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4026 | status = PSA_ERROR_BAD_STATE; |
| 4027 | goto exit; |
| 4028 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 4029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4030 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4031 | status = PSA_ERROR_BAD_STATE; |
| 4032 | goto exit; |
| 4033 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 4034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4036 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4037 | goto exit; |
| 4038 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4040 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 4041 | iv, |
| 4042 | iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 4043 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4044 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4045 | if (status == PSA_SUCCESS) { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 4046 | operation->iv_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4047 | } else { |
| 4048 | psa_cipher_abort(operation); |
| 4049 | } |
| 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_update(psa_cipher_operation_t *operation, |
| 4054 | const uint8_t *input, |
| 4055 | size_t input_length, |
| 4056 | uint8_t *output, |
| 4057 | size_t output_size, |
| 4058 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4059 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 4060 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4062 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4063 | status = PSA_ERROR_BAD_STATE; |
| 4064 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4065 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4067 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4068 | status = PSA_ERROR_BAD_STATE; |
| 4069 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4070 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4072 | status = psa_driver_wrapper_cipher_update(operation, |
| 4073 | input, |
| 4074 | input_length, |
| 4075 | output, |
| 4076 | output_size, |
| 4077 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4078 | |
| 4079 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4080 | if (status != PSA_SUCCESS) { |
| 4081 | psa_cipher_abort(operation); |
| 4082 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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_finish(psa_cipher_operation_t *operation, |
| 4088 | uint8_t *output, |
| 4089 | size_t output_size, |
| 4090 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4091 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4092 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 4093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4094 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4095 | status = PSA_ERROR_BAD_STATE; |
| 4096 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4097 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4099 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4100 | status = PSA_ERROR_BAD_STATE; |
| 4101 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 4102 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 4103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4104 | status = psa_driver_wrapper_cipher_finish(operation, |
| 4105 | output, |
| 4106 | output_size, |
| 4107 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 4108 | |
| 4109 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4110 | if (status == PSA_SUCCESS) { |
| 4111 | return psa_cipher_abort(operation); |
| 4112 | } else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4113 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4114 | (void) psa_cipher_abort(operation); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 4115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4116 | return status; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 4117 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4118 | } |
| 4119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4120 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4121 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4122 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 4123 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4124 | * in use. It's ok to call abort on such an object, and there's |
| 4125 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4126 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 4127 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4129 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 4130 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 4131 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4132 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 4133 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 4134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4135 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 4136 | } |
| 4137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4138 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 4139 | psa_algorithm_t alg, |
| 4140 | const uint8_t *input, |
| 4141 | size_t input_length, |
| 4142 | uint8_t *output, |
| 4143 | size_t output_size, |
| 4144 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4145 | { |
| 4146 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4147 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4148 | psa_key_slot_t *slot = NULL; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4149 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 4150 | size_t default_iv_length = 0; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4152 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4153 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4154 | goto exit; |
| 4155 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4157 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4158 | PSA_KEY_USAGE_ENCRYPT, |
| 4159 | alg); |
| 4160 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4161 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4162 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4163 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4164 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4165 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4166 | }; |
| 4167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4168 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
| 4169 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4170 | status = PSA_ERROR_GENERIC_ERROR; |
| 4171 | goto exit; |
| 4172 | } |
| 4173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4174 | if (default_iv_length > 0) { |
| 4175 | if (output_size < default_iv_length) { |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4176 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4177 | goto exit; |
| 4178 | } |
| 4179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4180 | status = psa_generate_random(local_iv, default_iv_length); |
| 4181 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4182 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4183 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4184 | } |
| 4185 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4186 | status = psa_driver_wrapper_cipher_encrypt( |
| 4187 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4188 | alg, local_iv, default_iv_length, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4189 | mbedtls_buffer_offset(output, default_iv_length), |
| 4190 | output_size - default_iv_length, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4191 | |
| 4192 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4193 | unlock_status = psa_unlock_key_slot(slot); |
| 4194 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4195 | status = unlock_status; |
Ronald Cron | c6e6f50 | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 4196 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4198 | if (status == PSA_SUCCESS) { |
| 4199 | if (default_iv_length > 0) { |
| 4200 | memcpy(output, local_iv, default_iv_length); |
| 4201 | } |
| 4202 | *output_length += default_iv_length; |
| 4203 | } else { |
| 4204 | *output_length = 0; |
| 4205 | } |
| 4206 | |
| 4207 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4208 | } |
| 4209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4210 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 4211 | psa_algorithm_t alg, |
| 4212 | const uint8_t *input, |
| 4213 | size_t input_length, |
| 4214 | uint8_t *output, |
| 4215 | size_t output_size, |
| 4216 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4217 | { |
| 4218 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4219 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4220 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 4221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4222 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4223 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4224 | goto exit; |
| 4225 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4227 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 4228 | PSA_KEY_USAGE_DECRYPT, |
| 4229 | alg); |
| 4230 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4231 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4232 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4233 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4234 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4235 | .core = slot->attr |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4236 | }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4238 | if (alg == PSA_ALG_CCM_STAR_NO_TAG && |
| 4239 | input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 4240 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4241 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4242 | } 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] | 4243 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4244 | goto exit; |
| 4245 | } |
| 4246 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 4247 | status = psa_driver_wrapper_cipher_decrypt( |
| 4248 | &attributes, slot->key.data, slot->key.bytes, |
| 4249 | alg, input, input_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4250 | output, output_size, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4251 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 4252 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4253 | unlock_status = psa_unlock_key_slot(slot); |
| 4254 | if (status == PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4255 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4256 | } |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4258 | if (status != PSA_SUCCESS) { |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4259 | *output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4260 | } |
Ronald Cron | 2391952 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 4261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4262 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4266 | /****************************************************************/ |
| 4267 | /* AEAD */ |
| 4268 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 4269 | |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4270 | /* Helper function to get the base algorithm from its variants. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4271 | 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] | 4272 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4273 | return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | /* Helper function to perform common nonce length checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4277 | static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg, |
| 4278 | size_t nonce_length) |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4279 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | baff51c | 2021-09-28 17:44:45 +0100 | [diff] [blame] | 4281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4282 | switch (base_alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4283 | #if defined(PSA_WANT_ALG_GCM) |
| 4284 | case PSA_ALG_GCM: |
| 4285 | /* Not checking max nonce size here as GCM spec allows almost |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4286 | * arbitrarily large nonces. Please note that we do not generally |
| 4287 | * recommend the usage of nonces of greater length than |
| 4288 | * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter |
| 4289 | * size, which can then lead to collisions if you encrypt a very |
| 4290 | * large number of messages.*/ |
| 4291 | if (nonce_length != 0) { |
| 4292 | return PSA_SUCCESS; |
| 4293 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4294 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4295 | #endif /* PSA_WANT_ALG_GCM */ |
| 4296 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4297 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4298 | if (nonce_length >= 7 && nonce_length <= 13) { |
| 4299 | return PSA_SUCCESS; |
| 4300 | } |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4301 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4302 | #endif /* PSA_WANT_ALG_CCM */ |
| 4303 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4304 | case PSA_ALG_CHACHA20_POLY1305: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4305 | if (nonce_length == 12) { |
| 4306 | return PSA_SUCCESS; |
| 4307 | } else if (nonce_length == 8) { |
| 4308 | return PSA_ERROR_NOT_SUPPORTED; |
| 4309 | } |
Bence Szépkúti | 6d48e20 | 2021-11-15 20:04:15 +0100 | [diff] [blame] | 4310 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4311 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4312 | default: |
Przemek Stekiel | 4c49927 | 2022-09-27 13:55:37 +0200 | [diff] [blame] | 4313 | (void) nonce_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4314 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4315 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4317 | return PSA_ERROR_INVALID_ARGUMENT; |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4318 | } |
| 4319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4320 | 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] | 4321 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4322 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 4323 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4324 | } |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4326 | return PSA_SUCCESS; |
Bence Szépkúti | 08f3465 | 2021-12-08 21:07:13 +0100 | [diff] [blame] | 4327 | } |
| 4328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4329 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 4330 | psa_algorithm_t alg, |
| 4331 | const uint8_t *nonce, |
| 4332 | size_t nonce_length, |
| 4333 | const uint8_t *additional_data, |
| 4334 | size_t additional_data_length, |
| 4335 | const uint8_t *plaintext, |
| 4336 | size_t plaintext_length, |
| 4337 | uint8_t *ciphertext, |
| 4338 | size_t ciphertext_size, |
| 4339 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4340 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4341 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4342 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4343 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 4344 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 4345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4346 | status = psa_aead_check_algorithm(alg); |
| 4347 | if (status != PSA_SUCCESS) { |
| 4348 | return status; |
| 4349 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4350 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4351 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4352 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 4353 | if (status != PSA_SUCCESS) { |
| 4354 | return status; |
| 4355 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4356 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4357 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4358 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4359 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4361 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4362 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4363 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4364 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4365 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4366 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4367 | &attributes, slot->key.data, slot->key.bytes, |
| 4368 | alg, |
| 4369 | nonce, nonce_length, |
| 4370 | additional_data, additional_data_length, |
| 4371 | plaintext, plaintext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4372 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | if (status != PSA_SUCCESS && ciphertext_size != 0) { |
| 4375 | memset(ciphertext, 0, ciphertext_size); |
| 4376 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4377 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4378 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4379 | psa_unlock_key_slot(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4381 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4382 | } |
| 4383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4384 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 4385 | psa_algorithm_t alg, |
| 4386 | const uint8_t *nonce, |
| 4387 | size_t nonce_length, |
| 4388 | const uint8_t *additional_data, |
| 4389 | size_t additional_data_length, |
| 4390 | const uint8_t *ciphertext, |
| 4391 | size_t ciphertext_length, |
| 4392 | uint8_t *plaintext, |
| 4393 | size_t plaintext_size, |
| 4394 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4395 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4396 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4397 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4398 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 4399 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 4400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4401 | status = psa_aead_check_algorithm(alg); |
| 4402 | if (status != PSA_SUCCESS) { |
| 4403 | return status; |
| 4404 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 4405 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 4406 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4407 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 4408 | if (status != PSA_SUCCESS) { |
| 4409 | return status; |
| 4410 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4411 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4412 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4413 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4414 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 4415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4416 | status = psa_aead_check_nonce_length(alg, nonce_length); |
| 4417 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4418 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4419 | } |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4420 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 4421 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4422 | &attributes, slot->key.data, slot->key.bytes, |
| 4423 | alg, |
| 4424 | nonce, nonce_length, |
| 4425 | additional_data, additional_data_length, |
| 4426 | ciphertext, ciphertext_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4427 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 4428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4429 | if (status != PSA_SUCCESS && plaintext_size != 0) { |
| 4430 | memset(plaintext, 0, plaintext_size); |
| 4431 | } |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 4432 | |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4433 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4434 | psa_unlock_key_slot(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 4435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4436 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 4437 | } |
| 4438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4439 | static psa_status_t psa_validate_tag_length(psa_algorithm_t alg) |
| 4440 | { |
| 4441 | const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4443 | switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) { |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4444 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4445 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4446 | /* 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] | 4447 | if (tag_len < 4 || tag_len > 16 || tag_len % 2) { |
| 4448 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4449 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4450 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4451 | #endif /* PSA_WANT_ALG_CCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4452 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4453 | #if defined(PSA_WANT_ALG_GCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4454 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4455 | /* 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] | 4456 | if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) { |
| 4457 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4458 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4459 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4460 | #endif /* PSA_WANT_ALG_GCM */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4461 | |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4462 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4463 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4464 | /* We only support the default tag length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4465 | if (tag_len != 16) { |
| 4466 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4467 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4468 | break; |
Przemek Stekiel | 86679c7 | 2022-10-06 17:06:56 +0200 | [diff] [blame] | 4469 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4470 | |
| 4471 | default: |
| 4472 | (void) tag_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4473 | return PSA_ERROR_NOT_SUPPORTED; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4474 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4475 | return PSA_SUCCESS; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4476 | } |
| 4477 | |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4478 | /* Set the key for a multipart authenticated operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4479 | static psa_status_t psa_aead_setup(psa_aead_operation_t *operation, |
| 4480 | int is_encrypt, |
| 4481 | mbedtls_svc_key_id_t key, |
| 4482 | psa_algorithm_t alg) |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4483 | { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4484 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4485 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4486 | psa_key_slot_t *slot = NULL; |
| 4487 | psa_key_usage_t key_usage = 0; |
| 4488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4489 | status = psa_aead_check_algorithm(alg); |
| 4490 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4491 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4492 | } |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4494 | if (operation->id != 0) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4495 | status = PSA_ERROR_BAD_STATE; |
| 4496 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4497 | } |
| 4498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4499 | if (operation->nonce_set || operation->lengths_set || |
| 4500 | operation->ad_started || operation->body_started) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4501 | status = PSA_ERROR_BAD_STATE; |
| 4502 | goto exit; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4503 | } |
| 4504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4505 | if (is_encrypt) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4506 | key_usage = PSA_KEY_USAGE_ENCRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4507 | } else { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4508 | key_usage = PSA_KEY_USAGE_DECRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4509 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage, |
| 4512 | alg); |
| 4513 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4514 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4515 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4516 | |
| 4517 | psa_key_attributes_t attributes = { |
| 4518 | .core = slot->attr |
| 4519 | }; |
| 4520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4521 | if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) { |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 4522 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4523 | } |
Przemek Stekiel | 6ab5076 | 2022-10-08 17:54:30 +0200 | [diff] [blame] | 4524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4525 | if (is_encrypt) { |
| 4526 | status = psa_driver_wrapper_aead_encrypt_setup(operation, |
| 4527 | &attributes, |
| 4528 | slot->key.data, |
| 4529 | slot->key.bytes, |
| 4530 | alg); |
| 4531 | } else { |
| 4532 | status = psa_driver_wrapper_aead_decrypt_setup(operation, |
| 4533 | &attributes, |
| 4534 | slot->key.data, |
| 4535 | slot->key.bytes, |
| 4536 | alg); |
| 4537 | } |
| 4538 | if (status != PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4539 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4540 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4542 | operation->key_type = psa_get_key_type(&attributes); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4543 | |
| 4544 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4545 | unlock_status = psa_unlock_key_slot(slot); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4547 | if (status == PSA_SUCCESS) { |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4548 | status = unlock_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4549 | operation->alg = psa_aead_get_base_algorithm(alg); |
Paul Elliott | d9343f2 | 2021-08-23 18:59:49 +0100 | [diff] [blame] | 4550 | operation->is_encrypt = is_encrypt; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4551 | } else { |
| 4552 | psa_aead_abort(operation); |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4553 | } |
Paul Elliott | e0a12bd | 2021-08-19 18:55:56 +0100 | [diff] [blame] | 4554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4555 | return status; |
Paul Elliott | c2b7144 | 2021-06-24 18:17:52 +0100 | [diff] [blame] | 4556 | } |
| 4557 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4558 | /* Set the key for a multipart authenticated encryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4559 | psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation, |
| 4560 | mbedtls_svc_key_id_t key, |
| 4561 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4562 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4563 | return psa_aead_setup(operation, 1, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | /* Set the key for a multipart authenticated decryption operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4567 | psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation, |
| 4568 | mbedtls_svc_key_id_t key, |
| 4569 | psa_algorithm_t alg) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4570 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4571 | return psa_aead_setup(operation, 0, key, alg); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | /* Generate a random nonce / IV for multipart AEAD operation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4575 | psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation, |
| 4576 | uint8_t *nonce, |
| 4577 | size_t nonce_size, |
| 4578 | size_t *nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4579 | { |
| 4580 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 4581 | uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE]; |
Paul Elliott | b91da71 | 2021-05-20 14:43:47 +0100 | [diff] [blame] | 4582 | size_t required_nonce_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4583 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4584 | *nonce_length = 0; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4586 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4587 | status = PSA_ERROR_BAD_STATE; |
| 4588 | goto exit; |
| 4589 | } |
| 4590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4591 | if (operation->nonce_set || !operation->is_encrypt) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4592 | status = PSA_ERROR_BAD_STATE; |
| 4593 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4594 | } |
| 4595 | |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4596 | /* For CCM, this size may not be correct according to the PSA |
| 4597 | * specification. The PSA Crypto 1.0.1 specification states: |
| 4598 | * |
| 4599 | * CCM encodes the plaintext length pLen in L octets, with L the smallest |
| 4600 | * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes. |
| 4601 | * |
| 4602 | * However this restriction that L has to be the smallest integer is not |
| 4603 | * applied in practice, and it is not implementable here since the |
| 4604 | * plaintext length may or may not be known at this time. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4605 | required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type, |
| 4606 | operation->alg); |
| 4607 | if (nonce_size < required_nonce_size) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4608 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 4609 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4610 | } |
| 4611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4612 | status = psa_generate_random(local_nonce, required_nonce_size); |
| 4613 | if (status != PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4614 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4615 | } |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4617 | status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4618 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4619 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4620 | if (status == PSA_SUCCESS) { |
| 4621 | memcpy(nonce, local_nonce, required_nonce_size); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4622 | *nonce_length = required_nonce_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4623 | } else { |
| 4624 | psa_aead_abort(operation); |
Ronald Cron | cae5909 | 2021-11-26 18:53:58 +0100 | [diff] [blame] | 4625 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4627 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | /* Set the nonce for a multipart authenticated encryption or decryption |
| 4631 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4632 | psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation, |
| 4633 | const uint8_t *nonce, |
| 4634 | size_t nonce_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4635 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4636 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4638 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4639 | status = PSA_ERROR_BAD_STATE; |
| 4640 | goto exit; |
| 4641 | } |
| 4642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4643 | if (operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4644 | status = PSA_ERROR_BAD_STATE; |
| 4645 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4646 | } |
| 4647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4648 | status = psa_aead_check_nonce_length(operation->alg, nonce_length); |
| 4649 | if (status != PSA_SUCCESS) { |
Paul Elliott | bb0f9e1 | 2021-09-28 11:14:27 +0100 | [diff] [blame] | 4650 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4651 | goto exit; |
Paul Elliott | 4ed1ed1 | 2021-09-27 18:09:28 +0100 | [diff] [blame] | 4652 | } |
Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 4653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4654 | status = psa_driver_wrapper_aead_set_nonce(operation, nonce, |
| 4655 | nonce_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4656 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4657 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4658 | if (status == PSA_SUCCESS) { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4659 | operation->nonce_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4660 | } else { |
| 4661 | psa_aead_abort(operation); |
| 4662 | } |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4664 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4665 | } |
| 4666 | |
| 4667 | /* Declare the lengths of the message and additional data for multipart AEAD. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4668 | psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation, |
| 4669 | size_t ad_length, |
| 4670 | size_t plaintext_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4671 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4672 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4674 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4675 | status = PSA_ERROR_BAD_STATE; |
| 4676 | goto exit; |
| 4677 | } |
| 4678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4679 | if (operation->lengths_set || operation->ad_started || |
| 4680 | operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4681 | status = PSA_ERROR_BAD_STATE; |
| 4682 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4683 | } |
| 4684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4685 | switch (operation->alg) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4686 | #if defined(PSA_WANT_ALG_GCM) |
| 4687 | case PSA_ALG_GCM: |
| 4688 | /* 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] | 4689 | * bits. Without the guard this code will generate warnings on 32bit |
| 4690 | * builds. */ |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 4691 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4692 | if (((uint64_t) ad_length) >> 61 != 0 || |
| 4693 | ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4694 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4695 | goto exit; |
| 4696 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 4697 | #endif |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4698 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4699 | #endif /* PSA_WANT_ALG_GCM */ |
| 4700 | #if defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4701 | case PSA_ALG_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4702 | if (ad_length > 0xFF00) { |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4703 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4704 | goto exit; |
| 4705 | } |
| 4706 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4707 | #endif /* PSA_WANT_ALG_CCM */ |
| 4708 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4709 | case PSA_ALG_CHACHA20_POLY1305: |
| 4710 | /* No length restrictions for ChaChaPoly. */ |
| 4711 | break; |
Paul Elliott | e716e6c | 2021-09-29 14:10:20 +0100 | [diff] [blame] | 4712 | #endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */ |
Przemyslaw Stekiel | 4cad4fc | 2021-10-13 11:12:08 +0200 | [diff] [blame] | 4713 | default: |
| 4714 | break; |
| 4715 | } |
Paul Elliott | 325d374 | 2021-09-27 17:56:28 +0100 | [diff] [blame] | 4716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4717 | status = psa_driver_wrapper_aead_set_lengths(operation, ad_length, |
| 4718 | plaintext_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4719 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4720 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4721 | if (status == PSA_SUCCESS) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4722 | operation->ad_remaining = ad_length; |
| 4723 | operation->body_remaining = plaintext_length; |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4724 | operation->lengths_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4725 | } else { |
| 4726 | psa_aead_abort(operation); |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4727 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4729 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4730 | } |
Paul Elliott | 3d7d52c | 2021-09-01 10:33:14 +0100 | [diff] [blame] | 4731 | |
| 4732 | /* Pass additional data to an active multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4733 | psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation, |
| 4734 | const uint8_t *input, |
| 4735 | size_t input_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4736 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4737 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4739 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4740 | status = PSA_ERROR_BAD_STATE; |
| 4741 | goto exit; |
| 4742 | } |
| 4743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4744 | if (!operation->nonce_set || operation->body_started) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4745 | status = PSA_ERROR_BAD_STATE; |
| 4746 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4747 | } |
| 4748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4749 | if (operation->lengths_set) { |
| 4750 | if (operation->ad_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4751 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4752 | goto exit; |
| 4753 | } |
| 4754 | |
| 4755 | operation->ad_remaining -= input_length; |
| 4756 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4757 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4758 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4759 | status = PSA_ERROR_BAD_STATE; |
| 4760 | goto exit; |
| 4761 | } |
| 4762 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4764 | status = psa_driver_wrapper_aead_update_ad(operation, input, |
| 4765 | input_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4766 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4767 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4769 | operation->ad_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4770 | } else { |
| 4771 | psa_aead_abort(operation); |
| 4772 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4774 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4775 | } |
| 4776 | |
| 4777 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 4778 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4779 | psa_status_t psa_aead_update(psa_aead_operation_t *operation, |
| 4780 | const uint8_t *input, |
| 4781 | size_t input_length, |
| 4782 | uint8_t *output, |
| 4783 | size_t output_size, |
| 4784 | size_t *output_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4785 | { |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4786 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4787 | |
| 4788 | *output_length = 0; |
| 4789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4790 | if (operation->id == 0) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4791 | status = PSA_ERROR_BAD_STATE; |
| 4792 | goto exit; |
| 4793 | } |
| 4794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4795 | if (!operation->nonce_set) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4796 | status = PSA_ERROR_BAD_STATE; |
| 4797 | goto exit; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4798 | } |
| 4799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4800 | if (operation->lengths_set) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4801 | /* Additional data length was supplied, but not all the additional |
| 4802 | data was supplied.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4803 | if (operation->ad_remaining != 0) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4804 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4805 | goto exit; |
| 4806 | } |
| 4807 | |
| 4808 | /* Too much data provided. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | if (operation->body_remaining < input_length) { |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4810 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4811 | goto exit; |
| 4812 | } |
| 4813 | |
| 4814 | operation->body_remaining -= input_length; |
| 4815 | } |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4816 | #if defined(PSA_WANT_ALG_CCM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4817 | else if (operation->alg == PSA_ALG_CCM) { |
Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 4818 | status = PSA_ERROR_BAD_STATE; |
| 4819 | goto exit; |
| 4820 | } |
| 4821 | #endif /* PSA_WANT_ALG_CCM */ |
Paul Elliott | ee4ffe0 | 2021-05-20 17:25:06 +0100 | [diff] [blame] | 4822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4823 | status = psa_driver_wrapper_aead_update(operation, input, input_length, |
| 4824 | output, output_size, |
| 4825 | output_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4826 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4827 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4828 | if (status == PSA_SUCCESS) { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4829 | operation->body_started = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4830 | } else { |
| 4831 | psa_aead_abort(operation); |
| 4832 | } |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4835 | } |
| 4836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4837 | 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] | 4838 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4839 | if (operation->id == 0 || !operation->nonce_set) { |
| 4840 | return PSA_ERROR_BAD_STATE; |
| 4841 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | if (operation->lengths_set && (operation->ad_remaining != 0 || |
| 4844 | operation->body_remaining != 0)) { |
| 4845 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4846 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4848 | return PSA_SUCCESS; |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4849 | } |
| 4850 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4851 | /* Finish encrypting a message in a multipart AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4852 | psa_status_t psa_aead_finish(psa_aead_operation_t *operation, |
| 4853 | uint8_t *ciphertext, |
| 4854 | size_t ciphertext_size, |
| 4855 | size_t *ciphertext_length, |
| 4856 | uint8_t *tag, |
| 4857 | size_t tag_size, |
| 4858 | size_t *tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4859 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4860 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4861 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4862 | *ciphertext_length = 0; |
Paul Elliott | f88a565 | 2021-06-22 17:53:45 +0100 | [diff] [blame] | 4863 | *tag_length = tag_size; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4865 | status = psa_aead_final_checks(operation); |
| 4866 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4867 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4870 | if (!operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4871 | status = PSA_ERROR_BAD_STATE; |
| 4872 | goto exit; |
| 4873 | } |
| 4874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4875 | status = psa_driver_wrapper_aead_finish(operation, ciphertext, |
| 4876 | ciphertext_size, |
| 4877 | ciphertext_length, |
| 4878 | tag, tag_size, tag_length); |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 4879 | |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4880 | exit: |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4881 | /* 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] | 4882 | * the zero tag size, make sure the tag is set to something implausible. |
| 4883 | * Even if the operation succeeds, make sure we clear the rest of the |
| 4884 | * buffer to prevent potential leakage of anything previously placed in |
| 4885 | * the same buffer.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4886 | if (tag != NULL) { |
| 4887 | if (status != PSA_SUCCESS) { |
| 4888 | memset(tag, '!', tag_size); |
| 4889 | } else if (*tag_length < tag_size) { |
| 4890 | memset(tag + *tag_length, '!', (tag_size - *tag_length)); |
| 4891 | } |
Paul Elliott | ec95cc9 | 2021-09-19 22:33:09 +0100 | [diff] [blame] | 4892 | } |
Paul Elliott | f47b095 | 2021-05-21 18:02:33 +0100 | [diff] [blame] | 4893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4894 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4896 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 4900 | operation.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4901 | psa_status_t psa_aead_verify(psa_aead_operation_t *operation, |
| 4902 | uint8_t *plaintext, |
| 4903 | size_t plaintext_size, |
| 4904 | size_t *plaintext_length, |
| 4905 | const uint8_t *tag, |
| 4906 | size_t tag_length) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4907 | { |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4908 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4909 | |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4910 | *plaintext_length = 0; |
| 4911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4912 | status = psa_aead_final_checks(operation); |
| 4913 | if (status != PSA_SUCCESS) { |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4914 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4915 | } |
Paul Elliott | ad53dcc | 2021-06-23 08:50:14 +0100 | [diff] [blame] | 4916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4917 | if (operation->is_encrypt) { |
Paul Elliott | cee785c | 2021-05-20 14:29:20 +0100 | [diff] [blame] | 4918 | status = PSA_ERROR_BAD_STATE; |
| 4919 | goto exit; |
| 4920 | } |
| 4921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4922 | status = psa_driver_wrapper_aead_verify(operation, plaintext, |
| 4923 | plaintext_size, |
| 4924 | plaintext_length, |
| 4925 | tag, tag_length); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4926 | |
| 4927 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4928 | psa_aead_abort(operation); |
Paul Elliott | 39dc6b8 | 2021-05-11 19:16:09 +0100 | [diff] [blame] | 4929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4930 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | /* Abort an AEAD operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4934 | psa_status_t psa_aead_abort(psa_aead_operation_t *operation) |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4935 | { |
| 4936 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4938 | if (operation->id == 0) { |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4939 | /* The object has (apparently) been initialized but it is not (yet) |
| 4940 | * in use. It's ok to call abort on such an object, and there's |
| 4941 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4942 | return PSA_SUCCESS; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4943 | } |
| 4944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4945 | status = psa_driver_wrapper_aead_abort(operation); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4947 | memset(operation, 0, sizeof(*operation)); |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4949 | return status; |
Paul Elliott | 302ff6b | 2021-04-20 18:10:30 +0100 | [diff] [blame] | 4950 | } |
| 4951 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4952 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4953 | /* Generators */ |
| 4954 | /****************************************************************/ |
| 4955 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4956 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4957 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 4958 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \ |
| 4959 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4960 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4961 | #endif /* At least one builtin KDF */ |
| 4962 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 4963 | #if defined(BUILTIN_ALG_ANY_HKDF) || \ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4964 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4965 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4966 | static psa_status_t psa_key_derivation_start_hmac( |
| 4967 | psa_mac_operation_t *operation, |
| 4968 | psa_algorithm_t hash_alg, |
| 4969 | const uint8_t *hmac_key, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4970 | size_t hmac_key_length) |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4971 | { |
| 4972 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4973 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4974 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 4975 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 4976 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4977 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4978 | operation->is_sign = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4979 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 4980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4981 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 4982 | &attributes, |
| 4983 | hmac_key, hmac_key_length, |
| 4984 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4986 | psa_reset_key_attributes(&attributes); |
| 4987 | return status; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 4988 | } |
| 4989 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4990 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4991 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 4992 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 4993 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 4994 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 4995 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4996 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4997 | const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4998 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4999 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 5000 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
| 5001 | } else { |
| 5002 | return operation->alg; |
| 5003 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5004 | } |
| 5005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5006 | 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] | 5007 | { |
| 5008 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5009 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 5010 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5011 | /* The object has (apparently) been initialized but it is not |
| 5012 | * in use. It's ok to call abort on such an object, and there's |
| 5013 | * nothing to do. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5014 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5015 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5016 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5017 | mbedtls_free(operation->ctx.hkdf.info); |
| 5018 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 5019 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5020 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5021 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5022 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5023 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5024 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 5025 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5026 | if (operation->ctx.tls12_prf.secret != NULL) { |
| 5027 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret, |
| 5028 | operation->ctx.tls12_prf.secret_length); |
| 5029 | mbedtls_free(operation->ctx.tls12_prf.secret); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5030 | } |
| 5031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5032 | if (operation->ctx.tls12_prf.seed != NULL) { |
| 5033 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed, |
| 5034 | operation->ctx.tls12_prf.seed_length); |
| 5035 | mbedtls_free(operation->ctx.tls12_prf.seed); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5036 | } |
| 5037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5038 | if (operation->ctx.tls12_prf.label != NULL) { |
| 5039 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.label, |
| 5040 | operation->ctx.tls12_prf.label_length); |
| 5041 | mbedtls_free(operation->ctx.tls12_prf.label); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5042 | } |
| 5043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5044 | if (operation->ctx.tls12_prf.other_secret != NULL) { |
| 5045 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret, |
| 5046 | operation->ctx.tls12_prf.other_secret_length); |
| 5047 | mbedtls_free(operation->ctx.tls12_prf.other_secret); |
Przemek Stekiel | e3ee221 | 2022-04-07 14:29:56 +0200 | [diff] [blame] | 5048 | } |
| 5049 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5050 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 5051 | |
| 5052 | /* We leave the fields Ai and output_block to be erased safely by the |
| 5053 | * mbedtls_platform_zeroize() in the end of this function. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5054 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5055 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 5056 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5057 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5058 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5059 | mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data, |
| 5060 | sizeof(operation->ctx.tls12_ecjpake_to_pms.data)); |
| 5061 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5062 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5063 | { |
| 5064 | status = PSA_ERROR_BAD_STATE; |
| 5065 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 5067 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5068 | } |
| 5069 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5070 | 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] | 5071 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5072 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5073 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5074 | /* This is a blank key derivation operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5075 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5078 | *capacity = operation->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5079 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5080 | } |
| 5081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5082 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 5083 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5084 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5085 | if (operation->alg == 0) { |
| 5086 | return PSA_ERROR_BAD_STATE; |
| 5087 | } |
| 5088 | if (capacity > operation->capacity) { |
| 5089 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5090 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5091 | operation->capacity = capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5092 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5093 | } |
| 5094 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5095 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5096 | /* Read some bytes from an HKDF-based operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5097 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 5098 | psa_algorithm_t kdf_alg, |
| 5099 | uint8_t *output, |
| 5100 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5101 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5102 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 5103 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5104 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5105 | psa_status_t status; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5106 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5107 | 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] | 5108 | #else |
| 5109 | const uint8_t last_block = 0xff; |
| 5110 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5112 | if (hkdf->state < HKDF_STATE_KEYED || |
| 5113 | (!hkdf->info_set |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5114 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5115 | && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5116 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5117 | )) { |
| 5118 | return PSA_ERROR_BAD_STATE; |
| 5119 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5120 | hkdf->state = HKDF_STATE_OUTPUT; |
| 5121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5122 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5123 | /* Copy what remains of the current block */ |
| 5124 | uint8_t n = hash_length - hkdf->offset_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5125 | if (n > output_length) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5126 | n = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5127 | } |
| 5128 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5129 | output += n; |
| 5130 | output_length -= n; |
| 5131 | hkdf->offset_in_block += n; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5132 | if (output_length == 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5133 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5134 | } |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 5135 | /* We can't be wanting more output after the last block, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5136 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5137 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5138 | * object was corrupted or if this function is called directly |
| 5139 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5140 | if (hkdf->block_number == last_block) { |
| 5141 | return PSA_ERROR_BAD_STATE; |
| 5142 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5143 | |
| 5144 | /* We need a new block */ |
| 5145 | ++hkdf->block_number; |
| 5146 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 5147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5148 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 5149 | hash_alg, |
| 5150 | hkdf->prk, |
| 5151 | hash_length); |
| 5152 | if (status != PSA_SUCCESS) { |
| 5153 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5154 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5155 | |
| 5156 | if (hkdf->block_number != 1) { |
| 5157 | status = psa_mac_update(&hkdf->hmac, |
| 5158 | hkdf->output_block, |
| 5159 | hash_length); |
| 5160 | if (status != PSA_SUCCESS) { |
| 5161 | return status; |
| 5162 | } |
| 5163 | } |
| 5164 | status = psa_mac_update(&hkdf->hmac, |
| 5165 | hkdf->info, |
| 5166 | hkdf->info_length); |
| 5167 | if (status != PSA_SUCCESS) { |
| 5168 | return status; |
| 5169 | } |
| 5170 | status = psa_mac_update(&hkdf->hmac, |
| 5171 | &hkdf->block_number, 1); |
| 5172 | if (status != PSA_SUCCESS) { |
| 5173 | return status; |
| 5174 | } |
| 5175 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 5176 | hkdf->output_block, |
| 5177 | sizeof(hkdf->output_block), |
| 5178 | &hmac_output_length); |
| 5179 | if (status != PSA_SUCCESS) { |
| 5180 | return status; |
| 5181 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5182 | } |
| 5183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5184 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5185 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5186 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5187 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5188 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5189 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5190 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 5191 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5192 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5193 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5194 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 5195 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5196 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 5197 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5198 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5199 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5200 | /* We can't be wanting more output after block 0xff, otherwise |
| 5201 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 5202 | * prevented this call. It could happen only if the operation |
| 5203 | * object was corrupted or if this function is called directly |
| 5204 | * inside the library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | if (tls12_prf->block_number == 0xff) { |
| 5206 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 5207 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5208 | |
| 5209 | /* We need a new block */ |
| 5210 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5211 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5212 | |
| 5213 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 5214 | * |
| 5215 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 5216 | * |
| 5217 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 5218 | * HMAC_hash(secret, A(2) + seed) + |
| 5219 | * HMAC_hash(secret, A(3) + seed) + ... |
| 5220 | * |
| 5221 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5222 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5223 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5224 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5225 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 5226 | * is currently extracted as `output_block` and where i is |
| 5227 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5228 | */ |
| 5229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5230 | status = psa_key_derivation_start_hmac(&hmac, |
| 5231 | hash_alg, |
| 5232 | tls12_prf->secret, |
| 5233 | tls12_prf->secret_length); |
| 5234 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5235 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5236 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5237 | |
| 5238 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5239 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5240 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 5241 | * the variable seed and in this instance means it in the context of the |
| 5242 | * P_hash function, where seed = label + seed.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5243 | status = psa_mac_update(&hmac, |
| 5244 | tls12_prf->label, |
| 5245 | tls12_prf->label_length); |
| 5246 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5247 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5248 | } |
| 5249 | status = psa_mac_update(&hmac, |
| 5250 | tls12_prf->seed, |
| 5251 | tls12_prf->seed_length); |
| 5252 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5253 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5254 | } |
| 5255 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5256 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5257 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 5258 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5259 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5260 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5261 | } |
| 5262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5263 | status = psa_mac_sign_finish(&hmac, |
| 5264 | tls12_prf->Ai, hash_length, |
| 5265 | &hmac_output_length); |
| 5266 | if (hmac_output_length != hash_length) { |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 5267 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5268 | } |
| 5269 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5270 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5272 | |
| 5273 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5274 | status = psa_key_derivation_start_hmac(&hmac, |
| 5275 | hash_alg, |
| 5276 | tls12_prf->secret, |
| 5277 | tls12_prf->secret_length); |
| 5278 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5279 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5280 | } |
| 5281 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_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 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 5286 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5287 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5288 | } |
| 5289 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 5290 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5291 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5292 | } |
| 5293 | status = psa_mac_sign_finish(&hmac, |
| 5294 | tls12_prf->output_block, hash_length, |
| 5295 | &hmac_output_length); |
| 5296 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5297 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5298 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5299 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5300 | |
| 5301 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5302 | cleanup_status = psa_mac_abort(&hmac); |
| 5303 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5304 | status = cleanup_status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5305 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 5306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5307 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 5308 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 5309 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5310 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 5311 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 5312 | psa_algorithm_t alg, |
| 5313 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5314 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5315 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5316 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 5317 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5318 | psa_status_t status; |
| 5319 | uint8_t offset, length; |
| 5320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5321 | switch (tls12_prf->state) { |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5322 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 5323 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 5324 | break; |
| 5325 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 5326 | break; |
| 5327 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5328 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 5329 | } |
| 5330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5331 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5332 | /* Check if we have fully processed the current block. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5333 | if (tls12_prf->left_in_block == 0) { |
| 5334 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 5335 | alg); |
| 5336 | if (status != PSA_SUCCESS) { |
| 5337 | return status; |
| 5338 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5339 | |
| 5340 | continue; |
| 5341 | } |
| 5342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | if (tls12_prf->left_in_block > output_length) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5344 | length = (uint8_t) output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5345 | } else { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5346 | length = tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5347 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5348 | |
| 5349 | offset = hash_length - tls12_prf->left_in_block; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5350 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5351 | output += length; |
| 5352 | output_length -= length; |
| 5353 | tls12_prf->left_in_block -= length; |
| 5354 | } |
| 5355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 5357 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5358 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5359 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 5360 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5361 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5362 | static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read( |
| 5363 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
| 5364 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5365 | size_t output_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5366 | { |
| 5367 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 5368 | size_t output_size = 0; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5370 | if (output_length != 32) { |
| 5371 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5372 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data, |
| 5375 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length, |
| 5376 | &output_size); |
| 5377 | if (status != PSA_SUCCESS) { |
| 5378 | return status; |
| 5379 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5381 | if (output_size != output_length) { |
| 5382 | return PSA_ERROR_GENERIC_ERROR; |
| 5383 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5385 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5386 | } |
| 5387 | #endif |
| 5388 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 5389 | psa_status_t psa_key_derivation_output_bytes( |
| 5390 | psa_key_derivation_operation_t *operation, |
| 5391 | uint8_t *output, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5393 | { |
| 5394 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5395 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5397 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5398 | /* This is a blank operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5399 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5402 | if (output_length > operation->capacity) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5403 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5404 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5405 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5406 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5407 | goto exit; |
| 5408 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5409 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5410 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5411 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5412 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 5413 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5414 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5415 | * output_length > 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5416 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5417 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5418 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5419 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5420 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5421 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 5422 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg, |
| 5423 | output, output_length); |
| 5424 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5425 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5426 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 5427 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5428 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5429 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5430 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, |
| 5431 | kdf_alg, output, |
| 5432 | output_length); |
| 5433 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5434 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 5435 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5436 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5438 | status = psa_key_derivation_tls12_ecjpake_to_pms_read( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5439 | &operation->ctx.tls12_ecjpake_to_pms, output, output_length); |
| 5440 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5441 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
| 5442 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5443 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 5444 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5445 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5449 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5450 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5451 | * This allows us to differentiate between exhausted operations and |
| 5452 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 5453 | * operations. */ |
| 5454 | psa_algorithm_t alg = operation->alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5455 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5456 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5457 | memset(output, '!', output_length); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5458 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5459 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5460 | } |
| 5461 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5462 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5463 | 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] | 5464 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5465 | if (data_size >= 8) { |
| 5466 | mbedtls_des_key_set_parity(data); |
| 5467 | } |
| 5468 | if (data_size >= 16) { |
| 5469 | mbedtls_des_key_set_parity(data + 8); |
| 5470 | } |
| 5471 | if (data_size >= 24) { |
| 5472 | mbedtls_des_key_set_parity(data + 16); |
| 5473 | } |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5474 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 5475 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 5476 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5477 | /* |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5478 | * ECC keys on a Weierstrass elliptic curve require the generation |
| 5479 | * of a private key which is an integer |
| 5480 | * in the range [1, N - 1], where N is the boundary of the private key domain: |
| 5481 | * N is the prime p for Diffie-Hellman, or the order of the |
| 5482 | * curve’s base point for ECC. |
| 5483 | * |
| 5484 | * Let m be the bit size of N, such that 2^m > N >= 2^(m-1). |
| 5485 | * This function generates the private key using the following process: |
| 5486 | * |
| 5487 | * 1. Draw a byte string of length ceiling(m/8) bytes. |
| 5488 | * 2. If m is not a multiple of 8, set the most significant |
| 5489 | * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero. |
| 5490 | * 3. Convert the string to integer k by decoding it as a big-endian byte string. |
| 5491 | * 4. If k > N - 2, discard the result and return to step 1. |
| 5492 | * 5. Output k + 1 as the private key. |
| 5493 | * |
| 5494 | * This method allows compliance to NIST standards, specifically the methods titled |
| 5495 | * Key-Pair Generation by Testing Candidates in the following publications: |
| 5496 | * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment |
| 5497 | * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for |
| 5498 | * Diffie-Hellman keys. |
| 5499 | * |
| 5500 | * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature |
| 5501 | * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys. |
| 5502 | * |
| 5503 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 5504 | * always NULL. |
| 5505 | */ |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5506 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 5507 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 5508 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 5509 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 5510 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5511 | static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper( |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5512 | psa_key_slot_t *slot, |
| 5513 | size_t bits, |
| 5514 | psa_key_derivation_operation_t *operation, |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5515 | uint8_t **data |
| 5516 | ) |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5517 | { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5518 | unsigned key_out_of_range = 1; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5519 | mbedtls_mpi k; |
| 5520 | mbedtls_mpi diff_N_2; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5521 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 5522 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5524 | mbedtls_mpi_init(&k); |
| 5525 | mbedtls_mpi_init(&diff_N_2); |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 5526 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5527 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5528 | slot->attr.type); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5529 | mbedtls_ecp_group_id grp_id = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5530 | mbedtls_ecc_group_of_psa(curve, bits, 0); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5532 | if (grp_id == MBEDTLS_ECP_DP_NONE) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5533 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
Przemyslaw Stekiel | 871a336 | 2021-12-02 08:46:39 +0100 | [diff] [blame] | 5534 | goto cleanup; |
| 5535 | } |
| 5536 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5537 | mbedtls_ecp_group ecp_group; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5538 | mbedtls_ecp_group_init(&ecp_group); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5540 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5541 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5542 | /* N is the boundary of the private key domain (ecp_group.N). */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5543 | /* Let m be the bit size of N. */ |
| 5544 | size_t m = ecp_group.nbits; |
| 5545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5546 | size_t m_bytes = PSA_BITS_TO_BYTES(m); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5547 | |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5548 | /* Calculate N - 2 - it will be needed later. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5549 | 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] | 5550 | |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5551 | /* Note: This function is always called with *data == NULL and it |
| 5552 | * allocates memory for the data buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5553 | *data = mbedtls_calloc(1, m_bytes); |
| 5554 | if (*data == NULL) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5555 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5556 | goto cleanup; |
| 5557 | } |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | while (key_out_of_range) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5560 | /* 1. Draw a byte string of length ceiling(m/8) bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5561 | if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5562 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | } |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5564 | |
| 5565 | /* 2. If m is not a multiple of 8 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5566 | if (m % 8 != 0) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5567 | /* Set the most significant |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5568 | * (8 * ceiling(m/8) - m) bits of the first byte in |
| 5569 | * the string to zero. |
| 5570 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | uint8_t clear_bit_mask = (1 << (m % 8)) - 1; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5572 | (*data)[0] &= clear_bit_mask; |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5573 | } |
| 5574 | |
| 5575 | /* 3. Convert the string to integer k by decoding it as a |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | * big-endian byte string. |
| 5577 | */ |
| 5578 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5579 | |
| 5580 | /* 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] | 5581 | * Result of comparison is returned. When it indicates error |
| 5582 | * then this function is called again. |
| 5583 | */ |
| 5584 | 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] | 5585 | } |
| 5586 | |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5587 | /* 5. Output k + 1 as the private key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5588 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1)); |
| 5589 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes)); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5590 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | if (ret != 0) { |
| 5592 | status = mbedtls_to_psa_error(ret); |
| 5593 | } |
| 5594 | if (status != PSA_SUCCESS) { |
| 5595 | mbedtls_free(*data); |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5596 | *data = NULL; |
| 5597 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | mbedtls_mpi_free(&k); |
| 5599 | mbedtls_mpi_free(&diff_N_2); |
| 5600 | return status; |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5601 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5602 | |
| 5603 | /* ECC keys on a Montgomery elliptic curve draws a byte string whose length |
| 5604 | * is determined by the curve, and sets the mandatory bits accordingly. That is: |
| 5605 | * |
| 5606 | * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits): |
| 5607 | * draw a 32-byte string and process it as specified in |
| 5608 | * Elliptic Curves for Security [RFC7748] §5. |
| 5609 | * |
| 5610 | * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits): |
| 5611 | * draw a 56-byte string and process it as specified in [RFC7748] §5. |
| 5612 | * |
| 5613 | * Note: Function allocates memory for *data buffer, so given *data should be |
| 5614 | * always NULL. |
| 5615 | */ |
| 5616 | |
| 5617 | static psa_status_t psa_generate_derived_ecc_key_montgomery_helper( |
| 5618 | size_t bits, |
| 5619 | psa_key_derivation_operation_t *operation, |
| 5620 | uint8_t **data |
| 5621 | ) |
| 5622 | { |
| 5623 | size_t output_length; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 5624 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5626 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5627 | case 255: |
| 5628 | output_length = 32; |
| 5629 | break; |
| 5630 | case 448: |
| 5631 | output_length = 56; |
| 5632 | break; |
| 5633 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | return PSA_ERROR_INVALID_ARGUMENT; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5635 | break; |
| 5636 | } |
| 5637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5638 | *data = mbedtls_calloc(1, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5640 | if (*data == NULL) { |
| 5641 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5642 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5644 | status = psa_key_derivation_output_bytes(operation, *data, output_length); |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5646 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5647 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5648 | } |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5650 | switch (bits) { |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5651 | case 255: |
| 5652 | (*data)[0] &= 248; |
| 5653 | (*data)[31] &= 127; |
| 5654 | (*data)[31] |= 64; |
| 5655 | break; |
| 5656 | case 448: |
| 5657 | (*data)[0] &= 252; |
| 5658 | (*data)[55] |= 128; |
| 5659 | break; |
| 5660 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5661 | return PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | aeaa4f0 | 2022-02-21 08:17:43 +0100 | [diff] [blame] | 5662 | break; |
| 5663 | } |
| 5664 | |
| 5665 | return status; |
| 5666 | } |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5667 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 5668 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 5669 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 5670 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 5671 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Przemyslaw Stekiel | d8cdcba | 2021-11-15 12:38:53 +0100 | [diff] [blame] | 5672 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 5673 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5674 | psa_key_slot_t *slot, |
| 5675 | size_t bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5677 | { |
| 5678 | uint8_t *data = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5679 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5680 | size_t storage_size = bytes; |
Przemek Stekiel | a81aed2 | 2022-03-01 15:13:30 +0100 | [diff] [blame] | 5681 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5683 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
| 5684 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5685 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5686 | |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5687 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 5688 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 5689 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 5690 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 5691 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) { |
| 5693 | psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type); |
| 5694 | if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5695 | /* Weierstrass elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5696 | status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data); |
| 5697 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 6d3d18b | 2022-01-20 22:41:17 +0100 | [diff] [blame] | 5698 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | } |
| 5700 | } else { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5701 | /* Montgomery elliptic curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5702 | status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data); |
| 5703 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5704 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5705 | } |
Przemyslaw Stekiel | 58ce8d8 | 2021-11-25 13:53:28 +0100 | [diff] [blame] | 5706 | } |
Przemyslaw Stekiel | 1dfd122 | 2021-11-18 13:35:00 +0100 | [diff] [blame] | 5707 | } else |
Przemek Stekiel | 7fc0751 | 2022-03-04 14:41:11 +0100 | [diff] [blame] | 5708 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 5709 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 5710 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 5711 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 5712 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | if (key_type_is_raw_bytes(slot->attr.type)) { |
| 5714 | if (bits % 8 != 0) { |
| 5715 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5716 | } |
| 5717 | data = mbedtls_calloc(1, bytes); |
| 5718 | if (data == NULL) { |
| 5719 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 5720 | } |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5722 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 5723 | if (status != PSA_SUCCESS) { |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5724 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5725 | } |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5726 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5727 | if (slot->attr.type == PSA_KEY_TYPE_DES) { |
| 5728 | psa_des_set_key_parity(data, bytes); |
| 5729 | } |
Przemek Stekiel | f110dc0 | 2022-03-01 14:48:05 +0100 | [diff] [blame] | 5730 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5731 | } else { |
| 5732 | return PSA_ERROR_NOT_SUPPORTED; |
Przemyslaw Stekiel | 1608e33 | 2021-11-09 10:46:40 +0100 | [diff] [blame] | 5733 | } |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 5734 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5735 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5736 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5737 | .core = slot->attr |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 5738 | }; |
| 5739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5740 | if (psa_key_lifetime_is_external(attributes.core.lifetime)) { |
| 5741 | status = psa_driver_wrapper_get_key_buffer_size(&attributes, |
| 5742 | &storage_size); |
| 5743 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5744 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5745 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5746 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5747 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 5748 | if (status != PSA_SUCCESS) { |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5749 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5750 | } |
Archana | d8a83dc | 2021-06-14 10:04:16 +0530 | [diff] [blame] | 5751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5752 | status = psa_driver_wrapper_import_key(&attributes, |
| 5753 | data, bytes, |
| 5754 | slot->key.data, |
| 5755 | slot->key.bytes, |
| 5756 | &slot->key.bytes, &bits); |
| 5757 | if (bits != slot->attr.bits) { |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 5758 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5759 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5760 | |
| 5761 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5762 | mbedtls_free(data); |
| 5763 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5764 | } |
| 5765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5766 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 5767 | psa_key_derivation_operation_t *operation, |
| 5768 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5769 | { |
| 5770 | psa_status_t status; |
| 5771 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5772 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5773 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5774 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5775 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5776 | /* Reject any attempt to create a zero-length key so that we don't |
| 5777 | * 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] | 5778 | if (psa_get_key_bits(attributes) == 0) { |
| 5779 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5780 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5782 | if (operation->alg == PSA_ALG_NONE) { |
| 5783 | return PSA_ERROR_BAD_STATE; |
| 5784 | } |
Dave Rodgman | d69da6c | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 5785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5786 | if (!operation->can_output_key) { |
| 5787 | return PSA_ERROR_NOT_PERMITTED; |
| 5788 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 5789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5790 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, |
| 5791 | &slot, &driver); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5792 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5793 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 5794 | /* Deriving a key in a secure element is not implemented yet. */ |
| 5795 | status = PSA_ERROR_NOT_SUPPORTED; |
| 5796 | } |
| 5797 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5798 | if (status == PSA_SUCCESS) { |
| 5799 | status = psa_generate_derived_key_internal(slot, |
| 5800 | attributes->core.bits, |
| 5801 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5802 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5803 | if (status == PSA_SUCCESS) { |
| 5804 | status = psa_finish_key_creation(slot, driver, key); |
| 5805 | } |
| 5806 | if (status != PSA_SUCCESS) { |
| 5807 | psa_fail_key_creation(slot, driver); |
| 5808 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5810 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5811 | } |
| 5812 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 5813 | |
| 5814 | |
| 5815 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5816 | /* Key derivation */ |
| 5817 | /****************************************************************/ |
| 5818 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 5819 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5820 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5821 | { |
| 5822 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5823 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 5824 | return 1; |
| 5825 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5826 | #endif |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5827 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5828 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 5829 | return 1; |
| 5830 | } |
Przemek Stekiel | b57a44b | 2022-06-06 08:33:45 +0200 | [diff] [blame] | 5831 | #endif |
| 5832 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5833 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 5834 | return 1; |
| 5835 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5836 | #endif |
| 5837 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5838 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 5839 | return 1; |
| 5840 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5841 | #endif |
| 5842 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5843 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 5844 | return 1; |
| 5845 | } |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5846 | #endif |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5847 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5849 | return 1; |
| 5850 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5851 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5852 | return 0; |
Gilles Peskine | 9efde4f | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 5853 | } |
| 5854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5855 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5856 | { |
| 5857 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | psa_status_t status = psa_hash_setup(&operation, alg); |
| 5859 | psa_hash_abort(&operation); |
| 5860 | return status; |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5861 | } |
| 5862 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5863 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5864 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5865 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5866 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5867 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 5868 | * initialisers for this union leave some bytes unspecified.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5869 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 5870 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5871 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5872 | if (!is_kdf_alg_supported(kdf_alg)) { |
| 5873 | return PSA_ERROR_NOT_SUPPORTED; |
| 5874 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5875 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5876 | /* All currently supported key derivation algorithms (apart from |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 5877 | * ecjpake to pms) are based on a hash algorithm. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5878 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 5879 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 5880 | if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5881 | if (hash_size == 0) { |
| 5882 | return PSA_ERROR_NOT_SUPPORTED; |
| 5883 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5884 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5885 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 5886 | * we might fail later, which is somewhat unfriendly and potentially |
| 5887 | * risk-prone. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | psa_status_t status = psa_hash_try_support(hash_alg); |
| 5889 | if (status != PSA_SUCCESS) { |
| 5890 | return status; |
| 5891 | } |
| 5892 | } else { |
| 5893 | hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 5894 | } |
Gilles Peskine | 0cc417d | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 5895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5896 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 5897 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && |
| 5898 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 5899 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5900 | } |
Andrzej Kurek | 7763829 | 2022-09-16 12:24:52 -0400 | [diff] [blame] | 5901 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5902 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5903 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) || |
| 5904 | (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) { |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 5905 | operation->capacity = hash_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5906 | } else |
Andrzej Kurek | b510cd2 | 2022-09-26 10:50:22 -0400 | [diff] [blame] | 5907 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT || |
| 5908 | MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5909 | operation->capacity = 255 * hash_size; |
| 5910 | return PSA_SUCCESS; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5911 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5913 | 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] | 5914 | { |
| 5915 | #if defined(PSA_WANT_ALG_ECDH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | if (alg == PSA_ALG_ECDH) { |
| 5917 | return PSA_SUCCESS; |
| 5918 | } |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5919 | #endif |
| 5920 | (void) alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5921 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0c3a071 | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 5922 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 5923 | |
| 5924 | static int psa_key_derivation_allows_free_form_secret_input( |
| 5925 | psa_algorithm_t kdf_alg) |
| 5926 | { |
| 5927 | #if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS) |
| 5928 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
| 5929 | return 0; |
| 5930 | } |
| 5931 | #endif |
| 5932 | (void) kdf_alg; |
| 5933 | return 1; |
| 5934 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 5935 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5937 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 5938 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5939 | { |
| 5940 | psa_status_t status; |
| 5941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5942 | if (operation->alg != 0) { |
| 5943 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5944 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 5945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5946 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 5947 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5948 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 5949 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 5950 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 5951 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); |
| 5952 | status = psa_key_agreement_try_support(ka_alg); |
| 5953 | if (status != PSA_SUCCESS) { |
| 5954 | return status; |
| 5955 | } |
Gilles Peskine | bb3814c | 2022-12-16 01:12:12 +0100 | [diff] [blame] | 5956 | if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) { |
| 5957 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5958 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5959 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 5960 | #else |
| 5961 | return PSA_ERROR_NOT_SUPPORTED; |
| 5962 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 5963 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 5964 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 5965 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 5966 | #else |
| 5967 | return PSA_ERROR_NOT_SUPPORTED; |
| 5968 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 5969 | } else { |
| 5970 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5971 | } |
| 5972 | |
| 5973 | if (status == PSA_SUCCESS) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5974 | operation->alg = alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5975 | } |
| 5976 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5977 | } |
| 5978 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 5979 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5980 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 5981 | psa_algorithm_t kdf_alg, |
| 5982 | psa_key_derivation_step_t step, |
| 5983 | const uint8_t *data, |
| 5984 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5985 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5986 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5987 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5988 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5989 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5990 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5991 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
| 5992 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5993 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 5994 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | if (hkdf->state != HKDF_STATE_INIT) { |
| 5996 | return PSA_ERROR_BAD_STATE; |
| 5997 | } else { |
| 5998 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 5999 | hash_alg, |
| 6000 | data, data_length); |
| 6001 | if (status != PSA_SUCCESS) { |
| 6002 | return status; |
| 6003 | } |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6004 | hkdf->state = HKDF_STATE_STARTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6005 | return PSA_SUCCESS; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 6006 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6007 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6008 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6009 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) { |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6010 | /* We shouldn't be in different state as HKDF_EXPAND only allows |
| 6011 | * two inputs: SECRET (this case) and INFO which does not modify |
| 6012 | * the state. It could happen only if the hkdf |
| 6013 | * object was corrupted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6014 | if (hkdf->state != HKDF_STATE_INIT) { |
| 6015 | return PSA_ERROR_BAD_STATE; |
| 6016 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6017 | |
Przemek Stekiel | 2fb0dcd | 2022-05-19 10:34:37 +0200 | [diff] [blame] | 6018 | /* Allow only input that fits expected prk size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6019 | if (data_length != PSA_HASH_LENGTH(hash_alg)) { |
| 6020 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6021 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6023 | memcpy(hkdf->prk, data, data_length); |
| 6024 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6025 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */ |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6026 | { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6027 | /* HKDF: If no salt was provided, use an empty salt. |
| 6028 | * HKDF-EXTRACT: salt is mandatory. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6029 | if (hkdf->state == HKDF_STATE_INIT) { |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6030 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6031 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6032 | return PSA_ERROR_BAD_STATE; |
| 6033 | } |
Przemek Stekiel | 0586f4c | 2022-06-03 16:00:25 +0200 | [diff] [blame] | 6034 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 6036 | hash_alg, |
| 6037 | NULL, 0); |
| 6038 | if (status != PSA_SUCCESS) { |
| 6039 | return status; |
| 6040 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6041 | hkdf->state = HKDF_STATE_STARTED; |
| 6042 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6043 | if (hkdf->state != HKDF_STATE_STARTED) { |
| 6044 | return PSA_ERROR_BAD_STATE; |
| 6045 | } |
| 6046 | status = psa_mac_update(&hkdf->hmac, |
| 6047 | data, data_length); |
| 6048 | if (status != PSA_SUCCESS) { |
| 6049 | return status; |
| 6050 | } |
| 6051 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 6052 | hkdf->prk, |
| 6053 | sizeof(hkdf->prk), |
| 6054 | &data_length); |
| 6055 | if (status != PSA_SUCCESS) { |
| 6056 | return status; |
| 6057 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6058 | } |
Przemek Stekiel | 17520fe | 2022-05-10 13:53:33 +0200 | [diff] [blame] | 6059 | |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 6060 | hkdf->state = HKDF_STATE_KEYED; |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6061 | hkdf->block_number = 0; |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6062 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6063 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6064 | /* The only block of output is the PRK. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6065 | memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg)); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6066 | hkdf->offset_in_block = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6067 | } else |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6068 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6069 | { |
| 6070 | /* Block 0 is empty, and the next block will be |
| 6071 | * generated by psa_key_derivation_hkdf_read(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6072 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Przemek Stekiel | 03d948c | 2022-05-19 11:45:20 +0200 | [diff] [blame] | 6073 | } |
| 6074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6075 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6076 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6077 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) { |
| 6079 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6080 | } |
Przemek Stekiel | 3e8249c | 2022-06-03 14:05:07 +0200 | [diff] [blame] | 6081 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6082 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) && |
| 6084 | hkdf->state == HKDF_STATE_INIT) { |
| 6085 | return PSA_ERROR_BAD_STATE; |
| 6086 | } |
Przemek Stekiel | cde3f78 | 2022-06-03 16:12:27 +0200 | [diff] [blame] | 6087 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6088 | if (hkdf->state == HKDF_STATE_OUTPUT) { |
| 6089 | return PSA_ERROR_BAD_STATE; |
| 6090 | } |
| 6091 | if (hkdf->info_set) { |
| 6092 | return PSA_ERROR_BAD_STATE; |
| 6093 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6094 | hkdf->info_length = data_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6095 | if (data_length != 0) { |
| 6096 | hkdf->info = mbedtls_calloc(1, data_length); |
| 6097 | if (hkdf->info == NULL) { |
| 6098 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6099 | } |
| 6100 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6101 | } |
| 6102 | hkdf->info_set = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6104 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6106 | } |
| 6107 | } |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6108 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6109 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6110 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 6111 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 6113 | const uint8_t *data, |
| 6114 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6115 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { |
| 6117 | return PSA_ERROR_BAD_STATE; |
| 6118 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6120 | if (data_length != 0) { |
| 6121 | prf->seed = mbedtls_calloc(1, data_length); |
| 6122 | if (prf->seed == NULL) { |
| 6123 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6124 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6127 | prf->seed_length = data_length; |
| 6128 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6129 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6130 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6132 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6133 | } |
| 6134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6135 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 6136 | const uint8_t *data, |
| 6137 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6138 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6139 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET && |
| 6140 | prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6141 | return PSA_ERROR_BAD_STATE; |
| 6142 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6144 | if (data_length != 0) { |
| 6145 | prf->secret = mbedtls_calloc(1, data_length); |
| 6146 | if (prf->secret == NULL) { |
| 6147 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6148 | } |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 6151 | prf->secret_length = data_length; |
| 6152 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6153 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6154 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6156 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6157 | } |
| 6158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 6160 | const uint8_t *data, |
| 6161 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6162 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6163 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { |
| 6164 | return PSA_ERROR_BAD_STATE; |
| 6165 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6167 | if (data_length != 0) { |
| 6168 | prf->label = mbedtls_calloc(1, data_length); |
| 6169 | if (prf->label == NULL) { |
| 6170 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6171 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6173 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 6174 | prf->label_length = data_length; |
| 6175 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6176 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 6177 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6179 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6180 | } |
| 6181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6182 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 6183 | psa_key_derivation_step_t step, |
| 6184 | const uint8_t *data, |
| 6185 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6186 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6187 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 6188 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6189 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 6190 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6191 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 6192 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6194 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6195 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 6196 | } |
| 6197 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6198 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 6199 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 6200 | |
| 6201 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 6202 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 6203 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6204 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6205 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6206 | { |
| 6207 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6208 | const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ? |
| 6209 | 4 + data_length + prf->other_secret_length : |
| 6210 | 4 + 2 * data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6212 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { |
| 6213 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6214 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6216 | uint8_t *pms = mbedtls_calloc(1, pms_len); |
| 6217 | if (pms == NULL) { |
| 6218 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6219 | } |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6220 | uint8_t *cur = pms; |
| 6221 | |
| 6222 | /* pure-PSK: |
| 6223 | * Quoting RFC 4279, Section 2: |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6224 | * |
| 6225 | * The premaster secret is formed as follows: if the PSK is N octets |
| 6226 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 6227 | * uint16 with the value N, and the PSK itself. |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6228 | * |
| 6229 | * mixed-PSK: |
| 6230 | * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as |
| 6231 | * follows: concatenate a uint16 with the length of the other secret, |
| 6232 | * the other secret itself, uint16 with the length of PSK, and the |
| 6233 | * PSK itself. |
| 6234 | * For details please check: |
| 6235 | * - RFC 4279, Section 4 for the definition of RSA-PSK, |
| 6236 | * - RFC 4279, Section 3 for the definition of DHE-PSK, |
| 6237 | * - RFC 5489 for the definition of ECDHE-PSK. |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6238 | */ |
| 6239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6240 | if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) { |
| 6241 | *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length); |
| 6242 | *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length); |
| 6243 | if (prf->other_secret_length != 0) { |
| 6244 | memcpy(cur, prf->other_secret, prf->other_secret_length); |
| 6245 | mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length); |
Przemek Stekiel | 2503f7e | 2022-04-12 12:08:01 +0200 | [diff] [blame] | 6246 | cur += prf->other_secret_length; |
| 6247 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6248 | } else { |
| 6249 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 6250 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 6251 | memset(cur, 0, data_length); |
Przemek Stekiel | c8fa5a1 | 2022-04-07 14:17:13 +0200 | [diff] [blame] | 6252 | cur += data_length; |
| 6253 | } |
| 6254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6255 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 6256 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 6257 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6258 | cur += data_length; |
| 6259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6260 | status = psa_tls12_prf_set_key(prf, pms, cur - pms); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6262 | mbedtls_platform_zeroize(pms, pms_len); |
| 6263 | mbedtls_free(pms); |
| 6264 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6265 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6266 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6267 | static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key( |
| 6268 | psa_tls12_prf_key_derivation_t *prf, |
| 6269 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | size_t data_length) |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6271 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6272 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { |
| 6273 | return PSA_ERROR_BAD_STATE; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6274 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6275 | |
| 6276 | if (data_length != 0) { |
| 6277 | prf->other_secret = mbedtls_calloc(1, data_length); |
| 6278 | if (prf->other_secret == NULL) { |
| 6279 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 6280 | } |
| 6281 | |
| 6282 | memcpy(prf->other_secret, data, data_length); |
| 6283 | prf->other_secret_length = data_length; |
| 6284 | } else { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6285 | prf->other_secret_length = 0; |
| 6286 | } |
| 6287 | |
| 6288 | prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET; |
| 6289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6290 | return PSA_SUCCESS; |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6291 | } |
| 6292 | |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6293 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 6294 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6295 | psa_key_derivation_step_t step, |
| 6296 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6297 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6298 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6299 | switch (step) { |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6300 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6301 | return psa_tls12_prf_psk_to_ms_set_key(prf, |
| 6302 | data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6303 | break; |
| 6304 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6305 | return psa_tls12_prf_psk_to_ms_set_other_key(prf, |
| 6306 | data, |
| 6307 | data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6308 | break; |
| 6309 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6310 | return psa_tls12_prf_input(prf, step, data, data_length); |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6311 | break; |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6312 | |
Przemek Stekiel | e47201b | 2022-04-19 13:53:28 +0200 | [diff] [blame] | 6313 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 6314 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6315 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6316 | |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6317 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
| 6318 | static psa_status_t psa_tls12_ecjpake_to_pms_input( |
| 6319 | psa_tls12_ecjpake_to_pms_t *ecjpake, |
Andrzej Kurek | 702776f | 2022-09-16 06:22:44 -0400 | [diff] [blame] | 6320 | psa_key_derivation_step_t step, |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6321 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6322 | size_t data_length) |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6323 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6324 | if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE || |
| 6325 | step != PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 6326 | return PSA_ERROR_INVALID_ARGUMENT; |
Andrzej Kurek | 5603efd | 2022-09-26 10:49:16 -0400 | [diff] [blame] | 6327 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6328 | |
| 6329 | /* Check if the passed point is in an uncompressed form */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | if (data[0] != 0x04) { |
| 6331 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6332 | } |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6333 | |
| 6334 | /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6337 | return PSA_SUCCESS; |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6338 | } |
| 6339 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 6340 | /** Check whether the given key type is acceptable for the given |
| 6341 | * input step of a key derivation. |
| 6342 | * |
| 6343 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 6344 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 6345 | * Both secret and non-secret inputs can alternatively have the type |
| 6346 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 6347 | * that the input was passed as a buffer rather than via a key object. |
| 6348 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6349 | static int psa_key_derivation_check_input_type( |
| 6350 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6351 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6352 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6353 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6354 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6355 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 6356 | return PSA_SUCCESS; |
| 6357 | } |
| 6358 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 6359 | return PSA_SUCCESS; |
| 6360 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6361 | break; |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 6362 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 6364 | return PSA_SUCCESS; |
| 6365 | } |
| 6366 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 6367 | return PSA_SUCCESS; |
| 6368 | } |
Przemek Stekiel | a7695a2 | 2022-04-07 15:39:01 +0200 | [diff] [blame] | 6369 | break; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6370 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 6371 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 6372 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 6373 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6374 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
| 6375 | return PSA_SUCCESS; |
| 6376 | } |
| 6377 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 6378 | return PSA_SUCCESS; |
| 6379 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6380 | break; |
| 6381 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6382 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6383 | } |
| 6384 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 6385 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6386 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6387 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6388 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6389 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6390 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6391 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6392 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6393 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 6394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6395 | status = psa_key_derivation_check_input_type(step, key_type); |
| 6396 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6397 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6398 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6399 | |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6400 | #if defined(BUILTIN_ALG_ANY_HKDF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) { |
| 6402 | status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg, |
| 6403 | step, data, data_length); |
| 6404 | } else |
Przemek Stekiel | 69c4679 | 2022-06-10 12:59:51 +0200 | [diff] [blame] | 6405 | #endif /* BUILTIN_ALG_ANY_HKDF */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6406 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6407 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 6408 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, |
| 6409 | step, data, data_length); |
| 6410 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6411 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 6412 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6413 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 6414 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, |
| 6415 | step, data, data_length); |
| 6416 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 6417 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6418 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6419 | if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) { |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6420 | status = psa_tls12_ecjpake_to_pms_input( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6421 | &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length); |
| 6422 | } else |
Andrzej Kurek | 08d34b8 | 2022-07-29 10:00:16 -0400 | [diff] [blame] | 6423 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6424 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6425 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 6426 | (void) data; |
| 6427 | (void) data_length; |
| 6428 | (void) kdf_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6429 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6430 | } |
| 6431 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6432 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6433 | if (status != PSA_SUCCESS) { |
| 6434 | psa_key_derivation_abort(operation); |
| 6435 | } |
| 6436 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6437 | } |
| 6438 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6439 | psa_status_t psa_key_derivation_input_bytes( |
| 6440 | psa_key_derivation_operation_t *operation, |
| 6441 | psa_key_derivation_step_t step, |
| 6442 | const uint8_t *data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6443 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6444 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6445 | return psa_key_derivation_input_internal(operation, step, |
| 6446 | PSA_KEY_TYPE_NONE, |
| 6447 | data, data_length); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 6448 | } |
| 6449 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 6450 | psa_status_t psa_key_derivation_input_key( |
| 6451 | psa_key_derivation_operation_t *operation, |
| 6452 | psa_key_derivation_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6453 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6454 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6455 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6456 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6457 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6458 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6459 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6460 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 6461 | if (status != PSA_SUCCESS) { |
| 6462 | psa_key_derivation_abort(operation); |
| 6463 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 6464 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6465 | |
| 6466 | /* Passing a key object as a SECRET input unlocks the permission |
| 6467 | * to output to a key object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6468 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6469 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6470 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 6471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6472 | status = psa_key_derivation_input_internal(operation, |
| 6473 | step, slot->attr.type, |
| 6474 | slot->key.data, |
| 6475 | slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6477 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6479 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6480 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6481 | |
| 6482 | |
| 6483 | |
| 6484 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6485 | /* Key agreement */ |
| 6486 | /****************************************************************/ |
| 6487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6488 | psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes, |
| 6489 | const uint8_t *key_buffer, |
| 6490 | size_t key_buffer_size, |
| 6491 | psa_algorithm_t alg, |
| 6492 | const uint8_t *peer_key, |
| 6493 | size_t peer_key_length, |
| 6494 | uint8_t *shared_secret, |
| 6495 | size_t shared_secret_size, |
| 6496 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 6497 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6498 | switch (alg) { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 6499 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6500 | case PSA_ALG_ECDH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6501 | return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer, |
| 6502 | key_buffer_size, alg, |
| 6503 | peer_key, peer_key_length, |
| 6504 | shared_secret, |
| 6505 | shared_secret_size, |
| 6506 | shared_secret_length); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6507 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6508 | default: |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6509 | (void) attributes; |
| 6510 | (void) key_buffer; |
| 6511 | (void) key_buffer_size; |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 6512 | (void) peer_key; |
| 6513 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6514 | (void) shared_secret; |
| 6515 | (void) shared_secret_size; |
| 6516 | (void) shared_secret_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6517 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6518 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6519 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6520 | |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6521 | /** Internal function for raw key agreement |
| 6522 | * Calls the driver wrapper which will hand off key agreement task |
Aditya Deshpande | 40c05cc | 2022-10-14 16:41:40 +0100 | [diff] [blame] | 6523 | * to the driver's implementation if a driver is present. |
| 6524 | * Fallback specified in the driver wrapper is built-in raw key agreement |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6525 | * (psa_key_agreement_raw_builtin). |
| 6526 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6527 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 6528 | psa_key_slot_t *private_key, |
| 6529 | const uint8_t *peer_key, |
| 6530 | size_t peer_key_length, |
| 6531 | uint8_t *shared_secret, |
| 6532 | size_t shared_secret_size, |
| 6533 | size_t *shared_secret_length) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6534 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6535 | if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 6536 | return PSA_ERROR_NOT_SUPPORTED; |
| 6537 | } |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6538 | |
| 6539 | psa_key_attributes_t attributes = { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6540 | .core = private_key->attr |
Aditya Deshpande | 17845b8 | 2022-10-13 17:21:01 +0100 | [diff] [blame] | 6541 | }; |
| 6542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6543 | return psa_driver_wrapper_key_agreement(&attributes, |
| 6544 | private_key->key.data, |
| 6545 | private_key->key.bytes, alg, |
| 6546 | peer_key, peer_key_length, |
| 6547 | shared_secret, |
| 6548 | shared_secret_size, |
| 6549 | shared_secret_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6550 | } |
| 6551 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6552 | /* 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] | 6553 | * to potentially free embedded data structures and wipe confidential data. |
| 6554 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6555 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 6556 | psa_key_derivation_step_t step, |
| 6557 | psa_key_slot_t *private_key, |
| 6558 | const uint8_t *peer_key, |
| 6559 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6560 | { |
| 6561 | psa_status_t status; |
Aditya Deshpande | 2f7fd76 | 2022-11-22 11:10:34 +0000 | [diff] [blame] | 6562 | uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE]; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6563 | size_t shared_secret_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6564 | 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] | 6565 | |
| 6566 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 6567 | * secret. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6568 | status = psa_key_agreement_raw_internal(ka_alg, |
| 6569 | private_key, |
| 6570 | peer_key, peer_key_length, |
| 6571 | shared_secret, |
| 6572 | sizeof(shared_secret), |
| 6573 | &shared_secret_length); |
| 6574 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6575 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6576 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6577 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 6578 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 6579 | * the shared secret. A shared secret is permitted wherever a key |
| 6580 | * of type DERIVE is permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6581 | status = psa_key_derivation_input_internal(operation, step, |
| 6582 | PSA_KEY_TYPE_DERIVE, |
| 6583 | shared_secret, |
| 6584 | shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6585 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6586 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 6587 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6588 | } |
| 6589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6590 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 6591 | psa_key_derivation_step_t step, |
| 6592 | mbedtls_svc_key_id_t private_key, |
| 6593 | const uint8_t *peer_key, |
| 6594 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6595 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6596 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6597 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 6598 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6600 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 6601 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6602 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6603 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6604 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 6605 | if (status != PSA_SUCCESS) { |
| 6606 | return status; |
| 6607 | } |
| 6608 | status = psa_key_agreement_internal(operation, step, |
| 6609 | slot, |
| 6610 | peer_key, peer_key_length); |
| 6611 | if (status != PSA_SUCCESS) { |
| 6612 | psa_key_derivation_abort(operation); |
| 6613 | } else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6614 | /* If a private key has been added as SECRET, we allow the derived |
| 6615 | * key material to be used as a key in PSA Crypto. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6616 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6617 | operation->can_output_key = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6618 | } |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6619 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6621 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6623 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 6624 | } |
| 6625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6626 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 6627 | mbedtls_svc_key_id_t private_key, |
| 6628 | const uint8_t *peer_key, |
| 6629 | size_t peer_key_length, |
| 6630 | uint8_t *output, |
| 6631 | size_t output_size, |
| 6632 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6633 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6634 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6635 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6636 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6638 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6639 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 6640 | goto exit; |
| 6641 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 6642 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6643 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 6644 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6645 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6646 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6647 | |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 6648 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 6649 | * for the output size. The PSA specification only guarantees that this |
| 6650 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 6651 | * but it might be nice to allow smaller buffers if the output fits. |
| 6652 | * At the time of writing this comment, with only ECDH implemented, |
| 6653 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 6654 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 6655 | * be exact for it as well. */ |
| 6656 | size_t expected_length = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6657 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); |
| 6658 | if (output_size < expected_length) { |
Gilles Peskine | 3e56130 | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 6659 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 6660 | goto exit; |
| 6661 | } |
| 6662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6663 | status = psa_key_agreement_raw_internal(alg, slot, |
| 6664 | peer_key, peer_key_length, |
| 6665 | output, output_size, |
| 6666 | output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6667 | |
| 6668 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6669 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6670 | /* If an error happens and is not handled properly, the output |
| 6671 | * may be used as a key to protect sensitive data. Arrange for such |
| 6672 | * a key to be random, which is likely to result in decryption or |
| 6673 | * verification errors. This is better than filling the buffer with |
| 6674 | * some constant data such as zeros, which would result in the data |
| 6675 | * being protected with a reproducible, easily knowable key. |
| 6676 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6677 | psa_generate_random(output, output_size); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6678 | *output_length = output_size; |
| 6679 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6681 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 6682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6683 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 6684 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6685 | |
| 6686 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6687 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 6688 | /****************************************************************/ |
| 6689 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 6690 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6691 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6692 | /** Initialize the PSA random generator. |
| 6693 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6694 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6695 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6696 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6697 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6698 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6699 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6700 | /* Set default configuration if |
| 6701 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6702 | if (rng->entropy_init == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6703 | rng->entropy_init = mbedtls_entropy_init; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6704 | } |
| 6705 | if (rng->entropy_free == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6706 | rng->entropy_free = mbedtls_entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6707 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6709 | rng->entropy_init(&rng->entropy); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6710 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 6711 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 6712 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 6713 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6714 | mbedtls_entropy_add_source(&rng->entropy, |
| 6715 | mbedtls_nv_seed_poll, NULL, |
| 6716 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 6717 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6718 | #endif |
| 6719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6720 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6721 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6722 | } |
| 6723 | |
| 6724 | /** Deinitialize the PSA random generator. |
| 6725 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6726 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6727 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6728 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6729 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6730 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6731 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 6732 | rng->entropy_free(&rng->entropy); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6733 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6734 | } |
| 6735 | |
| 6736 | /** Seed the PSA random generator. |
| 6737 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6738 | 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] | 6739 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6740 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6741 | /* Do nothing: the external RNG seeds itself. */ |
| 6742 | (void) rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6743 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6744 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6745 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6746 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, |
| 6747 | drbg_seed, sizeof(drbg_seed) - 1); |
| 6748 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6749 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 6750 | } |
| 6751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6752 | psa_status_t psa_generate_random(uint8_t *output, |
| 6753 | size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6754 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6755 | GUARD_MODULE_INITIALIZED; |
| 6756 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6757 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6758 | |
| 6759 | size_t output_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6760 | psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng, |
| 6761 | output, output_size, |
| 6762 | &output_length); |
| 6763 | if (status != PSA_SUCCESS) { |
| 6764 | return status; |
| 6765 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6766 | /* Breaking up a request into smaller chunks is currently not supported |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6767 | * for the external RNG interface. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6768 | if (output_length != output_size) { |
| 6769 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 6770 | } |
| 6771 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6772 | |
| 6773 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6775 | while (output_size > 0) { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6776 | size_t request_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6777 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 6778 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 6779 | output_size); |
| 6780 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 6781 | output, request_size); |
| 6782 | if (ret != 0) { |
| 6783 | return mbedtls_to_psa_error(ret); |
| 6784 | } |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 6785 | output_size -= request_size; |
| 6786 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 6787 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6788 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 6789 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6790 | } |
| 6791 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6792 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6793 | * |
| 6794 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 6795 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 6796 | * PSA API doesn't support passing an explicit state. |
| 6797 | * |
| 6798 | * In the non-external case, psa_generate_random() calls an |
| 6799 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 6800 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 6801 | * instead of doing this back-and-forth between the PSA API and the |
| 6802 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 6803 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6804 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6805 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 6806 | int mbedtls_psa_get_random(void *p_rng, |
| 6807 | unsigned char *output, |
| 6808 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6809 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 6810 | /* This function takes a pointer to the RNG state because that's what |
| 6811 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 6812 | * its own state internally and doesn't let the caller access that state. |
| 6813 | * So we just ignore the state parameter, and in practice we'll pass |
| 6814 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6815 | (void) p_rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6816 | psa_status_t status = psa_generate_random(output, output_size); |
| 6817 | if (status == PSA_SUCCESS) { |
| 6818 | return 0; |
| 6819 | } else { |
| 6820 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 6821 | } |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 6822 | } |
| 6823 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 6824 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6825 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
Chris Jones | ea0a865 | 2021-03-09 19:11:19 +0000 | [diff] [blame] | 6826 | #include "entropy_poll.h" |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6828 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
| 6829 | size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6830 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6831 | if (global_data.initialized) { |
| 6832 | return PSA_ERROR_NOT_PERMITTED; |
| 6833 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6835 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 6836 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 6837 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { |
| 6838 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6839 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 6840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6841 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6842 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 6843 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 6844 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6845 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6846 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6847 | * \param type The key type |
| 6848 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6849 | * |
| 6850 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6851 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6852 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 6853 | * The size in bits of the key is not valid. |
| 6854 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 6855 | * The type and/or the size in bits of the key or the combination of |
| 6856 | * the two is not supported. |
| 6857 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6858 | 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] | 6859 | psa_key_type_t type, size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6860 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 6861 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6863 | if (key_type_is_raw_bytes(type)) { |
| 6864 | status = psa_validate_unstructured_key_bit_size(type, bits); |
| 6865 | if (status != PSA_SUCCESS) { |
| 6866 | return status; |
| 6867 | } |
| 6868 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6869 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6870 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 6871 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { |
| 6872 | return PSA_ERROR_NOT_SUPPORTED; |
| 6873 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6874 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6875 | /* Accept only byte-aligned keys, for the same reasons as |
| 6876 | * in psa_import_rsa_key(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6877 | if (bits % 8 != 0) { |
| 6878 | return PSA_ERROR_NOT_SUPPORTED; |
| 6879 | } |
| 6880 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6881 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6882 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6883 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6884 | 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] | 6885 | /* To avoid empty block, return successfully here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6886 | return PSA_SUCCESS; |
| 6887 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 6888 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6889 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6890 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6891 | } |
| 6892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6893 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6894 | } |
| 6895 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 6896 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6897 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6898 | 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] | 6899 | { |
| 6900 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 6901 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6903 | if ((attributes->domain_parameters == NULL) && |
| 6904 | (attributes->domain_parameters_size != 0)) { |
| 6905 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6906 | } |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 6907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6908 | if (key_type_is_raw_bytes(type)) { |
| 6909 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 6910 | if (status != PSA_SUCCESS) { |
| 6911 | return status; |
| 6912 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 6913 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 6914 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6915 | if (type == PSA_KEY_TYPE_DES) { |
| 6916 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 6917 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 6918 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6919 | } else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6920 | |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6921 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 6922 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6923 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 6924 | return mbedtls_psa_rsa_generate_key(attributes, |
| 6925 | key_buffer, |
| 6926 | key_buffer_size, |
| 6927 | key_buffer_length); |
| 6928 | } else |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 6929 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 6930 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6931 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6932 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6933 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 6934 | return mbedtls_psa_ecp_generate_key(attributes, |
| 6935 | key_buffer, |
| 6936 | key_buffer_size, |
| 6937 | key_buffer_length); |
| 6938 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 6939 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6940 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6941 | (void) key_buffer_length; |
| 6942 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 6943 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6945 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6946 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6948 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 6949 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6950 | { |
| 6951 | psa_status_t status; |
| 6952 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 6953 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 6954 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6955 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 6956 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6957 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6958 | /* Reject any attempt to create a zero-length key so that we don't |
| 6959 | * 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] | 6960 | if (psa_get_key_bits(attributes) == 0) { |
| 6961 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6962 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 6963 | |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6964 | /* Reject any attempt to create a public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6965 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { |
| 6966 | return PSA_ERROR_INVALID_ARGUMENT; |
| 6967 | } |
Przemyslaw Stekiel | c0fe820 | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 6968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6969 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 6970 | &slot, &driver); |
| 6971 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6972 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6973 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 6974 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6975 | /* 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] | 6976 | * storage ( thus not in the case of generating a key in a secure element |
| 6977 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 6978 | * buffer to hold the generated key material. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6979 | if (slot->key.data == NULL) { |
| 6980 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 6981 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6982 | status = psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6983 | attributes->core.type, attributes->core.bits); |
| 6984 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6985 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6986 | } |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6987 | |
| 6988 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6989 | attributes->core.type, |
| 6990 | attributes->core.bits); |
| 6991 | } else { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6992 | status = psa_driver_wrapper_get_key_buffer_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6993 | attributes, &key_buffer_size); |
| 6994 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 6995 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6996 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6997 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 6998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6999 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 7000 | if (status != PSA_SUCCESS) { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7001 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7002 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 7003 | } |
| 7004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7005 | status = psa_driver_wrapper_generate_key(attributes, |
| 7006 | slot->key.data, slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7008 | if (status != PSA_SUCCESS) { |
| 7009 | psa_remove_key_data_from_memory(slot); |
| 7010 | } |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 7011 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 7012 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7013 | if (status == PSA_SUCCESS) { |
| 7014 | status = psa_finish_key_creation(slot, driver, key); |
| 7015 | } |
| 7016 | if (status != PSA_SUCCESS) { |
| 7017 | psa_fail_key_creation(slot, driver); |
| 7018 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 7019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7020 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7021 | } |
| 7022 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7023 | /****************************************************************/ |
| 7024 | /* Module setup */ |
| 7025 | /****************************************************************/ |
| 7026 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7027 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7028 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7029 | void (* entropy_init)(mbedtls_entropy_context *ctx), |
| 7030 | void (* entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7031 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7032 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 7033 | return PSA_ERROR_BAD_STATE; |
| 7034 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7035 | global_data.rng.entropy_init = entropy_init; |
| 7036 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7037 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7038 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 7039 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 7040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7041 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7042 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7043 | psa_wipe_all_key_slots(); |
| 7044 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 7045 | mbedtls_psa_random_free(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7046 | } |
| 7047 | /* Wipe all remaining data, including configuration. |
| 7048 | * In particular, this sets all state indicator to the value |
| 7049 | * indicating "uninitialized". */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7050 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 7051 | |
| 7052 | /* Terminate drivers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7053 | psa_driver_wrapper_free(); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7054 | } |
| 7055 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7056 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 7057 | /** Recover a transaction that was interrupted by a power failure. |
| 7058 | * |
| 7059 | * This function is called during initialization, before psa_crypto_init() |
| 7060 | * returns. If this function returns a failure status, the initialization |
| 7061 | * fails. |
| 7062 | */ |
| 7063 | static psa_status_t psa_crypto_recover_transaction( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7064 | const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7065 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7066 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7067 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 7068 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7069 | /* TODO - fall through to the failure case until this |
| 7070 | * is implemented. |
| 7071 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 7072 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7073 | default: |
| 7074 | /* We found an unsupported transaction in the storage. |
| 7075 | * We don't know what state the storage is in. Give up. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7076 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7077 | } |
| 7078 | } |
| 7079 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 7080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7081 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7082 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 7083 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7084 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7085 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7086 | if (global_data.initialized != 0) { |
| 7087 | return PSA_SUCCESS; |
| 7088 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7089 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 7090 | /* Initialize and seed the random generator. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7091 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7092 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7093 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 7094 | if (status != PSA_SUCCESS) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7095 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7096 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7097 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7099 | status = psa_initialize_key_slots(); |
| 7100 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 7101 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7102 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7103 | |
Ronald Cron | 9ba7691 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 7104 | /* Init drivers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7105 | status = psa_driver_wrapper_init(); |
| 7106 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 7107 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7108 | } |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 7109 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 7110 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7111 | status = psa_crypto_load_transaction(); |
| 7112 | if (status == PSA_SUCCESS) { |
| 7113 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 7114 | if (status != PSA_SUCCESS) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 7115 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7116 | } |
| 7117 | status = psa_crypto_stop_transaction(); |
| 7118 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 7119 | /* There's no transaction to complete. It's all good. */ |
| 7120 | status = PSA_SUCCESS; |
| 7121 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 7122 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 7123 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 7124 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7125 | global_data.initialized = 1; |
| 7126 | |
| 7127 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7128 | if (status != PSA_SUCCESS) { |
| 7129 | mbedtls_psa_crypto_free(); |
| 7130 | } |
| 7131 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 7132 | } |
| 7133 | |
| 7134 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |