1a8e1175bSopenharmony_ci/**
2a8e1175bSopenharmony_ci * PSA API multi-part HMAC demonstration.
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci * This programs computes the HMAC of two messages using the multi-part API.
5a8e1175bSopenharmony_ci *
6a8e1175bSopenharmony_ci * It comes with a companion program hash/md_hmac_demo.c, which does the same
7a8e1175bSopenharmony_ci * operations with the legacy MD API. The goal is that comparing the two
8a8e1175bSopenharmony_ci * programs will help people migrating to the PSA Crypto API.
9a8e1175bSopenharmony_ci *
10a8e1175bSopenharmony_ci * When it comes to multi-part HMAC operations, the `mbedtls_md_context`
11a8e1175bSopenharmony_ci * serves a dual purpose (1) hold the key, and (2) save progress information
12a8e1175bSopenharmony_ci * for the current operation. With PSA those roles are held by two disinct
13a8e1175bSopenharmony_ci * objects: (1) a psa_key_id_t to hold the key, and (2) a psa_operation_t for
14a8e1175bSopenharmony_ci * multi-part progress.
15a8e1175bSopenharmony_ci *
16a8e1175bSopenharmony_ci * This program and its companion hash/md_hmac_demo.c illustrate this by doing
17a8e1175bSopenharmony_ci * the same sequence of multi-part HMAC computation with both APIs; looking at
18a8e1175bSopenharmony_ci * the two side by side should make the differences and similarities clear.
19a8e1175bSopenharmony_ci */
20a8e1175bSopenharmony_ci
21a8e1175bSopenharmony_ci/*
22a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
23a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0
24a8e1175bSopenharmony_ci *
25a8e1175bSopenharmony_ci *  Licensed under the Apache License, Version 2.0 (the "License"); you may
26a8e1175bSopenharmony_ci *  not use this file except in compliance with the License.
27a8e1175bSopenharmony_ci *  You may obtain a copy of the License at
28a8e1175bSopenharmony_ci *
29a8e1175bSopenharmony_ci *  http://www.apache.org/licenses/LICENSE-2.0
30a8e1175bSopenharmony_ci *
31a8e1175bSopenharmony_ci *  Unless required by applicable law or agreed to in writing, software
32a8e1175bSopenharmony_ci *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
33a8e1175bSopenharmony_ci *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34a8e1175bSopenharmony_ci *  See the License for the specific language governing permissions and
35a8e1175bSopenharmony_ci *  limitations under the License.
36a8e1175bSopenharmony_ci */
37a8e1175bSopenharmony_ci
38a8e1175bSopenharmony_ci/* First include Mbed TLS headers to get the Mbed TLS configuration and
39a8e1175bSopenharmony_ci * platform definitions that we'll use in this program. Also include
40a8e1175bSopenharmony_ci * standard C headers for functions we'll use here. */
41a8e1175bSopenharmony_ci#include "mbedtls/build_info.h"
42a8e1175bSopenharmony_ci
43a8e1175bSopenharmony_ci#include "psa/crypto.h"
44a8e1175bSopenharmony_ci
45a8e1175bSopenharmony_ci#include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
46a8e1175bSopenharmony_ci
47a8e1175bSopenharmony_ci#include <stdlib.h>
48a8e1175bSopenharmony_ci#include <stdio.h>
49a8e1175bSopenharmony_ci
50a8e1175bSopenharmony_ci/* If the build options we need are not enabled, compile a placeholder. */
51a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PSA_CRYPTO_C) || \
52a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
53a8e1175bSopenharmony_ciint main(void)
54a8e1175bSopenharmony_ci{
55a8e1175bSopenharmony_ci    printf("MBEDTLS_PSA_CRYPTO_C not defined, "
56a8e1175bSopenharmony_ci           "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n");
57a8e1175bSopenharmony_ci    return 0;
58a8e1175bSopenharmony_ci}
59a8e1175bSopenharmony_ci#else
60a8e1175bSopenharmony_ci
61a8e1175bSopenharmony_ci/* The real program starts here. */
62a8e1175bSopenharmony_ci
63a8e1175bSopenharmony_ci/* Dummy inputs for HMAC */
64a8e1175bSopenharmony_ciconst unsigned char msg1_part1[] = { 0x01, 0x02 };
65a8e1175bSopenharmony_ciconst unsigned char msg1_part2[] = { 0x03, 0x04 };
66a8e1175bSopenharmony_ciconst unsigned char msg2_part1[] = { 0x05, 0x05 };
67a8e1175bSopenharmony_ciconst unsigned char msg2_part2[] = { 0x06, 0x06 };
68a8e1175bSopenharmony_ci
69a8e1175bSopenharmony_ci/* Dummy key material - never do this in production!
70a8e1175bSopenharmony_ci * This example program uses SHA-256, so a 32-byte key makes sense. */
71a8e1175bSopenharmony_ciconst unsigned char key_bytes[32] = { 0 };
72a8e1175bSopenharmony_ci
73a8e1175bSopenharmony_ci/* Print the contents of a buffer in hex */
74a8e1175bSopenharmony_civoid print_buf(const char *title, uint8_t *buf, size_t len)
75a8e1175bSopenharmony_ci{
76a8e1175bSopenharmony_ci    printf("%s:", title);
77a8e1175bSopenharmony_ci    for (size_t i = 0; i < len; i++) {
78a8e1175bSopenharmony_ci        printf(" %02x", buf[i]);
79a8e1175bSopenharmony_ci    }
80a8e1175bSopenharmony_ci    printf("\n");
81a8e1175bSopenharmony_ci}
82a8e1175bSopenharmony_ci
83a8e1175bSopenharmony_ci/* Run a PSA function and bail out if it fails.
84a8e1175bSopenharmony_ci * The symbolic name of the error code can be recovered using:
85a8e1175bSopenharmony_ci * programs/psa/psa_constant_name status <value> */
86a8e1175bSopenharmony_ci#define PSA_CHECK(expr)                                       \
87a8e1175bSopenharmony_ci    do                                                          \
88a8e1175bSopenharmony_ci    {                                                           \
89a8e1175bSopenharmony_ci        status = (expr);                                      \
90a8e1175bSopenharmony_ci        if (status != PSA_SUCCESS)                             \
91a8e1175bSopenharmony_ci        {                                                       \
92a8e1175bSopenharmony_ci            printf("Error %d at line %d: %s\n",                \
93a8e1175bSopenharmony_ci                   (int) status,                               \
94a8e1175bSopenharmony_ci                   __LINE__,                                   \
95a8e1175bSopenharmony_ci                   #expr);                                    \
96a8e1175bSopenharmony_ci            goto exit;                                          \
97a8e1175bSopenharmony_ci        }                                                       \
98a8e1175bSopenharmony_ci    }                                                           \
99a8e1175bSopenharmony_ci    while (0)
100a8e1175bSopenharmony_ci
101a8e1175bSopenharmony_ci/*
102a8e1175bSopenharmony_ci * This function demonstrates computation of the HMAC of two messages using
103a8e1175bSopenharmony_ci * the multipart API.
104a8e1175bSopenharmony_ci */
105a8e1175bSopenharmony_cipsa_status_t hmac_demo(void)
106a8e1175bSopenharmony_ci{
107a8e1175bSopenharmony_ci    psa_status_t status;
108a8e1175bSopenharmony_ci    const psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
109a8e1175bSopenharmony_ci    uint8_t out[PSA_MAC_MAX_SIZE]; // safe but not optimal
110a8e1175bSopenharmony_ci    /* PSA_MAC_LENGTH(PSA_KEY_TYPE_HMAC, 8 * sizeof( key_bytes ), alg)
111a8e1175bSopenharmony_ci     * should work but see https://github.com/Mbed-TLS/mbedtls/issues/4320 */
112a8e1175bSopenharmony_ci
113a8e1175bSopenharmony_ci    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
114a8e1175bSopenharmony_ci    psa_key_id_t key = 0;
115a8e1175bSopenharmony_ci
116a8e1175bSopenharmony_ci    /* prepare key */
117a8e1175bSopenharmony_ci    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
118a8e1175bSopenharmony_ci    psa_set_key_algorithm(&attributes, alg);
119a8e1175bSopenharmony_ci    psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
120a8e1175bSopenharmony_ci    psa_set_key_bits(&attributes, 8 * sizeof(key_bytes));     // optional
121a8e1175bSopenharmony_ci
122a8e1175bSopenharmony_ci    status = psa_import_key(&attributes,
123a8e1175bSopenharmony_ci                            key_bytes, sizeof(key_bytes), &key);
124a8e1175bSopenharmony_ci    if (status != PSA_SUCCESS) {
125a8e1175bSopenharmony_ci        return status;
126a8e1175bSopenharmony_ci    }
127a8e1175bSopenharmony_ci
128a8e1175bSopenharmony_ci    /* prepare operation */
129a8e1175bSopenharmony_ci    psa_mac_operation_t op = PSA_MAC_OPERATION_INIT;
130a8e1175bSopenharmony_ci    size_t out_len = 0;
131a8e1175bSopenharmony_ci
132a8e1175bSopenharmony_ci    /* compute HMAC(key, msg1_part1 | msg1_part2) */
133a8e1175bSopenharmony_ci    PSA_CHECK(psa_mac_sign_setup(&op, key, alg));
134a8e1175bSopenharmony_ci    PSA_CHECK(psa_mac_update(&op, msg1_part1, sizeof(msg1_part1)));
135a8e1175bSopenharmony_ci    PSA_CHECK(psa_mac_update(&op, msg1_part2, sizeof(msg1_part2)));
136a8e1175bSopenharmony_ci    PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len));
137a8e1175bSopenharmony_ci    print_buf("msg1", out, out_len);
138a8e1175bSopenharmony_ci
139a8e1175bSopenharmony_ci    /* compute HMAC(key, msg2_part1 | msg2_part2) */
140a8e1175bSopenharmony_ci    PSA_CHECK(psa_mac_sign_setup(&op, key, alg));
141a8e1175bSopenharmony_ci    PSA_CHECK(psa_mac_update(&op, msg2_part1, sizeof(msg2_part1)));
142a8e1175bSopenharmony_ci    PSA_CHECK(psa_mac_update(&op, msg2_part2, sizeof(msg2_part2)));
143a8e1175bSopenharmony_ci    PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len));
144a8e1175bSopenharmony_ci    print_buf("msg2", out, out_len);
145a8e1175bSopenharmony_ci
146a8e1175bSopenharmony_ciexit:
147a8e1175bSopenharmony_ci    psa_mac_abort(&op);   // needed on error, harmless on success
148a8e1175bSopenharmony_ci    psa_destroy_key(key);
149a8e1175bSopenharmony_ci    mbedtls_platform_zeroize(out, sizeof(out));
150a8e1175bSopenharmony_ci
151a8e1175bSopenharmony_ci    return status;
152a8e1175bSopenharmony_ci}
153a8e1175bSopenharmony_ci
154a8e1175bSopenharmony_ciint main(void)
155a8e1175bSopenharmony_ci{
156a8e1175bSopenharmony_ci    psa_status_t status = PSA_SUCCESS;
157a8e1175bSopenharmony_ci
158a8e1175bSopenharmony_ci    /* Initialize the PSA crypto library. */
159a8e1175bSopenharmony_ci    PSA_CHECK(psa_crypto_init());
160a8e1175bSopenharmony_ci
161a8e1175bSopenharmony_ci    /* Run the demo */
162a8e1175bSopenharmony_ci    PSA_CHECK(hmac_demo());
163a8e1175bSopenharmony_ci
164a8e1175bSopenharmony_ci    /* Deinitialize the PSA crypto library. */
165a8e1175bSopenharmony_ci    mbedtls_psa_crypto_free();
166a8e1175bSopenharmony_ci
167a8e1175bSopenharmony_ciexit:
168a8e1175bSopenharmony_ci    return status == PSA_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
169a8e1175bSopenharmony_ci}
170a8e1175bSopenharmony_ci
171a8e1175bSopenharmony_ci#endif
172