blob: ba51bfda26af0c0bbec2df80f52db67183b84058 [file] [log] [blame]
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +01001/**
2 * MD API multi-part HMAC demonstration.
3 *
4 * This programs computes the HMAC of two messages using the multi-part API.
5 *
Manuel Pégourié-Gonnard69bb3f52022-01-31 13:09:47 +01006 * This is a companion to psa/hmac_demo.c, doing the same operations with the
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +01007 * legacy MD API. The goal is that comparing the two programs will help people
8 * migrating to the PSA Crypto API.
9 *
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010010 * When it comes to multi-part HMAC operations, the `mbedtls_md_context`
11 * serves a dual purpose (1) hold the key, and (2) save progress information
12 * for the current operation. With PSA those roles are held by two disinct
13 * objects: (1) a psa_key_id_t to hold the key, and (2) a psa_operation_t for
14 * multi-part progress.
15 *
Manuel Pégourié-Gonnard69bb3f52022-01-31 13:09:47 +010016 * This program and its companion psa/hmac_demo.c illustrate this by doing the
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010017 * same sequence of multi-part HMAC computation with both APIs; looking at the
18 * two side by side should make the differences and similarities clear.
19 */
20
21/*
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010022 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000023 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010024 */
25
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010026/* First include Mbed TLS headers to get the Mbed TLS configuration and
27 * platform definitions that we'll use in this program. Also include
28 * standard C headers for functions we'll use here. */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010029#include "mbedtls/build_info.h"
30
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010031#include "mbedtls/md.h"
32
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010033#include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
34
Michael Schuster4595e682024-06-01 21:00:33 +020035#include <test/helpers.h>
36
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010037#include <stdlib.h>
38#include <stdio.h>
39
40/* If the build options we need are not enabled, compile a placeholder. */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010041#if !defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010042int main(void)
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010043{
Gilles Peskine449bd832023-01-11 14:50:10 +010044 printf("MBEDTLS_MD_C not defined\r\n");
45 return 0;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010046}
47#else
48
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010049/* The real program starts here. */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010050
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010051/* Dummy inputs for HMAC */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010052const unsigned char msg1_part1[] = { 0x01, 0x02 };
53const unsigned char msg1_part2[] = { 0x03, 0x04 };
54const unsigned char msg2_part1[] = { 0x05, 0x05 };
55const unsigned char msg2_part2[] = { 0x06, 0x06 };
56
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010057/* Dummy key material - never do this in production!
58 * This example program uses SHA-256, so a 32-byte key makes sense. */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010059const unsigned char key_bytes[32] = { 0 };
60
Manuel Pégourié-Gonnard340808c2022-02-08 11:15:26 +010061/* Run an Mbed TLS function and bail out if it fails.
62 * A string description of the error code can be recovered with:
63 * programs/util/strerror <value> */
Gilles Peskine449bd832023-01-11 14:50:10 +010064#define CHK(expr) \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010065 do \
66 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010067 ret = (expr); \
68 if (ret != 0) \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010069 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010070 printf("Error %d at line %d: %s\n", \
71 ret, \
72 __LINE__, \
73 #expr); \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010074 goto exit; \
75 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010076 } while (0)
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010077
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010078/*
79 * This function demonstrates computation of the HMAC of two messages using
80 * the multipart API.
81 */
Michael Schuster8db8d612024-06-01 21:15:02 +020082static int hmac_demo(void)
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010083{
84 int ret;
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010085 const mbedtls_md_type_t alg = MBEDTLS_MD_SHA256;
86 unsigned char out[MBEDTLS_MD_MAX_SIZE]; // safe but not optimal
87
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010088 mbedtls_md_context_t ctx;
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_md_init(&ctx);
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010091
92 /* prepare context and load key */
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010093 // the last argument to setup is 1 to enable HMAC (not just hashing)
Gilles Peskine449bd832023-01-11 14:50:10 +010094 const mbedtls_md_info_t *info = mbedtls_md_info_from_type(alg);
95 CHK(mbedtls_md_setup(&ctx, info, 1));
96 CHK(mbedtls_md_hmac_starts(&ctx, key_bytes, sizeof(key_bytes)));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010097
98 /* compute HMAC(key, msg1_part1 | msg1_part2) */
Gilles Peskine449bd832023-01-11 14:50:10 +010099 CHK(mbedtls_md_hmac_update(&ctx, msg1_part1, sizeof(msg1_part1)));
100 CHK(mbedtls_md_hmac_update(&ctx, msg1_part2, sizeof(msg1_part2)));
101 CHK(mbedtls_md_hmac_finish(&ctx, out));
Michael Schuster4595e682024-06-01 21:00:33 +0200102 mbedtls_test_print_buf("msg1", out, mbedtls_md_get_size(info));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100103
104 /* compute HMAC(key, msg2_part1 | msg2_part2) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 CHK(mbedtls_md_hmac_reset(&ctx)); // prepare for new operation
106 CHK(mbedtls_md_hmac_update(&ctx, msg2_part1, sizeof(msg2_part1)));
107 CHK(mbedtls_md_hmac_update(&ctx, msg2_part2, sizeof(msg2_part2)));
108 CHK(mbedtls_md_hmac_finish(&ctx, out));
Michael Schuster4595e682024-06-01 21:00:33 +0200109 mbedtls_test_print_buf("msg2", out, mbedtls_md_get_size(info));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100110
111exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_md_free(&ctx);
113 mbedtls_platform_zeroize(out, sizeof(out));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return ret;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100116}
117
118int main(void)
119{
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100120 int ret;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 CHK(hmac_demo());
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100123
124exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100126}
127
128#endif