blob: 2e722d2a91e2e60c9e1be826bfa637e812725e44 [file] [log] [blame]
Steven Cooreman896d51e2021-03-19 15:24:23 +01001/*
2 * PSA MAC layer on top of Mbed TLS software crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Steven Cooreman896d51e2021-03-19 15:24:23 +01007 */
8
9#include "common.h"
10
11#if defined(MBEDTLS_PSA_CRYPTO_C)
12
13#include <psa/crypto.h>
14#include "psa_crypto_core.h"
Dave Rodgman8e322b12022-11-02 09:25:38 +000015#include "psa_crypto_cipher.h"
Steven Cooreman896d51e2021-03-19 15:24:23 +010016#include "psa_crypto_mac.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010017#include <mbedtls/md.h>
Steven Cooreman896d51e2021-03-19 15:24:23 +010018
19#include <mbedtls/error.h>
20#include <string.h>
21
Ronald Croncfc3c7b2021-03-13 18:50:11 +010022#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman22dea1d2021-04-29 19:32:25 +020023static psa_status_t psa_hmac_abort_internal(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010024 mbedtls_psa_hmac_operation_t *hmac)
Steven Cooreman32d56942021-03-19 17:39:17 +010025{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010026 mbedtls_platform_zeroize(hmac->opad, sizeof(hmac->opad));
27 return psa_hash_abort(&hmac->hash_ctx);
Steven Cooreman32d56942021-03-19 17:39:17 +010028}
29
Steven Cooreman22dea1d2021-04-29 19:32:25 +020030static psa_status_t psa_hmac_setup_internal(
31 mbedtls_psa_hmac_operation_t *hmac,
32 const uint8_t *key,
33 size_t key_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010034 psa_algorithm_t hash_alg)
Steven Cooreman32d56942021-03-19 17:39:17 +010035{
36 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
37 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010038 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
39 size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg);
Steven Cooreman32d56942021-03-19 17:39:17 +010040 psa_status_t status;
41
42 hmac->alg = hash_alg;
43
44 /* Sanity checks on block_size, to guarantee that there won't be a buffer
45 * overflow below. This should never trigger if the hash algorithm
46 * is implemented correctly. */
47 /* The size checks against the ipad and opad buffers cannot be written
48 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
49 * because that triggers -Wlogical-op on GCC 7.3. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050 if (block_size > sizeof(ipad)) {
51 return PSA_ERROR_NOT_SUPPORTED;
52 }
53 if (block_size > sizeof(hmac->opad)) {
54 return PSA_ERROR_NOT_SUPPORTED;
55 }
56 if (block_size < hash_size) {
57 return PSA_ERROR_NOT_SUPPORTED;
58 }
Steven Cooreman32d56942021-03-19 17:39:17 +010059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 if (key_length > block_size) {
61 status = psa_hash_compute(hash_alg, key, key_length,
62 ipad, sizeof(ipad), &key_length);
63 if (status != PSA_SUCCESS) {
Steven Cooreman32d56942021-03-19 17:39:17 +010064 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 }
Steven Cooreman32d56942021-03-19 17:39:17 +010066 }
67 /* A 0-length key is not commonly used in HMAC when used as a MAC,
68 * but it is permitted. It is common when HMAC is used in HKDF, for
69 * example. Don't call `memcpy` in the 0-length because `key` could be
70 * an invalid pointer which would make the behavior undefined. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 else if (key_length != 0) {
72 memcpy(ipad, key, key_length);
73 }
Steven Cooreman32d56942021-03-19 17:39:17 +010074
75 /* ipad contains the key followed by garbage. Xor and fill with 0x36
76 * to create the ipad value. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 for (i = 0; i < key_length; i++) {
Steven Cooreman32d56942021-03-19 17:39:17 +010078 ipad[i] ^= 0x36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 }
80 memset(ipad + key_length, 0x36, block_size - key_length);
Steven Cooreman32d56942021-03-19 17:39:17 +010081
82 /* Copy the key material from ipad to opad, flipping the requisite bits,
83 * and filling the rest of opad with the requisite constant. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 for (i = 0; i < key_length; i++) {
Steven Cooreman32d56942021-03-19 17:39:17 +010085 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 }
87 memset(hmac->opad + key_length, 0x5C, block_size - key_length);
Steven Cooreman32d56942021-03-19 17:39:17 +010088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 status = psa_hash_setup(&hmac->hash_ctx, hash_alg);
90 if (status != PSA_SUCCESS) {
Steven Cooreman32d56942021-03-19 17:39:17 +010091 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 }
Steven Cooreman32d56942021-03-19 17:39:17 +010093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 status = psa_hash_update(&hmac->hash_ctx, ipad, block_size);
Steven Cooreman32d56942021-03-19 17:39:17 +010095
96cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 mbedtls_platform_zeroize(ipad, sizeof(ipad));
Steven Cooreman32d56942021-03-19 17:39:17 +010098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 return status;
Steven Cooreman32d56942021-03-19 17:39:17 +0100100}
101
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200102static psa_status_t psa_hmac_update_internal(
103 mbedtls_psa_hmac_operation_t *hmac,
104 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 size_t data_length)
Steven Cooreman76720f62021-03-22 12:21:10 +0100106{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 return psa_hash_update(&hmac->hash_ctx, data, data_length);
Steven Cooreman76720f62021-03-22 12:21:10 +0100108}
109
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200110static psa_status_t psa_hmac_finish_internal(
111 mbedtls_psa_hmac_operation_t *hmac,
112 uint8_t *mac,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 size_t mac_size)
Steven Cooreman32d56942021-03-19 17:39:17 +0100114{
Ronald Cron3a95d2b2021-10-18 09:47:58 +0200115 uint8_t tmp[PSA_HASH_MAX_SIZE];
Steven Cooreman32d56942021-03-19 17:39:17 +0100116 psa_algorithm_t hash_alg = hmac->alg;
117 size_t hash_size = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg);
Steven Cooreman32d56942021-03-19 17:39:17 +0100119 psa_status_t status;
120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 status = psa_hash_finish(&hmac->hash_ctx, tmp, sizeof(tmp), &hash_size);
122 if (status != PSA_SUCCESS) {
123 return status;
124 }
Steven Cooreman32d56942021-03-19 17:39:17 +0100125 /* From here on, tmp needs to be wiped. */
126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 status = psa_hash_setup(&hmac->hash_ctx, hash_alg);
128 if (status != PSA_SUCCESS) {
Steven Cooreman32d56942021-03-19 17:39:17 +0100129 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 }
Steven Cooreman32d56942021-03-19 17:39:17 +0100131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 status = psa_hash_update(&hmac->hash_ctx, hmac->opad, block_size);
133 if (status != PSA_SUCCESS) {
Steven Cooreman32d56942021-03-19 17:39:17 +0100134 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 }
Steven Cooreman32d56942021-03-19 17:39:17 +0100136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 status = psa_hash_update(&hmac->hash_ctx, tmp, hash_size);
138 if (status != PSA_SUCCESS) {
Steven Cooreman32d56942021-03-19 17:39:17 +0100139 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 }
Steven Cooreman32d56942021-03-19 17:39:17 +0100141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 status = psa_hash_finish(&hmac->hash_ctx, tmp, sizeof(tmp), &hash_size);
143 if (status != PSA_SUCCESS) {
Steven Cooreman32d56942021-03-19 17:39:17 +0100144 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 }
Steven Cooreman32d56942021-03-19 17:39:17 +0100146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 memcpy(mac, tmp, mac_size);
Steven Cooreman32d56942021-03-19 17:39:17 +0100148
149exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 mbedtls_platform_zeroize(tmp, hash_size);
151 return status;
Steven Cooreman32d56942021-03-19 17:39:17 +0100152}
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100153#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooreman07897832021-03-19 18:28:56 +0100154
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100155#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156static psa_status_t cmac_setup(mbedtls_psa_mac_operation_t *operation,
157 const psa_key_attributes_t *attributes,
158 const uint8_t *key_buffer)
Steven Cooreman07897832021-03-19 18:28:56 +0100159{
160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman63fa40e2021-05-07 17:27:27 +0200161
162#if defined(PSA_WANT_KEY_TYPE_DES)
163 /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept
164 * to do CMAC with pure DES, so return NOT_SUPPORTED here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_DES &&
166 (psa_get_key_bits(attributes) == 64 ||
167 psa_get_key_bits(attributes) == 128)) {
168 return PSA_ERROR_NOT_SUPPORTED;
169 }
Steven Cooreman63fa40e2021-05-07 17:27:27 +0200170#endif
171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 const mbedtls_cipher_info_t *cipher_info =
Steven Cooreman07897832021-03-19 18:28:56 +0100173 mbedtls_cipher_info_from_psa(
174 PSA_ALG_CMAC,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 psa_get_key_type(attributes),
176 psa_get_key_bits(attributes),
177 NULL);
Steven Cooreman07897832021-03-19 18:28:56 +0100178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if (cipher_info == NULL) {
180 return PSA_ERROR_NOT_SUPPORTED;
181 }
Steven Cooreman07897832021-03-19 18:28:56 +0100182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 ret = mbedtls_cipher_setup(&operation->ctx.cmac, cipher_info);
184 if (ret != 0) {
Steven Cooreman07897832021-03-19 18:28:56 +0100185 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 }
Steven Cooreman07897832021-03-19 18:28:56 +0100187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 ret = mbedtls_cipher_cmac_starts(&operation->ctx.cmac,
189 key_buffer,
190 psa_get_key_bits(attributes));
Steven Cooreman07897832021-03-19 18:28:56 +0100191exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 return mbedtls_to_psa_error(ret);
Steven Cooreman07897832021-03-19 18:28:56 +0100193}
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100194#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Steven Cooreman07897832021-03-19 18:28:56 +0100195
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100196#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
197 defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooremanaaf99442021-05-07 15:55:27 +0200198
Steven Cooreman07897832021-03-19 18:28:56 +0100199/* Initialize this driver's MAC operation structure. Once this function has been
200 * called, mbedtls_psa_mac_abort can run and will do the right thing. */
201static psa_status_t mac_init(
202 mbedtls_psa_mac_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 psa_algorithm_t alg)
Steven Cooreman07897832021-03-19 18:28:56 +0100204{
Steven Cooreman6e6451e2021-04-29 16:21:24 +0200205 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman07897832021-03-19 18:28:56 +0100206
Steven Cooreman15f0d922021-05-07 14:14:37 +0200207 operation->alg = alg;
Steven Cooreman07897832021-03-19 18:28:56 +0100208
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100209#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
211 mbedtls_cipher_init(&operation->ctx.cmac);
Steven Cooreman07897832021-03-19 18:28:56 +0100212 status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100214#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
215#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 if (PSA_ALG_IS_HMAC(operation->alg)) {
Steven Cooreman07897832021-03-19 18:28:56 +0100217 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
218 operation->ctx.hmac.alg = 0;
219 status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100221#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooreman07897832021-03-19 18:28:56 +0100222 {
Ronald Cron485559e2021-04-28 14:29:00 +0200223 (void) operation;
Steven Cooreman6e6451e2021-04-29 16:21:24 +0200224 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman07897832021-03-19 18:28:56 +0100225 }
226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 if (status != PSA_SUCCESS) {
228 memset(operation, 0, sizeof(*operation));
229 }
230 return status;
Steven Cooreman07897832021-03-19 18:28:56 +0100231}
232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233psa_status_t mbedtls_psa_mac_abort(mbedtls_psa_mac_operation_t *operation)
Steven Cooreman07897832021-03-19 18:28:56 +0100234{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 if (operation->alg == 0) {
Steven Cooreman07897832021-03-19 18:28:56 +0100236 /* The object has (apparently) been initialized but it is not
237 * in use. It's ok to call abort on such an object, and there's
238 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 return PSA_SUCCESS;
240 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100241#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
243 mbedtls_cipher_free(&operation->ctx.cmac);
244 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100245#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
246#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 if (PSA_ALG_IS_HMAC(operation->alg)) {
248 psa_hmac_abort_internal(&operation->ctx.hmac);
249 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100250#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooreman07897832021-03-19 18:28:56 +0100251 {
252 /* Sanity check (shouldn't happen: operation->alg should
253 * always have been initialized to a valid value). */
254 goto bad_state;
255 }
256
257 operation->alg = 0;
Steven Cooreman07897832021-03-19 18:28:56 +0100258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 return PSA_SUCCESS;
Steven Cooreman07897832021-03-19 18:28:56 +0100260
261bad_state:
262 /* If abort is called on an uninitialized object, we can't trust
263 * anything. Wipe the object in case it contains confidential data.
264 * This may result in a memory leak if a pointer gets overwritten,
265 * but it's too late to do anything about this. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 memset(operation, 0, sizeof(*operation));
267 return PSA_ERROR_BAD_STATE;
Steven Cooreman07897832021-03-19 18:28:56 +0100268}
269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270static psa_status_t psa_mac_setup(mbedtls_psa_mac_operation_t *operation,
271 const psa_key_attributes_t *attributes,
272 const uint8_t *key_buffer,
273 size_t key_buffer_size,
274 psa_algorithm_t alg)
Steven Cooreman07897832021-03-19 18:28:56 +0100275{
276 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
277
278 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 if (operation->alg != 0) {
280 return PSA_ERROR_BAD_STATE;
281 }
Steven Cooreman07897832021-03-19 18:28:56 +0100282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 status = mac_init(operation, alg);
284 if (status != PSA_SUCCESS) {
285 return status;
286 }
Steven Cooreman07897832021-03-19 18:28:56 +0100287
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100288#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 if (PSA_ALG_FULL_LENGTH_MAC(alg) == PSA_ALG_CMAC) {
Steven Cooremanaf81a712021-05-06 18:00:37 +0200290 /* Key buffer size for CMAC is dictated by the key bits set on the
291 * attributes, and previously validated by the core on key import. */
292 (void) key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 status = cmac_setup(operation, attributes, key_buffer);
294 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100295#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
296#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 if (PSA_ALG_IS_HMAC(alg)) {
298 status = psa_hmac_setup_internal(&operation->ctx.hmac,
299 key_buffer,
300 key_buffer_size,
301 PSA_ALG_HMAC_GET_HASH(alg));
302 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100303#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooreman07897832021-03-19 18:28:56 +0100304 {
Steven Cooreman9621f442021-05-10 09:47:05 +0200305 (void) attributes;
306 (void) key_buffer;
307 (void) key_buffer_size;
Steven Cooreman07897832021-03-19 18:28:56 +0100308 status = PSA_ERROR_NOT_SUPPORTED;
309 }
310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if (status != PSA_SUCCESS) {
312 mbedtls_psa_mac_abort(operation);
313 }
Steven Cooreman07897832021-03-19 18:28:56 +0100314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 return status;
Steven Cooreman07897832021-03-19 18:28:56 +0100316}
317
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100318psa_status_t mbedtls_psa_mac_sign_setup(
319 mbedtls_psa_mac_operation_t *operation,
320 const psa_key_attributes_t *attributes,
321 const uint8_t *key_buffer,
322 size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 psa_algorithm_t alg)
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100324{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 return psa_mac_setup(operation, attributes,
326 key_buffer, key_buffer_size, alg);
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100327}
328
329psa_status_t mbedtls_psa_mac_verify_setup(
330 mbedtls_psa_mac_operation_t *operation,
331 const psa_key_attributes_t *attributes,
332 const uint8_t *key_buffer,
333 size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 psa_algorithm_t alg)
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100335{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 return psa_mac_setup(operation, attributes,
337 key_buffer, key_buffer_size, alg);
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100338}
339
340psa_status_t mbedtls_psa_mac_update(
Steven Cooreman896d51e2021-03-19 15:24:23 +0100341 mbedtls_psa_mac_operation_t *operation,
342 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 size_t input_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +0100344{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 if (operation->alg == 0) {
346 return PSA_ERROR_BAD_STATE;
347 }
Steven Cooreman11743f92021-03-19 18:38:46 +0100348
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100349#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
351 return mbedtls_to_psa_error(
352 mbedtls_cipher_cmac_update(&operation->ctx.cmac,
353 input, input_length));
354 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100355#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
356#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 if (PSA_ALG_IS_HMAC(operation->alg)) {
358 return psa_hmac_update_internal(&operation->ctx.hmac,
359 input, input_length);
360 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100361#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooreman11743f92021-03-19 18:38:46 +0100362 {
363 /* This shouldn't happen if `operation` was initialized by
364 * a setup function. */
Steven Cooreman9621f442021-05-10 09:47:05 +0200365 (void) input;
366 (void) input_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 return PSA_ERROR_BAD_STATE;
Steven Cooreman11743f92021-03-19 18:38:46 +0100368 }
Steven Cooreman896d51e2021-03-19 15:24:23 +0100369}
370
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100371static psa_status_t psa_mac_finish_internal(
372 mbedtls_psa_mac_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 uint8_t *mac, size_t mac_size)
Steven Cooreman87885df2021-03-19 19:04:39 +0100374{
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100375#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
Steven Cooreman87885df2021-03-19 19:04:39 +0100377 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 int ret = mbedtls_cipher_cmac_finish(&operation->ctx.cmac, tmp);
379 if (ret == 0) {
380 memcpy(mac, tmp, mac_size);
381 }
382 mbedtls_platform_zeroize(tmp, sizeof(tmp));
383 return mbedtls_to_psa_error(ret);
384 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100385#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
386#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 if (PSA_ALG_IS_HMAC(operation->alg)) {
388 return psa_hmac_finish_internal(&operation->ctx.hmac,
389 mac, mac_size);
390 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100391#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooreman87885df2021-03-19 19:04:39 +0100392 {
393 /* This shouldn't happen if `operation` was initialized by
394 * a setup function. */
Steven Cooreman15f0d922021-05-07 14:14:37 +0200395 (void) operation;
396 (void) mac;
397 (void) mac_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 return PSA_ERROR_BAD_STATE;
Steven Cooreman87885df2021-03-19 19:04:39 +0100399 }
400}
401
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100402psa_status_t mbedtls_psa_mac_sign_finish(
Steven Cooreman896d51e2021-03-19 15:24:23 +0100403 mbedtls_psa_mac_operation_t *operation,
404 uint8_t *mac,
405 size_t mac_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 size_t *mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +0100407{
Steven Cooreman9878a162021-05-06 17:58:36 +0200408 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman87885df2021-03-19 19:04:39 +0100409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 if (operation->alg == 0) {
411 return PSA_ERROR_BAD_STATE;
412 }
Steven Cooreman87885df2021-03-19 19:04:39 +0100413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 status = psa_mac_finish_internal(operation, mac, mac_size);
415 if (status == PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +0200416 *mac_length = mac_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 }
Steven Cooreman87885df2021-03-19 19:04:39 +0100418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 return status;
Steven Cooreman87885df2021-03-19 19:04:39 +0100420}
421
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100422psa_status_t mbedtls_psa_mac_verify_finish(
Steven Cooreman896d51e2021-03-19 15:24:23 +0100423 mbedtls_psa_mac_operation_t *operation,
424 const uint8_t *mac,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 size_t mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +0100426{
Steven Cooreman87885df2021-03-19 19:04:39 +0100427 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman9878a162021-05-06 17:58:36 +0200428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman87885df2021-03-19 19:04:39 +0100429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 if (operation->alg == 0) {
431 return PSA_ERROR_BAD_STATE;
432 }
Steven Cooreman87885df2021-03-19 19:04:39 +0100433
Steven Cooreman15f0d922021-05-07 14:14:37 +0200434 /* Consistency check: requested MAC length fits our local buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 if (mac_length > sizeof(actual_mac)) {
436 return PSA_ERROR_INVALID_ARGUMENT;
437 }
Steven Cooreman87885df2021-03-19 19:04:39 +0100438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 status = psa_mac_finish_internal(operation, actual_mac, mac_length);
440 if (status != PSA_SUCCESS) {
Steven Cooreman87885df2021-03-19 19:04:39 +0100441 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 }
Steven Cooreman87885df2021-03-19 19:04:39 +0100443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 if (mbedtls_psa_safer_memcmp(mac, actual_mac, mac_length) != 0) {
Steven Cooreman87885df2021-03-19 19:04:39 +0100445 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 }
Steven Cooreman87885df2021-03-19 19:04:39 +0100447
448cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Steven Cooreman87885df2021-03-19 19:04:39 +0100450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 return status;
Steven Cooreman896d51e2021-03-19 15:24:23 +0100452}
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200453
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100454psa_status_t mbedtls_psa_mac_compute(
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200455 const psa_key_attributes_t *attributes,
456 const uint8_t *key_buffer,
457 size_t key_buffer_size,
458 psa_algorithm_t alg,
459 const uint8_t *input,
460 size_t input_length,
461 uint8_t *mac,
462 size_t mac_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 size_t *mac_length)
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200464{
465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
466 mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT;
467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 status = psa_mac_setup(&operation,
469 attributes, key_buffer, key_buffer_size,
470 alg);
471 if (status != PSA_SUCCESS) {
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200472 goto exit;
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200473 }
474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 if (input_length > 0) {
476 status = mbedtls_psa_mac_update(&operation, input, input_length);
477 if (status != PSA_SUCCESS) {
478 goto exit;
479 }
480 }
481
482 status = psa_mac_finish_internal(&operation, mac, mac_size);
483 if (status == PSA_SUCCESS) {
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200484 *mac_length = mac_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 }
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200486
487exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 mbedtls_psa_mac_abort(&operation);
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 return status;
Ronald Cronbfdfaa62021-06-17 17:34:43 +0200491}
492
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100493#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Steven Cooreman896d51e2021-03-19 15:24:23 +0100494
495#endif /* MBEDTLS_PSA_CRYPTO_C */