blob: 6ed82989b05eeea288c7d674a481525b26e9d38d [file] [log] [blame]
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +01001/**
2 * PSA API multi-part HMAC demonstration.
Manuel Pégourié-Gonnard0e725c32022-01-27 11:15:33 +01003 *
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +01004 * This programs computes the HMAC of two messages using the multi-part API.
5 *
Manuel Pégourié-Gonnard29088a42022-02-01 09:38:26 +01006 * It comes with a companion program hash/md_hmac_demo.c, which does the same
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +01007 * operations with the legacy MD API. The goal is that comparing the two
8 * programs will help people migrating to the PSA Crypto API.
9 *
10 * 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é-Gonnard64754e12022-02-08 11:21:14 +010016 * This program and its companion hash/md_hmac_demo.c illustrate this by doing
17 * the same sequence of multi-part HMAC computation with both APIs; looking at
18 * the two side by side should make the differences and similarities clear.
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010019 */
20
21/*
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +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é-Gonnard667b5562021-11-22 13:00:17 +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é-Gonnard667b5562021-11-22 13:00:17 +010029#include "mbedtls/build_info.h"
30
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010031#include "psa/crypto.h"
32
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010033#include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
34
Michael Schuster3a4c4312024-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_PSA_CRYPTO_C) || \
Manuel Pégourié-Gonnard9efbf532022-01-17 11:57:44 +010042 defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Gilles Peskine449bd832023-01-11 14:50:10 +010043int main(void)
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010044{
Gilles Peskine449bd832023-01-11 14:50:10 +010045 printf("MBEDTLS_PSA_CRYPTO_C not defined, "
46 "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n");
47 return 0;
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010048}
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010049#else
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010050
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010051/* The real program starts here. */
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010052
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010053/* Dummy inputs for HMAC */
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +010054const unsigned char msg1_part1[] = { 0x01, 0x02 };
55const unsigned char msg1_part2[] = { 0x03, 0x04 };
56const unsigned char msg2_part1[] = { 0x05, 0x05 };
57const unsigned char msg2_part2[] = { 0x06, 0x06 };
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010058
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010059/* Dummy key material - never do this in production!
60 * This example program uses SHA-256, so a 32-byte key makes sense. */
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010061const unsigned char key_bytes[32] = { 0 };
62
Manuel Pégourié-Gonnard340808c2022-02-08 11:15:26 +010063/* Run a PSA function and bail out if it fails.
64 * The symbolic name of the error code can be recovered using:
Tom Cosgrove1797b052022-12-04 17:19:59 +000065 * programs/psa/psa_constant_name status <value> */
Gilles Peskine449bd832023-01-11 14:50:10 +010066#define PSA_CHECK(expr) \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010067 do \
68 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010069 status = (expr); \
70 if (status != PSA_SUCCESS) \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010071 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010072 printf("Error %d at line %d: %s\n", \
73 (int) status, \
74 __LINE__, \
75 #expr); \
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010076 goto exit; \
77 } \
78 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010079 while (0)
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010080
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010081/*
82 * This function demonstrates computation of the HMAC of two messages using
83 * the multipart API.
84 */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010085psa_status_t hmac_demo(void)
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010086{
87 psa_status_t status;
Manuel Pégourié-Gonnardf6ea19c2022-02-01 13:08:21 +010088 const psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
89 uint8_t out[PSA_MAC_MAX_SIZE]; // safe but not optimal
90 /* PSA_MAC_LENGTH(PSA_KEY_TYPE_HMAC, 8 * sizeof( key_bytes ), alg)
Dave Rodgman017a1992022-03-31 14:07:01 +010091 * should work but see https://github.com/Mbed-TLS/mbedtls/issues/4320 */
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010092
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
94 psa_key_id_t key = 0;
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010095
96 /* prepare key */
Gilles Peskine449bd832023-01-11 14:50:10 +010097 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
98 psa_set_key_algorithm(&attributes, alg);
99 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
100 psa_set_key_bits(&attributes, 8 * sizeof(key_bytes)); // optional
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 status = psa_import_key(&attributes,
103 key_bytes, sizeof(key_bytes), &key);
104 if (status != PSA_SUCCESS) {
105 return status;
106 }
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100107
108 /* prepare operation */
109 psa_mac_operation_t op = PSA_MAC_OPERATION_INIT;
110 size_t out_len = 0;
111
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100112 /* compute HMAC(key, msg1_part1 | msg1_part2) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 PSA_CHECK(psa_mac_sign_setup(&op, key, alg));
114 PSA_CHECK(psa_mac_update(&op, msg1_part1, sizeof(msg1_part1)));
115 PSA_CHECK(psa_mac_update(&op, msg1_part2, sizeof(msg1_part2)));
116 PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len));
Michael Schuster3a4c4312024-06-01 21:00:33 +0200117 mbedtls_test_print_buf("msg1", out, out_len);
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100118
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100119 /* compute HMAC(key, msg2_part1 | msg2_part2) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 PSA_CHECK(psa_mac_sign_setup(&op, key, alg));
121 PSA_CHECK(psa_mac_update(&op, msg2_part1, sizeof(msg2_part1)));
122 PSA_CHECK(psa_mac_update(&op, msg2_part2, sizeof(msg2_part2)));
123 PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len));
Michael Schuster3a4c4312024-06-01 21:00:33 +0200124 mbedtls_test_print_buf("msg2", out, out_len);
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100125
126exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 psa_mac_abort(&op); // needed on error, harmless on success
128 psa_destroy_key(key);
129 mbedtls_platform_zeroize(out, sizeof(out));
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return status;
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100132}
133
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100134int main(void)
135{
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100136 psa_status_t status = PSA_SUCCESS;
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100137
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100138 /* Initialize the PSA crypto library. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 PSA_CHECK(psa_crypto_init());
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100140
141 /* Run the demo */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 PSA_CHECK(hmac_demo());
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100143
144 /* Deinitialize the PSA crypto library. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 mbedtls_psa_crypto_free();
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100146
147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return status == PSA_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100149}
150
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100151#endif