Manuel Pégourié-Gonnard | edf6e83 | 2022-01-27 12:36:39 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * This is a companion to hmac_psa.c, doing the same operations with the |
| 3 | * legacy MD API. The goal is that comparing the two programs will help people |
| 4 | * migrating to the PSA Crypto API. |
| 5 | * |
| 6 | * Copyright The Mbed TLS Contributors |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * When in comes to multi-part HMAC operations, the `mbedtls_md_context` |
| 24 | * serves a dual purpose (1) hold the key, and (2) save progress information |
| 25 | * for the current operation. With PSA those roles are held by two disinct |
| 26 | * objects: (1) a psa_key_id_t to hold the key, and (2) a psa_operation_t for |
| 27 | * multi-part progress. |
| 28 | * |
| 29 | * This program and its companion hmac_psa.c illustrate this by doing the |
| 30 | * same sequence of multi-part HMAC computation with both APIs; looking at the |
| 31 | * two side by side should make the differences and similarities clear. |
| 32 | */ |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | |
| 36 | #include "mbedtls/build_info.h" |
| 37 | |
| 38 | #if !defined(MBEDTLS_MD_C) |
| 39 | int main( void ) |
| 40 | { |
| 41 | printf( "MBEDTLS_MD_C not defined\r\n" ); |
| 42 | return( 0 ); |
| 43 | } |
| 44 | #else |
| 45 | |
| 46 | #include "mbedtls/md.h" |
| 47 | |
| 48 | /* |
| 49 | * Dummy inputs for HMAC |
| 50 | */ |
| 51 | const unsigned char msg1_part1[] = { 0x01, 0x02 }; |
| 52 | const unsigned char msg1_part2[] = { 0x03, 0x04 }; |
| 53 | const unsigned char msg2_part1[] = { 0x05, 0x05 }; |
| 54 | const unsigned char msg2_part2[] = { 0x06, 0x06 }; |
| 55 | |
| 56 | const unsigned char key_bytes[32] = { 0 }; |
| 57 | |
| 58 | unsigned char out[32]; |
| 59 | |
| 60 | void print_out( const char *title ) |
| 61 | { |
| 62 | printf( "%s:", title ); |
| 63 | for( size_t i = 0; i < sizeof( out ); i++ ) |
| 64 | printf( " %02x", out[i] ); |
| 65 | printf( "\n" ); |
| 66 | } |
| 67 | |
| 68 | #define CHK( code ) \ |
| 69 | do { \ |
| 70 | ret = code; \ |
| 71 | if( ret != 0 ) \ |
| 72 | goto exit; \ |
| 73 | } while( 0 ) |
| 74 | |
| 75 | int hmac_demo(void) |
| 76 | { |
| 77 | int ret; |
| 78 | mbedtls_md_context_t ctx; |
| 79 | |
| 80 | mbedtls_md_init( &ctx ); |
| 81 | |
| 82 | /* prepare context and load key */ |
| 83 | CHK( mbedtls_md_setup( &ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 ) ); |
| 84 | CHK( mbedtls_md_hmac_starts( &ctx, key_bytes, sizeof( key_bytes ) ) ); |
| 85 | |
| 86 | /* compute HMAC(key, msg1_part1 | msg1_part2) */ |
| 87 | CHK( mbedtls_md_hmac_update( &ctx, msg1_part1, sizeof( msg1_part1 ) ) ); |
| 88 | CHK( mbedtls_md_hmac_update( &ctx, msg1_part2, sizeof( msg1_part2 ) ) ); |
| 89 | CHK( mbedtls_md_hmac_finish( &ctx, out ) ); |
| 90 | print_out( "msg1" ); |
| 91 | |
| 92 | /* compute HMAC(key, msg2_part1 | msg2_part2) */ |
| 93 | CHK( mbedtls_md_hmac_reset( &ctx ) ); // prepare for new operation |
| 94 | CHK( mbedtls_md_hmac_update( &ctx, msg2_part1, sizeof( msg2_part1 ) ) ); |
| 95 | CHK( mbedtls_md_hmac_update( &ctx, msg2_part2, sizeof( msg2_part2 ) ) ); |
| 96 | CHK( mbedtls_md_hmac_finish( &ctx, out ) ); |
| 97 | print_out( "msg2" ); |
| 98 | |
| 99 | exit: |
| 100 | mbedtls_md_free( &ctx ); |
| 101 | |
| 102 | return( ret ); |
| 103 | } |
| 104 | |
| 105 | int main(void) |
| 106 | { |
| 107 | int ret = hmac_demo(); |
| 108 | if( ret != 0 ) |
| 109 | printf( "ret = %d (-0x%04x)\n", ret, (unsigned) -ret ); |
| 110 | |
| 111 | } |
| 112 | |
| 113 | #endif |