Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 1 | /* |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved. |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | #include <stdint.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <protocols/service/crypto/packed-c/opcodes.h> |
| 9 | #include <service/crypto/provider/mbedcrypto/crypto_provider.h> |
julhal01 | 2c18fbf | 2021-02-01 08:29:28 +0000 | [diff] [blame^] | 10 | #include <service/crypto/provider/mbedcrypto/trng_adapter/trng_adapter.h> |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 11 | #include <service/secure_storage/client/psa/its/its_client.h> |
| 12 | #include <protocols/rpc/common/packed-c/status.h> |
| 13 | #include <psa/crypto.h> |
| 14 | |
| 15 | /* Service request handlers */ |
| 16 | static rpc_status_t nop_handler(void *context, struct call_req* req); |
| 17 | static rpc_status_t generate_key_handler(void *context, struct call_req* req); |
| 18 | static rpc_status_t destroy_key_handler(void *context, struct call_req* req); |
| 19 | static rpc_status_t open_key_handler(void *context, struct call_req* req); |
| 20 | static rpc_status_t close_key_handler(void *context, struct call_req* req); |
| 21 | static rpc_status_t export_key_handler(void *context, struct call_req* req); |
| 22 | static rpc_status_t export_public_key_handler(void *context, struct call_req* req); |
| 23 | static rpc_status_t import_key_handler(void *context, struct call_req* req); |
| 24 | static rpc_status_t sign_hash_handler(void *context, struct call_req* req); |
| 25 | static rpc_status_t verify_hash_handler(void *context, struct call_req* req); |
| 26 | static rpc_status_t asymmetric_decrypt_handler(void *context, struct call_req* req); |
| 27 | static rpc_status_t asymmetric_encrypt_handler(void *context, struct call_req* req); |
| 28 | static rpc_status_t generate_random_handler(void *context, struct call_req* req); |
| 29 | |
| 30 | /* Handler mapping table for service */ |
| 31 | static const struct service_handler handler_table[] = { |
| 32 | {TS_CRYPTO_OPCODE_NOP, nop_handler}, |
| 33 | {TS_CRYPTO_OPCODE_GENERATE_KEY, generate_key_handler}, |
| 34 | {TS_CRYPTO_OPCODE_DESTROY_KEY, destroy_key_handler}, |
| 35 | {TS_CRYPTO_OPCODE_OPEN_KEY, open_key_handler}, |
| 36 | {TS_CRYPTO_OPCODE_CLOSE_KEY, close_key_handler}, |
| 37 | {TS_CRYPTO_OPCODE_EXPORT_KEY, export_key_handler}, |
| 38 | {TS_CRYPTO_OPCODE_EXPORT_PUBLIC_KEY, export_public_key_handler}, |
| 39 | {TS_CRYPTO_OPCODE_IMPORT_KEY, import_key_handler}, |
| 40 | {TS_CRYPTO_OPCODE_SIGN_HASH, sign_hash_handler}, |
| 41 | {TS_CRYPTO_OPCODE_VERIFY_HASH, verify_hash_handler}, |
| 42 | {TS_CRYPTO_OPCODE_ASYMMETRIC_DECRYPT, asymmetric_decrypt_handler}, |
| 43 | {TS_CRYPTO_OPCODE_ASYMMETRIC_ENCRYPT, asymmetric_encrypt_handler}, |
| 44 | {TS_CRYPTO_OPCODE_GENERATE_RANDOM, generate_random_handler} |
| 45 | }; |
| 46 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 47 | struct rpc_interface *mbed_crypto_provider_init(struct mbed_crypto_provider *context, |
julhal01 | ffa98d8 | 2021-01-20 13:51:58 +0000 | [diff] [blame] | 48 | struct rpc_caller *storage_provider, |
julhal01 | 2c18fbf | 2021-02-01 08:29:28 +0000 | [diff] [blame^] | 49 | int trng_instance) |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 50 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 51 | struct rpc_interface *rpc_interface = NULL; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 52 | |
julhal01 | 2c18fbf | 2021-02-01 08:29:28 +0000 | [diff] [blame^] | 53 | trng_adapter_init(trng_instance); |
julhal01 | ffa98d8 | 2021-01-20 13:51:58 +0000 | [diff] [blame] | 54 | |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 55 | /* |
| 56 | * A storage provider is required for persistent key storage. As this |
| 57 | * is a mandatory feature of the crypto service, insist on a storage |
| 58 | * provider being available. |
| 59 | */ |
| 60 | if (context && storage_provider) { |
| 61 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 62 | for (size_t encoding = 0; encoding < TS_RPC_ENCODING_LIMIT; ++encoding) |
| 63 | context->serializers[encoding] = NULL; |
| 64 | |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 65 | service_provider_init(&context->base_provider, context, |
| 66 | handler_table, sizeof(handler_table)/sizeof(struct service_handler)); |
| 67 | |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 68 | if ((psa_its_client_init(storage_provider) == PSA_SUCCESS) && |
| 69 | (psa_crypto_init() == PSA_SUCCESS)) |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 70 | rpc_interface = service_provider_get_rpc_interface(&context->base_provider); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 71 | } |
| 72 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 73 | return rpc_interface; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void mbed_crypto_provider_deinit(struct mbed_crypto_provider *context) |
| 77 | { |
| 78 | (void)context; |
julhal01 | 2c18fbf | 2021-02-01 08:29:28 +0000 | [diff] [blame^] | 79 | trng_adapter_deinit(); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 80 | } |
| 81 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 82 | void mbed_crypto_provider_register_serializer(struct mbed_crypto_provider *context, |
| 83 | unsigned int encoding, const struct crypto_provider_serializer *serializer) |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 84 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 85 | if (encoding < TS_RPC_ENCODING_LIMIT) |
| 86 | context->serializers[encoding] = serializer; |
| 87 | } |
| 88 | |
| 89 | static const struct crypto_provider_serializer* get_crypto_serializer(void *context, |
| 90 | const struct call_req *req) |
| 91 | { |
| 92 | struct mbed_crypto_provider *this_instance = (struct mbed_crypto_provider*)context; |
| 93 | const struct crypto_provider_serializer* serializer = NULL; |
| 94 | unsigned int encoding = call_req_get_encoding(req); |
| 95 | |
| 96 | if (encoding < TS_RPC_ENCODING_LIMIT) serializer = this_instance->serializers[encoding]; |
| 97 | |
| 98 | return serializer; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static rpc_status_t nop_handler(void *context, struct call_req* req) |
| 102 | { |
| 103 | /* Responds to a request by returning success */ |
| 104 | rpc_status_t rpc_status = TS_RPC_CALL_ACCEPTED; |
| 105 | psa_status_t psa_status = PSA_SUCCESS; |
| 106 | |
| 107 | (void)context; |
| 108 | call_req_set_opstatus(req, psa_status); |
| 109 | |
| 110 | return rpc_status; |
| 111 | } |
| 112 | |
| 113 | static rpc_status_t generate_key_handler(void *context, struct call_req* req) |
| 114 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 115 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 116 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 117 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 118 | |
| 119 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 120 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 121 | if (serializer) |
| 122 | rpc_status = serializer->deserialize_generate_key_req(req_buf, &attributes); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 123 | |
| 124 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 125 | |
| 126 | psa_status_t psa_status; |
| 127 | psa_key_handle_t handle; |
| 128 | |
| 129 | psa_status = psa_generate_key(&attributes, &handle); |
| 130 | |
| 131 | if (psa_status == PSA_SUCCESS) { |
| 132 | |
| 133 | struct call_param_buf *resp_buf = call_req_get_resp_buf(req); |
| 134 | rpc_status = serializer->serialize_generate_key_resp(resp_buf, handle); |
| 135 | } |
| 136 | |
| 137 | call_req_set_opstatus(req, psa_status); |
| 138 | } |
| 139 | |
| 140 | psa_reset_key_attributes(&attributes); |
| 141 | |
| 142 | return rpc_status; |
| 143 | } |
| 144 | |
| 145 | static rpc_status_t destroy_key_handler(void *context, struct call_req* req) |
| 146 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 147 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 148 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 149 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 150 | |
| 151 | psa_key_handle_t handle; |
| 152 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 153 | if (serializer) |
| 154 | rpc_status = serializer->deserialize_destroy_key_req(req_buf, &handle); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 155 | |
| 156 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 157 | |
| 158 | psa_status_t psa_status; |
| 159 | |
| 160 | psa_status = psa_destroy_key(handle); |
| 161 | call_req_set_opstatus(req, psa_status); |
| 162 | } |
| 163 | |
| 164 | return rpc_status; |
| 165 | } |
| 166 | |
| 167 | static rpc_status_t open_key_handler(void *context, struct call_req* req) |
| 168 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 169 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 170 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 171 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 172 | |
| 173 | psa_key_id_t id; |
| 174 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 175 | if (serializer) |
| 176 | rpc_status = serializer->deserialize_open_key_req(req_buf, &id); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 177 | |
| 178 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 179 | |
| 180 | psa_status_t psa_status; |
| 181 | psa_key_handle_t handle; |
| 182 | |
| 183 | psa_status = psa_open_key(id, &handle); |
| 184 | |
| 185 | if (psa_status == PSA_SUCCESS) { |
| 186 | |
| 187 | struct call_param_buf *resp_buf = call_req_get_resp_buf(req); |
| 188 | rpc_status = serializer->serialize_open_key_resp(resp_buf, handle); |
| 189 | } |
| 190 | |
| 191 | call_req_set_opstatus(req, psa_status); |
| 192 | } |
| 193 | |
| 194 | return rpc_status; |
| 195 | } |
| 196 | |
| 197 | static rpc_status_t close_key_handler(void *context, struct call_req* req) |
| 198 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 199 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 200 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 201 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 202 | |
| 203 | psa_key_handle_t handle; |
| 204 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 205 | if (serializer) |
| 206 | rpc_status = serializer->deserialize_close_key_req(req_buf, &handle); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 207 | |
| 208 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 209 | |
| 210 | psa_status_t psa_status; |
| 211 | |
| 212 | psa_status = psa_close_key(handle); |
| 213 | call_req_set_opstatus(req, psa_status); |
| 214 | } |
| 215 | |
| 216 | return rpc_status; |
| 217 | } |
| 218 | |
| 219 | static rpc_status_t export_key_handler(void *context, struct call_req* req) |
| 220 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 221 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 222 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 223 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 224 | |
| 225 | psa_key_handle_t handle; |
| 226 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 227 | if (serializer) |
| 228 | rpc_status = serializer->deserialize_export_key_req(req_buf, &handle); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 229 | |
| 230 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 231 | |
| 232 | psa_status_t psa_status; |
| 233 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 234 | |
| 235 | psa_status = psa_get_key_attributes(handle, &attributes); |
| 236 | |
| 237 | if (psa_status == PSA_SUCCESS) { |
| 238 | |
| 239 | size_t max_export_size = PSA_KEY_EXPORT_MAX_SIZE( |
| 240 | psa_get_key_type(&attributes), |
| 241 | psa_get_key_bits(&attributes)); |
| 242 | |
| 243 | uint8_t *key_buffer = malloc(max_export_size); |
| 244 | |
| 245 | if (key_buffer) { |
| 246 | |
| 247 | size_t export_size; |
| 248 | psa_status = psa_export_key(handle, key_buffer, max_export_size, &export_size); |
| 249 | |
| 250 | if (psa_status == PSA_SUCCESS) { |
| 251 | |
| 252 | struct call_param_buf *resp_buf = call_req_get_resp_buf(req); |
| 253 | rpc_status = serializer->serialize_export_key_resp(resp_buf, key_buffer, export_size); |
| 254 | } |
| 255 | |
| 256 | free(key_buffer); |
| 257 | } |
| 258 | else { |
| 259 | /* Failed to allocate key buffer */ |
| 260 | rpc_status = TS_RPC_ERROR_RESOURCE_FAILURE; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | call_req_set_opstatus(req, psa_status); |
| 265 | psa_reset_key_attributes(&attributes); |
| 266 | } |
| 267 | |
| 268 | return rpc_status; |
| 269 | } |
| 270 | |
| 271 | static rpc_status_t export_public_key_handler(void *context, struct call_req* req) |
| 272 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 273 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 274 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 275 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 276 | |
| 277 | psa_key_handle_t handle; |
| 278 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 279 | if (serializer) |
| 280 | rpc_status = serializer->deserialize_export_public_key_req(req_buf, &handle); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 281 | |
| 282 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 283 | |
| 284 | psa_status_t psa_status; |
| 285 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 286 | |
| 287 | psa_status = psa_get_key_attributes(handle, &attributes); |
| 288 | |
| 289 | if (psa_status == PSA_SUCCESS) { |
| 290 | |
| 291 | size_t max_export_size = PSA_KEY_EXPORT_MAX_SIZE( |
| 292 | PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(psa_get_key_type(&attributes)), |
| 293 | psa_get_key_bits(&attributes)); |
| 294 | |
| 295 | uint8_t *key_buffer = malloc(max_export_size); |
| 296 | |
| 297 | if (key_buffer) { |
| 298 | |
| 299 | size_t export_size; |
| 300 | psa_status = psa_export_public_key(handle, key_buffer, max_export_size, &export_size); |
| 301 | |
| 302 | if (psa_status == PSA_SUCCESS) { |
| 303 | |
| 304 | struct call_param_buf *resp_buf = call_req_get_resp_buf(req); |
| 305 | rpc_status = serializer->serialize_export_public_key_resp(resp_buf, key_buffer, export_size); |
| 306 | } |
| 307 | |
| 308 | free(key_buffer); |
| 309 | } |
| 310 | else { |
| 311 | /* Failed to allocate key buffer */ |
| 312 | rpc_status = TS_RPC_ERROR_RESOURCE_FAILURE; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | call_req_set_opstatus(req, psa_status); |
| 317 | psa_reset_key_attributes(&attributes); |
| 318 | } |
| 319 | |
| 320 | return rpc_status; |
| 321 | } |
| 322 | |
| 323 | static rpc_status_t import_key_handler(void *context, struct call_req* req) |
| 324 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 325 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 326 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 327 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 328 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 329 | if (serializer) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 330 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 331 | size_t key_data_len = serializer->max_deserialised_parameter_size(req_buf); |
| 332 | uint8_t *key_buffer = malloc(key_data_len); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 333 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 334 | if (key_buffer) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 335 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 336 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 337 | rpc_status = serializer->deserialize_import_key_req(req_buf, &attributes, key_buffer, &key_data_len); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 338 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 339 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 340 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 341 | psa_status_t psa_status; |
| 342 | psa_key_handle_t handle; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 343 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 344 | psa_status = psa_import_key(&attributes, key_buffer, key_data_len, &handle); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 345 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 346 | if (psa_status == PSA_SUCCESS) { |
| 347 | |
| 348 | struct call_param_buf *resp_buf = call_req_get_resp_buf(req); |
| 349 | rpc_status = serializer->serialize_import_key_resp(resp_buf, handle); |
| 350 | } |
| 351 | |
| 352 | call_req_set_opstatus(req, psa_status); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 353 | } |
| 354 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 355 | psa_reset_key_attributes(&attributes); |
| 356 | free(key_buffer); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 357 | } |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 358 | else { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 359 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 360 | rpc_status = TS_RPC_ERROR_RESOURCE_FAILURE; |
| 361 | } |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | return rpc_status; |
| 365 | } |
| 366 | |
| 367 | static rpc_status_t sign_hash_handler(void *context, struct call_req* req) |
| 368 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 369 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 370 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 371 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 372 | |
| 373 | psa_key_handle_t handle; |
| 374 | psa_algorithm_t alg; |
| 375 | size_t hash_len = PSA_HASH_MAX_SIZE; |
| 376 | uint8_t hash_buffer[PSA_HASH_MAX_SIZE]; |
| 377 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 378 | if (serializer) |
| 379 | rpc_status = serializer->deserialize_sign_hash_req(req_buf, &handle, &alg, hash_buffer, &hash_len); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 380 | |
| 381 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 382 | |
| 383 | psa_status_t psa_status; |
| 384 | size_t sig_len; |
| 385 | uint8_t sig_buffer[PSA_SIGNATURE_MAX_SIZE]; |
| 386 | |
| 387 | psa_status = psa_sign_hash(handle, alg, |
| 388 | hash_buffer, hash_len, |
| 389 | sig_buffer, sizeof(sig_buffer), &sig_len); |
| 390 | |
| 391 | if (psa_status == PSA_SUCCESS) { |
| 392 | |
| 393 | struct call_param_buf *resp_buf = call_req_get_resp_buf(req); |
| 394 | rpc_status = serializer->serialize_sign_hash_resp(resp_buf, sig_buffer, sig_len); |
| 395 | } |
| 396 | |
| 397 | call_req_set_opstatus(req, psa_status); |
| 398 | } |
| 399 | |
| 400 | return rpc_status; |
| 401 | } |
| 402 | |
| 403 | static rpc_status_t verify_hash_handler(void *context, struct call_req* req) |
| 404 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 405 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 406 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 407 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 408 | |
| 409 | psa_key_handle_t handle; |
| 410 | psa_algorithm_t alg; |
| 411 | size_t hash_len = PSA_HASH_MAX_SIZE; |
| 412 | uint8_t hash_buffer[PSA_HASH_MAX_SIZE]; |
| 413 | size_t sig_len = PSA_SIGNATURE_MAX_SIZE; |
| 414 | uint8_t sig_buffer[PSA_SIGNATURE_MAX_SIZE]; |
| 415 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 416 | if (serializer) |
| 417 | rpc_status = serializer->deserialize_verify_hash_req(req_buf, &handle, &alg, |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 418 | hash_buffer, &hash_len, |
| 419 | sig_buffer, &sig_len); |
| 420 | |
| 421 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 422 | |
| 423 | psa_status_t psa_status; |
| 424 | |
| 425 | psa_status = psa_verify_hash(handle, alg, |
| 426 | hash_buffer, hash_len, |
| 427 | sig_buffer, sig_len); |
| 428 | |
| 429 | call_req_set_opstatus(req, psa_status); |
| 430 | } |
| 431 | |
| 432 | return rpc_status; |
| 433 | } |
| 434 | |
| 435 | static rpc_status_t asymmetric_decrypt_handler(void *context, struct call_req* req) |
| 436 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 437 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 438 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 439 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 440 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 441 | if (serializer) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 442 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 443 | size_t max_param_size = serializer->max_deserialised_parameter_size(req_buf); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 444 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 445 | psa_key_handle_t handle; |
| 446 | psa_algorithm_t alg; |
| 447 | size_t ciphertext_len = max_param_size; |
| 448 | uint8_t *ciphertext_buffer = malloc(ciphertext_len); |
| 449 | size_t salt_len = max_param_size; |
| 450 | uint8_t *salt_buffer = malloc(salt_len); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 451 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 452 | if (ciphertext_buffer && salt_buffer) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 453 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 454 | rpc_status = serializer->deserialize_asymmetric_decrypt_req(req_buf, |
| 455 | &handle, &alg, |
| 456 | ciphertext_buffer, &ciphertext_len, |
| 457 | salt_buffer, &salt_len); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 458 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 459 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 460 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 461 | psa_status_t psa_status; |
| 462 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 463 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 464 | psa_status = psa_get_key_attributes(handle, &attributes); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 465 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 466 | if (psa_status == PSA_SUCCESS) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 467 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 468 | size_t max_decrypt_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( |
| 469 | psa_get_key_type(&attributes), |
| 470 | psa_get_key_bits(&attributes), |
| 471 | alg); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 472 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 473 | size_t plaintext_len; |
| 474 | uint8_t *plaintext_buffer = malloc(max_decrypt_size); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 475 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 476 | if (plaintext_buffer) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 477 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 478 | psa_status = psa_asymmetric_decrypt(handle, alg, |
| 479 | ciphertext_buffer, ciphertext_len, |
| 480 | salt_buffer, salt_len, |
| 481 | plaintext_buffer, max_decrypt_size, &plaintext_len); |
| 482 | |
| 483 | if (psa_status == PSA_SUCCESS) { |
| 484 | |
| 485 | struct call_param_buf *resp_buf = call_req_get_resp_buf(req); |
| 486 | rpc_status = serializer->serialize_asymmetric_decrypt_resp(resp_buf, |
| 487 | plaintext_buffer, plaintext_len); |
| 488 | } |
| 489 | |
| 490 | free(plaintext_buffer); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 491 | } |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 492 | else { |
| 493 | /* Failed to allocate ouptput buffer */ |
| 494 | rpc_status = TS_RPC_ERROR_RESOURCE_FAILURE; |
| 495 | } |
| 496 | } |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 497 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 498 | call_req_set_opstatus(req, psa_status); |
| 499 | psa_reset_key_attributes(&attributes); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 500 | } |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 501 | } |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 502 | else { |
| 503 | /* Failed to allocate buffers */ |
| 504 | rpc_status = TS_RPC_ERROR_RESOURCE_FAILURE; |
| 505 | } |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 506 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 507 | free(ciphertext_buffer); |
| 508 | free(salt_buffer); |
| 509 | } |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 510 | |
| 511 | return rpc_status; |
| 512 | } |
| 513 | |
| 514 | static rpc_status_t asymmetric_encrypt_handler(void *context, struct call_req* req) |
| 515 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 516 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 517 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 518 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 519 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 520 | if (serializer) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 521 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 522 | size_t max_param_size = serializer->max_deserialised_parameter_size(req_buf); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 523 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 524 | psa_key_handle_t handle; |
| 525 | psa_algorithm_t alg; |
| 526 | size_t plaintext_len = max_param_size; |
| 527 | uint8_t *plaintext_buffer = malloc(plaintext_len); |
| 528 | size_t salt_len = max_param_size; |
| 529 | uint8_t *salt_buffer = malloc(salt_len); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 530 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 531 | if (plaintext_buffer && salt_buffer) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 532 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 533 | rpc_status = serializer->deserialize_asymmetric_encrypt_req(req_buf, |
| 534 | &handle, &alg, |
| 535 | plaintext_buffer, &plaintext_len, |
| 536 | salt_buffer, &salt_len); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 537 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 538 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 539 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 540 | psa_status_t psa_status; |
| 541 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 542 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 543 | psa_status = psa_get_key_attributes(handle, &attributes); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 544 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 545 | if (psa_status == PSA_SUCCESS) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 546 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 547 | size_t max_encrypt_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( |
| 548 | psa_get_key_type(&attributes), |
| 549 | psa_get_key_bits(&attributes), |
| 550 | alg); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 551 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 552 | size_t ciphertext_len; |
| 553 | uint8_t *ciphertext_buffer = malloc(max_encrypt_size); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 554 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 555 | if (ciphertext_buffer) { |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 556 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 557 | psa_status = psa_asymmetric_encrypt(handle, alg, |
| 558 | plaintext_buffer, plaintext_len, |
| 559 | salt_buffer, salt_len, |
| 560 | ciphertext_buffer, max_encrypt_size, &ciphertext_len); |
| 561 | |
| 562 | if (psa_status == PSA_SUCCESS) { |
| 563 | |
| 564 | struct call_param_buf *resp_buf = call_req_get_resp_buf(req); |
| 565 | rpc_status = serializer->serialize_asymmetric_encrypt_resp(resp_buf, |
| 566 | ciphertext_buffer, ciphertext_len); |
| 567 | } |
| 568 | |
| 569 | free(ciphertext_buffer); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 570 | } |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 571 | else { |
| 572 | /* Failed to allocate ouptput buffer */ |
| 573 | rpc_status = TS_RPC_ERROR_RESOURCE_FAILURE; |
| 574 | } |
| 575 | } |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 576 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 577 | call_req_set_opstatus(req, psa_status); |
| 578 | psa_reset_key_attributes(&attributes); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 579 | } |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 580 | } |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 581 | else { |
| 582 | /* Failed to allocate buffers */ |
| 583 | rpc_status = TS_RPC_ERROR_RESOURCE_FAILURE; |
| 584 | } |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 585 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 586 | free(plaintext_buffer); |
| 587 | free(salt_buffer); |
| 588 | } |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 589 | |
| 590 | return rpc_status; |
| 591 | } |
| 592 | |
| 593 | static rpc_status_t generate_random_handler(void *context, struct call_req* req) |
| 594 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 595 | rpc_status_t rpc_status = TS_RPC_ERROR_SERIALIZATION_NOT_SUPPORTED; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 596 | struct call_param_buf *req_buf = call_req_get_req_buf(req); |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 597 | const struct crypto_provider_serializer *serializer = get_crypto_serializer(context, req); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 598 | |
| 599 | size_t output_size; |
| 600 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 601 | if (serializer) |
| 602 | rpc_status = serializer->deserialize_generate_random_req(req_buf, &output_size); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 603 | |
| 604 | if (rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 605 | |
| 606 | psa_status_t psa_status; |
| 607 | uint8_t *output_buffer = malloc(output_size); |
| 608 | |
| 609 | if (output_buffer) { |
| 610 | |
| 611 | psa_status = psa_generate_random(output_buffer, output_size); |
| 612 | |
| 613 | if (psa_status == PSA_SUCCESS) { |
| 614 | |
| 615 | struct call_param_buf *resp_buf = call_req_get_resp_buf(req); |
| 616 | rpc_status = serializer->serialize_generate_random_resp(resp_buf, |
| 617 | output_buffer, output_size); |
| 618 | } |
| 619 | |
| 620 | call_req_set_opstatus(req, psa_status); |
| 621 | free(output_buffer); |
| 622 | } |
| 623 | else { |
| 624 | /* Failed to allocate output buffer */ |
| 625 | rpc_status = TS_RPC_ERROR_RESOURCE_FAILURE; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | return rpc_status; |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 630 | } |