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" |
| 30 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 31 | #include "psa_crypto_cipher.h" |
Gilles Peskine | 039b90c | 2018-12-07 18:24:41 +0100 | [diff] [blame] | 32 | #include "psa_crypto_core.h" |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 33 | #include "psa_crypto_invasive.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 34 | #include "psa_crypto_driver_wrappers.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 35 | #include "psa_crypto_ecp.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 36 | #include "psa_crypto_hash.h" |
Steven Cooreman | 32d5694 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 37 | #include "psa_crypto_mac.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 38 | #include "psa_crypto_rsa.h" |
Ronald Cron | 7023db5 | 2020-11-20 18:17:42 +0100 | [diff] [blame] | 39 | #include "psa_crypto_ecp.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 41 | #include "psa_crypto_se.h" |
Gilles Peskine | a8ade16 | 2019-06-26 11:24:49 +0200 | [diff] [blame] | 42 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 43 | #include "psa_crypto_slot_management.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 44 | /* Include internal declarations that are useful for implementing persistently |
| 45 | * stored keys. */ |
| 46 | #include "psa_crypto_storage.h" |
| 47 | |
Gilles Peskine | b2b64d3 | 2020-12-14 16:43:58 +0100 | [diff] [blame] | 48 | #include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 49 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 50 | #include <stdlib.h> |
| 51 | #include <string.h> |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 52 | #include "mbedtls/platform.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 53 | |
Gilles Peskine | 1c49f1a | 2020-11-13 18:46:25 +0100 | [diff] [blame] | 54 | #include "mbedtls/aes.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 55 | #include "mbedtls/arc4.h" |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 56 | #include "mbedtls/asn1.h" |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 57 | #include "mbedtls/asn1write.h" |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 58 | #include "mbedtls/bignum.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 59 | #include "mbedtls/blowfish.h" |
| 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" |
| 72 | #include "mbedtls/md2.h" |
| 73 | #include "mbedtls/md4.h" |
| 74 | #include "mbedtls/md5.h" |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 75 | #include "mbedtls/md.h" |
| 76 | #include "mbedtls/md_internal.h" |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 77 | #include "mbedtls/pk.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 78 | #include "mbedtls/pk_internal.h" |
Gilles Peskine | 3f10812 | 2018-12-07 18:14:53 +0100 | [diff] [blame] | 79 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 80 | #include "mbedtls/error.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 81 | #include "mbedtls/ripemd160.h" |
Gilles Peskine | 969ac72 | 2018-01-28 18:16:59 +0100 | [diff] [blame] | 82 | #include "mbedtls/rsa.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 83 | #include "mbedtls/sha1.h" |
| 84 | #include "mbedtls/sha256.h" |
| 85 | #include "mbedtls/sha512.h" |
| 86 | #include "mbedtls/xtea.h" |
| 87 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 88 | #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array))) |
Gilles Peskine | 996deb1 | 2018-08-01 15:45:45 +0200 | [diff] [blame] | 89 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 90 | /****************************************************************/ |
| 91 | /* Global data, support functions and library management */ |
| 92 | /****************************************************************/ |
| 93 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | static int key_type_is_raw_bytes(psa_key_type_t type) |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 95 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | return PSA_KEY_TYPE_IS_UNSTRUCTURED(type); |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 99 | /* Values for psa_global_data_t::rng_state */ |
| 100 | #define RNG_NOT_INITIALIZED 0 |
| 101 | #define RNG_INITIALIZED 1 |
| 102 | #define RNG_SEEDED 2 |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 103 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | typedef struct { |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 105 | unsigned initialized : 1; |
Gilles Peskine | 5a3c50e | 2018-12-04 12:27:09 +0100 | [diff] [blame] | 106 | unsigned rng_state : 2; |
Gilles Peskine | b8006a6 | 2021-11-08 22:20:03 +0100 | [diff] [blame] | 107 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 108 | } psa_global_data_t; |
| 109 | |
| 110 | static psa_global_data_t global_data; |
| 111 | |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 112 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 113 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 114 | &global_data.rng.drbg; |
| 115 | #endif |
| 116 | |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 117 | #define GUARD_MODULE_INITIALIZED \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 118 | if (global_data.initialized == 0) \ |
| 119 | return PSA_ERROR_BAD_STATE; |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 120 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | psa_status_t mbedtls_to_psa_error(int ret) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 122 | { |
Gilles Peskine | dbf6896 | 2021-01-06 20:04:23 +0100 | [diff] [blame] | 123 | /* Mbed TLS error codes can combine a high-level error code and a |
| 124 | * low-level error code. The low-level error usually reflects the |
| 125 | * root cause better, so dispatch on that preferably. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | int low_level_ret = -(-ret & 0x007f); |
| 127 | switch (low_level_ret != 0 ? low_level_ret : ret) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 128 | case 0: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 129 | return PSA_SUCCESS; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 130 | |
| 131 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 132 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 133 | case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 134 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 135 | case MBEDTLS_ERR_AES_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 137 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 138 | case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 140 | |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 141 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 142 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 143 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 144 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 145 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 146 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 147 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 148 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 149 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 150 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 151 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 152 | #if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 153 | case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 154 | #elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) |
| 155 | case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH: |
| 156 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 157 | case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 158 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 159 | case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 161 | |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 162 | #if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 163 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Jaeden Amero | 93e2111 | 2019-02-20 13:57:28 +0000 | [diff] [blame] | 164 | #elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) |
| 165 | case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH: |
| 166 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 167 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 168 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 169 | case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 171 | |
| 172 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 173 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 174 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 175 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 176 | case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 177 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 178 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 179 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 181 | |
| 182 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 183 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 184 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 185 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 186 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 187 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 188 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 189 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 190 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 191 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 192 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 193 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 195 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 197 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 198 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 199 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 200 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 201 | case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 203 | |
| 204 | case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 205 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 206 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 207 | #if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 208 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 209 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 210 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 211 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 212 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 213 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 214 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 215 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 216 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 217 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 218 | #endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 219 | |
| 220 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 221 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 222 | case MBEDTLS_ERR_DES_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 223 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 224 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 225 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 226 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 227 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 229 | |
| 230 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 231 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 232 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 233 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 234 | case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 235 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 236 | |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 237 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 239 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 240 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 241 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 242 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 243 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 244 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 245 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 246 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 247 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 248 | #endif |
| 249 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 250 | case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED: |
| 251 | case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED: |
| 252 | case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 253 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 254 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 255 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 256 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 257 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 259 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 260 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 261 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 262 | return PSA_ERROR_STORAGE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 263 | case MBEDTLS_ERR_MD_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 265 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 266 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 267 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 268 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 269 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 270 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 271 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 272 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 273 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 274 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 275 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 276 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 277 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 278 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 279 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 280 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 281 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 282 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 283 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 284 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 285 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 286 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 287 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 288 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 289 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 290 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 291 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 292 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 293 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 294 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 295 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 296 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 297 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 298 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 299 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 300 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 301 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 302 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 304 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 305 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 306 | case MBEDTLS_ERR_PK_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 307 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 308 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 309 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 310 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 311 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 312 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 313 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 314 | case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 315 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 316 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 317 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 318 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 319 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 320 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 321 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 322 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 323 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 324 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 325 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 326 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 327 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 328 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 329 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 330 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 331 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 332 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 333 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 334 | case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 335 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 336 | case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 337 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 338 | |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 339 | case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED: |
| 340 | case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED: |
| 341 | case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 342 | return PSA_ERROR_HARDWARE_FAILURE; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 343 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 344 | case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 345 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 346 | case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 347 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 348 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 349 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 350 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 351 | return PSA_ERROR_INVALID_ARGUMENT; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 352 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 353 | return PSA_ERROR_BUFFER_TOO_SMALL; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 354 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 355 | return PSA_ERROR_NOT_SUPPORTED; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 356 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 357 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 358 | return PSA_ERROR_INVALID_SIGNATURE; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 359 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 360 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 361 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 362 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 363 | case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 364 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 365 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 366 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 367 | return PSA_ERROR_CORRUPTION_DETECTED; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 368 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 369 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 370 | return PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 374 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 375 | |
| 376 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 377 | /****************************************************************/ |
| 378 | /* Key management */ |
| 379 | /****************************************************************/ |
| 380 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 381 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 382 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 383 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 384 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \ |
| 385 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 386 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, |
| 387 | size_t bits, |
| 388 | int bits_is_sloppy) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 389 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 390 | switch (curve) { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 391 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 392 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 393 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 394 | case 192: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 395 | return MBEDTLS_ECP_DP_SECP192R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 396 | #endif |
| 397 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 398 | case 224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 399 | return MBEDTLS_ECP_DP_SECP224R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 400 | #endif |
| 401 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 402 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 403 | return MBEDTLS_ECP_DP_SECP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 404 | #endif |
| 405 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 406 | case 384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 407 | return MBEDTLS_ECP_DP_SECP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 408 | #endif |
| 409 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 410 | case 521: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 411 | return MBEDTLS_ECP_DP_SECP521R1; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 412 | case 528: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 413 | if (bits_is_sloppy) { |
| 414 | return MBEDTLS_ECP_DP_SECP521R1; |
| 415 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 416 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 417 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 418 | } |
| 419 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 420 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 421 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 422 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 423 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 424 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 425 | return MBEDTLS_ECP_DP_BP256R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 426 | #endif |
| 427 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 428 | case 384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 429 | return MBEDTLS_ECP_DP_BP384R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 430 | #endif |
| 431 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 432 | case 512: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 433 | return MBEDTLS_ECP_DP_BP512R1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 434 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 435 | } |
| 436 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 437 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 438 | case PSA_ECC_FAMILY_MONTGOMERY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 439 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 440 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 441 | case 255: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 442 | return MBEDTLS_ECP_DP_CURVE25519; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 443 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 444 | if (bits_is_sloppy) { |
| 445 | return MBEDTLS_ECP_DP_CURVE25519; |
| 446 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 447 | break; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 448 | #endif |
| 449 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 450 | case 448: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 451 | return MBEDTLS_ECP_DP_CURVE448; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 452 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 453 | } |
| 454 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 455 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 456 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 457 | switch (bits) { |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 458 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 459 | case 192: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 460 | return MBEDTLS_ECP_DP_SECP192K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 461 | #endif |
| 462 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 463 | case 224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 464 | return MBEDTLS_ECP_DP_SECP224K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 465 | #endif |
| 466 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 467 | case 256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 468 | return MBEDTLS_ECP_DP_SECP256K1; |
Gilles Peskine | a1684f4 | 2021-03-23 13:12:34 +0100 | [diff] [blame] | 469 | #endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 470 | } |
| 471 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 472 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 473 | |
Gilles Peskine | 71f45ba | 2021-03-23 14:17:55 +0100 | [diff] [blame] | 474 | (void) bits_is_sloppy; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 475 | return MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 476 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 477 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
Ronald Cron | 9ecdd97 | 2021-11-19 18:02:34 +0100 | [diff] [blame] | 478 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || |
| 479 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 480 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || |
| 481 | defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 482 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 483 | static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type, |
| 484 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 485 | { |
| 486 | /* Check that the bit size is acceptable for the key type */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 487 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 488 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 489 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 490 | case PSA_KEY_TYPE_DERIVE: |
| 491 | break; |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 492 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 493 | case PSA_KEY_TYPE_AES: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 494 | if (bits != 128 && bits != 192 && bits != 256) { |
| 495 | return PSA_ERROR_INVALID_ARGUMENT; |
| 496 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 497 | break; |
| 498 | #endif |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 499 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 500 | case PSA_KEY_TYPE_ARIA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 501 | if (bits != 128 && bits != 192 && bits != 256) { |
| 502 | return PSA_ERROR_INVALID_ARGUMENT; |
| 503 | } |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 504 | break; |
| 505 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 506 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 507 | case PSA_KEY_TYPE_CAMELLIA: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 508 | if (bits != 128 && bits != 192 && bits != 256) { |
| 509 | return PSA_ERROR_INVALID_ARGUMENT; |
| 510 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 511 | break; |
| 512 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 513 | #if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 514 | case PSA_KEY_TYPE_DES: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 515 | if (bits != 64 && bits != 128 && bits != 192) { |
| 516 | return PSA_ERROR_INVALID_ARGUMENT; |
| 517 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 518 | break; |
| 519 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 520 | #if defined(PSA_WANT_KEY_TYPE_ARC4) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 521 | case PSA_KEY_TYPE_ARC4: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 522 | if (bits < 8 || bits > 2048) { |
| 523 | return PSA_ERROR_INVALID_ARGUMENT; |
| 524 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 525 | break; |
| 526 | #endif |
Ronald Cron | 1f0db80 | 2021-03-12 09:59:30 +0100 | [diff] [blame] | 527 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 528 | case PSA_KEY_TYPE_CHACHA20: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 529 | if (bits != 256) { |
| 530 | return PSA_ERROR_INVALID_ARGUMENT; |
| 531 | } |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 532 | break; |
| 533 | #endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 534 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 535 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 536 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 537 | if (bits % 8 != 0) { |
| 538 | return PSA_ERROR_INVALID_ARGUMENT; |
| 539 | } |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 540 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 541 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 542 | } |
| 543 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 544 | /** 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] | 545 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 546 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 547 | * 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] | 548 | * |
| 549 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 550 | * \param[in] key_type The key type of the key to be used with the |
| 551 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 552 | * |
| 553 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 554 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 555 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 556 | * 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] | 557 | */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 558 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
Steven Cooreman | 58c94d3 | 2021-03-02 21:31:17 +0100 | [diff] [blame] | 559 | psa_algorithm_t algorithm, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 560 | psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 561 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 562 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 563 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 564 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 565 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 566 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 567 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 568 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 569 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 570 | * key. */ |
| 571 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 572 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 573 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 574 | * the block length (larger than 1) for block ciphers. */ |
| 575 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 576 | return PSA_SUCCESS; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 584 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 585 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 586 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 587 | if (slot->key.data != NULL) { |
| 588 | return PSA_ERROR_ALREADY_EXISTS; |
| 589 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 590 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 591 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 592 | if (slot->key.data == NULL) { |
| 593 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 594 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 595 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 596 | slot->key.bytes = buffer_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 597 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 600 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 601 | const uint8_t *data, |
| 602 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 603 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 604 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 605 | data_length); |
| 606 | if (status != PSA_SUCCESS) { |
| 607 | return status; |
| 608 | } |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 609 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 610 | memcpy(slot->key.data, data, data_length); |
| 611 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Ronald Cron | a0fe59f | 2020-11-29 11:14:53 +0100 | [diff] [blame] | 614 | psa_status_t psa_import_key_into_slot( |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 615 | const psa_key_attributes_t *attributes, |
| 616 | const uint8_t *data, size_t data_length, |
| 617 | uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 618 | size_t *key_buffer_length, size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 619 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 620 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 621 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 622 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 623 | /* zero-length keys are never supported. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 624 | if (data_length == 0) { |
| 625 | return PSA_ERROR_NOT_SUPPORTED; |
| 626 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 627 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 628 | if (key_type_is_raw_bytes(type)) { |
| 629 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 630 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 631 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 632 | if (data_length > SIZE_MAX / 8) { |
| 633 | return PSA_ERROR_NOT_SUPPORTED; |
| 634 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 635 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 636 | /* Enforce a size limit, and in particular ensure that the bit |
| 637 | * size fits in its representation type. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 638 | if ((*bits) > PSA_MAX_KEY_BITS) { |
| 639 | return PSA_ERROR_NOT_SUPPORTED; |
| 640 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 641 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 642 | status = validate_unstructured_key_bit_size(type, *bits); |
| 643 | if (status != PSA_SUCCESS) { |
| 644 | return status; |
| 645 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 646 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 647 | /* Copy the key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 648 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 649 | *key_buffer_length = data_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 650 | (void) key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 651 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 652 | return PSA_SUCCESS; |
| 653 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 654 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 655 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 656 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 657 | return mbedtls_psa_ecp_import_key(attributes, |
| 658 | data, data_length, |
| 659 | key_buffer, key_buffer_size, |
| 660 | key_buffer_length, |
| 661 | bits); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 662 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 663 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 664 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 665 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 666 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 667 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 668 | return mbedtls_psa_rsa_import_key(attributes, |
| 669 | data, data_length, |
| 670 | key_buffer, key_buffer_size, |
| 671 | key_buffer_length, |
| 672 | bits); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 673 | } |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 674 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 675 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 676 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 677 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 678 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 681 | /** Calculate the intersection of two algorithm usage policies. |
| 682 | * |
| 683 | * Return 0 (which allows no operation) on incompatibility. |
| 684 | */ |
| 685 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 686 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 687 | psa_algorithm_t alg1, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 688 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 689 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 690 | /* Common case: both sides actually specify the same policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 691 | if (alg1 == alg2) { |
| 692 | return alg1; |
| 693 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 694 | /* If the policies are from the same hash-and-sign family, check |
| 695 | * if one is a wildcard. If so the other has the specific algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 696 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 697 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 698 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 699 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 700 | return alg2; |
| 701 | } |
| 702 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 703 | return alg1; |
| 704 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 705 | } |
| 706 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 707 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 708 | * restrictive tag length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 709 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 710 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 711 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 712 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 713 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 714 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 715 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 716 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 717 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 718 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 719 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 720 | alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 721 | } |
| 722 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 723 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 724 | (alg1_len <= alg2_len)) { |
| 725 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 726 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 727 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 728 | (alg2_len <= alg1_len)) { |
| 729 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | /* If the policies are from the same MAC family, check whether one |
| 733 | * of them is a minimum-MAC-length policy. Calculate the most |
| 734 | * restrictive tag length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 735 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 736 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 737 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 738 | /* Validate the combination of key type and algorithm. Since the base |
| 739 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 740 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 741 | return 0; |
| 742 | } |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 743 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 744 | /* Get the (exact or at-least) output lengths for both sides of the |
| 745 | * requested intersection. None of the currently supported algorithms |
| 746 | * have an output length dependent on the actual key size, so setting it |
| 747 | * to a bogus value of 0 is currently OK. |
| 748 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 749 | * Note that for at-least-this-length wildcard algorithms, the output |
| 750 | * length is set to the shortest allowed length, which allows us to |
| 751 | * calculate the most restrictive tag length for the intersection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 752 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 753 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 754 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 755 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 756 | /* If both are wildcards, return most restrictive wildcard */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 757 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 758 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 759 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 760 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 761 | |
| 762 | /* If only one is an at-least-this-length policy, the intersection would |
| 763 | * be the other (fixed-length) policy as long as said fixed length is |
| 764 | * equal to or larger than the shortest allowed length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 765 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 766 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 767 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 768 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 769 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 770 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 771 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 772 | /* If none of them are wildcards, check whether they define the same tag |
| 773 | * length. This is still possible here when one is default-length and |
| 774 | * the other specific-length. Ensure to always return the |
| 775 | * specific-length version for the intersection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 776 | if (alg1_len == alg2_len) { |
| 777 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 778 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 779 | } |
| 780 | /* If the policies are incompatible, allow nothing. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 781 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 782 | } |
| 783 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 784 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 785 | psa_algorithm_t policy_alg, |
| 786 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 787 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 788 | /* Common case: the policy only allows requested_alg. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 789 | if (requested_alg == policy_alg) { |
| 790 | return 1; |
| 791 | } |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 792 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 793 | * and requested_alg is the same hash-and-sign family with any hash, |
| 794 | * then requested_alg is compliant with policy_alg. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 795 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 796 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 797 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 798 | (requested_alg & ~PSA_ALG_HASH_MASK); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 799 | } |
| 800 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 801 | * the requested algorithm, check the requested tag length to be |
| 802 | * equal-length or longer than the wildcard-specified length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 803 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 804 | PSA_ALG_IS_AEAD(requested_alg) && |
| 805 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 806 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 807 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 808 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 809 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 810 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 811 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 812 | * algorithm, check whether their MAC lengths are compatible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 813 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 814 | PSA_ALG_IS_MAC(requested_alg) && |
| 815 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 816 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 817 | /* Validate the combination of key type and algorithm. Since the policy |
| 818 | * and requested algorithms are the same, we only need this once. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 819 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 820 | return 0; |
| 821 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 822 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 823 | /* Get both the requested output length for the algorithm which is to be |
| 824 | * verified, and the default output length for the base algorithm. |
| 825 | * Note that none of the currently supported algorithms have an output |
| 826 | * length dependent on actual key size, so setting it to a bogus value |
| 827 | * of 0 is currently OK. */ |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 828 | size_t requested_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 829 | key_type, 0, requested_alg); |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 830 | size_t default_output_length = PSA_MAC_LENGTH( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 831 | key_type, 0, |
| 832 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 833 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 834 | /* If the policy is default-length, only allow an algorithm with |
| 835 | * a declared exact-length matching the default. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 836 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 837 | return requested_output_length == default_output_length; |
| 838 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 839 | |
| 840 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 841 | * length exactly matches the default length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 842 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 843 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 844 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 847 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 848 | * check for the requested MAC length to be equal to or longer than the |
| 849 | * minimum allowed length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 850 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 851 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 852 | requested_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 853 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 854 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 855 | /* 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] | 856 | * a key derivation with that key agreement should also be allowed. This |
| 857 | * behaviour is expected to be defined in a future specification version. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 858 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 859 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 860 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 861 | policy_alg; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 862 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 863 | /* If it isn't explicitly permitted, it's forbidden. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 864 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 865 | } |
| 866 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 867 | /** Test whether a policy permits an algorithm. |
| 868 | * |
| 869 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 870 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 871 | * \note This function requires providing the key type for which the policy is |
| 872 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 873 | * have different properties depending on what kind of cipher it is |
| 874 | * combined with. |
| 875 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 876 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 877 | * allowed by the \p policy. |
| 878 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 879 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 880 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 881 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 882 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 883 | psa_key_type_t key_type, |
| 884 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 885 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 886 | /* '0' is not a valid algorithm */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 887 | if (alg == 0) { |
| 888 | return PSA_ERROR_INVALID_ARGUMENT; |
| 889 | } |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 890 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 891 | /* A requested algorithm cannot be a wildcard. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 892 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 893 | return PSA_ERROR_INVALID_ARGUMENT; |
| 894 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 895 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 896 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 897 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 898 | return PSA_SUCCESS; |
| 899 | } else { |
| 900 | return PSA_ERROR_NOT_PERMITTED; |
| 901 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 902 | } |
| 903 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 904 | /** Restrict a key policy based on a constraint. |
| 905 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 906 | * \note This function requires providing the key type for which the policy is |
| 907 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 908 | * have different properties depending on what kind of cipher it is |
| 909 | * combined with. |
| 910 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 911 | * \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] | 912 | * \param[in,out] policy The policy to restrict. |
| 913 | * \param[in] constraint The policy constraint to apply. |
| 914 | * |
| 915 | * \retval #PSA_SUCCESS |
| 916 | * \c *policy contains the intersection of the original value of |
| 917 | * \c *policy and \c *constraint. |
| 918 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 919 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 920 | * \c *policy is unchanged. |
| 921 | */ |
| 922 | static psa_status_t psa_restrict_key_policy( |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 923 | psa_key_type_t key_type, |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 924 | psa_key_policy_t *policy, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 925 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 926 | { |
| 927 | psa_algorithm_t intersection_alg = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 928 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 929 | constraint->alg); |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 930 | psa_algorithm_t intersection_alg2 = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 931 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 932 | constraint->alg2); |
| 933 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 934 | return PSA_ERROR_INVALID_ARGUMENT; |
| 935 | } |
| 936 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 937 | return PSA_ERROR_INVALID_ARGUMENT; |
| 938 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 939 | policy->usage &= constraint->usage; |
| 940 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 941 | policy->alg2 = intersection_alg2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 942 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 943 | } |
| 944 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 945 | /** Get the description of a key given its identifier and policy constraints |
| 946 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 947 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 948 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 949 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 950 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 951 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 952 | * In case of a persistent key, the function loads the description of the key |
| 953 | * into a key slot if not already done. |
| 954 | * |
| 955 | * On success, the returned key slot is locked. It is the responsibility of |
| 956 | * the caller to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 957 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 958 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 959 | mbedtls_svc_key_id_t key, |
| 960 | psa_key_slot_t **p_slot, |
| 961 | psa_key_usage_t usage, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 962 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 963 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 964 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 965 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 966 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 967 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 968 | if (status != PSA_SUCCESS) { |
| 969 | return status; |
| 970 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 971 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 972 | |
| 973 | /* Enforce that usage policy for the key slot contains all the flags |
| 974 | * required by the usage parameter. There is one exception: public |
| 975 | * keys can always be exported, so we treat public key objects as |
| 976 | * if they had the export flag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 977 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 978 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 979 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 980 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 981 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 982 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 983 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 984 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 985 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 986 | /* Enforce that the usage policy permits the requested algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 987 | if (alg != 0) { |
| 988 | status = psa_key_policy_permits(&slot->attr.policy, |
| 989 | slot->attr.type, |
| 990 | alg); |
| 991 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 992 | goto error; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 993 | } |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 994 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 995 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 996 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 997 | |
| 998 | error: |
| 999 | *p_slot = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1000 | psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1001 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1002 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 1003 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 1004 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1005 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1006 | * |
| 1007 | * A transparent key is a key for which the key material is directly |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1008 | * available, as opposed to a key in a secure element and/or to be used |
| 1009 | * by a secure element. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1010 | * |
Ronald Cron | 8a0466a | 2021-08-24 15:39:44 +0200 | [diff] [blame] | 1011 | * This is a temporary function that may be used instead of |
| 1012 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1013 | * for a cryptographic operation. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1014 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1015 | * On success, the returned key slot is locked. It is the responsibility of the |
| 1016 | * 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] | 1017 | */ |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1018 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1019 | mbedtls_svc_key_id_t key, |
| 1020 | psa_key_slot_t **p_slot, |
| 1021 | psa_key_usage_t usage, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1022 | psa_algorithm_t alg) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1023 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1024 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 1025 | usage, alg); |
| 1026 | if (status != PSA_SUCCESS) { |
| 1027 | return status; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1028 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1029 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1030 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
| 1031 | psa_unlock_key_slot(*p_slot); |
| 1032 | *p_slot = NULL; |
| 1033 | return PSA_ERROR_NOT_SUPPORTED; |
| 1034 | } |
| 1035 | |
| 1036 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1037 | } |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 1038 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1039 | 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] | 1040 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1041 | /* Data pointer will always be either a valid pointer or NULL in an |
| 1042 | * initialized slot, so we can just free it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1043 | if (slot->key.data != NULL) { |
| 1044 | mbedtls_platform_zeroize(slot->key.data, slot->key.bytes); |
| 1045 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1046 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1047 | mbedtls_free(slot->key.data); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1048 | slot->key.data = NULL; |
| 1049 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1050 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1051 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1054 | /** Completely wipe a slot in memory, including its policy. |
| 1055 | * Persistent storage is not affected. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1056 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1057 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1058 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1059 | |
| 1060 | /* |
| 1061 | * As the return error code may not be handled in case of multiple errors, |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1062 | * do our best to report an unexpected lock counter: if available |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1063 | * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as |
| 1064 | * part of the execution of a test suite this will stop the test suite |
Ronald Cron | 4640c15 | 2020-11-13 10:11:01 +0100 | [diff] [blame] | 1065 | * execution). |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1066 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1067 | if (slot->lock_count != 1) { |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1068 | #ifdef MBEDTLS_CHECK_PARAMS |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1069 | MBEDTLS_PARAM_FAILED(slot->lock_count == 1); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1070 | #endif |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 1071 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1072 | } |
| 1073 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 1074 | /* Multipart operations may still be using the key. This is safe |
| 1075 | * because all multipart operation objects are independent from |
| 1076 | * the key slot: if they need to access the key after the setup |
| 1077 | * phase, they have a copy of the key. Note that this means that |
| 1078 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1079 | /* At this point, key material and other type-specific content has |
| 1080 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1081 | * zeroize because the metadata is not particularly sensitive. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1082 | memset(slot, 0, sizeof(*slot)); |
| 1083 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1086 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1087 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1088 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1089 | psa_status_t status; /* status of the last operation */ |
| 1090 | psa_status_t overall_status = PSA_SUCCESS; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1091 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1092 | psa_se_drv_table_entry_t *driver; |
| 1093 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1094 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1095 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1096 | return PSA_SUCCESS; |
| 1097 | } |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 1098 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1099 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 1100 | * 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] | 1101 | * key, this will load the key description from persistent memory if not |
| 1102 | * done yet. We cannot avoid this loading as without it we don't know if |
| 1103 | * the key is operated by an SE or not and this information is needed by |
| 1104 | * the current implementation. |
| 1105 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1106 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1107 | if (status != PSA_SUCCESS) { |
| 1108 | return status; |
| 1109 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1110 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1111 | /* |
| 1112 | * If the key slot containing the key description is under access by the |
| 1113 | * library (apart from the present access), the key cannot be destroyed |
| 1114 | * yet. For the time being, just return in error. Eventually (to be |
| 1115 | * implemented), the key should be destroyed when all accesses have |
| 1116 | * stopped. |
| 1117 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1118 | if (slot->lock_count > 1) { |
| 1119 | psa_unlock_key_slot(slot); |
| 1120 | return PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1121 | } |
| 1122 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1123 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1124 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1125 | * if we attempt it, depending on whether the key is merely read-only |
| 1126 | * by policy or actually physically read-only). |
Gilles Peskine | 2bfbb17 | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1127 | * Just do the best we can, which is to wipe the copy in memory |
| 1128 | * (done in this function's cleanup code). */ |
| 1129 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1130 | goto exit; |
Gilles Peskine | 251c774 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1133 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1134 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1135 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1136 | /* For a key in a secure element, we need to do three things: |
| 1137 | * remove the key file in internal storage, destroy the |
| 1138 | * key inside the secure element, and update the driver's |
| 1139 | * persistent data. Start a transaction that will encompass these |
| 1140 | * three actions. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1141 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1142 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1143 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1144 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1145 | status = psa_crypto_save_transaction(); |
| 1146 | if (status != PSA_SUCCESS) { |
| 1147 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1148 | /* We should still try to destroy the key in the secure |
| 1149 | * element and the key metadata in storage. This is especially |
| 1150 | * important if the error is that the storage is full. |
| 1151 | * But how to do it exactly without risking an inconsistent |
| 1152 | * state after a reset? |
| 1153 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1154 | */ |
| 1155 | overall_status = status; |
| 1156 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1159 | status = psa_destroy_se_key(driver, |
| 1160 | psa_key_slot_get_slot_number(slot)); |
| 1161 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1162 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1163 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1164 | } |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1165 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1166 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1167 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1168 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1169 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1170 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1171 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1172 | } |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1173 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1174 | /* TODO: other slots may have a copy of the same key. We should |
| 1175 | * invalidate them. |
| 1176 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1177 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1178 | } |
| 1179 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1180 | |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1181 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1182 | if (driver != NULL) { |
| 1183 | status = psa_save_se_persistent_data(driver); |
| 1184 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1185 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1186 | } |
| 1187 | status = psa_crypto_stop_transaction(); |
| 1188 | if (overall_status == PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1189 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1190 | } |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1191 | } |
| 1192 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1193 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1194 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1195 | status = psa_wipe_key_slot(slot); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1196 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1197 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1198 | overall_status = status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1199 | } |
| 1200 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1201 | } |
| 1202 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1203 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1204 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1205 | static psa_status_t psa_get_rsa_public_exponent( |
| 1206 | const mbedtls_rsa_context *rsa, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1207 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1208 | { |
| 1209 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1210 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1211 | uint8_t *buffer = NULL; |
| 1212 | size_t buflen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1213 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1214 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1215 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1216 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1217 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1218 | } |
| 1219 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1220 | /* It's the default value, which is reported as an empty string, |
| 1221 | * so there's nothing to do. */ |
| 1222 | goto exit; |
| 1223 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1224 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1225 | buflen = mbedtls_mpi_size(&mpi); |
| 1226 | buffer = mbedtls_calloc(1, buflen); |
| 1227 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1228 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1229 | goto exit; |
| 1230 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1231 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1232 | if (ret != 0) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1233 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1234 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1235 | attributes->domain_parameters = buffer; |
| 1236 | attributes->domain_parameters_size = buflen; |
| 1237 | |
| 1238 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1239 | mbedtls_mpi_free(&mpi); |
| 1240 | if (ret != 0) { |
| 1241 | mbedtls_free(buffer); |
| 1242 | } |
| 1243 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1244 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1245 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1246 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1247 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1248 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1249 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1250 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1251 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1252 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1253 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1254 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1255 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1256 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1257 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1258 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1259 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1260 | if (status != PSA_SUCCESS) { |
| 1261 | return status; |
| 1262 | } |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1263 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1264 | attributes->core = slot->attr; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1265 | attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1266 | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1267 | |
| 1268 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1269 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1270 | psa_set_key_slot_number(attributes, |
| 1271 | psa_key_slot_get_slot_number(slot)); |
| 1272 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1273 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1275 | switch (slot->attr.type) { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1276 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1277 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1278 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1279 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1280 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1281 | * is not yet implemented. |
| 1282 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1283 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1284 | if (!psa_key_lifetime_is_external(slot->attr.lifetime)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1285 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1286 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1287 | status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1288 | slot->attr.type, |
| 1289 | slot->key.data, |
| 1290 | slot->key.bytes, |
| 1291 | &rsa); |
| 1292 | if (status != PSA_SUCCESS) { |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1293 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1294 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1295 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1296 | status = psa_get_rsa_public_exponent(rsa, |
| 1297 | attributes); |
| 1298 | mbedtls_rsa_free(rsa); |
| 1299 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1300 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1301 | break; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 1302 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1303 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1304 | default: |
| 1305 | /* Nothing else to do. */ |
| 1306 | break; |
| 1307 | } |
| 1308 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1309 | if (status != PSA_SUCCESS) { |
| 1310 | psa_reset_key_attributes(attributes); |
| 1311 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1312 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1313 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1314 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1315 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1316 | } |
| 1317 | |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1318 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1319 | psa_status_t psa_get_key_slot_number( |
| 1320 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1321 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1322 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1323 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1324 | *slot_number = attributes->slot_number; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1325 | return PSA_SUCCESS; |
| 1326 | } else { |
| 1327 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1328 | } |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1329 | } |
| 1330 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1331 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1332 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1333 | size_t key_buffer_size, |
| 1334 | uint8_t *data, |
| 1335 | size_t data_size, |
| 1336 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1337 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1338 | if (key_buffer_size > data_size) { |
| 1339 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1340 | } |
| 1341 | memcpy(data, key_buffer, key_buffer_size); |
| 1342 | memset(data + key_buffer_size, 0, |
| 1343 | data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1344 | *data_length = key_buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1345 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1346 | } |
| 1347 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1348 | psa_status_t psa_export_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1349 | const psa_key_attributes_t *attributes, |
| 1350 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1351 | uint8_t *data, size_t data_size, size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1352 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1353 | psa_key_type_t type = attributes->core.type; |
| 1354 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1355 | if (key_type_is_raw_bytes(type) || |
| 1356 | PSA_KEY_TYPE_IS_RSA(type) || |
| 1357 | PSA_KEY_TYPE_IS_ECC(type)) { |
| 1358 | return psa_export_key_buffer_internal( |
| 1359 | key_buffer, key_buffer_size, |
| 1360 | data, data_size, data_length); |
| 1361 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1362 | /* This shouldn't happen in the reference implementation, but |
| 1363 | it is valid for a special-purpose implementation to omit |
| 1364 | support for exporting certain key types. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1365 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1369 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
| 1370 | uint8_t *data, |
| 1371 | size_t data_size, |
| 1372 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1373 | { |
| 1374 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1375 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1376 | psa_key_slot_t *slot; |
| 1377 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1378 | /* Reject a zero-length output buffer now, since this can never be a |
| 1379 | * valid key representation. This way we know that data must be a valid |
| 1380 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1381 | if (data_size == 0) { |
| 1382 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1383 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1384 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1385 | /* Set the key to empty now, so that even when there are errors, we always |
| 1386 | * set data_length to a value between 0 and data_size. On error, setting |
| 1387 | * the key to empty is a good choice because an empty key representation is |
| 1388 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1389 | *data_length = 0; |
| 1390 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1391 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1392 | * which don't require any flag, but |
| 1393 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1394 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1395 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1396 | PSA_KEY_USAGE_EXPORT, 0); |
| 1397 | if (status != PSA_SUCCESS) { |
| 1398 | return status; |
| 1399 | } |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1400 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1401 | psa_key_attributes_t attributes = { |
| 1402 | .core = slot->attr |
| 1403 | }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1404 | status = psa_driver_wrapper_export_key(&attributes, |
| 1405 | slot->key.data, slot->key.bytes, |
| 1406 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1407 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1408 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1409 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1410 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1411 | } |
| 1412 | |
Ronald Cron | 7285cda | 2020-11-26 14:46:37 +0100 | [diff] [blame] | 1413 | psa_status_t psa_export_public_key_internal( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1414 | const psa_key_attributes_t *attributes, |
| 1415 | const uint8_t *key_buffer, |
| 1416 | size_t key_buffer_size, |
| 1417 | uint8_t *data, |
| 1418 | size_t data_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1419 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1420 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1421 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1422 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1423 | if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) { |
| 1424 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1425 | /* Exporting public -> public */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1426 | return psa_export_key_buffer_internal( |
| 1427 | key_buffer, key_buffer_size, |
| 1428 | data, data_size, data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1429 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1430 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1431 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1432 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1433 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1434 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1435 | key_buffer, |
| 1436 | key_buffer_size, |
| 1437 | data, |
| 1438 | data_size, |
| 1439 | data_length); |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1440 | #else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1441 | /* We don't know how to convert a private RSA key to public. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1442 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1443 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1444 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1445 | } else { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 1446 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1447 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1448 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1449 | key_buffer, |
| 1450 | key_buffer_size, |
| 1451 | data, |
| 1452 | data_size, |
| 1453 | data_length); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1454 | #else |
| 1455 | /* We don't know how to convert a private ECC key to public */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1456 | return PSA_ERROR_NOT_SUPPORTED; |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1457 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || |
| 1458 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1459 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1460 | } else { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1461 | /* This shouldn't happen in the reference implementation, but |
| 1462 | it is valid for a special-purpose implementation to omit |
| 1463 | support for exporting certain key types. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1464 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1465 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1466 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1467 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1468 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, |
| 1469 | uint8_t *data, |
| 1470 | size_t data_size, |
| 1471 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1472 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1473 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1474 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1475 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1476 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1477 | /* Reject a zero-length output buffer now, since this can never be a |
| 1478 | * valid key representation. This way we know that data must be a valid |
| 1479 | * pointer and we can do things like memset(data, ..., data_size). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1480 | if (data_size == 0) { |
| 1481 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1482 | } |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1483 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1484 | /* Set the key to empty now, so that even when there are errors, we always |
| 1485 | * set data_length to a value between 0 and data_size. On error, setting |
| 1486 | * the key to empty is a good choice because an empty key representation is |
| 1487 | * unlikely to be accepted anywhere. */ |
| 1488 | *data_length = 0; |
| 1489 | |
| 1490 | /* Exporting a public key doesn't require a usage flag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1491 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1492 | if (status != PSA_SUCCESS) { |
| 1493 | return status; |
| 1494 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1495 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1496 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1497 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1498 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1499 | } |
| 1500 | |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1501 | psa_key_attributes_t attributes = { |
| 1502 | .core = slot->attr |
| 1503 | }; |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 1504 | status = psa_driver_wrapper_export_public_key( |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1505 | &attributes, slot->key.data, slot->key.bytes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1506 | data, data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1507 | |
| 1508 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1509 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1510 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1511 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1512 | } |
| 1513 | |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame^] | 1514 | MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1515 | "One or more key attribute flag is listed as both external-only and dual-use") |
| 1516 | MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1517 | "One or more key attribute flag is listed as both internal-only and dual-use") |
| 1518 | MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1519 | "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] | 1520 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1521 | /** Validate that a key policy is internally well-formed. |
| 1522 | * |
| 1523 | * This function only rejects invalid policies. It does not validate the |
| 1524 | * consistency of the policy with respect to other attributes of the key |
| 1525 | * such as the key type. |
| 1526 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1527 | 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] | 1528 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1529 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1530 | PSA_KEY_USAGE_COPY | |
| 1531 | PSA_KEY_USAGE_ENCRYPT | |
| 1532 | PSA_KEY_USAGE_DECRYPT | |
| 1533 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1534 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1535 | PSA_KEY_USAGE_SIGN_HASH | |
| 1536 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1537 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1538 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1539 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1540 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1541 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1542 | } |
| 1543 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1544 | /** Validate the internal consistency of key attributes. |
| 1545 | * |
| 1546 | * This function only rejects invalid attribute values. If does not |
| 1547 | * validate the consistency of the attributes with any key data that may |
| 1548 | * be involved in the creation of the key. |
| 1549 | * |
| 1550 | * Call this function early in the key creation process. |
| 1551 | * |
| 1552 | * \param[in] attributes Key attributes for the new key. |
| 1553 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1554 | * NULL for a transparent key. |
| 1555 | * |
| 1556 | */ |
| 1557 | static psa_status_t psa_validate_key_attributes( |
| 1558 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1559 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1560 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1561 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1562 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1563 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1564 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1565 | status = psa_validate_key_location(lifetime, p_drv); |
| 1566 | if (status != PSA_SUCCESS) { |
| 1567 | return status; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1568 | } |
| 1569 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1570 | status = psa_validate_key_persistence(lifetime); |
| 1571 | if (status != PSA_SUCCESS) { |
| 1572 | return status; |
| 1573 | } |
| 1574 | |
| 1575 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1576 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1577 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1578 | } |
| 1579 | } else { |
| 1580 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1581 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1586 | if (status != PSA_SUCCESS) { |
| 1587 | return status; |
| 1588 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1589 | |
| 1590 | /* Refuse to create overly large keys. |
| 1591 | * Note that this doesn't trigger on import if the attributes don't |
| 1592 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1593 | * psa_import_key() needs its own checks. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1594 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1595 | return PSA_ERROR_NOT_SUPPORTED; |
| 1596 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1597 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1598 | /* Reject invalid flags. These should not be reachable through the API. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1599 | if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | |
| 1600 | MBEDTLS_PSA_KA_MASK_DUAL_USE)) { |
| 1601 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1602 | } |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1603 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1604 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1605 | } |
| 1606 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1607 | /** Prepare a key slot to receive key material. |
| 1608 | * |
| 1609 | * This function allocates a key slot and sets its metadata. |
| 1610 | * |
| 1611 | * If this function fails, call psa_fail_key_creation(). |
| 1612 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1613 | * This function is intended to be used as follows: |
| 1614 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1615 | * it with the specified attributes, and in case of a volatile key assign it |
| 1616 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1617 | * -# Populate the slot with the key material. |
| 1618 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1619 | * In case of failure at any step, stop the sequence and call |
| 1620 | * psa_fail_key_creation(). |
| 1621 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1622 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1623 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1624 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1625 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1626 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1627 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1628 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1629 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1630 | * |
| 1631 | * \retval #PSA_SUCCESS |
| 1632 | * The key slot is ready to receive key material. |
| 1633 | * \return If this function fails, the key slot is an invalid state. |
| 1634 | * 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] | 1635 | */ |
| 1636 | static psa_status_t psa_start_key_creation( |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1637 | psa_key_creation_method_t method, |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1638 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1639 | psa_key_slot_t **p_slot, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1640 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1641 | { |
| 1642 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1643 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1644 | psa_key_slot_t *slot; |
| 1645 | |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1646 | (void) method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1647 | *p_drv = NULL; |
| 1648 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1649 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1650 | if (status != PSA_SUCCESS) { |
| 1651 | return status; |
| 1652 | } |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1653 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1654 | status = psa_get_empty_key_slot(&volatile_key_id, p_slot); |
| 1655 | if (status != PSA_SUCCESS) { |
| 1656 | return status; |
| 1657 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1658 | slot = *p_slot; |
| 1659 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1660 | /* We're storing the declared bit-size of the key. It's up to each |
| 1661 | * creation mechanism to verify that this information is correct. |
| 1662 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1663 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1664 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1665 | * volatile key identifier associated to the slot returned to contain its |
| 1666 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1667 | |
| 1668 | slot->attr = attributes->core; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1669 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1670 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1671 | slot->attr.id = volatile_key_id; |
| 1672 | #else |
| 1673 | slot->attr.id.key_id = volatile_key_id; |
| 1674 | #endif |
| 1675 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1676 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1677 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1678 | * external-only flags, query `attributes`. Thanks to the check |
| 1679 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1680 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1681 | * may have set. */ |
| 1682 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1683 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1684 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1685 | /* 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] | 1686 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1687 | * create the key file in internal storage, create the |
| 1688 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1689 | * persistent data. This is done by starting a transaction that will |
| 1690 | * encompass these three actions. |
| 1691 | * For registering a volatile key, we just need to find an appropriate |
| 1692 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1693 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1694 | /* The first thing to do is to find a slot number for the new key. |
| 1695 | * We save the slot number in persistent storage as part of the |
| 1696 | * transaction data. It will be needed to recover if the power |
| 1697 | * fails during the key creation process, to clean up on the secure |
| 1698 | * element side after restarting. Obtaining a slot number from the |
| 1699 | * secure element driver updates its persistent state, but we do not yet |
| 1700 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1701 | * we can roll back to a state where the key doesn't exist. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1702 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1703 | psa_key_slot_number_t slot_number; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1704 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1705 | &slot_number); |
| 1706 | if (status != PSA_SUCCESS) { |
| 1707 | return status; |
| 1708 | } |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1709 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1710 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1711 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1712 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1713 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1714 | psa_crypto_transaction.key.id = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1715 | status = psa_crypto_save_transaction(); |
| 1716 | if (status != PSA_SUCCESS) { |
| 1717 | (void) psa_crypto_stop_transaction(); |
| 1718 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1719 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1720 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1721 | |
| 1722 | status = psa_copy_key_material_into_slot( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1723 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1724 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1725 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1726 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1727 | /* Key registration only makes sense with a secure element. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1728 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1729 | } |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1730 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1731 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1732 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1733 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1734 | |
| 1735 | /** Finalize the creation of a key once its key material has been set. |
| 1736 | * |
| 1737 | * This entails writing the key to persistent storage. |
| 1738 | * |
| 1739 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1740 | * See the documentation of psa_start_key_creation() for the intended use |
| 1741 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1742 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1743 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1744 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1745 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1746 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1747 | * \param[in,out] slot Pointer to the slot with key material. |
| 1748 | * \param[in] driver The secure element driver for the key, |
| 1749 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1750 | * \param[out] key On success, identifier of the key. Note that the |
| 1751 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1752 | * |
| 1753 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1754 | * The key was successfully created. |
Gilles Peskine | ec1eff3 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 1755 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1756 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription |
| 1757 | * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription |
| 1758 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 1759 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1760 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1761 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1762 | * \return If this function fails, the key slot is an invalid state. |
| 1763 | * 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] | 1764 | */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1765 | static psa_status_t psa_finish_key_creation( |
| 1766 | psa_key_slot_t *slot, |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1767 | psa_se_drv_table_entry_t *driver, |
| 1768 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1769 | { |
| 1770 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 30afafd | 2019-04-25 13:47:40 +0200 | [diff] [blame] | 1771 | (void) slot; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1772 | (void) driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1773 | |
| 1774 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1775 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1776 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1777 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1778 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1779 | psa_key_slot_number_t slot_number = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1780 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1781 | |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame^] | 1782 | MBEDTLS_STATIC_ASSERT(sizeof(slot_number) == |
| 1783 | sizeof(data.slot_number), |
| 1784 | "Slot number size does not match psa_se_key_data_storage_t"); |
| 1785 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1786 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1787 | status = psa_save_persistent_key(&slot->attr, |
| 1788 | (uint8_t *) &data, |
| 1789 | sizeof(data)); |
| 1790 | } else |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1791 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1792 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1793 | /* Key material is saved in export representation in the slot, so |
| 1794 | * just pass the slot buffer for storage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1795 | status = psa_save_persistent_key(&slot->attr, |
| 1796 | slot->key.data, |
| 1797 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1798 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1799 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1800 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1801 | |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1802 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1803 | /* Finish the transaction for a key creation. This does not |
| 1804 | * happen when registering an existing key. Detect this case |
| 1805 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1806 | * creation of a persistent key in a secure element requires a transaction, |
| 1807 | * but registration or volatile key creation doesn't use one). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1808 | if (driver != NULL && |
| 1809 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1810 | status = psa_save_se_persistent_data(driver); |
| 1811 | if (status != PSA_SUCCESS) { |
| 1812 | psa_destroy_persistent_key(slot->attr.id); |
| 1813 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1814 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1815 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1816 | } |
| 1817 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1818 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1819 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1820 | *key = slot->attr.id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1821 | status = psa_unlock_key_slot(slot); |
| 1822 | if (status != PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1823 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1824 | } |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1825 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1826 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1827 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | /** Abort the creation of a key. |
| 1831 | * |
| 1832 | * You may call this function after calling psa_start_key_creation(), |
| 1833 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1834 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1835 | * See the documentation of psa_start_key_creation() for the intended use |
| 1836 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1837 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1838 | * \param[in,out] slot Pointer to the slot with key material. |
| 1839 | * \param[in] driver The secure element driver for the key, |
| 1840 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1841 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1842 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 1843 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1844 | { |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1845 | (void) driver; |
| 1846 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1847 | if (slot == NULL) { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1848 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1849 | } |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1850 | |
| 1851 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1852 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1853 | * element, and the failure happened later (when saving metadata |
| 1854 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1855 | * element. |
| 1856 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1857 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1858 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1859 | /* Abort the ongoing transaction if any (there may not be one if |
| 1860 | * the creation process failed before starting one, or if the |
| 1861 | * key creation is a registration of a key in a secure element). |
| 1862 | * Earlier functions must already have done what it takes to undo any |
| 1863 | * partial creation. All that's left is to update the transaction data |
| 1864 | * itself. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1865 | (void) psa_crypto_stop_transaction(); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1866 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1867 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1868 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1869 | } |
| 1870 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1871 | /** Validate optional attributes during key creation. |
| 1872 | * |
| 1873 | * Some key attributes are optional during key creation. If they are |
| 1874 | * specified in the attributes structure, check that they are consistent |
| 1875 | * with the data in the slot. |
| 1876 | * |
| 1877 | * This function should be called near the end of key creation, after |
| 1878 | * the slot in memory is fully populated but before saving persistent data. |
| 1879 | */ |
| 1880 | static psa_status_t psa_validate_optional_attributes( |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1881 | const psa_key_slot_t *slot, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1882 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1883 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1884 | if (attributes->core.type != 0) { |
| 1885 | if (attributes->core.type != slot->attr.type) { |
| 1886 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1887 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1890 | if (attributes->domain_parameters_size != 0) { |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 1891 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1892 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1893 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1894 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1895 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1896 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1897 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1898 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1899 | slot->attr.type, |
| 1900 | slot->key.data, |
| 1901 | slot->key.bytes, |
| 1902 | &rsa); |
| 1903 | if (status != PSA_SUCCESS) { |
| 1904 | return status; |
| 1905 | } |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1906 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1907 | mbedtls_mpi_init(&actual); |
| 1908 | mbedtls_mpi_init(&required); |
| 1909 | ret = mbedtls_rsa_export(rsa, |
| 1910 | NULL, NULL, NULL, NULL, &actual); |
| 1911 | mbedtls_rsa_free(rsa); |
| 1912 | mbedtls_free(rsa); |
| 1913 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1914 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1915 | } |
| 1916 | ret = mbedtls_mpi_read_binary(&required, |
| 1917 | attributes->domain_parameters, |
| 1918 | attributes->domain_parameters_size); |
| 1919 | if (ret != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1920 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1921 | } |
| 1922 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1923 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1924 | } |
| 1925 | rsa_exit: |
| 1926 | mbedtls_mpi_free(&actual); |
| 1927 | mbedtls_mpi_free(&required); |
| 1928 | if (ret != 0) { |
| 1929 | return mbedtls_to_psa_error(ret); |
| 1930 | } |
| 1931 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 1932 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 1933 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1934 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1935 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1939 | if (attributes->core.bits != 0) { |
| 1940 | if (attributes->core.bits != slot->attr.bits) { |
| 1941 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1942 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1943 | } |
| 1944 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1945 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1946 | } |
| 1947 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1948 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
| 1949 | const uint8_t *data, |
| 1950 | size_t data_length, |
| 1951 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1952 | { |
| 1953 | psa_status_t status; |
| 1954 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1955 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1956 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1957 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1958 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1959 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1960 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1961 | * This also rejects any key which might be encoded as an empty string, |
| 1962 | * which is never valid. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1963 | if (data_length == 0) { |
| 1964 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1965 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1966 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1967 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 1968 | &slot, &driver); |
| 1969 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1970 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1971 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1972 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1973 | /* In the case of a transparent key or an opaque key stored in local |
| 1974 | * storage (thus not in the case of generating a key in a secure element |
| 1975 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 1976 | * hold the generated key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1977 | if (slot->key.data == NULL) { |
| 1978 | status = psa_allocate_buffer_to_slot(slot, data_length); |
| 1979 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1980 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1981 | } |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1982 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1983 | |
| 1984 | bits = slot->attr.bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1985 | status = psa_driver_wrapper_import_key(attributes, |
| 1986 | data, data_length, |
| 1987 | slot->key.data, |
| 1988 | slot->key.bytes, |
| 1989 | &slot->key.bytes, &bits); |
| 1990 | if (status != PSA_SUCCESS) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1991 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1992 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1993 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1994 | if (slot->attr.bits == 0) { |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1995 | slot->attr.bits = (psa_key_bits_t) bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1996 | } else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1997 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1998 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1999 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 2000 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2001 | status = psa_validate_optional_attributes(slot, attributes); |
| 2002 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 2003 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2004 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2005 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2006 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2007 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2008 | if (status != PSA_SUCCESS) { |
| 2009 | psa_fail_key_creation(slot, driver); |
| 2010 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2011 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2012 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 2013 | } |
| 2014 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2015 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2016 | psa_status_t mbedtls_psa_register_se_key( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2017 | const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2018 | { |
| 2019 | psa_status_t status; |
| 2020 | psa_key_slot_t *slot = NULL; |
| 2021 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2022 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2023 | |
| 2024 | /* Leaving attributes unspecified is not currently supported. |
| 2025 | * It could make sense to query the key type and size from the |
| 2026 | * secure element, but not all secure elements support this |
| 2027 | * and the driver HAL doesn't currently support it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2028 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 2029 | return PSA_ERROR_NOT_SUPPORTED; |
| 2030 | } |
| 2031 | if (psa_get_key_bits(attributes) == 0) { |
| 2032 | return PSA_ERROR_NOT_SUPPORTED; |
| 2033 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2034 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2035 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 2036 | &slot, &driver); |
| 2037 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2038 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2039 | } |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2040 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2041 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2042 | |
| 2043 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2044 | if (status != PSA_SUCCESS) { |
| 2045 | psa_fail_key_creation(slot, driver); |
| 2046 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2047 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2048 | /* Registration doesn't keep the key in RAM. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2049 | psa_close_key(key); |
| 2050 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 2051 | } |
| 2052 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2053 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2054 | static psa_status_t psa_copy_key_material(const psa_key_slot_t *source, |
| 2055 | psa_key_slot_t *target) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2056 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2057 | psa_status_t status = psa_copy_key_material_into_slot(target, |
| 2058 | source->key.data, |
| 2059 | source->key.bytes); |
| 2060 | if (status != PSA_SUCCESS) { |
| 2061 | return status; |
| 2062 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2063 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 2064 | target->attr.type = source->attr.type; |
| 2065 | target->attr.bits = source->attr.bits; |
| 2066 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2067 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2068 | } |
| 2069 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2070 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2071 | const psa_key_attributes_t *specified_attributes, |
| 2072 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2073 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2074 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2075 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2076 | psa_key_slot_t *source_slot = NULL; |
| 2077 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2078 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 2079 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2080 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 2081 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2082 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2083 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2084 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2085 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2086 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2087 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2088 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2089 | status = psa_validate_optional_attributes(source_slot, |
| 2090 | specified_attributes); |
| 2091 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2092 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2093 | } |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2094 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2095 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2096 | &actual_attributes.core.policy, |
| 2097 | &source_slot->attr.policy); |
| 2098 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2099 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2100 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2101 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2102 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2103 | &target_slot, &driver); |
| 2104 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2105 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2106 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2107 | |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2108 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2109 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2110 | /* Copying to a secure element is not implemented yet. */ |
| 2111 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2112 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2113 | } |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 2114 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2115 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2116 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 2117 | /* |
| 2118 | * Copying through an opaque driver is not implemented yet, consider |
| 2119 | * a lifetime with an external location as an invalid parameter for |
| 2120 | * now. |
| 2121 | */ |
| 2122 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2123 | goto exit; |
| 2124 | } |
| 2125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2126 | status = psa_copy_key_material(source_slot, target_slot); |
| 2127 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 2128 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2129 | } |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2130 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2131 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2132 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2133 | if (status != PSA_SUCCESS) { |
| 2134 | psa_fail_key_creation(target_slot, driver); |
| 2135 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2136 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2137 | unlock_status = psa_unlock_key_slot(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2138 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2139 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 2142 | |
| 2143 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2144 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2145 | /* Message digests */ |
| 2146 | /****************************************************************/ |
| 2147 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2148 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2149 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2150 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2151 | if (operation->id == 0) { |
| 2152 | return PSA_SUCCESS; |
| 2153 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2154 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2155 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2156 | operation->id = 0; |
| 2157 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2158 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2159 | } |
| 2160 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2161 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2162 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2163 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2164 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2165 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2166 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2167 | if (operation->id != 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2168 | status = PSA_ERROR_BAD_STATE; |
| 2169 | goto exit; |
| 2170 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2172 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2173 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2174 | goto exit; |
| 2175 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2176 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 2177 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 2178 | * directly zeroes the int-sized dummy member of the context union. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2179 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 2180 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2181 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2182 | |
| 2183 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2184 | if (status != PSA_SUCCESS) { |
| 2185 | psa_hash_abort(operation); |
| 2186 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2187 | |
| 2188 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2189 | } |
| 2190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2191 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 2192 | const uint8_t *input, |
| 2193 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2194 | { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2195 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2196 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2197 | if (operation->id == 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2198 | status = PSA_ERROR_BAD_STATE; |
| 2199 | goto exit; |
| 2200 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2201 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2202 | /* Don't require hash implementations to behave correctly on a |
| 2203 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2204 | if (input_length == 0) { |
| 2205 | return PSA_SUCCESS; |
| 2206 | } |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2207 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2208 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2209 | |
| 2210 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2211 | if (status != PSA_SUCCESS) { |
| 2212 | psa_hash_abort(operation); |
| 2213 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2214 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2215 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2216 | } |
| 2217 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2218 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2219 | uint8_t *hash, |
| 2220 | size_t hash_size, |
| 2221 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2222 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2223 | *hash_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2224 | if (operation->id == 0) { |
| 2225 | return PSA_ERROR_BAD_STATE; |
| 2226 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2227 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2228 | psa_status_t status = psa_driver_wrapper_hash_finish( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2229 | operation, hash, hash_size, hash_length); |
| 2230 | psa_hash_abort(operation); |
| 2231 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2232 | } |
| 2233 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2234 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 2235 | const uint8_t *hash, |
| 2236 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2237 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2238 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2239 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2240 | psa_status_t status = psa_hash_finish( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2241 | operation, |
| 2242 | actual_hash, sizeof(actual_hash), |
| 2243 | &actual_hash_length); |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2244 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2245 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2246 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2247 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2248 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2249 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2250 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2251 | goto exit; |
| 2252 | } |
| 2253 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2254 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2255 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2256 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2257 | |
| 2258 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2259 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2260 | if (status != PSA_SUCCESS) { |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2261 | psa_hash_abort(operation); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2262 | } |
Dave Rodgman | 4e0a82e | 2021-06-24 11:52:23 +0100 | [diff] [blame] | 2263 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2264 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2265 | } |
| 2266 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2267 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 2268 | const uint8_t *input, size_t input_length, |
| 2269 | uint8_t *hash, size_t hash_size, |
| 2270 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2271 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2272 | *hash_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2273 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2274 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2275 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2276 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2277 | return psa_driver_wrapper_hash_compute(alg, input, input_length, |
| 2278 | hash, hash_size, hash_length); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2279 | } |
| 2280 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2281 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 2282 | const uint8_t *input, size_t input_length, |
| 2283 | const uint8_t *hash, size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2284 | { |
Ronald Cron | 3a95d2b | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 2285 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2286 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2287 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2288 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2289 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2290 | } |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2291 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2292 | psa_status_t status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2293 | alg, input, input_length, |
| 2294 | actual_hash, sizeof(actual_hash), |
| 2295 | &actual_hash_length); |
| 2296 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2297 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2298 | } |
| 2299 | if (actual_hash_length != hash_length) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2300 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2301 | goto exit; |
| 2302 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2303 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2304 | status = PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2305 | } |
Gilles Peskine | b3f4e5b | 2021-12-13 12:33:18 +0100 | [diff] [blame] | 2306 | |
| 2307 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2308 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2309 | return status; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2312 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2313 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2314 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2315 | if (source_operation->id == 0 || |
| 2316 | target_operation->id != 0) { |
| 2317 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2318 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2319 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2320 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2321 | target_operation); |
| 2322 | if (status != PSA_SUCCESS) { |
| 2323 | psa_hash_abort(target_operation); |
| 2324 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2325 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2326 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2327 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2328 | |
| 2329 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2330 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2331 | /* MAC */ |
| 2332 | /****************************************************************/ |
| 2333 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2334 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2335 | { |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2336 | /* Aborting a non-active operation is allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2337 | if (operation->id == 0) { |
| 2338 | return PSA_SUCCESS; |
| 2339 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2340 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2341 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2342 | operation->mac_size = 0; |
| 2343 | operation->is_sign = 0; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2344 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2345 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2346 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2347 | } |
| 2348 | |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2349 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2350 | psa_algorithm_t alg, |
Ronald Cron | def68e7 | 2021-06-17 16:46:44 +0200 | [diff] [blame] | 2351 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2352 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2353 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2354 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2355 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2356 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2357 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2358 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2359 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2360 | } |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2361 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2362 | /* Validate the combination of key type and algorithm */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2363 | status = psa_mac_key_can_do(alg, key_type); |
| 2364 | if (status != PSA_SUCCESS) { |
| 2365 | return status; |
| 2366 | } |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2367 | |
Steven Cooreman | a6474de | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2368 | /* Get the output length for the algorithm and key combination */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2369 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2370 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2371 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2372 | /* A very short MAC is too short for security since it can be |
| 2373 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2374 | * so we make this our minimum, even though 32 bits is still |
| 2375 | * too small for security. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2376 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2377 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2378 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2379 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2380 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2381 | /* It's impossible to "truncate" to a larger length than the full length |
| 2382 | * of the algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2383 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2384 | } |
| 2385 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2386 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
Gilles Peskine | bc79582 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2387 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2388 | * that is disabled in the compile-time configuration. The result can |
| 2389 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2390 | * configuration into account. In this case, force a return of |
| 2391 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2392 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2393 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2394 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2395 | * systematically generated tests. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2396 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | bc79582 | 2022-03-16 13:54:49 +0100 | [diff] [blame] | 2397 | } |
| 2398 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2399 | return PSA_SUCCESS; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2400 | } |
| 2401 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2402 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2403 | mbedtls_svc_key_id_t key, |
| 2404 | psa_algorithm_t alg, |
| 2405 | int is_sign) |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2406 | { |
| 2407 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2408 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2409 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2410 | |
| 2411 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2412 | if (operation->id != 0) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2413 | status = PSA_ERROR_BAD_STATE; |
| 2414 | goto exit; |
| 2415 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2416 | |
| 2417 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2418 | key, |
| 2419 | &slot, |
| 2420 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2421 | alg); |
| 2422 | if (status != PSA_SUCCESS) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 2423 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2424 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2425 | |
| 2426 | psa_key_attributes_t attributes = { |
| 2427 | .core = slot->attr |
| 2428 | }; |
| 2429 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2430 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2431 | &operation->mac_size); |
| 2432 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2433 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2434 | } |
Ronald Cron | 1c650a1 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2435 | |
| 2436 | operation->is_sign = is_sign; |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2437 | /* Dispatch the MAC setup call with validated input */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2438 | if (is_sign) { |
| 2439 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2440 | &attributes, |
| 2441 | slot->key.data, |
| 2442 | slot->key.bytes, |
| 2443 | alg); |
| 2444 | } else { |
| 2445 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2446 | &attributes, |
| 2447 | slot->key.data, |
| 2448 | slot->key.bytes, |
| 2449 | alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2450 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2451 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2452 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2453 | if (status != PSA_SUCCESS) { |
| 2454 | psa_mac_abort(operation); |
| 2455 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2456 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2457 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2458 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2459 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2460 | } |
| 2461 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2462 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2463 | mbedtls_svc_key_id_t key, |
| 2464 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2465 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2466 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2467 | } |
| 2468 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2469 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2470 | mbedtls_svc_key_id_t key, |
| 2471 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2472 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2473 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2476 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 2477 | const uint8_t *input, |
| 2478 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2479 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2480 | if (operation->id == 0) { |
| 2481 | return PSA_ERROR_BAD_STATE; |
| 2482 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2483 | |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2484 | /* Don't require hash implementations to behave correctly on a |
| 2485 | * zero-length input, which may have an invalid pointer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2486 | if (input_length == 0) { |
| 2487 | return PSA_SUCCESS; |
| 2488 | } |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2489 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2490 | psa_status_t status = psa_driver_wrapper_mac_update(operation, |
| 2491 | input, input_length); |
| 2492 | if (status != PSA_SUCCESS) { |
| 2493 | psa_mac_abort(operation); |
| 2494 | } |
Steven Cooreman | 11743f9 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2495 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2496 | return status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2497 | } |
| 2498 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2499 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 2500 | uint8_t *mac, |
| 2501 | size_t mac_size, |
| 2502 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2503 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2504 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2505 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2506 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2507 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2508 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2509 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2510 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2511 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2512 | if (!operation->is_sign) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2513 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2514 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2515 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2516 | |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2517 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2518 | * once all the error checks are done. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2519 | if (operation->mac_size == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2520 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2521 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2522 | } |
Steven Cooreman | f8ad212 | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2523 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2524 | if (mac_size < operation->mac_size) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2525 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2526 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2527 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2528 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2529 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2530 | mac, operation->mac_size, |
| 2531 | mac_length); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2532 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2533 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2534 | /* In case of success, set the potential excess room in the output buffer |
| 2535 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2536 | * In case of error, set the output length and content to a safe default, |
| 2537 | * such that in case the caller misses an error check, the output would be |
| 2538 | * an unachievable MAC. |
| 2539 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2540 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2541 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2542 | operation->mac_size = 0; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2543 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2544 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2545 | if (mac_size > operation->mac_size) { |
| 2546 | memset(&mac[operation->mac_size], '!', |
| 2547 | mac_size - operation->mac_size); |
| 2548 | } |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2549 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2550 | abort_status = psa_mac_abort(operation); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2551 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2552 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2553 | } |
| 2554 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2555 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2556 | const uint8_t *mac, |
| 2557 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2558 | { |
Steven Cooreman | 87885df | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2559 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2560 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2561 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2562 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2563 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2564 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2565 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2566 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2567 | if (operation->is_sign) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2568 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2569 | goto exit; |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2570 | } |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2571 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2572 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2573 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2574 | goto exit; |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2575 | } |
| 2576 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2577 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2578 | mac, mac_length); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2579 | |
Dave Rodgman | 478ab54 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2580 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2581 | abort_status = psa_mac_abort(operation); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2582 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2583 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2584 | } |
| 2585 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2586 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2587 | psa_algorithm_t alg, |
| 2588 | const uint8_t *input, |
| 2589 | size_t input_length, |
| 2590 | uint8_t *mac, |
| 2591 | size_t mac_size, |
| 2592 | size_t *mac_length, |
| 2593 | int is_sign) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2594 | { |
| 2595 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2596 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2597 | psa_key_slot_t *slot; |
| 2598 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2599 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2600 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2601 | key, |
| 2602 | &slot, |
| 2603 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2604 | alg); |
| 2605 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2606 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2607 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2608 | |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2609 | psa_key_attributes_t attributes = { |
| 2610 | .core = slot->attr |
| 2611 | }; |
| 2612 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2613 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2614 | &operation_mac_size); |
| 2615 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2616 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2617 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2618 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2619 | if (mac_size < operation_mac_size) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2620 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2621 | goto exit; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | status = psa_driver_wrapper_mac_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2625 | &attributes, |
| 2626 | slot->key.data, slot->key.bytes, |
| 2627 | alg, |
| 2628 | input, input_length, |
| 2629 | mac, operation_mac_size, mac_length); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2630 | |
| 2631 | exit: |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2632 | /* In case of success, set the potential excess room in the output buffer |
| 2633 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2634 | * In case of error, set the output length and content to a safe default, |
| 2635 | * such that in case the caller misses an error check, the output would be |
| 2636 | * an unachievable MAC. |
| 2637 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2638 | if (status != PSA_SUCCESS) { |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2639 | *mac_length = mac_size; |
Ronald Cron | 882eb78 | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2640 | operation_mac_size = 0; |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2641 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2642 | if (mac_size > operation_mac_size) { |
| 2643 | memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size); |
| 2644 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2646 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | dbb8646 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2647 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2648 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2649 | } |
| 2650 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2651 | psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key, |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2652 | psa_algorithm_t alg, |
| 2653 | const uint8_t *input, |
| 2654 | size_t input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2655 | uint8_t *mac, |
| 2656 | size_t mac_size, |
| 2657 | size_t *mac_length) |
| 2658 | { |
| 2659 | return psa_mac_compute_internal(key, alg, |
| 2660 | input, input_length, |
| 2661 | mac, mac_size, mac_length, 1); |
| 2662 | } |
| 2663 | |
| 2664 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2665 | psa_algorithm_t alg, |
| 2666 | const uint8_t *input, |
| 2667 | size_t input_length, |
| 2668 | const uint8_t *mac, |
| 2669 | size_t mac_length) |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2670 | { |
| 2671 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2672 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2673 | size_t actual_mac_length; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2674 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2675 | status = psa_mac_compute_internal(key, alg, |
| 2676 | input, input_length, |
| 2677 | actual_mac, sizeof(actual_mac), |
| 2678 | &actual_mac_length, 0); |
| 2679 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2680 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2681 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2682 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2683 | if (mac_length != actual_mac_length) { |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2684 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2685 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2686 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2687 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2688 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2689 | goto exit; |
Ronald Cron | 4ed8368 | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2690 | } |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2691 | |
| 2692 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2693 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2694 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2695 | return status; |
gabor-mezei-arm | a00616f | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2696 | } |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2697 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2698 | /****************************************************************/ |
| 2699 | /* Asymmetric cryptography */ |
| 2700 | /****************************************************************/ |
| 2701 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2702 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2703 | psa_algorithm_t alg) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2704 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2705 | if (input_is_message) { |
| 2706 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 2707 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2708 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2709 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2710 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2711 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) { |
| 2712 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2713 | } |
| 2714 | } |
| 2715 | } else { |
| 2716 | if (!PSA_ALG_IS_SIGN_HASH(alg)) { |
| 2717 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2718 | } |
| 2719 | } |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2720 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2721 | return PSA_SUCCESS; |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2722 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2723 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2724 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 2725 | int input_is_message, |
| 2726 | psa_algorithm_t alg, |
| 2727 | const uint8_t *input, |
| 2728 | size_t input_length, |
| 2729 | uint8_t *signature, |
| 2730 | size_t signature_size, |
| 2731 | size_t *signature_length) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2732 | { |
| 2733 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2734 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2735 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2736 | |
| 2737 | *signature_length = 0; |
| 2738 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2739 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2740 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2741 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2742 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2743 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2744 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12ff4d5 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2745 | * that signature must be a valid pointer. (On the other hand, the input |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2746 | * buffer can in principle be empty since it doesn't actually have |
| 2747 | * to be a hash.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2748 | if (signature_size == 0) { |
| 2749 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 2750 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2751 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2752 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2753 | key, &slot, |
| 2754 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 2755 | PSA_KEY_USAGE_SIGN_HASH, |
| 2756 | alg); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2757 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2758 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2759 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2760 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2761 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2762 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2763 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2764 | goto exit; |
| 2765 | } |
| 2766 | |
| 2767 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2768 | .core = slot->attr |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2769 | }; |
| 2770 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2771 | if (input_is_message) { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2772 | status = psa_driver_wrapper_sign_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2773 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2774 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2775 | signature, signature_size, signature_length); |
| 2776 | } else { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2777 | |
| 2778 | status = psa_driver_wrapper_sign_hash( |
| 2779 | &attributes, slot->key.data, slot->key.bytes, |
| 2780 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2781 | signature, signature_size, signature_length); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2782 | } |
| 2783 | |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2784 | |
| 2785 | exit: |
| 2786 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2787 | * the trailing part on success) with something that isn't a valid signature |
| 2788 | * (barring an attack on the signature and deliberately-crafted input), |
| 2789 | * in case the caller doesn't check the return status properly. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2790 | if (status == PSA_SUCCESS) { |
| 2791 | memset(signature + *signature_length, '!', |
| 2792 | signature_size - *signature_length); |
| 2793 | } else { |
| 2794 | memset(signature, '!', signature_size); |
| 2795 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2796 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2797 | * memset because signature may be NULL in this case. */ |
| 2798 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2799 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2800 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2801 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2802 | } |
| 2803 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2804 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 2805 | int input_is_message, |
| 2806 | psa_algorithm_t alg, |
| 2807 | const uint8_t *input, |
| 2808 | size_t input_length, |
| 2809 | const uint8_t *signature, |
| 2810 | size_t signature_length) |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2811 | { |
| 2812 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2813 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2814 | psa_key_slot_t *slot; |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2815 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2816 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2817 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2818 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2819 | } |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2820 | |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2821 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2822 | key, &slot, |
| 2823 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2824 | PSA_KEY_USAGE_VERIFY_HASH, |
| 2825 | alg); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2826 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2827 | if (status != PSA_SUCCESS) { |
| 2828 | return status; |
| 2829 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2830 | |
| 2831 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2832 | .core = slot->attr |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2833 | }; |
| 2834 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2835 | if (input_is_message) { |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2836 | status = psa_driver_wrapper_verify_message( |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2837 | &attributes, slot->key.data, slot->key.bytes, |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2838 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2839 | signature, signature_length); |
| 2840 | } else { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2841 | status = psa_driver_wrapper_verify_hash( |
| 2842 | &attributes, slot->key.data, slot->key.bytes, |
| 2843 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2844 | signature, signature_length); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2845 | } |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2846 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2847 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2848 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2849 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2850 | |
| 2851 | } |
| 2852 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2853 | psa_status_t psa_sign_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2854 | const psa_key_attributes_t *attributes, |
| 2855 | const uint8_t *key_buffer, |
| 2856 | size_t key_buffer_size, |
| 2857 | psa_algorithm_t alg, |
| 2858 | const uint8_t *input, |
| 2859 | size_t input_length, |
| 2860 | uint8_t *signature, |
| 2861 | size_t signature_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2862 | size_t *signature_length) |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2863 | { |
| 2864 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2865 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2866 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2867 | size_t hash_length; |
| 2868 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2869 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2870 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2871 | PSA_ALG_SIGN_GET_HASH(alg), |
| 2872 | input, input_length, |
| 2873 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2874 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2875 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2876 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2877 | } |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2878 | |
| 2879 | return psa_driver_wrapper_sign_hash( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2880 | attributes, key_buffer, key_buffer_size, |
| 2881 | alg, hash, hash_length, |
| 2882 | signature, signature_size, signature_length); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2883 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2884 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2885 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2886 | } |
| 2887 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2888 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 2889 | psa_algorithm_t alg, |
| 2890 | const uint8_t *input, |
| 2891 | size_t input_length, |
| 2892 | uint8_t *signature, |
| 2893 | size_t signature_size, |
| 2894 | size_t *signature_length) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2895 | { |
| 2896 | return psa_sign_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2897 | key, 1, alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2898 | signature, signature_size, signature_length); |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2901 | psa_status_t psa_verify_message_builtin( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2902 | const psa_key_attributes_t *attributes, |
| 2903 | const uint8_t *key_buffer, |
| 2904 | size_t key_buffer_size, |
| 2905 | psa_algorithm_t alg, |
| 2906 | const uint8_t *input, |
| 2907 | size_t input_length, |
| 2908 | const uint8_t *signature, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2909 | size_t signature_length) |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2910 | { |
| 2911 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2912 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2913 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2914 | size_t hash_length; |
| 2915 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2916 | |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2917 | status = psa_driver_wrapper_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2918 | PSA_ALG_SIGN_GET_HASH(alg), |
| 2919 | input, input_length, |
| 2920 | hash, sizeof(hash), &hash_length); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2921 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2922 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2923 | return status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2924 | } |
gabor-mezei-arm | 9719a84 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2925 | |
| 2926 | return psa_driver_wrapper_verify_hash( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2927 | attributes, key_buffer, key_buffer_size, |
| 2928 | alg, hash, hash_length, |
| 2929 | signature, signature_length); |
gabor-mezei-arm | bfbe465 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2930 | } |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2931 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2932 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2933 | } |
| 2934 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2935 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 2936 | psa_algorithm_t alg, |
| 2937 | const uint8_t *input, |
| 2938 | size_t input_length, |
| 2939 | const uint8_t *signature, |
| 2940 | size_t signature_length) |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2941 | { |
| 2942 | return psa_verify_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2943 | key, 1, alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2944 | signature, signature_length); |
gabor-mezei-arm | e8efa39 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2945 | } |
| 2946 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 2947 | psa_status_t psa_sign_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2948 | const psa_key_attributes_t *attributes, |
| 2949 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 2950 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2951 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2952 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2953 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 2954 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 2955 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2956 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2957 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2958 | return mbedtls_psa_rsa_sign_hash( |
| 2959 | attributes, |
| 2960 | key_buffer, key_buffer_size, |
| 2961 | alg, hash, hash_length, |
| 2962 | signature, signature_size, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2963 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 2964 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2965 | } else { |
| 2966 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2967 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2968 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 2969 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2970 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2971 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2972 | return mbedtls_psa_ecdsa_sign_hash( |
| 2973 | attributes, |
| 2974 | key_buffer, key_buffer_size, |
| 2975 | alg, hash, hash_length, |
| 2976 | signature, signature_size, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2977 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 2978 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2979 | } else { |
| 2980 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2981 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2982 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2983 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2984 | (void) key_buffer; |
| 2985 | (void) key_buffer_size; |
| 2986 | (void) hash; |
| 2987 | (void) hash_length; |
| 2988 | (void) signature; |
| 2989 | (void) signature_size; |
| 2990 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2991 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2992 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2993 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2994 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2995 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 2996 | psa_algorithm_t alg, |
| 2997 | const uint8_t *hash, |
| 2998 | size_t hash_length, |
| 2999 | uint8_t *signature, |
| 3000 | size_t signature_size, |
| 3001 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3002 | { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3003 | return psa_sign_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3004 | key, 0, alg, hash, hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3005 | signature, signature_size, signature_length); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3006 | } |
| 3007 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 3008 | psa_status_t psa_verify_hash_builtin( |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3009 | const psa_key_attributes_t *attributes, |
| 3010 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3011 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3012 | const uint8_t *signature, size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3013 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3014 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 3015 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3016 | PSA_ALG_IS_RSA_PSS(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3017 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3018 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3019 | return mbedtls_psa_rsa_verify_hash( |
| 3020 | attributes, |
| 3021 | key_buffer, key_buffer_size, |
| 3022 | alg, hash, hash_length, |
| 3023 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3024 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3025 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3026 | } else { |
| 3027 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 3028 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3029 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 3030 | if (PSA_ALG_IS_ECDSA(alg)) { |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3031 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3032 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3033 | return mbedtls_psa_ecdsa_verify_hash( |
| 3034 | attributes, |
| 3035 | key_buffer, key_buffer_size, |
| 3036 | alg, hash, hash_length, |
| 3037 | signature, signature_length); |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3038 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3039 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3040 | } else { |
| 3041 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3042 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 3043 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3044 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3045 | (void) key_buffer; |
| 3046 | (void) key_buffer_size; |
| 3047 | (void) hash; |
| 3048 | (void) hash_length; |
| 3049 | (void) signature; |
| 3050 | (void) signature_length; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 3051 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3052 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 3053 | } |
| 3054 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3055 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3056 | psa_algorithm_t alg, |
| 3057 | const uint8_t *hash, |
| 3058 | size_t hash_length, |
| 3059 | const uint8_t *signature, |
| 3060 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3061 | { |
gabor-mezei-arm | bc0088b | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 3062 | return psa_verify_internal( |
gabor-mezei-arm | 5698048 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 3063 | key, 0, alg, hash, hash_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3064 | signature, signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3065 | } |
| 3066 | |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3067 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3068 | static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg, |
| 3069 | mbedtls_rsa_context *rsa) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3070 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3071 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg); |
| 3072 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg); |
| 3073 | mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info); |
| 3074 | mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg); |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3075 | } |
John Durkop | 0e00519 | 2020-10-31 22:06:54 -0700 | [diff] [blame] | 3076 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3077 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3078 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3079 | psa_algorithm_t alg, |
| 3080 | const uint8_t *input, |
| 3081 | size_t input_length, |
| 3082 | const uint8_t *salt, |
| 3083 | size_t salt_length, |
| 3084 | uint8_t *output, |
| 3085 | size_t output_size, |
| 3086 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3087 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3088 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3089 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3090 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3091 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3092 | (void) input; |
| 3093 | (void) input_length; |
| 3094 | (void) salt; |
| 3095 | (void) output; |
| 3096 | (void) output_size; |
| 3097 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3098 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3099 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3100 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3101 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3102 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3103 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3104 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3105 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3106 | if (status != PSA_SUCCESS) { |
| 3107 | return status; |
| 3108 | } |
| 3109 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3110 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3111 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3112 | goto exit; |
| 3113 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3114 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3115 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3116 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3117 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3118 | mbedtls_rsa_context *rsa = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3119 | status = mbedtls_psa_rsa_load_representation(slot->attr.type, |
| 3120 | slot->key.data, |
| 3121 | slot->key.bytes, |
| 3122 | &rsa); |
| 3123 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3124 | goto rsa_exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3125 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 3126 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3127 | if (output_size < mbedtls_rsa_get_len(rsa)) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3128 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3129 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3130 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3131 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3132 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3133 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3134 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3135 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3136 | mbedtls_rsa_pkcs1_encrypt(rsa, |
| 3137 | mbedtls_psa_get_random, |
| 3138 | MBEDTLS_PSA_RANDOM_STATE, |
| 3139 | MBEDTLS_RSA_PUBLIC, |
| 3140 | input_length, |
| 3141 | input, |
| 3142 | output)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3143 | #else |
| 3144 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3145 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3146 | } else |
| 3147 | if (PSA_ALG_IS_RSA_OAEP(alg)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3148 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3149 | psa_rsa_oaep_set_padding_mode(alg, rsa); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3150 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3151 | mbedtls_rsa_rsaes_oaep_encrypt(rsa, |
| 3152 | mbedtls_psa_get_random, |
| 3153 | MBEDTLS_PSA_RANDOM_STATE, |
| 3154 | MBEDTLS_RSA_PUBLIC, |
| 3155 | salt, salt_length, |
| 3156 | input_length, |
| 3157 | input, |
| 3158 | output)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3159 | #else |
| 3160 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3161 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3162 | } else { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3163 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3164 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3165 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3166 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3167 | rsa_exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3168 | if (status == PSA_SUCCESS) { |
| 3169 | *output_length = mbedtls_rsa_get_len(rsa); |
| 3170 | } |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3172 | mbedtls_rsa_free(rsa); |
| 3173 | mbedtls_free(rsa); |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 3174 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3175 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3176 | } else { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3177 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3178 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3179 | |
| 3180 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3181 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3182 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3183 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3184 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3185 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3186 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3187 | psa_algorithm_t alg, |
| 3188 | const uint8_t *input, |
| 3189 | size_t input_length, |
| 3190 | const uint8_t *salt, |
| 3191 | size_t salt_length, |
| 3192 | uint8_t *output, |
| 3193 | size_t output_size, |
| 3194 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3195 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3196 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3197 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 3198 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 3199 | |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 3200 | (void) input; |
| 3201 | (void) input_length; |
| 3202 | (void) salt; |
| 3203 | (void) output; |
| 3204 | (void) output_size; |
| 3205 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 3206 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3207 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3208 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3209 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3210 | } |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 3211 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 3212 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3213 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3214 | if (status != PSA_SUCCESS) { |
| 3215 | return status; |
| 3216 | } |
| 3217 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3218 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3219 | goto exit; |
| 3220 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3221 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3222 | if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3223 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3224 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3225 | mbedtls_rsa_context *rsa = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3226 | status = mbedtls_psa_rsa_load_representation(slot->attr.type, |
| 3227 | slot->key.data, |
| 3228 | slot->key.bytes, |
| 3229 | &rsa); |
| 3230 | if (status != PSA_SUCCESS) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3231 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3232 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3233 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3234 | if (input_length != mbedtls_rsa_get_len(rsa)) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3235 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3236 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 3237 | } |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3238 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3239 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3240 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3241 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3242 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3243 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3244 | mbedtls_rsa_pkcs1_decrypt(rsa, |
| 3245 | mbedtls_psa_get_random, |
| 3246 | MBEDTLS_PSA_RANDOM_STATE, |
| 3247 | MBEDTLS_RSA_PRIVATE, |
| 3248 | output_length, |
| 3249 | input, |
| 3250 | output, |
| 3251 | output_size)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3252 | #else |
| 3253 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3254 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3255 | } else |
| 3256 | if (PSA_ALG_IS_RSA_OAEP(alg)) { |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3257 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3258 | psa_rsa_oaep_set_padding_mode(alg, rsa); |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3259 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3260 | mbedtls_rsa_rsaes_oaep_decrypt(rsa, |
| 3261 | mbedtls_psa_get_random, |
| 3262 | MBEDTLS_PSA_RANDOM_STATE, |
| 3263 | MBEDTLS_RSA_PRIVATE, |
| 3264 | salt, salt_length, |
| 3265 | output_length, |
| 3266 | input, |
| 3267 | output, |
| 3268 | output_size)); |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3269 | #else |
| 3270 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3271 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3272 | } else { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3273 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3274 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 3275 | |
Ronald Cron | 7207d57 | 2021-09-08 14:28:35 +0200 | [diff] [blame] | 3276 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3277 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 3278 | rsa_exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3279 | mbedtls_rsa_free(rsa); |
| 3280 | mbedtls_free(rsa); |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 3281 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
| 3282 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3283 | } else { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3284 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3285 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3286 | |
| 3287 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3288 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3289 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3290 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3291 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3292 | |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3293 | |
| 3294 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3295 | /****************************************************************/ |
| 3296 | /* Symmetric cryptography */ |
| 3297 | /****************************************************************/ |
| 3298 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3299 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 3300 | mbedtls_svc_key_id_t key, |
| 3301 | psa_algorithm_t alg, |
| 3302 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3303 | { |
| 3304 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3305 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3306 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3307 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 3308 | PSA_KEY_USAGE_ENCRYPT : |
| 3309 | PSA_KEY_USAGE_DECRYPT); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3310 | |
| 3311 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3312 | if (operation->id != 0) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3313 | status = PSA_ERROR_BAD_STATE; |
| 3314 | goto exit; |
| 3315 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3316 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3317 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | cccb05d | 2021-06-24 11:52:47 +0100 | [diff] [blame] | 3318 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3319 | goto exit; |
| 3320 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3321 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3322 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 3323 | if (status != PSA_SUCCESS) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3324 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3325 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3326 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3327 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3328 | * 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] | 3329 | * so we only set it (in the driver wrapper) after resources have been |
| 3330 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3331 | operation->iv_set = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3332 | if (alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3333 | operation->iv_required = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3334 | } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) { |
Gilles Peskine | 4a83c10 | 2022-04-20 17:00:22 +0200 | [diff] [blame] | 3335 | operation->iv_required = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3336 | } else { |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3337 | operation->iv_required = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3338 | } |
| 3339 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3340 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3341 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3342 | .core = slot->attr |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3343 | }; |
| 3344 | |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3345 | /* Try doing the operation through a driver before using software fallback. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3346 | if (cipher_operation == MBEDTLS_ENCRYPT) { |
| 3347 | status = psa_driver_wrapper_cipher_encrypt_setup(operation, |
| 3348 | &attributes, |
| 3349 | slot->key.data, |
| 3350 | slot->key.bytes, |
| 3351 | alg); |
| 3352 | } else { |
| 3353 | status = psa_driver_wrapper_cipher_decrypt_setup(operation, |
| 3354 | &attributes, |
| 3355 | slot->key.data, |
| 3356 | slot->key.bytes, |
| 3357 | alg); |
| 3358 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3359 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3360 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3361 | if (status != PSA_SUCCESS) { |
| 3362 | psa_cipher_abort(operation); |
| 3363 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3365 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3366 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3367 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3368 | } |
| 3369 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3370 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 3371 | mbedtls_svc_key_id_t key, |
| 3372 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3373 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3374 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3375 | } |
| 3376 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3377 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 3378 | mbedtls_svc_key_id_t key, |
| 3379 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3380 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3381 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3382 | } |
| 3383 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3384 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 3385 | uint8_t *iv, |
| 3386 | size_t iv_size, |
| 3387 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3388 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3389 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3390 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3391 | size_t default_iv_length; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3392 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3393 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3394 | status = PSA_ERROR_BAD_STATE; |
| 3395 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3396 | } |
| 3397 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3398 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3399 | status = PSA_ERROR_BAD_STATE; |
| 3400 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3401 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3402 | |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3403 | default_iv_length = operation->default_iv_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3404 | if (iv_size < default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3405 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3406 | goto exit; |
| 3407 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3408 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3409 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3410 | status = PSA_ERROR_GENERIC_ERROR; |
| 3411 | goto exit; |
| 3412 | } |
| 3413 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3414 | status = psa_generate_random(local_iv, default_iv_length); |
| 3415 | if (status != PSA_SUCCESS) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3416 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3417 | } |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3418 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3419 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 3420 | local_iv, default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3421 | |
| 3422 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3423 | if (status == PSA_SUCCESS) { |
| 3424 | memcpy(iv, local_iv, default_iv_length); |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3425 | *iv_length = default_iv_length; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3426 | operation->iv_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3427 | } else { |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3428 | *iv_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3429 | psa_cipher_abort(operation); |
Ronald Cron | c423acb | 2021-07-05 12:31:44 +0200 | [diff] [blame] | 3430 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3431 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3432 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3433 | } |
| 3434 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3435 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 3436 | const uint8_t *iv, |
| 3437 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3438 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3439 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3440 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3441 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3442 | status = PSA_ERROR_BAD_STATE; |
| 3443 | goto exit; |
| 3444 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3445 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3446 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3447 | status = PSA_ERROR_BAD_STATE; |
| 3448 | goto exit; |
| 3449 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3450 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3451 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3452 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3453 | goto exit; |
| 3454 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3455 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3456 | status = psa_driver_wrapper_cipher_set_iv(operation, |
| 3457 | iv, |
| 3458 | iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3459 | |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3460 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3461 | if (status == PSA_SUCCESS) { |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3462 | operation->iv_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3463 | } else { |
| 3464 | psa_cipher_abort(operation); |
| 3465 | } |
| 3466 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3467 | } |
| 3468 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3469 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 3470 | const uint8_t *input, |
| 3471 | size_t input_length, |
| 3472 | uint8_t *output, |
| 3473 | size_t output_size, |
| 3474 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3475 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3476 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3477 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3478 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3479 | status = PSA_ERROR_BAD_STATE; |
| 3480 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3481 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3482 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3483 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3484 | status = PSA_ERROR_BAD_STATE; |
| 3485 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3486 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3487 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3488 | status = psa_driver_wrapper_cipher_update(operation, |
| 3489 | input, |
| 3490 | input_length, |
| 3491 | output, |
| 3492 | output_size, |
| 3493 | output_length); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3494 | |
| 3495 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3496 | if (status != PSA_SUCCESS) { |
| 3497 | psa_cipher_abort(operation); |
| 3498 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3499 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3500 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3501 | } |
| 3502 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3503 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
| 3504 | uint8_t *output, |
| 3505 | size_t output_size, |
| 3506 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3507 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3508 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3509 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3510 | if (operation->id == 0) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3511 | status = PSA_ERROR_BAD_STATE; |
| 3512 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3513 | } |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3514 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3515 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3516 | status = PSA_ERROR_BAD_STATE; |
| 3517 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3518 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3519 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3520 | status = psa_driver_wrapper_cipher_finish(operation, |
| 3521 | output, |
| 3522 | output_size, |
| 3523 | output_length); |
Dave Rodgman | c88b0a5 | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3524 | |
| 3525 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3526 | if (status == PSA_SUCCESS) { |
| 3527 | return psa_cipher_abort(operation); |
| 3528 | } else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3529 | *output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3530 | (void) psa_cipher_abort(operation); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3531 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3532 | return status; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3533 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3536 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3537 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3538 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3539 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3540 | * in use. It's ok to call abort on such an object, and there's |
| 3541 | * nothing to do. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3542 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3543 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3544 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3545 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3546 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3547 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3548 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3549 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3550 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3551 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3552 | } |
| 3553 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3554 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 3555 | psa_algorithm_t alg, |
| 3556 | const uint8_t *input, |
| 3557 | size_t input_length, |
| 3558 | uint8_t *output, |
| 3559 | size_t output_size, |
| 3560 | size_t *output_length) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3561 | { |
| 3562 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3563 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3564 | psa_key_slot_t *slot = NULL; |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3565 | uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE]; |
| 3566 | size_t default_iv_length = 0; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3567 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3568 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3569 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3570 | goto exit; |
| 3571 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3572 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3573 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3574 | PSA_KEY_USAGE_ENCRYPT, |
| 3575 | alg); |
| 3576 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3577 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3578 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3579 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3580 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3581 | .core = slot->attr |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3582 | }; |
| 3583 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3584 | default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
| 3585 | if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3586 | status = PSA_ERROR_GENERIC_ERROR; |
| 3587 | goto exit; |
| 3588 | } |
| 3589 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3590 | if (default_iv_length > 0) { |
| 3591 | if (output_size < default_iv_length) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3592 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3593 | goto exit; |
| 3594 | } |
| 3595 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3596 | status = psa_generate_random(local_iv, default_iv_length); |
| 3597 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3598 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3599 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3600 | } |
| 3601 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3602 | status = psa_driver_wrapper_cipher_encrypt( |
| 3603 | &attributes, slot->key.data, slot->key.bytes, |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3604 | alg, local_iv, default_iv_length, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3605 | mbedtls_buffer_offset(output, default_iv_length), |
| 3606 | output_size - default_iv_length, output_length); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3607 | |
| 3608 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3609 | unlock_status = psa_unlock_key_slot(slot); |
| 3610 | if (status == PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3611 | status = unlock_status; |
Ronald Cron | 4c224fe | 2021-07-09 18:44:58 +0200 | [diff] [blame] | 3612 | } |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3613 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3614 | if (status == PSA_SUCCESS) { |
| 3615 | if (default_iv_length > 0) { |
| 3616 | memcpy(output, local_iv, default_iv_length); |
| 3617 | } |
| 3618 | *output_length += default_iv_length; |
| 3619 | } else { |
| 3620 | *output_length = 0; |
| 3621 | } |
| 3622 | |
| 3623 | return status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3624 | } |
| 3625 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3626 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 3627 | psa_algorithm_t alg, |
| 3628 | const uint8_t *input, |
| 3629 | size_t input_length, |
| 3630 | uint8_t *output, |
| 3631 | size_t output_size, |
| 3632 | size_t *output_length) |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3633 | { |
| 3634 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3635 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3636 | psa_key_slot_t *slot = NULL; |
gabor-mezei-arm | 52ae871 | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3637 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3638 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3639 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3640 | goto exit; |
| 3641 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3642 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3643 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3644 | PSA_KEY_USAGE_DECRYPT, |
| 3645 | alg); |
| 3646 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3647 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3648 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3649 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3650 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3651 | .core = slot->attr |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3652 | }; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3653 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3654 | if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) { |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3655 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3656 | goto exit; |
| 3657 | } |
| 3658 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3659 | status = psa_driver_wrapper_cipher_decrypt( |
| 3660 | &attributes, slot->key.data, slot->key.bytes, |
| 3661 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3662 | output, output_size, output_length); |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3663 | |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3664 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3665 | unlock_status = psa_unlock_key_slot(slot); |
| 3666 | if (status == PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3667 | status = unlock_status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3668 | } |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3669 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3670 | if (status != PSA_SUCCESS) { |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3671 | *output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3672 | } |
Ronald Cron | 1637707 | 2021-07-08 19:00:07 +0200 | [diff] [blame] | 3673 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3674 | return status; |
gabor-mezei-arm | 0ef3b85 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3678 | /****************************************************************/ |
| 3679 | /* AEAD */ |
| 3680 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3681 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3682 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 3683 | psa_algorithm_t alg, |
| 3684 | const uint8_t *nonce, |
| 3685 | size_t nonce_length, |
| 3686 | const uint8_t *additional_data, |
| 3687 | size_t additional_data_length, |
| 3688 | const uint8_t *plaintext, |
| 3689 | size_t plaintext_length, |
| 3690 | uint8_t *ciphertext, |
| 3691 | size_t ciphertext_size, |
| 3692 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3693 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3694 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3695 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3696 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3697 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3698 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3699 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 3700 | return PSA_ERROR_NOT_SUPPORTED; |
| 3701 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3702 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3703 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3704 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3705 | if (status != PSA_SUCCESS) { |
| 3706 | return status; |
| 3707 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3708 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3709 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3710 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3711 | }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3712 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3713 | status = psa_driver_wrapper_aead_encrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3714 | &attributes, slot->key.data, slot->key.bytes, |
| 3715 | alg, |
| 3716 | nonce, nonce_length, |
| 3717 | additional_data, additional_data_length, |
| 3718 | plaintext, plaintext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3719 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3720 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3721 | if (status != PSA_SUCCESS && ciphertext_size != 0) { |
| 3722 | memset(ciphertext, 0, ciphertext_size); |
| 3723 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3724 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3725 | psa_unlock_key_slot(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3726 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3727 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3728 | } |
| 3729 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3730 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 3731 | psa_algorithm_t alg, |
| 3732 | const uint8_t *nonce, |
| 3733 | size_t nonce_length, |
| 3734 | const uint8_t *additional_data, |
| 3735 | size_t additional_data_length, |
| 3736 | const uint8_t *ciphertext, |
| 3737 | size_t ciphertext_length, |
| 3738 | uint8_t *plaintext, |
| 3739 | size_t plaintext_size, |
| 3740 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3741 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3742 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3743 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3744 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3745 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3746 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3747 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) { |
| 3748 | return PSA_ERROR_NOT_SUPPORTED; |
| 3749 | } |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3750 | |
Ronald Cron | 9a98616 | 2021-03-26 12:40:07 +0100 | [diff] [blame] | 3751 | status = psa_get_and_lock_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3752 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3753 | if (status != PSA_SUCCESS) { |
| 3754 | return status; |
| 3755 | } |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3756 | |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3757 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3758 | .core = slot->attr |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3759 | }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3760 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3761 | status = psa_driver_wrapper_aead_decrypt( |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3762 | &attributes, slot->key.data, slot->key.bytes, |
| 3763 | alg, |
| 3764 | nonce, nonce_length, |
| 3765 | additional_data, additional_data_length, |
| 3766 | ciphertext, ciphertext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3767 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3768 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3769 | if (status != PSA_SUCCESS && plaintext_size != 0) { |
| 3770 | memset(plaintext, 0, plaintext_size); |
| 3771 | } |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3772 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3773 | psa_unlock_key_slot(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3774 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3775 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3776 | } |
| 3777 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3778 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3779 | /* Generators */ |
| 3780 | /****************************************************************/ |
| 3781 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3782 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3783 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3784 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3785 | #define AT_LEAST_ONE_BUILTIN_KDF |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3786 | #endif /* At least one builtin KDF */ |
| 3787 | |
| 3788 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3789 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3790 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3791 | static psa_status_t psa_key_derivation_start_hmac( |
| 3792 | psa_mac_operation_t *operation, |
| 3793 | psa_algorithm_t hash_alg, |
| 3794 | const uint8_t *hmac_key, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3795 | size_t hmac_key_length) |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3796 | { |
| 3797 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3798 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3799 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 3800 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 3801 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3802 | |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 3803 | operation->is_sign = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3804 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 15f0d92 | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 3805 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3806 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 3807 | &attributes, |
| 3808 | hmac_key, hmac_key_length, |
| 3809 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3810 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3811 | psa_reset_key_attributes(&attributes); |
| 3812 | return status; |
Steven Cooreman | 9878a16 | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3813 | } |
| 3814 | #endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3815 | |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3816 | #define HKDF_STATE_INIT 0 /* no input yet */ |
| 3817 | #define HKDF_STATE_STARTED 1 /* got salt */ |
| 3818 | #define HKDF_STATE_KEYED 2 /* got key */ |
| 3819 | #define HKDF_STATE_OUTPUT 3 /* output started */ |
| 3820 | |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 3821 | static psa_algorithm_t psa_key_derivation_get_kdf_alg( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3822 | const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3823 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3824 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 3825 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
| 3826 | } else { |
| 3827 | return operation->alg; |
| 3828 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3829 | } |
| 3830 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3831 | 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] | 3832 | { |
| 3833 | psa_status_t status = PSA_SUCCESS; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3834 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 3835 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3836 | /* The object has (apparently) been initialized but it is not |
| 3837 | * in use. It's ok to call abort on such an object, and there's |
| 3838 | * nothing to do. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3839 | } else |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3840 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3841 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 3842 | mbedtls_free(operation->ctx.hkdf.info); |
| 3843 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 3844 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3845 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 3846 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3847 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3848 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 3849 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 3850 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 3851 | if (operation->ctx.tls12_prf.secret != NULL) { |
| 3852 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret, |
| 3853 | operation->ctx.tls12_prf.secret_length); |
| 3854 | mbedtls_free(operation->ctx.tls12_prf.secret); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3855 | } |
| 3856 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3857 | if (operation->ctx.tls12_prf.seed != NULL) { |
| 3858 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed, |
| 3859 | operation->ctx.tls12_prf.seed_length); |
| 3860 | mbedtls_free(operation->ctx.tls12_prf.seed); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3861 | } |
| 3862 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3863 | if (operation->ctx.tls12_prf.label != NULL) { |
| 3864 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.label, |
| 3865 | operation->ctx.tls12_prf.label_length); |
| 3866 | mbedtls_free(operation->ctx.tls12_prf.label); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3867 | } |
| 3868 | |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3869 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3870 | |
| 3871 | /* We leave the fields Ai and output_block to be erased safely by the |
| 3872 | * mbedtls_platform_zeroize() in the end of this function. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3873 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3874 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 3875 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3876 | { |
| 3877 | status = PSA_ERROR_BAD_STATE; |
| 3878 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3879 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 3880 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3883 | psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3884 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3885 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3886 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3887 | /* This is a blank key derivation operation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3888 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3891 | *capacity = operation->capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3892 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3893 | } |
| 3894 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3895 | psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 3896 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3897 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3898 | if (operation->alg == 0) { |
| 3899 | return PSA_ERROR_BAD_STATE; |
| 3900 | } |
| 3901 | if (capacity > operation->capacity) { |
| 3902 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3903 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3904 | operation->capacity = capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3905 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3906 | } |
| 3907 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 3908 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3909 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3910 | * of the expand phase of the HKDF algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3911 | static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 3912 | psa_algorithm_t hash_alg, |
| 3913 | uint8_t *output, |
| 3914 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3915 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3916 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3917 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3918 | psa_status_t status; |
| 3919 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3920 | if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) { |
| 3921 | return PSA_ERROR_BAD_STATE; |
| 3922 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3923 | hkdf->state = HKDF_STATE_OUTPUT; |
| 3924 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3925 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3926 | /* Copy what remains of the current block */ |
| 3927 | uint8_t n = hash_length - hkdf->offset_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3928 | if (n > output_length) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3929 | n = (uint8_t) output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3930 | } |
| 3931 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3932 | output += n; |
| 3933 | output_length -= n; |
| 3934 | hkdf->offset_in_block += n; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3935 | if (output_length == 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3936 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3937 | } |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3938 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 3939 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3940 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3941 | * object was corrupted or if this function is called directly |
| 3942 | * inside the library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3943 | if (hkdf->block_number == 0xff) { |
| 3944 | return PSA_ERROR_BAD_STATE; |
| 3945 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3946 | |
| 3947 | /* We need a new block */ |
| 3948 | ++hkdf->block_number; |
| 3949 | hkdf->offset_in_block = 0; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3950 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3951 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 3952 | hash_alg, |
| 3953 | hkdf->prk, |
| 3954 | hash_length); |
| 3955 | if (status != PSA_SUCCESS) { |
| 3956 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3957 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3958 | |
| 3959 | if (hkdf->block_number != 1) { |
| 3960 | status = psa_mac_update(&hkdf->hmac, |
| 3961 | hkdf->output_block, |
| 3962 | hash_length); |
| 3963 | if (status != PSA_SUCCESS) { |
| 3964 | return status; |
| 3965 | } |
| 3966 | } |
| 3967 | status = psa_mac_update(&hkdf->hmac, |
| 3968 | hkdf->info, |
| 3969 | hkdf->info_length); |
| 3970 | if (status != PSA_SUCCESS) { |
| 3971 | return status; |
| 3972 | } |
| 3973 | status = psa_mac_update(&hkdf->hmac, |
| 3974 | &hkdf->block_number, 1); |
| 3975 | if (status != PSA_SUCCESS) { |
| 3976 | return status; |
| 3977 | } |
| 3978 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 3979 | hkdf->output_block, |
| 3980 | sizeof(hkdf->output_block), |
| 3981 | &hmac_output_length); |
| 3982 | if (status != PSA_SUCCESS) { |
| 3983 | return status; |
| 3984 | } |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3985 | } |
| 3986 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3987 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3988 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3989 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3990 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3991 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3992 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3993 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 3994 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3995 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3996 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3997 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 3998 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3999 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 4000 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4001 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4002 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4003 | /* We can't be wanting more output after block 0xff, otherwise |
| 4004 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 4005 | * prevented this call. It could happen only if the operation |
| 4006 | * object was corrupted or if this function is called directly |
| 4007 | * inside the library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4008 | if (tls12_prf->block_number == 0xff) { |
| 4009 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 4010 | } |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4011 | |
| 4012 | /* We need a new block */ |
| 4013 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4014 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4015 | |
| 4016 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 4017 | * |
| 4018 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 4019 | * |
| 4020 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 4021 | * HMAC_hash(secret, A(2) + seed) + |
| 4022 | * HMAC_hash(secret, A(3) + seed) + ... |
| 4023 | * |
| 4024 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4025 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4026 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4027 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4028 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 4029 | * is currently extracted as `output_block` and where i is |
| 4030 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4031 | */ |
| 4032 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4033 | status = psa_key_derivation_start_hmac(&hmac, |
| 4034 | hash_alg, |
| 4035 | tls12_prf->secret, |
| 4036 | tls12_prf->secret_length); |
| 4037 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4038 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4039 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4040 | |
| 4041 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4042 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4043 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 4044 | * the variable seed and in this instance means it in the context of the |
| 4045 | * P_hash function, where seed = label + seed.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4046 | status = psa_mac_update(&hmac, |
| 4047 | tls12_prf->label, |
| 4048 | tls12_prf->label_length); |
| 4049 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4050 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4051 | } |
| 4052 | status = psa_mac_update(&hmac, |
| 4053 | tls12_prf->seed, |
| 4054 | tls12_prf->seed_length); |
| 4055 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4056 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4057 | } |
| 4058 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4059 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4060 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 4061 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4062 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4063 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4064 | } |
| 4065 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4066 | status = psa_mac_sign_finish(&hmac, |
| 4067 | tls12_prf->Ai, hash_length, |
| 4068 | &hmac_output_length); |
| 4069 | if (hmac_output_length != hash_length) { |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4070 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4071 | } |
| 4072 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4073 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4074 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4075 | |
| 4076 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4077 | status = psa_key_derivation_start_hmac(&hmac, |
| 4078 | hash_alg, |
| 4079 | tls12_prf->secret, |
| 4080 | tls12_prf->secret_length); |
| 4081 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4082 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4083 | } |
| 4084 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 4085 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4086 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4087 | } |
| 4088 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 4089 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4090 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4091 | } |
| 4092 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 4093 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4094 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4095 | } |
| 4096 | status = psa_mac_sign_finish(&hmac, |
| 4097 | tls12_prf->output_block, hash_length, |
| 4098 | &hmac_output_length); |
| 4099 | if (status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4100 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4101 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4102 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4103 | |
| 4104 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4105 | cleanup_status = psa_mac_abort(&hmac); |
| 4106 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4107 | status = cleanup_status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4108 | } |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 4109 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4110 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 4111 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 4112 | |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4113 | static psa_status_t psa_key_derivation_tls12_prf_read( |
| 4114 | psa_tls12_prf_key_derivation_t *tls12_prf, |
| 4115 | psa_algorithm_t alg, |
| 4116 | uint8_t *output, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4117 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4118 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4119 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 4120 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4121 | psa_status_t status; |
| 4122 | uint8_t offset, length; |
| 4123 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4124 | switch (tls12_prf->state) { |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4125 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 4126 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 4127 | break; |
| 4128 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 4129 | break; |
| 4130 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4131 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | f216f0d | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 4132 | } |
| 4133 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4134 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4135 | /* Check if we have fully processed the current block. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4136 | if (tls12_prf->left_in_block == 0) { |
| 4137 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 4138 | alg); |
| 4139 | if (status != PSA_SUCCESS) { |
| 4140 | return status; |
| 4141 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4142 | |
| 4143 | continue; |
| 4144 | } |
| 4145 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4146 | if (tls12_prf->left_in_block > output_length) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4147 | length = (uint8_t) output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4148 | } else { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4149 | length = tls12_prf->left_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4150 | } |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4151 | |
| 4152 | offset = hash_length - tls12_prf->left_in_block; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4153 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4154 | output += length; |
| 4155 | output_length -= length; |
| 4156 | tls12_prf->left_in_block -= length; |
| 4157 | } |
| 4158 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4159 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 4160 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4161 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4162 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 4163 | |
Janos Follath | 6c6c8fc | 2019-06-17 12:38:20 +0100 | [diff] [blame] | 4164 | psa_status_t psa_key_derivation_output_bytes( |
| 4165 | psa_key_derivation_operation_t *operation, |
| 4166 | uint8_t *output, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4167 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4168 | { |
| 4169 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4170 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4172 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4173 | /* This is a blank operation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4174 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4177 | if (output_length > operation->capacity) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4178 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4179 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4180 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4181 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4182 | goto exit; |
| 4183 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4184 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4185 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4186 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4187 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 4188 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4189 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4190 | * output_length > 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4191 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4192 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4193 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4194 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4195 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4196 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4197 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 4198 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg, |
| 4199 | output, output_length); |
| 4200 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4201 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4202 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4203 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4204 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 4205 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4206 | status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf, |
| 4207 | kdf_alg, output, |
| 4208 | output_length); |
| 4209 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4210 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || |
| 4211 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4212 | { |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4213 | (void) kdf_alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4214 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4218 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4219 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4220 | * This allows us to differentiate between exhausted operations and |
| 4221 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 4222 | * operations. */ |
| 4223 | psa_algorithm_t alg = operation->alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4224 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4225 | operation->alg = alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4226 | memset(output, '!', output_length); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4227 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4228 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4229 | } |
| 4230 | |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4231 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4232 | 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] | 4233 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4234 | if (data_size >= 8) { |
| 4235 | mbedtls_des_key_set_parity(data); |
| 4236 | } |
| 4237 | if (data_size >= 16) { |
| 4238 | mbedtls_des_key_set_parity(data + 8); |
| 4239 | } |
| 4240 | if (data_size >= 24) { |
| 4241 | mbedtls_des_key_set_parity(data + 16); |
| 4242 | } |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4243 | } |
David Brown | 7807bf7 | 2021-02-09 16:28:23 -0700 | [diff] [blame] | 4244 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 4245 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 4246 | static psa_status_t psa_generate_derived_key_internal( |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4247 | psa_key_slot_t *slot, |
| 4248 | size_t bits, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4249 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4250 | { |
| 4251 | uint8_t *data = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4252 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4253 | psa_status_t status; |
| 4254 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4255 | if (!key_type_is_raw_bytes(slot->attr.type)) { |
| 4256 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4257 | } |
| 4258 | if (bits % 8 != 0) { |
| 4259 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4260 | } |
| 4261 | data = mbedtls_calloc(1, bytes); |
| 4262 | if (data == NULL) { |
| 4263 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4264 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4265 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4266 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 4267 | if (status != PSA_SUCCESS) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4268 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4269 | } |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 4270 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4271 | if (slot->attr.type == PSA_KEY_TYPE_DES) { |
| 4272 | psa_des_set_key_parity(data, bytes); |
| 4273 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 4274 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4275 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4276 | status = psa_allocate_buffer_to_slot(slot, bytes); |
| 4277 | if (status != PSA_SUCCESS) { |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 4278 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4279 | } |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 4280 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4281 | slot->attr.bits = (psa_key_bits_t) bits; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4282 | psa_key_attributes_t attributes = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4283 | .core = slot->attr |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 4284 | }; |
| 4285 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4286 | status = psa_driver_wrapper_import_key(&attributes, |
| 4287 | data, bytes, |
| 4288 | slot->key.data, |
| 4289 | slot->key.bytes, |
| 4290 | &slot->key.bytes, &bits); |
| 4291 | if (bits != slot->attr.bits) { |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 4292 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4293 | } |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4294 | |
| 4295 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4296 | mbedtls_free(data); |
| 4297 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4298 | } |
| 4299 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4300 | psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 4301 | psa_key_derivation_operation_t *operation, |
| 4302 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4303 | { |
| 4304 | psa_status_t status; |
| 4305 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4306 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4307 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4308 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4309 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4310 | /* Reject any attempt to create a zero-length key so that we don't |
| 4311 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4312 | if (psa_get_key_bits(attributes) == 0) { |
| 4313 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4314 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4315 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4316 | if (operation->alg == PSA_ALG_NONE) { |
| 4317 | return PSA_ERROR_BAD_STATE; |
| 4318 | } |
Dave Rodgman | 021e724 | 2021-11-16 10:32:48 +0000 | [diff] [blame] | 4319 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4320 | if (!operation->can_output_key) { |
| 4321 | return PSA_ERROR_NOT_PERMITTED; |
| 4322 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4323 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4324 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, |
| 4325 | &slot, &driver); |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4326 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4327 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 4328 | /* Deriving a key in a secure element is not implemented yet. */ |
| 4329 | status = PSA_ERROR_NOT_SUPPORTED; |
| 4330 | } |
| 4331 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4332 | if (status == PSA_SUCCESS) { |
| 4333 | status = psa_generate_derived_key_internal(slot, |
| 4334 | attributes->core.bits, |
| 4335 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4336 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4337 | if (status == PSA_SUCCESS) { |
| 4338 | status = psa_finish_key_creation(slot, driver, key); |
| 4339 | } |
| 4340 | if (status != PSA_SUCCESS) { |
| 4341 | psa_fail_key_creation(slot, driver); |
| 4342 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4343 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4344 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4345 | } |
| 4346 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 4347 | |
| 4348 | |
| 4349 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4350 | /* Key derivation */ |
| 4351 | /****************************************************************/ |
| 4352 | |
Steven Cooreman | 932ffb7 | 2021-02-15 12:14:32 +0100 | [diff] [blame] | 4353 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4354 | static int is_kdf_alg_supported(psa_algorithm_t kdf_alg) |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4355 | { |
| 4356 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4357 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4358 | return 1; |
| 4359 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4360 | #endif |
| 4361 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4362 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 4363 | return 1; |
| 4364 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4365 | #endif |
| 4366 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4367 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4368 | return 1; |
| 4369 | } |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4370 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4371 | return 0; |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4372 | } |
| 4373 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4374 | static psa_status_t psa_hash_try_support(psa_algorithm_t alg) |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4375 | { |
| 4376 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4377 | psa_status_t status = psa_hash_setup(&operation, alg); |
| 4378 | psa_hash_abort(&operation); |
| 4379 | return status; |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4380 | } |
| 4381 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4382 | static psa_status_t psa_key_derivation_setup_kdf( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4383 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4384 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4385 | { |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4386 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 4387 | * initialisers for this union leave some bytes unspecified.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4388 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 4389 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4390 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4391 | if (!is_kdf_alg_supported(kdf_alg)) { |
| 4392 | return PSA_ERROR_NOT_SUPPORTED; |
| 4393 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4394 | |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4395 | /* All currently supported key derivation algorithms are based on a |
| 4396 | * hash algorithm. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4397 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 4398 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 4399 | if (hash_size == 0) { |
| 4400 | return PSA_ERROR_NOT_SUPPORTED; |
| 4401 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4402 | |
| 4403 | /* Make sure that hash_alg is a supported hash algorithm. Otherwise |
| 4404 | * we might fail later, which is somewhat unfriendly and potentially |
| 4405 | * risk-prone. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4406 | psa_status_t status = psa_hash_try_support(hash_alg); |
| 4407 | if (status != PSA_SUCCESS) { |
| 4408 | return status; |
| 4409 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4410 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4411 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 4412 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && |
| 4413 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 4414 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4415 | } |
Gilles Peskine | ea0d95e | 2021-04-29 21:18:14 +0200 | [diff] [blame] | 4416 | |
Gilles Peskine | cdacf04 | 2021-04-29 21:10:00 +0200 | [diff] [blame] | 4417 | operation->capacity = 255 * hash_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4418 | return PSA_SUCCESS; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4419 | } |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4420 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4421 | static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg) |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4422 | { |
| 4423 | #if defined(PSA_WANT_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4424 | if (alg == PSA_ALG_ECDH) { |
| 4425 | return PSA_SUCCESS; |
| 4426 | } |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4427 | #endif |
| 4428 | (void) alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4429 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 739c98c | 2021-04-29 21:34:33 +0200 | [diff] [blame] | 4430 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4431 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4432 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4433 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 4434 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4435 | { |
| 4436 | psa_status_t status; |
| 4437 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4438 | if (operation->alg != 0) { |
| 4439 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4440 | } |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 4441 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4442 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) { |
| 4443 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4444 | } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 4445 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 4446 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 4447 | psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg); |
| 4448 | status = psa_key_agreement_try_support(ka_alg); |
| 4449 | if (status != PSA_SUCCESS) { |
| 4450 | return status; |
| 4451 | } |
| 4452 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 4453 | #else |
| 4454 | return PSA_ERROR_NOT_SUPPORTED; |
| 4455 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 4456 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 4457 | #if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 4458 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 4459 | #else |
| 4460 | return PSA_ERROR_NOT_SUPPORTED; |
| 4461 | #endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 4462 | } else { |
| 4463 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4464 | } |
| 4465 | |
| 4466 | if (status == PSA_SUCCESS) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4467 | operation->alg = alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4468 | } |
| 4469 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4470 | } |
| 4471 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4472 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4473 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 4474 | psa_algorithm_t hash_alg, |
| 4475 | psa_key_derivation_step_t step, |
| 4476 | const uint8_t *data, |
| 4477 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4478 | { |
| 4479 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4480 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4481 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4482 | if (hkdf->state != HKDF_STATE_INIT) { |
| 4483 | return PSA_ERROR_BAD_STATE; |
| 4484 | } else { |
| 4485 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4486 | hash_alg, |
| 4487 | data, data_length); |
| 4488 | if (status != PSA_SUCCESS) { |
| 4489 | return status; |
| 4490 | } |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4491 | hkdf->state = HKDF_STATE_STARTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4492 | return PSA_SUCCESS; |
Steven Cooreman | b27e350 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 4493 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4494 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4495 | /* If no salt was provided, use an empty salt. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4496 | if (hkdf->state == HKDF_STATE_INIT) { |
| 4497 | status = psa_key_derivation_start_hmac(&hkdf->hmac, |
| 4498 | hash_alg, |
| 4499 | NULL, 0); |
| 4500 | if (status != PSA_SUCCESS) { |
| 4501 | return status; |
| 4502 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4503 | hkdf->state = HKDF_STATE_STARTED; |
| 4504 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4505 | if (hkdf->state != HKDF_STATE_STARTED) { |
| 4506 | return PSA_ERROR_BAD_STATE; |
| 4507 | } |
| 4508 | status = psa_mac_update(&hkdf->hmac, |
| 4509 | data, data_length); |
| 4510 | if (status != PSA_SUCCESS) { |
| 4511 | return status; |
| 4512 | } |
| 4513 | status = psa_mac_sign_finish(&hkdf->hmac, |
| 4514 | hkdf->prk, |
| 4515 | sizeof(hkdf->prk), |
| 4516 | &data_length); |
| 4517 | if (status != PSA_SUCCESS) { |
| 4518 | return status; |
| 4519 | } |
| 4520 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 4521 | hkdf->block_number = 0; |
| 4522 | hkdf->state = HKDF_STATE_KEYED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4523 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4524 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4525 | if (hkdf->state == HKDF_STATE_OUTPUT) { |
| 4526 | return PSA_ERROR_BAD_STATE; |
| 4527 | } |
| 4528 | if (hkdf->info_set) { |
| 4529 | return PSA_ERROR_BAD_STATE; |
| 4530 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4531 | hkdf->info_length = data_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4532 | if (data_length != 0) { |
| 4533 | hkdf->info = mbedtls_calloc(1, data_length); |
| 4534 | if (hkdf->info == NULL) { |
| 4535 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4536 | } |
| 4537 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4538 | } |
| 4539 | hkdf->info_set = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4540 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4541 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4542 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4543 | } |
| 4544 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4545 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4546 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4547 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4548 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4549 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 4550 | const uint8_t *data, |
| 4551 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4552 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4553 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) { |
| 4554 | return PSA_ERROR_BAD_STATE; |
| 4555 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4556 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4557 | if (data_length != 0) { |
| 4558 | prf->seed = mbedtls_calloc(1, data_length); |
| 4559 | if (prf->seed == NULL) { |
| 4560 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4561 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4562 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4563 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4564 | prf->seed_length = data_length; |
| 4565 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4566 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4567 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4568 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4569 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4570 | } |
| 4571 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4572 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 4573 | const uint8_t *data, |
| 4574 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4575 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4576 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) { |
| 4577 | return PSA_ERROR_BAD_STATE; |
| 4578 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4579 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4580 | if (data_length != 0) { |
| 4581 | prf->secret = mbedtls_calloc(1, data_length); |
| 4582 | if (prf->secret == NULL) { |
| 4583 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4584 | } |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4585 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4586 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | 22dea1d | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4587 | prf->secret_length = data_length; |
| 4588 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4589 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4590 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4591 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4592 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4593 | } |
| 4594 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4595 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 4596 | const uint8_t *data, |
| 4597 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4598 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4599 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) { |
| 4600 | return PSA_ERROR_BAD_STATE; |
| 4601 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4602 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4603 | if (data_length != 0) { |
| 4604 | prf->label = mbedtls_calloc(1, data_length); |
| 4605 | if (prf->label == NULL) { |
| 4606 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4607 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4608 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4609 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4610 | prf->label_length = data_length; |
| 4611 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4612 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4613 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4614 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4615 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4616 | } |
| 4617 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4618 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 4619 | psa_key_derivation_step_t step, |
| 4620 | const uint8_t *data, |
| 4621 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4622 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4623 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4624 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4625 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4626 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4627 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4628 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4629 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4630 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4631 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4632 | } |
| 4633 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4634 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || |
| 4635 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
| 4636 | |
| 4637 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4638 | static psa_status_t psa_tls12_prf_psk_to_ms_set_key( |
| 4639 | psa_tls12_prf_key_derivation_t *prf, |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4640 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4641 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4642 | { |
| 4643 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4644 | uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE]; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4645 | uint8_t *cur = pms; |
| 4646 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4647 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) { |
| 4648 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4649 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4650 | |
| 4651 | /* Quoting RFC 4279, Section 2: |
| 4652 | * |
| 4653 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4654 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4655 | * uint16 with the value N, and the PSK itself. |
| 4656 | */ |
| 4657 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4658 | *cur++ = MBEDTLS_BYTE_1(data_length); |
| 4659 | *cur++ = MBEDTLS_BYTE_0(data_length); |
| 4660 | memset(cur, 0, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4661 | cur += data_length; |
| 4662 | *cur++ = pms[0]; |
| 4663 | *cur++ = pms[1]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4664 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4665 | cur += data_length; |
| 4666 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4667 | status = psa_tls12_prf_set_key(prf, pms, cur - pms); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4668 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4669 | mbedtls_platform_zeroize(pms, sizeof(pms)); |
| 4670 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4671 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4672 | |
| 4673 | static psa_status_t psa_tls12_prf_psk_to_ms_input( |
| 4674 | psa_tls12_prf_key_derivation_t *prf, |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4675 | psa_key_derivation_step_t step, |
| 4676 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4677 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4678 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4679 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 4680 | return psa_tls12_prf_psk_to_ms_set_key(prf, |
| 4681 | data, data_length); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4682 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4683 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4684 | return psa_tls12_prf_input(prf, step, data, data_length); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4685 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4686 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4687 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4688 | /** Check whether the given key type is acceptable for the given |
| 4689 | * input step of a key derivation. |
| 4690 | * |
| 4691 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 4692 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 4693 | * Both secret and non-secret inputs can alternatively have the type |
| 4694 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 4695 | * that the input was passed as a buffer rather than via a key object. |
| 4696 | */ |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4697 | static int psa_key_derivation_check_input_type( |
| 4698 | psa_key_derivation_step_t step, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4699 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4700 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4701 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4702 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4703 | if (key_type == PSA_KEY_TYPE_DERIVE) { |
| 4704 | return PSA_SUCCESS; |
| 4705 | } |
| 4706 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 4707 | return PSA_SUCCESS; |
| 4708 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4709 | break; |
| 4710 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4711 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 4712 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 4713 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4714 | if (key_type == PSA_KEY_TYPE_RAW_DATA) { |
| 4715 | return PSA_SUCCESS; |
| 4716 | } |
| 4717 | if (key_type == PSA_KEY_TYPE_NONE) { |
| 4718 | return PSA_SUCCESS; |
| 4719 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4720 | break; |
| 4721 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4722 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4723 | } |
| 4724 | |
Janos Follath | b80a94e | 2019-06-12 15:54:46 +0100 | [diff] [blame] | 4725 | static psa_status_t psa_key_derivation_input_internal( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4726 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4727 | psa_key_derivation_step_t step, |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4728 | psa_key_type_t key_type, |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4729 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4730 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4731 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 4732 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4733 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 4734 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4735 | status = psa_key_derivation_check_input_type(step, key_type); |
| 4736 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4737 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4738 | } |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4739 | |
John Durkop | d032195 | 2020-10-29 21:37:36 -0700 | [diff] [blame] | 4740 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4741 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4742 | status = psa_hkdf_input(&operation->ctx.hkdf, |
| 4743 | PSA_ALG_HKDF_GET_HASH(kdf_alg), |
| 4744 | step, data, data_length); |
| 4745 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4746 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4747 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4748 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 4749 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, |
| 4750 | step, data, data_length); |
| 4751 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4752 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 4753 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4754 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4755 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, |
| 4756 | step, data, data_length); |
| 4757 | } else |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4758 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4759 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4760 | /* This can't happen unless the operation object was not initialized */ |
Steven Cooreman | 74afe47 | 2021-02-10 17:19:22 +0100 | [diff] [blame] | 4761 | (void) data; |
| 4762 | (void) data_length; |
| 4763 | (void) kdf_alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4764 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4765 | } |
| 4766 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4767 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4768 | if (status != PSA_SUCCESS) { |
| 4769 | psa_key_derivation_abort(operation); |
| 4770 | } |
| 4771 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4772 | } |
| 4773 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4774 | psa_status_t psa_key_derivation_input_bytes( |
| 4775 | psa_key_derivation_operation_t *operation, |
| 4776 | psa_key_derivation_step_t step, |
| 4777 | const uint8_t *data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4778 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4779 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4780 | return psa_key_derivation_input_internal(operation, step, |
| 4781 | PSA_KEY_TYPE_NONE, |
| 4782 | data, data_length); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4783 | } |
| 4784 | |
Janos Follath | 51f4a0f | 2019-06-14 11:35:55 +0100 | [diff] [blame] | 4785 | psa_status_t psa_key_derivation_input_key( |
| 4786 | psa_key_derivation_operation_t *operation, |
| 4787 | psa_key_derivation_step_t step, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4788 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4789 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4790 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4791 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4792 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4793 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4794 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4795 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 4796 | if (status != PSA_SUCCESS) { |
| 4797 | psa_key_derivation_abort(operation); |
| 4798 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4799 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4800 | |
| 4801 | /* Passing a key object as a SECRET input unlocks the permission |
| 4802 | * to output to a key object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4803 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4804 | operation->can_output_key = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4805 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4806 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4807 | status = psa_key_derivation_input_internal(operation, |
| 4808 | step, slot->attr.type, |
| 4809 | slot->key.data, |
| 4810 | slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4811 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4812 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4813 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4814 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4815 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4816 | |
| 4817 | |
| 4818 | |
| 4819 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4820 | /* Key agreement */ |
| 4821 | /****************************************************************/ |
| 4822 | |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4823 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4824 | static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key, |
| 4825 | size_t peer_key_length, |
| 4826 | const mbedtls_ecp_keypair *our_key, |
| 4827 | uint8_t *shared_secret, |
| 4828 | size_t shared_secret_size, |
| 4829 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4830 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 4831 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4832 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4833 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4834 | size_t bits = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4835 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits); |
| 4836 | mbedtls_ecdh_init(&ecdh); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4837 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4838 | status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4839 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), |
| 4840 | bits, |
| 4841 | peer_key, |
| 4842 | peer_key_length, |
| 4843 | &their_key); |
| 4844 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4845 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4846 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4847 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4848 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4849 | mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS)); |
| 4850 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4851 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4852 | } |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4853 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4854 | mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS)); |
| 4855 | if (status != PSA_SUCCESS) { |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4856 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4857 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4858 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4859 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4860 | mbedtls_ecdh_calc_secret(&ecdh, |
| 4861 | shared_secret_length, |
| 4862 | shared_secret, shared_secret_size, |
| 4863 | mbedtls_psa_get_random, |
| 4864 | MBEDTLS_PSA_RANDOM_STATE)); |
| 4865 | if (status != PSA_SUCCESS) { |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4866 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4867 | } |
| 4868 | if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) { |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4869 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4870 | } |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4871 | |
| 4872 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4873 | if (status != PSA_SUCCESS) { |
| 4874 | mbedtls_platform_zeroize(shared_secret, shared_secret_size); |
| 4875 | } |
| 4876 | mbedtls_ecdh_free(&ecdh); |
| 4877 | mbedtls_ecp_keypair_free(their_key); |
| 4878 | mbedtls_free(their_key); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4879 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4880 | return status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4881 | } |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4882 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4883 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4884 | #define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
| 4885 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4886 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 4887 | psa_key_slot_t *private_key, |
| 4888 | const uint8_t *peer_key, |
| 4889 | size_t peer_key_length, |
| 4890 | uint8_t *shared_secret, |
| 4891 | size_t shared_secret_size, |
| 4892 | size_t *shared_secret_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4893 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4894 | switch (alg) { |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4895 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4896 | case PSA_ALG_ECDH: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4897 | if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) { |
| 4898 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4899 | } |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4900 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4901 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4902 | private_key->attr.type, |
| 4903 | private_key->attr.bits, |
| 4904 | private_key->key.data, |
| 4905 | private_key->key.bytes, |
| 4906 | &ecp); |
| 4907 | if (status != PSA_SUCCESS) { |
| 4908 | return status; |
| 4909 | } |
| 4910 | status = psa_key_agreement_ecdh(peer_key, peer_key_length, |
| 4911 | ecp, |
| 4912 | shared_secret, shared_secret_size, |
| 4913 | shared_secret_length); |
| 4914 | mbedtls_ecp_keypair_free(ecp); |
| 4915 | mbedtls_free(ecp); |
| 4916 | return status; |
John Durkop | 6ba40d1 | 2020-11-10 08:50:04 -0800 | [diff] [blame] | 4917 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4918 | default: |
Gilles Peskine | 93f8500 | 2018-11-16 16:43:31 +0100 | [diff] [blame] | 4919 | (void) private_key; |
| 4920 | (void) peer_key; |
| 4921 | (void) peer_key_length; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4922 | (void) shared_secret; |
| 4923 | (void) shared_secret_size; |
| 4924 | (void) shared_secret_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4925 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4926 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4927 | } |
| 4928 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4929 | /* 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] | 4930 | * to potentially free embedded data structures and wipe confidential data. |
| 4931 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4932 | static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 4933 | psa_key_derivation_step_t step, |
| 4934 | psa_key_slot_t *private_key, |
| 4935 | const uint8_t *peer_key, |
| 4936 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4937 | { |
| 4938 | psa_status_t status; |
| 4939 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 4940 | size_t shared_secret_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4941 | 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] | 4942 | |
| 4943 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 4944 | * secret. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4945 | status = psa_key_agreement_raw_internal(ka_alg, |
| 4946 | private_key, |
| 4947 | peer_key, peer_key_length, |
| 4948 | shared_secret, |
| 4949 | sizeof(shared_secret), |
| 4950 | &shared_secret_length); |
| 4951 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4952 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4953 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4954 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4955 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4956 | * the shared secret. A shared secret is permitted wherever a key |
| 4957 | * of type DERIVE is permitted. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4958 | status = psa_key_derivation_input_internal(operation, step, |
| 4959 | PSA_KEY_TYPE_DERIVE, |
| 4960 | shared_secret, |
| 4961 | shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4962 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4963 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 4964 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4965 | } |
| 4966 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4967 | psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 4968 | psa_key_derivation_step_t step, |
| 4969 | mbedtls_svc_key_id_t private_key, |
| 4970 | const uint8_t *peer_key, |
| 4971 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4972 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4973 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4974 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4975 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4976 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4977 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) { |
| 4978 | return PSA_ERROR_INVALID_ARGUMENT; |
| 4979 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4980 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4981 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 4982 | if (status != PSA_SUCCESS) { |
| 4983 | return status; |
| 4984 | } |
| 4985 | status = psa_key_agreement_internal(operation, step, |
| 4986 | slot, |
| 4987 | peer_key, peer_key_length); |
| 4988 | if (status != PSA_SUCCESS) { |
| 4989 | psa_key_derivation_abort(operation); |
| 4990 | } else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4991 | /* If a private key has been added as SECRET, we allow the derived |
| 4992 | * key material to be used as a key in PSA Crypto. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4993 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4994 | operation->can_output_key = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4995 | } |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4996 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4997 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4998 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4999 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5000 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 5001 | } |
| 5002 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5003 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 5004 | mbedtls_svc_key_id_t private_key, |
| 5005 | const uint8_t *peer_key, |
| 5006 | size_t peer_key_length, |
| 5007 | uint8_t *output, |
| 5008 | size_t output_size, |
| 5009 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5010 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5011 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5012 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5013 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5014 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5015 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5016 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 5017 | goto exit; |
| 5018 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 5019 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5020 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 5021 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5022 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5023 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5024 | |
Gilles Peskine | 42313fb | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5025 | /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound |
| 5026 | * for the output size. The PSA specification only guarantees that this |
| 5027 | * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...), |
| 5028 | * but it might be nice to allow smaller buffers if the output fits. |
| 5029 | * At the time of writing this comment, with only ECDH implemented, |
| 5030 | * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. |
| 5031 | * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily |
| 5032 | * be exact for it as well. */ |
| 5033 | size_t expected_length = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5034 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits); |
| 5035 | if (output_size < expected_length) { |
Gilles Peskine | 42313fb | 2022-04-14 00:17:15 +0200 | [diff] [blame] | 5036 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 5037 | goto exit; |
| 5038 | } |
| 5039 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5040 | status = psa_key_agreement_raw_internal(alg, slot, |
| 5041 | peer_key, peer_key_length, |
| 5042 | output, output_size, |
| 5043 | output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5044 | |
| 5045 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5046 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5047 | /* If an error happens and is not handled properly, the output |
| 5048 | * may be used as a key to protect sensitive data. Arrange for such |
| 5049 | * a key to be random, which is likely to result in decryption or |
| 5050 | * verification errors. This is better than filling the buffer with |
| 5051 | * some constant data such as zeros, which would result in the data |
| 5052 | * being protected with a reproducible, easily knowable key. |
| 5053 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5054 | psa_generate_random(output, output_size); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5055 | *output_length = output_size; |
| 5056 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5057 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5058 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5059 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5060 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 5061 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5062 | |
| 5063 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5064 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 5065 | /****************************************************************/ |
| 5066 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 5067 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5068 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5069 | /** Initialize the PSA random generator. |
| 5070 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5071 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5072 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5073 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5074 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5075 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5076 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5077 | /* Set default configuration if |
| 5078 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5079 | if (rng->entropy_init == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5080 | rng->entropy_init = mbedtls_entropy_init; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5081 | } |
| 5082 | if (rng->entropy_free == NULL) { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5083 | rng->entropy_free = mbedtls_entropy_free; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5084 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5085 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5086 | rng->entropy_init(&rng->entropy); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5087 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 5088 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 5089 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 5090 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5091 | mbedtls_entropy_add_source(&rng->entropy, |
| 5092 | mbedtls_nv_seed_poll, NULL, |
| 5093 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 5094 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5095 | #endif |
| 5096 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5097 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5098 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | /** Deinitialize the PSA random generator. |
| 5102 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5103 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5104 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5105 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5106 | memset(rng, 0, sizeof(*rng)); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5107 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5108 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 5109 | rng->entropy_free(&rng->entropy); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5110 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5111 | } |
| 5112 | |
| 5113 | /** Seed the PSA random generator. |
| 5114 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5115 | 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] | 5116 | { |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5117 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5118 | /* Do nothing: the external RNG seeds itself. */ |
| 5119 | (void) rng; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5120 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5121 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5122 | const unsigned char drbg_seed[] = "PSA"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5123 | int ret = mbedtls_psa_drbg_seed(&rng->entropy, |
| 5124 | drbg_seed, sizeof(drbg_seed) - 1); |
| 5125 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5126 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5127 | } |
| 5128 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5129 | psa_status_t psa_generate_random(uint8_t *output, |
| 5130 | size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5131 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5132 | GUARD_MODULE_INITIALIZED; |
| 5133 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5134 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5135 | |
| 5136 | size_t output_length = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5137 | psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng, |
| 5138 | output, output_size, |
| 5139 | &output_length); |
| 5140 | if (status != PSA_SUCCESS) { |
| 5141 | return status; |
| 5142 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5143 | /* Breaking up a request into smaller chunks is currently not supported |
Tom Cosgrove | 49f99bc | 2022-12-04 16:44:21 +0000 | [diff] [blame] | 5144 | * for the external RNG interface. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5145 | if (output_length != output_size) { |
| 5146 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 5147 | } |
| 5148 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5149 | |
| 5150 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5151 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5152 | while (output_size > 0) { |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5153 | size_t request_size = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5154 | (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 5155 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 5156 | output_size); |
| 5157 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 5158 | output, request_size); |
| 5159 | if (ret != 0) { |
| 5160 | return mbedtls_to_psa_error(ret); |
| 5161 | } |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 5162 | output_size -= request_size; |
| 5163 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 5164 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5165 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5166 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5167 | } |
| 5168 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5169 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5170 | * |
| 5171 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 5172 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 5173 | * PSA API doesn't support passing an explicit state. |
| 5174 | * |
| 5175 | * In the non-external case, psa_generate_random() calls an |
| 5176 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 5177 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 5178 | * instead of doing this back-and-forth between the PSA API and the |
| 5179 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 5180 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5181 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5182 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 5183 | int mbedtls_psa_get_random(void *p_rng, |
| 5184 | unsigned char *output, |
| 5185 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5186 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 5187 | /* This function takes a pointer to the RNG state because that's what |
| 5188 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 5189 | * its own state internally and doesn't let the caller access that state. |
| 5190 | * So we just ignore the state parameter, and in practice we'll pass |
| 5191 | * NULL. */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5192 | (void) p_rng; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5193 | psa_status_t status = psa_generate_random(output, output_size); |
| 5194 | if (status == PSA_SUCCESS) { |
| 5195 | return 0; |
| 5196 | } else { |
| 5197 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 5198 | } |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 5199 | } |
| 5200 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 5201 | |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5202 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 5203 | #include "mbedtls/entropy_poll.h" |
| 5204 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5205 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, |
| 5206 | size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5207 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5208 | if (global_data.initialized) { |
| 5209 | return PSA_ERROR_NOT_PERMITTED; |
| 5210 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5211 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5212 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 5213 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 5214 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) { |
| 5215 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5216 | } |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 5217 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5218 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5219 | } |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 5220 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 5221 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5222 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5223 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5224 | * \param type The key type |
| 5225 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5226 | * |
| 5227 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5228 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5229 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 5230 | * The size in bits of the key is not valid. |
| 5231 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 5232 | * The type and/or the size in bits of the key or the combination of |
| 5233 | * the two is not supported. |
| 5234 | */ |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5235 | static psa_status_t psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5236 | psa_key_type_t type, size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5237 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 5238 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5239 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5240 | if (key_type_is_raw_bytes(type)) { |
| 5241 | status = validate_unstructured_key_bit_size(type, bits); |
| 5242 | if (status != PSA_SUCCESS) { |
| 5243 | return status; |
| 5244 | } |
| 5245 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5246 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5247 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 5248 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) { |
| 5249 | return PSA_ERROR_NOT_SUPPORTED; |
| 5250 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5251 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5252 | /* Accept only byte-aligned keys, for the same reasons as |
| 5253 | * in psa_import_rsa_key(). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5254 | if (bits % 8 != 0) { |
| 5255 | return PSA_ERROR_NOT_SUPPORTED; |
| 5256 | } |
| 5257 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5258 | #endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5259 | |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5260 | #if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5261 | 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] | 5262 | /* To avoid empty block, return successfully here. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5263 | return PSA_SUCCESS; |
| 5264 | } else |
Ronald Cron | 5c4d386 | 2020-12-07 11:07:24 +0100 | [diff] [blame] | 5265 | #endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5266 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5267 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5268 | } |
| 5269 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5270 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5271 | } |
| 5272 | |
Ronald Cron | 55ed059 | 2020-10-05 10:30:40 +0200 | [diff] [blame] | 5273 | psa_status_t psa_generate_key_internal( |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5274 | const psa_key_attributes_t *attributes, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5275 | 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] | 5276 | { |
| 5277 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 5278 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5279 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5280 | if ((attributes->domain_parameters == NULL) && |
| 5281 | (attributes->domain_parameters_size != 0)) { |
| 5282 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5283 | } |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 5284 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5285 | if (key_type_is_raw_bytes(type)) { |
| 5286 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 5287 | if (status != PSA_SUCCESS) { |
| 5288 | return status; |
| 5289 | } |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 5290 | |
David Brown | 12ca503 | 2021-02-11 11:02:00 -0700 | [diff] [blame] | 5291 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5292 | if (type == PSA_KEY_TYPE_DES) { |
| 5293 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 5294 | } |
David Brown | 8107e31 | 2021-02-12 08:08:46 -0700 | [diff] [blame] | 5295 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5296 | } else |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5297 | |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5298 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 5299 | defined(MBEDTLS_GENPRIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5300 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 5301 | return mbedtls_psa_rsa_generate_key(attributes, |
| 5302 | key_buffer, |
| 5303 | key_buffer_size, |
| 5304 | key_buffer_length); |
| 5305 | } else |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 5306 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 5307 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5308 | |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5309 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5310 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 5311 | return mbedtls_psa_ecp_generate_key(attributes, |
| 5312 | key_buffer, |
| 5313 | key_buffer_size, |
| 5314 | key_buffer_length); |
| 5315 | } else |
John Durkop | 9814fa2 | 2020-11-04 12:28:15 -0800 | [diff] [blame] | 5316 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5317 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5318 | (void) key_buffer_length; |
| 5319 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 5320 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5321 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5322 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5323 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5325 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 5326 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5327 | { |
| 5328 | psa_status_t status; |
| 5329 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 5330 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5331 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5332 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 5333 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5334 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5335 | /* Reject any attempt to create a zero-length key so that we don't |
| 5336 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5337 | if (psa_get_key_bits(attributes) == 0) { |
| 5338 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5339 | } |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 5340 | |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5341 | /* Reject any attempt to create a public key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5342 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) { |
| 5343 | return PSA_ERROR_INVALID_ARGUMENT; |
| 5344 | } |
Przemyslaw Stekiel | 5b20a7e | 2021-10-07 11:08:56 +0200 | [diff] [blame] | 5345 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5346 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 5347 | &slot, &driver); |
| 5348 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5349 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5350 | } |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5351 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5352 | /* In the case of a transparent key or an opaque key stored in local |
| 5353 | * storage (thus not in the case of generating a key in a secure element |
| 5354 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 5355 | * hold the generated key material. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5356 | if (slot->key.data == NULL) { |
| 5357 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 5358 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5359 | status = psa_validate_key_type_and_size_for_key_generation( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5360 | attributes->core.type, attributes->core.bits); |
| 5361 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5362 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5363 | } |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5364 | |
| 5365 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5366 | attributes->core.type, |
| 5367 | attributes->core.bits); |
| 5368 | } else { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5369 | status = psa_driver_wrapper_get_key_buffer_size( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5370 | attributes, &key_buffer_size); |
| 5371 | if (status != PSA_SUCCESS) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 5372 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5373 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5374 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5375 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5376 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 5377 | if (status != PSA_SUCCESS) { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5378 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5379 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 5380 | } |
| 5381 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5382 | status = psa_driver_wrapper_generate_key(attributes, |
| 5383 | slot->key.data, slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5384 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5385 | if (status != PSA_SUCCESS) { |
| 5386 | psa_remove_key_data_from_memory(slot); |
| 5387 | } |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 5388 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 5389 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5390 | if (status == PSA_SUCCESS) { |
| 5391 | status = psa_finish_key_creation(slot, driver, key); |
| 5392 | } |
| 5393 | if (status != PSA_SUCCESS) { |
| 5394 | psa_fail_key_creation(slot, driver); |
| 5395 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 5396 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5397 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5398 | } |
| 5399 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5400 | /****************************************************************/ |
| 5401 | /* Module setup */ |
| 5402 | /****************************************************************/ |
| 5403 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5404 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5405 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5406 | void (* entropy_init)(mbedtls_entropy_context *ctx), |
| 5407 | void (* entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5408 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5409 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 5410 | return PSA_ERROR_BAD_STATE; |
| 5411 | } |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5412 | global_data.rng.entropy_init = entropy_init; |
| 5413 | global_data.rng.entropy_free = entropy_free; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5414 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5415 | } |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 5416 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 5417 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5418 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5419 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5420 | psa_wipe_all_key_slots(); |
| 5421 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 5422 | mbedtls_psa_random_free(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5423 | } |
| 5424 | /* Wipe all remaining data, including configuration. |
| 5425 | * In particular, this sets all state indicator to the value |
| 5426 | * indicating "uninitialized". */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5427 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5428 | |
| 5429 | /* Terminate drivers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5430 | psa_driver_wrapper_free(); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5431 | } |
| 5432 | |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5433 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 5434 | /** Recover a transaction that was interrupted by a power failure. |
| 5435 | * |
| 5436 | * This function is called during initialization, before psa_crypto_init() |
| 5437 | * returns. If this function returns a failure status, the initialization |
| 5438 | * fails. |
| 5439 | */ |
| 5440 | static psa_status_t psa_crypto_recover_transaction( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5441 | const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5442 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5443 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5444 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 5445 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5446 | /* TODO - fall through to the failure case until this |
| 5447 | * is implemented. |
| 5448 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 5449 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5450 | default: |
| 5451 | /* We found an unsupported transaction in the storage. |
| 5452 | * We don't know what state the storage is in. Give up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5453 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5454 | } |
| 5455 | } |
| 5456 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
| 5457 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5458 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5459 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5460 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5461 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5462 | /* Double initialization is explicitly allowed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5463 | if (global_data.initialized != 0) { |
| 5464 | return PSA_SUCCESS; |
| 5465 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5466 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 5467 | /* Initialize and seed the random generator. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5468 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5469 | global_data.rng_state = RNG_INITIALIZED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5470 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 5471 | if (status != PSA_SUCCESS) { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5472 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5473 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5474 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5475 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5476 | status = psa_initialize_key_slots(); |
| 5477 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 5478 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5479 | } |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5480 | |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 5481 | /* Init drivers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5482 | status = psa_driver_wrapper_init(); |
| 5483 | if (status != PSA_SUCCESS) { |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5484 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5485 | } |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 5486 | |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5487 | #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5488 | status = psa_crypto_load_transaction(); |
| 5489 | if (status == PSA_SUCCESS) { |
| 5490 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 5491 | if (status != PSA_SUCCESS) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 5492 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5493 | } |
| 5494 | status = psa_crypto_stop_transaction(); |
| 5495 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5496 | /* There's no transaction to complete. It's all good. */ |
| 5497 | status = PSA_SUCCESS; |
| 5498 | } |
Gilles Peskine | 4b73422 | 2019-07-24 15:56:31 +0200 | [diff] [blame] | 5499 | #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 5500 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 5501 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5502 | global_data.initialized = 1; |
| 5503 | |
| 5504 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5505 | if (status != PSA_SUCCESS) { |
| 5506 | mbedtls_psa_crypto_free(); |
| 5507 | } |
| 5508 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5509 | } |
| 5510 | |
| 5511 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |