Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 1 | /** |
| 2 | * PSA API multi-part HMAC demonstration. |
Manuel Pégourié-Gonnard | 0e725c3 | 2022-01-27 11:15:33 +0100 | [diff] [blame] | 3 | * |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 4 | * This programs computes the HMAC of two messages using the multi-part API. |
| 5 | * |
| 6 | * It comes with a companion program hmac_non_psa.c, which does the same |
| 7 | * 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 | * |
| 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 | 667b556 | 2021-11-22 13:00:17 +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 | 667b556 | 2021-11-22 13:00:17 +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 "psa/crypto.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_PSA_CRYPTO_C) || \ |
Manuel Pégourié-Gonnard | 9efbf53 | 2022-01-17 11:57:44 +0100 | [diff] [blame] | 50 | defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 51 | int main( void ) |
| 52 | { |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 53 | printf( "MBEDTLS_PSA_CRYPTO_C not defined, " |
Manuel Pégourié-Gonnard | 9efbf53 | 2022-01-17 11:57:44 +0100 | [diff] [blame] | 54 | "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n" ); |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 55 | return( 0 ); |
| 56 | } |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 57 | #else |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 59 | /* The real program starts here. */ |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 61 | /* Dummy inputs for HMAC */ |
Manuel Pégourié-Gonnard | beef9c2 | 2022-01-27 11:42:47 +0100 | [diff] [blame] | 62 | const unsigned char msg1_part1[] = { 0x01, 0x02 }; |
| 63 | const unsigned char msg1_part2[] = { 0x03, 0x04 }; |
| 64 | const unsigned char msg2_part1[] = { 0x05, 0x05 }; |
| 65 | const unsigned char msg2_part2[] = { 0x06, 0x06 }; |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 66 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 67 | /* 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é-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 69 | const unsigned char key_bytes[32] = { 0 }; |
| 70 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 71 | /* Buffer for the output - using SHA-256, so 32-byte output */ |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 72 | unsigned char out[32]; |
| 73 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 74 | /* Print the contents of the output buffer in hex */ |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 75 | void print_out( const char *title ) |
| 76 | { |
| 77 | printf( "%s:", title ); |
| 78 | for( size_t i = 0; i < sizeof( out ); i++ ) |
| 79 | printf( " %02x", out[i] ); |
| 80 | printf( "\n" ); |
| 81 | } |
| 82 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 83 | /* Run a PSA function and bail out if it fails. */ |
| 84 | #define PSA_CHECK( expr ) \ |
| 85 | do \ |
| 86 | { \ |
| 87 | status = ( expr ); \ |
| 88 | if( status != PSA_SUCCESS ) \ |
| 89 | { \ |
| 90 | printf( "Error %d at line %d: %s\n", \ |
| 91 | (int) status, \ |
| 92 | __LINE__, \ |
| 93 | #expr ); \ |
| 94 | goto exit; \ |
| 95 | } \ |
| 96 | } \ |
| 97 | while( 0 ) |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 98 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 99 | /* |
| 100 | * This function demonstrates computation of the HMAC of two messages using |
| 101 | * the multipart API. |
| 102 | */ |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 103 | psa_status_t hmac_demo(void) |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 104 | { |
| 105 | psa_status_t status; |
| 106 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 107 | psa_key_id_t key = 0; |
| 108 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
| 109 | |
| 110 | /* prepare key */ |
| 111 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 112 | psa_set_key_algorithm( &attributes, alg ); |
| 113 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 114 | psa_set_key_bits( &attributes, 8 * sizeof( key_bytes ) ); // optional |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 115 | |
| 116 | status = psa_import_key( &attributes, key_bytes, sizeof( key_bytes ), &key ); |
| 117 | if( status != PSA_SUCCESS ) |
| 118 | return( status ); |
| 119 | |
| 120 | /* prepare operation */ |
| 121 | psa_mac_operation_t op = PSA_MAC_OPERATION_INIT; |
| 122 | size_t out_len = 0; |
| 123 | |
Manuel Pégourié-Gonnard | beef9c2 | 2022-01-27 11:42:47 +0100 | [diff] [blame] | 124 | /* compute HMAC(key, msg1_part1 | msg1_part2) */ |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 125 | PSA_CHECK( psa_mac_sign_setup( &op, key, alg ) ); |
| 126 | PSA_CHECK( psa_mac_update( &op, msg1_part1, sizeof( msg1_part1 ) ) ); |
| 127 | PSA_CHECK( psa_mac_update( &op, msg1_part2, sizeof( msg1_part2 ) ) ); |
| 128 | PSA_CHECK( psa_mac_sign_finish( &op, out, sizeof( out ), &out_len ) ); |
Manuel Pégourié-Gonnard | beef9c2 | 2022-01-27 11:42:47 +0100 | [diff] [blame] | 129 | print_out( "msg1" ); |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 130 | |
Manuel Pégourié-Gonnard | beef9c2 | 2022-01-27 11:42:47 +0100 | [diff] [blame] | 131 | /* compute HMAC(key, msg2_part1 | msg2_part2) */ |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 132 | PSA_CHECK( psa_mac_sign_setup( &op, key, alg ) ); |
| 133 | PSA_CHECK( psa_mac_update( &op, msg2_part1, sizeof( msg2_part1 ) ) ); |
| 134 | PSA_CHECK( psa_mac_update( &op, msg2_part2, sizeof( msg2_part2 ) ) ); |
| 135 | PSA_CHECK( psa_mac_sign_finish( &op, out, sizeof( out ), &out_len ) ); |
Manuel Pégourié-Gonnard | beef9c2 | 2022-01-27 11:42:47 +0100 | [diff] [blame] | 136 | print_out( "msg2" ); |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 137 | |
| 138 | exit: |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 139 | psa_mac_abort( &op ); // needed on error, harmless on success |
Manuel Pégourié-Gonnard | 1a45c71 | 2022-01-27 12:17:20 +0100 | [diff] [blame] | 140 | psa_destroy_key( key ); |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 141 | |
| 142 | return( status ); |
| 143 | } |
| 144 | |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 145 | int main(void) |
| 146 | { |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 147 | psa_status_t status = PSA_SUCCESS; |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 148 | |
Manuel Pégourié-Gonnard | f392a02 | 2022-01-31 12:06:07 +0100 | [diff] [blame^] | 149 | /* Initialize the PSA crypto library. */ |
| 150 | PSA_CHECK( psa_crypto_init( ) ); |
| 151 | |
| 152 | /* Run the demo */ |
| 153 | PSA_CHECK( hmac_demo() ); |
| 154 | |
| 155 | /* Deinitialize the PSA crypto library. */ |
| 156 | mbedtls_psa_crypto_free( ); |
| 157 | |
| 158 | exit: |
| 159 | return( status == PSA_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE ); |
Manuel Pégourié-Gonnard | 667b556 | 2021-11-22 13:00:17 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame] | 162 | #endif |