Split hmac_md_psa.c
Having two programs might make comparison easier, and will make it easier to people to use just the PSA one as an example. Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
This commit is contained in:
parent
1a45c713f0
commit
edf6e83cbc
3
programs/.gitignore
vendored
3
programs/.gitignore
vendored
@ -40,7 +40,8 @@ pkey/rsa_verify
|
|||||||
pkey/rsa_verify_pss
|
pkey/rsa_verify_pss
|
||||||
psa/aead_cipher_psa
|
psa/aead_cipher_psa
|
||||||
psa/crypto_examples
|
psa/crypto_examples
|
||||||
psa/hmac_md_psa
|
psa/hmac_non_psa
|
||||||
|
psa/hmac_psa
|
||||||
psa/key_ladder_demo
|
psa/key_ladder_demo
|
||||||
psa/psa_constant_names
|
psa/psa_constant_names
|
||||||
random/gen_entropy
|
random/gen_entropy
|
||||||
|
@ -86,7 +86,8 @@ APPS = \
|
|||||||
pkey/rsa_verify_pss \
|
pkey/rsa_verify_pss \
|
||||||
psa/aead_cipher_psa \
|
psa/aead_cipher_psa \
|
||||||
psa/crypto_examples \
|
psa/crypto_examples \
|
||||||
psa/hmac_md_psa \
|
psa/hmac_non_psa \
|
||||||
|
psa/hmac_psa \
|
||||||
psa/key_ladder_demo \
|
psa/key_ladder_demo \
|
||||||
psa/psa_constant_names \
|
psa/psa_constant_names \
|
||||||
random/gen_entropy \
|
random/gen_entropy \
|
||||||
@ -271,9 +272,13 @@ psa/crypto_examples$(EXEXT): psa/crypto_examples.c $(DEP)
|
|||||||
echo " CC psa/crypto_examples.c"
|
echo " CC psa/crypto_examples.c"
|
||||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/crypto_examples.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/crypto_examples.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||||
|
|
||||||
psa/hmac_md_psa$(EXEXT): psa/hmac_md_psa.c $(DEP)
|
psa/hmac_non_psa$(EXEXT): psa/hmac_non_psa.c $(DEP)
|
||||||
echo " CC psa/hmac_md_psa.c"
|
echo " CC psa/hmac_non_psa.c"
|
||||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/hmac_md_psa.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/hmac_non_psa.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||||
|
|
||||||
|
psa/hmac_psa$(EXEXT): psa/hmac_psa.c $(DEP)
|
||||||
|
echo " CC psa/hmac_psa.c"
|
||||||
|
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/hmac_psa.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||||
|
|
||||||
psa/key_ladder_demo$(EXEXT): psa/key_ladder_demo.c $(DEP)
|
psa/key_ladder_demo$(EXEXT): psa/key_ladder_demo.c $(DEP)
|
||||||
echo " CC psa/key_ladder_demo.c"
|
echo " CC psa/key_ladder_demo.c"
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
set(executables
|
set(executables
|
||||||
aead_cipher_psa
|
aead_cipher_psa
|
||||||
crypto_examples
|
crypto_examples
|
||||||
hmac_md_psa
|
hmac_non_psa
|
||||||
|
hmac_psa
|
||||||
key_ladder_demo
|
key_ladder_demo
|
||||||
psa_constant_names
|
psa_constant_names
|
||||||
)
|
)
|
||||||
|
113
programs/psa/hmac_non_psa.c
Normal file
113
programs/psa/hmac_non_psa.c
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* This is a companion to hmac_psa.c, doing the same operations with the
|
||||||
|
* legacy MD API. The goal is that comparing the two programs will help people
|
||||||
|
* migrating to the PSA Crypto API.
|
||||||
|
*
|
||||||
|
* Copyright The Mbed TLS Contributors
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
* not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* When in comes to multi-part HMAC operations, the `mbedtls_md_context`
|
||||||
|
* serves a dual purpose (1) hold the key, and (2) save progress information
|
||||||
|
* for the current operation. With PSA those roles are held by two disinct
|
||||||
|
* objects: (1) a psa_key_id_t to hold the key, and (2) a psa_operation_t for
|
||||||
|
* multi-part progress.
|
||||||
|
*
|
||||||
|
* This program and its companion hmac_psa.c illustrate this by doing the
|
||||||
|
* same sequence of multi-part HMAC computation with both APIs; looking at the
|
||||||
|
* two side by side should make the differences and similarities clear.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "mbedtls/build_info.h"
|
||||||
|
|
||||||
|
#if !defined(MBEDTLS_MD_C)
|
||||||
|
int main( void )
|
||||||
|
{
|
||||||
|
printf( "MBEDTLS_MD_C not defined\r\n" );
|
||||||
|
return( 0 );
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include "mbedtls/md.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Dummy inputs for HMAC
|
||||||
|
*/
|
||||||
|
const unsigned char msg1_part1[] = { 0x01, 0x02 };
|
||||||
|
const unsigned char msg1_part2[] = { 0x03, 0x04 };
|
||||||
|
const unsigned char msg2_part1[] = { 0x05, 0x05 };
|
||||||
|
const unsigned char msg2_part2[] = { 0x06, 0x06 };
|
||||||
|
|
||||||
|
const unsigned char key_bytes[32] = { 0 };
|
||||||
|
|
||||||
|
unsigned char out[32];
|
||||||
|
|
||||||
|
void print_out( const char *title )
|
||||||
|
{
|
||||||
|
printf( "%s:", title );
|
||||||
|
for( size_t i = 0; i < sizeof( out ); i++ )
|
||||||
|
printf( " %02x", out[i] );
|
||||||
|
printf( "\n" );
|
||||||
|
}
|
||||||
|
|
||||||
|
#define CHK( code ) \
|
||||||
|
do { \
|
||||||
|
ret = code; \
|
||||||
|
if( ret != 0 ) \
|
||||||
|
goto exit; \
|
||||||
|
} while( 0 )
|
||||||
|
|
||||||
|
int hmac_demo(void)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
mbedtls_md_context_t ctx;
|
||||||
|
|
||||||
|
mbedtls_md_init( &ctx );
|
||||||
|
|
||||||
|
/* prepare context and load key */
|
||||||
|
CHK( mbedtls_md_setup( &ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 ) );
|
||||||
|
CHK( mbedtls_md_hmac_starts( &ctx, key_bytes, sizeof( key_bytes ) ) );
|
||||||
|
|
||||||
|
/* compute HMAC(key, msg1_part1 | msg1_part2) */
|
||||||
|
CHK( mbedtls_md_hmac_update( &ctx, msg1_part1, sizeof( msg1_part1 ) ) );
|
||||||
|
CHK( mbedtls_md_hmac_update( &ctx, msg1_part2, sizeof( msg1_part2 ) ) );
|
||||||
|
CHK( mbedtls_md_hmac_finish( &ctx, out ) );
|
||||||
|
print_out( "msg1" );
|
||||||
|
|
||||||
|
/* compute HMAC(key, msg2_part1 | msg2_part2) */
|
||||||
|
CHK( mbedtls_md_hmac_reset( &ctx ) ); // prepare for new operation
|
||||||
|
CHK( mbedtls_md_hmac_update( &ctx, msg2_part1, sizeof( msg2_part1 ) ) );
|
||||||
|
CHK( mbedtls_md_hmac_update( &ctx, msg2_part2, sizeof( msg2_part2 ) ) );
|
||||||
|
CHK( mbedtls_md_hmac_finish( &ctx, out ) );
|
||||||
|
print_out( "msg2" );
|
||||||
|
|
||||||
|
exit:
|
||||||
|
mbedtls_md_free( &ctx );
|
||||||
|
|
||||||
|
return( ret );
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int ret = hmac_demo();
|
||||||
|
if( ret != 0 )
|
||||||
|
printf( "ret = %d (-0x%04x)\n", ret, (unsigned) -ret );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,6 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
* This is a simple example of multi-part HMAC computation using both the old
|
* This is a simple example of multi-part HMAC computation using the PSA
|
||||||
* MD API and the new PSA API; its goal is to help migration to PSA Crypto.
|
* Crypto API. It comes with a companion program hmac_non_psa.c, which does
|
||||||
|
* the same operations with the legacy MD API. The goal is that comparing the
|
||||||
|
* two programs will help people migrating to the PSA Crypto API.
|
||||||
*
|
*
|
||||||
* Copyright The Mbed TLS Contributors
|
* Copyright The Mbed TLS Contributors
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
@ -25,26 +27,25 @@
|
|||||||
* objects: (1) a psa_key_id_t to hold the key, and (2) a psa_operation_t for
|
* objects: (1) a psa_key_id_t to hold the key, and (2) a psa_operation_t for
|
||||||
* multi-part progress.
|
* multi-part progress.
|
||||||
*
|
*
|
||||||
* This program illustrates this by doing the same sequence of multi-part HMAC
|
* This program and its companion hmac_non_psa.c illustrate this by doing the
|
||||||
* computation with both APIs; looking at the two functions md() and mac() side
|
* same sequence of multi-part HMAC computation with both APIs; looking at the
|
||||||
* by side should make the differences and similarities clear.
|
* two side by side should make the differences and similarities clear.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "mbedtls/build_info.h"
|
#include "mbedtls/build_info.h"
|
||||||
|
|
||||||
#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_MD_C) || \
|
#if !defined(MBEDTLS_PSA_CRYPTO_C) || \
|
||||||
defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
||||||
int main( void )
|
int main( void )
|
||||||
{
|
{
|
||||||
printf( "MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_MD_C not defined, "
|
printf( "MBEDTLS_PSA_CRYPTO_C not defined, "
|
||||||
"and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n" );
|
"and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n" );
|
||||||
return( 0 );
|
return( 0 );
|
||||||
}
|
}
|
||||||
#else /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_MD_C */
|
#else
|
||||||
|
|
||||||
#include "mbedtls/md.h"
|
|
||||||
#include "psa/crypto.h"
|
#include "psa/crypto.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -67,45 +68,6 @@ void print_out( const char *title )
|
|||||||
printf( "\n" );
|
printf( "\n" );
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CHK( code ) \
|
|
||||||
do { \
|
|
||||||
ret = code; \
|
|
||||||
if( ret != 0 ) \
|
|
||||||
goto exit; \
|
|
||||||
} while( 0 )
|
|
||||||
|
|
||||||
int md(void)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
mbedtls_md_context_t ctx;
|
|
||||||
|
|
||||||
mbedtls_md_init( &ctx );
|
|
||||||
|
|
||||||
/* prepare context and load key */
|
|
||||||
CHK( mbedtls_md_setup( &ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 ) );
|
|
||||||
CHK( mbedtls_md_hmac_starts( &ctx, key_bytes, sizeof( key_bytes ) ) );
|
|
||||||
|
|
||||||
/* compute HMAC(key, msg1_part1 | msg1_part2) */
|
|
||||||
CHK( mbedtls_md_hmac_update( &ctx, msg1_part1, sizeof( msg1_part1 ) ) );
|
|
||||||
CHK( mbedtls_md_hmac_update( &ctx, msg1_part2, sizeof( msg1_part2 ) ) );
|
|
||||||
CHK( mbedtls_md_hmac_finish( &ctx, out ) );
|
|
||||||
print_out( "msg1" );
|
|
||||||
|
|
||||||
/* compute HMAC(key, msg2_part1 | msg2_part2) */
|
|
||||||
CHK( mbedtls_md_hmac_reset( &ctx ) ); // prepare for new operation
|
|
||||||
CHK( mbedtls_md_hmac_update( &ctx, msg2_part1, sizeof( msg2_part1 ) ) );
|
|
||||||
CHK( mbedtls_md_hmac_update( &ctx, msg2_part2, sizeof( msg2_part2 ) ) );
|
|
||||||
CHK( mbedtls_md_hmac_finish( &ctx, out ) );
|
|
||||||
print_out( "msg2" );
|
|
||||||
|
|
||||||
exit:
|
|
||||||
mbedtls_md_free( &ctx );
|
|
||||||
|
|
||||||
return( ret );
|
|
||||||
}
|
|
||||||
|
|
||||||
#undef CHK
|
|
||||||
|
|
||||||
#define CHK( code ) \
|
#define CHK( code ) \
|
||||||
do { \
|
do { \
|
||||||
status = code; \
|
status = code; \
|
||||||
@ -113,7 +75,7 @@ exit:
|
|||||||
goto exit; \
|
goto exit; \
|
||||||
} while( 0 )
|
} while( 0 )
|
||||||
|
|
||||||
psa_status_t mac(void)
|
psa_status_t hmac_demo(void)
|
||||||
{
|
{
|
||||||
psa_status_t status;
|
psa_status_t status;
|
||||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||||
@ -155,23 +117,15 @@ exit:
|
|||||||
return( status );
|
return( status );
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef CHK
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
printf( "MD\n" );
|
|
||||||
int ret = md();
|
|
||||||
if( ret != 0 )
|
|
||||||
printf( "ret = %d (-0x%04x)\n", ret, (unsigned) -ret );
|
|
||||||
|
|
||||||
psa_status_t status = psa_crypto_init();
|
psa_status_t status = psa_crypto_init();
|
||||||
if( status != PSA_SUCCESS )
|
if( status != PSA_SUCCESS )
|
||||||
printf( "psa init: %d\n", status );
|
printf( "psa init: %d\n", status );
|
||||||
|
|
||||||
printf( "\nPSA\n" );
|
status = hmac_demo();
|
||||||
status = mac();
|
|
||||||
if( status != PSA_SUCCESS )
|
if( status != PSA_SUCCESS )
|
||||||
printf( "psa mac: %d\n", status );
|
printf( "hmac_demo: %d\n", status );
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_MD_C */
|
#endif
|
Loading…
Reference in New Issue
Block a user