Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 1 | /* |
Julian Hall | b8b026e | 2022-02-11 14:19:26 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved. |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 7 | #include <psa/crypto.h> |
| 8 | #include "psa_crypto_client.h" |
Julian Hall | a949004 | 2021-08-04 10:43:34 +0100 | [diff] [blame] | 9 | #include "crypto_caller_selector.h" |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 10 | |
| 11 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 12 | psa_algorithm_t alg) |
| 13 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 14 | if (psa_crypto_client_instance.init_status != PSA_SUCCESS) |
| 15 | return psa_crypto_client_instance.init_status; |
Julian Hall | 8359a63 | 2021-07-08 15:10:30 +0100 | [diff] [blame] | 16 | |
Julian Hall | b8b026e | 2022-02-11 14:19:26 +0000 | [diff] [blame^] | 17 | if (operation->handle) |
| 18 | return PSA_ERROR_BAD_STATE; |
| 19 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 20 | return crypto_caller_hash_setup(&psa_crypto_client_instance.base, |
| 21 | &operation->handle, alg); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 25 | const uint8_t *input, |
| 26 | size_t input_length) |
| 27 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 28 | return crypto_caller_hash_update(&psa_crypto_client_instance.base, |
| 29 | operation->handle, |
| 30 | input, input_length); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | psa_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 Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 38 | return crypto_caller_hash_finish(&psa_crypto_client_instance.base, |
| 39 | operation->handle, |
| 40 | hash, hash_size, hash_length); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
| 44 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 45 | return crypto_caller_hash_abort(&psa_crypto_client_instance.base, |
| 46 | operation->handle); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 50 | const uint8_t *hash, |
| 51 | size_t hash_length) |
| 52 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 53 | return crypto_caller_hash_verify(&psa_crypto_client_instance.base, |
| 54 | operation->handle, |
| 55 | hash, hash_length); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 59 | psa_hash_operation_t *target_operation) |
| 60 | { |
Julian Hall | b8b026e | 2022-02-11 14:19:26 +0000 | [diff] [blame^] | 61 | if (target_operation->handle) |
| 62 | return PSA_ERROR_BAD_STATE; |
| 63 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 64 | return crypto_caller_hash_clone(&psa_crypto_client_instance.base, |
| 65 | source_operation->handle, |
| 66 | &target_operation->handle); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Julian Hall | f284b09 | 2021-07-23 12:00:01 +0100 | [diff] [blame] | 69 | psa_status_t psa_hash_suspend(psa_hash_operation_t *operation, |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 70 | uint8_t *hash_state, |
| 71 | size_t hash_state_size, |
| 72 | size_t *hash_state_length) |
Julian Hall | f284b09 | 2021-07-23 12:00:01 +0100 | [diff] [blame] | 73 | { |
| 74 | return PSA_ERROR_NOT_SUPPORTED; |
| 75 | } |
| 76 | |
| 77 | psa_status_t psa_hash_resume(psa_hash_operation_t *operation, |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 78 | const uint8_t *hash_state, |
| 79 | size_t hash_state_length) |
Julian Hall | f284b09 | 2021-07-23 12:00:01 +0100 | [diff] [blame] | 80 | { |
| 81 | return PSA_ERROR_NOT_SUPPORTED; |
| 82 | } |
| 83 | |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 84 | static 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 Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 91 | size_t max_update_size = crypto_caller_hash_max_update_size(&psa_crypto_client_instance.base); |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 92 | |
| 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 Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 128 | psa_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 Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 134 | 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); |
| 140 | } |
| 141 | |
| 142 | return psa_status; |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 146 | const uint8_t *input, |
| 147 | size_t input_length, |
| 148 | uint8_t *hash, |
| 149 | size_t hash_size, |
| 150 | size_t *hash_length) |
| 151 | { |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 152 | psa_hash_operation_t operation; |
| 153 | psa_status_t psa_status = multi_hash_update(&operation, alg, input, input_length); |
| 154 | |
| 155 | if (psa_status == PSA_SUCCESS) { |
| 156 | |
| 157 | psa_status = psa_hash_finish(&operation, hash, hash_size, hash_length); |
| 158 | } |
| 159 | |
| 160 | return psa_status; |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 161 | } |