Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <string.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <psa/crypto.h> |
| 10 | #include "psa_crypto_client.h" |
| 11 | #include <protocols/rpc/common/packed-c/status.h> |
| 12 | #include <protocols/service/crypto/packed-c/opcodes.h> |
| 13 | #include <protocols/service/crypto/packed-c/hash.h> |
| 14 | #include <common/tlv/tlv.h> |
| 15 | |
| 16 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 17 | psa_algorithm_t alg) |
| 18 | { |
Julian Hall | 8359a63 | 2021-07-08 15:10:30 +0100 | [diff] [blame^] | 19 | psa_status_t psa_status = PSA_ERROR_GENERIC_ERROR; |
| 20 | struct ts_crypto_hash_setup_in req_msg; |
| 21 | size_t req_len = sizeof(struct ts_crypto_hash_setup_in); |
| 22 | |
| 23 | req_msg.alg = alg; |
| 24 | |
| 25 | rpc_call_handle call_handle; |
| 26 | uint8_t *req_buf; |
| 27 | |
| 28 | call_handle = rpc_caller_begin(psa_crypto_client_instance.caller, &req_buf, req_len); |
| 29 | |
| 30 | if (call_handle) { |
| 31 | |
| 32 | uint8_t *resp_buf; |
| 33 | size_t resp_len; |
| 34 | int opstatus; |
| 35 | |
| 36 | memcpy(req_buf, &req_msg, req_len); |
| 37 | |
| 38 | psa_crypto_client_instance.rpc_status = |
| 39 | rpc_caller_invoke(psa_crypto_client_instance.caller, call_handle, |
| 40 | TS_CRYPTO_OPCODE_HASH_SETUP, &opstatus, &resp_buf, &resp_len); |
| 41 | |
| 42 | if (psa_crypto_client_instance.rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 43 | |
| 44 | psa_status = opstatus; |
| 45 | |
| 46 | if (psa_status == PSA_SUCCESS) { |
| 47 | |
| 48 | if (resp_len >= sizeof(struct ts_crypto_hash_setup_out)) { |
| 49 | |
| 50 | struct ts_crypto_hash_setup_out resp_msg; |
| 51 | memcpy(&resp_msg, resp_buf, sizeof(struct ts_crypto_hash_setup_out)); |
| 52 | operation->handle = resp_msg.op_handle; |
| 53 | } |
| 54 | else { |
| 55 | /* Failed to decode response message */ |
| 56 | psa_status = PSA_ERROR_GENERIC_ERROR; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | rpc_caller_end(psa_crypto_client_instance.caller, call_handle); |
| 62 | } |
| 63 | |
| 64 | return psa_status; |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 68 | const uint8_t *input, |
| 69 | size_t input_length) |
| 70 | { |
Julian Hall | 8359a63 | 2021-07-08 15:10:30 +0100 | [diff] [blame^] | 71 | psa_status_t psa_status = PSA_ERROR_GENERIC_ERROR; |
| 72 | struct ts_crypto_hash_update_in req_msg; |
| 73 | size_t req_fixed_len = sizeof(struct ts_crypto_hash_update_in); |
| 74 | size_t req_len = req_fixed_len; |
| 75 | |
| 76 | req_msg.op_handle = operation->handle; |
| 77 | |
| 78 | /* Mandatory input data parameter */ |
| 79 | struct tlv_record data_record; |
| 80 | data_record.tag = TS_CRYPTO_HASH_UPDATE_IN_TAG_DATA; |
| 81 | data_record.length = input_length; |
| 82 | data_record.value = input; |
| 83 | req_len += tlv_required_space(data_record.length); |
| 84 | |
| 85 | rpc_call_handle call_handle; |
| 86 | uint8_t *req_buf; |
| 87 | |
| 88 | call_handle = rpc_caller_begin(psa_crypto_client_instance.caller, &req_buf, req_len); |
| 89 | |
| 90 | if (call_handle) { |
| 91 | |
| 92 | uint8_t *resp_buf; |
| 93 | size_t resp_len; |
| 94 | int opstatus; |
| 95 | struct tlv_iterator req_iter; |
| 96 | |
| 97 | memcpy(req_buf, &req_msg, req_fixed_len); |
| 98 | |
| 99 | tlv_iterator_begin(&req_iter, &req_buf[req_fixed_len], req_len - req_fixed_len); |
| 100 | tlv_encode(&req_iter, &data_record); |
| 101 | |
| 102 | psa_crypto_client_instance.rpc_status = |
| 103 | rpc_caller_invoke(psa_crypto_client_instance.caller, call_handle, |
| 104 | TS_CRYPTO_OPCODE_HASH_UPDATE, &opstatus, &resp_buf, &resp_len); |
| 105 | |
| 106 | if (psa_crypto_client_instance.rpc_status == TS_RPC_CALL_ACCEPTED) psa_status = opstatus; |
| 107 | |
| 108 | rpc_caller_end(psa_crypto_client_instance.caller, call_handle); |
| 109 | } |
| 110 | |
| 111 | return psa_status; |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 115 | uint8_t *hash, |
| 116 | size_t hash_size, |
| 117 | size_t *hash_length) |
| 118 | { |
Julian Hall | 8359a63 | 2021-07-08 15:10:30 +0100 | [diff] [blame^] | 119 | psa_status_t psa_status = PSA_ERROR_GENERIC_ERROR; |
| 120 | struct ts_crypto_hash_finish_in req_msg; |
| 121 | size_t req_fixed_len = sizeof(struct ts_crypto_hash_finish_in); |
| 122 | size_t req_len = req_fixed_len; |
| 123 | |
| 124 | *hash_length = 0; |
| 125 | req_msg.op_handle = operation->handle; |
| 126 | |
| 127 | rpc_call_handle call_handle; |
| 128 | uint8_t *req_buf; |
| 129 | |
| 130 | call_handle = rpc_caller_begin(psa_crypto_client_instance.caller, &req_buf, req_len); |
| 131 | |
| 132 | if (call_handle) { |
| 133 | |
| 134 | uint8_t *resp_buf; |
| 135 | size_t resp_len; |
| 136 | int opstatus; |
| 137 | |
| 138 | memcpy(req_buf, &req_msg, req_fixed_len); |
| 139 | |
| 140 | psa_crypto_client_instance.rpc_status = |
| 141 | rpc_caller_invoke(psa_crypto_client_instance.caller, call_handle, |
| 142 | TS_CRYPTO_OPCODE_HASH_FINISH, &opstatus, &resp_buf, &resp_len); |
| 143 | |
| 144 | if (psa_crypto_client_instance.rpc_status == TS_RPC_CALL_ACCEPTED) { |
| 145 | |
| 146 | psa_status = opstatus; |
| 147 | |
| 148 | if (psa_status == PSA_SUCCESS) { |
| 149 | |
| 150 | struct tlv_const_iterator resp_iter; |
| 151 | struct tlv_record decoded_record; |
| 152 | tlv_const_iterator_begin(&resp_iter, resp_buf, resp_len); |
| 153 | |
| 154 | if (tlv_find_decode(&resp_iter, |
| 155 | TS_CRYPTO_HASH_FINISH_OUT_TAG_HASH, &decoded_record)) { |
| 156 | |
| 157 | if (decoded_record.length <= hash_size) { |
| 158 | |
| 159 | memcpy(hash, decoded_record.value, decoded_record.length); |
| 160 | *hash_length = decoded_record.length; |
| 161 | } |
| 162 | else { |
| 163 | /* Provided buffer is too small */ |
| 164 | psa_status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 165 | } |
| 166 | } |
| 167 | else { |
| 168 | /* Mandatory response parameter missing */ |
| 169 | psa_status = PSA_ERROR_GENERIC_ERROR; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | rpc_caller_end(psa_crypto_client_instance.caller, call_handle); |
| 175 | } |
| 176 | |
| 177 | return psa_status; |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
| 181 | { |
| 182 | return PSA_ERROR_NOT_SUPPORTED; |
| 183 | } |
| 184 | |
| 185 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 186 | const uint8_t *hash, |
| 187 | size_t hash_length) |
| 188 | { |
| 189 | return PSA_ERROR_NOT_SUPPORTED; |
| 190 | } |
| 191 | |
| 192 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 193 | psa_hash_operation_t *target_operation) |
| 194 | { |
| 195 | return PSA_ERROR_NOT_SUPPORTED; |
| 196 | } |
| 197 | |
| 198 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 199 | const uint8_t *input, |
| 200 | size_t input_length, |
| 201 | const uint8_t *hash, |
| 202 | size_t hash_length) |
| 203 | { |
| 204 | return PSA_ERROR_NOT_SUPPORTED; |
| 205 | } |
| 206 | |
| 207 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 208 | const uint8_t *input, |
| 209 | size_t input_length, |
| 210 | uint8_t *hash, |
| 211 | size_t hash_size, |
| 212 | size_t *hash_length) |
| 213 | { |
| 214 | return PSA_ERROR_NOT_SUPPORTED; |
| 215 | } |