Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "tfm_crypto_veneers.h" |
| 9 | #include "psa_crypto.h" |
| 10 | #include "tfm_ns_lock.h" |
| 11 | #include "crypto_psa_wrappers.h" |
| 12 | |
| 13 | psa_status_t psa_crypto_init(void) |
| 14 | { |
| 15 | /* Service init is performed during TFM boot up, |
| 16 | * so application level initialisation is empty |
| 17 | */ |
| 18 | return PSA_SUCCESS; |
| 19 | } |
| 20 | |
| 21 | psa_status_t psa_import_key(psa_key_slot_t key, |
| 22 | psa_key_type_t type, |
| 23 | const uint8_t *data, |
| 24 | size_t data_length) |
| 25 | { |
| 26 | enum tfm_crypto_err_t err; |
| 27 | |
| 28 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_import_key, |
| 29 | (uint32_t)key, |
| 30 | (uint32_t)type, |
| 31 | (uint32_t)data, |
| 32 | (uint32_t)data_length); |
| 33 | |
| 34 | return TFM_CRYPTO_PSA_RETURN(err); |
| 35 | } |
| 36 | |
| 37 | psa_status_t psa_destroy_key(psa_key_slot_t key) |
| 38 | { |
| 39 | enum tfm_crypto_err_t err; |
| 40 | |
| 41 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_destroy_key, |
| 42 | (uint32_t)key, |
| 43 | 0, |
| 44 | 0, |
| 45 | 0); |
| 46 | |
| 47 | return TFM_CRYPTO_PSA_RETURN(err); |
| 48 | } |
| 49 | |
| 50 | psa_status_t psa_get_key_information(psa_key_slot_t key, |
| 51 | psa_key_type_t *type, |
| 52 | size_t *bits) |
| 53 | { |
| 54 | enum tfm_crypto_err_t err; |
| 55 | |
| 56 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_get_key_information, |
| 57 | (uint32_t)key, |
| 58 | (uint32_t)type, |
| 59 | (uint32_t)bits, |
| 60 | 0); |
| 61 | |
| 62 | return TFM_CRYPTO_PSA_RETURN(err); |
| 63 | } |
| 64 | |
| 65 | psa_status_t psa_export_key(psa_key_slot_t key, |
| 66 | uint8_t *data, |
| 67 | size_t data_size, |
| 68 | size_t *data_length) |
| 69 | { |
| 70 | enum tfm_crypto_err_t err; |
| 71 | |
| 72 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_export_key, |
| 73 | (uint32_t)key, |
| 74 | (uint32_t)data, |
| 75 | (uint32_t)data_size, |
| 76 | (uint32_t)data_length); |
| 77 | |
| 78 | return TFM_CRYPTO_PSA_RETURN(err); |
| 79 | } |
| 80 | |
| 81 | psa_status_t psa_export_public_key(psa_key_slot_t key, |
| 82 | uint8_t *data, |
| 83 | size_t data_size, |
| 84 | size_t *data_length) |
| 85 | { |
| 86 | /* TODO: This API is not supported yet */ |
| 87 | return PSA_ERROR_NOT_SUPPORTED; |
| 88 | } |
| 89 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 90 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 91 | const unsigned char *iv, |
| 92 | size_t iv_length) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 93 | { |
| 94 | enum tfm_crypto_err_t err; |
| 95 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 96 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_cipher_set_iv, |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 97 | (uint32_t)operation, |
| 98 | (uint32_t)iv, |
| 99 | (uint32_t)iv_length, |
| 100 | 0); |
| 101 | |
| 102 | return TFM_CRYPTO_PSA_RETURN(err); |
| 103 | } |
| 104 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 105 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 106 | psa_key_slot_t key, |
| 107 | psa_algorithm_t alg) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 108 | { |
| 109 | enum tfm_crypto_err_t err; |
| 110 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 111 | err = tfm_ns_lock_dispatch( |
| 112 | (veneer_fn)tfm_crypto_veneer_cipher_encrypt_setup, |
| 113 | (uint32_t)operation, |
| 114 | (uint32_t)key, |
| 115 | (uint32_t)alg, |
| 116 | 0); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 117 | |
| 118 | return TFM_CRYPTO_PSA_RETURN(err); |
| 119 | } |
| 120 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 121 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 122 | psa_key_slot_t key, |
| 123 | psa_algorithm_t alg) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 124 | { |
| 125 | enum tfm_crypto_err_t err; |
| 126 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 127 | err = tfm_ns_lock_dispatch( |
| 128 | (veneer_fn)tfm_crypto_veneer_cipher_decrypt_setup, |
| 129 | (uint32_t)operation, |
| 130 | (uint32_t)key, |
| 131 | (uint32_t)alg, |
| 132 | 0); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 133 | |
| 134 | return TFM_CRYPTO_PSA_RETURN(err); |
| 135 | } |
| 136 | |
| 137 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 138 | const uint8_t *input, |
| 139 | size_t input_length, |
| 140 | unsigned char *output, |
| 141 | size_t output_size, |
| 142 | size_t *output_length) |
| 143 | { |
| 144 | enum tfm_crypto_err_t err; |
| 145 | |
| 146 | /* Packing in structures is needed to overcome the 4 parameters |
| 147 | * per call limit |
| 148 | */ |
| 149 | struct psa_cipher_update_input input_s = {.input = input, |
| 150 | .input_length = input_length}; |
| 151 | struct psa_cipher_update_output output_s = {.output = output, |
| 152 | .output_size = output_size, |
| 153 | .output_length = |
| 154 | output_length}; |
| 155 | |
| 156 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_cipher_update, |
| 157 | (uint32_t)operation, |
| 158 | (uint32_t)&input_s, |
| 159 | (uint32_t)&output_s, |
| 160 | 0); |
| 161 | |
| 162 | return TFM_CRYPTO_PSA_RETURN(err); |
| 163 | } |
| 164 | |
| 165 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) |
| 166 | { |
| 167 | enum tfm_crypto_err_t err; |
| 168 | |
| 169 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_cipher_abort, |
| 170 | (uint32_t)operation, |
| 171 | 0, |
| 172 | 0, |
| 173 | 0); |
| 174 | |
| 175 | return TFM_CRYPTO_PSA_RETURN(err); |
| 176 | } |
| 177 | |
| 178 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
| 179 | uint8_t *output, |
| 180 | size_t output_size, |
| 181 | size_t *output_length) |
| 182 | { |
| 183 | enum tfm_crypto_err_t err; |
| 184 | |
| 185 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_cipher_finish, |
| 186 | (uint32_t)operation, |
| 187 | (uint32_t)output, |
| 188 | (uint32_t)output_size, |
| 189 | (uint32_t)output_length); |
| 190 | |
| 191 | return TFM_CRYPTO_PSA_RETURN(err); |
| 192 | } |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 193 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 194 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 195 | psa_algorithm_t alg) |
| 196 | { |
| 197 | enum tfm_crypto_err_t err; |
| 198 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame^] | 199 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_hash_setup, |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 200 | (uint32_t)operation, |
| 201 | (uint32_t)alg, |
| 202 | 0, |
| 203 | 0); |
| 204 | |
| 205 | return TFM_CRYPTO_PSA_RETURN(err); |
| 206 | } |
| 207 | |
| 208 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 209 | const uint8_t *input, |
| 210 | size_t input_length) |
| 211 | { |
| 212 | enum tfm_crypto_err_t err; |
| 213 | |
| 214 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_hash_update, |
| 215 | (uint32_t)operation, |
| 216 | (uint32_t)input, |
| 217 | (uint32_t)input_length, |
| 218 | 0); |
| 219 | |
| 220 | return TFM_CRYPTO_PSA_RETURN(err); |
| 221 | } |
| 222 | |
| 223 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 224 | uint8_t *hash, |
| 225 | size_t hash_size, |
| 226 | size_t *hash_length) |
| 227 | { |
| 228 | enum tfm_crypto_err_t err; |
| 229 | |
| 230 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_hash_finish, |
| 231 | (uint32_t)operation, |
| 232 | (uint32_t)hash, |
| 233 | (uint32_t)hash_size, |
| 234 | (uint32_t)hash_length); |
| 235 | |
| 236 | return TFM_CRYPTO_PSA_RETURN(err); |
| 237 | } |
| 238 | |
| 239 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 240 | const uint8_t *hash, |
| 241 | size_t hash_length) |
| 242 | { |
| 243 | enum tfm_crypto_err_t err; |
| 244 | |
| 245 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_hash_verify, |
| 246 | (uint32_t)operation, |
| 247 | (uint32_t)hash, |
| 248 | (uint32_t)hash_length, |
| 249 | 0); |
| 250 | |
| 251 | return TFM_CRYPTO_PSA_RETURN(err); |
| 252 | } |
| 253 | |
| 254 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
| 255 | { |
| 256 | enum tfm_crypto_err_t err; |
| 257 | |
| 258 | err = tfm_ns_lock_dispatch((veneer_fn)tfm_crypto_veneer_hash_abort, |
| 259 | (uint32_t)operation, |
| 260 | 0, |
| 261 | 0, |
| 262 | 0); |
| 263 | |
| 264 | return TFM_CRYPTO_PSA_RETURN(err); |
| 265 | } |