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 | |
Julian Hall | a6d3cbc | 2021-07-20 10:13:21 +0100 | [diff] [blame] | 11 | |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 12 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 13 | psa_key_id_t key, |
| 14 | psa_algorithm_t alg) |
| 15 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 16 | if (psa_crypto_client_instance.init_status != PSA_SUCCESS) |
| 17 | return psa_crypto_client_instance.init_status; |
| 18 | |
Julian Hall | b8b026e | 2022-02-11 14:19:26 +0000 | [diff] [blame] | 19 | if (operation->handle) |
| 20 | return PSA_ERROR_BAD_STATE; |
| 21 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 22 | return crypto_caller_mac_sign_setup(&psa_crypto_client_instance.base, |
| 23 | &operation->handle, |
| 24 | key, alg); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 28 | psa_key_id_t key, |
| 29 | psa_algorithm_t alg) |
| 30 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 31 | if (psa_crypto_client_instance.init_status != PSA_SUCCESS) |
| 32 | return psa_crypto_client_instance.init_status; |
| 33 | |
Julian Hall | b8b026e | 2022-02-11 14:19:26 +0000 | [diff] [blame] | 34 | if (operation->handle) |
| 35 | return PSA_ERROR_BAD_STATE; |
| 36 | |
| 37 | return crypto_caller_mac_verify_setup(&psa_crypto_client_instance.base, |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 38 | &operation->handle, |
| 39 | key, alg); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 43 | const uint8_t *input, |
| 44 | size_t input_length) |
| 45 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 46 | return crypto_caller_mac_update(&psa_crypto_client_instance.base, |
| 47 | operation->handle, |
| 48 | input, input_length); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 52 | uint8_t *mac, |
| 53 | size_t mac_size, |
| 54 | size_t *mac_length) |
| 55 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 56 | return crypto_caller_mac_sign_finish(&psa_crypto_client_instance.base, |
| 57 | operation->handle, |
| 58 | mac, mac_size, mac_length); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 62 | const uint8_t *mac, |
| 63 | size_t mac_length) |
| 64 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 65 | return crypto_caller_mac_verify_finish(&psa_crypto_client_instance.base, |
| 66 | operation->handle, |
| 67 | mac, mac_length); |
Julian Hall | d407138 | 2021-07-07 16:45:53 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
| 71 | { |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 72 | return crypto_caller_mac_abort(&psa_crypto_client_instance.base, |
| 73 | operation->handle); |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static psa_status_t multi_mac_update(psa_mac_operation_t *operation, |
| 77 | const uint8_t *input, |
| 78 | size_t input_length) |
| 79 | { |
| 80 | psa_status_t psa_status = PSA_SUCCESS; |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 81 | size_t max_update_size = crypto_caller_mac_max_update_size(&psa_crypto_client_instance.base); |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 82 | size_t bytes_processed = 0; |
| 83 | |
| 84 | if (!max_update_size) { |
| 85 | |
| 86 | /* Don't know the max update size so assume that the entire |
| 87 | * input can be handled in a single update. If this isn't |
| 88 | * true, the first mac update operation will fail safely. |
| 89 | */ |
| 90 | max_update_size = input_length; |
| 91 | } |
| 92 | |
| 93 | while (bytes_processed < input_length) { |
| 94 | |
| 95 | size_t bytes_remaining = input_length - bytes_processed; |
| 96 | size_t update_len = (bytes_remaining < max_update_size) ? |
| 97 | bytes_remaining : |
| 98 | max_update_size; |
| 99 | |
| 100 | psa_status = psa_mac_update(operation, &input[bytes_processed], update_len); |
| 101 | |
| 102 | if (psa_status != PSA_SUCCESS) { |
| 103 | |
| 104 | psa_mac_abort(operation); |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | bytes_processed += update_len; |
| 109 | } |
| 110 | |
| 111 | return psa_status; |
| 112 | } |
| 113 | |
Julian Hall | d670b41 | 2021-07-19 15:16:27 +0100 | [diff] [blame] | 114 | psa_status_t psa_mac_verify(psa_key_id_t key, |
| 115 | psa_algorithm_t alg, |
| 116 | const uint8_t *input, |
| 117 | size_t input_length, |
| 118 | const uint8_t *mac, |
| 119 | size_t mac_length) |
| 120 | { |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 121 | psa_mac_operation_t operation = psa_mac_operation_init(); |
| 122 | psa_status_t psa_status = psa_mac_verify_setup(&operation, key, alg); |
| 123 | |
| 124 | if (psa_status == PSA_SUCCESS) { |
| 125 | |
| 126 | psa_status = multi_mac_update(&operation, input, input_length); |
| 127 | } |
| 128 | |
| 129 | if (psa_status == PSA_SUCCESS) { |
| 130 | |
| 131 | psa_status = psa_mac_verify_finish(&operation, mac, mac_length); |
Julian Hall | c6d7e4d | 2022-02-16 10:37:04 +0000 | [diff] [blame] | 132 | |
| 133 | if (psa_status != PSA_SUCCESS) { |
| 134 | |
| 135 | psa_mac_abort(&operation); |
| 136 | } |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return psa_status; |
Julian Hall | d670b41 | 2021-07-19 15:16:27 +0100 | [diff] [blame] | 140 | } |
Julian Hall | b7db580 | 2021-07-26 16:20:40 +0100 | [diff] [blame] | 141 | |
| 142 | psa_status_t psa_mac_compute(psa_key_id_t key, |
| 143 | psa_algorithm_t alg, |
| 144 | const uint8_t *input, |
| 145 | size_t input_length, |
| 146 | uint8_t *mac, |
| 147 | size_t mac_size, |
| 148 | size_t *mac_length) |
| 149 | { |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 150 | psa_mac_operation_t operation = psa_mac_operation_init(); |
| 151 | psa_status_t psa_status = psa_mac_sign_setup(&operation, key, alg); |
| 152 | |
| 153 | if (psa_status == PSA_SUCCESS) { |
| 154 | |
| 155 | psa_status = multi_mac_update(&operation, input, input_length); |
| 156 | } |
| 157 | |
| 158 | if (psa_status == PSA_SUCCESS) { |
| 159 | |
| 160 | psa_status = psa_mac_sign_finish(&operation, mac, mac_size, mac_length); |
Julian Hall | c6d7e4d | 2022-02-16 10:37:04 +0000 | [diff] [blame] | 161 | |
| 162 | if (psa_status != PSA_SUCCESS) { |
| 163 | |
| 164 | psa_mac_abort(&operation); |
| 165 | } |
Julian Hall | 188953d | 2021-07-30 12:11:43 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return psa_status; |
Julian Hall | b7db580 | 2021-07-26 16:20:40 +0100 | [diff] [blame] | 169 | } |