Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA MAC layer on top of Mbed TLS software crypto |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #include "common.h" |
| 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 24 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 25 | # include <psa/crypto.h> |
| 26 | # include "psa_crypto_core.h" |
| 27 | # include "psa_crypto_mac.h" |
| 28 | # include <mbedtls/md.h> |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 29 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 30 | # include <mbedtls/error.h> |
| 31 | # include <string.h> |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 32 | |
| 33 | /* Use builtin defines specific to this compilation unit, since the test driver |
| 34 | * relies on the software driver. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 35 | # if (defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \ |
| 36 | (defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 37 | defined(MBEDTLS_PSA_ACCEL_ALG_CMAC))) |
| 38 | # define BUILTIN_ALG_CMAC 1 |
| 39 | # endif |
| 40 | # if (defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \ |
| 41 | (defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 42 | defined(MBEDTLS_PSA_ACCEL_ALG_HMAC))) |
| 43 | # define BUILTIN_ALG_HMAC 1 |
| 44 | # endif |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 45 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 46 | # if defined(BUILTIN_ALG_HMAC) |
| 47 | static size_t psa_get_hash_block_size(psa_algorithm_t alg) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 48 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 49 | switch (alg) { |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 50 | case PSA_ALG_MD5: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 51 | return 64; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 52 | case PSA_ALG_RIPEMD160: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 53 | return 64; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 54 | case PSA_ALG_SHA_1: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 55 | return 64; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 56 | case PSA_ALG_SHA_224: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 57 | return 64; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 58 | case PSA_ALG_SHA_256: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 59 | return 64; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 60 | case PSA_ALG_SHA_384: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 61 | return 128; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 62 | case PSA_ALG_SHA_512: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 63 | return 128; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 64 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 65 | return 0; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 69 | static psa_status_t psa_hmac_abort_internal(mbedtls_psa_hmac_operation_t *hmac) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 70 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 71 | mbedtls_platform_zeroize(hmac->opad, sizeof(hmac->opad)); |
| 72 | return psa_hash_abort(&hmac->hash_ctx); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 75 | static psa_status_t psa_hmac_setup_internal(mbedtls_psa_hmac_operation_t *hmac, |
| 76 | const uint8_t *key, |
| 77 | size_t key_length, |
| 78 | psa_algorithm_t hash_alg) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 79 | { |
| 80 | uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; |
| 81 | size_t i; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 82 | size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 83 | size_t block_size = psa_get_hash_block_size(hash_alg); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 84 | psa_status_t status; |
| 85 | |
| 86 | hmac->alg = hash_alg; |
| 87 | |
| 88 | /* Sanity checks on block_size, to guarantee that there won't be a buffer |
| 89 | * overflow below. This should never trigger if the hash algorithm |
| 90 | * is implemented correctly. */ |
| 91 | /* The size checks against the ipad and opad buffers cannot be written |
| 92 | * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )` |
| 93 | * because that triggers -Wlogical-op on GCC 7.3. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 94 | if (block_size > sizeof(ipad)) |
| 95 | return PSA_ERROR_NOT_SUPPORTED; |
| 96 | if (block_size > sizeof(hmac->opad)) |
| 97 | return PSA_ERROR_NOT_SUPPORTED; |
| 98 | if (block_size < hash_size) |
| 99 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 100 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 101 | if (key_length > block_size) { |
| 102 | status = psa_hash_compute(hash_alg, key, key_length, ipad, sizeof(ipad), |
| 103 | &key_length); |
| 104 | if (status != PSA_SUCCESS) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 105 | goto cleanup; |
| 106 | } |
| 107 | /* A 0-length key is not commonly used in HMAC when used as a MAC, |
| 108 | * but it is permitted. It is common when HMAC is used in HKDF, for |
| 109 | * example. Don't call `memcpy` in the 0-length because `key` could be |
| 110 | * an invalid pointer which would make the behavior undefined. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 111 | else if (key_length != 0) |
| 112 | memcpy(ipad, key, key_length); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 113 | |
| 114 | /* ipad contains the key followed by garbage. Xor and fill with 0x36 |
| 115 | * to create the ipad value. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 116 | for (i = 0; i < key_length; i++) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 117 | ipad[i] ^= 0x36; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 118 | memset(ipad + key_length, 0x36, block_size - key_length); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 119 | |
| 120 | /* Copy the key material from ipad to opad, flipping the requisite bits, |
| 121 | * and filling the rest of opad with the requisite constant. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 122 | for (i = 0; i < key_length; i++) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 123 | hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 124 | memset(hmac->opad + key_length, 0x5C, block_size - key_length); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 125 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 126 | status = psa_hash_setup(&hmac->hash_ctx, hash_alg); |
| 127 | if (status != PSA_SUCCESS) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 128 | goto cleanup; |
| 129 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 130 | status = psa_hash_update(&hmac->hash_ctx, ipad, block_size); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 131 | |
| 132 | cleanup: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 133 | mbedtls_platform_zeroize(ipad, sizeof(ipad)); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 134 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 135 | return status; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 138 | static psa_status_t psa_hmac_update_internal(mbedtls_psa_hmac_operation_t *hmac, |
| 139 | const uint8_t *data, |
| 140 | size_t data_length) |
Steven Cooreman | 4fdf060 | 2021-03-22 12:21:10 +0100 | [diff] [blame] | 141 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 142 | return psa_hash_update(&hmac->hash_ctx, data, data_length); |
Steven Cooreman | 4fdf060 | 2021-03-22 12:21:10 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 145 | static psa_status_t psa_hmac_finish_internal(mbedtls_psa_hmac_operation_t *hmac, |
| 146 | uint8_t *mac, |
| 147 | size_t mac_size) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 148 | { |
| 149 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
| 150 | psa_algorithm_t hash_alg = hmac->alg; |
| 151 | size_t hash_size = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 152 | size_t block_size = psa_get_hash_block_size(hash_alg); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 153 | psa_status_t status; |
| 154 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 155 | status = psa_hash_finish(&hmac->hash_ctx, tmp, sizeof(tmp), &hash_size); |
| 156 | if (status != PSA_SUCCESS) |
| 157 | return status; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 158 | /* From here on, tmp needs to be wiped. */ |
| 159 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 160 | status = psa_hash_setup(&hmac->hash_ctx, hash_alg); |
| 161 | if (status != PSA_SUCCESS) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 162 | goto exit; |
| 163 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 164 | status = psa_hash_update(&hmac->hash_ctx, hmac->opad, block_size); |
| 165 | if (status != PSA_SUCCESS) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 166 | goto exit; |
| 167 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 168 | status = psa_hash_update(&hmac->hash_ctx, tmp, hash_size); |
| 169 | if (status != PSA_SUCCESS) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 170 | goto exit; |
| 171 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 172 | status = psa_hash_finish(&hmac->hash_ctx, tmp, sizeof(tmp), &hash_size); |
| 173 | if (status != PSA_SUCCESS) |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 174 | goto exit; |
| 175 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 176 | memcpy(mac, tmp, mac_size); |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 177 | |
| 178 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 179 | mbedtls_platform_zeroize(tmp, hash_size); |
| 180 | return status; |
Steven Cooreman | 82c66b6 | 2021-03-19 17:39:17 +0100 | [diff] [blame] | 181 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 182 | # endif /* BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 183 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 184 | # if defined(BUILTIN_ALG_CMAC) |
| 185 | static psa_status_t cmac_setup(mbedtls_psa_mac_operation_t *operation, |
| 186 | const psa_key_attributes_t *attributes, |
| 187 | const uint8_t *key_buffer) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 188 | { |
| 189 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 0c23965 | 2021-05-07 17:27:27 +0200 | [diff] [blame] | 190 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 191 | # if defined(PSA_WANT_KEY_TYPE_DES) |
Steven Cooreman | 0c23965 | 2021-05-07 17:27:27 +0200 | [diff] [blame] | 192 | /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept |
| 193 | * to do CMAC with pure DES, so return NOT_SUPPORTED here. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 194 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_DES && |
| 195 | (psa_get_key_bits(attributes) == 64 || |
| 196 | psa_get_key_bits(attributes) == 128)) |
| 197 | return PSA_ERROR_NOT_SUPPORTED; |
| 198 | # endif |
Steven Cooreman | 0c23965 | 2021-05-07 17:27:27 +0200 | [diff] [blame] | 199 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 200 | const mbedtls_cipher_info_t *cipher_info = |
| 201 | mbedtls_cipher_info_from_psa(PSA_ALG_CMAC, psa_get_key_type(attributes), |
| 202 | psa_get_key_bits(attributes), NULL); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 203 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 204 | if (cipher_info == NULL) |
| 205 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 206 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 207 | ret = mbedtls_cipher_setup(&operation->ctx.cmac, cipher_info); |
| 208 | if (ret != 0) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 209 | goto exit; |
| 210 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 211 | ret = mbedtls_cipher_cmac_starts(&operation->ctx.cmac, key_buffer, |
| 212 | psa_get_key_bits(attributes)); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 213 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 214 | return mbedtls_to_psa_error(ret); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 215 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 216 | # endif /* BUILTIN_ALG_CMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 217 | |
Steven Cooreman | 02865f5 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 218 | /* Implement the PSA driver MAC interface on top of mbed TLS if either the |
| 219 | * software driver or the test driver requires it. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 220 | # if defined(BUILTIN_ALG_HMAC) || defined(BUILTIN_ALG_CMAC) |
Steven Cooreman | 02865f5 | 2021-05-07 15:55:27 +0200 | [diff] [blame] | 221 | |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 222 | /* Initialize this driver's MAC operation structure. Once this function has been |
| 223 | * called, mbedtls_psa_mac_abort can run and will do the right thing. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 224 | static psa_status_t mac_init(mbedtls_psa_mac_operation_t *operation, |
| 225 | psa_algorithm_t alg) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 226 | { |
Steven Cooreman | ba9a5bf | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 227 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 228 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 229 | operation->alg = alg; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 230 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 231 | # if defined(BUILTIN_ALG_CMAC) |
| 232 | if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) { |
| 233 | mbedtls_cipher_init(&operation->ctx.cmac); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 234 | status = PSA_SUCCESS; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 235 | } else |
| 236 | # endif /* BUILTIN_ALG_CMAC */ |
| 237 | # if defined(BUILTIN_ALG_HMAC) |
| 238 | if (PSA_ALG_IS_HMAC(operation->alg)) { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 239 | /* We'll set up the hash operation later in psa_hmac_setup_internal. */ |
| 240 | operation->ctx.hmac.alg = 0; |
| 241 | status = PSA_SUCCESS; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 242 | } else |
| 243 | # endif /* BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 244 | { |
Steven Cooreman | ba9a5bf | 2021-04-29 16:21:24 +0200 | [diff] [blame] | 245 | status = PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 248 | if (status != PSA_SUCCESS) |
| 249 | memset(operation, 0, sizeof(*operation)); |
| 250 | return status; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 251 | } |
| 252 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 253 | static psa_status_t mac_abort(mbedtls_psa_mac_operation_t *operation) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 254 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 255 | if (operation->alg == 0) { |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 256 | /* The object has (apparently) been initialized but it is not |
| 257 | * in use. It's ok to call abort on such an object, and there's |
| 258 | * nothing to do. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 259 | return PSA_SUCCESS; |
| 260 | } else |
| 261 | # if defined(BUILTIN_ALG_CMAC) |
| 262 | if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) { |
| 263 | mbedtls_cipher_free(&operation->ctx.cmac); |
| 264 | } else |
| 265 | # endif /* BUILTIN_ALG_CMAC */ |
| 266 | # if defined(BUILTIN_ALG_HMAC) |
| 267 | if (PSA_ALG_IS_HMAC(operation->alg)) { |
| 268 | psa_hmac_abort_internal(&operation->ctx.hmac); |
| 269 | } else |
| 270 | # endif /* BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 271 | { |
| 272 | /* Sanity check (shouldn't happen: operation->alg should |
| 273 | * always have been initialized to a valid value). */ |
| 274 | goto bad_state; |
| 275 | } |
| 276 | |
| 277 | operation->alg = 0; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 278 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 279 | return PSA_SUCCESS; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 280 | |
| 281 | bad_state: |
| 282 | /* If abort is called on an uninitialized object, we can't trust |
| 283 | * anything. Wipe the object in case it contains confidential data. |
| 284 | * This may result in a memory leak if a pointer gets overwritten, |
| 285 | * but it's too late to do anything about this. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 286 | memset(operation, 0, sizeof(*operation)); |
| 287 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 288 | } |
| 289 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 290 | static psa_status_t mac_setup(mbedtls_psa_mac_operation_t *operation, |
| 291 | const psa_key_attributes_t *attributes, |
| 292 | const uint8_t *key_buffer, |
| 293 | size_t key_buffer_size, |
| 294 | psa_algorithm_t alg) |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 295 | { |
| 296 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 297 | |
| 298 | /* A context must be freshly initialized before it can be set up. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 299 | if (operation->alg != 0) |
| 300 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 301 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 302 | status = mac_init(operation, alg); |
| 303 | if (status != PSA_SUCCESS) |
| 304 | return status; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 305 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 306 | # if defined(BUILTIN_ALG_CMAC) |
| 307 | if (PSA_ALG_FULL_LENGTH_MAC(alg) == PSA_ALG_CMAC) { |
Steven Cooreman | dcd0811 | 2021-05-06 18:00:37 +0200 | [diff] [blame] | 308 | /* Key buffer size for CMAC is dictated by the key bits set on the |
| 309 | * attributes, and previously validated by the core on key import. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 310 | (void)key_buffer_size; |
| 311 | status = cmac_setup(operation, attributes, key_buffer); |
| 312 | } else |
| 313 | # endif /* BUILTIN_ALG_CMAC */ |
| 314 | # if defined(BUILTIN_ALG_HMAC) |
| 315 | if (PSA_ALG_IS_HMAC(alg)) { |
| 316 | status = psa_hmac_setup_internal(&operation->ctx.hmac, key_buffer, |
| 317 | key_buffer_size, |
| 318 | PSA_ALG_HMAC_GET_HASH(alg)); |
| 319 | } else |
| 320 | # endif /* BUILTIN_ALG_HMAC */ |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 321 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 322 | (void)attributes; |
| 323 | (void)key_buffer; |
| 324 | (void)key_buffer_size; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 325 | status = PSA_ERROR_NOT_SUPPORTED; |
| 326 | } |
| 327 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 328 | if (status != PSA_SUCCESS) |
| 329 | mac_abort(operation); |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 330 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 331 | return status; |
Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 334 | static psa_status_t mac_update(mbedtls_psa_mac_operation_t *operation, |
| 335 | const uint8_t *input, |
| 336 | size_t input_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 337 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 338 | if (operation->alg == 0) |
| 339 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 340 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 341 | # if defined(BUILTIN_ALG_CMAC) |
| 342 | if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) { |
| 343 | return (mbedtls_to_psa_error(mbedtls_cipher_cmac_update( |
| 344 | &operation->ctx.cmac, input, input_length))); |
| 345 | } else |
| 346 | # endif /* BUILTIN_ALG_CMAC */ |
| 347 | # if defined(BUILTIN_ALG_HMAC) |
| 348 | if (PSA_ALG_IS_HMAC(operation->alg)) { |
| 349 | return (psa_hmac_update_internal(&operation->ctx.hmac, input, |
| 350 | input_length)); |
| 351 | } else |
| 352 | # endif /* BUILTIN_ALG_HMAC */ |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 353 | { |
| 354 | /* This shouldn't happen if `operation` was initialized by |
| 355 | * a setup function. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 356 | (void)input; |
| 357 | (void)input_length; |
| 358 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 6e7f291 | 2021-03-19 18:38:46 +0100 | [diff] [blame] | 359 | } |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 362 | static psa_status_t mac_finish_internal(mbedtls_psa_mac_operation_t *operation, |
| 363 | uint8_t *mac, |
| 364 | size_t mac_size) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 365 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 366 | # if defined(BUILTIN_ALG_CMAC) |
| 367 | if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 368 | uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 369 | int ret = mbedtls_cipher_cmac_finish(&operation->ctx.cmac, tmp); |
| 370 | if (ret == 0) |
| 371 | memcpy(mac, tmp, mac_size); |
| 372 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
| 373 | return mbedtls_to_psa_error(ret); |
| 374 | } else |
| 375 | # endif /* BUILTIN_ALG_CMAC */ |
| 376 | # if defined(BUILTIN_ALG_HMAC) |
| 377 | if (PSA_ALG_IS_HMAC(operation->alg)) { |
| 378 | return (psa_hmac_finish_internal(&operation->ctx.hmac, mac, mac_size)); |
| 379 | } else |
| 380 | # endif /* BUILTIN_ALG_HMAC */ |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 381 | { |
| 382 | /* This shouldn't happen if `operation` was initialized by |
| 383 | * a setup function. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 384 | (void)operation; |
| 385 | (void)mac; |
| 386 | (void)mac_size; |
| 387 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 391 | static psa_status_t mac_sign_finish(mbedtls_psa_mac_operation_t *operation, |
| 392 | uint8_t *mac, |
| 393 | size_t mac_size, |
| 394 | size_t *mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 395 | { |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 396 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 397 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 398 | if (operation->alg == 0) |
| 399 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 400 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 401 | status = mac_finish_internal(operation, mac, mac_size); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 402 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 403 | if (status == PSA_SUCCESS) |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 404 | *mac_length = mac_size; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 405 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 406 | return status; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 407 | } |
| 408 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 409 | static psa_status_t mac_verify_finish(mbedtls_psa_mac_operation_t *operation, |
| 410 | const uint8_t *mac, |
| 411 | size_t mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 412 | { |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 413 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
Steven Cooreman | 094a77e | 2021-05-06 17:58:36 +0200 | [diff] [blame] | 414 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 415 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 416 | if (operation->alg == 0) |
| 417 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 418 | |
Steven Cooreman | 72f736a | 2021-05-07 14:14:37 +0200 | [diff] [blame] | 419 | /* Consistency check: requested MAC length fits our local buffer */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 420 | if (mac_length > sizeof(actual_mac)) |
| 421 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 422 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 423 | status = mac_finish_internal(operation, actual_mac, mac_length); |
| 424 | if (status != PSA_SUCCESS) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 425 | goto cleanup; |
| 426 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 427 | if (mbedtls_psa_safer_memcmp(mac, actual_mac, mac_length) != 0) |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 428 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 429 | |
| 430 | cleanup: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 431 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
Steven Cooreman | a5b860a | 2021-03-19 19:04:39 +0100 | [diff] [blame] | 432 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 433 | return status; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 434 | } |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 435 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 436 | static psa_status_t mac_compute(const psa_key_attributes_t *attributes, |
| 437 | const uint8_t *key_buffer, |
| 438 | size_t key_buffer_size, |
| 439 | psa_algorithm_t alg, |
| 440 | const uint8_t *input, |
| 441 | size_t input_length, |
| 442 | uint8_t *mac, |
| 443 | size_t mac_size, |
| 444 | size_t *mac_length) |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 445 | { |
| 446 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 447 | mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT; |
| 448 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 449 | status = |
| 450 | mac_setup(&operation, attributes, key_buffer, key_buffer_size, alg); |
| 451 | if (status != PSA_SUCCESS) |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 452 | goto exit; |
| 453 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 454 | if (input_length > 0) { |
| 455 | status = mac_update(&operation, input, input_length); |
| 456 | if (status != PSA_SUCCESS) |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 457 | goto exit; |
| 458 | } |
| 459 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 460 | status = mac_finish_internal(&operation, mac, mac_size); |
| 461 | if (status == PSA_SUCCESS) |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 462 | *mac_length = mac_size; |
| 463 | |
| 464 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 465 | mac_abort(&operation); |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 466 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 467 | return status; |
Ronald Cron | 76be3e0 | 2021-06-17 17:34:43 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 470 | # endif /* BUILTIN_ALG_HMAC || BUILTIN_ALG_CMAC */ |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 471 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 472 | # if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 473 | psa_status_t mbedtls_psa_mac_compute(const psa_key_attributes_t *attributes, |
| 474 | const uint8_t *key_buffer, |
| 475 | size_t key_buffer_size, |
| 476 | psa_algorithm_t alg, |
| 477 | const uint8_t *input, |
| 478 | size_t input_length, |
| 479 | uint8_t *mac, |
| 480 | size_t mac_size, |
| 481 | size_t *mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 482 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 483 | return (mac_compute(attributes, key_buffer, key_buffer_size, alg, input, |
| 484 | input_length, mac, mac_size, mac_length)); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 487 | psa_status_t mbedtls_psa_mac_sign_setup(mbedtls_psa_mac_operation_t *operation, |
| 488 | const psa_key_attributes_t *attributes, |
| 489 | const uint8_t *key_buffer, |
| 490 | size_t key_buffer_size, |
| 491 | psa_algorithm_t alg) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 492 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 493 | return (mac_setup(operation, attributes, key_buffer, key_buffer_size, alg)); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 496 | psa_status_t |
| 497 | mbedtls_psa_mac_verify_setup(mbedtls_psa_mac_operation_t *operation, |
| 498 | const psa_key_attributes_t *attributes, |
| 499 | const uint8_t *key_buffer, |
| 500 | size_t key_buffer_size, |
| 501 | psa_algorithm_t alg) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 502 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 503 | return (mac_setup(operation, attributes, key_buffer, key_buffer_size, alg)); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 506 | psa_status_t mbedtls_psa_mac_update(mbedtls_psa_mac_operation_t *operation, |
| 507 | const uint8_t *input, |
| 508 | size_t input_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 509 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 510 | return mac_update(operation, input, input_length); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 513 | psa_status_t mbedtls_psa_mac_sign_finish(mbedtls_psa_mac_operation_t *operation, |
| 514 | uint8_t *mac, |
| 515 | size_t mac_size, |
| 516 | size_t *mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 517 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 518 | return mac_sign_finish(operation, mac, mac_size, mac_length); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 521 | psa_status_t |
| 522 | mbedtls_psa_mac_verify_finish(mbedtls_psa_mac_operation_t *operation, |
| 523 | const uint8_t *mac, |
| 524 | size_t mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 525 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 526 | return mac_verify_finish(operation, mac, mac_length); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 529 | psa_status_t mbedtls_psa_mac_abort(mbedtls_psa_mac_operation_t *operation) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 530 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 531 | return mac_abort(operation); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 532 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 533 | # endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 534 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 535 | /* |
| 536 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 537 | */ |
| 538 | # if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 539 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 540 | static int is_mac_accelerated(psa_algorithm_t alg) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 541 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 542 | # if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) |
| 543 | if (PSA_ALG_IS_HMAC(alg)) |
| 544 | return 1; |
| 545 | # endif |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 546 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 547 | switch (PSA_ALG_FULL_LENGTH_MAC(alg)) { |
| 548 | # if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 549 | case PSA_ALG_CMAC: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 550 | return 1; |
| 551 | # endif |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 552 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 553 | return 0; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | psa_status_t mbedtls_transparent_test_driver_mac_compute( |
| 558 | const psa_key_attributes_t *attributes, |
| 559 | const uint8_t *key_buffer, |
| 560 | size_t key_buffer_size, |
| 561 | psa_algorithm_t alg, |
| 562 | const uint8_t *input, |
| 563 | size_t input_length, |
| 564 | uint8_t *mac, |
| 565 | size_t mac_size, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 566 | size_t *mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 567 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 568 | if (is_mac_accelerated(alg)) |
| 569 | return (mac_compute(attributes, key_buffer, key_buffer_size, alg, input, |
| 570 | input_length, mac, mac_size, mac_length)); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 571 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 572 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | psa_status_t mbedtls_transparent_test_driver_mac_sign_setup( |
| 576 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 577 | const psa_key_attributes_t *attributes, |
| 578 | const uint8_t *key_buffer, |
| 579 | size_t key_buffer_size, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 580 | psa_algorithm_t alg) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 581 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 582 | if (is_mac_accelerated(alg)) |
| 583 | return ( |
| 584 | mac_setup(operation, attributes, key_buffer, key_buffer_size, alg)); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 585 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 586 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | psa_status_t mbedtls_transparent_test_driver_mac_verify_setup( |
| 590 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 591 | const psa_key_attributes_t *attributes, |
| 592 | const uint8_t *key_buffer, |
| 593 | size_t key_buffer_size, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 594 | psa_algorithm_t alg) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 595 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 596 | if (is_mac_accelerated(alg)) |
| 597 | return ( |
| 598 | mac_setup(operation, attributes, key_buffer, key_buffer_size, alg)); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 599 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 600 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | psa_status_t mbedtls_transparent_test_driver_mac_update( |
| 604 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 605 | const uint8_t *input, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 606 | size_t input_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 607 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 608 | if (is_mac_accelerated(operation->alg)) |
| 609 | return mac_update(operation, input, input_length); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 610 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 611 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | psa_status_t mbedtls_transparent_test_driver_mac_sign_finish( |
| 615 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 616 | uint8_t *mac, |
| 617 | size_t mac_size, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 618 | size_t *mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 619 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 620 | if (is_mac_accelerated(operation->alg)) |
| 621 | return mac_sign_finish(operation, mac, mac_size, mac_length); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 622 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 623 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | psa_status_t mbedtls_transparent_test_driver_mac_verify_finish( |
| 627 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 628 | const uint8_t *mac, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 629 | size_t mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 630 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 631 | if (is_mac_accelerated(operation->alg)) |
| 632 | return mac_verify_finish(operation, mac, mac_length); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 633 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 634 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | psa_status_t mbedtls_transparent_test_driver_mac_abort( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 638 | mbedtls_transparent_test_driver_mac_operation_t *operation) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 639 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 640 | return mac_abort(operation); |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 641 | } |
| 642 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 643 | psa_status_t |
| 644 | mbedtls_opaque_test_driver_mac_compute(const psa_key_attributes_t *attributes, |
| 645 | const uint8_t *key_buffer, |
| 646 | size_t key_buffer_size, |
| 647 | psa_algorithm_t alg, |
| 648 | const uint8_t *input, |
| 649 | size_t input_length, |
| 650 | uint8_t *mac, |
| 651 | size_t mac_size, |
| 652 | size_t *mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 653 | { |
| 654 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 655 | (void)attributes; |
| 656 | (void)key_buffer; |
| 657 | (void)key_buffer_size; |
| 658 | (void)alg; |
| 659 | (void)input; |
| 660 | (void)input_length; |
| 661 | (void)mac; |
| 662 | (void)mac_size; |
| 663 | (void)mac_length; |
| 664 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | psa_status_t mbedtls_opaque_test_driver_mac_sign_setup( |
| 668 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 669 | const psa_key_attributes_t *attributes, |
| 670 | const uint8_t *key_buffer, |
| 671 | size_t key_buffer_size, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 672 | psa_algorithm_t alg) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 673 | { |
| 674 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 675 | (void)operation; |
| 676 | (void)attributes; |
| 677 | (void)key_buffer; |
| 678 | (void)key_buffer_size; |
| 679 | (void)alg; |
| 680 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | psa_status_t mbedtls_opaque_test_driver_mac_verify_setup( |
| 684 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 685 | const psa_key_attributes_t *attributes, |
| 686 | const uint8_t *key_buffer, |
| 687 | size_t key_buffer_size, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 688 | psa_algorithm_t alg) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 689 | { |
| 690 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 691 | (void)operation; |
| 692 | (void)attributes; |
| 693 | (void)key_buffer; |
| 694 | (void)key_buffer_size; |
| 695 | (void)alg; |
| 696 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | psa_status_t mbedtls_opaque_test_driver_mac_update( |
| 700 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 701 | const uint8_t *input, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 702 | size_t input_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 703 | { |
| 704 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 705 | (void)operation; |
| 706 | (void)input; |
| 707 | (void)input_length; |
| 708 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | psa_status_t mbedtls_opaque_test_driver_mac_sign_finish( |
| 712 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 713 | uint8_t *mac, |
| 714 | size_t mac_size, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 715 | size_t *mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 716 | { |
| 717 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 718 | (void)operation; |
| 719 | (void)mac; |
| 720 | (void)mac_size; |
| 721 | (void)mac_length; |
| 722 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | psa_status_t mbedtls_opaque_test_driver_mac_verify_finish( |
| 726 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 727 | const uint8_t *mac, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 728 | size_t mac_length) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 729 | { |
| 730 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 731 | (void)operation; |
| 732 | (void)mac; |
| 733 | (void)mac_length; |
| 734 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | psa_status_t mbedtls_opaque_test_driver_mac_abort( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 738 | mbedtls_opaque_test_driver_mac_operation_t *operation) |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 739 | { |
| 740 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 741 | (void)operation; |
| 742 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 743 | } |
| 744 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 745 | # endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 746 | |
| 747 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |