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 | |
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 | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 17 | return crypto_caller_hash_setup(&psa_crypto_client_instance.base, |
| 18 | &operation->handle, alg); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 22 | const uint8_t *input, |
| 23 | size_t input_length) |
| 24 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 25 | return crypto_caller_hash_update(&psa_crypto_client_instance.base, |
| 26 | operation->handle, |
| 27 | input, input_length); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 31 | uint8_t *hash, |
| 32 | size_t hash_size, |
| 33 | size_t *hash_length) |
| 34 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 35 | return crypto_caller_hash_finish(&psa_crypto_client_instance.base, |
| 36 | operation->handle, |
| 37 | hash, hash_size, hash_length); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
| 41 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 42 | return crypto_caller_hash_abort(&psa_crypto_client_instance.base, |
| 43 | operation->handle); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 47 | const uint8_t *hash, |
| 48 | size_t hash_length) |
| 49 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 50 | return crypto_caller_hash_verify(&psa_crypto_client_instance.base, |
| 51 | operation->handle, |
| 52 | hash, hash_length); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 56 | psa_hash_operation_t *target_operation) |
| 57 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 58 | return crypto_caller_hash_clone(&psa_crypto_client_instance.base, |
| 59 | source_operation->handle, |
| 60 | &target_operation->handle); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Julian Hall | f284b09 | 2021-07-23 12:00:01 +0100 | [diff] [blame] | 63 | psa_status_t psa_hash_suspend(psa_hash_operation_t *operation, |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 64 | uint8_t *hash_state, |
| 65 | size_t hash_state_size, |
| 66 | size_t *hash_state_length) |
Julian Hall | f284b09 | 2021-07-23 12:00:01 +0100 | [diff] [blame] | 67 | { |
| 68 | return PSA_ERROR_NOT_SUPPORTED; |
| 69 | } |
| 70 | |
| 71 | psa_status_t psa_hash_resume(psa_hash_operation_t *operation, |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 72 | const uint8_t *hash_state, |
| 73 | size_t hash_state_length) |
Julian Hall | f284b09 | 2021-07-23 12:00:01 +0100 | [diff] [blame] | 74 | { |
| 75 | return PSA_ERROR_NOT_SUPPORTED; |
| 76 | } |
| 77 | |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 78 | static psa_status_t multi_hash_update(psa_hash_operation_t *operation, |
| 79 | psa_algorithm_t alg, |
| 80 | const uint8_t *input, |
| 81 | size_t input_length) |
| 82 | { |
| 83 | *operation = psa_hash_operation_init(); |
| 84 | psa_status_t psa_status = psa_hash_setup(operation, alg); |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 85 | 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] | 86 | |
| 87 | if (!max_update_size) { |
| 88 | |
| 89 | /* Don't know the max update size so assume that the entire |
| 90 | * input can be handled in a single update. If this isn't |
| 91 | * true, the first hash update operation will fail safely. |
| 92 | */ |
| 93 | max_update_size = input_length; |
| 94 | } |
| 95 | |
| 96 | if (psa_status == PSA_SUCCESS) { |
| 97 | |
| 98 | size_t bytes_processed = 0; |
| 99 | |
| 100 | while (bytes_processed < input_length) { |
| 101 | |
| 102 | size_t bytes_remaining = input_length - bytes_processed; |
| 103 | size_t update_len = (bytes_remaining < max_update_size) ? |
| 104 | bytes_remaining : |
| 105 | max_update_size; |
| 106 | |
| 107 | psa_status = psa_hash_update(operation, &input[bytes_processed], update_len); |
| 108 | |
| 109 | if (psa_status != PSA_SUCCESS) { |
| 110 | |
| 111 | psa_hash_abort(operation); |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | bytes_processed += update_len; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return psa_status; |
| 120 | } |
| 121 | |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 122 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 123 | const uint8_t *input, |
| 124 | size_t input_length, |
| 125 | const uint8_t *hash, |
| 126 | size_t hash_length) |
| 127 | { |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 128 | psa_hash_operation_t operation; |
| 129 | psa_status_t psa_status = multi_hash_update(&operation, alg, input, input_length); |
| 130 | |
| 131 | if (psa_status == PSA_SUCCESS) { |
| 132 | |
| 133 | psa_status = psa_hash_verify(&operation, hash, hash_length); |
| 134 | } |
| 135 | |
| 136 | return psa_status; |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 140 | const uint8_t *input, |
| 141 | size_t input_length, |
| 142 | uint8_t *hash, |
| 143 | size_t hash_size, |
| 144 | size_t *hash_length) |
| 145 | { |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 146 | psa_hash_operation_t operation; |
| 147 | psa_status_t psa_status = multi_hash_update(&operation, alg, input, input_length); |
| 148 | |
| 149 | if (psa_status == PSA_SUCCESS) { |
| 150 | |
| 151 | psa_status = psa_hash_finish(&operation, hash, hash_size, hash_length); |
| 152 | } |
| 153 | |
| 154 | return psa_status; |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 155 | } |