2021-03-19 15:24:23 +01:00
|
|
|
/*
|
|
|
|
* PSA MAC layer on top of Mbed TLS software crypto
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
|
|
|
|
#include <psa/crypto.h>
|
|
|
|
#include "psa_crypto_core.h"
|
2022-11-02 10:25:38 +01:00
|
|
|
#include "psa_crypto_cipher.h"
|
2021-03-19 15:24:23 +01:00
|
|
|
#include "psa_crypto_mac.h"
|
2021-03-19 17:39:17 +01:00
|
|
|
#include <mbedtls/md.h>
|
2021-03-19 15:24:23 +01:00
|
|
|
|
|
|
|
#include <mbedtls/error.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2021-04-29 19:32:25 +02:00
|
|
|
static psa_status_t psa_hmac_abort_internal(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_psa_hmac_operation_t *hmac)
|
2021-03-19 17:39:17 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(hmac->opad, sizeof(hmac->opad));
|
|
|
|
return psa_hash_abort(&hmac->hash_ctx);
|
2021-03-19 17:39:17 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 19:32:25 +02:00
|
|
|
static psa_status_t psa_hmac_setup_internal(
|
|
|
|
mbedtls_psa_hmac_operation_t *hmac,
|
|
|
|
const uint8_t *key,
|
|
|
|
size_t key_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg)
|
2021-03-19 17:39:17 +01:00
|
|
|
{
|
|
|
|
uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
|
|
|
|
size_t i;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t hash_size = PSA_HASH_LENGTH(hash_alg);
|
|
|
|
size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg);
|
2021-03-19 17:39:17 +01:00
|
|
|
psa_status_t status;
|
|
|
|
|
|
|
|
hmac->alg = hash_alg;
|
|
|
|
|
|
|
|
/* Sanity checks on block_size, to guarantee that there won't be a buffer
|
|
|
|
* overflow below. This should never trigger if the hash algorithm
|
|
|
|
* is implemented correctly. */
|
|
|
|
/* The size checks against the ipad and opad buffers cannot be written
|
|
|
|
* `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
|
|
|
|
* because that triggers -Wlogical-op on GCC 7.3. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (block_size > sizeof(ipad)) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
if (block_size > sizeof(hmac->opad)) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
if (block_size < hash_size) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key_length > block_size) {
|
|
|
|
status = psa_hash_compute(hash_alg, key, key_length,
|
|
|
|
ipad, sizeof(ipad), &key_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-19 17:39:17 +01:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 17:39:17 +01:00
|
|
|
}
|
|
|
|
/* A 0-length key is not commonly used in HMAC when used as a MAC,
|
|
|
|
* but it is permitted. It is common when HMAC is used in HKDF, for
|
|
|
|
* example. Don't call `memcpy` in the 0-length because `key` could be
|
|
|
|
* an invalid pointer which would make the behavior undefined. */
|
2023-01-11 14:50:10 +01:00
|
|
|
else if (key_length != 0) {
|
|
|
|
memcpy(ipad, key, key_length);
|
|
|
|
}
|
2021-03-19 17:39:17 +01:00
|
|
|
|
|
|
|
/* ipad contains the key followed by garbage. Xor and fill with 0x36
|
|
|
|
* to create the ipad value. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < key_length; i++) {
|
2021-03-19 17:39:17 +01:00
|
|
|
ipad[i] ^= 0x36;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
memset(ipad + key_length, 0x36, block_size - key_length);
|
2021-03-19 17:39:17 +01:00
|
|
|
|
|
|
|
/* Copy the key material from ipad to opad, flipping the requisite bits,
|
|
|
|
* and filling the rest of opad with the requisite constant. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < key_length; i++) {
|
2021-03-19 17:39:17 +01:00
|
|
|
hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
memset(hmac->opad + key_length, 0x5C, block_size - key_length);
|
2021-03-19 17:39:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_setup(&hmac->hash_ctx, hash_alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-19 17:39:17 +01:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 17:39:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_update(&hmac->hash_ctx, ipad, block_size);
|
2021-03-19 17:39:17 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(ipad, sizeof(ipad));
|
2021-03-19 17:39:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2021-03-19 17:39:17 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 19:32:25 +02:00
|
|
|
static psa_status_t psa_hmac_update_internal(
|
|
|
|
mbedtls_psa_hmac_operation_t *hmac,
|
|
|
|
const uint8_t *data,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t data_length)
|
2021-03-22 12:21:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return psa_hash_update(&hmac->hash_ctx, data, data_length);
|
2021-03-22 12:21:10 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 19:32:25 +02:00
|
|
|
static psa_status_t psa_hmac_finish_internal(
|
|
|
|
mbedtls_psa_hmac_operation_t *hmac,
|
|
|
|
uint8_t *mac,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mac_size)
|
2021-03-19 17:39:17 +01:00
|
|
|
{
|
2021-10-18 09:47:58 +02:00
|
|
|
uint8_t tmp[PSA_HASH_MAX_SIZE];
|
2021-03-19 17:39:17 +01:00
|
|
|
psa_algorithm_t hash_alg = hmac->alg;
|
|
|
|
size_t hash_size = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg);
|
2021-03-19 17:39:17 +01:00
|
|
|
psa_status_t status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_finish(&hmac->hash_ctx, tmp, sizeof(tmp), &hash_size);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2021-03-19 17:39:17 +01:00
|
|
|
/* From here on, tmp needs to be wiped. */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_setup(&hmac->hash_ctx, hash_alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-19 17:39:17 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 17:39:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_update(&hmac->hash_ctx, hmac->opad, block_size);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-19 17:39:17 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 17:39:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_update(&hmac->hash_ctx, tmp, hash_size);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-19 17:39:17 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 17:39:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_finish(&hmac->hash_ctx, tmp, sizeof(tmp), &hash_size);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-19 17:39:17 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 17:39:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(mac, tmp, mac_size);
|
2021-03-19 17:39:17 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(tmp, hash_size);
|
|
|
|
return status;
|
2021-03-19 17:39:17 +01:00
|
|
|
}
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2021-03-19 18:28:56 +01:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t cmac_setup(mbedtls_psa_mac_operation_t *operation,
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer)
|
2021-03-19 18:28:56 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-05-07 17:27:27 +02:00
|
|
|
|
|
|
|
#if defined(PSA_WANT_KEY_TYPE_DES)
|
|
|
|
/* Mbed TLS CMAC does not accept 3DES with only two keys, nor does it accept
|
|
|
|
* to do CMAC with pure DES, so return NOT_SUPPORTED here. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psa_get_key_type(attributes) == PSA_KEY_TYPE_DES &&
|
|
|
|
(psa_get_key_bits(attributes) == 64 ||
|
|
|
|
psa_get_key_bits(attributes) == 128)) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
2021-05-07 17:27:27 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_cipher_info_t *cipher_info =
|
2021-03-19 18:28:56 +01:00
|
|
|
mbedtls_cipher_info_from_psa(
|
|
|
|
PSA_ALG_CMAC,
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_get_key_type(attributes),
|
|
|
|
psa_get_key_bits(attributes),
|
|
|
|
NULL);
|
2021-03-19 18:28:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cipher_info == NULL) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
2021-03-19 18:28:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_cipher_setup(&operation->ctx.cmac, cipher_info);
|
|
|
|
if (ret != 0) {
|
2021-03-19 18:28:56 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 18:28:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_cipher_cmac_starts(&operation->ctx.cmac,
|
|
|
|
key_buffer,
|
|
|
|
psa_get_key_bits(attributes));
|
2021-03-19 18:28:56 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_to_psa_error(ret);
|
2021-03-19 18:28:56 +01:00
|
|
|
}
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
|
2021-03-19 18:28:56 +01:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
|
2021-05-07 15:55:27 +02:00
|
|
|
|
2021-03-19 18:28:56 +01:00
|
|
|
/* Initialize this driver's MAC operation structure. Once this function has been
|
|
|
|
* called, mbedtls_psa_mac_abort can run and will do the right thing. */
|
|
|
|
static psa_status_t mac_init(
|
|
|
|
mbedtls_psa_mac_operation_t *operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t alg)
|
2021-03-19 18:28:56 +01:00
|
|
|
{
|
2021-04-29 16:21:24 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-03-19 18:28:56 +01:00
|
|
|
|
2021-05-07 14:14:37 +02:00
|
|
|
operation->alg = alg;
|
2021-03-19 18:28:56 +01:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
|
|
|
|
mbedtls_cipher_init(&operation->ctx.cmac);
|
2021-03-19 18:28:56 +01:00
|
|
|
status = PSA_SUCCESS;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_HMAC(operation->alg)) {
|
2021-03-19 18:28:56 +01:00
|
|
|
/* We'll set up the hash operation later in psa_hmac_setup_internal. */
|
|
|
|
operation->ctx.hmac.alg = 0;
|
|
|
|
status = PSA_SUCCESS;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2021-03-19 18:28:56 +01:00
|
|
|
{
|
2021-04-28 14:29:00 +02:00
|
|
|
(void) operation;
|
2021-04-29 16:21:24 +02:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
2021-03-19 18:28:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
memset(operation, 0, sizeof(*operation));
|
|
|
|
}
|
|
|
|
return status;
|
2021-03-19 18:28:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t mbedtls_psa_mac_abort(mbedtls_psa_mac_operation_t *operation)
|
2021-03-19 18:28:56 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (operation->alg == 0) {
|
2021-03-19 18:28:56 +01:00
|
|
|
/* The object has (apparently) been initialized but it is not
|
|
|
|
* in use. It's ok to call abort on such an object, and there's
|
|
|
|
* nothing to do. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
|
|
|
|
mbedtls_cipher_free(&operation->ctx.cmac);
|
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_HMAC(operation->alg)) {
|
|
|
|
psa_hmac_abort_internal(&operation->ctx.hmac);
|
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2021-03-19 18:28:56 +01:00
|
|
|
{
|
|
|
|
/* Sanity check (shouldn't happen: operation->alg should
|
|
|
|
* always have been initialized to a valid value). */
|
|
|
|
goto bad_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
operation->alg = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2021-03-19 18:28:56 +01:00
|
|
|
|
|
|
|
bad_state:
|
|
|
|
/* If abort is called on an uninitialized object, we can't trust
|
|
|
|
* anything. Wipe the object in case it contains confidential data.
|
|
|
|
* This may result in a memory leak if a pointer gets overwritten,
|
|
|
|
* but it's too late to do anything about this. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(operation, 0, sizeof(*operation));
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2021-03-19 18:28:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t psa_mac_setup(mbedtls_psa_mac_operation_t *operation,
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg)
|
2021-03-19 18:28:56 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
/* A context must be freshly initialized before it can be set up. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (operation->alg != 0) {
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
}
|
2021-03-19 18:28:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mac_init(operation, alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2021-03-19 18:28:56 +01:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_FULL_LENGTH_MAC(alg) == PSA_ALG_CMAC) {
|
2021-05-06 18:00:37 +02:00
|
|
|
/* Key buffer size for CMAC is dictated by the key bits set on the
|
|
|
|
* attributes, and previously validated by the core on key import. */
|
|
|
|
(void) key_buffer_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = cmac_setup(operation, attributes, key_buffer);
|
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_HMAC(alg)) {
|
|
|
|
status = psa_hmac_setup_internal(&operation->ctx.hmac,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
PSA_ALG_HMAC_GET_HASH(alg));
|
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2021-03-19 18:28:56 +01:00
|
|
|
{
|
2021-05-10 09:47:05 +02:00
|
|
|
(void) attributes;
|
|
|
|
(void) key_buffer;
|
|
|
|
(void) key_buffer_size;
|
2021-03-19 18:28:56 +01:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
mbedtls_psa_mac_abort(operation);
|
|
|
|
}
|
2021-03-19 18:28:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2021-03-19 18:28:56 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_mac_sign_setup(
|
|
|
|
mbedtls_psa_mac_operation_t *operation,
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
size_t key_buffer_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t alg)
|
2021-03-13 18:50:11 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return psa_mac_setup(operation, attributes,
|
|
|
|
key_buffer, key_buffer_size, alg);
|
2021-03-13 18:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t mbedtls_psa_mac_verify_setup(
|
|
|
|
mbedtls_psa_mac_operation_t *operation,
|
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
size_t key_buffer_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t alg)
|
2021-03-13 18:50:11 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return psa_mac_setup(operation, attributes,
|
|
|
|
key_buffer, key_buffer_size, alg);
|
2021-03-13 18:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t mbedtls_psa_mac_update(
|
2021-03-19 15:24:23 +01:00
|
|
|
mbedtls_psa_mac_operation_t *operation,
|
|
|
|
const uint8_t *input,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t input_length)
|
2021-03-19 15:24:23 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (operation->alg == 0) {
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
}
|
2021-03-19 18:38:46 +01:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
|
|
|
|
return mbedtls_to_psa_error(
|
|
|
|
mbedtls_cipher_cmac_update(&operation->ctx.cmac,
|
|
|
|
input, input_length));
|
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_HMAC(operation->alg)) {
|
|
|
|
return psa_hmac_update_internal(&operation->ctx.hmac,
|
|
|
|
input, input_length);
|
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2021-03-19 18:38:46 +01:00
|
|
|
{
|
|
|
|
/* This shouldn't happen if `operation` was initialized by
|
|
|
|
* a setup function. */
|
2021-05-10 09:47:05 +02:00
|
|
|
(void) input;
|
|
|
|
(void) input_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_BAD_STATE;
|
2021-03-19 18:38:46 +01:00
|
|
|
}
|
2021-03-19 15:24:23 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
static psa_status_t psa_mac_finish_internal(
|
|
|
|
mbedtls_psa_mac_operation_t *operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *mac, size_t mac_size)
|
2021-03-19 19:04:39 +01:00
|
|
|
{
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_FULL_LENGTH_MAC(operation->alg) == PSA_ALG_CMAC) {
|
2021-03-19 19:04:39 +01:00
|
|
|
uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = mbedtls_cipher_cmac_finish(&operation->ctx.cmac, tmp);
|
|
|
|
if (ret == 0) {
|
|
|
|
memcpy(mac, tmp, mac_size);
|
|
|
|
}
|
|
|
|
mbedtls_platform_zeroize(tmp, sizeof(tmp));
|
|
|
|
return mbedtls_to_psa_error(ret);
|
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_HMAC(operation->alg)) {
|
|
|
|
return psa_hmac_finish_internal(&operation->ctx.hmac,
|
|
|
|
mac, mac_size);
|
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
|
2021-03-19 19:04:39 +01:00
|
|
|
{
|
|
|
|
/* This shouldn't happen if `operation` was initialized by
|
|
|
|
* a setup function. */
|
2021-05-07 14:14:37 +02:00
|
|
|
(void) operation;
|
|
|
|
(void) mac;
|
|
|
|
(void) mac_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_BAD_STATE;
|
2021-03-19 19:04:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_mac_sign_finish(
|
2021-03-19 15:24:23 +01:00
|
|
|
mbedtls_psa_mac_operation_t *operation,
|
|
|
|
uint8_t *mac,
|
|
|
|
size_t mac_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *mac_length)
|
2021-03-19 15:24:23 +01:00
|
|
|
{
|
2021-05-06 17:58:36 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-03-19 19:04:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (operation->alg == 0) {
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
}
|
2021-03-19 19:04:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_finish_internal(operation, mac, mac_size);
|
|
|
|
if (status == PSA_SUCCESS) {
|
2021-05-07 14:14:37 +02:00
|
|
|
*mac_length = mac_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 19:04:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2021-03-19 19:04:39 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_mac_verify_finish(
|
2021-03-19 15:24:23 +01:00
|
|
|
mbedtls_psa_mac_operation_t *operation,
|
|
|
|
const uint8_t *mac,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mac_length)
|
2021-03-19 15:24:23 +01:00
|
|
|
{
|
2021-03-19 19:04:39 +01:00
|
|
|
uint8_t actual_mac[PSA_MAC_MAX_SIZE];
|
2021-05-06 17:58:36 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-03-19 19:04:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (operation->alg == 0) {
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
|
|
|
}
|
2021-03-19 19:04:39 +01:00
|
|
|
|
2021-05-07 14:14:37 +02:00
|
|
|
/* Consistency check: requested MAC length fits our local buffer */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mac_length > sizeof(actual_mac)) {
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
2021-03-19 19:04:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_finish_internal(operation, actual_mac, mac_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-19 19:04:39 +01:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 19:04:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_psa_safer_memcmp(mac, actual_mac, mac_length) != 0) {
|
2021-03-19 19:04:39 +01:00
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 19:04:39 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
|
2021-03-19 19:04:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2021-03-19 15:24:23 +01:00
|
|
|
}
|
2021-06-17 17:34:43 +02:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_mac_compute(
|
2021-06-17 17:34:43 +02:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
|
|
|
uint8_t *mac,
|
|
|
|
size_t mac_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *mac_length)
|
2021-06-17 17:34:43 +02:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_psa_mac_operation_t operation = MBEDTLS_PSA_MAC_OPERATION_INIT;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_setup(&operation,
|
|
|
|
attributes, key_buffer, key_buffer_size,
|
|
|
|
alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-06-17 17:34:43 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-06-17 17:34:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (input_length > 0) {
|
|
|
|
status = mbedtls_psa_mac_update(&operation, input, input_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-06-17 17:34:43 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-06-17 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_mac_finish_internal(&operation, mac, mac_size);
|
|
|
|
if (status == PSA_SUCCESS) {
|
2021-06-17 17:34:43 +02:00
|
|
|
*mac_length = mac_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-06-17 17:34:43 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_psa_mac_abort(&operation);
|
2021-06-17 17:34:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2021-06-17 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_CMAC */
|
2021-03-19 15:24:23 +01:00
|
|
|
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|