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 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 25 | # if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 26 | # include "check_crypto_config.h" |
| 27 | # endif |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 28 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 29 | # include "psa/crypto.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 30 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 31 | # include "psa_crypto_cipher.h" |
| 32 | # include "psa_crypto_core.h" |
| 33 | # include "psa_crypto_invasive.h" |
| 34 | # include "psa_crypto_driver_wrappers.h" |
| 35 | # include "psa_crypto_ecp.h" |
| 36 | # include "psa_crypto_hash.h" |
| 37 | # include "psa_crypto_mac.h" |
| 38 | # include "psa_crypto_rsa.h" |
| 39 | # include "psa_crypto_ecp.h" |
| 40 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 41 | # include "psa_crypto_se.h" |
| 42 | # endif |
| 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. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 46 | # include "psa_crypto_storage.h" |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 47 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 48 | # include "psa_crypto_random_impl.h" |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 49 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 50 | # include <assert.h> |
| 51 | # include <stdlib.h> |
| 52 | # include <string.h> |
| 53 | # include "mbedtls/platform.h" |
| 54 | # if !defined(MBEDTLS_PLATFORM_C) |
| 55 | # define mbedtls_calloc calloc |
| 56 | # define mbedtls_free free |
| 57 | # endif |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 58 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 59 | # include "mbedtls/aes.h" |
| 60 | # include "mbedtls/asn1.h" |
| 61 | # include "mbedtls/asn1write.h" |
| 62 | # include "mbedtls/bignum.h" |
| 63 | # include "mbedtls/camellia.h" |
| 64 | # include "mbedtls/chacha20.h" |
| 65 | # include "mbedtls/chachapoly.h" |
| 66 | # include "mbedtls/cipher.h" |
| 67 | # include "mbedtls/ccm.h" |
| 68 | # include "mbedtls/cmac.h" |
| 69 | # include "mbedtls/des.h" |
| 70 | # include "mbedtls/ecdh.h" |
| 71 | # include "mbedtls/ecp.h" |
| 72 | # include "mbedtls/entropy.h" |
| 73 | # include "mbedtls/error.h" |
| 74 | # include "mbedtls/gcm.h" |
| 75 | # include "mbedtls/md5.h" |
| 76 | # include "mbedtls/md.h" |
| 77 | # include "md_wrap.h" |
| 78 | # include "mbedtls/pk.h" |
| 79 | # include "pk_wrap.h" |
| 80 | # include "mbedtls/platform_util.h" |
| 81 | # include "mbedtls/error.h" |
| 82 | # include "mbedtls/ripemd160.h" |
| 83 | # include "mbedtls/rsa.h" |
| 84 | # include "mbedtls/sha1.h" |
| 85 | # include "mbedtls/sha256.h" |
| 86 | # include "mbedtls/sha512.h" |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 87 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [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 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [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 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [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 */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 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 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 104 | typedef struct { |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 105 | mbedtls_psa_random_context_t rng; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 106 | unsigned initialized : 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 107 | unsigned rng_state : 2; |
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 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 112 | # if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 113 | mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state = |
| 114 | &global_data.rng.drbg; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 115 | # endif |
Gilles Peskine | 5894e8e | 2020-12-14 14:54:06 +0100 | [diff] [blame] | 116 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 117 | # define GUARD_MODULE_INITIALIZED \ |
| 118 | if (global_data.initialized == 0) \ |
| 119 | return PSA_ERROR_BAD_STATE; |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 120 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [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. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [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: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [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: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 133 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 134 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 135 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 136 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 137 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 138 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 139 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 140 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 141 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 142 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 143 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | 9a94480 | 2018-06-21 09:35:35 +0200 | [diff] [blame] | 144 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 145 | # if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) |
Jaeden Amero | 892cd6d | 2019-02-11 12:21:12 +0000 | [diff] [blame] | 146 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 147 | # endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 148 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 149 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 150 | |
| 151 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 152 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 153 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 154 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 155 | |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 156 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 157 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 158 | |
| 159 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 160 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 161 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 162 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 163 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 164 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 165 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 166 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 167 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 168 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 169 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 170 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 171 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 172 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 173 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 174 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 175 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 176 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 177 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 178 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 179 | # if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 180 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 181 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 182 | * functions are passed a CTR_DRBG instance. */ |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 183 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 184 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 185 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 186 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 187 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 188 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 189 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 190 | # endif |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 191 | |
| 192 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 193 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 194 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 195 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 196 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 197 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 198 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 199 | |
| 200 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 201 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 202 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 203 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 204 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 205 | # if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 206 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
Gilles Peskine | bee96c8 | 2020-11-23 21:00:09 +0100 | [diff] [blame] | 207 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 208 | * functions are passed a HMAC_DRBG instance. */ |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 209 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 210 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 211 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 212 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 213 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 214 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 215 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 216 | # endif |
Gilles Peskine | 82e57d1 | 2020-11-13 21:31:17 +0100 | [diff] [blame] | 217 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 218 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 219 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 220 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 221 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 222 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 223 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 224 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 225 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 226 | |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 227 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 228 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 229 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 230 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 231 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 232 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 233 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 234 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 235 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 236 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 237 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 238 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 239 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 240 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 241 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 242 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 243 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 244 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 245 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 246 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 247 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 248 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 249 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 250 | return PSA_ERROR_STORAGE_FAILURE; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 251 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 252 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 253 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 254 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 255 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 256 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 257 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 258 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 259 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 260 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 261 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 262 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 263 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 264 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 265 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 266 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | f9f1bdf | 2021-06-23 20:32:27 +0200 | [diff] [blame] | 267 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 268 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 269 | |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 270 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 271 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 272 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 273 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | ff2d200 | 2019-05-06 15:26:23 +0200 | [diff] [blame] | 274 | |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 275 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 276 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 277 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 278 | return PSA_ERROR_INVALID_PADDING; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 279 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 280 | return PSA_ERROR_HARDWARE_FAILURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 281 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 282 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 283 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 284 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 285 | return PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 286 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 287 | return PSA_ERROR_INVALID_SIGNATURE; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 288 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 289 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 290 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 291 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Manuel Pégourié-Gonnard | 93c0847 | 2021-04-15 12:23:55 +0200 | [diff] [blame] | 292 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 293 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 294 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 295 | return PSA_ERROR_INVALID_ARGUMENT; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 296 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 297 | return PSA_ERROR_BUFFER_TOO_SMALL; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 298 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 299 | return PSA_ERROR_NOT_SUPPORTED; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 300 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 301 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 302 | return PSA_ERROR_INVALID_SIGNATURE; |
itayzafrir | 7b30f8b | 2018-05-09 16:07:36 +0300 | [diff] [blame] | 303 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 304 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 305 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 306 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
Gilles Peskine | 40d8160 | 2020-11-25 00:09:47 +0100 | [diff] [blame] | 307 | |
Janos Follath | a13b905 | 2019-11-22 12:48:59 +0000 | [diff] [blame] | 308 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 309 | return PSA_ERROR_CORRUPTION_DETECTED; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 310 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 311 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 312 | return PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 316 | /****************************************************************/ |
| 317 | /* Key management */ |
| 318 | /****************************************************************/ |
| 319 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 320 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 321 | static inline int psa_key_slot_is_external(const psa_key_slot_t *slot) |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 322 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 323 | return psa_key_lifetime_is_external(slot->attr.lifetime); |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 324 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 325 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 73167e1 | 2019-07-12 23:44:37 +0200 | [diff] [blame] | 326 | |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 327 | /* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the |
| 328 | * current test driver in key_management.c is using this function |
| 329 | * when accelerators are used for ECC key pair and public key. |
| 330 | * Once that dependency is resolved these guards can be removed. |
| 331 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 332 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 333 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 334 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 335 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) |
| 336 | mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, |
| 337 | size_t bits, |
| 338 | int bits_is_sloppy) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 339 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 340 | switch (curve) { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 341 | case PSA_ECC_FAMILY_SECP_R1: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 342 | switch (bits) { |
| 343 | # if defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 344 | case 192: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 345 | return MBEDTLS_ECP_DP_SECP192R1; |
| 346 | # endif |
| 347 | # if defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 348 | case 224: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 349 | return MBEDTLS_ECP_DP_SECP224R1; |
| 350 | # endif |
| 351 | # if defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 352 | case 256: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 353 | return MBEDTLS_ECP_DP_SECP256R1; |
| 354 | # endif |
| 355 | # if defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 356 | case 384: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 357 | return MBEDTLS_ECP_DP_SECP384R1; |
| 358 | # endif |
| 359 | # if defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 360 | case 521: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 361 | return MBEDTLS_ECP_DP_SECP521R1; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 362 | case 528: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 363 | if (bits_is_sloppy) |
| 364 | return MBEDTLS_ECP_DP_SECP521R1; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 365 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 366 | # endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 367 | } |
| 368 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 369 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 370 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 371 | switch (bits) { |
| 372 | # if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 373 | case 256: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 374 | return MBEDTLS_ECP_DP_BP256R1; |
| 375 | # endif |
| 376 | # if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 377 | case 384: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 378 | return MBEDTLS_ECP_DP_BP384R1; |
| 379 | # endif |
| 380 | # if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 381 | case 512: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 382 | return MBEDTLS_ECP_DP_BP512R1; |
| 383 | # endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 384 | } |
| 385 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 386 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 387 | case PSA_ECC_FAMILY_MONTGOMERY: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 388 | switch (bits) { |
| 389 | # if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 390 | case 255: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 391 | return MBEDTLS_ECP_DP_CURVE25519; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 392 | case 256: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 393 | if (bits_is_sloppy) |
| 394 | return MBEDTLS_ECP_DP_CURVE25519; |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 395 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 396 | # endif |
| 397 | # if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 398 | case 448: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 399 | return MBEDTLS_ECP_DP_CURVE448; |
| 400 | # endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 401 | } |
| 402 | break; |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 403 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 404 | case PSA_ECC_FAMILY_SECP_K1: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 405 | switch (bits) { |
| 406 | # if defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 407 | case 192: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 408 | return MBEDTLS_ECP_DP_SECP192K1; |
| 409 | # endif |
| 410 | # if defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 411 | case 224: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 412 | return MBEDTLS_ECP_DP_SECP224K1; |
| 413 | # endif |
| 414 | # if defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 415 | case 256: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 416 | return MBEDTLS_ECP_DP_SECP256K1; |
| 417 | # endif |
Gilles Peskine | 228abc5 | 2019-12-03 17:24:19 +0100 | [diff] [blame] | 418 | } |
| 419 | break; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 420 | } |
Gilles Peskine | 2fa6b5f | 2021-01-27 15:44:45 +0100 | [diff] [blame] | 421 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 422 | (void)bits_is_sloppy; |
| 423 | return MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 424 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 425 | # endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 426 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \ |
| 427 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 428 | * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 429 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 430 | static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type, |
| 431 | size_t bits) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 432 | { |
| 433 | /* Check that the bit size is acceptable for the key type */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 434 | switch (type) { |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 435 | case PSA_KEY_TYPE_RAW_DATA: |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 436 | case PSA_KEY_TYPE_HMAC: |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 437 | case PSA_KEY_TYPE_DERIVE: |
| 438 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 439 | # if defined(PSA_WANT_KEY_TYPE_AES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 440 | case PSA_KEY_TYPE_AES: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 441 | if (bits != 128 && bits != 192 && bits != 256) |
| 442 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 443 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 444 | # endif |
| 445 | # if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 446 | case PSA_KEY_TYPE_CAMELLIA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 447 | if (bits != 128 && bits != 192 && bits != 256) |
| 448 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 449 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 450 | # endif |
| 451 | # if defined(PSA_WANT_KEY_TYPE_DES) |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 452 | case PSA_KEY_TYPE_DES: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 453 | if (bits != 64 && bits != 128 && bits != 192) |
| 454 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 455 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 456 | # endif |
| 457 | # if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 458 | case PSA_KEY_TYPE_CHACHA20: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 459 | if (bits != 256) |
| 460 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 26869f2 | 2019-05-06 15:25:00 +0200 | [diff] [blame] | 461 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 462 | # endif |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 463 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 464 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 465 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 466 | if (bits % 8 != 0) |
| 467 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 468 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 469 | return PSA_SUCCESS; |
Gilles Peskine | 0ff4b0f | 2018-06-19 21:31:50 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 472 | /** 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] | 473 | * |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 474 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 475 | * 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] | 476 | * |
| 477 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 478 | * \param[in] key_type The key type of the key to be used with the |
| 479 | * \p algorithm. |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 480 | * |
| 481 | * \retval #PSA_SUCCESS |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 482 | * The \p key_type is valid for use with the \p algorithm |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 483 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 484 | * 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] | 485 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 486 | MBEDTLS_STATIC_TESTABLE psa_status_t |
| 487 | psa_mac_key_can_do(psa_algorithm_t algorithm, psa_key_type_t key_type) |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 488 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 489 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 490 | if (key_type == PSA_KEY_TYPE_HMAC) |
| 491 | return PSA_SUCCESS; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 492 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 493 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 494 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 495 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 496 | * key. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 497 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 498 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 499 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 500 | * the block length (larger than 1) for block ciphers. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 501 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) |
| 502 | return PSA_SUCCESS; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 503 | } |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 504 | } |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 505 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 506 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 507 | } |
| 508 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 509 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 510 | size_t buffer_length) |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 511 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 512 | if (slot->key.data != NULL) |
| 513 | return PSA_ERROR_ALREADY_EXISTS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 514 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 515 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 516 | if (slot->key.data == NULL) |
| 517 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 518 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 519 | slot->key.bytes = buffer_length; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 520 | return PSA_SUCCESS; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 523 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 524 | const uint8_t *data, |
| 525 | size_t data_length) |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 526 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 527 | psa_status_t status = psa_allocate_buffer_to_slot(slot, data_length); |
| 528 | if (status != PSA_SUCCESS) |
| 529 | return status; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 530 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 531 | memcpy(slot->key.data, data, data_length); |
| 532 | return PSA_SUCCESS; |
Steven Cooreman | f7cebd4 | 2020-10-13 20:27:40 +0200 | [diff] [blame] | 533 | } |
| 534 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 535 | psa_status_t psa_import_key_into_slot(const psa_key_attributes_t *attributes, |
| 536 | const uint8_t *data, |
| 537 | size_t data_length, |
| 538 | uint8_t *key_buffer, |
| 539 | size_t key_buffer_size, |
| 540 | size_t *key_buffer_length, |
| 541 | size_t *bits) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 542 | { |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 543 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 544 | psa_key_type_t type = attributes->core.type; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 545 | |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 546 | /* zero-length keys are never supported. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 547 | if (data_length == 0) |
| 548 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 549 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 550 | if (key_type_is_raw_bytes(type)) { |
| 551 | *bits = PSA_BYTES_TO_BITS(data_length); |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 552 | |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 553 | /* Ensure that the bytes-to-bits conversion hasn't overflown. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 554 | if (data_length > SIZE_MAX / 8) |
| 555 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 556 | |
Gilles Peskine | 1b9505c | 2019-08-07 10:59:45 +0200 | [diff] [blame] | 557 | /* Enforce a size limit, and in particular ensure that the bit |
| 558 | * size fits in its representation type. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 559 | if ((*bits) > PSA_MAX_KEY_BITS) |
| 560 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 561 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 562 | status = validate_unstructured_key_bit_size(type, *bits); |
| 563 | if (status != PSA_SUCCESS) |
| 564 | return status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 565 | |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 566 | /* Copy the key material. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 567 | memcpy(key_buffer, data, data_length); |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 568 | *key_buffer_length = data_length; |
| 569 | (void)key_buffer_size; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 570 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 571 | return PSA_SUCCESS; |
| 572 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
| 573 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 574 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 575 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 576 | return (mbedtls_psa_ecp_import_key(attributes, data, data_length, |
| 577 | key_buffer, key_buffer_size, |
| 578 | key_buffer_length, bits)); |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 579 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 580 | # endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 581 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
| 582 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 583 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 584 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 585 | return (mbedtls_psa_rsa_import_key(attributes, data, data_length, |
| 586 | key_buffer, key_buffer_size, |
| 587 | key_buffer_length, bits)); |
Steven Cooreman | 3ea0ce4 | 2020-10-23 11:37:05 +0200 | [diff] [blame] | 588 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 589 | # endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 590 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 591 | } |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 592 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 593 | return PSA_ERROR_NOT_SUPPORTED; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 594 | } |
| 595 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 596 | /** Calculate the intersection of two algorithm usage policies. |
| 597 | * |
| 598 | * Return 0 (which allows no operation) on incompatibility. |
| 599 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 600 | static psa_algorithm_t |
| 601 | psa_key_policy_algorithm_intersection(psa_key_type_t key_type, |
| 602 | psa_algorithm_t alg1, |
| 603 | psa_algorithm_t alg2) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 604 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 605 | /* Common case: both sides actually specify the same policy. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 606 | if (alg1 == alg2) |
| 607 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 608 | /* If the policies are from the same hash-and-sign family, check |
| 609 | * if one is a wildcard. If so the other has the specific algorithm. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 610 | if (PSA_ALG_IS_HASH_AND_SIGN(alg1) && PSA_ALG_IS_HASH_AND_SIGN(alg2) && |
| 611 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 612 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) |
| 613 | return alg2; |
| 614 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) |
| 615 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 616 | } |
| 617 | /* If the policies are from the same AEAD family, check whether |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 618 | * one of them is a minimum-tag-length wildcard. Calculate the most |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 619 | * restrictive tag length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 620 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 621 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 622 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 623 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 624 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 625 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 626 | |
Steven Cooreman | 7de9e2d | 2021-02-22 17:05:56 +0100 | [diff] [blame] | 627 | /* If both are wildcards, return most restrictive wildcard */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 628 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 629 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 630 | return (PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(alg1, |
| 631 | restricted_len)); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 632 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 633 | /* If only one is a wildcard, return specific algorithm if compatible. |
| 634 | */ |
| 635 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 636 | (alg1_len <= alg2_len)) { |
| 637 | return alg2; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 638 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 639 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 640 | (alg2_len <= alg1_len)) { |
| 641 | return alg1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | /* If the policies are from the same MAC family, check whether one |
| 645 | * of them is a minimum-MAC-length policy. Calculate the most |
| 646 | * restrictive tag length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 647 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 648 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 649 | /* Validate the combination of key type and algorithm. Since the base |
| 650 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 651 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) |
| 652 | return 0; |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 653 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 654 | /* Get the (exact or at-least) output lengths for both sides of the |
| 655 | * requested intersection. None of the currently supported algorithms |
| 656 | * have an output length dependent on the actual key size, so setting it |
| 657 | * to a bogus value of 0 is currently OK. |
| 658 | * |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 659 | * Note that for at-least-this-length wildcard algorithms, the output |
| 660 | * length is set to the shortest allowed length, which allows us to |
| 661 | * calculate the most restrictive tag length for the intersection. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 662 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 663 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 664 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 665 | |
Steven Cooreman | a1d8322 | 2021-02-25 10:20:29 +0100 | [diff] [blame] | 666 | /* If both are wildcards, return most restrictive wildcard */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 667 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 668 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 669 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 670 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 671 | |
| 672 | /* If only one is an at-least-this-length policy, the intersection would |
| 673 | * be the other (fixed-length) policy as long as said fixed length is |
| 674 | * equal to or larger than the shortest allowed length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 675 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 676 | return (alg1_len <= alg2_len) ? alg2 : 0; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 677 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 678 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 679 | return (alg2_len <= alg1_len) ? alg1 : 0; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 680 | } |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 681 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 682 | /* If none of them are wildcards, check whether they define the same tag |
| 683 | * length. This is still possible here when one is default-length and |
| 684 | * the other specific-length. Ensure to always return the |
| 685 | * specific-length version for the intersection. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 686 | if (alg1_len == alg2_len) |
| 687 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 688 | } |
| 689 | /* If the policies are incompatible, allow nothing. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 690 | return 0; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 693 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 694 | psa_algorithm_t policy_alg, |
| 695 | psa_algorithm_t requested_alg) |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 696 | { |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 697 | /* Common case: the policy only allows requested_alg. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 698 | if (requested_alg == policy_alg) |
| 699 | return 1; |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 700 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 701 | * and requested_alg is the same hash-and-sign family with any hash, |
| 702 | * then requested_alg is compliant with policy_alg. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 703 | if (PSA_ALG_IS_HASH_AND_SIGN(requested_alg) && |
| 704 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 705 | return ((policy_alg & ~PSA_ALG_HASH_MASK) == |
| 706 | (requested_alg & ~PSA_ALG_HASH_MASK)); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 707 | } |
| 708 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 709 | * the requested algorithm, check the requested tag length to be |
| 710 | * equal-length or longer than the wildcard-specified length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 711 | if (PSA_ALG_IS_AEAD(policy_alg) && PSA_ALG_IS_AEAD(requested_alg) && |
| 712 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 713 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 714 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 715 | return (PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 716 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg)); |
Steven Cooreman | 0348802 | 2021-02-18 12:07:20 +0100 | [diff] [blame] | 717 | } |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 718 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 719 | * algorithm, check whether their MAC lengths are compatible. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 720 | if (PSA_ALG_IS_MAC(policy_alg) && PSA_ALG_IS_MAC(requested_alg) && |
| 721 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 722 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 723 | /* Validate the combination of key type and algorithm. Since the policy |
| 724 | * and requested algorithms are the same, we only need this once. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 725 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) |
| 726 | return 0; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 727 | |
Steven Cooreman | 31a876d | 2021-03-03 20:47:40 +0100 | [diff] [blame] | 728 | /* Get both the requested output length for the algorithm which is to be |
| 729 | * verified, and the default output length for the base algorithm. |
| 730 | * Note that none of the currently supported algorithms have an output |
| 731 | * length dependent on actual key size, so setting it to a bogus value |
| 732 | * of 0 is currently OK. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 733 | size_t requested_output_length = |
| 734 | PSA_MAC_LENGTH(key_type, 0, requested_alg); |
| 735 | size_t default_output_length = |
| 736 | PSA_MAC_LENGTH(key_type, 0, PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 737 | |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 738 | /* If the policy is default-length, only allow an algorithm with |
| 739 | * a declared exact-length matching the default. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 740 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) |
| 741 | return requested_output_length == default_output_length; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 742 | |
| 743 | /* If the requested algorithm is default-length, allow it if the policy |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 744 | * length exactly matches the default length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 745 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 746 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 747 | return 1; |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 748 | } |
| 749 | |
Steven Cooreman | cd64093 | 2021-03-08 12:00:27 +0100 | [diff] [blame] | 750 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 751 | * check for the requested MAC length to be equal to or longer than the |
| 752 | * minimum allowed length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 753 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 754 | return (PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 755 | requested_output_length); |
Steven Cooreman | 5ad4bf7 | 2021-03-02 15:11:57 +0100 | [diff] [blame] | 756 | } |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 757 | } |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 758 | /* 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] | 759 | * a key derivation with that key agreement should also be allowed. This |
| 760 | * behaviour is expected to be defined in a future specification version. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 761 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 762 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 763 | return (PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == policy_alg); |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 764 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 765 | /* If it isn't explicitly permitted, it's forbidden. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 766 | return 0; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 767 | } |
| 768 | |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 769 | /** Test whether a policy permits an algorithm. |
| 770 | * |
| 771 | * The caller must test usage flags separately. |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 772 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 773 | * \note This function requires providing the key type for which the policy is |
| 774 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 775 | * have different properties depending on what kind of cipher it is |
| 776 | * combined with. |
| 777 | * |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 778 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 779 | * allowed by the \p policy. |
| 780 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 781 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 782 | * the \p policy does not allow it. |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 783 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 784 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 785 | psa_key_type_t key_type, |
| 786 | psa_algorithm_t alg) |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 787 | { |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 788 | /* '0' is not a valid algorithm */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 789 | if (alg == 0) |
| 790 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | d788fab | 2021-02-25 11:29:17 +0100 | [diff] [blame] | 791 | |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 792 | /* A requested algorithm cannot be a wildcard. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 793 | if (PSA_ALG_IS_WILDCARD(alg)) |
| 794 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 795 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 796 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 797 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) |
| 798 | return PSA_SUCCESS; |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 799 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 800 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 801 | } |
| 802 | |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 803 | /** Restrict a key policy based on a constraint. |
| 804 | * |
Steven Cooreman | 5a17267 | 2021-03-02 21:27:42 +0100 | [diff] [blame] | 805 | * \note This function requires providing the key type for which the policy is |
| 806 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 807 | * have different properties depending on what kind of cipher it is |
| 808 | * combined with. |
| 809 | * |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 810 | * \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] | 811 | * \param[in,out] policy The policy to restrict. |
| 812 | * \param[in] constraint The policy constraint to apply. |
| 813 | * |
| 814 | * \retval #PSA_SUCCESS |
| 815 | * \c *policy contains the intersection of the original value of |
| 816 | * \c *policy and \c *constraint. |
| 817 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Steven Cooreman | 1ac5ce3 | 2021-03-02 16:34:49 +0100 | [diff] [blame] | 818 | * \c key_type, \c *policy and \c *constraint are incompatible. |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 819 | * \c *policy is unchanged. |
| 820 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 821 | static psa_status_t psa_restrict_key_policy(psa_key_type_t key_type, |
| 822 | psa_key_policy_t *policy, |
| 823 | const psa_key_policy_t *constraint) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 824 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 825 | psa_algorithm_t intersection_alg = psa_key_policy_algorithm_intersection( |
| 826 | key_type, policy->alg, constraint->alg); |
| 827 | psa_algorithm_t intersection_alg2 = psa_key_policy_algorithm_intersection( |
| 828 | key_type, policy->alg2, constraint->alg2); |
| 829 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) |
| 830 | return PSA_ERROR_INVALID_ARGUMENT; |
| 831 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) |
| 832 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 833 | policy->usage &= constraint->usage; |
| 834 | policy->alg = intersection_alg; |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 835 | policy->alg2 = intersection_alg2; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 836 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 837 | } |
| 838 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 839 | /** Get the description of a key given its identifier and policy constraints |
| 840 | * and lock it. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 841 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 842 | * 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] | 843 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 844 | * zero, the algorithm is not checked. |
Ronald Cron | 7587ae4 | 2020-11-11 15:04:25 +0100 | [diff] [blame] | 845 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 846 | * In case of a persistent key, the function loads the description of the key |
| 847 | * into a key slot if not already done. |
| 848 | * |
| 849 | * On success, the returned key slot is locked. It is the responsibility of |
| 850 | * 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] | 851 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 852 | static psa_status_t |
| 853 | psa_get_and_lock_key_slot_with_policy(mbedtls_svc_key_id_t key, |
| 854 | psa_key_slot_t **p_slot, |
| 855 | psa_key_usage_t usage, |
| 856 | psa_algorithm_t alg) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 857 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 858 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 859 | psa_key_slot_t *slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 860 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 861 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 862 | if (status != PSA_SUCCESS) |
| 863 | return status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 864 | slot = *p_slot; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 865 | |
| 866 | /* Enforce that usage policy for the key slot contains all the flags |
| 867 | * required by the usage parameter. There is one exception: public |
| 868 | * keys can always be exported, so we treat public key objects as |
| 869 | * if they had the export flag. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 870 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 871 | usage &= ~PSA_KEY_USAGE_EXPORT; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 872 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 873 | if ((slot->attr.policy.usage & usage) != usage) { |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 874 | status = PSA_ERROR_NOT_PERMITTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 875 | goto error; |
Steven Cooreman | 328f11c | 2021-03-02 11:44:51 +0100 | [diff] [blame] | 876 | } |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 877 | |
| 878 | /* Enforce that the usage policy permits the requested algortihm. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 879 | if (alg != 0) { |
| 880 | status = |
| 881 | psa_key_policy_permits(&slot->attr.policy, slot->attr.type, alg); |
| 882 | if (status != PSA_SUCCESS) |
Steven Cooreman | 7e39f05 | 2021-02-23 14:18:32 +0100 | [diff] [blame] | 883 | goto error; |
| 884 | } |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 885 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 886 | return PSA_SUCCESS; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 887 | |
| 888 | error: |
| 889 | *p_slot = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 890 | psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 891 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 892 | return status; |
Darryl Green | 06fd18d | 2018-07-16 11:21:11 +0100 | [diff] [blame] | 893 | } |
Darryl Green | 940d72c | 2018-07-13 13:18:51 +0100 | [diff] [blame] | 894 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 895 | /** Get a key slot containing a transparent key and lock it. |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 896 | * |
| 897 | * A transparent key is a key for which the key material is directly |
| 898 | * available, as opposed to a key in a secure element. |
| 899 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 900 | * This is a temporary function to use instead of |
| 901 | * psa_get_and_lock_key_slot_with_policy() until secure element support is |
| 902 | * fully implemented. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 903 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 904 | * On success, the returned key slot is locked. It is the responsibility of the |
| 905 | * 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] | 906 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 907 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 908 | static psa_status_t |
| 909 | psa_get_and_lock_transparent_key_slot_with_policy(mbedtls_svc_key_id_t key, |
| 910 | psa_key_slot_t **p_slot, |
| 911 | psa_key_usage_t usage, |
| 912 | psa_algorithm_t alg) |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 913 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 914 | psa_status_t status = |
| 915 | psa_get_and_lock_key_slot_with_policy(key, p_slot, usage, alg); |
| 916 | if (status != PSA_SUCCESS) |
| 917 | return status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 918 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 919 | if (psa_key_slot_is_external(*p_slot)) { |
| 920 | psa_unlock_key_slot(*p_slot); |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 921 | *p_slot = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 922 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 923 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 924 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 925 | return PSA_SUCCESS; |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 926 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 927 | # else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 928 | /* With no secure element support, all keys are transparent. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 929 | # define psa_get_and_lock_transparent_key_slot_with_policy(key, p_slot, \ |
| 930 | usage, alg) \ |
| 931 | psa_get_and_lock_key_slot_with_policy(key, p_slot, usage, alg) |
| 932 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 28f8f30 | 2019-07-24 13:30:31 +0200 | [diff] [blame] | 933 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 934 | 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] | 935 | { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 936 | /* Data pointer will always be either a valid pointer or NULL in an |
| 937 | * initialized slot, so we can just free it. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 938 | if (slot->key.data != NULL) |
| 939 | mbedtls_platform_zeroize(slot->key.data, slot->key.bytes); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 940 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 941 | mbedtls_free(slot->key.data); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 942 | slot->key.data = NULL; |
| 943 | slot->key.bytes = 0; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 944 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 945 | return PSA_SUCCESS; |
Darryl Green | 40225ba | 2018-11-15 14:48:15 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 948 | /** Completely wipe a slot in memory, including its policy. |
| 949 | * Persistent storage is not affected. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 950 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 951 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 952 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 953 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 954 | /* |
| 955 | * As the return error code may not be handled in case of multiple errors, |
| 956 | * do our best to report an unexpected lock counter. Assert with |
| 957 | * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one: |
| 958 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 959 | * function is called as part of the execution of a test suite, the |
| 960 | * execution of the test suite is stopped in error if the assertion fails. |
| 961 | */ |
| 962 | if (slot->lock_count != 1) { |
| 963 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1); |
Ronald Cron | ddd3d05 | 2020-10-30 14:07:07 +0100 | [diff] [blame] | 964 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 965 | } |
| 966 | |
Gilles Peskine | 3f7cd62 | 2019-08-13 15:01:08 +0200 | [diff] [blame] | 967 | /* Multipart operations may still be using the key. This is safe |
| 968 | * because all multipart operation objects are independent from |
| 969 | * the key slot: if they need to access the key after the setup |
| 970 | * phase, they have a copy of the key. Note that this means that |
| 971 | * key material can linger until all operations are completed. */ |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 972 | /* At this point, key material and other type-specific content has |
| 973 | * been wiped. Clear remaining metadata. We can call memset and not |
| 974 | * zeroize because the metadata is not particularly sensitive. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 975 | memset(slot, 0, sizeof(*slot)); |
| 976 | return status; |
Gilles Peskine | f77ed1f | 2018-12-03 11:58:46 +0100 | [diff] [blame] | 977 | } |
| 978 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 979 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 980 | { |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 981 | psa_key_slot_t *slot; |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 982 | psa_status_t status; /* status of the last operation */ |
| 983 | psa_status_t overall_status = PSA_SUCCESS; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 984 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 985 | psa_se_drv_table_entry_t *driver; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 986 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 987 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 988 | if (mbedtls_svc_key_id_is_null(key)) |
| 989 | return PSA_SUCCESS; |
Gilles Peskine | 1841cf4 | 2019-10-08 15:48:25 +0200 | [diff] [blame] | 990 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 991 | /* |
Ronald Cron | 19daca9 | 2020-11-10 18:08:03 +0100 | [diff] [blame] | 992 | * 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] | 993 | * key, this will load the key description from persistent memory if not |
| 994 | * done yet. We cannot avoid this loading as without it we don't know if |
| 995 | * the key is operated by an SE or not and this information is needed by |
| 996 | * the current implementation. |
| 997 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 998 | status = psa_get_and_lock_key_slot(key, &slot); |
| 999 | if (status != PSA_SUCCESS) |
| 1000 | return status; |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1001 | |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1002 | /* |
| 1003 | * If the key slot containing the key description is under access by the |
| 1004 | * library (apart from the present access), the key cannot be destroyed |
| 1005 | * yet. For the time being, just return in error. Eventually (to be |
| 1006 | * implemented), the key should be destroyed when all accesses have |
| 1007 | * stopped. |
| 1008 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1009 | if (slot->lock_count > 1) { |
| 1010 | psa_unlock_key_slot(slot); |
| 1011 | return PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | f291111 | 2020-10-29 17:51:10 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1014 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1015 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1016 | * if we attempt it, depending on whether the key is merely read-only |
| 1017 | * by policy or actually physically read-only). |
Gilles Peskine | f9a046e | 2021-06-07 23:27:54 +0200 | [diff] [blame] | 1018 | * Just do the best we can, which is to wipe the copy in memory |
| 1019 | * (done in this function's cleanup code). */ |
| 1020 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1021 | goto exit; |
Gilles Peskine | 6687cd0 | 2021-04-21 22:32:05 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1024 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1025 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1026 | if (driver != NULL) { |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1027 | /* For a key in a secure element, we need to do three things: |
| 1028 | * remove the key file in internal storage, destroy the |
| 1029 | * key inside the secure element, and update the driver's |
| 1030 | * persistent data. Start a transaction that will encompass these |
| 1031 | * three actions. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1032 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1033 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1034 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
Gilles Peskine | 8e33870 | 2019-07-30 20:06:31 +0200 | [diff] [blame] | 1035 | psa_crypto_transaction.key.id = slot->attr.id; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1036 | status = psa_crypto_save_transaction(); |
| 1037 | if (status != PSA_SUCCESS) { |
| 1038 | (void)psa_crypto_stop_transaction(); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1039 | /* We should still try to destroy the key in the secure |
| 1040 | * element and the key metadata in storage. This is especially |
| 1041 | * important if the error is that the storage is full. |
| 1042 | * But how to do it exactly without risking an inconsistent |
| 1043 | * state after a reset? |
| 1044 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1045 | */ |
| 1046 | overall_status = status; |
| 1047 | goto exit; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1050 | status = psa_destroy_se_key(driver, psa_key_slot_get_slot_number(slot)); |
| 1051 | if (overall_status == PSA_SUCCESS) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1052 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1053 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1054 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1055 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1056 | # if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 1057 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1058 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1059 | if (overall_status == PSA_SUCCESS) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1060 | overall_status = status; |
| 1061 | |
Gilles Peskine | 9ce31c4 | 2019-08-13 15:14:20 +0200 | [diff] [blame] | 1062 | /* TODO: other slots may have a copy of the same key. We should |
| 1063 | * invalidate them. |
| 1064 | * https://github.com/ARMmbed/mbed-crypto/issues/214 |
| 1065 | */ |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1066 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1067 | # endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Gilles Peskine | 354f767 | 2019-07-12 23:46:38 +0200 | [diff] [blame] | 1068 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1069 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1070 | if (driver != NULL) { |
| 1071 | status = psa_save_se_persistent_data(driver); |
| 1072 | if (overall_status == PSA_SUCCESS) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1073 | overall_status = status; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1074 | status = psa_crypto_stop_transaction(); |
| 1075 | if (overall_status == PSA_SUCCESS) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1076 | overall_status = status; |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1077 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1078 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1079 | |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1080 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1081 | status = psa_wipe_key_slot(slot); |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1082 | /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1083 | if (status != PSA_SUCCESS) |
Gilles Peskine | 4b7f340 | 2019-08-13 15:58:36 +0200 | [diff] [blame] | 1084 | overall_status = status; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1085 | return overall_status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1086 | } |
| 1087 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1088 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1089 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1090 | static psa_status_t |
| 1091 | psa_get_rsa_public_exponent(const mbedtls_rsa_context *rsa, |
| 1092 | psa_key_attributes_t *attributes) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1093 | { |
| 1094 | mbedtls_mpi mpi; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1095 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1096 | uint8_t *buffer = NULL; |
| 1097 | size_t buflen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1098 | mbedtls_mpi_init(&mpi); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1099 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1100 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi); |
| 1101 | if (ret != 0) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1102 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1103 | if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) { |
Gilles Peskine | 772c8b1 | 2019-04-26 17:37:21 +0200 | [diff] [blame] | 1104 | /* It's the default value, which is reported as an empty string, |
| 1105 | * so there's nothing to do. */ |
| 1106 | goto exit; |
| 1107 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1108 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1109 | buflen = mbedtls_mpi_size(&mpi); |
| 1110 | buffer = mbedtls_calloc(1, buflen); |
| 1111 | if (buffer == NULL) { |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1112 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 1113 | goto exit; |
| 1114 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1115 | ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen); |
| 1116 | if (ret != 0) |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1117 | goto exit; |
| 1118 | attributes->domain_parameters = buffer; |
| 1119 | attributes->domain_parameters_size = buflen; |
| 1120 | |
| 1121 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1122 | mbedtls_mpi_free(&mpi); |
| 1123 | if (ret != 0) |
| 1124 | mbedtls_free(buffer); |
| 1125 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1126 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1127 | # endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1128 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1129 | |
Gilles Peskine | bfd322f | 2019-07-23 11:58:03 +0200 | [diff] [blame] | 1130 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1131 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1132 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1133 | psa_key_attributes_t *attributes) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1134 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1135 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1136 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1137 | psa_key_slot_t *slot; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1138 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1139 | psa_reset_key_attributes(attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1140 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1141 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1142 | if (status != PSA_SUCCESS) |
| 1143 | return status; |
Gilles Peskine | b870b18 | 2018-07-06 16:02:09 +0200 | [diff] [blame] | 1144 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1145 | attributes->core = slot->attr; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1146 | attributes->core.flags &= |
| 1147 | (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | MBEDTLS_PSA_KA_MASK_DUAL_USE); |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1148 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1149 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1150 | if (psa_key_slot_is_external(slot)) |
| 1151 | psa_set_key_slot_number(attributes, psa_key_slot_get_slot_number(slot)); |
| 1152 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1153 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1154 | switch (slot->attr.type) { |
| 1155 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1156 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1157 | case PSA_KEY_TYPE_RSA_KEY_PAIR: |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1158 | case PSA_KEY_TYPE_RSA_PUBLIC_KEY: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1159 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1160 | /* TODO: reporting the public exponent for opaque keys |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1161 | * is not yet implemented. |
| 1162 | * https://github.com/ARMmbed/mbed-crypto/issues/216 |
| 1163 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1164 | if (psa_key_slot_is_external(slot)) |
Gilles Peskine | dc5bfe9 | 2019-07-24 19:09:30 +0200 | [diff] [blame] | 1165 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1166 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1167 | { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1168 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1169 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1170 | status = mbedtls_psa_rsa_load_representation( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1171 | slot->attr.type, slot->key.data, slot->key.bytes, &rsa); |
| 1172 | if (status != PSA_SUCCESS) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1173 | break; |
| 1174 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1175 | status = psa_get_rsa_public_exponent(rsa, attributes); |
| 1176 | mbedtls_rsa_free(rsa); |
| 1177 | mbedtls_free(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1178 | } |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1179 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1180 | # endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1181 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | b699f07 | 2019-04-26 16:06:02 +0200 | [diff] [blame] | 1182 | default: |
| 1183 | /* Nothing else to do. */ |
| 1184 | break; |
| 1185 | } |
| 1186 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1187 | if (status != PSA_SUCCESS) |
| 1188 | psa_reset_key_attributes(attributes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1189 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1190 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1191 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1192 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1193 | } |
| 1194 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1195 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1196 | psa_status_t psa_get_key_slot_number(const psa_key_attributes_t *attributes, |
| 1197 | psa_key_slot_number_t *slot_number) |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1198 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1199 | if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) { |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1200 | *slot_number = attributes->slot_number; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1201 | return PSA_SUCCESS; |
| 1202 | } else |
| 1203 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1204 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1205 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 1206 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1207 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1208 | size_t key_buffer_size, |
| 1209 | uint8_t *data, |
| 1210 | size_t data_size, |
| 1211 | size_t *data_length) |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1212 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1213 | if (key_buffer_size > data_size) |
| 1214 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1215 | memcpy(data, key_buffer, key_buffer_size); |
| 1216 | memset(data + key_buffer_size, 0, data_size - key_buffer_size); |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1217 | *data_length = key_buffer_size; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1218 | return PSA_SUCCESS; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1221 | psa_status_t psa_export_key_internal(const psa_key_attributes_t *attributes, |
| 1222 | const uint8_t *key_buffer, |
| 1223 | size_t key_buffer_size, |
| 1224 | uint8_t *data, |
| 1225 | size_t data_size, |
| 1226 | size_t *data_length) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1227 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1228 | psa_key_type_t type = attributes->core.type; |
| 1229 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1230 | if (key_type_is_raw_bytes(type) || PSA_KEY_TYPE_IS_RSA(type) || |
| 1231 | PSA_KEY_TYPE_IS_ECC(type)) { |
| 1232 | return (psa_export_key_buffer_internal(key_buffer, key_buffer_size, |
| 1233 | data, data_size, data_length)); |
| 1234 | } else { |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1235 | /* This shouldn't happen in the reference implementation, but |
| 1236 | it is valid for a special-purpose implementation to omit |
| 1237 | support for exporting certain key types. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1238 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1242 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
| 1243 | uint8_t *data, |
| 1244 | size_t data_size, |
| 1245 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1246 | { |
| 1247 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1248 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1249 | psa_key_slot_t *slot; |
| 1250 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1251 | /* Reject a zero-length output buffer now, since this can never be a |
| 1252 | * valid key representation. This way we know that data must be a valid |
| 1253 | * pointer and we can do things like memset(data, ..., data_size). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1254 | if (data_size == 0) |
| 1255 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1256 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1257 | /* Set the key to empty now, so that even when there are errors, we always |
| 1258 | * set data_length to a value between 0 and data_size. On error, setting |
| 1259 | * the key to empty is a good choice because an empty key representation is |
| 1260 | * unlikely to be accepted anywhere. */ |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1261 | *data_length = 0; |
| 1262 | |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1263 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1264 | * which don't require any flag, but |
| 1265 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1266 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1267 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1268 | PSA_KEY_USAGE_EXPORT, 0); |
| 1269 | if (status != PSA_SUCCESS) |
| 1270 | return status; |
mohammad1603 | 06e7920 | 2018-03-28 13:17:44 +0300 | [diff] [blame] | 1271 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1272 | psa_key_attributes_t attributes = { .core = slot->attr }; |
| 1273 | status = psa_driver_wrapper_export_key(&attributes, slot->key.data, |
| 1274 | slot->key.bytes, data, data_size, |
| 1275 | data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1276 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1277 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1278 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1279 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1280 | } |
| 1281 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1282 | psa_status_t |
| 1283 | psa_export_public_key_internal(const psa_key_attributes_t *attributes, |
| 1284 | const uint8_t *key_buffer, |
| 1285 | size_t key_buffer_size, |
| 1286 | uint8_t *data, |
| 1287 | size_t data_size, |
| 1288 | size_t *data_length) |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1289 | { |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1290 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1291 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1292 | if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) { |
| 1293 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1294 | /* Exporting public -> public */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1295 | return (psa_export_key_buffer_internal( |
| 1296 | key_buffer, key_buffer_size, data, data_size, data_length)); |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1297 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 1298 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1299 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 1300 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1301 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1302 | return (mbedtls_psa_rsa_export_public_key(attributes, key_buffer, |
| 1303 | key_buffer_size, data, |
| 1304 | data_size, data_length)); |
| 1305 | # else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1306 | /* We don't know how to convert a private RSA key to public. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1307 | return PSA_ERROR_NOT_SUPPORTED; |
| 1308 | # endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1309 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 1310 | } else { |
| 1311 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1312 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1313 | return (mbedtls_psa_ecp_export_public_key(attributes, key_buffer, |
| 1314 | key_buffer_size, data, |
| 1315 | data_size, data_length)); |
| 1316 | # else |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1317 | /* We don't know how to convert a private ECC key to public */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1318 | return PSA_ERROR_NOT_SUPPORTED; |
| 1319 | # endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \ |
| 1320 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
Moran Peker | a998bc6 | 2018-04-16 18:16:20 +0300 | [diff] [blame] | 1321 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1322 | } else { |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1323 | /* This shouldn't happen in the reference implementation, but |
| 1324 | it is valid for a special-purpose implementation to omit |
| 1325 | support for exporting certain key types. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1326 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 560c28a | 2020-07-24 23:20:24 +0200 | [diff] [blame] | 1327 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1328 | } |
Ronald Cron | d18b5f8 | 2020-11-25 16:46:05 +0100 | [diff] [blame] | 1329 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1330 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, |
| 1331 | uint8_t *data, |
| 1332 | size_t data_size, |
| 1333 | size_t *data_length) |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1334 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1335 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1336 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 1337 | psa_key_slot_t *slot; |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1338 | |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1339 | /* Reject a zero-length output buffer now, since this can never be a |
| 1340 | * valid key representation. This way we know that data must be a valid |
| 1341 | * pointer and we can do things like memset(data, ..., data_size). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1342 | if (data_size == 0) |
| 1343 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Ronald Cron | e907e55 | 2021-01-18 13:22:38 +0100 | [diff] [blame] | 1344 | |
Darryl Green | dd8fb77 | 2018-11-07 16:00:44 +0000 | [diff] [blame] | 1345 | /* Set the key to empty now, so that even when there are errors, we always |
| 1346 | * set data_length to a value between 0 and data_size. On error, setting |
| 1347 | * the key to empty is a good choice because an empty key representation is |
| 1348 | * unlikely to be accepted anywhere. */ |
| 1349 | *data_length = 0; |
| 1350 | |
| 1351 | /* Exporting a public key doesn't require a usage flag. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1352 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1353 | if (status != PSA_SUCCESS) |
| 1354 | return status; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1355 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1356 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1357 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1358 | goto exit; |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1359 | } |
| 1360 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1361 | psa_key_attributes_t attributes = { .core = slot->attr }; |
| 1362 | status = psa_driver_wrapper_export_public_key(&attributes, slot->key.data, |
| 1363 | slot->key.bytes, data, |
| 1364 | data_size, data_length); |
Ronald Cron | 9486f9d | 2020-11-26 13:36:23 +0100 | [diff] [blame] | 1365 | |
| 1366 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1367 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1368 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1369 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1370 | } |
| 1371 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1372 | # if defined(static_assert) |
| 1373 | static_assert( |
| 1374 | (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1375 | "One or more key attribute flag is listed as both external-only and dual-use"); |
| 1376 | static_assert( |
| 1377 | (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0, |
| 1378 | "One or more key attribute flag is listed as both internal-only and dual-use"); |
| 1379 | static_assert( |
| 1380 | (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0, |
| 1381 | "One or more key attribute flag is listed as both internal-only and external-only"); |
| 1382 | # endif |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1383 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1384 | /** Validate that a key policy is internally well-formed. |
| 1385 | * |
| 1386 | * This function only rejects invalid policies. It does not validate the |
| 1387 | * consistency of the policy with respect to other attributes of the key |
| 1388 | * such as the key type. |
| 1389 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1390 | 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] | 1391 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1392 | if ((policy->usage & |
| 1393 | ~(PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY | PSA_KEY_USAGE_ENCRYPT | |
| 1394 | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1395 | PSA_KEY_USAGE_VERIFY_MESSAGE | PSA_KEY_USAGE_SIGN_HASH | |
| 1396 | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_VERIFY_DERIVATION | |
| 1397 | PSA_KEY_USAGE_DERIVE)) != 0) |
| 1398 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1399 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1400 | return PSA_SUCCESS; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1401 | } |
| 1402 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1403 | /** Validate the internal consistency of key attributes. |
| 1404 | * |
| 1405 | * This function only rejects invalid attribute values. If does not |
| 1406 | * validate the consistency of the attributes with any key data that may |
| 1407 | * be involved in the creation of the key. |
| 1408 | * |
| 1409 | * Call this function early in the key creation process. |
| 1410 | * |
| 1411 | * \param[in] attributes Key attributes for the new key. |
| 1412 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1413 | * NULL for a transparent key. |
| 1414 | * |
| 1415 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1416 | static psa_status_t |
| 1417 | psa_validate_key_attributes(const psa_key_attributes_t *attributes, |
| 1418 | psa_se_drv_table_entry_t **p_drv) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1419 | { |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1420 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1421 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1422 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1423 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1424 | status = psa_validate_key_location(lifetime, p_drv); |
| 1425 | if (status != PSA_SUCCESS) |
| 1426 | return status; |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 1427 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1428 | status = psa_validate_key_persistence(lifetime); |
| 1429 | if (status != PSA_SUCCESS) |
| 1430 | return status; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1431 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1432 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1433 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) |
| 1434 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1435 | } else { |
| 1436 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) |
| 1437 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame] | 1438 | } |
| 1439 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1440 | status = psa_validate_key_policy(&attributes->core.policy); |
| 1441 | if (status != PSA_SUCCESS) |
| 1442 | return status; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1443 | |
| 1444 | /* Refuse to create overly large keys. |
| 1445 | * Note that this doesn't trigger on import if the attributes don't |
| 1446 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1447 | * psa_import_key() needs its own checks. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1448 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) |
| 1449 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1450 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1451 | /* Reject invalid flags. These should not be reachable through the API. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1452 | if (attributes->core.flags & |
| 1453 | ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY | MBEDTLS_PSA_KA_MASK_DUAL_USE)) |
| 1454 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1455 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1456 | return PSA_SUCCESS; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1457 | } |
| 1458 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1459 | /** Prepare a key slot to receive key material. |
| 1460 | * |
| 1461 | * This function allocates a key slot and sets its metadata. |
| 1462 | * |
| 1463 | * If this function fails, call psa_fail_key_creation(). |
| 1464 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1465 | * This function is intended to be used as follows: |
| 1466 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1467 | * it with the specified attributes, and in case of a volatile key assign it |
| 1468 | * a volatile key identifier. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1469 | * -# Populate the slot with the key material. |
| 1470 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1471 | * In case of failure at any step, stop the sequence and call |
| 1472 | * psa_fail_key_creation(). |
| 1473 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1474 | * On success, the key slot is locked. It is the responsibility of the caller |
| 1475 | * to unlock the key slot when it does not access it anymore. |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1476 | * |
Gilles Peskine | df17914 | 2019-07-15 22:02:14 +0200 | [diff] [blame] | 1477 | * \param method An identification of the calling function. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1478 | * \param[in] attributes Key attributes for the new key. |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1479 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1480 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1481 | * NULL for a transparent key. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1482 | * |
| 1483 | * \retval #PSA_SUCCESS |
| 1484 | * The key slot is ready to receive key material. |
| 1485 | * \return If this function fails, the key slot is an invalid state. |
| 1486 | * 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] | 1487 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1488 | static psa_status_t |
| 1489 | psa_start_key_creation(psa_key_creation_method_t method, |
| 1490 | const psa_key_attributes_t *attributes, |
| 1491 | psa_key_slot_t **p_slot, |
| 1492 | psa_se_drv_table_entry_t **p_drv) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1493 | { |
| 1494 | psa_status_t status; |
Ronald Cron | 2a99315 | 2020-07-17 14:13:26 +0200 | [diff] [blame] | 1495 | psa_key_id_t volatile_key_id; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1496 | psa_key_slot_t *slot; |
| 1497 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1498 | (void)method; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1499 | *p_drv = NULL; |
| 1500 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1501 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1502 | if (status != PSA_SUCCESS) |
| 1503 | return status; |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1504 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1505 | status = psa_get_empty_key_slot(&volatile_key_id, p_slot); |
| 1506 | if (status != PSA_SUCCESS) |
| 1507 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1508 | slot = *p_slot; |
| 1509 | |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1510 | /* We're storing the declared bit-size of the key. It's up to each |
| 1511 | * creation mechanism to verify that this information is correct. |
| 1512 | * It's automatically correct for mechanisms that use the bit-size as |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1513 | * an input (generate, device) but not for those where the bit-size |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1514 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1515 | * volatile key identifier associated to the slot returned to contain its |
| 1516 | * definition. */ |
Gilles Peskine | 76aa09c | 2019-07-31 14:15:34 +0200 | [diff] [blame] | 1517 | |
| 1518 | slot->attr = attributes->core; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1519 | if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1520 | # if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1521 | slot->attr.id = volatile_key_id; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1522 | # else |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1523 | slot->attr.id.key_id = volatile_key_id; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1524 | # endif |
Ronald Cron | c4d1b51 | 2020-07-31 11:26:37 +0200 | [diff] [blame] | 1525 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1526 | |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1527 | /* Erase external-only flags from the internal copy. To access |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1528 | * external-only flags, query `attributes`. Thanks to the check |
| 1529 | * in psa_validate_key_attributes(), this leaves the dual-use |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 1530 | * flags and any internal flag that psa_get_empty_key_slot() |
Gilles Peskine | 013f547 | 2019-08-07 15:42:14 +0200 | [diff] [blame] | 1531 | * may have set. */ |
| 1532 | slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY; |
Gilles Peskine | 91e8c33 | 2019-08-02 19:19:39 +0200 | [diff] [blame] | 1533 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1534 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1535 | /* 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] | 1536 | * when creating or registering a persistent key: |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1537 | * create the key file in internal storage, create the |
| 1538 | * key inside the secure element, and update the driver's |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1539 | * persistent data. This is done by starting a transaction that will |
| 1540 | * encompass these three actions. |
| 1541 | * For registering a volatile key, we just need to find an appropriate |
| 1542 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1543 | * a transaction is not required. */ |
Gilles Peskine | 60450a4 | 2019-07-25 11:32:45 +0200 | [diff] [blame] | 1544 | /* The first thing to do is to find a slot number for the new key. |
| 1545 | * We save the slot number in persistent storage as part of the |
| 1546 | * transaction data. It will be needed to recover if the power |
| 1547 | * fails during the key creation process, to clean up on the secure |
| 1548 | * element side after restarting. Obtaining a slot number from the |
| 1549 | * secure element driver updates its persistent state, but we do not yet |
| 1550 | * save the driver's persistent state, so that if the power fails, |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1551 | * we can roll back to a state where the key doesn't exist. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1552 | if (*p_drv != NULL) { |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1553 | psa_key_slot_number_t slot_number; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1554 | status = |
| 1555 | psa_find_se_slot_for_key(attributes, method, *p_drv, &slot_number); |
| 1556 | if (status != PSA_SUCCESS) |
| 1557 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1558 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1559 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) { |
| 1560 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1561 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1562 | psa_crypto_transaction.key.slot = slot_number; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1563 | psa_crypto_transaction.key.id = slot->attr.id; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1564 | status = psa_crypto_save_transaction(); |
| 1565 | if (status != PSA_SUCCESS) { |
| 1566 | (void)psa_crypto_stop_transaction(); |
| 1567 | return status; |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1568 | } |
Gilles Peskine | 66be51c | 2019-07-25 18:02:52 +0200 | [diff] [blame] | 1569 | } |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1570 | |
| 1571 | status = psa_copy_key_material_into_slot( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1572 | slot, (uint8_t *)(&slot_number), sizeof(slot_number)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1573 | } |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1574 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1575 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1576 | /* Key registration only makes sense with a secure element. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1577 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 3efcebb | 2019-10-01 14:18:35 +0200 | [diff] [blame] | 1578 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1579 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1580 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1581 | return PSA_SUCCESS; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1582 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1583 | |
| 1584 | /** Finalize the creation of a key once its key material has been set. |
| 1585 | * |
| 1586 | * This entails writing the key to persistent storage. |
| 1587 | * |
| 1588 | * If this function fails, call psa_fail_key_creation(). |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1589 | * See the documentation of psa_start_key_creation() for the intended use |
| 1590 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1591 | * |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1592 | * If the finalization succeeds, the function unlocks the key slot (it was |
| 1593 | * locked by psa_start_key_creation()) and the key slot cannot be accessed |
| 1594 | * anymore as part of the key creation process. |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1595 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1596 | * \param[in,out] slot Pointer to the slot with key material. |
| 1597 | * \param[in] driver The secure element driver for the key, |
| 1598 | * or NULL for a transparent key. |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1599 | * \param[out] key On success, identifier of the key. Note that the |
| 1600 | * key identifier is also stored in the key slot. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1601 | * |
| 1602 | * \retval #PSA_SUCCESS |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1603 | * The key was successfully created. |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1604 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1605 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1606 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 1607 | * \retval #PSA_ERROR_DATA_INVALID |
| 1608 | * \retval #PSA_ERROR_DATA_CORRUPT |
gabor-mezei-arm | 86326a9 | 2020-11-30 16:50:34 +0100 | [diff] [blame] | 1609 | * \retval #PSA_ERROR_STORAGE_FAILURE |
gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1610 | * |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1611 | * \return If this function fails, the key slot is an invalid state. |
| 1612 | * 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] | 1613 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1614 | static psa_status_t psa_finish_key_creation(psa_key_slot_t *slot, |
| 1615 | psa_se_drv_table_entry_t *driver, |
| 1616 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1617 | { |
| 1618 | psa_status_t status = PSA_SUCCESS; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1619 | (void)slot; |
| 1620 | (void)driver; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1621 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1622 | # if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 1623 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1624 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1625 | if (driver != NULL) { |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1626 | psa_se_key_data_storage_t data; |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1627 | psa_key_slot_number_t slot_number = |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1628 | psa_key_slot_get_slot_number(slot); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 1629 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1630 | # if defined(static_assert) |
| 1631 | static_assert( |
| 1632 | sizeof(slot_number) == sizeof(data.slot_number), |
| 1633 | "Slot number size does not match psa_se_key_data_storage_t"); |
| 1634 | # endif |
| 1635 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1636 | status = psa_save_persistent_key(&slot->attr, (uint8_t *)&data, |
| 1637 | sizeof(data)); |
| 1638 | } else |
| 1639 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1640 | { |
Steven Cooreman | 40120f6 | 2020-10-29 11:42:22 +0100 | [diff] [blame] | 1641 | /* Key material is saved in export representation in the slot, so |
| 1642 | * just pass the slot buffer for storage. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1643 | status = psa_save_persistent_key(&slot->attr, slot->key.data, |
| 1644 | slot->key.bytes); |
Gilles Peskine | 1df83d4 | 2019-07-23 16:13:14 +0200 | [diff] [blame] | 1645 | } |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1646 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1647 | # endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1648 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1649 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1650 | /* Finish the transaction for a key creation. This does not |
| 1651 | * happen when registering an existing key. Detect this case |
| 1652 | * by checking whether a transaction is in progress (actual |
Steven Cooreman | bbeaf18 | 2020-06-08 18:29:44 +0200 | [diff] [blame] | 1653 | * creation of a persistent key in a secure element requires a transaction, |
| 1654 | * but registration or volatile key creation doesn't use one). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1655 | if (driver != NULL && psa_crypto_transaction.unknown.type == |
| 1656 | PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1657 | status = psa_save_se_persistent_data(driver); |
| 1658 | if (status != PSA_SUCCESS) { |
| 1659 | psa_destroy_persistent_key(slot->attr.id); |
| 1660 | return status; |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1661 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1662 | status = psa_crypto_stop_transaction(); |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1663 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1664 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | cbaff46 | 2019-07-12 23:46:04 +0200 | [diff] [blame] | 1665 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1666 | if (status == PSA_SUCCESS) { |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1667 | *key = slot->attr.id; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1668 | status = psa_unlock_key_slot(slot); |
| 1669 | if (status != PSA_SUCCESS) |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1670 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1671 | } |
Ronald Cron | 5097294 | 2020-11-14 11:28:25 +0100 | [diff] [blame] | 1672 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1673 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /** Abort the creation of a key. |
| 1677 | * |
| 1678 | * You may call this function after calling psa_start_key_creation(), |
| 1679 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 1680 | * function may not clean up persistent storage. |
Gilles Peskine | 9bc88c6 | 2019-04-28 11:37:03 +0200 | [diff] [blame] | 1681 | * See the documentation of psa_start_key_creation() for the intended use |
| 1682 | * of this function. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1683 | * |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1684 | * \param[in,out] slot Pointer to the slot with key material. |
| 1685 | * \param[in] driver The secure element driver for the key, |
| 1686 | * or NULL for a transparent key. |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1687 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1688 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 1689 | psa_se_drv_table_entry_t *driver) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1690 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1691 | (void)driver; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1692 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1693 | if (slot == NULL) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1694 | return; |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1695 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1696 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 1697 | /* TODO: If the key has already been created in the secure |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1698 | * element, and the failure happened later (when saving metadata |
| 1699 | * to internal storage), we need to destroy the key in the secure |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 1700 | * element. |
| 1701 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 1702 | */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 1703 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1704 | /* Abort the ongoing transaction if any (there may not be one if |
| 1705 | * the creation process failed before starting one, or if the |
| 1706 | * key creation is a registration of a key in a secure element). |
| 1707 | * Earlier functions must already have done what it takes to undo any |
| 1708 | * partial creation. All that's left is to update the transaction data |
| 1709 | * itself. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1710 | (void)psa_crypto_stop_transaction(); |
| 1711 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 1712 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1713 | psa_wipe_key_slot(slot); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1714 | } |
| 1715 | |
Gilles Peskine | 1b8594a | 2019-07-31 17:21:46 +0200 | [diff] [blame] | 1716 | /** Validate optional attributes during key creation. |
| 1717 | * |
| 1718 | * Some key attributes are optional during key creation. If they are |
| 1719 | * specified in the attributes structure, check that they are consistent |
| 1720 | * with the data in the slot. |
| 1721 | * |
| 1722 | * This function should be called near the end of key creation, after |
| 1723 | * the slot in memory is fully populated but before saving persistent data. |
| 1724 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1725 | static psa_status_t |
| 1726 | psa_validate_optional_attributes(const psa_key_slot_t *slot, |
| 1727 | const psa_key_attributes_t *attributes) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1728 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1729 | if (attributes->core.type != 0) { |
| 1730 | if (attributes->core.type != slot->attr.type) |
| 1731 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1734 | if (attributes->domain_parameters_size != 0) { |
| 1735 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1736 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1737 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 1738 | mbedtls_rsa_context *rsa = NULL; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1739 | mbedtls_mpi actual, required; |
Steven Cooreman | 6d839f0 | 2020-07-30 11:36:45 +0200 | [diff] [blame] | 1740 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 1741 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 1742 | psa_status_t status = mbedtls_psa_rsa_load_representation( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1743 | slot->attr.type, slot->key.data, slot->key.bytes, &rsa); |
| 1744 | if (status != PSA_SUCCESS) |
| 1745 | return status; |
Steven Cooreman | 75b7436 | 2020-07-28 14:30:13 +0200 | [diff] [blame] | 1746 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1747 | mbedtls_mpi_init(&actual); |
| 1748 | mbedtls_mpi_init(&required); |
| 1749 | ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &actual); |
| 1750 | mbedtls_rsa_free(rsa); |
| 1751 | mbedtls_free(rsa); |
| 1752 | if (ret != 0) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1753 | goto rsa_exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1754 | ret = mbedtls_mpi_read_binary(&required, |
| 1755 | attributes->domain_parameters, |
| 1756 | attributes->domain_parameters_size); |
| 1757 | if (ret != 0) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1758 | goto rsa_exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1759 | if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1760 | ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1761 | rsa_exit: |
| 1762 | mbedtls_mpi_free(&actual); |
| 1763 | mbedtls_mpi_free(&required); |
| 1764 | if (ret != 0) |
| 1765 | return mbedtls_to_psa_error(ret); |
| 1766 | } else |
| 1767 | # endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 1768 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1769 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1770 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1774 | if (attributes->core.bits != 0) { |
| 1775 | if (attributes->core.bits != slot->attr.bits) |
| 1776 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1777 | } |
| 1778 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1779 | return PSA_SUCCESS; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1780 | } |
| 1781 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1782 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
| 1783 | const uint8_t *data, |
| 1784 | size_t data_length, |
| 1785 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1786 | { |
| 1787 | psa_status_t status; |
| 1788 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1789 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1790 | size_t bits; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1791 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1792 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1793 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1794 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 1795 | * This also rejects any key which might be encoded as an empty string, |
| 1796 | * which is never valid. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1797 | if (data_length == 0) |
| 1798 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 1799 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1800 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, &slot, |
| 1801 | &driver); |
| 1802 | if (status != PSA_SUCCESS) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1803 | goto exit; |
| 1804 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1805 | /* In the case of a transparent key or an opaque key stored in local |
| 1806 | * storage (thus not in the case of generating a key in a secure element |
| 1807 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 1808 | * hold the generated key material. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1809 | if (slot->key.data == NULL) { |
| 1810 | status = psa_allocate_buffer_to_slot(slot, data_length); |
| 1811 | if (status != PSA_SUCCESS) |
Gilles Peskine | b46bef2 | 2019-07-30 21:32:04 +0200 | [diff] [blame] | 1812 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1813 | } |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1814 | |
| 1815 | bits = slot->attr.bits; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1816 | status = psa_driver_wrapper_import_key(attributes, data, data_length, |
| 1817 | slot->key.data, slot->key.bytes, |
| 1818 | &slot->key.bytes, &bits); |
| 1819 | if (status != PSA_SUCCESS) |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 1820 | goto exit; |
| 1821 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1822 | if (slot->attr.bits == 0) |
| 1823 | slot->attr.bits = (psa_key_bits_t)bits; |
| 1824 | else if (bits != slot->attr.bits) { |
Steven Cooreman | ac3434f | 2021-01-15 17:36:02 +0100 | [diff] [blame] | 1825 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1826 | goto exit; |
Gilles Peskine | 5d30967 | 2019-07-12 23:47:28 +0200 | [diff] [blame] | 1827 | } |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 1828 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1829 | status = psa_validate_optional_attributes(slot, attributes); |
| 1830 | if (status != PSA_SUCCESS) |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 1831 | goto exit; |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1832 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1833 | status = psa_finish_key_creation(slot, driver, key); |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1834 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1835 | if (status != PSA_SUCCESS) |
| 1836 | psa_fail_key_creation(slot, driver); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1837 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1838 | return status; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1839 | } |
| 1840 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1841 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1842 | psa_status_t mbedtls_psa_register_se_key(const psa_key_attributes_t *attributes) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1843 | { |
| 1844 | psa_status_t status; |
| 1845 | psa_key_slot_t *slot = NULL; |
| 1846 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1847 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1848 | |
| 1849 | /* Leaving attributes unspecified is not currently supported. |
| 1850 | * It could make sense to query the key type and size from the |
| 1851 | * secure element, but not all secure elements support this |
| 1852 | * and the driver HAL doesn't currently support it. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1853 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) |
| 1854 | return PSA_ERROR_NOT_SUPPORTED; |
| 1855 | if (psa_get_key_bits(attributes) == 0) |
| 1856 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1857 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1858 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 1859 | &slot, &driver); |
| 1860 | if (status != PSA_SUCCESS) |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1861 | goto exit; |
| 1862 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1863 | status = psa_finish_key_creation(slot, driver, &key); |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1864 | |
| 1865 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1866 | if (status != PSA_SUCCESS) |
| 1867 | psa_fail_key_creation(slot, driver); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1868 | |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1869 | /* Registration doesn't keep the key in RAM. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1870 | psa_close_key(key); |
| 1871 | return status; |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1872 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1873 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | d772958 | 2019-08-05 15:55:54 +0200 | [diff] [blame] | 1874 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1875 | static psa_status_t psa_copy_key_material(const psa_key_slot_t *source, |
| 1876 | psa_key_slot_t *target) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1877 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1878 | psa_status_t status = psa_copy_key_material_into_slot( |
| 1879 | target, source->key.data, source->key.bytes); |
| 1880 | if (status != PSA_SUCCESS) |
| 1881 | return status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1882 | |
Steven Cooreman | 398aee5 | 2020-10-13 14:35:45 +0200 | [diff] [blame] | 1883 | target->attr.type = source->attr.type; |
| 1884 | target->attr.bits = source->attr.bits; |
| 1885 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1886 | return PSA_SUCCESS; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1887 | } |
| 1888 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1889 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 1890 | const psa_key_attributes_t *specified_attributes, |
| 1891 | mbedtls_svc_key_id_t *target_key) |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1892 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1893 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1894 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1895 | psa_key_slot_t *source_slot = NULL; |
| 1896 | psa_key_slot_t *target_slot = NULL; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1897 | psa_key_attributes_t actual_attributes = *specified_attributes; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 1898 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1899 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 1900 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1901 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 1902 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1903 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 1904 | if (status != PSA_SUCCESS) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1905 | goto exit; |
| 1906 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1907 | status = |
| 1908 | psa_validate_optional_attributes(source_slot, specified_attributes); |
| 1909 | if (status != PSA_SUCCESS) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1910 | goto exit; |
| 1911 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1912 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 1913 | &actual_attributes.core.policy, |
| 1914 | &source_slot->attr.policy); |
| 1915 | if (status != PSA_SUCCESS) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1916 | goto exit; |
| 1917 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1918 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 1919 | &target_slot, &driver); |
| 1920 | if (status != PSA_SUCCESS) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1921 | goto exit; |
| 1922 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1923 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1924 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 1925 | /* Copying to a secure element is not implemented yet. */ |
| 1926 | status = PSA_ERROR_NOT_SUPPORTED; |
| 1927 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1928 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1929 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1930 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1931 | if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) { |
Ronald Cron | 6cc6631 | 2021-04-02 12:27:47 +0200 | [diff] [blame] | 1932 | /* |
| 1933 | * Copying through an opaque driver is not implemented yet, consider |
| 1934 | * a lifetime with an external location as an invalid parameter for |
| 1935 | * now. |
| 1936 | */ |
| 1937 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1938 | goto exit; |
| 1939 | } |
| 1940 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1941 | status = psa_copy_key_material(source_slot, target_slot); |
| 1942 | if (status != PSA_SUCCESS) |
Gilles Peskine | 4ce2a9d | 2019-05-03 16:57:15 +0200 | [diff] [blame] | 1943 | goto exit; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1944 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1945 | status = psa_finish_key_creation(target_slot, driver, target_key); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1946 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1947 | if (status != PSA_SUCCESS) |
| 1948 | psa_fail_key_creation(target_slot, driver); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1949 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1950 | unlock_status = psa_unlock_key_slot(source_slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 1951 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1952 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | f603c71 | 2019-01-19 13:40:11 +0100 | [diff] [blame] | 1953 | } |
| 1954 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1955 | /****************************************************************/ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1956 | /* Message digests */ |
| 1957 | /****************************************************************/ |
| 1958 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1959 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1960 | { |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1961 | /* Aborting a non-active operation is allowed */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1962 | if (operation->id == 0) |
| 1963 | return PSA_SUCCESS; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1964 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1965 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1966 | operation->id = 0; |
| 1967 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1968 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1969 | } |
| 1970 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1971 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 1972 | psa_algorithm_t alg) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1973 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 1974 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1975 | |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1976 | /* A context must be freshly initialized before it can be set up. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1977 | if (operation->id != 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 1978 | status = PSA_ERROR_BAD_STATE; |
| 1979 | goto exit; |
| 1980 | } |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1981 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1982 | if (!PSA_ALG_IS_HASH(alg)) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 1983 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1984 | goto exit; |
| 1985 | } |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1986 | |
Steven Cooreman | b6bf4bb | 2021-03-15 19:00:14 +0100 | [diff] [blame] | 1987 | /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only |
| 1988 | * directly zeroes the int-sized dummy member of the context union. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1989 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Steven Cooreman | 893232f | 2021-03-15 12:23:37 +0100 | [diff] [blame] | 1990 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1991 | status = psa_driver_wrapper_hash_setup(operation, alg); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 1992 | |
| 1993 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1994 | if (status != PSA_SUCCESS) |
| 1995 | psa_hash_abort(operation); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 1996 | |
| 1997 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1998 | } |
| 1999 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2000 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 2001 | const uint8_t *input, |
| 2002 | size_t input_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2003 | { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2004 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2005 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2006 | if (operation->id == 0) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2007 | status = PSA_ERROR_BAD_STATE; |
| 2008 | goto exit; |
| 2009 | } |
Gilles Peskine | 94e4454 | 2018-07-12 16:58:43 +0200 | [diff] [blame] | 2010 | |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2011 | /* Don't require hash implementations to behave correctly on a |
| 2012 | * zero-length input, which may have an invalid pointer. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2013 | if (input_length == 0) |
| 2014 | return PSA_SUCCESS; |
Steven Cooreman | c828835 | 2021-03-04 14:02:19 +0100 | [diff] [blame] | 2015 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2016 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2017 | |
| 2018 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2019 | if (status != PSA_SUCCESS) |
| 2020 | psa_hash_abort(operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2021 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2022 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2025 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2026 | uint8_t *hash, |
| 2027 | size_t hash_size, |
| 2028 | size_t *hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2029 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2030 | *hash_length = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2031 | if (operation->id == 0) |
| 2032 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2033 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2034 | psa_status_t status = |
| 2035 | psa_driver_wrapper_hash_finish(operation, hash, hash_size, hash_length); |
| 2036 | psa_hash_abort(operation); |
| 2037 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2040 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 2041 | const uint8_t *hash, |
| 2042 | size_t hash_length) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2043 | { |
| 2044 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2045 | size_t actual_hash_length; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2046 | psa_status_t status = psa_hash_finish( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2047 | operation, actual_hash, sizeof(actual_hash), &actual_hash_length); |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2048 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2049 | if (status != PSA_SUCCESS) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2050 | goto exit; |
| 2051 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2052 | if (actual_hash_length != hash_length) { |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2053 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2054 | goto exit; |
| 2055 | } |
| 2056 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2057 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2058 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2059 | |
| 2060 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2061 | if (status != PSA_SUCCESS) |
Dave Rodgman | 685b6a7 | 2021-06-24 11:49:14 +0100 | [diff] [blame] | 2062 | psa_hash_abort(operation); |
| 2063 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2064 | return status; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2065 | } |
| 2066 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2067 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 2068 | const uint8_t *input, |
| 2069 | size_t input_length, |
| 2070 | uint8_t *hash, |
| 2071 | size_t hash_size, |
| 2072 | size_t *hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2073 | { |
Steven Cooreman | fbe0928 | 2021-03-08 18:41:12 +0100 | [diff] [blame] | 2074 | *hash_length = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2075 | if (!PSA_ALG_IS_HASH(alg)) |
| 2076 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2077 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2078 | return (psa_driver_wrapper_hash_compute(alg, input, input_length, hash, |
| 2079 | hash_size, hash_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2080 | } |
| 2081 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2082 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 2083 | const uint8_t *input, |
| 2084 | size_t input_length, |
| 2085 | const uint8_t *hash, |
| 2086 | size_t hash_length) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2087 | { |
Steven Cooreman | 84d670d | 2021-02-18 16:22:53 +0100 | [diff] [blame] | 2088 | uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 2089 | size_t actual_hash_length; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2090 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2091 | if (!PSA_ALG_IS_HASH(alg)) |
| 2092 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f66d5fd | 2021-03-08 18:40:40 +0100 | [diff] [blame] | 2093 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 2094 | psa_status_t status = psa_driver_wrapper_hash_compute( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2095 | alg, input, input_length, actual_hash, sizeof(actual_hash), |
| 2096 | &actual_hash_length); |
| 2097 | if (status != PSA_SUCCESS) |
| 2098 | return status; |
| 2099 | if (actual_hash_length != hash_length) |
| 2100 | return PSA_ERROR_INVALID_SIGNATURE; |
| 2101 | if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) |
| 2102 | return PSA_ERROR_INVALID_SIGNATURE; |
| 2103 | return PSA_SUCCESS; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2106 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2107 | psa_hash_operation_t *target_operation) |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2108 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2109 | if (source_operation->id == 0 || target_operation->id != 0) { |
| 2110 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2111 | } |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2112 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2113 | psa_status_t status = |
| 2114 | psa_driver_wrapper_hash_clone(source_operation, target_operation); |
| 2115 | if (status != PSA_SUCCESS) |
| 2116 | psa_hash_abort(target_operation); |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 2117 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2118 | return status; |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2119 | } |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2120 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2121 | /****************************************************************/ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2122 | /* MAC */ |
| 2123 | /****************************************************************/ |
| 2124 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2125 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2126 | { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2127 | /* Aborting a non-active operation is allowed */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2128 | if (operation->id == 0) |
| 2129 | return PSA_SUCCESS; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2130 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2131 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2132 | operation->mac_size = 0; |
| 2133 | operation->is_sign = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2134 | operation->id = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2135 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2136 | return status; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2137 | } |
| 2138 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2139 | static psa_status_t |
| 2140 | psa_mac_finalize_alg_and_key_validation(psa_algorithm_t alg, |
| 2141 | const psa_key_attributes_t *attributes, |
| 2142 | uint8_t *mac_size) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2143 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2144 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2145 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2146 | size_t key_bits = psa_get_key_bits(attributes); |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2147 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2148 | if (!PSA_ALG_IS_MAC(alg)) |
| 2149 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2150 | |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2151 | /* Validate the combination of key type and algorithm */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2152 | status = psa_mac_key_can_do(alg, key_type); |
| 2153 | if (status != PSA_SUCCESS) |
| 2154 | return status; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2155 | |
Steven Cooreman | d1a68f1 | 2021-05-10 11:13:41 +0200 | [diff] [blame] | 2156 | /* Get the output length for the algorithm and key combination */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2157 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2158 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2159 | if (*mac_size < 4) { |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2160 | /* A very short MAC is too short for security since it can be |
| 2161 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2162 | * so we make this our minimum, even though 32 bits is still |
| 2163 | * too small for security. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2164 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 15472f8 | 2021-03-02 16:16:22 +0100 | [diff] [blame] | 2165 | } |
Gilles Peskine | ab1d7ab | 2018-07-06 16:07:47 +0200 | [diff] [blame] | 2166 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2167 | if (*mac_size > |
| 2168 | PSA_MAC_LENGTH(key_type, key_bits, PSA_ALG_FULL_LENGTH_MAC(alg))) { |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2169 | /* It's impossible to "truncate" to a larger length than the full length |
| 2170 | * of the algorithm. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2171 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | f9f7fdf | 2021-03-03 19:04:05 +0100 | [diff] [blame] | 2172 | } |
| 2173 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2174 | return PSA_SUCCESS; |
Ronald Cron | 2dff3b2 | 2021-06-17 16:33:22 +0200 | [diff] [blame] | 2175 | } |
| 2176 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2177 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2178 | mbedtls_svc_key_id_t key, |
| 2179 | psa_algorithm_t alg, |
| 2180 | int is_sign) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2181 | { |
| 2182 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2183 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2184 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2185 | |
| 2186 | /* A context must be freshly initialized before it can be set up. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2187 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2188 | status = PSA_ERROR_BAD_STATE; |
| 2189 | goto exit; |
| 2190 | } |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2191 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2192 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2193 | key, &slot, |
| 2194 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH, alg); |
| 2195 | if (status != PSA_SUCCESS) |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2196 | goto exit; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2197 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2198 | psa_key_attributes_t attributes = { .core = slot->attr }; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2199 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2200 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2201 | &operation->mac_size); |
| 2202 | if (status != PSA_SUCCESS) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2203 | goto exit; |
| 2204 | |
| 2205 | operation->is_sign = is_sign; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 2206 | /* Dispatch the MAC setup call with validated input */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2207 | if (is_sign) { |
| 2208 | status = psa_driver_wrapper_mac_sign_setup( |
| 2209 | operation, &attributes, slot->key.data, slot->key.bytes, alg); |
| 2210 | } else { |
| 2211 | status = psa_driver_wrapper_mac_verify_setup( |
| 2212 | operation, &attributes, slot->key.data, slot->key.bytes, alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2213 | } |
Gilles Peskine | 7e454bc | 2018-06-11 17:26:17 +0200 | [diff] [blame] | 2214 | |
Gilles Peskine | fbfac68 | 2018-07-08 20:51:54 +0200 | [diff] [blame] | 2215 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2216 | if (status != PSA_SUCCESS) |
| 2217 | psa_mac_abort(operation); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2218 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2219 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2220 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2221 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2222 | } |
| 2223 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2224 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2225 | mbedtls_svc_key_id_t key, |
| 2226 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2227 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2228 | return psa_mac_setup(operation, key, alg, 1); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2229 | } |
| 2230 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2231 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2232 | mbedtls_svc_key_id_t key, |
| 2233 | psa_algorithm_t alg) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2234 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2235 | return psa_mac_setup(operation, key, alg, 0); |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2236 | } |
| 2237 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2238 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 2239 | const uint8_t *input, |
| 2240 | size_t input_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2241 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2242 | if (operation->id == 0) |
| 2243 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2244 | |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2245 | /* Don't require hash implementations to behave correctly on a |
| 2246 | * zero-length input, which may have an invalid pointer. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2247 | if (input_length == 0) |
| 2248 | return PSA_SUCCESS; |
Nir Sonnenschein | dcd636a | 2018-06-04 16:03:32 +0300 | [diff] [blame] | 2249 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2250 | psa_status_t status = |
| 2251 | psa_driver_wrapper_mac_update(operation, input, input_length); |
| 2252 | if (status != PSA_SUCCESS) |
| 2253 | psa_mac_abort(operation); |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 2254 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2255 | return status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2256 | } |
| 2257 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2258 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 2259 | uint8_t *mac, |
| 2260 | size_t mac_size, |
| 2261 | size_t *mac_length) |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2262 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2263 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2264 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2265 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2266 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2267 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2268 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2269 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2270 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2271 | if (!operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2272 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2273 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2274 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2275 | |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2276 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2277 | * once all the error checks are done. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2278 | if (operation->mac_size == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2279 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2280 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2281 | } |
Steven Cooreman | 8af5c5c | 2021-05-11 11:09:13 +0200 | [diff] [blame] | 2282 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2283 | if (mac_size < operation->mac_size) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2284 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2285 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2286 | } |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2287 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2288 | status = psa_driver_wrapper_mac_sign_finish( |
| 2289 | operation, mac, operation->mac_size, mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2290 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2291 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2292 | /* In case of success, set the potential excess room in the output buffer |
| 2293 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2294 | * In case of error, set the output length and content to a safe default, |
| 2295 | * such that in case the caller misses an error check, the output would be |
| 2296 | * an unachievable MAC. |
| 2297 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2298 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2299 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2300 | operation->mac_size = 0; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2301 | } |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2302 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2303 | if (mac_size > operation->mac_size) |
| 2304 | memset(&mac[operation->mac_size], '!', mac_size - operation->mac_size); |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2305 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2306 | abort_status = psa_mac_abort(operation); |
Gilles Peskine | 5d0b864 | 2018-07-08 20:35:02 +0200 | [diff] [blame] | 2307 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2308 | return status == PSA_SUCCESS ? abort_status : status; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2311 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2312 | const uint8_t *mac, |
| 2313 | size_t mac_length) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2314 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 2315 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2316 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 2317 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2318 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2319 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2320 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2321 | } |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2322 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2323 | if (operation->is_sign) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2324 | status = PSA_ERROR_BAD_STATE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2325 | goto exit; |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 2326 | } |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2327 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2328 | if (operation->mac_size != mac_length) { |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2329 | status = PSA_ERROR_INVALID_SIGNATURE; |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2330 | goto exit; |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2331 | } |
| 2332 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2333 | status = psa_driver_wrapper_mac_verify_finish(operation, mac, mac_length); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 2334 | |
Dave Rodgman | 90d1cb8 | 2021-06-25 09:09:02 +0100 | [diff] [blame] | 2335 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2336 | abort_status = psa_mac_abort(operation); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2337 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2338 | return status == PSA_SUCCESS ? abort_status : status; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2339 | } |
| 2340 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2341 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2342 | psa_algorithm_t alg, |
| 2343 | const uint8_t *input, |
| 2344 | size_t input_length, |
| 2345 | uint8_t *mac, |
| 2346 | size_t mac_size, |
| 2347 | size_t *mac_length, |
| 2348 | int is_sign) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2349 | { |
| 2350 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2351 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2352 | psa_key_slot_t *slot; |
| 2353 | uint8_t operation_mac_size = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2354 | |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2355 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2356 | key, &slot, |
| 2357 | is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH, alg); |
| 2358 | if (status != PSA_SUCCESS) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2359 | goto exit; |
| 2360 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2361 | psa_key_attributes_t attributes = { .core = slot->attr }; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2362 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2363 | status = psa_mac_finalize_alg_and_key_validation(alg, &attributes, |
| 2364 | &operation_mac_size); |
| 2365 | if (status != PSA_SUCCESS) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2366 | goto exit; |
| 2367 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2368 | if (mac_size < operation_mac_size) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2369 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2370 | goto exit; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2371 | } |
| 2372 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2373 | status = psa_driver_wrapper_mac_compute(&attributes, slot->key.data, |
| 2374 | slot->key.bytes, alg, input, |
| 2375 | input_length, mac, |
| 2376 | operation_mac_size, mac_length); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2377 | |
| 2378 | exit: |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2379 | /* In case of success, set the potential excess room in the output buffer |
| 2380 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2381 | * In case of error, set the output length and content to a safe default, |
| 2382 | * such that in case the caller misses an error check, the output would be |
| 2383 | * an unachievable MAC. |
| 2384 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2385 | if (status != PSA_SUCCESS) { |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2386 | *mac_length = mac_size; |
Ronald Cron | c3dd75f | 2021-06-18 13:05:48 +0200 | [diff] [blame] | 2387 | operation_mac_size = 0; |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2388 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2389 | if (mac_size > operation_mac_size) |
| 2390 | memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2391 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2392 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | 51131b5 | 2021-06-17 17:17:20 +0200 | [diff] [blame] | 2393 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2394 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2395 | } |
| 2396 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2397 | psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key, |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2398 | psa_algorithm_t alg, |
| 2399 | const uint8_t *input, |
| 2400 | size_t input_length, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2401 | uint8_t *mac, |
| 2402 | size_t mac_size, |
| 2403 | size_t *mac_length) |
| 2404 | { |
| 2405 | return (psa_mac_compute_internal(key, alg, input, input_length, mac, |
| 2406 | mac_size, mac_length, 1)); |
| 2407 | } |
| 2408 | |
| 2409 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2410 | psa_algorithm_t alg, |
| 2411 | const uint8_t *input, |
| 2412 | size_t input_length, |
| 2413 | const uint8_t *mac, |
| 2414 | size_t mac_length) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2415 | { |
| 2416 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2417 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2418 | size_t actual_mac_length; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2419 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2420 | status = psa_mac_compute_internal(key, alg, input, input_length, actual_mac, |
| 2421 | sizeof(actual_mac), &actual_mac_length, |
| 2422 | 0); |
| 2423 | if (status != PSA_SUCCESS) |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2424 | goto exit; |
| 2425 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2426 | if (mac_length != actual_mac_length) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2427 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2428 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2429 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2430 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2431 | status = PSA_ERROR_INVALID_SIGNATURE; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2432 | goto exit; |
Ronald Cron | a587cbc | 2021-06-18 14:51:29 +0200 | [diff] [blame] | 2433 | } |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2434 | |
| 2435 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2436 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2437 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2438 | return status; |
gabor-mezei-arm | 4076d3e | 2021-03-01 15:34:18 +0100 | [diff] [blame] | 2439 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2440 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2441 | /****************************************************************/ |
| 2442 | /* Asymmetric cryptography */ |
| 2443 | /****************************************************************/ |
| 2444 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2445 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 2446 | psa_algorithm_t alg) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2447 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2448 | if (input_is_message) { |
| 2449 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) |
| 2450 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2451 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2452 | if (PSA_ALG_IS_HASH_AND_SIGN(alg)) { |
| 2453 | if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) |
| 2454 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2455 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2456 | } else { |
| 2457 | if (!PSA_ALG_IS_HASH_AND_SIGN(alg)) |
| 2458 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2459 | } |
| 2460 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2461 | return PSA_SUCCESS; |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2462 | } |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2463 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2464 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 2465 | int input_is_message, |
| 2466 | psa_algorithm_t alg, |
| 2467 | const uint8_t *input, |
| 2468 | size_t input_length, |
| 2469 | uint8_t *signature, |
| 2470 | size_t signature_size, |
| 2471 | size_t *signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2472 | { |
| 2473 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2474 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2475 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2476 | |
| 2477 | *signature_length = 0; |
| 2478 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2479 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2480 | if (status != PSA_SUCCESS) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2481 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2482 | |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2483 | /* Immediately reject a zero-length signature buffer. This guarantees |
gabor-mezei-arm | 12b4f34 | 2021-05-05 13:54:55 +0200 | [diff] [blame] | 2484 | * that signature must be a valid pointer. (On the other hand, the input |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2485 | * buffer can in principle be empty since it doesn't actually have |
| 2486 | * to be a hash.) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2487 | if (signature_size == 0) |
| 2488 | return PSA_ERROR_BUFFER_TOO_SMALL; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2489 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2490 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2491 | key, &slot, |
| 2492 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_SIGN_HASH, |
| 2493 | alg); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2494 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2495 | if (status != PSA_SUCCESS) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2496 | goto exit; |
| 2497 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2498 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2499 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2500 | goto exit; |
| 2501 | } |
| 2502 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2503 | psa_key_attributes_t attributes = { .core = slot->attr }; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2504 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2505 | if (input_is_message) { |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2506 | status = psa_driver_wrapper_sign_message( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2507 | &attributes, slot->key.data, slot->key.bytes, alg, input, |
| 2508 | input_length, signature, signature_size, signature_length); |
| 2509 | } else { |
| 2510 | status = psa_driver_wrapper_sign_hash(&attributes, slot->key.data, |
| 2511 | slot->key.bytes, alg, input, |
| 2512 | input_length, signature, |
| 2513 | signature_size, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2514 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2515 | |
| 2516 | exit: |
| 2517 | /* Fill the unused part of the output buffer (the whole buffer on error, |
| 2518 | * the trailing part on success) with something that isn't a valid signature |
| 2519 | * (barring an attack on the signature and deliberately-crafted input), |
| 2520 | * in case the caller doesn't check the return status properly. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2521 | if (status == PSA_SUCCESS) |
| 2522 | memset(signature + *signature_length, '!', |
| 2523 | signature_size - *signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2524 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2525 | memset(signature, '!', signature_size); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2526 | /* If signature_size is 0 then we have nothing to do. We must not call |
| 2527 | * memset because signature may be NULL in this case. */ |
| 2528 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2529 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2530 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2531 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2532 | } |
| 2533 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2534 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 2535 | int input_is_message, |
| 2536 | psa_algorithm_t alg, |
| 2537 | const uint8_t *input, |
| 2538 | size_t input_length, |
| 2539 | const uint8_t *signature, |
| 2540 | size_t signature_length) |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2541 | { |
| 2542 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2543 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2544 | psa_key_slot_t *slot; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2545 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2546 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 2547 | if (status != PSA_SUCCESS) |
gabor-mezei-arm | 8b3e886 | 2021-05-05 13:56:27 +0200 | [diff] [blame] | 2548 | return status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2549 | |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2550 | status = psa_get_and_lock_key_slot_with_policy( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2551 | key, &slot, |
| 2552 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 2553 | PSA_KEY_USAGE_VERIFY_HASH, |
| 2554 | alg); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2555 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2556 | if (status != PSA_SUCCESS) |
| 2557 | return status; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2558 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2559 | psa_key_attributes_t attributes = { .core = slot->attr }; |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2560 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2561 | if (input_is_message) { |
| 2562 | status = psa_driver_wrapper_verify_message(&attributes, slot->key.data, |
| 2563 | slot->key.bytes, alg, input, |
| 2564 | input_length, signature, |
| 2565 | signature_length); |
| 2566 | } else { |
| 2567 | status = psa_driver_wrapper_verify_hash(&attributes, slot->key.data, |
| 2568 | slot->key.bytes, alg, input, |
| 2569 | input_length, signature, |
| 2570 | signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2571 | } |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2572 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2573 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2574 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2575 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2576 | } |
| 2577 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2578 | psa_status_t psa_sign_message_builtin(const psa_key_attributes_t *attributes, |
| 2579 | const uint8_t *key_buffer, |
| 2580 | size_t key_buffer_size, |
| 2581 | psa_algorithm_t alg, |
| 2582 | const uint8_t *input, |
| 2583 | size_t input_length, |
| 2584 | uint8_t *signature, |
| 2585 | size_t signature_size, |
| 2586 | size_t *signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2587 | { |
| 2588 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2589 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2590 | if (PSA_ALG_IS_HASH_AND_SIGN(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2591 | size_t hash_length; |
| 2592 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2593 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2594 | status = psa_driver_wrapper_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), |
| 2595 | input, input_length, hash, |
| 2596 | sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2597 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2598 | if (status != PSA_SUCCESS) |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2599 | return status; |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2600 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2601 | return psa_driver_wrapper_sign_hash(attributes, key_buffer, |
| 2602 | key_buffer_size, alg, hash, |
| 2603 | hash_length, signature, |
| 2604 | signature_size, signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2605 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2606 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2607 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2608 | } |
| 2609 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2610 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 2611 | psa_algorithm_t alg, |
| 2612 | const uint8_t *input, |
| 2613 | size_t input_length, |
| 2614 | uint8_t *signature, |
| 2615 | size_t signature_size, |
| 2616 | size_t *signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2617 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2618 | return psa_sign_internal(key, 1, alg, input, input_length, signature, |
| 2619 | signature_size, signature_length); |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2620 | } |
| 2621 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2622 | psa_status_t psa_verify_message_builtin(const psa_key_attributes_t *attributes, |
| 2623 | const uint8_t *key_buffer, |
| 2624 | size_t key_buffer_size, |
| 2625 | psa_algorithm_t alg, |
| 2626 | const uint8_t *input, |
| 2627 | size_t input_length, |
| 2628 | const uint8_t *signature, |
| 2629 | size_t signature_length) |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2630 | { |
| 2631 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2632 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2633 | if (PSA_ALG_IS_HASH_AND_SIGN(alg)) { |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2634 | size_t hash_length; |
| 2635 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 2636 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2637 | status = psa_driver_wrapper_hash_compute(PSA_ALG_SIGN_GET_HASH(alg), |
| 2638 | input, input_length, hash, |
| 2639 | sizeof(hash), &hash_length); |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2640 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2641 | if (status != PSA_SUCCESS) |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2642 | return status; |
gabor-mezei-arm | f9820f9 | 2021-05-03 16:26:20 +0200 | [diff] [blame] | 2643 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2644 | return psa_driver_wrapper_verify_hash(attributes, key_buffer, |
| 2645 | key_buffer_size, alg, hash, |
| 2646 | hash_length, signature, |
| 2647 | signature_length); |
gabor-mezei-arm | 0f62240 | 2021-04-29 16:48:44 +0200 | [diff] [blame] | 2648 | } |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2649 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2650 | return PSA_ERROR_NOT_SUPPORTED; |
gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 2651 | } |
| 2652 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2653 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 2654 | psa_algorithm_t alg, |
| 2655 | const uint8_t *input, |
| 2656 | size_t input_length, |
| 2657 | const uint8_t *signature, |
| 2658 | size_t signature_length) |
gabor-mezei-arm | 5b44652 | 2021-04-20 12:11:35 +0200 | [diff] [blame] | 2659 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2660 | return psa_verify_internal(key, 1, alg, input, input_length, signature, |
| 2661 | signature_length); |
gabor-mezei-arm | 4a21019 | 2021-04-14 21:14:28 +0200 | [diff] [blame] | 2662 | } |
| 2663 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2664 | psa_status_t psa_sign_hash_builtin(const psa_key_attributes_t *attributes, |
| 2665 | const uint8_t *key_buffer, |
| 2666 | size_t key_buffer_size, |
| 2667 | psa_algorithm_t alg, |
| 2668 | const uint8_t *hash, |
| 2669 | size_t hash_length, |
| 2670 | uint8_t *signature, |
| 2671 | size_t signature_size, |
| 2672 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2673 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2674 | if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 2675 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || PSA_ALG_IS_RSA_PSS(alg)) { |
| 2676 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2677 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2678 | return (mbedtls_psa_rsa_sign_hash( |
| 2679 | attributes, key_buffer, key_buffer_size, alg, hash, hash_length, |
| 2680 | signature, signature_size, signature_length)); |
| 2681 | # endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2682 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
| 2683 | } else { |
| 2684 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2685 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2686 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 2687 | if (PSA_ALG_IS_ECDSA(alg)) { |
| 2688 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2689 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2690 | return (mbedtls_psa_ecdsa_sign_hash( |
| 2691 | attributes, key_buffer, key_buffer_size, alg, hash, hash_length, |
| 2692 | signature, signature_size, signature_length)); |
| 2693 | # endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2694 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 2695 | } else { |
| 2696 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2697 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2698 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2699 | |
| 2700 | (void)key_buffer; |
| 2701 | (void)key_buffer_size; |
| 2702 | (void)hash; |
| 2703 | (void)hash_length; |
| 2704 | (void)signature; |
| 2705 | (void)signature_size; |
| 2706 | (void)signature_length; |
| 2707 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2708 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2709 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2710 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2711 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 2712 | psa_algorithm_t alg, |
| 2713 | const uint8_t *hash, |
| 2714 | size_t hash_length, |
| 2715 | uint8_t *signature, |
| 2716 | size_t signature_size, |
| 2717 | size_t *signature_length) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2718 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2719 | return psa_sign_internal(key, 0, alg, hash, hash_length, signature, |
| 2720 | signature_size, signature_length); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2721 | } |
| 2722 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2723 | psa_status_t psa_verify_hash_builtin(const psa_key_attributes_t *attributes, |
| 2724 | const uint8_t *key_buffer, |
| 2725 | size_t key_buffer_size, |
| 2726 | psa_algorithm_t alg, |
| 2727 | const uint8_t *hash, |
| 2728 | size_t hash_length, |
| 2729 | const uint8_t *signature, |
| 2730 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2731 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2732 | if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) { |
| 2733 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || PSA_ALG_IS_RSA_PSS(alg)) { |
| 2734 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2735 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 2736 | return (mbedtls_psa_rsa_verify_hash( |
| 2737 | attributes, key_buffer, key_buffer_size, alg, hash, hash_length, |
| 2738 | signature, signature_length)); |
| 2739 | # endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 2740 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
| 2741 | } else { |
| 2742 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | acda834 | 2020-07-24 23:09:52 +0200 | [diff] [blame] | 2743 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2744 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) { |
| 2745 | if (PSA_ALG_IS_ECDSA(alg)) { |
| 2746 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2747 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 2748 | return (mbedtls_psa_ecdsa_verify_hash( |
| 2749 | attributes, key_buffer, key_buffer_size, alg, hash, hash_length, |
| 2750 | signature, signature_length)); |
| 2751 | # endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 2752 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 2753 | } else { |
| 2754 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2755 | } |
Ronald Cron | 8a494f3 | 2021-02-17 09:49:51 +0100 | [diff] [blame] | 2756 | } |
Ronald Cron | 4501c98 | 2021-03-19 20:09:32 +0100 | [diff] [blame] | 2757 | |
| 2758 | (void)key_buffer; |
| 2759 | (void)key_buffer_size; |
| 2760 | (void)hash; |
| 2761 | (void)hash_length; |
| 2762 | (void)signature; |
| 2763 | (void)signature_length; |
| 2764 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2765 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 1865993 | 2020-12-08 16:32:23 +0100 | [diff] [blame] | 2766 | } |
| 2767 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2768 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 2769 | psa_algorithm_t alg, |
| 2770 | const uint8_t *hash, |
| 2771 | size_t hash_length, |
| 2772 | const uint8_t *signature, |
| 2773 | size_t signature_length) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 2774 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2775 | return psa_verify_internal(key, 0, alg, hash, hash_length, signature, |
| 2776 | signature_length); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2777 | } |
| 2778 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2779 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
| 2780 | static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg, |
| 2781 | mbedtls_rsa_context *rsa) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 2782 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2783 | psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg); |
| 2784 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg); |
| 2785 | mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info); |
Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 2786 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2787 | return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg); |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 2788 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2789 | # endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 2790 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2791 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 2792 | psa_algorithm_t alg, |
| 2793 | const uint8_t *input, |
| 2794 | size_t input_length, |
| 2795 | const uint8_t *salt, |
| 2796 | size_t salt_length, |
| 2797 | uint8_t *output, |
| 2798 | size_t output_size, |
| 2799 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2800 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2801 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2802 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2803 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2804 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2805 | (void)input; |
| 2806 | (void)input_length; |
| 2807 | (void)salt; |
| 2808 | (void)output; |
| 2809 | (void)output_size; |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 2810 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 2811 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2812 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2813 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) |
| 2814 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 2815 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2816 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2817 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 2818 | if (status != PSA_SUCCESS) |
| 2819 | return status; |
| 2820 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 2821 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2822 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2823 | goto exit; |
| 2824 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2825 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2826 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 2827 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
| 2828 | if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) { |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2829 | mbedtls_rsa_context *rsa = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2830 | status = mbedtls_psa_rsa_load_representation( |
| 2831 | slot->attr.type, slot->key.data, slot->key.bytes, &rsa); |
| 2832 | if (status != PSA_SUCCESS) |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2833 | goto rsa_exit; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 2834 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2835 | if (output_size < mbedtls_rsa_get_len(rsa)) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2836 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2837 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2838 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2839 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
| 2840 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
| 2841 | status = mbedtls_to_psa_error(mbedtls_rsa_pkcs1_encrypt( |
| 2842 | rsa, mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE, |
| 2843 | input_length, input, output)); |
| 2844 | } else |
| 2845 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 2846 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
| 2847 | if (PSA_ALG_IS_RSA_OAEP(alg)) { |
| 2848 | status = |
| 2849 | mbedtls_to_psa_error(psa_rsa_oaep_set_padding_mode(alg, rsa)); |
| 2850 | if (status != PSA_SUCCESS) |
Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 2851 | goto rsa_exit; |
| 2852 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2853 | status = mbedtls_to_psa_error(mbedtls_rsa_rsaes_oaep_encrypt( |
| 2854 | rsa, mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE, salt, |
| 2855 | salt_length, input_length, input, output)); |
| 2856 | } else |
| 2857 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2858 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2859 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2860 | goto rsa_exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2861 | } |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2862 | rsa_exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2863 | if (status == PSA_SUCCESS) |
| 2864 | *output_length = mbedtls_rsa_get_len(rsa); |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2865 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2866 | mbedtls_rsa_free(rsa); |
| 2867 | mbedtls_free(rsa); |
| 2868 | } else |
| 2869 | # endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 2870 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2871 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2872 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2873 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2874 | |
| 2875 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2876 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2877 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2878 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2879 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2880 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2881 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 2882 | psa_algorithm_t alg, |
| 2883 | const uint8_t *input, |
| 2884 | size_t input_length, |
| 2885 | const uint8_t *salt, |
| 2886 | size_t salt_length, |
| 2887 | uint8_t *output, |
| 2888 | size_t output_size, |
| 2889 | size_t *output_length) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2890 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2891 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2892 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 2893 | psa_key_slot_t *slot; |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 2894 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2895 | (void)input; |
| 2896 | (void)input_length; |
| 2897 | (void)salt; |
| 2898 | (void)output; |
| 2899 | (void)output_size; |
Darryl Green | 5cc689a | 2018-07-24 15:34:10 +0100 | [diff] [blame] | 2900 | |
Nir Sonnenschein | ca466c8 | 2018-06-04 16:43:12 +0300 | [diff] [blame] | 2901 | *output_length = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2902 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2903 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) |
| 2904 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b3fc05d | 2018-06-30 19:04:35 +0200 | [diff] [blame] | 2905 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 2906 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2907 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 2908 | if (status != PSA_SUCCESS) |
| 2909 | return status; |
| 2910 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2911 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2912 | goto exit; |
| 2913 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2914 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2915 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 2916 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
| 2917 | if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2918 | mbedtls_rsa_context *rsa = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2919 | status = mbedtls_psa_rsa_load_representation( |
| 2920 | slot->attr.type, slot->key.data, slot->key.bytes, &rsa); |
| 2921 | if (status != PSA_SUCCESS) |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2922 | goto exit; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2923 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2924 | if (input_length != mbedtls_rsa_get_len(rsa)) { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2925 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2926 | goto rsa_exit; |
Steven Cooreman | a01795d | 2020-07-24 22:48:15 +0200 | [diff] [blame] | 2927 | } |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2928 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2929 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) |
| 2930 | if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) { |
| 2931 | status = mbedtls_to_psa_error(mbedtls_rsa_pkcs1_decrypt( |
| 2932 | rsa, mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE, |
| 2933 | output_length, input, output, output_size)); |
| 2934 | } else |
| 2935 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ |
| 2936 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) |
| 2937 | if (PSA_ALG_IS_RSA_OAEP(alg)) { |
| 2938 | status = |
| 2939 | mbedtls_to_psa_error(psa_rsa_oaep_set_padding_mode(alg, rsa)); |
| 2940 | if (status != PSA_SUCCESS) |
Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 2941 | goto rsa_exit; |
| 2942 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2943 | status = mbedtls_to_psa_error(mbedtls_rsa_rsaes_oaep_decrypt( |
| 2944 | rsa, mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE, salt, |
| 2945 | salt_length, output_length, input, output, output_size)); |
| 2946 | } else |
| 2947 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2948 | { |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2949 | status = PSA_ERROR_INVALID_ARGUMENT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2950 | } |
Nir Sonnenschein | 717a040 | 2018-06-04 16:36:15 +0300 | [diff] [blame] | 2951 | |
Steven Cooreman | 4fed455 | 2020-08-03 14:46:03 +0200 | [diff] [blame] | 2952 | rsa_exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2953 | mbedtls_rsa_free(rsa); |
| 2954 | mbedtls_free(rsa); |
| 2955 | } else |
| 2956 | # endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
| 2957 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2958 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2959 | status = PSA_ERROR_NOT_SUPPORTED; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2960 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2961 | |
| 2962 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2963 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 2964 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2965 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2966 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2967 | |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2968 | /****************************************************************/ |
| 2969 | /* Symmetric cryptography */ |
| 2970 | /****************************************************************/ |
| 2971 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2972 | static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, |
| 2973 | mbedtls_svc_key_id_t key, |
| 2974 | psa_algorithm_t alg, |
| 2975 | mbedtls_operation_t cipher_operation) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 2976 | { |
| 2977 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2978 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2979 | psa_key_slot_t *slot = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2980 | psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ? |
| 2981 | PSA_KEY_USAGE_ENCRYPT : |
| 2982 | PSA_KEY_USAGE_DECRYPT); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 2983 | |
| 2984 | /* A context must be freshly initialized before it can be set up. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2985 | if (operation->id != 0) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2986 | status = PSA_ERROR_BAD_STATE; |
| 2987 | goto exit; |
| 2988 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 2989 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2990 | if (!PSA_ALG_IS_CIPHER(alg)) { |
Dave Rodgman | 5464824 | 2021-06-24 11:49:45 +0100 | [diff] [blame] | 2991 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2992 | goto exit; |
| 2993 | } |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 2994 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2995 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg); |
| 2996 | if (status != PSA_SUCCESS) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 2997 | goto exit; |
| 2998 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 2999 | /* Initialize the operation struct members, except for id. The id member |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3000 | * 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] | 3001 | * so we only set it (in the driver wrapper) after resources have been |
| 3002 | * allocated/initialized. */ |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3003 | operation->iv_set = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3004 | if (alg == PSA_ALG_ECB_NO_PADDING) |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3005 | operation->iv_required = 0; |
| 3006 | else |
| 3007 | operation->iv_required = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3008 | operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3009 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3010 | psa_key_attributes_t attributes = { .core = slot->attr }; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 3011 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3012 | /* Try doing the operation through a driver before using software fallback. |
| 3013 | */ |
| 3014 | if (cipher_operation == MBEDTLS_ENCRYPT) |
| 3015 | status = psa_driver_wrapper_cipher_encrypt_setup( |
| 3016 | operation, &attributes, slot->key.data, slot->key.bytes, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3017 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3018 | status = psa_driver_wrapper_cipher_decrypt_setup( |
| 3019 | operation, &attributes, slot->key.data, slot->key.bytes, alg); |
Ronald Cron | ab99ac2 | 2020-10-01 16:38:28 +0200 | [diff] [blame] | 3020 | |
Gilles Peskine | 9ab61b6 | 2019-02-25 17:43:14 +0100 | [diff] [blame] | 3021 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3022 | if (status != PSA_SUCCESS) |
| 3023 | psa_cipher_abort(operation); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3024 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3025 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3026 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3027 | return (status == PSA_SUCCESS) ? unlock_status : status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3028 | } |
| 3029 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3030 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 3031 | mbedtls_svc_key_id_t key, |
| 3032 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3033 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3034 | return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3035 | } |
| 3036 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3037 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 3038 | mbedtls_svc_key_id_t key, |
| 3039 | psa_algorithm_t alg) |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3040 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3041 | return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 3042 | } |
| 3043 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3044 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 3045 | uint8_t *iv, |
| 3046 | size_t iv_size, |
| 3047 | size_t *iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3048 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3049 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3050 | |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3051 | *iv_length = 0; |
| 3052 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3053 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3054 | status = PSA_ERROR_BAD_STATE; |
| 3055 | goto exit; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3056 | } |
| 3057 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3058 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3059 | status = PSA_ERROR_BAD_STATE; |
| 3060 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3061 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3062 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3063 | if (iv_size < operation->default_iv_length) { |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3064 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3065 | goto exit; |
| 3066 | } |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3067 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3068 | status = psa_generate_random(iv, operation->default_iv_length); |
| 3069 | if (status != PSA_SUCCESS) |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3070 | goto exit; |
| 3071 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3072 | status = psa_driver_wrapper_cipher_set_iv(operation, iv, |
| 3073 | operation->default_iv_length); |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3074 | |
| 3075 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3076 | if (status == PSA_SUCCESS) { |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3077 | operation->iv_set = 1; |
Ronald Cron | 5618a39 | 2021-03-26 09:52:26 +0100 | [diff] [blame] | 3078 | *iv_length = operation->default_iv_length; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3079 | } else |
| 3080 | psa_cipher_abort(operation); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3081 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3082 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3083 | } |
| 3084 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3085 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 3086 | const uint8_t *iv, |
| 3087 | size_t iv_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3088 | { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3089 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3090 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3091 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3092 | status = PSA_ERROR_BAD_STATE; |
| 3093 | goto exit; |
| 3094 | } |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 3095 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3096 | if (operation->iv_set || !operation->iv_required) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3097 | status = PSA_ERROR_BAD_STATE; |
| 3098 | goto exit; |
| 3099 | } |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame] | 3100 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3101 | if (iv_length > PSA_CIPHER_IV_MAX_SIZE) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3102 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3103 | goto exit; |
| 3104 | } |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3105 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3106 | status = psa_driver_wrapper_cipher_set_iv(operation, iv, iv_length); |
Steven Cooreman | d3feccd | 2020-09-01 15:56:14 +0200 | [diff] [blame] | 3107 | |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3108 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3109 | if (status == PSA_SUCCESS) |
itayzafrir | 534bd7c | 2018-08-02 13:56:32 +0300 | [diff] [blame] | 3110 | operation->iv_set = 1; |
| 3111 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3112 | psa_cipher_abort(operation); |
| 3113 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3114 | } |
| 3115 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3116 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 3117 | const uint8_t *input, |
| 3118 | size_t input_length, |
| 3119 | uint8_t *output, |
| 3120 | size_t output_size, |
| 3121 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3122 | { |
Steven Cooreman | ffecb7b | 2020-08-25 15:13:13 +0200 | [diff] [blame] | 3123 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3124 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3125 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3126 | status = PSA_ERROR_BAD_STATE; |
| 3127 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3128 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3129 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3130 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3131 | status = PSA_ERROR_BAD_STATE; |
| 3132 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3133 | } |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3134 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3135 | status = psa_driver_wrapper_cipher_update( |
| 3136 | operation, input, input_length, output, output_size, output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3137 | |
| 3138 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3139 | if (status != PSA_SUCCESS) |
| 3140 | psa_cipher_abort(operation); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3141 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3142 | return status; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3143 | } |
| 3144 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3145 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
| 3146 | uint8_t *output, |
| 3147 | size_t output_size, |
| 3148 | size_t *output_length) |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3149 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3150 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 3151 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3152 | if (operation->id == 0) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3153 | status = PSA_ERROR_BAD_STATE; |
| 3154 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3155 | } |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3156 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3157 | if (operation->iv_required && !operation->iv_set) { |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3158 | status = PSA_ERROR_BAD_STATE; |
| 3159 | goto exit; |
Steven Cooreman | 7df0292 | 2020-09-09 15:28:49 +0200 | [diff] [blame] | 3160 | } |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 3161 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3162 | status = psa_driver_wrapper_cipher_finish(operation, output, output_size, |
| 3163 | output_length); |
Dave Rodgman | 38e62ae | 2021-06-23 11:38:39 +0100 | [diff] [blame] | 3164 | |
| 3165 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3166 | if (status == PSA_SUCCESS) |
| 3167 | return psa_cipher_abort(operation); |
| 3168 | else { |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3169 | *output_length = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3170 | (void)psa_cipher_abort(operation); |
Janos Follath | 315b51c | 2018-07-09 16:04:51 +0100 | [diff] [blame] | 3171 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3172 | return status; |
Steven Cooreman | ef8575e | 2020-09-11 11:44:50 +0200 | [diff] [blame] | 3173 | } |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3174 | } |
| 3175 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3176 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3177 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3178 | if (operation->id == 0) { |
Steven Cooreman | a07b997 | 2020-09-10 14:54:14 +0200 | [diff] [blame] | 3179 | /* The object has (apparently) been initialized but it is not (yet) |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3180 | * in use. It's ok to call abort on such an object, and there's |
| 3181 | * nothing to do. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3182 | return PSA_SUCCESS; |
Gilles Peskine | 8173631 | 2018-06-26 15:04:31 +0200 | [diff] [blame] | 3183 | } |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3184 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3185 | psa_driver_wrapper_cipher_abort(operation); |
Gilles Peskine | e553c65 | 2018-06-04 16:22:46 +0200 | [diff] [blame] | 3186 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 3187 | operation->id = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3188 | operation->iv_set = 0; |
Moran Peker | ad9d82c | 2018-04-30 12:31:04 +0300 | [diff] [blame] | 3189 | operation->iv_required = 0; |
Moran Peker | 41deec4 | 2018-04-04 15:43:05 +0300 | [diff] [blame] | 3190 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3191 | return PSA_SUCCESS; |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 3192 | } |
| 3193 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3194 | psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key, |
| 3195 | psa_algorithm_t alg, |
| 3196 | const uint8_t *input, |
| 3197 | size_t input_length, |
| 3198 | uint8_t *output, |
| 3199 | size_t output_size, |
| 3200 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3201 | { |
| 3202 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3203 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3204 | psa_key_slot_t *slot; |
| 3205 | psa_key_type_t key_type; |
| 3206 | size_t iv_length; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3207 | |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3208 | *output_length = 0; |
| 3209 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3210 | if (!PSA_ALG_IS_CIPHER(alg)) |
| 3211 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3212 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3213 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3214 | PSA_KEY_USAGE_ENCRYPT, alg); |
| 3215 | if (status != PSA_SUCCESS) |
| 3216 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3217 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3218 | psa_key_attributes_t attributes = { .core = slot->attr }; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3219 | |
| 3220 | key_type = slot->attr.type; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3221 | iv_length = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3222 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3223 | if (iv_length > 0) { |
| 3224 | if (output_size < iv_length) { |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3225 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3226 | goto exit; |
| 3227 | } |
| 3228 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3229 | status = psa_generate_random(output, iv_length); |
| 3230 | if (status != PSA_SUCCESS) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3231 | goto exit; |
| 3232 | } |
| 3233 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3234 | status = psa_driver_wrapper_cipher_encrypt(&attributes, slot->key.data, |
| 3235 | slot->key.bytes, alg, input, |
| 3236 | input_length, output, |
| 3237 | output_size, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3238 | |
| 3239 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3240 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3241 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3242 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3243 | } |
| 3244 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3245 | psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key, |
| 3246 | psa_algorithm_t alg, |
| 3247 | const uint8_t *input, |
| 3248 | size_t input_length, |
| 3249 | uint8_t *output, |
| 3250 | size_t output_size, |
| 3251 | size_t *output_length) |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3252 | { |
| 3253 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3254 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3255 | psa_key_slot_t *slot; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3256 | |
gabor-mezei-arm | 6f4e5bb | 2021-06-25 15:21:11 +0200 | [diff] [blame] | 3257 | *output_length = 0; |
| 3258 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3259 | if (!PSA_ALG_IS_CIPHER(alg)) |
| 3260 | return PSA_ERROR_INVALID_ARGUMENT; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 3261 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3262 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3263 | PSA_KEY_USAGE_DECRYPT, alg); |
| 3264 | if (status != PSA_SUCCESS) |
| 3265 | return status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3266 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3267 | psa_key_attributes_t attributes = { .core = slot->attr }; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3268 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3269 | if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) { |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3270 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3271 | goto exit; |
| 3272 | } |
| 3273 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3274 | status = psa_driver_wrapper_cipher_decrypt(&attributes, slot->key.data, |
| 3275 | slot->key.bytes, alg, input, |
| 3276 | input_length, output, |
| 3277 | output_size, output_length); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3278 | |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 3279 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3280 | unlock_status = psa_unlock_key_slot(slot); |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3281 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3282 | return (status == PSA_SUCCESS) ? unlock_status : status; |
gabor-mezei-arm | ba0fa75 | 2021-03-01 15:04:24 +0100 | [diff] [blame] | 3283 | } |
| 3284 | |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3285 | /****************************************************************/ |
| 3286 | /* AEAD */ |
| 3287 | /****************************************************************/ |
Gilles Peskine | 7bcfc0a | 2018-06-18 21:49:39 +0200 | [diff] [blame] | 3288 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3289 | psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key, |
| 3290 | psa_algorithm_t alg, |
| 3291 | const uint8_t *nonce, |
| 3292 | size_t nonce_length, |
| 3293 | const uint8_t *additional_data, |
| 3294 | size_t additional_data_length, |
| 3295 | const uint8_t *plaintext, |
| 3296 | size_t plaintext_length, |
| 3297 | uint8_t *ciphertext, |
| 3298 | size_t ciphertext_size, |
| 3299 | size_t *ciphertext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3300 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3301 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3302 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3303 | |
mohammad1603 | f08a550 | 2018-06-03 15:05:47 +0300 | [diff] [blame] | 3304 | *ciphertext_length = 0; |
mohammad1603 | e58e684 | 2018-05-09 04:58:32 -0700 | [diff] [blame] | 3305 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3306 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) |
| 3307 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3308 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3309 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3310 | PSA_KEY_USAGE_ENCRYPT, alg); |
| 3311 | if (status != PSA_SUCCESS) |
| 3312 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3313 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3314 | psa_key_attributes_t attributes = { .core = slot->attr }; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3315 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3316 | status = psa_driver_wrapper_aead_encrypt( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3317 | &attributes, slot->key.data, slot->key.bytes, alg, nonce, nonce_length, |
| 3318 | additional_data, additional_data_length, plaintext, plaintext_length, |
| 3319 | ciphertext, ciphertext_size, ciphertext_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3320 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3321 | if (status != PSA_SUCCESS && ciphertext_size != 0) |
| 3322 | memset(ciphertext, 0, ciphertext_size); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3323 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3324 | psa_unlock_key_slot(slot); |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3325 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3326 | return status; |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3327 | } |
| 3328 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3329 | psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key, |
| 3330 | psa_algorithm_t alg, |
| 3331 | const uint8_t *nonce, |
| 3332 | size_t nonce_length, |
| 3333 | const uint8_t *additional_data, |
| 3334 | size_t additional_data_length, |
| 3335 | const uint8_t *ciphertext, |
| 3336 | size_t ciphertext_length, |
| 3337 | uint8_t *plaintext, |
| 3338 | size_t plaintext_size, |
| 3339 | size_t *plaintext_length) |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3340 | { |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3341 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3342 | psa_key_slot_t *slot; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3343 | |
Gilles Peskine | ee652a3 | 2018-06-01 19:23:52 +0200 | [diff] [blame] | 3344 | *plaintext_length = 0; |
mohammad1603 | 9e5a515 | 2018-04-26 12:07:35 +0300 | [diff] [blame] | 3345 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3346 | if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) |
| 3347 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | ea7ab13 | 2021-03-17 16:28:00 +0100 | [diff] [blame] | 3348 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3349 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3350 | PSA_KEY_USAGE_DECRYPT, alg); |
| 3351 | if (status != PSA_SUCCESS) |
| 3352 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3353 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3354 | psa_key_attributes_t attributes = { .core = slot->attr }; |
Gilles Peskine | f7e7b01 | 2019-05-06 15:27:16 +0200 | [diff] [blame] | 3355 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 3356 | status = psa_driver_wrapper_aead_decrypt( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3357 | &attributes, slot->key.data, slot->key.bytes, alg, nonce, nonce_length, |
| 3358 | additional_data, additional_data_length, ciphertext, ciphertext_length, |
| 3359 | plaintext, plaintext_size, plaintext_length); |
Gilles Peskine | a40d774 | 2018-06-01 16:28:30 +0200 | [diff] [blame] | 3360 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3361 | if (status != PSA_SUCCESS && plaintext_size != 0) |
| 3362 | memset(plaintext, 0, plaintext_size); |
mohammad1603 | 60a64d0 | 2018-06-03 17:20:42 +0300 | [diff] [blame] | 3363 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3364 | psa_unlock_key_slot(slot); |
Ronald Cron | 215633c | 2021-03-16 17:15:37 +0100 | [diff] [blame] | 3365 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3366 | return status; |
mohammad1603 | 5955c98 | 2018-04-26 00:53:03 +0300 | [diff] [blame] | 3367 | } |
| 3368 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3369 | /****************************************************************/ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3370 | /* Generators */ |
| 3371 | /****************************************************************/ |
| 3372 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3373 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3374 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3375 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3376 | # define AT_LEAST_ONE_BUILTIN_KDF |
| 3377 | # endif /* At least one builtin KDF */ |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3378 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3379 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 3380 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3381 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3382 | static psa_status_t |
| 3383 | psa_key_derivation_start_hmac(psa_mac_operation_t *operation, |
| 3384 | psa_algorithm_t hash_alg, |
| 3385 | const uint8_t *hmac_key, |
| 3386 | size_t hmac_key_length) |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3387 | { |
| 3388 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3389 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3390 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 3391 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length)); |
| 3392 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3393 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 3394 | operation->is_sign = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3395 | operation->mac_size = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 3396 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3397 | status = psa_driver_wrapper_mac_sign_setup(operation, &attributes, hmac_key, |
| 3398 | hmac_key_length, |
| 3399 | PSA_ALG_HMAC(hash_alg)); |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3400 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3401 | psa_reset_key_attributes(&attributes); |
| 3402 | return status; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 3403 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3404 | # endif /* KDF algorithms reliant on HMAC */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3405 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3406 | # define HKDF_STATE_INIT 0 /* no input yet */ |
| 3407 | # define HKDF_STATE_STARTED 1 /* got salt */ |
| 3408 | # define HKDF_STATE_KEYED 2 /* got key */ |
| 3409 | # define HKDF_STATE_OUTPUT 3 /* output started */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3410 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3411 | static psa_algorithm_t |
| 3412 | psa_key_derivation_get_kdf_alg(const psa_key_derivation_operation_t *operation) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3413 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3414 | if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) |
| 3415 | return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg); |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3416 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3417 | return operation->alg; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3418 | } |
| 3419 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3420 | 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] | 3421 | { |
| 3422 | psa_status_t status = PSA_SUCCESS; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3423 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
| 3424 | if (kdf_alg == 0) { |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3425 | /* The object has (apparently) been initialized but it is not |
| 3426 | * in use. It's ok to call abort on such an object, and there's |
| 3427 | * nothing to do. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3428 | } else |
| 3429 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 3430 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 3431 | mbedtls_free(operation->ctx.hkdf.info); |
| 3432 | status = psa_mac_abort(&operation->ctx.hkdf.hmac); |
| 3433 | } else |
| 3434 | # endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 3435 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3436 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3437 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 3438 | /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */ |
| 3439 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 3440 | if (operation->ctx.tls12_prf.secret != NULL) { |
| 3441 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret, |
| 3442 | operation->ctx.tls12_prf.secret_length); |
| 3443 | mbedtls_free(operation->ctx.tls12_prf.secret); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3444 | } |
| 3445 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3446 | if (operation->ctx.tls12_prf.seed != NULL) { |
| 3447 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed, |
| 3448 | operation->ctx.tls12_prf.seed_length); |
| 3449 | mbedtls_free(operation->ctx.tls12_prf.seed); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3450 | } |
| 3451 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3452 | if (operation->ctx.tls12_prf.label != NULL) { |
| 3453 | mbedtls_platform_zeroize(operation->ctx.tls12_prf.label, |
| 3454 | operation->ctx.tls12_prf.label_length); |
| 3455 | mbedtls_free(operation->ctx.tls12_prf.label); |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3456 | } |
| 3457 | |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3458 | status = PSA_SUCCESS; |
Janos Follath | 6a1d262 | 2019-06-11 10:37:28 +0100 | [diff] [blame] | 3459 | |
| 3460 | /* We leave the fields Ai and output_block to be erased safely by the |
| 3461 | * mbedtls_platform_zeroize() in the end of this function. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3462 | } else |
| 3463 | # endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3464 | * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3465 | { |
| 3466 | status = PSA_ERROR_BAD_STATE; |
| 3467 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3468 | mbedtls_platform_zeroize(operation, sizeof(*operation)); |
| 3469 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3470 | } |
| 3471 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3472 | psa_status_t |
| 3473 | psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation, |
| 3474 | size_t *capacity) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3475 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3476 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3477 | /* This is a blank key derivation operation. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3478 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3481 | *capacity = operation->capacity; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3482 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3483 | } |
| 3484 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3485 | psa_status_t |
| 3486 | psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, |
| 3487 | size_t capacity) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3488 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3489 | if (operation->alg == 0) |
| 3490 | return PSA_ERROR_BAD_STATE; |
| 3491 | if (capacity > operation->capacity) |
| 3492 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3493 | operation->capacity = capacity; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3494 | return PSA_SUCCESS; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3495 | } |
| 3496 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3497 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3498 | /* Read some bytes from an HKDF-based operation. This performs a chunk |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3499 | * of the expand phase of the HKDF algorithm. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3500 | static psa_status_t |
| 3501 | psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf, |
| 3502 | psa_algorithm_t hash_alg, |
| 3503 | uint8_t *output, |
| 3504 | size_t output_length) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3505 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3506 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3507 | size_t hmac_output_length; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3508 | psa_status_t status; |
| 3509 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3510 | if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) |
| 3511 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3512 | hkdf->state = HKDF_STATE_OUTPUT; |
| 3513 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3514 | while (output_length != 0) { |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3515 | /* Copy what remains of the current block */ |
| 3516 | uint8_t n = hash_length - hkdf->offset_in_block; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3517 | if (n > output_length) |
| 3518 | n = (uint8_t)output_length; |
| 3519 | memcpy(output, hkdf->output_block + hkdf->offset_in_block, n); |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3520 | output += n; |
| 3521 | output_length -= n; |
| 3522 | hkdf->offset_in_block += n; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3523 | if (output_length == 0) |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3524 | break; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3525 | /* We can't be wanting more output after block 0xff, otherwise |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 3526 | * the capacity check in psa_key_derivation_output_bytes() would have |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3527 | * prevented this call. It could happen only if the operation |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3528 | * object was corrupted or if this function is called directly |
| 3529 | * inside the library. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3530 | if (hkdf->block_number == 0xff) |
| 3531 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3532 | |
| 3533 | /* We need a new block */ |
| 3534 | ++hkdf->block_number; |
| 3535 | hkdf->offset_in_block = 0; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3536 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3537 | status = psa_key_derivation_start_hmac(&hkdf->hmac, hash_alg, hkdf->prk, |
| 3538 | hash_length); |
| 3539 | if (status != PSA_SUCCESS) |
| 3540 | return status; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3541 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3542 | if (hkdf->block_number != 1) { |
| 3543 | status = |
| 3544 | psa_mac_update(&hkdf->hmac, hkdf->output_block, hash_length); |
| 3545 | if (status != PSA_SUCCESS) |
| 3546 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3547 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3548 | status = psa_mac_update(&hkdf->hmac, hkdf->info, hkdf->info_length); |
| 3549 | if (status != PSA_SUCCESS) |
| 3550 | return status; |
| 3551 | status = psa_mac_update(&hkdf->hmac, &hkdf->block_number, 1); |
| 3552 | if (status != PSA_SUCCESS) |
| 3553 | return status; |
| 3554 | status = psa_mac_sign_finish(&hkdf->hmac, hkdf->output_block, |
| 3555 | sizeof(hkdf->output_block), |
| 3556 | &hmac_output_length); |
| 3557 | if (status != PSA_SUCCESS) |
| 3558 | return status; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3559 | } |
| 3560 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3561 | return PSA_SUCCESS; |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3562 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3563 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3564 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3565 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3566 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3567 | static psa_status_t psa_key_derivation_tls12_prf_generate_next_block( |
| 3568 | psa_tls12_prf_key_derivation_t *tls12_prf, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3569 | psa_algorithm_t alg) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3570 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3571 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg); |
| 3572 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3573 | psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT; |
| 3574 | size_t hmac_output_length; |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3575 | psa_status_t status, cleanup_status; |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3576 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3577 | /* We can't be wanting more output after block 0xff, otherwise |
| 3578 | * the capacity check in psa_key_derivation_output_bytes() would have |
| 3579 | * prevented this call. It could happen only if the operation |
| 3580 | * object was corrupted or if this function is called directly |
| 3581 | * inside the library. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3582 | if (tls12_prf->block_number == 0xff) |
| 3583 | return PSA_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3584 | |
| 3585 | /* We need a new block */ |
| 3586 | ++tls12_prf->block_number; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3587 | tls12_prf->left_in_block = hash_length; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3588 | |
| 3589 | /* Recall the definition of the TLS-1.2-PRF from RFC 5246: |
| 3590 | * |
| 3591 | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
| 3592 | * |
| 3593 | * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
| 3594 | * HMAC_hash(secret, A(2) + seed) + |
| 3595 | * HMAC_hash(secret, A(3) + seed) + ... |
| 3596 | * |
| 3597 | * A(0) = seed |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3598 | * A(i) = HMAC_hash(secret, A(i-1)) |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3599 | * |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3600 | * The `psa_tls12_prf_key_derivation` structure saves the block |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3601 | * `HMAC_hash(secret, A(i) + seed)` from which the output |
Janos Follath | 76c3984 | 2019-06-26 12:50:36 +0100 | [diff] [blame] | 3602 | * is currently extracted as `output_block` and where i is |
| 3603 | * `block_number`. |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3604 | */ |
| 3605 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3606 | status = psa_key_derivation_start_hmac(&hmac, hash_alg, tls12_prf->secret, |
| 3607 | tls12_prf->secret_length); |
| 3608 | if (status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3609 | goto cleanup; |
| 3610 | |
| 3611 | /* Calculate A(i) where i = tls12_prf->block_number. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3612 | if (tls12_prf->block_number == 1) { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3613 | /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads |
| 3614 | * the variable seed and in this instance means it in the context of the |
| 3615 | * P_hash function, where seed = label + seed.) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3616 | status = |
| 3617 | psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 3618 | if (status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3619 | goto cleanup; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3620 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 3621 | if (status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3622 | goto cleanup; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3623 | } else { |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3624 | /* A(i) = HMAC_hash(secret, A(i-1)) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3625 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 3626 | if (status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3627 | goto cleanup; |
| 3628 | } |
| 3629 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3630 | status = psa_mac_sign_finish(&hmac, tls12_prf->Ai, hash_length, |
| 3631 | &hmac_output_length); |
| 3632 | if (hmac_output_length != hash_length) |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 3633 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3634 | if (status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3635 | goto cleanup; |
| 3636 | |
| 3637 | /* Calculate HMAC_hash(secret, A(i) + label + seed). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3638 | status = psa_key_derivation_start_hmac(&hmac, hash_alg, tls12_prf->secret, |
| 3639 | tls12_prf->secret_length); |
| 3640 | if (status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3641 | goto cleanup; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3642 | status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length); |
| 3643 | if (status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3644 | goto cleanup; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3645 | status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length); |
| 3646 | if (status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3647 | goto cleanup; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3648 | status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length); |
| 3649 | if (status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3650 | goto cleanup; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3651 | status = psa_mac_sign_finish(&hmac, tls12_prf->output_block, hash_length, |
| 3652 | &hmac_output_length); |
| 3653 | if (status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3654 | goto cleanup; |
| 3655 | |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3656 | cleanup: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3657 | cleanup_status = psa_mac_abort(&hmac); |
| 3658 | if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) |
Janos Follath | ea29bfb | 2019-06-19 12:21:20 +0100 | [diff] [blame] | 3659 | status = cleanup_status; |
| 3660 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3661 | return status; |
Janos Follath | 7742fee | 2019-06-17 12:58:10 +0100 | [diff] [blame] | 3662 | } |
Hanno Becker | c8a41d7 | 2018-10-09 17:33:01 +0100 | [diff] [blame] | 3663 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3664 | static psa_status_t |
| 3665 | psa_key_derivation_tls12_prf_read(psa_tls12_prf_key_derivation_t *tls12_prf, |
| 3666 | psa_algorithm_t alg, |
| 3667 | uint8_t *output, |
| 3668 | size_t output_length) |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3669 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3670 | psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg); |
| 3671 | uint8_t hash_length = PSA_HASH_LENGTH(hash_alg); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3672 | psa_status_t status; |
| 3673 | uint8_t offset, length; |
| 3674 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3675 | switch (tls12_prf->state) { |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 3676 | case PSA_TLS12_PRF_STATE_LABEL_SET: |
| 3677 | tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT; |
| 3678 | break; |
| 3679 | case PSA_TLS12_PRF_STATE_OUTPUT: |
| 3680 | break; |
| 3681 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3682 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b1edaec | 2021-06-11 22:41:46 +0200 | [diff] [blame] | 3683 | } |
| 3684 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3685 | while (output_length != 0) { |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3686 | /* Check if we have fully processed the current block. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3687 | if (tls12_prf->left_in_block == 0) { |
| 3688 | status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf, |
| 3689 | alg); |
| 3690 | if (status != PSA_SUCCESS) |
| 3691 | return status; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3692 | |
| 3693 | continue; |
| 3694 | } |
| 3695 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3696 | if (tls12_prf->left_in_block > output_length) |
| 3697 | length = (uint8_t)output_length; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3698 | else |
| 3699 | length = tls12_prf->left_in_block; |
| 3700 | |
| 3701 | offset = hash_length - tls12_prf->left_in_block; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3702 | memcpy(output, tls12_prf->output_block + offset, length); |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3703 | output += length; |
| 3704 | output_length -= length; |
| 3705 | tls12_prf->left_in_block -= length; |
| 3706 | } |
| 3707 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3708 | return PSA_SUCCESS; |
Janos Follath | 844eb0e | 2019-06-19 12:10:49 +0100 | [diff] [blame] | 3709 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3710 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || \ |
| 3711 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3712 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3713 | psa_status_t |
| 3714 | psa_key_derivation_output_bytes(psa_key_derivation_operation_t *operation, |
| 3715 | uint8_t *output, |
| 3716 | size_t output_length) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3717 | { |
| 3718 | psa_status_t status; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3719 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3720 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3721 | if (operation->alg == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3722 | /* This is a blank operation. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3723 | return PSA_ERROR_BAD_STATE; |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3726 | if (output_length > operation->capacity) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3727 | operation->capacity = 0; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3728 | /* Go through the error path to wipe all confidential data now |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3729 | * that the operation object is useless. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 3730 | status = PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3731 | goto exit; |
| 3732 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3733 | if (output_length == 0 && operation->capacity == 0) { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3734 | /* Edge case: this is a finished operation, and 0 bytes |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3735 | * were requested. The right error in this case could |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3736 | * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return |
| 3737 | * INSUFFICIENT_CAPACITY, which is right for a finished |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3738 | * operation, for consistency with the case when |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3739 | * output_length > 0. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3740 | return PSA_ERROR_INSUFFICIENT_DATA; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3741 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3742 | operation->capacity -= output_length; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3743 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3744 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 3745 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 3746 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 3747 | status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg, |
| 3748 | output, output_length); |
| 3749 | } else |
| 3750 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 3751 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 3752 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3753 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 3754 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 3755 | status = psa_key_derivation_tls12_prf_read( |
| 3756 | &operation->ctx.tls12_prf, kdf_alg, output, output_length); |
| 3757 | } else |
| 3758 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF || \ |
| 3759 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3760 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3761 | (void)kdf_alg; |
| 3762 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3766 | if (status != PSA_SUCCESS) { |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 3767 | /* Preserve the algorithm upon errors, but clear all sensitive state. |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3768 | * This allows us to differentiate between exhausted operations and |
| 3769 | * blank operations, so we can return PSA_ERROR_BAD_STATE on blank |
| 3770 | * operations. */ |
| 3771 | psa_algorithm_t alg = operation->alg; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3772 | psa_key_derivation_abort(operation); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3773 | operation->alg = alg; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3774 | memset(output, '!', output_length); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3775 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3776 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3777 | } |
| 3778 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3779 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
| 3780 | 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] | 3781 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3782 | if (data_size >= 8) |
| 3783 | mbedtls_des_key_set_parity(data); |
| 3784 | if (data_size >= 16) |
| 3785 | mbedtls_des_key_set_parity(data + 8); |
| 3786 | if (data_size >= 24) |
| 3787 | mbedtls_des_key_set_parity(data + 16); |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 3788 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3789 | # endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Gilles Peskine | 08542d8 | 2018-07-19 17:05:42 +0200 | [diff] [blame] | 3790 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3791 | static psa_status_t |
| 3792 | psa_generate_derived_key_internal(psa_key_slot_t *slot, |
| 3793 | size_t bits, |
| 3794 | psa_key_derivation_operation_t *operation) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3795 | { |
| 3796 | uint8_t *data = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3797 | size_t bytes = PSA_BITS_TO_BYTES(bits); |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3798 | psa_status_t status; |
| 3799 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3800 | if (!key_type_is_raw_bytes(slot->attr.type)) |
| 3801 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3802 | if (bits % 8 != 0) |
| 3803 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3804 | data = mbedtls_calloc(1, bytes); |
| 3805 | if (data == NULL) |
| 3806 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3807 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3808 | status = psa_key_derivation_output_bytes(operation, data, bytes); |
| 3809 | if (status != PSA_SUCCESS) |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3810 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3811 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
| 3812 | if (slot->attr.type == PSA_KEY_TYPE_DES) |
| 3813 | psa_des_set_key_parity(data, bytes); |
| 3814 | # endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 3815 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3816 | status = psa_allocate_buffer_to_slot(slot, bytes); |
| 3817 | if (status != PSA_SUCCESS) |
Paul Elliott | da3e7db | 2021-02-09 18:58:20 +0000 | [diff] [blame] | 3818 | goto exit; |
Ronald Cron | dd04d42 | 2020-11-28 15:14:42 +0100 | [diff] [blame] | 3819 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3820 | slot->attr.bits = (psa_key_bits_t)bits; |
| 3821 | psa_key_attributes_t attributes = { .core = slot->attr }; |
Ronald Cron | 2ebfdcc | 2020-11-28 15:54:54 +0100 | [diff] [blame] | 3822 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3823 | status = psa_driver_wrapper_import_key(&attributes, data, bytes, |
| 3824 | slot->key.data, slot->key.bytes, |
| 3825 | &slot->key.bytes, &bits); |
| 3826 | if (bits != slot->attr.bits) |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 3827 | status = PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3828 | |
| 3829 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3830 | mbedtls_free(data); |
| 3831 | return status; |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3832 | } |
| 3833 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3834 | psa_status_t |
| 3835 | psa_key_derivation_output_key(const psa_key_attributes_t *attributes, |
| 3836 | psa_key_derivation_operation_t *operation, |
| 3837 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 3838 | { |
| 3839 | psa_status_t status; |
| 3840 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 3841 | psa_se_drv_table_entry_t *driver = NULL; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 3842 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 3843 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3844 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 3845 | /* Reject any attempt to create a zero-length key so that we don't |
| 3846 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3847 | if (psa_get_key_bits(attributes) == 0) |
| 3848 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 3849 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3850 | if (!operation->can_output_key) |
| 3851 | return PSA_ERROR_NOT_PERMITTED; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 3852 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3853 | status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes, &slot, |
| 3854 | &driver); |
| 3855 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 3856 | if (driver != NULL) { |
Gilles Peskine | f4ee662 | 2019-07-24 13:44:30 +0200 | [diff] [blame] | 3857 | /* Deriving a key in a secure element is not implemented yet. */ |
| 3858 | status = PSA_ERROR_NOT_SUPPORTED; |
| 3859 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3860 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 3861 | if (status == PSA_SUCCESS) { |
| 3862 | status = psa_generate_derived_key_internal(slot, attributes->core.bits, |
| 3863 | operation); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 3864 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3865 | if (status == PSA_SUCCESS) |
| 3866 | status = psa_finish_key_creation(slot, driver, key); |
| 3867 | if (status != PSA_SUCCESS) |
| 3868 | psa_fail_key_creation(slot, driver); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 3869 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3870 | return status; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 3871 | } |
| 3872 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3873 | /****************************************************************/ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3874 | /* Key derivation */ |
| 3875 | /****************************************************************/ |
| 3876 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3877 | # if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 3878 | static psa_status_t |
| 3879 | psa_key_derivation_setup_kdf(psa_key_derivation_operation_t *operation, |
| 3880 | psa_algorithm_t kdf_alg) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3881 | { |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3882 | int is_kdf_alg_supported; |
| 3883 | |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 3884 | /* Make sure that operation->ctx is properly zero-initialised. (Macro |
| 3885 | * initialisers for this union leave some bytes unspecified.) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3886 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
Janos Follath | 5fe1973 | 2019-06-20 15:09:30 +0100 | [diff] [blame] | 3887 | |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3888 | /* Make sure that kdf_alg is a supported key derivation algorithm. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3889 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 3890 | if (PSA_ALG_IS_HKDF(kdf_alg)) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3891 | is_kdf_alg_supported = 1; |
| 3892 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3893 | # endif |
| 3894 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 3895 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3896 | is_kdf_alg_supported = 1; |
| 3897 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3898 | # endif |
| 3899 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 3900 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3901 | is_kdf_alg_supported = 1; |
| 3902 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3903 | # endif |
| 3904 | is_kdf_alg_supported = 0; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3905 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3906 | if (is_kdf_alg_supported) { |
| 3907 | psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg); |
| 3908 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 3909 | if (hash_size == 0) |
| 3910 | return PSA_ERROR_NOT_SUPPORTED; |
| 3911 | if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) || |
| 3912 | PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) && |
| 3913 | !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) { |
| 3914 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3915 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3916 | operation->capacity = 255 * hash_size; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3917 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3918 | } |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 3919 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3920 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3921 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3922 | # endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3923 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3924 | psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation, |
| 3925 | psa_algorithm_t alg) |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3926 | { |
| 3927 | psa_status_t status; |
| 3928 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3929 | if (operation->alg != 0) |
| 3930 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3931 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3932 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) |
| 3933 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3934 | else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
| 3935 | # if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 3936 | psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg); |
| 3937 | status = psa_key_derivation_setup_kdf(operation, kdf_alg); |
| 3938 | # else |
| 3939 | return PSA_ERROR_NOT_SUPPORTED; |
| 3940 | # endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 3941 | } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) { |
| 3942 | # if defined(AT_LEAST_ONE_BUILTIN_KDF) |
| 3943 | status = psa_key_derivation_setup_kdf(operation, alg); |
| 3944 | # else |
| 3945 | return PSA_ERROR_NOT_SUPPORTED; |
| 3946 | # endif /* AT_LEAST_ONE_BUILTIN_KDF */ |
| 3947 | } else |
| 3948 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 969c5d6 | 2019-01-16 15:53:06 +0100 | [diff] [blame] | 3949 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3950 | if (status == PSA_SUCCESS) |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 3951 | operation->alg = alg; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3952 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3953 | } |
| 3954 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3955 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 3956 | static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf, |
| 3957 | psa_algorithm_t hash_alg, |
| 3958 | psa_key_derivation_step_t step, |
| 3959 | const uint8_t *data, |
| 3960 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3961 | { |
| 3962 | psa_status_t status; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3963 | switch (step) { |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 3964 | case PSA_KEY_DERIVATION_INPUT_SALT: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3965 | if (hkdf->state != HKDF_STATE_INIT) |
| 3966 | return PSA_ERROR_BAD_STATE; |
| 3967 | else { |
| 3968 | status = psa_key_derivation_start_hmac(&hkdf->hmac, hash_alg, |
| 3969 | data, data_length); |
| 3970 | if (status != PSA_SUCCESS) |
| 3971 | return status; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3972 | hkdf->state = HKDF_STATE_STARTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3973 | return PSA_SUCCESS; |
Steven Cooreman | d1ed1d9 | 2021-04-29 19:11:25 +0200 | [diff] [blame] | 3974 | } |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 3975 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3976 | /* If no salt was provided, use an empty salt. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3977 | if (hkdf->state == HKDF_STATE_INIT) { |
| 3978 | status = psa_key_derivation_start_hmac(&hkdf->hmac, hash_alg, |
| 3979 | NULL, 0); |
| 3980 | if (status != PSA_SUCCESS) |
| 3981 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 3982 | hkdf->state = HKDF_STATE_STARTED; |
| 3983 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3984 | if (hkdf->state != HKDF_STATE_STARTED) |
| 3985 | return PSA_ERROR_BAD_STATE; |
| 3986 | status = psa_mac_update(&hkdf->hmac, data, data_length); |
| 3987 | if (status != PSA_SUCCESS) |
| 3988 | return status; |
| 3989 | status = psa_mac_sign_finish(&hkdf->hmac, hkdf->prk, |
| 3990 | sizeof(hkdf->prk), &data_length); |
| 3991 | if (status != PSA_SUCCESS) |
| 3992 | return status; |
| 3993 | hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg); |
Gilles Peskine | 2b522db | 2019-04-12 15:11:49 +0200 | [diff] [blame] | 3994 | hkdf->block_number = 0; |
| 3995 | hkdf->state = HKDF_STATE_KEYED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3996 | return PSA_SUCCESS; |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 3997 | case PSA_KEY_DERIVATION_INPUT_INFO: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3998 | if (hkdf->state == HKDF_STATE_OUTPUT) |
| 3999 | return PSA_ERROR_BAD_STATE; |
| 4000 | if (hkdf->info_set) |
| 4001 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4002 | hkdf->info_length = data_length; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4003 | if (data_length != 0) { |
| 4004 | hkdf->info = mbedtls_calloc(1, data_length); |
| 4005 | if (hkdf->info == NULL) |
| 4006 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 4007 | memcpy(hkdf->info, data, data_length); |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4008 | } |
| 4009 | hkdf->info_set = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4010 | return PSA_SUCCESS; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4011 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4012 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4013 | } |
| 4014 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4015 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4016 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4017 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4018 | defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4019 | static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf, |
| 4020 | const uint8_t *data, |
| 4021 | size_t data_length) |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4022 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4023 | if (prf->state != PSA_TLS12_PRF_STATE_INIT) |
| 4024 | return PSA_ERROR_BAD_STATE; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4025 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4026 | if (data_length != 0) { |
| 4027 | prf->seed = mbedtls_calloc(1, data_length); |
| 4028 | if (prf->seed == NULL) |
| 4029 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4030 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4031 | memcpy(prf->seed, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4032 | prf->seed_length = data_length; |
| 4033 | } |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4034 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4035 | prf->state = PSA_TLS12_PRF_STATE_SEED_SET; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4036 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4037 | return PSA_SUCCESS; |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4038 | } |
| 4039 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4040 | static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 4041 | const uint8_t *data, |
| 4042 | size_t data_length) |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4043 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4044 | if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) |
| 4045 | return PSA_ERROR_BAD_STATE; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4046 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4047 | if (data_length != 0) { |
| 4048 | prf->secret = mbedtls_calloc(1, data_length); |
| 4049 | if (prf->secret == NULL) |
| 4050 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4051 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4052 | memcpy(prf->secret, data, data_length); |
Steven Cooreman | a6df604 | 2021-04-29 19:32:25 +0200 | [diff] [blame] | 4053 | prf->secret_length = data_length; |
| 4054 | } |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4055 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4056 | prf->state = PSA_TLS12_PRF_STATE_KEY_SET; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4057 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4058 | return PSA_SUCCESS; |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4059 | } |
| 4060 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4061 | static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf, |
| 4062 | const uint8_t *data, |
| 4063 | size_t data_length) |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4064 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4065 | if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) |
| 4066 | return PSA_ERROR_BAD_STATE; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4067 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4068 | if (data_length != 0) { |
| 4069 | prf->label = mbedtls_calloc(1, data_length); |
| 4070 | if (prf->label == NULL) |
| 4071 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4072 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4073 | memcpy(prf->label, data, data_length); |
Janos Follath | d6dce9f | 2019-07-04 09:11:38 +0100 | [diff] [blame] | 4074 | prf->label_length = data_length; |
| 4075 | } |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4076 | |
Gilles Peskine | 43f958b | 2020-12-13 14:55:14 +0100 | [diff] [blame] | 4077 | prf->state = PSA_TLS12_PRF_STATE_LABEL_SET; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4078 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4079 | return PSA_SUCCESS; |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4080 | } |
| 4081 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4082 | static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf, |
| 4083 | psa_key_derivation_step_t step, |
| 4084 | const uint8_t *data, |
| 4085 | size_t data_length) |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4086 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4087 | switch (step) { |
Janos Follath | f08e265 | 2019-06-13 09:05:41 +0100 | [diff] [blame] | 4088 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4089 | return psa_tls12_prf_set_seed(prf, data, data_length); |
Janos Follath | 8155054 | 2019-06-13 14:26:34 +0100 | [diff] [blame] | 4090 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4091 | return psa_tls12_prf_set_key(prf, data, data_length); |
Janos Follath | 63028dd | 2019-06-13 09:15:47 +0100 | [diff] [blame] | 4092 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4093 | return psa_tls12_prf_set_label(prf, data, data_length); |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4094 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4095 | return PSA_ERROR_INVALID_ARGUMENT; |
Janos Follath | b03233e | 2019-06-11 15:30:30 +0100 | [diff] [blame] | 4096 | } |
| 4097 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4098 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \ |
| 4099 | * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4100 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4101 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4102 | static psa_status_t |
| 4103 | psa_tls12_prf_psk_to_ms_set_key(psa_tls12_prf_key_derivation_t *prf, |
| 4104 | const uint8_t *data, |
| 4105 | size_t data_length) |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4106 | { |
| 4107 | psa_status_t status; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4108 | 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] | 4109 | uint8_t *cur = pms; |
| 4110 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4111 | if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) |
| 4112 | return PSA_ERROR_INVALID_ARGUMENT; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4113 | |
| 4114 | /* Quoting RFC 4279, Section 2: |
| 4115 | * |
| 4116 | * The premaster secret is formed as follows: if the PSK is N octets |
| 4117 | * long, concatenate a uint16 with the value N, N zero octets, a second |
| 4118 | * uint16 with the value N, and the PSK itself. |
| 4119 | */ |
| 4120 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4121 | *cur++ = (data_length >> 8) & 0xff; |
| 4122 | *cur++ = (data_length >> 0) & 0xff; |
| 4123 | memset(cur, 0, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4124 | cur += data_length; |
| 4125 | *cur++ = pms[0]; |
| 4126 | *cur++ = pms[1]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4127 | memcpy(cur, data, data_length); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4128 | cur += data_length; |
| 4129 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4130 | status = psa_tls12_prf_set_key(prf, pms, cur - pms); |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4131 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4132 | mbedtls_platform_zeroize(pms, sizeof(pms)); |
| 4133 | return status; |
John Durkop | 07cc04a | 2020-11-16 22:08:34 -0800 | [diff] [blame] | 4134 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4135 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4136 | static psa_status_t |
| 4137 | psa_tls12_prf_psk_to_ms_input(psa_tls12_prf_key_derivation_t *prf, |
| 4138 | psa_key_derivation_step_t step, |
| 4139 | const uint8_t *data, |
| 4140 | size_t data_length) |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4141 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4142 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) { |
| 4143 | return (psa_tls12_prf_psk_to_ms_set_key(prf, data, data_length)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 4144 | } |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4145 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4146 | return psa_tls12_prf_input(prf, step, data, data_length); |
Janos Follath | 6660f0e | 2019-06-17 08:44:03 +0100 | [diff] [blame] | 4147 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4148 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4149 | |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4150 | /** Check whether the given key type is acceptable for the given |
| 4151 | * input step of a key derivation. |
| 4152 | * |
| 4153 | * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE. |
| 4154 | * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA. |
| 4155 | * Both secret and non-secret inputs can alternatively have the type |
| 4156 | * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning |
| 4157 | * that the input was passed as a buffer rather than via a key object. |
| 4158 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4159 | static int psa_key_derivation_check_input_type(psa_key_derivation_step_t step, |
| 4160 | psa_key_type_t key_type) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4161 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4162 | switch (step) { |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4163 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4164 | if (key_type == PSA_KEY_TYPE_DERIVE) |
| 4165 | return PSA_SUCCESS; |
| 4166 | if (key_type == PSA_KEY_TYPE_NONE) |
| 4167 | return PSA_SUCCESS; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4168 | break; |
| 4169 | case PSA_KEY_DERIVATION_INPUT_LABEL: |
| 4170 | case PSA_KEY_DERIVATION_INPUT_SALT: |
| 4171 | case PSA_KEY_DERIVATION_INPUT_INFO: |
| 4172 | case PSA_KEY_DERIVATION_INPUT_SEED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4173 | if (key_type == PSA_KEY_TYPE_RAW_DATA) |
| 4174 | return PSA_SUCCESS; |
| 4175 | if (key_type == PSA_KEY_TYPE_NONE) |
| 4176 | return PSA_SUCCESS; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4177 | break; |
| 4178 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4179 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4180 | } |
| 4181 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4182 | static psa_status_t |
| 4183 | psa_key_derivation_input_internal(psa_key_derivation_operation_t *operation, |
| 4184 | psa_key_derivation_step_t step, |
| 4185 | psa_key_type_t key_type, |
| 4186 | const uint8_t *data, |
| 4187 | size_t data_length) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4188 | { |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 4189 | psa_status_t status; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4190 | psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation); |
Gilles Peskine | 46d7faf | 2019-09-23 19:22:55 +0200 | [diff] [blame] | 4191 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4192 | status = psa_key_derivation_check_input_type(step, key_type); |
| 4193 | if (status != PSA_SUCCESS) |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4194 | goto exit; |
| 4195 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4196 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) |
| 4197 | if (PSA_ALG_IS_HKDF(kdf_alg)) { |
| 4198 | status = psa_hkdf_input(&operation->ctx.hkdf, |
| 4199 | PSA_ALG_HKDF_GET_HASH(kdf_alg), step, data, |
| 4200 | data_length); |
| 4201 | } else |
| 4202 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ |
| 4203 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) |
| 4204 | if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) { |
| 4205 | status = psa_tls12_prf_input(&operation->ctx.tls12_prf, step, data, |
| 4206 | data_length); |
| 4207 | } else |
| 4208 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */ |
| 4209 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) |
| 4210 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) { |
| 4211 | status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf, step, |
| 4212 | data, data_length); |
| 4213 | } else |
| 4214 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4215 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4216 | /* This can't happen unless the operation object was not initialized */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4217 | (void)data; |
| 4218 | (void)data_length; |
| 4219 | (void)kdf_alg; |
| 4220 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4221 | } |
| 4222 | |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4223 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4224 | if (status != PSA_SUCCESS) |
| 4225 | psa_key_derivation_abort(operation); |
| 4226 | return status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4227 | } |
| 4228 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4229 | psa_status_t |
| 4230 | psa_key_derivation_input_bytes(psa_key_derivation_operation_t *operation, |
| 4231 | psa_key_derivation_step_t step, |
| 4232 | const uint8_t *data, |
| 4233 | size_t data_length) |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4234 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4235 | return (psa_key_derivation_input_internal( |
| 4236 | operation, step, PSA_KEY_TYPE_NONE, data, data_length)); |
Gilles Peskine | 6cdfdb7 | 2019-01-08 10:31:27 +0100 | [diff] [blame] | 4237 | } |
| 4238 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4239 | psa_status_t |
| 4240 | psa_key_derivation_input_key(psa_key_derivation_operation_t *operation, |
| 4241 | psa_key_derivation_step_t step, |
| 4242 | mbedtls_svc_key_id_t key) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4243 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4244 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4245 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4246 | psa_key_slot_t *slot; |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4247 | |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4248 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4249 | key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 4250 | if (status != PSA_SUCCESS) { |
| 4251 | psa_key_derivation_abort(operation); |
| 4252 | return status; |
Gilles Peskine | 593773d | 2019-09-23 18:17:40 +0200 | [diff] [blame] | 4253 | } |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4254 | |
| 4255 | /* Passing a key object as a SECRET input unlocks the permission |
| 4256 | * to output to a key object. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4257 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) |
Gilles Peskine | 178c9aa | 2019-09-24 18:21:06 +0200 | [diff] [blame] | 4258 | operation->can_output_key = 1; |
| 4259 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4260 | status = psa_key_derivation_input_internal(operation, step, slot->attr.type, |
| 4261 | slot->key.data, slot->key.bytes); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4262 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4263 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4264 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4265 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4266 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4267 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4268 | /****************************************************************/ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4269 | /* Key agreement */ |
| 4270 | /****************************************************************/ |
| 4271 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4272 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
| 4273 | static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key, |
| 4274 | size_t peer_key_length, |
| 4275 | const mbedtls_ecp_keypair *our_key, |
| 4276 | uint8_t *shared_secret, |
| 4277 | size_t shared_secret_size, |
| 4278 | size_t *shared_secret_length) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4279 | { |
Steven Cooreman | d486787 | 2020-08-05 16:31:39 +0200 | [diff] [blame] | 4280 | mbedtls_ecp_keypair *their_key = NULL; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4281 | mbedtls_ecdh_context ecdh; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4282 | psa_status_t status; |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4283 | size_t bits = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4284 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits); |
| 4285 | mbedtls_ecdh_init(&ecdh); |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4286 | |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4287 | status = mbedtls_psa_ecp_load_representation( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4288 | PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve), bits, peer_key, peer_key_length, |
| 4289 | &their_key); |
| 4290 | if (status != PSA_SUCCESS) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4291 | goto exit; |
| 4292 | |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4293 | status = mbedtls_to_psa_error( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4294 | mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS)); |
| 4295 | if (status != PSA_SUCCESS) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4296 | goto exit; |
Jaeden Amero | 97271b3 | 2019-01-10 19:38:51 +0000 | [diff] [blame] | 4297 | status = mbedtls_to_psa_error( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4298 | mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS)); |
| 4299 | if (status != PSA_SUCCESS) |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4300 | goto exit; |
| 4301 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4302 | status = mbedtls_to_psa_error(mbedtls_ecdh_calc_secret( |
| 4303 | &ecdh, shared_secret_length, shared_secret, shared_secret_size, |
| 4304 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE)); |
| 4305 | if (status != PSA_SUCCESS) |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4306 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4307 | if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) |
Gilles Peskine | c7ef5b3 | 2019-12-12 16:58:00 +0100 | [diff] [blame] | 4308 | status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4309 | |
| 4310 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4311 | if (status != PSA_SUCCESS) |
| 4312 | mbedtls_platform_zeroize(shared_secret, shared_secret_size); |
| 4313 | mbedtls_ecdh_free(&ecdh); |
| 4314 | mbedtls_ecp_keypair_free(their_key); |
| 4315 | mbedtls_free(their_key); |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4316 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4317 | return status; |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4318 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4319 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | b7ecdf0 | 2018-09-18 12:11:27 +0200 | [diff] [blame] | 4320 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4321 | # define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4322 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4323 | static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg, |
| 4324 | psa_key_slot_t *private_key, |
| 4325 | const uint8_t *peer_key, |
| 4326 | size_t peer_key_length, |
| 4327 | uint8_t *shared_secret, |
| 4328 | size_t shared_secret_size, |
| 4329 | size_t *shared_secret_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4330 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4331 | switch (alg) { |
| 4332 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4333 | case PSA_ALG_ECDH: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4334 | if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) |
| 4335 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | a2371e5 | 2020-07-28 14:30:39 +0200 | [diff] [blame] | 4336 | mbedtls_ecp_keypair *ecp = NULL; |
Ronald Cron | 9085708 | 2020-11-25 14:58:33 +0100 | [diff] [blame] | 4337 | psa_status_t status = mbedtls_psa_ecp_load_representation( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4338 | private_key->attr.type, private_key->attr.bits, |
| 4339 | private_key->key.data, private_key->key.bytes, &ecp); |
| 4340 | if (status != PSA_SUCCESS) |
| 4341 | return status; |
| 4342 | status = psa_key_agreement_ecdh(peer_key, peer_key_length, ecp, |
| 4343 | shared_secret, shared_secret_size, |
| 4344 | shared_secret_length); |
| 4345 | mbedtls_ecp_keypair_free(ecp); |
| 4346 | mbedtls_free(ecp); |
| 4347 | return status; |
| 4348 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4349 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4350 | (void)private_key; |
| 4351 | (void)peer_key; |
| 4352 | (void)peer_key_length; |
| 4353 | (void)shared_secret; |
| 4354 | (void)shared_secret_size; |
| 4355 | (void)shared_secret_length; |
| 4356 | return PSA_ERROR_NOT_SUPPORTED; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4357 | } |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4358 | } |
| 4359 | |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4360 | /* 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] | 4361 | * to potentially free embedded data structures and wipe confidential data. |
| 4362 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4363 | static psa_status_t |
| 4364 | psa_key_agreement_internal(psa_key_derivation_operation_t *operation, |
| 4365 | psa_key_derivation_step_t step, |
| 4366 | psa_key_slot_t *private_key, |
| 4367 | const uint8_t *peer_key, |
| 4368 | size_t peer_key_length) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4369 | { |
| 4370 | psa_status_t status; |
| 4371 | uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE]; |
| 4372 | size_t shared_secret_length = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4373 | 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] | 4374 | |
| 4375 | /* Step 1: run the secret agreement algorithm to generate the shared |
| 4376 | * secret. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4377 | status = psa_key_agreement_raw_internal(ka_alg, private_key, peer_key, |
| 4378 | peer_key_length, shared_secret, |
| 4379 | sizeof(shared_secret), |
| 4380 | &shared_secret_length); |
| 4381 | if (status != PSA_SUCCESS) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4382 | goto exit; |
| 4383 | |
Gilles Peskine | b0b255c | 2018-07-06 17:01:38 +0200 | [diff] [blame] | 4384 | /* Step 2: set up the key derivation to generate key material from |
Gilles Peskine | 224b0d6 | 2019-09-23 18:13:17 +0200 | [diff] [blame] | 4385 | * the shared secret. A shared secret is permitted wherever a key |
| 4386 | * of type DERIVE is permitted. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4387 | status = |
| 4388 | psa_key_derivation_input_internal(operation, step, PSA_KEY_TYPE_DERIVE, |
| 4389 | shared_secret, shared_secret_length); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4390 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4391 | mbedtls_platform_zeroize(shared_secret, shared_secret_length); |
| 4392 | return status; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4393 | } |
| 4394 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4395 | psa_status_t |
| 4396 | psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation, |
| 4397 | psa_key_derivation_step_t step, |
| 4398 | mbedtls_svc_key_id_t private_key, |
| 4399 | const uint8_t *peer_key, |
| 4400 | size_t peer_key_length) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4401 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4402 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4403 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 2f060a8 | 2018-12-04 17:12:32 +0100 | [diff] [blame] | 4404 | psa_key_slot_t *slot; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4405 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4406 | if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) |
| 4407 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4408 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4409 | private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg); |
| 4410 | if (status != PSA_SUCCESS) |
| 4411 | return status; |
| 4412 | status = psa_key_agreement_internal(operation, step, slot, peer_key, |
| 4413 | peer_key_length); |
| 4414 | if (status != PSA_SUCCESS) |
| 4415 | psa_key_derivation_abort(operation); |
| 4416 | else { |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4417 | /* If a private key has been added as SECRET, we allow the derived |
| 4418 | * key material to be used as a key in PSA Crypto. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4419 | if (step == PSA_KEY_DERIVATION_INPUT_SECRET) |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4420 | operation->can_output_key = 1; |
| 4421 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4422 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4423 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4424 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4425 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 4426 | } |
| 4427 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4428 | psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, |
| 4429 | mbedtls_svc_key_id_t private_key, |
| 4430 | const uint8_t *peer_key, |
| 4431 | size_t peer_key_length, |
| 4432 | uint8_t *output, |
| 4433 | size_t output_size, |
| 4434 | size_t *output_length) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4435 | { |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4436 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4437 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4438 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4439 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4440 | if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4441 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 4442 | goto exit; |
| 4443 | } |
Ronald Cron | 5c52292 | 2020-11-14 16:35:34 +0100 | [diff] [blame] | 4444 | status = psa_get_and_lock_transparent_key_slot_with_policy( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4445 | private_key, &slot, PSA_KEY_USAGE_DERIVE, alg); |
| 4446 | if (status != PSA_SUCCESS) |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4447 | goto exit; |
| 4448 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4449 | status = psa_key_agreement_raw_internal(alg, slot, peer_key, |
| 4450 | peer_key_length, output, |
| 4451 | output_size, output_length); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4452 | |
| 4453 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4454 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4455 | /* If an error happens and is not handled properly, the output |
| 4456 | * may be used as a key to protect sensitive data. Arrange for such |
| 4457 | * a key to be random, which is likely to result in decryption or |
| 4458 | * verification errors. This is better than filling the buffer with |
| 4459 | * some constant data such as zeros, which would result in the data |
| 4460 | * being protected with a reproducible, easily knowable key. |
| 4461 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4462 | psa_generate_random(output, output_size); |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4463 | *output_length = output_size; |
| 4464 | } |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4465 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4466 | unlock_status = psa_unlock_key_slot(slot); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4467 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4468 | return (status == PSA_SUCCESS) ? unlock_status : status; |
Gilles Peskine | 0216fe1 | 2019-04-11 21:23:21 +0200 | [diff] [blame] | 4469 | } |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 4470 | |
Gilles Peskine | 86a440b | 2018-11-12 18:39:40 +0100 | [diff] [blame] | 4471 | /****************************************************************/ |
| 4472 | /* Random generation */ |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 4473 | /****************************************************************/ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4474 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4475 | /** Initialize the PSA random generator. |
| 4476 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4477 | static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4478 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4479 | # if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4480 | memset(rng, 0, sizeof(*rng)); |
| 4481 | # else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4482 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4483 | /* Set default configuration if |
| 4484 | * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4485 | if (rng->entropy_init == NULL) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4486 | rng->entropy_init = mbedtls_entropy_init; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4487 | if (rng->entropy_free == NULL) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4488 | rng->entropy_free = mbedtls_entropy_free; |
| 4489 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4490 | rng->entropy_init(&rng->entropy); |
| 4491 | # if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \ |
| 4492 | defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4493 | /* The PSA entropy injection feature depends on using NV seed as an entropy |
| 4494 | * source. Add NV seed as an entropy source for PSA entropy injection. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4495 | mbedtls_entropy_add_source(&rng->entropy, mbedtls_nv_seed_poll, NULL, |
| 4496 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 4497 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
| 4498 | # endif |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4499 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4500 | mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE); |
| 4501 | # endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | /** Deinitialize the PSA random generator. |
| 4505 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4506 | static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng) |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4507 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4508 | # if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4509 | memset(rng, 0, sizeof(*rng)); |
| 4510 | # else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 4511 | mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE); |
| 4512 | rng->entropy_free(&rng->entropy); |
| 4513 | # endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | /** Seed the PSA random generator. |
| 4517 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4518 | 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] | 4519 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4520 | # if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4521 | /* Do nothing: the external RNG seeds itself. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4522 | (void)rng; |
| 4523 | return PSA_SUCCESS; |
| 4524 | # else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4525 | const unsigned char drbg_seed[] = "PSA"; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4526 | int ret = |
| 4527 | mbedtls_psa_drbg_seed(&rng->entropy, drbg_seed, sizeof(drbg_seed) - 1); |
| 4528 | return mbedtls_to_psa_error(ret); |
| 4529 | # endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4530 | } |
| 4531 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4532 | psa_status_t psa_generate_random(uint8_t *output, size_t output_size) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4533 | { |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4534 | GUARD_MODULE_INITIALIZED; |
| 4535 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4536 | # if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4537 | |
| 4538 | size_t output_length = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4539 | psa_status_t status = mbedtls_psa_external_get_random( |
| 4540 | &global_data.rng, output, output_size, &output_length); |
| 4541 | if (status != PSA_SUCCESS) |
| 4542 | return status; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4543 | /* Breaking up a request into smaller chunks is currently not supported |
| 4544 | * for the extrernal RNG interface. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4545 | if (output_length != output_size) |
| 4546 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 4547 | return PSA_SUCCESS; |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4548 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4549 | # else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame] | 4550 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4551 | while (output_size > 0) { |
| 4552 | size_t request_size = (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? |
| 4553 | MBEDTLS_PSA_RANDOM_MAX_REQUEST : |
| 4554 | output_size); |
| 4555 | int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, output, |
| 4556 | request_size); |
| 4557 | if (ret != 0) |
| 4558 | return mbedtls_to_psa_error(ret); |
Gilles Peskine | 71ddab9 | 2021-01-04 21:01:07 +0100 | [diff] [blame] | 4559 | output_size -= request_size; |
| 4560 | output += request_size; |
Gilles Peskine | f181eca | 2019-08-07 13:49:00 +0200 | [diff] [blame] | 4561 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4562 | return PSA_SUCCESS; |
| 4563 | # endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4564 | } |
| 4565 | |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 4566 | /* Wrapper function allowing the classic API to use the PSA RNG. |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 4567 | * |
| 4568 | * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls |
| 4569 | * `psa_generate_random(...)`. The state parameter is ignored since the |
| 4570 | * PSA API doesn't support passing an explicit state. |
| 4571 | * |
| 4572 | * In the non-external case, psa_generate_random() calls an |
| 4573 | * `mbedtls_xxx_drbg_random` function which has exactly the same signature |
| 4574 | * and semantics as mbedtls_psa_get_random(). As an optimization, |
| 4575 | * instead of doing this back-and-forth between the PSA API and the |
| 4576 | * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random` |
| 4577 | * as a constant function pointer to `mbedtls_xxx_drbg_random`. |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 4578 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4579 | # if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 4580 | int mbedtls_psa_get_random(void *p_rng, |
| 4581 | unsigned char *output, |
| 4582 | size_t output_size) |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 4583 | { |
Gilles Peskine | 9c3e060 | 2021-01-05 16:03:55 +0100 | [diff] [blame] | 4584 | /* This function takes a pointer to the RNG state because that's what |
| 4585 | * classic mbedtls functions using an RNG expect. The PSA RNG manages |
| 4586 | * its own state internally and doesn't let the caller access that state. |
| 4587 | * So we just ignore the state parameter, and in practice we'll pass |
| 4588 | * NULL. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4589 | (void)p_rng; |
| 4590 | psa_status_t status = psa_generate_random(output, output_size); |
| 4591 | if (status == PSA_SUCCESS) |
| 4592 | return 0; |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 4593 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4594 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 4595 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4596 | # endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Gilles Peskine | 8814fc4 | 2020-12-14 15:33:44 +0100 | [diff] [blame] | 4597 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4598 | # if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 4599 | # include "entropy_poll.h" |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 4600 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4601 | psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, size_t seed_size) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4602 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4603 | if (global_data.initialized) |
| 4604 | return PSA_ERROR_NOT_PERMITTED; |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 4605 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4606 | if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) || |
| 4607 | (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) || |
| 4608 | (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) |
| 4609 | return PSA_ERROR_INVALID_ARGUMENT; |
Netanel Gonen | 21f37cb | 2018-11-19 11:53:55 +0200 | [diff] [blame] | 4610 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4611 | return mbedtls_psa_storage_inject_entropy(seed, seed_size); |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4612 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4613 | # endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 4614 | |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 4615 | /** Validate the key type and size for key generation |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4616 | * |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 4617 | * \param type The key type |
| 4618 | * \param bits The number of bits of the key |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4619 | * |
| 4620 | * \retval #PSA_SUCCESS |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 4621 | * The key type and size are valid. |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4622 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 4623 | * The size in bits of the key is not valid. |
| 4624 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 4625 | * The type and/or the size in bits of the key or the combination of |
| 4626 | * the two is not supported. |
| 4627 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4628 | static psa_status_t |
| 4629 | psa_validate_key_type_and_size_for_key_generation(psa_key_type_t type, |
| 4630 | size_t bits) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4631 | { |
Ronald Cron | f3bb761 | 2020-10-02 20:11:59 +0200 | [diff] [blame] | 4632 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4633 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4634 | if (key_type_is_raw_bytes(type)) { |
| 4635 | status = validate_unstructured_key_bit_size(type, bits); |
| 4636 | if (status != PSA_SUCCESS) |
| 4637 | return status; |
| 4638 | } else |
| 4639 | # if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
| 4640 | if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 4641 | if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) |
| 4642 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 4643 | |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4644 | /* Accept only byte-aligned keys, for the same reasons as |
| 4645 | * in psa_import_rsa_key(). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4646 | if (bits % 8 != 0) |
| 4647 | return PSA_ERROR_NOT_SUPPORTED; |
| 4648 | } else |
| 4649 | # endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4650 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4651 | # if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) |
| 4652 | 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] | 4653 | /* To avoid empty block, return successfully here. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4654 | return PSA_SUCCESS; |
| 4655 | } else |
| 4656 | # endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */ |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4657 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4658 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4659 | } |
| 4660 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4661 | return PSA_SUCCESS; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4662 | } |
| 4663 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4664 | psa_status_t psa_generate_key_internal(const psa_key_attributes_t *attributes, |
| 4665 | uint8_t *key_buffer, |
| 4666 | size_t key_buffer_size, |
| 4667 | size_t *key_buffer_length) |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4668 | { |
| 4669 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 4670 | psa_key_type_t type = attributes->core.type; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4671 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4672 | if ((attributes->domain_parameters == NULL) && |
| 4673 | (attributes->domain_parameters_size != 0)) |
| 4674 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 01b2aba | 2020-10-05 09:42:02 +0200 | [diff] [blame] | 4675 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4676 | if (key_type_is_raw_bytes(type)) { |
| 4677 | status = psa_generate_random(key_buffer, key_buffer_size); |
| 4678 | if (status != PSA_SUCCESS) |
| 4679 | return status; |
Steven Cooreman | 81be2fa | 2020-07-24 22:04:59 +0200 | [diff] [blame] | 4680 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4681 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
| 4682 | if (type == PSA_KEY_TYPE_DES) |
| 4683 | psa_des_set_key_parity(key_buffer, key_buffer_size); |
| 4684 | # endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */ |
| 4685 | } else |
| 4686 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 4687 | defined(MBEDTLS_GENPRIME) |
| 4688 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 4689 | return (mbedtls_psa_rsa_generate_key( |
| 4690 | attributes, key_buffer, key_buffer_size, key_buffer_length)); |
| 4691 | } else |
| 4692 | # endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) \ |
| 4693 | * defined(MBEDTLS_GENPRIME) */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4694 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4695 | # if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) |
| 4696 | if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 4697 | return (mbedtls_psa_ecp_generate_key( |
| 4698 | attributes, key_buffer, key_buffer_size, key_buffer_length)); |
| 4699 | } else |
| 4700 | # endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */ |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4701 | { |
Ronald Cron | 2a38a6b | 2020-10-02 20:02:04 +0200 | [diff] [blame] | 4702 | (void)key_buffer_length; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4703 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 2914986 | 2020-08-05 15:43:42 +0200 | [diff] [blame] | 4704 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4705 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4706 | return PSA_SUCCESS; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4707 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4708 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4709 | psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, |
| 4710 | mbedtls_svc_key_id_t *key) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4711 | { |
| 4712 | psa_status_t status; |
| 4713 | psa_key_slot_t *slot = NULL; |
Gilles Peskine | 8abe6a2 | 2019-07-12 23:40:35 +0200 | [diff] [blame] | 4714 | psa_se_drv_table_entry_t *driver = NULL; |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 4715 | size_t key_buffer_size; |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 4716 | |
Ronald Cron | 81709fc | 2020-11-14 12:10:32 +0100 | [diff] [blame] | 4717 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4718 | |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4719 | /* Reject any attempt to create a zero-length key so that we don't |
| 4720 | * risk tripping up later, e.g. on a malloc(0) that returns NULL. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4721 | if (psa_get_key_bits(attributes) == 0) |
| 4722 | return PSA_ERROR_INVALID_ARGUMENT; |
Gilles Peskine | 0f84d62 | 2019-09-12 19:03:13 +0200 | [diff] [blame] | 4723 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4724 | status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes, |
| 4725 | &slot, &driver); |
| 4726 | if (status != PSA_SUCCESS) |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 4727 | goto exit; |
| 4728 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 4729 | /* In the case of a transparent key or an opaque key stored in local |
| 4730 | * storage (thus not in the case of generating a key in a secure element |
| 4731 | * or cryptoprocessor with storage), we have to allocate a buffer to |
| 4732 | * hold the generated key material. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4733 | if (slot->key.data == NULL) { |
| 4734 | if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) == |
| 4735 | PSA_KEY_LOCATION_LOCAL_STORAGE) { |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 4736 | status = psa_validate_key_type_and_size_for_key_generation( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4737 | attributes->core.type, attributes->core.bits); |
| 4738 | if (status != PSA_SUCCESS) |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 4739 | goto exit; |
| 4740 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4741 | key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(attributes->core.type, |
| 4742 | attributes->core.bits); |
| 4743 | } else { |
| 4744 | status = psa_driver_wrapper_get_key_buffer_size(attributes, |
| 4745 | &key_buffer_size); |
| 4746 | if (status != PSA_SUCCESS) |
Ronald Cron | 3772afe | 2021-02-08 16:10:05 +0100 | [diff] [blame] | 4747 | goto exit; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 4748 | } |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 4749 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4750 | status = psa_allocate_buffer_to_slot(slot, key_buffer_size); |
| 4751 | if (status != PSA_SUCCESS) |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 4752 | goto exit; |
| 4753 | } |
| 4754 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4755 | status = psa_driver_wrapper_generate_key(attributes, slot->key.data, |
| 4756 | slot->key.bytes, &slot->key.bytes); |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 4757 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4758 | if (status != PSA_SUCCESS) |
| 4759 | psa_remove_key_data_from_memory(slot); |
Ronald Cron | 2b56bc8 | 2020-10-05 10:02:26 +0200 | [diff] [blame] | 4760 | |
Gilles Peskine | 1179208 | 2019-08-06 18:36:36 +0200 | [diff] [blame] | 4761 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4762 | if (status == PSA_SUCCESS) |
| 4763 | status = psa_finish_key_creation(slot, driver, key); |
| 4764 | if (status != PSA_SUCCESS) |
| 4765 | psa_fail_key_creation(slot, driver); |
Ronald Cron | f95a2b1 | 2020-10-22 15:24:49 +0200 | [diff] [blame] | 4766 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4767 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4768 | } |
| 4769 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4770 | /****************************************************************/ |
| 4771 | /* Module setup */ |
| 4772 | /****************************************************************/ |
| 4773 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4774 | # if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 4775 | psa_status_t mbedtls_psa_crypto_configure_entropy_sources( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4776 | void (*entropy_init)(mbedtls_entropy_context *ctx), |
| 4777 | void (*entropy_free)(mbedtls_entropy_context *ctx)) |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 4778 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4779 | if (global_data.rng_state != RNG_NOT_INITIALIZED) |
| 4780 | return PSA_ERROR_BAD_STATE; |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4781 | global_data.rng.entropy_init = entropy_init; |
| 4782 | global_data.rng.entropy_free = entropy_free; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4783 | return PSA_SUCCESS; |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 4784 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4785 | # endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
Gilles Peskine | 5e76952 | 2018-11-20 21:59:56 +0100 | [diff] [blame] | 4786 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4787 | void mbedtls_psa_crypto_free(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4788 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4789 | psa_wipe_all_key_slots(); |
| 4790 | if (global_data.rng_state != RNG_NOT_INITIALIZED) { |
| 4791 | mbedtls_psa_random_free(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4792 | } |
| 4793 | /* Wipe all remaining data, including configuration. |
| 4794 | * In particular, this sets all state indicator to the value |
| 4795 | * indicating "uninitialized". */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4796 | mbedtls_platform_zeroize(&global_data, sizeof(global_data)); |
| 4797 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | d089021 | 2019-06-24 14:34:43 +0200 | [diff] [blame] | 4798 | /* Unregister all secure element drivers, so that we restart from |
| 4799 | * a pristine state. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4800 | psa_unregister_all_se_drivers(); |
| 4801 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4802 | } |
| 4803 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4804 | # if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 4805 | /** Recover a transaction that was interrupted by a power failure. |
| 4806 | * |
| 4807 | * This function is called during initialization, before psa_crypto_init() |
| 4808 | * returns. If this function returns a failure status, the initialization |
| 4809 | * fails. |
| 4810 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4811 | static psa_status_t |
| 4812 | psa_crypto_recover_transaction(const psa_crypto_transaction_t *transaction) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 4813 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4814 | switch (transaction->unknown.type) { |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 4815 | case PSA_CRYPTO_TRANSACTION_CREATE_KEY: |
| 4816 | case PSA_CRYPTO_TRANSACTION_DESTROY_KEY: |
Janos Follath | 1d57a20 | 2019-08-13 12:15:34 +0100 | [diff] [blame] | 4817 | /* TODO - fall through to the failure case until this |
Gilles Peskine | c9d7f94 | 2019-08-13 16:17:16 +0200 | [diff] [blame] | 4818 | * is implemented. |
| 4819 | * https://github.com/ARMmbed/mbed-crypto/issues/218 |
| 4820 | */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 4821 | default: |
| 4822 | /* We found an unsupported transaction in the storage. |
| 4823 | * We don't know what state the storage is in. Give up. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4824 | return PSA_ERROR_DATA_INVALID; |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 4825 | } |
| 4826 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4827 | # endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 4828 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4829 | psa_status_t psa_crypto_init(void) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4830 | { |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 4831 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4832 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4833 | /* Double initialization is explicitly allowed. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4834 | if (global_data.initialized != 0) |
| 4835 | return PSA_SUCCESS; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4836 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 4837 | /* Initialize and seed the random generator. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4838 | mbedtls_psa_random_init(&global_data.rng); |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4839 | global_data.rng_state = RNG_INITIALIZED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4840 | status = mbedtls_psa_random_seed(&global_data.rng); |
| 4841 | if (status != PSA_SUCCESS) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4842 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4843 | global_data.rng_state = RNG_SEEDED; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4844 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4845 | status = psa_initialize_key_slots(); |
| 4846 | if (status != PSA_SUCCESS) |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 4847 | goto exit; |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4848 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4849 | # if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 4850 | status = psa_init_all_se_drivers(); |
| 4851 | if (status != PSA_SUCCESS) |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 4852 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4853 | # endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | d9348f2 | 2019-10-01 15:22:29 +0200 | [diff] [blame] | 4854 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4855 | # if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS) |
| 4856 | status = psa_crypto_load_transaction(); |
| 4857 | if (status == PSA_SUCCESS) { |
| 4858 | status = psa_crypto_recover_transaction(&psa_crypto_transaction); |
| 4859 | if (status != PSA_SUCCESS) |
Gilles Peskine | f9bb29e | 2019-07-25 17:52:59 +0200 | [diff] [blame] | 4860 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4861 | status = psa_crypto_stop_transaction(); |
| 4862 | } else if (status == PSA_ERROR_DOES_NOT_EXIST) { |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 4863 | /* There's no transaction to complete. It's all good. */ |
| 4864 | status = PSA_SUCCESS; |
| 4865 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4866 | # endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */ |
Gilles Peskine | fc76265 | 2019-07-22 19:30:34 +0200 | [diff] [blame] | 4867 | |
Gilles Peskine | c6b6907 | 2018-11-20 21:42:52 +0100 | [diff] [blame] | 4868 | /* All done. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4869 | global_data.initialized = 1; |
| 4870 | |
| 4871 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4872 | if (status != PSA_SUCCESS) |
| 4873 | mbedtls_psa_crypto_free(); |
| 4874 | return status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |