blob: 23cc3883ba887c747de77e32a8400acdb1e4b822 [file] [log] [blame]
Maulik Patel5204dc02023-11-08 08:36:31 +00001/*
2 * Copyright (c) 2020-2023, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <string.h>
9#include "psa_adac.h"
10#include "psa_adac_crypto_api.h"
11
12psa_status_t psa_adac_hash(psa_algorithm_t alg,
13 const uint8_t *input,
14 size_t input_size,
15 uint8_t *hash,
16 size_t hash_size,
17 size_t *hash_length)
18{
19 return psa_adac_hash_multiple(alg, &input, &input_size, 1,
20 hash, hash_size, hash_length);
21}
22
23psa_status_t psa_adac_hash_multiple(psa_algorithm_t alg,
24 const uint8_t *inputs[],
25 size_t input_sizes[],
26 size_t input_count,
27 uint8_t hash[],
28 size_t hash_size,
29 size_t *hash_length)
30{
31 psa_status_t status;
32 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
33 if (PSA_ALG_IS_VENDOR_DEFINED(alg) != 0) {
34 // TODO: Add support for extra algorithms
35 status = PSA_ERROR_NOT_SUPPORTED;
36 } else {
37 status = psa_hash_setup(&hash_operation, alg);
38 for (size_t i = 0; (i < input_count) && (status == PSA_SUCCESS); i++) {
39 status = psa_hash_update(&hash_operation, inputs[i], input_sizes[i]);
40
41 }
42 if (PSA_SUCCESS == status) {
43 status = psa_hash_finish(&hash_operation, hash, hash_size, hash_length);
44 } else {
45 /* Free all allocated context in case hashing operation fails */
46 /* Return the failed error status to the callee */
47 (void)psa_hash_abort(&hash_operation);
48 }
49 }
50
51 return status;
52}
53
54psa_status_t psa_adac_hash_verify(psa_algorithm_t alg,
55 const uint8_t input[],
56 size_t input_size,
57 uint8_t hash[],
58 size_t hash_size)
59{
60 psa_status_t status;
61 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
62 if (PSA_ALG_IS_VENDOR_DEFINED(alg) != 0) {
63 // TODO: Add support for extra algorithms
64 status = PSA_ERROR_NOT_SUPPORTED;
65 } else {
66 status = psa_hash_setup(&hash_operation, alg);
67 if (PSA_SUCCESS == status) {
68 status = psa_hash_update(&hash_operation, input, input_size);
69 }
70
71 if (PSA_SUCCESS == status) {
72 status = psa_hash_verify(&hash_operation, hash, hash_size);
73 }
74 }
75
76 return status;
77}
78
79static psa_status_t hash_check(const uint8_t *input_a,
80 size_t len_a,
81 const uint8_t *input_b,
82 size_t len_b)
83{
84 int32_t result = 1;
85
86 if (len_a == len_b) {
87 result = memcmp(input_b, input_a, len_a);
88 }
89
90 return (result == 0U) ? PSA_SUCCESS : PSA_ERROR_INVALID_SIGNATURE;
91}
92
93psa_status_t psa_adac_hash_verify_multiple(psa_algorithm_t alg,
94 const uint8_t input[],
95 size_t input_length,
96 uint8_t *hash[],
97 size_t hash_size[],
98 size_t hash_count)
99{
100 psa_status_t status;
101 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
102 uint8_t computed_hash[PSA_HASH_MAX_SIZE];
103 size_t computed_hash_len;
104
105 if (PSA_ALG_IS_VENDOR_DEFINED(alg) != 0) {
106 // TODO: Add support for extra algorithms
107 status = PSA_ERROR_NOT_SUPPORTED;
108 } else {
109 status = psa_hash_setup(&hash_operation, alg);
110 if (PSA_SUCCESS == status) {
111 status = psa_hash_update(&hash_operation, input, input_length);
112 }
113 if (PSA_SUCCESS == status) {
114 status = psa_hash_finish(&hash_operation, computed_hash,
115 sizeof(computed_hash), &computed_hash_len);
116 }
117 if (PSA_SUCCESS == status) {
118 for (size_t i = 0; i < hash_count; i++) {
119 status = hash_check(hash[i], hash_size[i], computed_hash, computed_hash_len);
120 if (status == PSA_SUCCESS) {
121 break;
122 }
123 }
124 }
125 }
126
127 return status;
128}