2022-01-31 12:06:07 +01:00
|
|
|
/**
|
|
|
|
* PSA API multi-part HMAC demonstration.
|
|
|
|
*
|
|
|
|
* This programs computes the HMAC of two messages using the multi-part API.
|
|
|
|
*
|
2022-02-01 09:38:26 +01:00
|
|
|
* It comes with a companion program hash/md_hmac_demo.c, which does the same
|
2022-01-31 12:06:07 +01:00
|
|
|
* operations with the legacy MD API. The goal is that comparing the two
|
|
|
|
* programs will help people migrating to the PSA Crypto API.
|
|
|
|
*
|
|
|
|
* When it comes to multi-part HMAC operations, the `mbedtls_md_context`
|
|
|
|
* serves a dual purpose (1) hold the key, and (2) save progress information
|
|
|
|
* for the current operation. With PSA those roles are held by two disinct
|
|
|
|
* objects: (1) a psa_key_id_t to hold the key, and (2) a psa_operation_t for
|
|
|
|
* multi-part progress.
|
2022-01-27 11:15:33 +01:00
|
|
|
*
|
2022-02-08 11:21:14 +01:00
|
|
|
* This program and its companion hash/md_hmac_demo.c illustrate this by doing
|
|
|
|
* the same sequence of multi-part HMAC computation with both APIs; looking at
|
|
|
|
* the two side by side should make the differences and similarities clear.
|
2022-01-31 12:06:07 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-11-22 13:00:17 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-01-31 12:06:07 +01:00
|
|
|
/* First include Mbed TLS headers to get the Mbed TLS configuration and
|
|
|
|
* platform definitions that we'll use in this program. Also include
|
|
|
|
* standard C headers for functions we'll use here. */
|
|
|
|
#include "mbedtls/build_info.h"
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2022-01-31 12:06:07 +01:00
|
|
|
#include "psa/crypto.h"
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2022-01-31 12:23:37 +01:00
|
|
|
#include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
|
|
|
|
|
2022-01-31 12:06:07 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2022-01-31 12:06:07 +01:00
|
|
|
/* If the build options we need are not enabled, compile a placeholder. */
|
2022-01-27 12:36:39 +01:00
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_C) || \
|
2022-01-17 11:57:44 +01:00
|
|
|
defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(void)
|
2021-11-22 13:00:17 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("MBEDTLS_PSA_CRYPTO_C not defined, "
|
|
|
|
"and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n");
|
|
|
|
return 0;
|
2021-11-22 13:00:17 +01:00
|
|
|
}
|
2022-01-27 12:36:39 +01:00
|
|
|
#else
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2022-01-31 12:06:07 +01:00
|
|
|
/* The real program starts here. */
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2022-01-31 12:06:07 +01:00
|
|
|
/* Dummy inputs for HMAC */
|
2022-01-27 11:42:47 +01:00
|
|
|
const unsigned char msg1_part1[] = { 0x01, 0x02 };
|
|
|
|
const unsigned char msg1_part2[] = { 0x03, 0x04 };
|
|
|
|
const unsigned char msg2_part1[] = { 0x05, 0x05 };
|
|
|
|
const unsigned char msg2_part2[] = { 0x06, 0x06 };
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2022-01-31 12:06:07 +01:00
|
|
|
/* Dummy key material - never do this in production!
|
|
|
|
* This example program uses SHA-256, so a 32-byte key makes sense. */
|
2021-11-22 13:00:17 +01:00
|
|
|
const unsigned char key_bytes[32] = { 0 };
|
|
|
|
|
2022-01-31 12:23:37 +01:00
|
|
|
/* Print the contents of a buffer in hex */
|
2023-01-11 14:50:10 +01:00
|
|
|
void print_buf(const char *title, uint8_t *buf, size_t len)
|
2021-11-22 13:00:17 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("%s:", title);
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
printf(" %02x", buf[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2021-11-22 13:00:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 11:15:26 +01:00
|
|
|
/* Run a PSA function and bail out if it fails.
|
|
|
|
* The symbolic name of the error code can be recovered using:
|
2022-12-04 18:19:59 +01:00
|
|
|
* programs/psa/psa_constant_name status <value> */
|
2023-01-11 14:50:10 +01:00
|
|
|
#define PSA_CHECK(expr) \
|
2022-01-31 12:06:07 +01:00
|
|
|
do \
|
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
status = (expr); \
|
|
|
|
if (status != PSA_SUCCESS) \
|
2022-01-31 12:06:07 +01:00
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("Error %d at line %d: %s\n", \
|
|
|
|
(int) status, \
|
|
|
|
__LINE__, \
|
|
|
|
#expr); \
|
2022-01-31 12:06:07 +01:00
|
|
|
goto exit; \
|
|
|
|
} \
|
|
|
|
} \
|
2023-01-11 14:50:10 +01:00
|
|
|
while (0)
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2022-01-31 12:06:07 +01:00
|
|
|
/*
|
|
|
|
* This function demonstrates computation of the HMAC of two messages using
|
|
|
|
* the multipart API.
|
|
|
|
*/
|
2022-01-27 12:36:39 +01:00
|
|
|
psa_status_t hmac_demo(void)
|
2021-11-22 13:00:17 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2022-02-01 13:08:21 +01:00
|
|
|
const psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
|
|
|
|
uint8_t out[PSA_MAC_MAX_SIZE]; // safe but not optimal
|
|
|
|
/* PSA_MAC_LENGTH(PSA_KEY_TYPE_HMAC, 8 * sizeof( key_bytes ), alg)
|
2022-03-31 15:07:01 +02:00
|
|
|
* should work but see https://github.com/Mbed-TLS/mbedtls/issues/4320 */
|
2022-01-31 12:23:37 +01:00
|
|
|
|
2021-11-22 13:00:17 +01:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_id_t key = 0;
|
|
|
|
|
|
|
|
/* prepare key */
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
|
|
|
|
psa_set_key_bits(&attributes, 8 * sizeof(key_bytes)); // optional
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_import_key(&attributes,
|
|
|
|
key_bytes, sizeof(key_bytes), &key);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2021-11-22 13:00:17 +01:00
|
|
|
|
|
|
|
/* prepare operation */
|
|
|
|
psa_mac_operation_t op = PSA_MAC_OPERATION_INIT;
|
|
|
|
size_t out_len = 0;
|
|
|
|
|
2022-01-27 11:42:47 +01:00
|
|
|
/* compute HMAC(key, msg1_part1 | msg1_part2) */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(psa_mac_sign_setup(&op, key, alg));
|
|
|
|
PSA_CHECK(psa_mac_update(&op, msg1_part1, sizeof(msg1_part1)));
|
|
|
|
PSA_CHECK(psa_mac_update(&op, msg1_part2, sizeof(msg1_part2)));
|
|
|
|
PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len));
|
|
|
|
print_buf("msg1", out, out_len);
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2022-01-27 11:42:47 +01:00
|
|
|
/* compute HMAC(key, msg2_part1 | msg2_part2) */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(psa_mac_sign_setup(&op, key, alg));
|
|
|
|
PSA_CHECK(psa_mac_update(&op, msg2_part1, sizeof(msg2_part1)));
|
|
|
|
PSA_CHECK(psa_mac_update(&op, msg2_part2, sizeof(msg2_part2)));
|
|
|
|
PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len));
|
|
|
|
print_buf("msg2", out, out_len);
|
2021-11-22 13:00:17 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_mac_abort(&op); // needed on error, harmless on success
|
|
|
|
psa_destroy_key(key);
|
|
|
|
mbedtls_platform_zeroize(out, sizeof(out));
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2021-11-22 13:00:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2022-01-31 12:06:07 +01:00
|
|
|
psa_status_t status = PSA_SUCCESS;
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2022-01-31 12:06:07 +01:00
|
|
|
/* Initialize the PSA crypto library. */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(psa_crypto_init());
|
2022-01-31 12:06:07 +01:00
|
|
|
|
|
|
|
/* Run the demo */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHECK(hmac_demo());
|
2022-01-31 12:06:07 +01:00
|
|
|
|
|
|
|
/* Deinitialize the PSA crypto library. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_psa_crypto_free();
|
2022-01-31 12:06:07 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return status == PSA_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
|
2021-11-22 13:00:17 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 12:36:39 +01:00
|
|
|
#endif
|