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