blob: 2f2c51dce5adf6d037331b99239ca1f7ae13273b [file] [log] [blame]
Steven Cooremand13a70f2021-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
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
25#include <psa/crypto.h>
26#include "psa_crypto_core.h"
Dave Rodgman16304472022-11-02 09:25:38 +000027#include "psa_crypto_cipher.h"
Steven Cooremand13a70f2021-03-19 15:24:23 +010028#include "psa_crypto_mac.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include <mbedtls/md.h>
Steven Cooremand13a70f2021-03-19 15:24:23 +010030
31#include <mbedtls/error.h>
Dave Rodgman78701152023-08-29 14:20:18 +010032#include "mbedtls/constant_time.h"
Steven Cooremand13a70f2021-03-19 15:24:23 +010033#include <string.h>
34
Ronald Cron0266cfe2021-03-13 18:50:11 +010035#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooremana6df6042021-04-29 19:32:25 +020036static psa_status_t psa_hmac_abort_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +010037 mbedtls_psa_hmac_operation_t *hmac)
Steven Cooreman82c66b62021-03-19 17:39:17 +010038{
Gilles Peskine449bd832023-01-11 14:50:10 +010039 mbedtls_platform_zeroize(hmac->opad, sizeof(hmac->opad));
40 return psa_hash_abort(&hmac->hash_ctx);
Steven Cooreman82c66b62021-03-19 17:39:17 +010041}
42
Steven Cooremana6df6042021-04-29 19:32:25 +020043static psa_status_t psa_hmac_setup_internal(
44 mbedtls_psa_hmac_operation_t *hmac,
45 const uint8_t *key,
46 size_t key_length,
Gilles Peskine449bd832023-01-11 14:50:10 +010047 psa_algorithm_t hash_alg)
Steven Cooreman82c66b62021-03-19 17:39:17 +010048{
49 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
50 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010051 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
52 size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg);
Steven Cooreman82c66b62021-03-19 17:39:17 +010053 psa_status_t status;
54
55 hmac->alg = hash_alg;
56
57 /* Sanity checks on block_size, to guarantee that there won't be a buffer
58 * overflow below. This should never trigger if the hash algorithm
59 * is implemented correctly. */
60 /* The size checks against the ipad and opad buffers cannot be written
61 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
62 * because that triggers -Wlogical-op on GCC 7.3. */
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if (block_size > sizeof(ipad)) {
64 return PSA_ERROR_NOT_SUPPORTED;
65 }
66 if (block_size > sizeof(hmac->opad)) {
67 return PSA_ERROR_NOT_SUPPORTED;
68 }
69 if (block_size < hash_size) {
70 return PSA_ERROR_NOT_SUPPORTED;
71 }
Steven Cooreman82c66b62021-03-19 17:39:17 +010072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (key_length > block_size) {
74 status = psa_hash_compute(hash_alg, key, key_length,
75 ipad, sizeof(ipad), &key_length);
76 if (status != PSA_SUCCESS) {
Steven Cooreman82c66b62021-03-19 17:39:17 +010077 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 }
Steven Cooreman82c66b62021-03-19 17:39:17 +010079 }
80 /* A 0-length key is not commonly used in HMAC when used as a MAC,
81 * but it is permitted. It is common when HMAC is used in HKDF, for
82 * example. Don't call `memcpy` in the 0-length because `key` could be
83 * an invalid pointer which would make the behavior undefined. */
Gilles Peskine449bd832023-01-11 14:50:10 +010084 else if (key_length != 0) {
85 memcpy(ipad, key, key_length);
86 }
Steven Cooreman82c66b62021-03-19 17:39:17 +010087
88 /* ipad contains the key followed by garbage. Xor and fill with 0x36
89 * to create the ipad value. */
Gilles Peskine449bd832023-01-11 14:50:10 +010090 for (i = 0; i < key_length; i++) {
Steven Cooreman82c66b62021-03-19 17:39:17 +010091 ipad[i] ^= 0x36;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 }
93 memset(ipad + key_length, 0x36, block_size - key_length);
Steven Cooreman82c66b62021-03-19 17:39:17 +010094
95 /* Copy the key material from ipad to opad, flipping the requisite bits,
96 * and filling the rest of opad with the requisite constant. */
Gilles Peskine449bd832023-01-11 14:50:10 +010097 for (i = 0; i < key_length; i++) {
Steven Cooreman82c66b62021-03-19 17:39:17 +010098 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 }
100 memset(hmac->opad + key_length, 0x5C, block_size - key_length);
Steven Cooreman82c66b62021-03-19 17:39:17 +0100101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 status = psa_hash_setup(&hmac->hash_ctx, hash_alg);
103 if (status != PSA_SUCCESS) {
Steven Cooreman82c66b62021-03-19 17:39:17 +0100104 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 }
Steven Cooreman82c66b62021-03-19 17:39:17 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 status = psa_hash_update(&hmac->hash_ctx, ipad, block_size);
Steven Cooreman82c66b62021-03-19 17:39:17 +0100108
109cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_platform_zeroize(ipad, sizeof(ipad));
Steven Cooreman82c66b62021-03-19 17:39:17 +0100111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 return status;
Steven Cooreman82c66b62021-03-19 17:39:17 +0100113}
114
Steven Cooremana6df6042021-04-29 19:32:25 +0200115static psa_status_t psa_hmac_update_internal(
116 mbedtls_psa_hmac_operation_t *hmac,
117 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 size_t data_length)
Steven Cooreman4fdf0602021-03-22 12:21:10 +0100119{
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return psa_hash_update(&hmac->hash_ctx, data, data_length);
Steven Cooreman4fdf0602021-03-22 12:21:10 +0100121}
122
Steven Cooremana6df6042021-04-29 19:32:25 +0200123static psa_status_t psa_hmac_finish_internal(
124 mbedtls_psa_hmac_operation_t *hmac,
125 uint8_t *mac,
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 size_t mac_size)
Steven Cooreman82c66b62021-03-19 17:39:17 +0100127{
Ronald Cron69a63422021-10-18 09:47:58 +0200128 uint8_t tmp[PSA_HASH_MAX_SIZE];
Steven Cooreman82c66b62021-03-19 17:39:17 +0100129 psa_algorithm_t hash_alg = hmac->alg;
130 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg);
Steven Cooreman82c66b62021-03-19 17:39:17 +0100132 psa_status_t status;
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 status = psa_hash_finish(&hmac->hash_ctx, tmp, sizeof(tmp), &hash_size);
135 if (status != PSA_SUCCESS) {
136 return status;
137 }
Steven Cooreman82c66b62021-03-19 17:39:17 +0100138 /* From here on, tmp needs to be wiped. */
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 status = psa_hash_setup(&hmac->hash_ctx, hash_alg);
141 if (status != PSA_SUCCESS) {
Steven Cooreman82c66b62021-03-19 17:39:17 +0100142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 }
Steven Cooreman82c66b62021-03-19 17:39:17 +0100144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 status = psa_hash_update(&hmac->hash_ctx, hmac->opad, block_size);
146 if (status != PSA_SUCCESS) {
Steven Cooreman82c66b62021-03-19 17:39:17 +0100147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 }
Steven Cooreman82c66b62021-03-19 17:39:17 +0100149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 status = psa_hash_update(&hmac->hash_ctx, tmp, hash_size);
151 if (status != PSA_SUCCESS) {
Steven Cooreman82c66b62021-03-19 17:39:17 +0100152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 }
Steven Cooreman82c66b62021-03-19 17:39:17 +0100154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 status = psa_hash_finish(&hmac->hash_ctx, tmp, sizeof(tmp), &hash_size);
156 if (status != PSA_SUCCESS) {
Steven Cooreman82c66b62021-03-19 17:39:17 +0100157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 }
Steven Cooreman82c66b62021-03-19 17:39:17 +0100159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 memcpy(mac, tmp, mac_size);
Steven Cooreman82c66b62021-03-19 17:39:17 +0100161
162exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 mbedtls_platform_zeroize(tmp, hash_size);
164 return status;
Steven Cooreman82c66b62021-03-19 17:39:17 +0100165}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100166#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100167
Ronald Cron0266cfe2021-03-13 18:50:11 +0100168#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100169static psa_status_t cmac_setup(mbedtls_psa_mac_operation_t *operation,
170 const psa_key_attributes_t *attributes,
171 const uint8_t *key_buffer)
Steven Cooremane6804192021-03-19 18:28:56 +0100172{
173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman0c239652021-05-07 17:27:27 +0200174
175#if defined(PSA_WANT_KEY_TYPE_DES)
176 /* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept
177 * to do CMAC with pure DES, so return NOT_SUPPORTED here. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_DES &&
179 (psa_get_key_bits(attributes) == 64 ||
180 psa_get_key_bits(attributes) == 128)) {
181 return PSA_ERROR_NOT_SUPPORTED;
182 }
Steven Cooreman0c239652021-05-07 17:27:27 +0200183#endif
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 const mbedtls_cipher_info_t *cipher_info =
Steven Cooremane6804192021-03-19 18:28:56 +0100186 mbedtls_cipher_info_from_psa(
187 PSA_ALG_CMAC,
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 psa_get_key_type(attributes),
189 psa_get_key_bits(attributes),
190 NULL);
Steven Cooremane6804192021-03-19 18:28:56 +0100191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (cipher_info == NULL) {
193 return PSA_ERROR_NOT_SUPPORTED;
194 }
Steven Cooremane6804192021-03-19 18:28:56 +0100195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 ret = mbedtls_cipher_setup(&operation->ctx.cmac, cipher_info);
197 if (ret != 0) {
Steven Cooremane6804192021-03-19 18:28:56 +0100198 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 }
Steven Cooremane6804192021-03-19 18:28:56 +0100200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 ret = mbedtls_cipher_cmac_starts(&operation->ctx.cmac,
202 key_buffer,
203 psa_get_key_bits(attributes));
Steven Cooremane6804192021-03-19 18:28:56 +0100204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return mbedtls_to_psa_error(ret);
Steven Cooremane6804192021-03-19 18:28:56 +0100206}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100207#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100208
Ronald Cron0266cfe2021-03-13 18:50:11 +0100209#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
210 defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman02865f52021-05-07 15:55:27 +0200211
Steven Cooremane6804192021-03-19 18:28:56 +0100212/* Initialize this driver's MAC operation structure. Once this function has been
213 * called, mbedtls_psa_mac_abort can run and will do the right thing. */
214static psa_status_t mac_init(
215 mbedtls_psa_mac_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 psa_algorithm_t alg)
Steven Cooremane6804192021-03-19 18:28:56 +0100217{
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200218 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100219
Steven Cooreman72f736a2021-05-07 14:14:37 +0200220 operation->alg = alg;
Steven Cooremane6804192021-03-19 18:28:56 +0100221
Ronald Cron0266cfe2021-03-13 18:50:11 +0100222#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
224 mbedtls_cipher_init(&operation->ctx.cmac);
Steven Cooremane6804192021-03-19 18:28:56 +0100225 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100227#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
228#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (PSA_ALG_IS_HMAC(operation->alg)) {
Steven Cooremane6804192021-03-19 18:28:56 +0100230 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
231 operation->ctx.hmac.alg = 0;
232 status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100234#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100235 {
Ronald Cron7a55deb2021-04-28 14:29:00 +0200236 (void) operation;
Steven Cooremanba9a5bf2021-04-29 16:21:24 +0200237 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooremane6804192021-03-19 18:28:56 +0100238 }
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if (status != PSA_SUCCESS) {
241 memset(operation, 0, sizeof(*operation));
242 }
243 return status;
Steven Cooremane6804192021-03-19 18:28:56 +0100244}
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246psa_status_t mbedtls_psa_mac_abort(mbedtls_psa_mac_operation_t *operation)
Steven Cooremane6804192021-03-19 18:28:56 +0100247{
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (operation->alg == 0) {
Steven Cooremane6804192021-03-19 18:28:56 +0100249 /* The object has (apparently) been initialized but it is not
250 * in use. It's ok to call abort on such an object, and there's
251 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_SUCCESS;
253 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100254#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
256 mbedtls_cipher_free(&operation->ctx.cmac);
257 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100258#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
259#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (PSA_ALG_IS_HMAC(operation->alg)) {
261 psa_hmac_abort_internal(&operation->ctx.hmac);
262 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100263#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100264 {
265 /* Sanity check (shouldn't happen: operation->alg should
266 * always have been initialized to a valid value). */
267 goto bad_state;
268 }
269
270 operation->alg = 0;
Steven Cooremane6804192021-03-19 18:28:56 +0100271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_SUCCESS;
Steven Cooremane6804192021-03-19 18:28:56 +0100273
274bad_state:
275 /* If abort is called on an uninitialized object, we can't trust
276 * anything. Wipe the object in case it contains confidential data.
277 * This may result in a memory leak if a pointer gets overwritten,
278 * but it's too late to do anything about this. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 memset(operation, 0, sizeof(*operation));
280 return PSA_ERROR_BAD_STATE;
Steven Cooremane6804192021-03-19 18:28:56 +0100281}
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283static psa_status_t psa_mac_setup(mbedtls_psa_mac_operation_t *operation,
284 const psa_key_attributes_t *attributes,
285 const uint8_t *key_buffer,
286 size_t key_buffer_size,
287 psa_algorithm_t alg)
Steven Cooremane6804192021-03-19 18:28:56 +0100288{
289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
290
291 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (operation->alg != 0) {
293 return PSA_ERROR_BAD_STATE;
294 }
Steven Cooremane6804192021-03-19 18:28:56 +0100295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 status = mac_init(operation, alg);
297 if (status != PSA_SUCCESS) {
298 return status;
299 }
Steven Cooremane6804192021-03-19 18:28:56 +0100300
Ronald Cron0266cfe2021-03-13 18:50:11 +0100301#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (PSA_ALG_FULL_LENGTH_MAC(alg) == PSA_ALG_CMAC) {
Steven Cooremandcd08112021-05-06 18:00:37 +0200303 /* Key buffer size for CMAC is dictated by the key bits set on the
304 * attributes, and previously validated by the core on key import. */
305 (void) key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 status = cmac_setup(operation, attributes, key_buffer);
307 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100308#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
309#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (PSA_ALG_IS_HMAC(alg)) {
311 status = psa_hmac_setup_internal(&operation->ctx.hmac,
312 key_buffer,
313 key_buffer_size,
314 PSA_ALG_HMAC_GET_HASH(alg));
315 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100316#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremane6804192021-03-19 18:28:56 +0100317 {
Steven Cooremanb29902a2021-05-10 09:47:05 +0200318 (void) attributes;
319 (void) key_buffer;
320 (void) key_buffer_size;
Steven Cooremane6804192021-03-19 18:28:56 +0100321 status = PSA_ERROR_NOT_SUPPORTED;
322 }
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (status != PSA_SUCCESS) {
325 mbedtls_psa_mac_abort(operation);
326 }
Steven Cooremane6804192021-03-19 18:28:56 +0100327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return status;
Steven Cooremane6804192021-03-19 18:28:56 +0100329}
330
Ronald Cron0266cfe2021-03-13 18:50:11 +0100331psa_status_t mbedtls_psa_mac_sign_setup(
332 mbedtls_psa_mac_operation_t *operation,
333 const psa_key_attributes_t *attributes,
334 const uint8_t *key_buffer,
335 size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 psa_algorithm_t alg)
Ronald Cron0266cfe2021-03-13 18:50:11 +0100337{
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return psa_mac_setup(operation, attributes,
339 key_buffer, key_buffer_size, alg);
Ronald Cron0266cfe2021-03-13 18:50:11 +0100340}
341
342psa_status_t mbedtls_psa_mac_verify_setup(
343 mbedtls_psa_mac_operation_t *operation,
344 const psa_key_attributes_t *attributes,
345 const uint8_t *key_buffer,
346 size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 psa_algorithm_t alg)
Ronald Cron0266cfe2021-03-13 18:50:11 +0100348{
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return psa_mac_setup(operation, attributes,
350 key_buffer, key_buffer_size, alg);
Ronald Cron0266cfe2021-03-13 18:50:11 +0100351}
352
353psa_status_t mbedtls_psa_mac_update(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100354 mbedtls_psa_mac_operation_t *operation,
355 const uint8_t *input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 size_t input_length)
Steven Cooremand13a70f2021-03-19 15:24:23 +0100357{
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (operation->alg == 0) {
359 return PSA_ERROR_BAD_STATE;
360 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100361
Ronald Cron0266cfe2021-03-13 18:50:11 +0100362#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
364 return mbedtls_to_psa_error(
365 mbedtls_cipher_cmac_update(&operation->ctx.cmac,
366 input, input_length));
367 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100368#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
369#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (PSA_ALG_IS_HMAC(operation->alg)) {
371 return psa_hmac_update_internal(&operation->ctx.hmac,
372 input, input_length);
373 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100374#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100375 {
376 /* This shouldn't happen if `operation` was initialized by
377 * a setup function. */
Steven Cooremanb29902a2021-05-10 09:47:05 +0200378 (void) input;
379 (void) input_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return PSA_ERROR_BAD_STATE;
Steven Cooreman6e7f2912021-03-19 18:38:46 +0100381 }
Steven Cooremand13a70f2021-03-19 15:24:23 +0100382}
383
Ronald Cron0266cfe2021-03-13 18:50:11 +0100384static psa_status_t psa_mac_finish_internal(
385 mbedtls_psa_mac_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 uint8_t *mac, size_t mac_size)
Steven Cooremana5b860a2021-03-19 19:04:39 +0100387{
Ronald Cron0266cfe2021-03-13 18:50:11 +0100388#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
Steven Cooremana5b860a2021-03-19 19:04:39 +0100390 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 int ret = mbedtls_cipher_cmac_finish(&operation->ctx.cmac, tmp);
392 if (ret == 0) {
393 memcpy(mac, tmp, mac_size);
394 }
395 mbedtls_platform_zeroize(tmp, sizeof(tmp));
396 return mbedtls_to_psa_error(ret);
397 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100398#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
399#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (PSA_ALG_IS_HMAC(operation->alg)) {
401 return psa_hmac_finish_internal(&operation->ctx.hmac,
402 mac, mac_size);
403 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100404#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Steven Cooremana5b860a2021-03-19 19:04:39 +0100405 {
406 /* This shouldn't happen if `operation` was initialized by
407 * a setup function. */
Steven Cooreman72f736a2021-05-07 14:14:37 +0200408 (void) operation;
409 (void) mac;
410 (void) mac_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return PSA_ERROR_BAD_STATE;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100412 }
413}
414
Ronald Cron0266cfe2021-03-13 18:50:11 +0100415psa_status_t mbedtls_psa_mac_sign_finish(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100416 mbedtls_psa_mac_operation_t *operation,
417 uint8_t *mac,
418 size_t mac_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 size_t *mac_length)
Steven Cooremand13a70f2021-03-19 15:24:23 +0100420{
Steven Cooreman094a77e2021-05-06 17:58:36 +0200421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (operation->alg == 0) {
424 return PSA_ERROR_BAD_STATE;
425 }
Steven Cooremana5b860a2021-03-19 19:04:39 +0100426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 status = psa_mac_finish_internal(operation, mac, mac_size);
428 if (status == PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +0200429 *mac_length = mac_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 }
Steven Cooremana5b860a2021-03-19 19:04:39 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return status;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100433}
434
Ronald Cron0266cfe2021-03-13 18:50:11 +0100435psa_status_t mbedtls_psa_mac_verify_finish(
Steven Cooremand13a70f2021-03-19 15:24:23 +0100436 mbedtls_psa_mac_operation_t *operation,
437 const uint8_t *mac,
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 size_t mac_length)
Steven Cooremand13a70f2021-03-19 15:24:23 +0100439{
Steven Cooremana5b860a2021-03-19 19:04:39 +0100440 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Steven Cooreman094a77e2021-05-06 17:58:36 +0200441 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremana5b860a2021-03-19 19:04:39 +0100442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (operation->alg == 0) {
444 return PSA_ERROR_BAD_STATE;
445 }
Steven Cooremana5b860a2021-03-19 19:04:39 +0100446
Steven Cooreman72f736a2021-05-07 14:14:37 +0200447 /* Consistency check: requested MAC length fits our local buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (mac_length > sizeof(actual_mac)) {
449 return PSA_ERROR_INVALID_ARGUMENT;
450 }
Steven Cooremana5b860a2021-03-19 19:04:39 +0100451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 status = psa_mac_finish_internal(operation, actual_mac, mac_length);
453 if (status != PSA_SUCCESS) {
Steven Cooremana5b860a2021-03-19 19:04:39 +0100454 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 }
Steven Cooremana5b860a2021-03-19 19:04:39 +0100456
Dave Rodgman78701152023-08-29 14:20:18 +0100457 if (mbedtls_ct_memcmp(mac, actual_mac, mac_length) != 0) {
Steven Cooremana5b860a2021-03-19 19:04:39 +0100458 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 }
Steven Cooremana5b860a2021-03-19 19:04:39 +0100460
461cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
Steven Cooremana5b860a2021-03-19 19:04:39 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 return status;
Steven Cooremand13a70f2021-03-19 15:24:23 +0100465}
Ronald Cron76be3e02021-06-17 17:34:43 +0200466
Ronald Cron0266cfe2021-03-13 18:50:11 +0100467psa_status_t mbedtls_psa_mac_compute(
Ronald Cron76be3e02021-06-17 17:34:43 +0200468 const psa_key_attributes_t *attributes,
469 const uint8_t *key_buffer,
470 size_t key_buffer_size,
471 psa_algorithm_t alg,
472 const uint8_t *input,
473 size_t input_length,
474 uint8_t *mac,
475 size_t mac_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 size_t *mac_length)
Ronald Cron76be3e02021-06-17 17:34:43 +0200477{
478 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
479 mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT;
480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 status = psa_mac_setup(&operation,
482 attributes, key_buffer, key_buffer_size,
483 alg);
484 if (status != PSA_SUCCESS) {
Ronald Cron76be3e02021-06-17 17:34:43 +0200485 goto exit;
Ronald Cron76be3e02021-06-17 17:34:43 +0200486 }
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (input_length > 0) {
489 status = mbedtls_psa_mac_update(&operation, input, input_length);
490 if (status != PSA_SUCCESS) {
491 goto exit;
492 }
493 }
494
495 status = psa_mac_finish_internal(&operation, mac, mac_size);
496 if (status == PSA_SUCCESS) {
Ronald Cron76be3e02021-06-17 17:34:43 +0200497 *mac_length = mac_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 }
Ronald Cron76be3e02021-06-17 17:34:43 +0200499
500exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 mbedtls_psa_mac_abort(&operation);
Ronald Cron76be3e02021-06-17 17:34:43 +0200502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return status;
Ronald Cron76be3e02021-06-17 17:34:43 +0200504}
505
Ronald Cron0266cfe2021-03-13 18:50:11 +0100506#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Steven Cooremand13a70f2021-03-19 15:24:23 +0100507
508#endif /* MBEDTLS_PSA_CRYPTO_C */