blob: 581816a1d926df2318e626555ed3f78218620ce3 [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
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010035#include <stdlib.h>
36#include <stdio.h>
37
38/* If the build options we need are not enabled, compile a placeholder. */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010039#if !defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010040int main(void)
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010041{
Gilles Peskine449bd832023-01-11 14:50:10 +010042 printf("MBEDTLS_MD_C not defined\r\n");
43 return 0;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010044}
45#else
46
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010047/* The real program starts here. */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010048
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010049/* Dummy inputs for HMAC */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010050const unsigned char msg1_part1[] = { 0x01, 0x02 };
51const unsigned char msg1_part2[] = { 0x03, 0x04 };
52const unsigned char msg2_part1[] = { 0x05, 0x05 };
53const unsigned char msg2_part2[] = { 0x06, 0x06 };
54
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010055/* Dummy key material - never do this in production!
56 * This example program uses SHA-256, so a 32-byte key makes sense. */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010057const unsigned char key_bytes[32] = { 0 };
58
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010059/* Print the contents of a buffer in hex */
Gilles Peskine449bd832023-01-11 14:50:10 +010060void print_buf(const char *title, unsigned char *buf, size_t len)
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010061{
Gilles Peskine449bd832023-01-11 14:50:10 +010062 printf("%s:", title);
63 for (size_t i = 0; i < len; i++) {
64 printf(" %02x", buf[i]);
65 }
66 printf("\n");
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010067}
68
Manuel Pégourié-Gonnard340808c2022-02-08 11:15:26 +010069/* Run an Mbed TLS function and bail out if it fails.
70 * A string description of the error code can be recovered with:
71 * programs/util/strerror <value> */
Gilles Peskine449bd832023-01-11 14:50:10 +010072#define CHK(expr) \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010073 do \
74 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010075 ret = (expr); \
76 if (ret != 0) \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010077 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010078 printf("Error %d at line %d: %s\n", \
79 ret, \
80 __LINE__, \
81 #expr); \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010082 goto exit; \
83 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010084 } while (0)
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010085
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010086/*
87 * This function demonstrates computation of the HMAC of two messages using
88 * the multipart API.
89 */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010090int hmac_demo(void)
91{
92 int ret;
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010093 const mbedtls_md_type_t alg = MBEDTLS_MD_SHA256;
94 unsigned char out[MBEDTLS_MD_MAX_SIZE]; // safe but not optimal
95
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010096 mbedtls_md_context_t ctx;
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098 mbedtls_md_init(&ctx);
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010099
100 /* prepare context and load key */
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +0100101 // the last argument to setup is 1 to enable HMAC (not just hashing)
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 const mbedtls_md_info_t *info = mbedtls_md_info_from_type(alg);
103 CHK(mbedtls_md_setup(&ctx, info, 1));
104 CHK(mbedtls_md_hmac_starts(&ctx, key_bytes, sizeof(key_bytes)));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100105
106 /* compute HMAC(key, msg1_part1 | msg1_part2) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 CHK(mbedtls_md_hmac_update(&ctx, msg1_part1, sizeof(msg1_part1)));
108 CHK(mbedtls_md_hmac_update(&ctx, msg1_part2, sizeof(msg1_part2)));
109 CHK(mbedtls_md_hmac_finish(&ctx, out));
110 print_buf("msg1", out, mbedtls_md_get_size(info));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100111
112 /* compute HMAC(key, msg2_part1 | msg2_part2) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 CHK(mbedtls_md_hmac_reset(&ctx)); // prepare for new operation
114 CHK(mbedtls_md_hmac_update(&ctx, msg2_part1, sizeof(msg2_part1)));
115 CHK(mbedtls_md_hmac_update(&ctx, msg2_part2, sizeof(msg2_part2)));
116 CHK(mbedtls_md_hmac_finish(&ctx, out));
117 print_buf("msg2", out, mbedtls_md_get_size(info));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100118
119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_md_free(&ctx);
121 mbedtls_platform_zeroize(out, sizeof(out));
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return ret;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100124}
125
126int main(void)
127{
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100128 int ret;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 CHK(hmac_demo());
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100131
132exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100134}
135
136#endif