Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 1 | /** |
| 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é-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 6 | * This is a companion to hmac_psa.c, doing the same operations with the |
| 7 | * 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é-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 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 | * |
| 16 | * This program and its companion hmac_non_psa.c illustrate this by doing the |
| 17 | * 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é-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 22 | * 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é-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 38 | /* 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é-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 41 | #include "mbedtls/build_info.h" |
| 42 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 43 | #include "mbedtls/md.h" |
| 44 | |
| 45 | #include <stdlib.h> |
| 46 | #include <stdio.h> |
| 47 | |
| 48 | /* If the build options we need are not enabled, compile a placeholder. */ |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 49 | #if !defined(MBEDTLS_MD_C) |
| 50 | int main( void ) |
| 51 | { |
| 52 | printf( "MBEDTLS_MD_C not defined\r\n" ); |
| 53 | return( 0 ); |
| 54 | } |
| 55 | #else |
| 56 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 57 | /* The real program starts here. */ |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 59 | /* Dummy inputs for HMAC */ |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 60 | const unsigned char msg1_part1[] = { 0x01, 0x02 }; |
| 61 | const unsigned char msg1_part2[] = { 0x03, 0x04 }; |
| 62 | const unsigned char msg2_part1[] = { 0x05, 0x05 }; |
| 63 | const unsigned char msg2_part2[] = { 0x06, 0x06 }; |
| 64 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 65 | /* Dummy key material - never do this in production! |
| 66 | * This example program uses SHA-256, so a 32-byte key makes sense. */ |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 67 | const unsigned char key_bytes[32] = { 0 }; |
| 68 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 69 | /* Buffer for the output - using SHA-256, so 32-byte output */ |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 70 | unsigned char out[32]; |
| 71 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 72 | /* Print the contents of the output buffer in hex */ |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 73 | void print_out( const char *title ) |
| 74 | { |
| 75 | printf( "%s:", title ); |
| 76 | for( size_t i = 0; i < sizeof( out ); i++ ) |
| 77 | printf( " %02x", out[i] ); |
| 78 | printf( "\n" ); |
| 79 | } |
| 80 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 81 | /* Run an Mbed TLS function and bail out if it fails. */ |
| 82 | #define CHK( expr ) \ |
| 83 | do \ |
| 84 | { \ |
| 85 | ret = ( expr ); \ |
| 86 | if( ret != 0 ) \ |
| 87 | { \ |
| 88 | printf( "Error %d at line %d: %s\n", \ |
| 89 | ret, \ |
| 90 | __LINE__, \ |
| 91 | #expr ); \ |
| 92 | goto exit; \ |
| 93 | } \ |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 94 | } while( 0 ) |
| 95 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 96 | /* |
| 97 | * This function demonstrates computation of the HMAC of two messages using |
| 98 | * the multipart API. |
| 99 | */ |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 100 | int hmac_demo(void) |
| 101 | { |
| 102 | int ret; |
| 103 | mbedtls_md_context_t ctx; |
| 104 | |
| 105 | mbedtls_md_init( &ctx ); |
| 106 | |
| 107 | /* prepare context and load key */ |
| 108 | CHK( mbedtls_md_setup( &ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 ) ); |
| 109 | CHK( mbedtls_md_hmac_starts( &ctx, key_bytes, sizeof( key_bytes ) ) ); |
| 110 | |
| 111 | /* compute HMAC(key, msg1_part1 | msg1_part2) */ |
| 112 | CHK( mbedtls_md_hmac_update( &ctx, msg1_part1, sizeof( msg1_part1 ) ) ); |
| 113 | CHK( mbedtls_md_hmac_update( &ctx, msg1_part2, sizeof( msg1_part2 ) ) ); |
| 114 | CHK( mbedtls_md_hmac_finish( &ctx, out ) ); |
| 115 | print_out( "msg1" ); |
| 116 | |
| 117 | /* compute HMAC(key, msg2_part1 | msg2_part2) */ |
| 118 | CHK( mbedtls_md_hmac_reset( &ctx ) ); // prepare for new operation |
| 119 | CHK( mbedtls_md_hmac_update( &ctx, msg2_part1, sizeof( msg2_part1 ) ) ); |
| 120 | CHK( mbedtls_md_hmac_update( &ctx, msg2_part2, sizeof( msg2_part2 ) ) ); |
| 121 | CHK( mbedtls_md_hmac_finish( &ctx, out ) ); |
| 122 | print_out( "msg2" ); |
| 123 | |
| 124 | exit: |
| 125 | mbedtls_md_free( &ctx ); |
| 126 | |
| 127 | return( ret ); |
| 128 | } |
| 129 | |
| 130 | int main(void) |
| 131 | { |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 132 | int ret; |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 133 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 134 | CHK( hmac_demo() ); |
| 135 | |
| 136 | exit: |
| 137 | return( ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE ); |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | #endif |