blob: 0f6495c866ed0e83aa2a4cca699677917a1b688d [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
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é-Gonnardedf6e832022-01-27 12:36:39 +010041#include "mbedtls/build_info.h"
42
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010043#include "mbedtls/md.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_MD_C)
52int main( void )
53{
54 printf( "MBEDTLS_MD_C not defined\r\n" );
55 return( 0 );
56}
57#else
58
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010059/* The real program starts here. */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010060
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010061/* Dummy inputs for HMAC */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010062const unsigned char msg1_part1[] = { 0x01, 0x02 };
63const unsigned char msg1_part2[] = { 0x03, 0x04 };
64const unsigned char msg2_part1[] = { 0x05, 0x05 };
65const unsigned char msg2_part2[] = { 0x06, 0x06 };
66
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010067/* Dummy key material - never do this in production!
68 * This example program uses SHA-256, so a 32-byte key makes sense. */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010069const unsigned char key_bytes[32] = { 0 };
70
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010071/* Print the contents of a buffer in hex */
72void print_buf( const char *title, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010073{
74 printf( "%s:", title );
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +010075 for( size_t i = 0; i < len; i++ )
76 printf( " %02x", buf[i] );
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010077 printf( "\n" );
78}
79
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010080/* Run an Mbed TLS function and bail out if it fails. */
81#define CHK( expr ) \
82 do \
83 { \
84 ret = ( expr ); \
85 if( ret != 0 ) \
86 { \
87 printf( "Error %d at line %d: %s\n", \
88 ret, \
89 __LINE__, \
90 #expr ); \
91 goto exit; \
92 } \
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010093 } while( 0 )
94
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +010095/*
96 * This function demonstrates computation of the HMAC of two messages using
97 * the multipart API.
98 */
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +010099int hmac_demo(void)
100{
101 int ret;
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +0100102 const mbedtls_md_type_t alg = MBEDTLS_MD_SHA256;
103 unsigned char out[MBEDTLS_MD_MAX_SIZE]; // safe but not optimal
104
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100105 mbedtls_md_context_t ctx;
106
107 mbedtls_md_init( &ctx );
108
109 /* prepare context and load key */
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +0100110 // the last argument to setup is 1 to enable HMAC (not just hashing)
Manuel Pégourié-Gonnard12ec5712022-02-01 09:47:46 +0100111 const mbedtls_md_info_t *info = mbedtls_md_info_from_type( alg );
112 CHK( mbedtls_md_setup( &ctx, info, 1 ) );
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100113 CHK( mbedtls_md_hmac_starts( &ctx, key_bytes, sizeof( key_bytes ) ) );
114
115 /* compute HMAC(key, msg1_part1 | msg1_part2) */
116 CHK( mbedtls_md_hmac_update( &ctx, msg1_part1, sizeof( msg1_part1 ) ) );
117 CHK( mbedtls_md_hmac_update( &ctx, msg1_part2, sizeof( msg1_part2 ) ) );
118 CHK( mbedtls_md_hmac_finish( &ctx, out ) );
Manuel Pégourié-Gonnard12ec5712022-02-01 09:47:46 +0100119 print_buf( "msg1", out, mbedtls_md_get_size( info ) );
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100120
121 /* compute HMAC(key, msg2_part1 | msg2_part2) */
122 CHK( mbedtls_md_hmac_reset( &ctx ) ); // prepare for new operation
123 CHK( mbedtls_md_hmac_update( &ctx, msg2_part1, sizeof( msg2_part1 ) ) );
124 CHK( mbedtls_md_hmac_update( &ctx, msg2_part2, sizeof( msg2_part2 ) ) );
125 CHK( mbedtls_md_hmac_finish( &ctx, out ) );
Manuel Pégourié-Gonnard12ec5712022-02-01 09:47:46 +0100126 print_buf( "msg2", out, mbedtls_md_get_size( info ) );
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100127
128exit:
129 mbedtls_md_free( &ctx );
Manuel Pégourié-Gonnard63497942022-01-31 12:23:37 +0100130 mbedtls_platform_zeroize( out, sizeof( out ) );
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100131
132 return( ret );
133}
134
135int main(void)
136{
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100137 int ret;
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100138
Manuel Pégourié-Gonnardf392a022022-01-31 12:06:07 +0100139 CHK( hmac_demo() );
140
141exit:
142 return( ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE );
Manuel Pégourié-Gonnardedf6e832022-01-27 12:36:39 +0100143}
144
145#endif