blob: 3cb0d21dd155e2ef09508e9a2b43b328496b8ee7 [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é-Gonnard29088a42022-02-01 09:38:26 +010016 * This program and its companion hash/md_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é-Gonnard667b5562021-11-22 13:00:17 +010022 * Copyright The Mbed TLS Contributors
23 * SPDX-License-Identifier: Apache-2.0
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License"); you may
26 * not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
33 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
36 */
37
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010038/* First include Mbed TLS headers to get the Mbed TLS configuration and
39 * platform definitions that we'll use in this program. Also include
40 * standard C headers for functions we'll use here. */
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010041#include "mbedtls/build_info.h"
42
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010043#include "psa/crypto.h"
44
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010045#include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
46
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010047#include <stdlib.h>
48#include <stdio.h>
49
50/* If the build options we need are not enabled, compile a placeholder. */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010051#if !defined(MBEDTLS_PSA_CRYPTO_C) || \
Manuel Pégourié-Gonnard9efbf532022-01-17 11:57:44 +010052 defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010053int main( void )
54{
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010055 printf( "MBEDTLS_PSA_CRYPTO_C not defined, "
Manuel Pégourié-Gonnard9efbf532022-01-17 11:57:44 +010056 "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n" );
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010057 return( 0 );
58}
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010059#else
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010060
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010061/* The real program starts here. */
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010062
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010063/* Dummy inputs for HMAC */
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +010064const unsigned char msg1_part1[] = { 0x01, 0x02 };
65const unsigned char msg1_part2[] = { 0x03, 0x04 };
66const unsigned char msg2_part1[] = { 0x05, 0x05 };
67const unsigned char msg2_part2[] = { 0x06, 0x06 };
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010068
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010069/* Dummy key material - never do this in production!
70 * This example program uses SHA-256, so a 32-byte key makes sense. */
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010071const unsigned char key_bytes[32] = { 0 };
72
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010073/* Print the contents of a buffer in hex */
74void print_buf( const char *title, uint8_t *buf, size_t len )
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010075{
76 printf( "%s:", title );
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010077 for( size_t i = 0; i < len; i++ )
78 printf( " %02x", buf[i] );
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010079 printf( "\n" );
80}
81
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010082/* Run a PSA function and bail out if it fails. */
83#define PSA_CHECK( expr ) \
84 do \
85 { \
86 status = ( expr ); \
87 if( status != PSA_SUCCESS ) \
88 { \
89 printf( "Error %d at line %d: %s\n", \
90 (int) status, \
91 __LINE__, \
92 #expr ); \
93 goto exit; \
94 } \
95 } \
96 while( 0 )
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +010097
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010098/*
99 * This function demonstrates computation of the HMAC of two messages using
100 * the multipart API.
101 */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100102psa_status_t hmac_demo(void)
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100103{
104 psa_status_t status;
Manuel Pégourié-Gonnardf6ea19c2022-02-01 13:08:21 +0100105 const psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
106 uint8_t out[PSA_MAC_MAX_SIZE]; // safe but not optimal
107 /* PSA_MAC_LENGTH(PSA_KEY_TYPE_HMAC, 8 * sizeof( key_bytes ), alg)
108 * should work but see https://github.com/ARMmbed/mbedtls/issues/4320 */
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +0100109
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
111 psa_key_id_t key = 0;
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100112
113 /* prepare key */
114 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
115 psa_set_key_algorithm( &attributes, alg );
116 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100117 psa_set_key_bits( &attributes, 8 * sizeof( key_bytes ) ); // optional
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100118
119 status = psa_import_key( &attributes, key_bytes, sizeof( key_bytes ), &key );
120 if( status != PSA_SUCCESS )
121 return( status );
122
123 /* prepare operation */
124 psa_mac_operation_t op = PSA_MAC_OPERATION_INIT;
125 size_t out_len = 0;
126
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100127 /* compute HMAC(key, msg1_part1 | msg1_part2) */
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100128 PSA_CHECK( psa_mac_sign_setup( &op, key, alg ) );
129 PSA_CHECK( psa_mac_update( &op, msg1_part1, sizeof( msg1_part1 ) ) );
130 PSA_CHECK( psa_mac_update( &op, msg1_part2, sizeof( msg1_part2 ) ) );
131 PSA_CHECK( psa_mac_sign_finish( &op, out, sizeof( out ), &out_len ) );
Manuel Pégourié-Gonnardf6ea19c2022-02-01 13:08:21 +0100132 print_buf( "msg1", out, out_len );
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100133
Manuel Pégourié-Gonnardbeef9c22022-01-27 11:42:47 +0100134 /* compute HMAC(key, msg2_part1 | msg2_part2) */
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100135 PSA_CHECK( psa_mac_sign_setup( &op, key, alg ) );
136 PSA_CHECK( psa_mac_update( &op, msg2_part1, sizeof( msg2_part1 ) ) );
137 PSA_CHECK( psa_mac_update( &op, msg2_part2, sizeof( msg2_part2 ) ) );
138 PSA_CHECK( psa_mac_sign_finish( &op, out, sizeof( out ), &out_len ) );
Manuel Pégourié-Gonnardf6ea19c2022-02-01 13:08:21 +0100139 print_buf( "msg2", out, out_len );
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100140
141exit:
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100142 psa_mac_abort( &op ); // needed on error, harmless on success
Manuel Pégourié-Gonnard1a45c712022-01-27 12:17:20 +0100143 psa_destroy_key( key );
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +0100144 mbedtls_platform_zeroize( out, sizeof( out ) );
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100145
146 return( status );
147}
148
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100149int main(void)
150{
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100151 psa_status_t status = PSA_SUCCESS;
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100152
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100153 /* Initialize the PSA crypto library. */
154 PSA_CHECK( psa_crypto_init( ) );
155
156 /* Run the demo */
157 PSA_CHECK( hmac_demo() );
158
159 /* Deinitialize the PSA crypto library. */
160 mbedtls_psa_crypto_free( );
161
162exit:
163 return( status == PSA_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE );
Manuel Pégourié-Gonnard667b5562021-11-22 13:00:17 +0100164}
165
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100166#endif