Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 11 | /* FixMe: Use PSA_ERROR_CONNECTION_REFUSED when performing parameter |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 12 | * integrity checks but this will have to be revised |
| 13 | * when the full set of error codes mandated by PSA FF |
| 14 | * is available. |
| 15 | */ |
| 16 | #include "tfm_mbedcrypto_include.h" |
| 17 | |
| 18 | #include "tfm_crypto_api.h" |
| 19 | #include "tfm_crypto_defs.h" |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 20 | #include "tfm_crypto_private.h" |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 21 | |
| 22 | /*! |
| 23 | * \defgroup public_psa Public functions, PSA |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | /*!@{*/ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 28 | psa_status_t tfm_crypto_sign_hash(psa_invec in_vec[], |
| 29 | size_t in_len, |
| 30 | psa_outvec out_vec[], |
| 31 | size_t out_len) |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 32 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 33 | #ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 34 | return PSA_ERROR_NOT_SUPPORTED; |
| 35 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 36 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 1); |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 37 | |
| 38 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 39 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 40 | } |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 41 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 42 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 43 | psa_key_handle_t handle = iov->key_handle; |
| 44 | psa_algorithm_t alg = iov->alg; |
| 45 | const uint8_t *hash = in_vec[1].base; |
| 46 | size_t hash_length = in_vec[1].len; |
| 47 | uint8_t *signature = out_vec[0].base; |
| 48 | size_t signature_size = out_vec[0].len; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 49 | psa_status_t status = tfm_crypto_check_handle_owner(handle, NULL); |
| 50 | |
| 51 | if (status != PSA_SUCCESS) { |
| 52 | return status; |
| 53 | } |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 54 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 55 | return psa_sign_hash(handle, alg, hash, hash_length, |
| 56 | signature, signature_size, &(out_vec[0].len)); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 57 | #endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 60 | psa_status_t tfm_crypto_verify_hash(psa_invec in_vec[], |
| 61 | size_t in_len, |
| 62 | psa_outvec out_vec[], |
| 63 | size_t out_len) |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 64 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 65 | #ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 66 | return PSA_ERROR_NOT_SUPPORTED; |
| 67 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 68 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 0); |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 69 | |
| 70 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 71 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 72 | } |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 73 | |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 74 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 75 | |
| 76 | psa_key_handle_t handle = iov->key_handle; |
| 77 | psa_algorithm_t alg = iov->alg; |
| 78 | const uint8_t *hash = in_vec[1].base; |
| 79 | size_t hash_length = in_vec[1].len; |
| 80 | const uint8_t *signature = in_vec[2].base; |
| 81 | size_t signature_length = in_vec[2].len; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 82 | psa_status_t status = tfm_crypto_check_handle_owner(handle, NULL); |
| 83 | |
| 84 | if (status != PSA_SUCCESS) { |
| 85 | return status; |
| 86 | } |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 87 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 88 | return psa_verify_hash(handle, alg, hash, hash_length, |
| 89 | signature, signature_length); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 90 | #endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | psa_status_t tfm_crypto_asymmetric_encrypt(psa_invec in_vec[], |
| 94 | size_t in_len, |
| 95 | psa_outvec out_vec[], |
| 96 | size_t out_len) |
| 97 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 98 | #ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 99 | return PSA_ERROR_NOT_SUPPORTED; |
| 100 | #else |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 101 | psa_status_t status; |
| 102 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 103 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1); |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 104 | |
| 105 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 106 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 107 | } |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 108 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 109 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 110 | psa_key_handle_t handle = iov->key_handle; |
| 111 | psa_algorithm_t alg = iov->alg; |
| 112 | const uint8_t *input = in_vec[1].base; |
| 113 | size_t input_length = in_vec[1].len; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 114 | const uint8_t *salt = in_vec[2].base; |
| 115 | size_t salt_length = in_vec[2].len; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 116 | uint8_t *output = out_vec[0].base; |
| 117 | size_t output_size = out_vec[0].len; |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 118 | psa_key_type_t type; |
| 119 | size_t key_bits; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 120 | psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 121 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 122 | status = tfm_crypto_check_handle_owner(handle, NULL); |
| 123 | if (status != PSA_SUCCESS) { |
| 124 | return status; |
| 125 | } |
| 126 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 127 | status = psa_get_key_attributes(handle, &key_attributes); |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 128 | if (status != PSA_SUCCESS) { |
| 129 | return status; |
| 130 | } |
| 131 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 132 | key_bits = psa_get_key_bits(&key_attributes); |
| 133 | type = psa_get_key_type(&key_attributes); |
| 134 | |
| 135 | psa_reset_key_attributes(&key_attributes); |
| 136 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 137 | /* Check that the output buffer is large enough */ |
| 138 | if (output_size < PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(type, key_bits, alg)) { |
| 139 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 140 | } |
| 141 | |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 142 | return psa_asymmetric_encrypt(handle, alg, input, input_length, |
| 143 | salt, salt_length, |
| 144 | output, output_size, &(out_vec[0].len)); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 145 | #endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | psa_status_t tfm_crypto_asymmetric_decrypt(psa_invec in_vec[], |
| 149 | size_t in_len, |
| 150 | psa_outvec out_vec[], |
| 151 | size_t out_len) |
| 152 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 153 | #ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 154 | return PSA_ERROR_NOT_SUPPORTED; |
| 155 | #else |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 156 | |
| 157 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1); |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 158 | |
| 159 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 160 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 161 | } |
| 162 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 163 | |
| 164 | psa_key_handle_t handle = iov->key_handle; |
| 165 | psa_algorithm_t alg = iov->alg; |
| 166 | const uint8_t *input = in_vec[1].base; |
| 167 | size_t input_length = in_vec[1].len; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 168 | const uint8_t *salt = in_vec[2].base; |
| 169 | size_t salt_length = in_vec[2].len; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 170 | uint8_t *output = out_vec[0].base; |
| 171 | size_t output_size = out_vec[0].len; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 172 | psa_status_t status; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 173 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 174 | status = tfm_crypto_check_handle_owner(handle, NULL); |
| 175 | if (status != PSA_SUCCESS) { |
| 176 | return status; |
| 177 | } |
| 178 | |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 179 | return psa_asymmetric_decrypt(handle, alg, input, input_length, |
| 180 | salt, salt_length, |
| 181 | output, output_size, &(out_vec[0].len)); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 182 | #endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 183 | } |
| 184 | /*!@}*/ |