2022-01-31 12:06:07 +01:00
|
|
|
/**
|
|
|
|
* MD API multi-part HMAC demonstration.
|
|
|
|
*
|
|
|
|
* This programs computes the HMAC of two messages using the multi-part API.
|
|
|
|
*
|
2022-01-31 13:09:47 +01:00
|
|
|
* This is a companion to psa/hmac_demo.c, doing the same operations with the
|
2022-01-27 12:36:39 +01:00
|
|
|
* legacy MD API. The goal is that comparing the two programs will help people
|
|
|
|
* migrating to the PSA Crypto API.
|
2022-01-27 11:15:33 +01:00
|
|
|
*
|
2022-01-31 12:06:07 +01:00
|
|
|
* 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-31 13:09:47 +01:00
|
|
|
* This program and its companion psa/hmac_demo.c illustrate this by doing the
|
2022-01-31 12:06:07 +01:00
|
|
|
* same sequence of multi-part HMAC computation with both APIs; looking at the
|
|
|
|
* two side by side should make the differences and similarities clear.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-11-22 13:00:17 +01:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2021-11-22 13:00:17 +01:00
|
|
|
*/
|
|
|
|
|
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 "mbedtls/md.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_MD_C)
|
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_MD_C not 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, unsigned char *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 an Mbed TLS function and bail out if it fails.
|
|
|
|
* A string description of the error code can be recovered with:
|
|
|
|
* programs/util/strerror <value> */
|
2023-01-11 14:50:10 +01:00
|
|
|
#define CHK(expr) \
|
2022-01-31 12:06:07 +01:00
|
|
|
do \
|
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = (expr); \
|
|
|
|
if (ret != 0) \
|
2022-01-31 12:06:07 +01:00
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("Error %d at line %d: %s\n", \
|
|
|
|
ret, \
|
|
|
|
__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
|
|
|
int hmac_demo(void)
|
2021-11-22 13:00:17 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2022-01-31 12:23:37 +01:00
|
|
|
const mbedtls_md_type_t alg = MBEDTLS_MD_SHA256;
|
|
|
|
unsigned char out[MBEDTLS_MD_MAX_SIZE]; // safe but not optimal
|
|
|
|
|
2021-11-22 13:00:17 +01:00
|
|
|
mbedtls_md_context_t ctx;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&ctx);
|
2021-11-22 13:00:17 +01:00
|
|
|
|
|
|
|
/* prepare context and load key */
|
2022-01-31 12:23:37 +01:00
|
|
|
// the last argument to setup is 1 to enable HMAC (not just hashing)
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_md_info_t *info = mbedtls_md_info_from_type(alg);
|
|
|
|
CHK(mbedtls_md_setup(&ctx, info, 1));
|
|
|
|
CHK(mbedtls_md_hmac_starts(&ctx, key_bytes, sizeof(key_bytes)));
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2022-01-27 11:42:47 +01:00
|
|
|
/* compute HMAC(key, msg1_part1 | msg1_part2) */
|
2023-01-11 14:50:10 +01:00
|
|
|
CHK(mbedtls_md_hmac_update(&ctx, msg1_part1, sizeof(msg1_part1)));
|
|
|
|
CHK(mbedtls_md_hmac_update(&ctx, msg1_part2, sizeof(msg1_part2)));
|
|
|
|
CHK(mbedtls_md_hmac_finish(&ctx, out));
|
|
|
|
print_buf("msg1", out, mbedtls_md_get_size(info));
|
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
|
|
|
CHK(mbedtls_md_hmac_reset(&ctx)); // prepare for new operation
|
|
|
|
CHK(mbedtls_md_hmac_update(&ctx, msg2_part1, sizeof(msg2_part1)));
|
|
|
|
CHK(mbedtls_md_hmac_update(&ctx, msg2_part2, sizeof(msg2_part2)));
|
|
|
|
CHK(mbedtls_md_hmac_finish(&ctx, out));
|
|
|
|
print_buf("msg2", out, mbedtls_md_get_size(info));
|
2021-11-22 13:00:17 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&ctx);
|
|
|
|
mbedtls_platform_zeroize(out, sizeof(out));
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-11-22 13:00:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2022-01-31 12:06:07 +01:00
|
|
|
int ret;
|
2021-11-22 13:00:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
CHK(hmac_demo());
|
2022-01-31 12:06:07 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
2021-11-22 13:00:17 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 12:36:39 +01:00
|
|
|
#endif
|