blob: e5dd0030bd5a773d7515a9cca1fdbece99d59fd4 [file] [log] [blame]
Julian Halld4071382021-07-07 16:45:53 +01001/*
Julian Hallb8b026e2022-02-11 14:19:26 +00002 * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
Julian Halld4071382021-07-07 16:45:53 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Julian Halld4071382021-07-07 16:45:53 +01007#include <psa/crypto.h>
8#include "psa_crypto_client.h"
Julian Halla9490042021-08-04 10:43:34 +01009#include "crypto_caller_selector.h"
Julian Halld4071382021-07-07 16:45:53 +010010
11psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
12 psa_algorithm_t alg)
13{
Julian Hall7a703402021-08-04 09:20:43 +010014 if (psa_crypto_client_instance.init_status != PSA_SUCCESS)
15 return psa_crypto_client_instance.init_status;
Julian Hall8359a632021-07-08 15:10:30 +010016
Julian Hallb8b026e2022-02-11 14:19:26 +000017 if (operation->handle)
18 return PSA_ERROR_BAD_STATE;
19
Julian Hall7a703402021-08-04 09:20:43 +010020 return crypto_caller_hash_setup(&psa_crypto_client_instance.base,
21 &operation->handle, alg);
Julian Halld4071382021-07-07 16:45:53 +010022}
23
24psa_status_t psa_hash_update(psa_hash_operation_t *operation,
25 const uint8_t *input,
26 size_t input_length)
27{
Julian Hall7a703402021-08-04 09:20:43 +010028 return crypto_caller_hash_update(&psa_crypto_client_instance.base,
29 operation->handle,
30 input, input_length);
Julian Halld4071382021-07-07 16:45:53 +010031}
32
33psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
34 uint8_t *hash,
35 size_t hash_size,
36 size_t *hash_length)
37{
Julian Hall7a703402021-08-04 09:20:43 +010038 return crypto_caller_hash_finish(&psa_crypto_client_instance.base,
39 operation->handle,
40 hash, hash_size, hash_length);
Julian Halld4071382021-07-07 16:45:53 +010041}
42
43psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
44{
Julian Hall7a703402021-08-04 09:20:43 +010045 return crypto_caller_hash_abort(&psa_crypto_client_instance.base,
46 operation->handle);
Julian Halld4071382021-07-07 16:45:53 +010047}
48
49psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
50 const uint8_t *hash,
51 size_t hash_length)
52{
Julian Hall7a703402021-08-04 09:20:43 +010053 return crypto_caller_hash_verify(&psa_crypto_client_instance.base,
54 operation->handle,
55 hash, hash_length);
Julian Halld4071382021-07-07 16:45:53 +010056}
57
58psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
59 psa_hash_operation_t *target_operation)
60{
Julian Hallb8b026e2022-02-11 14:19:26 +000061 if (target_operation->handle)
62 return PSA_ERROR_BAD_STATE;
63
Julian Hall7a703402021-08-04 09:20:43 +010064 return crypto_caller_hash_clone(&psa_crypto_client_instance.base,
65 source_operation->handle,
66 &target_operation->handle);
Julian Halld4071382021-07-07 16:45:53 +010067}
68
Julian Hallf284b092021-07-23 12:00:01 +010069psa_status_t psa_hash_suspend(psa_hash_operation_t *operation,
Julian Hall188953d2021-07-30 12:11:43 +010070 uint8_t *hash_state,
71 size_t hash_state_size,
72 size_t *hash_state_length)
Julian Hallf284b092021-07-23 12:00:01 +010073{
74 return PSA_ERROR_NOT_SUPPORTED;
75}
76
77psa_status_t psa_hash_resume(psa_hash_operation_t *operation,
Julian Hall188953d2021-07-30 12:11:43 +010078 const uint8_t *hash_state,
79 size_t hash_state_length)
Julian Hallf284b092021-07-23 12:00:01 +010080{
81 return PSA_ERROR_NOT_SUPPORTED;
82}
83
Julian Hall188953d2021-07-30 12:11:43 +010084static psa_status_t multi_hash_update(psa_hash_operation_t *operation,
85 psa_algorithm_t alg,
86 const uint8_t *input,
87 size_t input_length)
88{
89 *operation = psa_hash_operation_init();
90 psa_status_t psa_status = psa_hash_setup(operation, alg);
Julian Hall7a703402021-08-04 09:20:43 +010091 size_t max_update_size = crypto_caller_hash_max_update_size(&psa_crypto_client_instance.base);
Julian Hall188953d2021-07-30 12:11:43 +010092
93 if (!max_update_size) {
94
95 /* Don't know the max update size so assume that the entire
96 * input can be handled in a single update. If this isn't
97 * true, the first hash update operation will fail safely.
98 */
99 max_update_size = input_length;
100 }
101
102 if (psa_status == PSA_SUCCESS) {
103
104 size_t bytes_processed = 0;
105
106 while (bytes_processed < input_length) {
107
108 size_t bytes_remaining = input_length - bytes_processed;
109 size_t update_len = (bytes_remaining < max_update_size) ?
110 bytes_remaining :
111 max_update_size;
112
113 psa_status = psa_hash_update(operation, &input[bytes_processed], update_len);
114
115 if (psa_status != PSA_SUCCESS) {
116
117 psa_hash_abort(operation);
118 break;
119 }
120
121 bytes_processed += update_len;
122 }
123 }
124
125 return psa_status;
126}
127
Julian Halld4071382021-07-07 16:45:53 +0100128psa_status_t psa_hash_compare(psa_algorithm_t alg,
129 const uint8_t *input,
130 size_t input_length,
131 const uint8_t *hash,
132 size_t hash_length)
133{
Julian Hall188953d2021-07-30 12:11:43 +0100134 psa_hash_operation_t operation;
135 psa_status_t psa_status = multi_hash_update(&operation, alg, input, input_length);
136
137 if (psa_status == PSA_SUCCESS) {
138
139 psa_status = psa_hash_verify(&operation, hash, hash_length);
Julian Hallc6d7e4d2022-02-16 10:37:04 +0000140
141 if (psa_status != PSA_SUCCESS) {
142
143 psa_hash_abort(&operation);
144 }
Julian Hall188953d2021-07-30 12:11:43 +0100145 }
146
147 return psa_status;
Julian Halld4071382021-07-07 16:45:53 +0100148}
149
150psa_status_t psa_hash_compute(psa_algorithm_t alg,
151 const uint8_t *input,
152 size_t input_length,
153 uint8_t *hash,
154 size_t hash_size,
155 size_t *hash_length)
156{
Julian Hall188953d2021-07-30 12:11:43 +0100157 psa_hash_operation_t operation;
158 psa_status_t psa_status = multi_hash_update(&operation, alg, input, input_length);
159
160 if (psa_status == PSA_SUCCESS) {
161
162 psa_status = psa_hash_finish(&operation, hash, hash_size, hash_length);
Julian Hallc6d7e4d2022-02-16 10:37:04 +0000163
164 if (psa_status != PSA_SUCCESS) {
165
166 psa_hash_abort(&operation);
167 }
Julian Hall188953d2021-07-30 12:11:43 +0100168 }
169
170 return psa_status;
Julian Halld4071382021-07-07 16:45:53 +0100171}