Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019, Arm Limited. All rights reserved. |
| 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" |
| 20 | |
| 21 | /*! |
| 22 | * \defgroup public_psa Public functions, PSA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | /*!@{*/ |
| 27 | psa_status_t tfm_crypto_asymmetric_sign(psa_invec in_vec[], |
| 28 | size_t in_len, |
| 29 | psa_outvec out_vec[], |
| 30 | size_t out_len) |
| 31 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 32 | #ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 33 | return PSA_ERROR_NOT_SUPPORTED; |
| 34 | #else |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 35 | if ((in_len != 2) || (out_len != 1)) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 36 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 40 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 41 | } |
| 42 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 43 | |
| 44 | psa_key_handle_t handle = iov->key_handle; |
| 45 | psa_algorithm_t alg = iov->alg; |
| 46 | const uint8_t *hash = in_vec[1].base; |
| 47 | size_t hash_length = in_vec[1].len; |
| 48 | uint8_t *signature = out_vec[0].base; |
| 49 | size_t signature_size = out_vec[0].len; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 50 | psa_status_t status = tfm_crypto_check_handle_owner(handle, NULL); |
| 51 | |
| 52 | if (status != PSA_SUCCESS) { |
| 53 | return status; |
| 54 | } |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 55 | |
| 56 | return psa_asymmetric_sign(handle, alg, hash, hash_length, |
| 57 | signature, signature_size, &(out_vec[0].len)); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 58 | #endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | psa_status_t tfm_crypto_asymmetric_verify(psa_invec in_vec[], |
| 62 | size_t in_len, |
| 63 | psa_outvec out_vec[], |
| 64 | size_t out_len) |
| 65 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 66 | #ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 67 | return PSA_ERROR_NOT_SUPPORTED; |
| 68 | #else |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 69 | if ((in_len != 3) || (out_len != 0)) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 70 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 74 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 75 | } |
| 76 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 77 | |
| 78 | psa_key_handle_t handle = iov->key_handle; |
| 79 | psa_algorithm_t alg = iov->alg; |
| 80 | const uint8_t *hash = in_vec[1].base; |
| 81 | size_t hash_length = in_vec[1].len; |
| 82 | const uint8_t *signature = in_vec[2].base; |
| 83 | size_t signature_length = in_vec[2].len; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 84 | psa_status_t status = tfm_crypto_check_handle_owner(handle, NULL); |
| 85 | |
| 86 | if (status != PSA_SUCCESS) { |
| 87 | return status; |
| 88 | } |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 89 | |
| 90 | return psa_asymmetric_verify(handle, alg, hash, hash_length, |
| 91 | signature, signature_length); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 92 | #endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | psa_status_t tfm_crypto_asymmetric_encrypt(psa_invec in_vec[], |
| 96 | size_t in_len, |
| 97 | psa_outvec out_vec[], |
| 98 | size_t out_len) |
| 99 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 100 | #ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 101 | return PSA_ERROR_NOT_SUPPORTED; |
| 102 | #else |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 103 | psa_status_t status; |
| 104 | |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 105 | if (!((in_len == 2) || (in_len == 3)) || (out_len != 1)) { |
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 | } |
| 108 | |
| 109 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 110 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 111 | } |
| 112 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 113 | |
| 114 | psa_key_handle_t handle = iov->key_handle; |
| 115 | psa_algorithm_t alg = iov->alg; |
| 116 | const uint8_t *input = in_vec[1].base; |
| 117 | size_t input_length = in_vec[1].len; |
| 118 | const uint8_t *salt = NULL; |
| 119 | size_t salt_length = 0; |
| 120 | uint8_t *output = out_vec[0].base; |
| 121 | size_t output_size = out_vec[0].len; |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 122 | psa_key_type_t type; |
| 123 | size_t key_bits; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 124 | |
| 125 | if (in_len == 3) { |
| 126 | salt = in_vec[2].base; |
| 127 | salt_length = in_vec[2].len; |
| 128 | } |
| 129 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 130 | status = tfm_crypto_check_handle_owner(handle, NULL); |
| 131 | if (status != PSA_SUCCESS) { |
| 132 | return status; |
| 133 | } |
| 134 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 135 | status = psa_get_key_information(handle, &type, &key_bits); |
| 136 | if (status != PSA_SUCCESS) { |
| 137 | return status; |
| 138 | } |
| 139 | |
| 140 | /* Check that the output buffer is large enough */ |
| 141 | if (output_size < PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(type, key_bits, alg)) { |
| 142 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 143 | } |
| 144 | |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 145 | return psa_asymmetric_encrypt(handle, alg, input, input_length, |
| 146 | salt, salt_length, |
| 147 | output, output_size, &(out_vec[0].len)); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 148 | #endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | psa_status_t tfm_crypto_asymmetric_decrypt(psa_invec in_vec[], |
| 152 | size_t in_len, |
| 153 | psa_outvec out_vec[], |
| 154 | size_t out_len) |
| 155 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame^] | 156 | #ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 157 | return PSA_ERROR_NOT_SUPPORTED; |
| 158 | #else |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 159 | if (!((in_len == 2) || (in_len == 3)) || (out_len != 1)) { |
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 | |
| 163 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 164 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 165 | } |
| 166 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 167 | |
| 168 | psa_key_handle_t handle = iov->key_handle; |
| 169 | psa_algorithm_t alg = iov->alg; |
| 170 | const uint8_t *input = in_vec[1].base; |
| 171 | size_t input_length = in_vec[1].len; |
| 172 | const uint8_t *salt = NULL; |
| 173 | size_t salt_length = 0; |
| 174 | uint8_t *output = out_vec[0].base; |
| 175 | size_t output_size = out_vec[0].len; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 176 | psa_status_t status; |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 177 | |
| 178 | if (in_len == 3) { |
| 179 | salt = in_vec[2].base; |
| 180 | salt_length = in_vec[2].len; |
| 181 | } |
| 182 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 183 | status = tfm_crypto_check_handle_owner(handle, NULL); |
| 184 | if (status != PSA_SUCCESS) { |
| 185 | return status; |
| 186 | } |
| 187 | |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 188 | return psa_asymmetric_decrypt(handle, alg, input, input_length, |
| 189 | salt, salt_length, |
| 190 | output, output_size, &(out_vec[0].len)); |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 191 | #endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 192 | } |
| 193 | /*!@}*/ |