blob: 0fe0700ce4fc36de86de01778fccae7522a48744 [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. */
Felix Conway998760a2025-03-24 11:37:33 +000029#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
30
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010031#include "mbedtls/build_info.h"
32
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010033#include "mbedtls/md.h"
34
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010035#include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
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
Michael Schusterc1cd26b2024-07-21 00:44:33 +020061/* Print the contents of a buffer in hex */
Michael Schuster9e52d152024-07-21 00:46:10 +020062static void print_buf(const char *title, unsigned char *buf, size_t len)
Michael Schusterc1cd26b2024-07-21 00:44:33 +020063{
64 printf("%s:", title);
65 for (size_t i = 0; i < len; i++) {
66 printf(" %02x", buf[i]);
67 }
68 printf("\n");
69}
70
Manuel Pégourié-Gonnard340808c2022-02-08 11:15:26 +010071/* Run an Mbed TLS function and bail out if it fails.
72 * A string description of the error code can be recovered with:
73 * programs/util/strerror <value> */
Gilles Peskine449bd832023-01-11 14:50:10 +010074#define CHK(expr) \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010075 do \
76 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010077 ret = (expr); \
78 if (ret != 0) \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010079 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010080 printf("Error %d at line %d: %s\n", \
81 ret, \
82 __LINE__, \
83 #expr); \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010084 goto exit; \
85 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010086 } while (0)
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010087
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010088/*
89 * This function demonstrates computation of the HMAC of two messages using
90 * the multipart API.
91 */
Michael Schuster8db8d612024-06-01 21:15:02 +020092static int hmac_demo(void)
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010093{
94 int ret;
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010095 const mbedtls_md_type_t alg = MBEDTLS_MD_SHA256;
96 unsigned char out[MBEDTLS_MD_MAX_SIZE]; // safe but not optimal
97
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010098 mbedtls_md_context_t ctx;
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 mbedtls_md_init(&ctx);
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100101
102 /* prepare context and load key */
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +0100103 // the last argument to setup is 1 to enable HMAC (not just hashing)
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 const mbedtls_md_info_t *info = mbedtls_md_info_from_type(alg);
105 CHK(mbedtls_md_setup(&ctx, info, 1));
106 CHK(mbedtls_md_hmac_starts(&ctx, key_bytes, sizeof(key_bytes)));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100107
108 /* compute HMAC(key, msg1_part1 | msg1_part2) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 CHK(mbedtls_md_hmac_update(&ctx, msg1_part1, sizeof(msg1_part1)));
110 CHK(mbedtls_md_hmac_update(&ctx, msg1_part2, sizeof(msg1_part2)));
111 CHK(mbedtls_md_hmac_finish(&ctx, out));
Michael Schusterc1cd26b2024-07-21 00:44:33 +0200112 print_buf("msg1", out, mbedtls_md_get_size(info));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100113
114 /* compute HMAC(key, msg2_part1 | msg2_part2) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 CHK(mbedtls_md_hmac_reset(&ctx)); // prepare for new operation
116 CHK(mbedtls_md_hmac_update(&ctx, msg2_part1, sizeof(msg2_part1)));
117 CHK(mbedtls_md_hmac_update(&ctx, msg2_part2, sizeof(msg2_part2)));
118 CHK(mbedtls_md_hmac_finish(&ctx, out));
Michael Schusterc1cd26b2024-07-21 00:44:33 +0200119 print_buf("msg2", out, mbedtls_md_get_size(info));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100120
121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_md_free(&ctx);
123 mbedtls_platform_zeroize(out, sizeof(out));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return ret;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100126}
127
128int main(void)
129{
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100130 int ret;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 CHK(hmac_demo());
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100133
134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100136}
137
138#endif