mirror of
https://github.com/yuzu-mirror/mbedtls.git
synced 2026-04-04 14:08:39 +00:00
Merge mbed-crypto into mbedtls: the merge commit
Merge `unremove-non-crypto` into `mbedtls/development`. The branch
`unremove-non-crypto` was obtained by starting from `mbed-crypto/development`,
then reverting many commits that removed X.509 and TLS functionality when Mbed
Crypto forked from Mbed TLS (the “unremoval”), then make a few tweaks to
facilitate the merge.
The unremoval step restored old versions of some tls files. If a file doesn't
exist in mbed-crypto, check out the mbedtls version, regardless of what
happened during the unremoval of tls files in the crypto tree. Also
unconditionally take the mbedtls version of a few files where the
modifications are completely project-specific and are not relevant in
mbed-crypto:
* `.github/issue_template.md`: completely different. We may want to reconcile
them independently as a follow-up.
* `.travis.yml`: would only be reverted to an earlier tls version.
* `README.md`: completely different. We may want to reconcile them
independently as a follow-up.
* `doxygen/input/doc_mainpage.h`: the changes in crypto were minimal and not
relevant except as a stopgap as mbed-crypto did not have its own product
versioning in the Doxygen documentation.
* `tests/.jenkins/Jenkinsfile`: completely different.
* `tests/data_files/Makefile`: there were no changes in mbed-crypto,
but the unremoval step restored an old version.
Shell script for everything to do after the merge apart from the conflict
resolution:
```
tls_files=($(comm -23 <(git ls-tree -r --name-only HEAD) <(git ls-tree -r --name-only $(git merge-base upstream-crypto/development MERGE_HEAD))))
tls_files+=($tls_files .github/issue_template.md .travis.yml README.md doxygen/input/doc_mainpage.h tests/.jenkins/Jenkinsfile tests/data_files/Makefile)
git checkout --theirs HEAD -- $tls_files
git add -- $tls_files
```
Resolve the remaining conflicts:
* `library/CMakeLists.txt`:
* Keep the TLS definition of `src_crypto`
* `USE_SHARED_MBEDTLS_LIBRARY`: keep all three libraries, with both
`include` and `crypto/include` in `target_include_directories`, all with
version `2.21.0`.
* `programs/Makefile`:
* Reconcile the APPS lists (add/add from a differently-formatted common
ancestor): insert the `psa/*` from crypto into the tls list.
* Keep the `fuzz` target defined only in tls version.
* Keep the recipe (only in tls version) cleaning `ssl_pthread_server`
stuff for the `clean` target.
* `scripts/config.py`:
* `include_in_full`: add/add conflict. Keep both.
* `tests/scripts/all.sh`:
* `component_test_no_use_psa_crypto_full_cmake_asan`: partially old
version in crypto. Take the tls version.
* `component_test_malloc_0_null` and more: take
`component_test_malloc_0_null` from crypto (with `config.py` rather than
`config.pl`, and with `$ASAN_FLAGS` rather than an explicit list), but
add the call to `ssl-opt.sh` from tls. Take the other components from
crypto.
With this commit, building and running the unit tests with both `make ` and
`cmake` work in the default configuration on Linux. Other platforms, build
systems and configurations are likely not to work, and there is some
regression in test coverage.
There is some loss of functionality because the unremoval step restored older
versions of tls content. This commit contains the latest tls version of
tls-only files, but some changes from the tls side in files that existed on
both sides have regressed. Most problematic changes are hunks that remove some
tls-specific feature and contain either a C preprocessor symbol identifying a
tls-specific module or option, or the name of a tls-specific file. Hunks
that remove a tls-specific preprocessor symbol can be identified with the
regular expression `^-.*MBEDTLS_(ERR_)?(PKCS11|X509|NET|SSL)_`.
Subsequent commits will revert a few parts of the patch from this merge commit
in order to restore the tls functionality that it removes, ensure that the
test coverage includes what was covered in either branch, and fix test
failures.
This commit is contained in:
commit
b99bd39b4e
397 changed files with 168444 additions and 995 deletions
4
programs/.gitignore
vendored
4
programs/.gitignore
vendored
|
|
@ -29,6 +29,10 @@ pkey/rsa_sign
|
|||
pkey/rsa_sign_pss
|
||||
pkey/rsa_verify
|
||||
pkey/rsa_verify_pss
|
||||
psa/crypto_examples
|
||||
psa/psa_constant_names
|
||||
psa/psa_constant_names_generated.c
|
||||
psa/key_ladder_demo
|
||||
random/gen_entropy
|
||||
random/gen_random_ctr_drbg
|
||||
random/gen_random_havege
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
add_subdirectory(aes)
|
||||
add_subdirectory(hash)
|
||||
add_subdirectory(pkey)
|
||||
add_subdirectory(psa)
|
||||
add_subdirectory(random)
|
||||
add_subdirectory(ssl)
|
||||
add_subdirectory(test)
|
||||
add_subdirectory(x509)
|
||||
add_subdirectory(util)
|
||||
if (NOT WIN32)
|
||||
add_subdirectory(fuzz)
|
||||
endif()
|
||||
|
|
|
|||
|
|
@ -14,14 +14,14 @@ LOCAL_LDFLAGS = -L../library \
|
|||
-lmbedx509$(SHARED_SUFFIX) \
|
||||
-lmbedcrypto$(SHARED_SUFFIX)
|
||||
|
||||
LOCAL_LDFLAGS += -L../crypto/library
|
||||
LOCAL_CFLAGS += -I../crypto/include
|
||||
LOCAL_CXXFLAGS += -I../crypto/include
|
||||
include ../3rdparty/Makefile.inc
|
||||
LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
|
||||
|
||||
INCLUDING_FROM_MBEDTLS:=1
|
||||
include ../crypto/3rdparty/Makefile.inc
|
||||
LOCAL_CFLAGS += $(patsubst -I../3rdparty/%, -I../crypto/3rdparty/%, $(THIRDPARTY_INCLUDES))
|
||||
LOCAL_CFLAGS += $(patsubst -I../3rdparty/%, -I../crypto/3rdparty/%, $(THIRDPARTY_INCLUDES))
|
||||
ifndef SHARED
|
||||
DEP=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
|
||||
else
|
||||
DEP=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
|
||||
endif
|
||||
|
||||
ifdef DEBUG
|
||||
LOCAL_CFLAGS += -g3
|
||||
|
|
@ -45,12 +45,6 @@ EXEXT=
|
|||
SHARED_SUFFIX=
|
||||
endif
|
||||
|
||||
ifndef SHARED
|
||||
DEP=../crypto/library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
|
||||
else
|
||||
DEP=../crypto/library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
|
||||
endif
|
||||
|
||||
# Zlib shared library extensions:
|
||||
ifdef ZLIB
|
||||
LOCAL_LDFLAGS += -lz
|
||||
|
|
@ -81,6 +75,9 @@ APPS = \
|
|||
pkey/rsa_verify$(EXEXT) \
|
||||
pkey/rsa_sign_pss$(EXEXT) \
|
||||
pkey/rsa_verify_pss$(EXEXT) \
|
||||
psa/crypto_examples$(EXEXT) \
|
||||
psa/key_ladder_demo$(EXEXT) \
|
||||
psa/psa_constant_names$(EXEXT) \
|
||||
ssl/dtls_client$(EXEXT) \
|
||||
ssl/dtls_server$(EXEXT) \
|
||||
ssl/ssl_client1$(EXEXT) \
|
||||
|
|
@ -115,6 +112,8 @@ ifdef TEST_CPP
|
|||
APPS += test/cpp_dummy_build$(EXEXT)
|
||||
endif
|
||||
|
||||
EXTRA_GENERATED =
|
||||
|
||||
.SILENT:
|
||||
|
||||
.PHONY: all clean list fuzz
|
||||
|
|
@ -133,6 +132,16 @@ fuzz:
|
|||
$(DEP):
|
||||
$(MAKE) -C ../library
|
||||
|
||||
ifdef WINDOWS
|
||||
EXTRA_GENERATED += psa\psa_constant_names_generated.c
|
||||
else
|
||||
EXTRA_GENERATED += psa/psa_constant_names_generated.c
|
||||
endif
|
||||
|
||||
psa/psa_constant_names$(EXEXT): psa/psa_constant_names_generated.c
|
||||
psa/psa_constant_names_generated.c: ../scripts/generate_psa_constants.py ../include/psa/crypto_values.h ../include/psa/crypto_extra.h
|
||||
../scripts/generate_psa_constants.py
|
||||
|
||||
aes/aescrypt2$(EXEXT): aes/aescrypt2.c $(DEP)
|
||||
echo " CC aes/aescrypt2.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) aes/aescrypt2.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
|
@ -229,6 +238,14 @@ pkey/rsa_encrypt$(EXEXT): pkey/rsa_encrypt.c $(DEP)
|
|||
echo " CC pkey/rsa_encrypt.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/rsa_encrypt.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
psa/key_ladder_demo$(EXEXT): psa/key_ladder_demo.c $(DEP)
|
||||
echo " CC psa/key_ladder_demo.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/key_ladder_demo.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
psa/psa_constant_names$(EXEXT): psa/psa_constant_names.c $(DEP)
|
||||
echo " CC psa/psa_constant_names.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/psa_constant_names.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
random/gen_entropy$(EXEXT): random/gen_entropy.c $(DEP)
|
||||
echo " CC random/gen_entropy.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) random/gen_entropy.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
|
@ -333,14 +350,20 @@ x509/req_app$(EXEXT): x509/req_app.c $(DEP)
|
|||
echo " CC x509/req_app.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) x509/req_app.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
psa/crypto_examples$(EXEXT): psa/crypto_examples.c $(DEP)
|
||||
echo " CC psa/crypto_examples.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/crypto_examples.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
clean:
|
||||
ifndef WINDOWS
|
||||
rm -f $(APPS)
|
||||
-rm -f ssl/ssl_pthread_server$(EXEXT)
|
||||
rm -f $(EXTRA_GENERATED)
|
||||
-rm -f test/cpp_dummy_build$(EXEXT)
|
||||
else
|
||||
if exist *.o del /Q /F *.o
|
||||
if exist *.exe del /Q /F *.exe
|
||||
if exist $(EXTRA_GENERATED) del /S /Q /F $(EXTRA_GENERATED)
|
||||
endif
|
||||
$(MAKE) -C fuzz clean
|
||||
|
||||
|
|
|
|||
26
programs/psa/CMakeLists.txt
Normal file
26
programs/psa/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
add_executable(crypto_examples crypto_examples.c)
|
||||
target_link_libraries(crypto_examples mbedtls)
|
||||
|
||||
add_executable(key_ladder_demo key_ladder_demo.c)
|
||||
target_link_libraries(key_ladder_demo mbedtls)
|
||||
|
||||
add_executable(psa_constant_names psa_constant_names.c)
|
||||
target_link_libraries(psa_constant_names mbedtls)
|
||||
|
||||
add_custom_target(
|
||||
psa_constant_names_generated
|
||||
COMMAND ${PYTHON_EXECUTABLE} scripts/generate_psa_constants.py
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../
|
||||
)
|
||||
add_dependencies(psa_constant_names psa_constant_names_generated)
|
||||
|
||||
install(TARGETS
|
||||
crypto_examples
|
||||
key_ladder_demo
|
||||
psa_constant_names
|
||||
DESTINATION "bin"
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
||||
|
||||
install(PROGRAMS
|
||||
key_ladder_demo.sh
|
||||
DESTINATION "bin")
|
||||
324
programs/psa/crypto_examples.c
Normal file
324
programs/psa/crypto_examples.c
Normal file
|
|
@ -0,0 +1,324 @@
|
|||
#include "psa/crypto.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ASSERT( predicate ) \
|
||||
do \
|
||||
{ \
|
||||
if( ! ( predicate ) ) \
|
||||
{ \
|
||||
printf( "\tassertion failed at %s:%d - '%s'\r\n", \
|
||||
__FILE__, __LINE__, #predicate); \
|
||||
goto exit; \
|
||||
} \
|
||||
} while ( 0 )
|
||||
|
||||
#define ASSERT_STATUS( actual, expected ) \
|
||||
do \
|
||||
{ \
|
||||
if( ( actual ) != ( expected ) ) \
|
||||
{ \
|
||||
printf( "\tassertion failed at %s:%d - " \
|
||||
"actual:%d expected:%d\r\n", __FILE__, __LINE__, \
|
||||
(psa_status_t) actual, (psa_status_t) expected ); \
|
||||
goto exit; \
|
||||
} \
|
||||
} while ( 0 )
|
||||
|
||||
#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_AES_C) || \
|
||||
!defined(MBEDTLS_CIPHER_MODE_CBC) || !defined(MBEDTLS_CIPHER_MODE_CTR) || \
|
||||
!defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
||||
int main( void )
|
||||
{
|
||||
printf( "MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_AES_C and/or "
|
||||
"MBEDTLS_CIPHER_MODE_CBC and/or MBEDTLS_CIPHER_MODE_CTR "
|
||||
"and/or MBEDTLS_CIPHER_MODE_WITH_PADDING "
|
||||
"not defined.\r\n" );
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
static psa_status_t cipher_operation( psa_cipher_operation_t *operation,
|
||||
const uint8_t * input,
|
||||
size_t input_size,
|
||||
size_t part_size,
|
||||
uint8_t * output,
|
||||
size_t output_size,
|
||||
size_t *output_len )
|
||||
{
|
||||
psa_status_t status;
|
||||
size_t bytes_to_write = 0, bytes_written = 0, len = 0;
|
||||
|
||||
*output_len = 0;
|
||||
while( bytes_written != input_size )
|
||||
{
|
||||
bytes_to_write = ( input_size - bytes_written > part_size ?
|
||||
part_size :
|
||||
input_size - bytes_written );
|
||||
|
||||
status = psa_cipher_update( operation, input + bytes_written,
|
||||
bytes_to_write, output + *output_len,
|
||||
output_size - *output_len, &len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
bytes_written += bytes_to_write;
|
||||
*output_len += len;
|
||||
}
|
||||
|
||||
status = psa_cipher_finish( operation, output + *output_len,
|
||||
output_size - *output_len, &len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
*output_len += len;
|
||||
|
||||
exit:
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t cipher_encrypt( psa_key_handle_t key_handle,
|
||||
psa_algorithm_t alg,
|
||||
uint8_t * iv,
|
||||
size_t iv_size,
|
||||
const uint8_t * input,
|
||||
size_t input_size,
|
||||
size_t part_size,
|
||||
uint8_t * output,
|
||||
size_t output_size,
|
||||
size_t *output_len )
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
||||
size_t iv_len = 0;
|
||||
|
||||
memset( &operation, 0, sizeof( operation ) );
|
||||
status = psa_cipher_encrypt_setup( &operation, key_handle, alg );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = psa_cipher_generate_iv( &operation, iv, iv_size, &iv_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_operation( &operation, input, input_size, part_size,
|
||||
output, output_size, output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
exit:
|
||||
psa_cipher_abort( &operation );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t cipher_decrypt( psa_key_handle_t key_handle,
|
||||
psa_algorithm_t alg,
|
||||
const uint8_t * iv,
|
||||
size_t iv_size,
|
||||
const uint8_t * input,
|
||||
size_t input_size,
|
||||
size_t part_size,
|
||||
uint8_t * output,
|
||||
size_t output_size,
|
||||
size_t *output_len )
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
||||
|
||||
memset( &operation, 0, sizeof( operation ) );
|
||||
status = psa_cipher_decrypt_setup( &operation, key_handle, alg );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = psa_cipher_set_iv( &operation, iv, iv_size );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_operation( &operation, input, input_size, part_size,
|
||||
output, output_size, output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
exit:
|
||||
psa_cipher_abort( &operation );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t
|
||||
cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block( void )
|
||||
{
|
||||
enum {
|
||||
block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( PSA_KEY_TYPE_AES ),
|
||||
key_bits = 256,
|
||||
part_size = block_size,
|
||||
};
|
||||
const psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
|
||||
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_handle_t key_handle = 0;
|
||||
size_t output_len = 0;
|
||||
uint8_t iv[block_size];
|
||||
uint8_t input[block_size];
|
||||
uint8_t encrypt[block_size];
|
||||
uint8_t decrypt[block_size];
|
||||
|
||||
status = psa_generate_random( input, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
psa_set_key_usage_flags( &attributes,
|
||||
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
||||
psa_set_key_algorithm( &attributes, alg );
|
||||
psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
|
||||
psa_set_key_bits( &attributes, key_bits );
|
||||
|
||||
status = psa_generate_key( &attributes, &key_handle );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_encrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
input, sizeof( input ), part_size,
|
||||
encrypt, sizeof( encrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_decrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
encrypt, output_len, part_size,
|
||||
decrypt, sizeof( decrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = memcmp( input, decrypt, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
exit:
|
||||
psa_destroy_key( key_handle );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi( void )
|
||||
{
|
||||
enum {
|
||||
block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( PSA_KEY_TYPE_AES ),
|
||||
key_bits = 256,
|
||||
input_size = 100,
|
||||
part_size = 10,
|
||||
};
|
||||
|
||||
const psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
|
||||
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_handle_t key_handle = 0;
|
||||
size_t output_len = 0;
|
||||
uint8_t iv[block_size], input[input_size],
|
||||
encrypt[input_size + block_size], decrypt[input_size + block_size];
|
||||
|
||||
status = psa_generate_random( input, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
psa_set_key_usage_flags( &attributes,
|
||||
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
||||
psa_set_key_algorithm( &attributes, alg );
|
||||
psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
|
||||
psa_set_key_bits( &attributes, key_bits );
|
||||
|
||||
status = psa_generate_key( &attributes, &key_handle );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_encrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
input, sizeof( input ), part_size,
|
||||
encrypt, sizeof( encrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_decrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
encrypt, output_len, part_size,
|
||||
decrypt, sizeof( decrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = memcmp( input, decrypt, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
exit:
|
||||
psa_destroy_key( key_handle );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi( void )
|
||||
{
|
||||
enum {
|
||||
block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( PSA_KEY_TYPE_AES ),
|
||||
key_bits = 256,
|
||||
input_size = 100,
|
||||
part_size = 10,
|
||||
};
|
||||
const psa_algorithm_t alg = PSA_ALG_CTR;
|
||||
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_handle_t key_handle = 0;
|
||||
size_t output_len = 0;
|
||||
uint8_t iv[block_size], input[input_size], encrypt[input_size],
|
||||
decrypt[input_size];
|
||||
|
||||
status = psa_generate_random( input, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
psa_set_key_usage_flags( &attributes,
|
||||
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
||||
psa_set_key_algorithm( &attributes, alg );
|
||||
psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
|
||||
psa_set_key_bits( &attributes, key_bits );
|
||||
|
||||
status = psa_generate_key( &attributes, &key_handle );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_encrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
input, sizeof( input ), part_size,
|
||||
encrypt, sizeof( encrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_decrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
encrypt, output_len, part_size,
|
||||
decrypt, sizeof( decrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = memcmp( input, decrypt, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
exit:
|
||||
psa_destroy_key( key_handle );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static void cipher_examples( void )
|
||||
{
|
||||
psa_status_t status;
|
||||
|
||||
printf( "cipher encrypt/decrypt AES CBC no padding:\r\n" );
|
||||
status = cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block( );
|
||||
if( status == PSA_SUCCESS )
|
||||
printf( "\tsuccess!\r\n" );
|
||||
|
||||
printf( "cipher encrypt/decrypt AES CBC PKCS7 multipart:\r\n" );
|
||||
status = cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi( );
|
||||
if( status == PSA_SUCCESS )
|
||||
printf( "\tsuccess!\r\n" );
|
||||
|
||||
printf( "cipher encrypt/decrypt AES CTR multipart:\r\n" );
|
||||
status = cipher_example_encrypt_decrypt_aes_ctr_multi( );
|
||||
if( status == PSA_SUCCESS )
|
||||
printf( "\tsuccess!\r\n" );
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
exit( EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( void )
|
||||
{
|
||||
ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
||||
cipher_examples( );
|
||||
exit:
|
||||
mbedtls_psa_crypto_free( );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CBC &&
|
||||
MBEDTLS_CIPHER_MODE_CTR && MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
||||
711
programs/psa/key_ladder_demo.c
Normal file
711
programs/psa/key_ladder_demo.c
Normal file
|
|
@ -0,0 +1,711 @@
|
|||
/**
|
||||
* PSA API key derivation demonstration
|
||||
*
|
||||
* This program calculates a key ladder: a chain of secret material, each
|
||||
* derived from the previous one in a deterministic way based on a label.
|
||||
* Two keys are identical if and only if they are derived from the same key
|
||||
* using the same label.
|
||||
*
|
||||
* The initial key is called the master key. The master key is normally
|
||||
* randomly generated, but it could itself be derived from another key.
|
||||
*
|
||||
* This program derives a series of keys called intermediate keys.
|
||||
* The first intermediate key is derived from the master key using the
|
||||
* first label passed on the command line. Each subsequent intermediate
|
||||
* key is derived from the previous one using the next label passed
|
||||
* on the command line.
|
||||
*
|
||||
* This program has four modes of operation:
|
||||
*
|
||||
* - "generate": generate a random master key.
|
||||
* - "wrap": derive a wrapping key from the last intermediate key,
|
||||
* and use that key to encrypt-and-authenticate some data.
|
||||
* - "unwrap": derive a wrapping key from the last intermediate key,
|
||||
* and use that key to decrypt-and-authenticate some
|
||||
* ciphertext created by wrap mode.
|
||||
* - "save": save the last intermediate key so that it can be reused as
|
||||
* the master key in another run of the program.
|
||||
*
|
||||
* See the usage() output for the command line usage. See the file
|
||||
* `key_ladder_demo.sh` for an example run.
|
||||
*/
|
||||
|
||||
/* Copyright (C) 2018, ARM Limited, All Rights Reserved
|
||||
* 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.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/* First include Mbed TLS headers to get the Mbed TLS configuration and
|
||||
* platform definitions that we'll use in this program. Also include
|
||||
* standard C headers for functions we'll use here. */
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
|
||||
|
||||
#include <psa/crypto.h>
|
||||
|
||||
/* If the build options we need are not enabled, compile a placeholder. */
|
||||
#if !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
!defined(MBEDTLS_AES_C) || !defined(MBEDTLS_CCM_C) || \
|
||||
!defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
printf("MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
|
||||
"MBEDTLS_AES_C and/or MBEDTLS_CCM_C and/or "
|
||||
"MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_FS_IO "
|
||||
"not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
/* The real program starts here. */
|
||||
|
||||
/* Run a system function and bail out if it fails. */
|
||||
#define SYS_CHECK( expr ) \
|
||||
do \
|
||||
{ \
|
||||
if( ! ( expr ) ) \
|
||||
{ \
|
||||
perror( #expr ); \
|
||||
status = DEMO_ERROR; \
|
||||
goto exit; \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
/* Run a PSA function and bail out if it fails. */
|
||||
#define PSA_CHECK( expr ) \
|
||||
do \
|
||||
{ \
|
||||
status = ( expr ); \
|
||||
if( status != PSA_SUCCESS ) \
|
||||
{ \
|
||||
printf( "Error %d at line %u: %s\n", \
|
||||
(int) status, \
|
||||
__LINE__, \
|
||||
#expr ); \
|
||||
goto exit; \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
/* To report operational errors in this program, use an error code that is
|
||||
* different from every PSA error code. */
|
||||
#define DEMO_ERROR 120
|
||||
|
||||
/* The maximum supported key ladder depth. */
|
||||
#define MAX_LADDER_DEPTH 10
|
||||
|
||||
/* Salt to use when deriving an intermediate key. */
|
||||
#define DERIVE_KEY_SALT ( (uint8_t *) "key_ladder_demo.derive" )
|
||||
#define DERIVE_KEY_SALT_LENGTH ( strlen( (const char*) DERIVE_KEY_SALT ) )
|
||||
|
||||
/* Salt to use when deriving a wrapping key. */
|
||||
#define WRAPPING_KEY_SALT ( (uint8_t *) "key_ladder_demo.wrap" )
|
||||
#define WRAPPING_KEY_SALT_LENGTH ( strlen( (const char*) WRAPPING_KEY_SALT ) )
|
||||
|
||||
/* Size of the key derivation keys (applies both to the master key and
|
||||
* to intermediate keys). */
|
||||
#define KEY_SIZE_BYTES 40
|
||||
|
||||
/* Algorithm for key derivation. */
|
||||
#define KDF_ALG PSA_ALG_HKDF( PSA_ALG_SHA_256 )
|
||||
|
||||
/* Type and size of the key used to wrap data. */
|
||||
#define WRAPPING_KEY_TYPE PSA_KEY_TYPE_AES
|
||||
#define WRAPPING_KEY_BITS 128
|
||||
|
||||
/* Cipher mode used to wrap data. */
|
||||
#define WRAPPING_ALG PSA_ALG_CCM
|
||||
|
||||
/* Nonce size used to wrap data. */
|
||||
#define WRAPPING_IV_SIZE 13
|
||||
|
||||
/* Header used in files containing wrapped data. We'll save this header
|
||||
* directly without worrying about data representation issues such as
|
||||
* integer sizes and endianness, because the data is meant to be read
|
||||
* back by the same program on the same machine. */
|
||||
#define WRAPPED_DATA_MAGIC "key_ladder_demo" // including trailing null byte
|
||||
#define WRAPPED_DATA_MAGIC_LENGTH ( sizeof( WRAPPED_DATA_MAGIC ) )
|
||||
typedef struct
|
||||
{
|
||||
char magic[WRAPPED_DATA_MAGIC_LENGTH];
|
||||
size_t ad_size; /* Size of the additional data, which is this header. */
|
||||
size_t payload_size; /* Size of the encrypted data. */
|
||||
/* Store the IV inside the additional data. It's convenient. */
|
||||
uint8_t iv[WRAPPING_IV_SIZE];
|
||||
} wrapped_data_header_t;
|
||||
|
||||
/* The modes that this program can operate in (see usage). */
|
||||
enum program_mode
|
||||
{
|
||||
MODE_GENERATE,
|
||||
MODE_SAVE,
|
||||
MODE_UNWRAP,
|
||||
MODE_WRAP
|
||||
};
|
||||
|
||||
/* Save a key to a file. In the real world, you may want to export a derived
|
||||
* key sometimes, to share it with another party. */
|
||||
static psa_status_t save_key( psa_key_handle_t key_handle,
|
||||
const char *output_file_name )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
uint8_t key_data[KEY_SIZE_BYTES];
|
||||
size_t key_size;
|
||||
FILE *key_file = NULL;
|
||||
|
||||
PSA_CHECK( psa_export_key( key_handle,
|
||||
key_data, sizeof( key_data ),
|
||||
&key_size ) );
|
||||
SYS_CHECK( ( key_file = fopen( output_file_name, "wb" ) ) != NULL );
|
||||
SYS_CHECK( fwrite( key_data, 1, key_size, key_file ) == key_size );
|
||||
SYS_CHECK( fclose( key_file ) == 0 );
|
||||
key_file = NULL;
|
||||
|
||||
exit:
|
||||
if( key_file != NULL)
|
||||
fclose( key_file );
|
||||
return( status );
|
||||
}
|
||||
|
||||
/* Generate a master key for use in this demo.
|
||||
*
|
||||
* Normally a master key would be non-exportable. For the purpose of this
|
||||
* demo, we want to save it to a file, to avoid relying on the keystore
|
||||
* capability of the PSA crypto library. */
|
||||
static psa_status_t generate( const char *key_file_name )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_handle_t key_handle = 0;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
|
||||
psa_set_key_usage_flags( &attributes,
|
||||
PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
|
||||
psa_set_key_algorithm( &attributes, KDF_ALG );
|
||||
psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
|
||||
psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( KEY_SIZE_BYTES ) );
|
||||
|
||||
PSA_CHECK( psa_generate_key( &attributes, &key_handle ) );
|
||||
|
||||
PSA_CHECK( save_key( key_handle, key_file_name ) );
|
||||
|
||||
exit:
|
||||
(void) psa_destroy_key( key_handle );
|
||||
return( status );
|
||||
}
|
||||
|
||||
/* Load the master key from a file.
|
||||
*
|
||||
* In the real world, this master key would be stored in an internal memory
|
||||
* and the storage would be managed by the keystore capability of the PSA
|
||||
* crypto library. */
|
||||
static psa_status_t import_key_from_file( psa_key_usage_t usage,
|
||||
psa_algorithm_t alg,
|
||||
const char *key_file_name,
|
||||
psa_key_handle_t *master_key_handle )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
uint8_t key_data[KEY_SIZE_BYTES];
|
||||
size_t key_size;
|
||||
FILE *key_file = NULL;
|
||||
unsigned char extra_byte;
|
||||
|
||||
*master_key_handle = 0;
|
||||
|
||||
SYS_CHECK( ( key_file = fopen( key_file_name, "rb" ) ) != NULL );
|
||||
SYS_CHECK( ( key_size = fread( key_data, 1, sizeof( key_data ),
|
||||
key_file ) ) != 0 );
|
||||
if( fread( &extra_byte, 1, 1, key_file ) != 0 )
|
||||
{
|
||||
printf( "Key file too large (max: %u).\n",
|
||||
(unsigned) sizeof( key_data ) );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
SYS_CHECK( fclose( key_file ) == 0 );
|
||||
key_file = NULL;
|
||||
|
||||
psa_set_key_usage_flags( &attributes, usage );
|
||||
psa_set_key_algorithm( &attributes, alg );
|
||||
psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
|
||||
PSA_CHECK( psa_import_key( &attributes, key_data, key_size,
|
||||
master_key_handle ) );
|
||||
exit:
|
||||
if( key_file != NULL )
|
||||
fclose( key_file );
|
||||
mbedtls_platform_zeroize( key_data, sizeof( key_data ) );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
/* If the key creation hasn't happened yet or has failed,
|
||||
* *master_key_handle is 0. psa_destroy_key(0) is guaranteed to do
|
||||
* nothing and return PSA_ERROR_INVALID_HANDLE. */
|
||||
(void) psa_destroy_key( *master_key_handle );
|
||||
*master_key_handle = 0;
|
||||
}
|
||||
return( status );
|
||||
}
|
||||
|
||||
/* Derive the intermediate keys, using the list of labels provided on
|
||||
* the command line. On input, *key_handle is a handle to the master key.
|
||||
* This function closes the master key. On successful output, *key_handle
|
||||
* is a handle to the final derived key. */
|
||||
static psa_status_t derive_key_ladder( const char *ladder[],
|
||||
size_t ladder_depth,
|
||||
psa_key_handle_t *key_handle )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
||||
size_t i;
|
||||
|
||||
psa_set_key_usage_flags( &attributes,
|
||||
PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
|
||||
psa_set_key_algorithm( &attributes, KDF_ALG );
|
||||
psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
|
||||
psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( KEY_SIZE_BYTES ) );
|
||||
|
||||
/* For each label in turn, ... */
|
||||
for( i = 0; i < ladder_depth; i++ )
|
||||
{
|
||||
/* Start deriving material from the master key (if i=0) or from
|
||||
* the current intermediate key (if i>0). */
|
||||
PSA_CHECK( psa_key_derivation_setup( &operation, KDF_ALG ) );
|
||||
PSA_CHECK( psa_key_derivation_input_bytes(
|
||||
&operation, PSA_KEY_DERIVATION_INPUT_SALT,
|
||||
DERIVE_KEY_SALT, DERIVE_KEY_SALT_LENGTH ) );
|
||||
PSA_CHECK( psa_key_derivation_input_key(
|
||||
&operation, PSA_KEY_DERIVATION_INPUT_SECRET,
|
||||
*key_handle ) );
|
||||
PSA_CHECK( psa_key_derivation_input_bytes(
|
||||
&operation, PSA_KEY_DERIVATION_INPUT_INFO,
|
||||
(uint8_t*) ladder[i], strlen( ladder[i] ) ) );
|
||||
/* When the parent key is not the master key, destroy it,
|
||||
* since it is no longer needed. */
|
||||
PSA_CHECK( psa_close_key( *key_handle ) );
|
||||
*key_handle = 0;
|
||||
/* Derive the next intermediate key from the parent key. */
|
||||
PSA_CHECK( psa_key_derivation_output_key( &attributes, &operation,
|
||||
key_handle ) );
|
||||
PSA_CHECK( psa_key_derivation_abort( &operation ) );
|
||||
}
|
||||
|
||||
exit:
|
||||
psa_key_derivation_abort( &operation );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
psa_close_key( *key_handle );
|
||||
*key_handle = 0;
|
||||
}
|
||||
return( status );
|
||||
}
|
||||
|
||||
/* Derive a wrapping key from the last intermediate key. */
|
||||
static psa_status_t derive_wrapping_key( psa_key_usage_t usage,
|
||||
psa_key_handle_t derived_key_handle,
|
||||
psa_key_handle_t *wrapping_key_handle )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
||||
|
||||
*wrapping_key_handle = 0;
|
||||
|
||||
/* Set up a key derivation operation from the key derived from
|
||||
* the master key. */
|
||||
PSA_CHECK( psa_key_derivation_setup( &operation, KDF_ALG ) );
|
||||
PSA_CHECK( psa_key_derivation_input_bytes(
|
||||
&operation, PSA_KEY_DERIVATION_INPUT_SALT,
|
||||
WRAPPING_KEY_SALT, WRAPPING_KEY_SALT_LENGTH ) );
|
||||
PSA_CHECK( psa_key_derivation_input_key(
|
||||
&operation, PSA_KEY_DERIVATION_INPUT_SECRET,
|
||||
derived_key_handle ) );
|
||||
PSA_CHECK( psa_key_derivation_input_bytes(
|
||||
&operation, PSA_KEY_DERIVATION_INPUT_INFO,
|
||||
NULL, 0 ) );
|
||||
|
||||
/* Create the wrapping key. */
|
||||
psa_set_key_usage_flags( &attributes, usage );
|
||||
psa_set_key_algorithm( &attributes, WRAPPING_ALG );
|
||||
psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
|
||||
psa_set_key_bits( &attributes, WRAPPING_KEY_BITS );
|
||||
PSA_CHECK( psa_key_derivation_output_key( &attributes, &operation,
|
||||
wrapping_key_handle ) );
|
||||
|
||||
exit:
|
||||
psa_key_derivation_abort( &operation );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t wrap_data( const char *input_file_name,
|
||||
const char *output_file_name,
|
||||
psa_key_handle_t wrapping_key_handle )
|
||||
{
|
||||
psa_status_t status;
|
||||
FILE *input_file = NULL;
|
||||
FILE *output_file = NULL;
|
||||
long input_position;
|
||||
size_t input_size;
|
||||
size_t buffer_size = 0;
|
||||
unsigned char *buffer = NULL;
|
||||
size_t ciphertext_size;
|
||||
wrapped_data_header_t header;
|
||||
|
||||
/* Find the size of the data to wrap. */
|
||||
SYS_CHECK( ( input_file = fopen( input_file_name, "rb" ) ) != NULL );
|
||||
SYS_CHECK( fseek( input_file, 0, SEEK_END ) == 0 );
|
||||
SYS_CHECK( ( input_position = ftell( input_file ) ) != -1 );
|
||||
#if LONG_MAX > SIZE_MAX
|
||||
if( input_position > SIZE_MAX )
|
||||
{
|
||||
printf( "Input file too large.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
input_size = input_position;
|
||||
buffer_size = PSA_AEAD_ENCRYPT_OUTPUT_SIZE( WRAPPING_ALG, input_size );
|
||||
/* Check for integer overflow. */
|
||||
if( buffer_size < input_size )
|
||||
{
|
||||
printf( "Input file too large.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Load the data to wrap. */
|
||||
SYS_CHECK( fseek( input_file, 0, SEEK_SET ) == 0 );
|
||||
SYS_CHECK( ( buffer = calloc( 1, buffer_size ) ) != NULL );
|
||||
SYS_CHECK( fread( buffer, 1, input_size, input_file ) == input_size );
|
||||
SYS_CHECK( fclose( input_file ) == 0 );
|
||||
input_file = NULL;
|
||||
|
||||
/* Construct a header. */
|
||||
memcpy( &header.magic, WRAPPED_DATA_MAGIC, WRAPPED_DATA_MAGIC_LENGTH );
|
||||
header.ad_size = sizeof( header );
|
||||
header.payload_size = input_size;
|
||||
|
||||
/* Wrap the data. */
|
||||
PSA_CHECK( psa_generate_random( header.iv, WRAPPING_IV_SIZE ) );
|
||||
PSA_CHECK( psa_aead_encrypt( wrapping_key_handle, WRAPPING_ALG,
|
||||
header.iv, WRAPPING_IV_SIZE,
|
||||
(uint8_t *) &header, sizeof( header ),
|
||||
buffer, input_size,
|
||||
buffer, buffer_size,
|
||||
&ciphertext_size ) );
|
||||
|
||||
/* Write the output. */
|
||||
SYS_CHECK( ( output_file = fopen( output_file_name, "wb" ) ) != NULL );
|
||||
SYS_CHECK( fwrite( &header, 1, sizeof( header ),
|
||||
output_file ) == sizeof( header ) );
|
||||
SYS_CHECK( fwrite( buffer, 1, ciphertext_size,
|
||||
output_file ) == ciphertext_size );
|
||||
SYS_CHECK( fclose( output_file ) == 0 );
|
||||
output_file = NULL;
|
||||
|
||||
exit:
|
||||
if( input_file != NULL )
|
||||
fclose( input_file );
|
||||
if( output_file != NULL )
|
||||
fclose( output_file );
|
||||
if( buffer != NULL )
|
||||
mbedtls_platform_zeroize( buffer, buffer_size );
|
||||
free( buffer );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t unwrap_data( const char *input_file_name,
|
||||
const char *output_file_name,
|
||||
psa_key_handle_t wrapping_key_handle )
|
||||
{
|
||||
psa_status_t status;
|
||||
FILE *input_file = NULL;
|
||||
FILE *output_file = NULL;
|
||||
unsigned char *buffer = NULL;
|
||||
size_t ciphertext_size = 0;
|
||||
size_t plaintext_size;
|
||||
wrapped_data_header_t header;
|
||||
unsigned char extra_byte;
|
||||
|
||||
/* Load and validate the header. */
|
||||
SYS_CHECK( ( input_file = fopen( input_file_name, "rb" ) ) != NULL );
|
||||
SYS_CHECK( fread( &header, 1, sizeof( header ),
|
||||
input_file ) == sizeof( header ) );
|
||||
if( memcmp( &header.magic, WRAPPED_DATA_MAGIC,
|
||||
WRAPPED_DATA_MAGIC_LENGTH ) != 0 )
|
||||
{
|
||||
printf( "The input does not start with a valid magic header.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
if( header.ad_size != sizeof( header ) )
|
||||
{
|
||||
printf( "The header size is not correct.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
ciphertext_size =
|
||||
PSA_AEAD_ENCRYPT_OUTPUT_SIZE( WRAPPING_ALG, header.payload_size );
|
||||
/* Check for integer overflow. */
|
||||
if( ciphertext_size < header.payload_size )
|
||||
{
|
||||
printf( "Input file too large.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Load the payload data. */
|
||||
SYS_CHECK( ( buffer = calloc( 1, ciphertext_size ) ) != NULL );
|
||||
SYS_CHECK( fread( buffer, 1, ciphertext_size,
|
||||
input_file ) == ciphertext_size );
|
||||
if( fread( &extra_byte, 1, 1, input_file ) != 0 )
|
||||
{
|
||||
printf( "Extra garbage after ciphertext\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
SYS_CHECK( fclose( input_file ) == 0 );
|
||||
input_file = NULL;
|
||||
|
||||
/* Unwrap the data. */
|
||||
PSA_CHECK( psa_aead_decrypt( wrapping_key_handle, WRAPPING_ALG,
|
||||
header.iv, WRAPPING_IV_SIZE,
|
||||
(uint8_t *) &header, sizeof( header ),
|
||||
buffer, ciphertext_size,
|
||||
buffer, ciphertext_size,
|
||||
&plaintext_size ) );
|
||||
if( plaintext_size != header.payload_size )
|
||||
{
|
||||
printf( "Incorrect payload size in the header.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Write the output. */
|
||||
SYS_CHECK( ( output_file = fopen( output_file_name, "wb" ) ) != NULL );
|
||||
SYS_CHECK( fwrite( buffer, 1, plaintext_size,
|
||||
output_file ) == plaintext_size );
|
||||
SYS_CHECK( fclose( output_file ) == 0 );
|
||||
output_file = NULL;
|
||||
|
||||
exit:
|
||||
if( input_file != NULL )
|
||||
fclose( input_file );
|
||||
if( output_file != NULL )
|
||||
fclose( output_file );
|
||||
if( buffer != NULL )
|
||||
mbedtls_platform_zeroize( buffer, ciphertext_size );
|
||||
free( buffer );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t run( enum program_mode mode,
|
||||
const char *key_file_name,
|
||||
const char *ladder[], size_t ladder_depth,
|
||||
const char *input_file_name,
|
||||
const char *output_file_name )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_handle_t derivation_key_handle = 0;
|
||||
psa_key_handle_t wrapping_key_handle = 0;
|
||||
|
||||
/* Initialize the PSA crypto library. */
|
||||
PSA_CHECK( psa_crypto_init( ) );
|
||||
|
||||
/* Generate mode is unlike the others. Generate the master key and exit. */
|
||||
if( mode == MODE_GENERATE )
|
||||
return( generate( key_file_name ) );
|
||||
|
||||
/* Read the master key. */
|
||||
PSA_CHECK( import_key_from_file( PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT,
|
||||
KDF_ALG,
|
||||
key_file_name,
|
||||
&derivation_key_handle ) );
|
||||
|
||||
/* Calculate the derived key for this session. */
|
||||
PSA_CHECK( derive_key_ladder( ladder, ladder_depth,
|
||||
&derivation_key_handle ) );
|
||||
|
||||
switch( mode )
|
||||
{
|
||||
case MODE_SAVE:
|
||||
PSA_CHECK( save_key( derivation_key_handle, output_file_name ) );
|
||||
break;
|
||||
case MODE_UNWRAP:
|
||||
PSA_CHECK( derive_wrapping_key( PSA_KEY_USAGE_DECRYPT,
|
||||
derivation_key_handle,
|
||||
&wrapping_key_handle ) );
|
||||
PSA_CHECK( unwrap_data( input_file_name, output_file_name,
|
||||
wrapping_key_handle ) );
|
||||
break;
|
||||
case MODE_WRAP:
|
||||
PSA_CHECK( derive_wrapping_key( PSA_KEY_USAGE_ENCRYPT,
|
||||
derivation_key_handle,
|
||||
&wrapping_key_handle ) );
|
||||
PSA_CHECK( wrap_data( input_file_name, output_file_name,
|
||||
wrapping_key_handle ) );
|
||||
break;
|
||||
default:
|
||||
/* Unreachable but some compilers don't realize it. */
|
||||
break;
|
||||
}
|
||||
|
||||
exit:
|
||||
/* Close any remaining key. Deinitializing the crypto library would do
|
||||
* this anyway, but explicitly closing handles makes the code easier
|
||||
* to reuse. */
|
||||
(void) psa_close_key( derivation_key_handle );
|
||||
(void) psa_close_key( wrapping_key_handle );
|
||||
/* Deinitialize the PSA crypto library. */
|
||||
mbedtls_psa_crypto_free( );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static void usage( void )
|
||||
{
|
||||
printf( "Usage: key_ladder_demo MODE [OPTION=VALUE]...\n" );
|
||||
printf( "Demonstrate the usage of a key derivation ladder.\n" );
|
||||
printf( "\n" );
|
||||
printf( "Modes:\n" );
|
||||
printf( " generate Generate the master key\n" );
|
||||
printf( " save Save the derived key\n" );
|
||||
printf( " unwrap Unwrap (decrypt) input with the derived key\n" );
|
||||
printf( " wrap Wrap (encrypt) input with the derived key\n" );
|
||||
printf( "\n" );
|
||||
printf( "Options:\n" );
|
||||
printf( " input=FILENAME Input file (required for wrap/unwrap)\n" );
|
||||
printf( " master=FILENAME File containing the master key (default: master.key)\n" );
|
||||
printf( " output=FILENAME Output file (required for save/wrap/unwrap)\n" );
|
||||
printf( " label=TEXT Label for the key derivation.\n" );
|
||||
printf( " This may be repeated multiple times.\n" );
|
||||
printf( " To get the same key, you must use the same master key\n" );
|
||||
printf( " and the same sequence of labels.\n" );
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
exit( EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
const char *key_file_name = "master.key";
|
||||
const char *input_file_name = NULL;
|
||||
const char *output_file_name = NULL;
|
||||
const char *ladder[MAX_LADDER_DEPTH];
|
||||
size_t ladder_depth = 0;
|
||||
int i;
|
||||
enum program_mode mode;
|
||||
psa_status_t status;
|
||||
|
||||
if( argc <= 1 ||
|
||||
strcmp( argv[1], "help" ) == 0 ||
|
||||
strcmp( argv[1], "-help" ) == 0 ||
|
||||
strcmp( argv[1], "--help" ) == 0 )
|
||||
{
|
||||
usage( );
|
||||
return( EXIT_SUCCESS );
|
||||
}
|
||||
|
||||
for( i = 2; i < argc; i++ )
|
||||
{
|
||||
char *q = strchr( argv[i], '=' );
|
||||
if( q == NULL )
|
||||
{
|
||||
printf( "Missing argument to option %s\n", argv[i] );
|
||||
goto usage_failure;
|
||||
}
|
||||
*q = 0;
|
||||
++q;
|
||||
if( strcmp( argv[i], "input" ) == 0 )
|
||||
input_file_name = q;
|
||||
else if( strcmp( argv[i], "label" ) == 0 )
|
||||
{
|
||||
if( ladder_depth == MAX_LADDER_DEPTH )
|
||||
{
|
||||
printf( "Maximum ladder depth %u exceeded.\n",
|
||||
(unsigned) MAX_LADDER_DEPTH );
|
||||
return( EXIT_FAILURE );
|
||||
}
|
||||
ladder[ladder_depth] = q;
|
||||
++ladder_depth;
|
||||
}
|
||||
else if( strcmp( argv[i], "master" ) == 0 )
|
||||
key_file_name = q;
|
||||
else if( strcmp( argv[i], "output" ) == 0 )
|
||||
output_file_name = q;
|
||||
else
|
||||
{
|
||||
printf( "Unknown option: %s\n", argv[i] );
|
||||
goto usage_failure;
|
||||
}
|
||||
}
|
||||
|
||||
if( strcmp( argv[1], "generate" ) == 0 )
|
||||
mode = MODE_GENERATE;
|
||||
else if( strcmp( argv[1], "save" ) == 0 )
|
||||
mode = MODE_SAVE;
|
||||
else if( strcmp( argv[1], "unwrap" ) == 0 )
|
||||
mode = MODE_UNWRAP;
|
||||
else if( strcmp( argv[1], "wrap" ) == 0 )
|
||||
mode = MODE_WRAP;
|
||||
else
|
||||
{
|
||||
printf( "Unknown action: %s\n", argv[1] );
|
||||
goto usage_failure;
|
||||
}
|
||||
|
||||
if( input_file_name == NULL &&
|
||||
( mode == MODE_WRAP || mode == MODE_UNWRAP ) )
|
||||
{
|
||||
printf( "Required argument missing: input\n" );
|
||||
return( DEMO_ERROR );
|
||||
}
|
||||
if( output_file_name == NULL &&
|
||||
( mode == MODE_SAVE || mode == MODE_WRAP || mode == MODE_UNWRAP ) )
|
||||
{
|
||||
printf( "Required argument missing: output\n" );
|
||||
return( DEMO_ERROR );
|
||||
}
|
||||
|
||||
status = run( mode, key_file_name,
|
||||
ladder, ladder_depth,
|
||||
input_file_name, output_file_name );
|
||||
return( status == PSA_SUCCESS ?
|
||||
EXIT_SUCCESS :
|
||||
EXIT_FAILURE );
|
||||
|
||||
usage_failure:
|
||||
usage( );
|
||||
return( EXIT_FAILURE );
|
||||
}
|
||||
#endif /* MBEDTLS_SHA256_C && MBEDTLS_MD_C && MBEDTLS_AES_C && MBEDTLS_CCM_C && MBEDTLS_PSA_CRYPTO_C && MBEDTLS_FS_IO */
|
||||
49
programs/psa/key_ladder_demo.sh
Executable file
49
programs/psa/key_ladder_demo.sh
Executable file
|
|
@ -0,0 +1,49 @@
|
|||
#!/bin/sh
|
||||
set -e -u
|
||||
|
||||
program="${0%/*}"/key_ladder_demo
|
||||
files_to_clean=
|
||||
|
||||
run () {
|
||||
echo
|
||||
echo "# $1"
|
||||
shift
|
||||
echo "+ $*"
|
||||
"$@"
|
||||
}
|
||||
|
||||
if [ -e master.key ]; then
|
||||
echo "# Reusing the existing master.key file."
|
||||
else
|
||||
files_to_clean="$files_to_clean master.key"
|
||||
run "Generate a master key." \
|
||||
"$program" generate master=master.key
|
||||
fi
|
||||
|
||||
files_to_clean="$files_to_clean input.txt hello_world.wrap"
|
||||
echo "Here is some input. See it wrapped." >input.txt
|
||||
run "Derive a key and wrap some data with it." \
|
||||
"$program" wrap master=master.key label=hello label=world \
|
||||
input=input.txt output=hello_world.wrap
|
||||
|
||||
files_to_clean="$files_to_clean hello_world.txt"
|
||||
run "Derive the same key again and unwrap the data." \
|
||||
"$program" unwrap master=master.key label=hello label=world \
|
||||
input=hello_world.wrap output=hello_world.txt
|
||||
run "Compare the unwrapped data with the original input." \
|
||||
cmp input.txt hello_world.txt
|
||||
|
||||
files_to_clean="$files_to_clean hellow_orld.txt"
|
||||
! run "Derive a different key and attempt to unwrap the data. This must fail." \
|
||||
"$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld
|
||||
|
||||
files_to_clean="$files_to_clean hello.key"
|
||||
run "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \
|
||||
"$program" save master=master.key label=hello \
|
||||
input=hello_world.wrap output=hello.key
|
||||
run "Check that we get the same key by unwrapping data made by the other key." \
|
||||
"$program" unwrap master=hello.key label=world \
|
||||
input=hello_world.wrap output=hello_world.txt
|
||||
|
||||
# Cleanup
|
||||
rm -f $files_to_clean
|
||||
310
programs/psa/psa_constant_names.c
Normal file
310
programs/psa/psa_constant_names.c
Normal file
|
|
@ -0,0 +1,310 @@
|
|||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
/* This block is present to support Visual Studio builds prior to 2015 */
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1900
|
||||
#include <stdarg.h>
|
||||
int snprintf( char *s, size_t n, const char *fmt, ... )
|
||||
{
|
||||
int ret;
|
||||
va_list argp;
|
||||
|
||||
/* Avoid calling the invalid parameter handler by checking ourselves */
|
||||
if( s == NULL || n == 0 || fmt == NULL )
|
||||
return( -1 );
|
||||
|
||||
va_start( argp, fmt );
|
||||
#if defined(_TRUNCATE) && !defined(__MINGW32__)
|
||||
ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
|
||||
#else
|
||||
ret = _vsnprintf( s, n, fmt, argp );
|
||||
if( ret < 0 || (size_t) ret == n )
|
||||
{
|
||||
s[n-1] = '\0';
|
||||
ret = -1;
|
||||
}
|
||||
#endif
|
||||
va_end( argp );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif
|
||||
|
||||
/* There are different GET_HASH macros for different kinds of algorithms
|
||||
* built from hashes, but the values are all constructed on the
|
||||
* same model. */
|
||||
#define PSA_ALG_GET_HASH(alg) \
|
||||
(((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
|
||||
|
||||
static void append(char **buffer, size_t buffer_size,
|
||||
size_t *required_size,
|
||||
const char *string, size_t length)
|
||||
{
|
||||
*required_size += length;
|
||||
if (*required_size < buffer_size) {
|
||||
memcpy(*buffer, string, length);
|
||||
*buffer += length;
|
||||
}
|
||||
}
|
||||
|
||||
static void append_integer(char **buffer, size_t buffer_size,
|
||||
size_t *required_size,
|
||||
const char *format /*printf format for value*/,
|
||||
unsigned long value)
|
||||
{
|
||||
size_t n = snprintf(*buffer, buffer_size - *required_size, format, value);
|
||||
if (n < buffer_size - *required_size) *buffer += n;
|
||||
*required_size += n;
|
||||
}
|
||||
|
||||
/* The code of these function is automatically generated and included below. */
|
||||
static const char *psa_ecc_curve_name(psa_ecc_curve_t curve);
|
||||
static const char *psa_dh_group_name(psa_dh_group_t group);
|
||||
static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
|
||||
|
||||
static void append_with_curve(char **buffer, size_t buffer_size,
|
||||
size_t *required_size,
|
||||
const char *string, size_t length,
|
||||
psa_ecc_curve_t curve)
|
||||
{
|
||||
const char *curve_name = psa_ecc_curve_name(curve);
|
||||
append(buffer, buffer_size, required_size, string, length);
|
||||
append(buffer, buffer_size, required_size, "(", 1);
|
||||
if (curve_name != NULL) {
|
||||
append(buffer, buffer_size, required_size,
|
||||
curve_name, strlen(curve_name));
|
||||
} else {
|
||||
append_integer(buffer, buffer_size, required_size,
|
||||
"0x%02x", curve);
|
||||
}
|
||||
append(buffer, buffer_size, required_size, ")", 1);
|
||||
}
|
||||
|
||||
static void append_with_group(char **buffer, size_t buffer_size,
|
||||
size_t *required_size,
|
||||
const char *string, size_t length,
|
||||
psa_dh_group_t group)
|
||||
{
|
||||
const char *group_name = psa_dh_group_name(group);
|
||||
append(buffer, buffer_size, required_size, string, length);
|
||||
append(buffer, buffer_size, required_size, "(", 1);
|
||||
if (group_name != NULL) {
|
||||
append(buffer, buffer_size, required_size,
|
||||
group_name, strlen(group_name));
|
||||
} else {
|
||||
append_integer(buffer, buffer_size, required_size,
|
||||
"0x%02x", group);
|
||||
}
|
||||
append(buffer, buffer_size, required_size, ")", 1);
|
||||
}
|
||||
|
||||
typedef const char *(*psa_get_algorithm_name_func_ptr)(psa_algorithm_t alg);
|
||||
|
||||
static void append_with_alg(char **buffer, size_t buffer_size,
|
||||
size_t *required_size,
|
||||
psa_get_algorithm_name_func_ptr get_name,
|
||||
psa_algorithm_t alg)
|
||||
{
|
||||
const char *name = get_name(alg);
|
||||
if (name != NULL) {
|
||||
append(buffer, buffer_size, required_size,
|
||||
name, strlen(name));
|
||||
} else {
|
||||
append_integer(buffer, buffer_size, required_size,
|
||||
"0x%08lx", alg);
|
||||
}
|
||||
}
|
||||
|
||||
#include "psa_constant_names_generated.c"
|
||||
|
||||
static int psa_snprint_status(char *buffer, size_t buffer_size,
|
||||
psa_status_t status)
|
||||
{
|
||||
const char *name = psa_strerror(status);
|
||||
if (name == NULL) {
|
||||
return snprintf(buffer, buffer_size, "%ld", (long) status);
|
||||
} else {
|
||||
size_t length = strlen(name);
|
||||
if (length < buffer_size) {
|
||||
memcpy(buffer, name, length + 1);
|
||||
return (int) length;
|
||||
} else {
|
||||
return (int) buffer_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
|
||||
psa_ecc_curve_t curve)
|
||||
{
|
||||
const char *name = psa_ecc_curve_name(curve);
|
||||
if (name == NULL) {
|
||||
return snprintf(buffer, buffer_size, "0x%02x", (unsigned) curve);
|
||||
} else {
|
||||
size_t length = strlen(name);
|
||||
if (length < buffer_size) {
|
||||
memcpy(buffer, name, length + 1);
|
||||
return (int) length;
|
||||
} else {
|
||||
return (int) buffer_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int psa_snprint_dh_group(char *buffer, size_t buffer_size,
|
||||
psa_dh_group_t group)
|
||||
{
|
||||
const char *name = psa_dh_group_name(group);
|
||||
if (name == NULL) {
|
||||
return snprintf(buffer, buffer_size, "0x%02x", (unsigned) group);
|
||||
} else {
|
||||
size_t length = strlen(name);
|
||||
if (length < buffer_size) {
|
||||
memcpy(buffer, name, length + 1);
|
||||
return (int) length;
|
||||
} else {
|
||||
return (int) buffer_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void usage(const char *program_name)
|
||||
{
|
||||
printf("Usage: %s TYPE VALUE [VALUE...]\n",
|
||||
program_name == NULL ? "psa_constant_names" : program_name);
|
||||
printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
|
||||
printf("Supported types (with = between aliases):\n");
|
||||
printf(" alg=algorithm Algorithm (psa_algorithm_t)\n");
|
||||
printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_curve_t)\n");
|
||||
printf(" group=dh_group Diffie-Hellman group identifier (psa_dh_group_t)\n");
|
||||
printf(" type=key_type Key type (psa_key_type_t)\n");
|
||||
printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
|
||||
printf(" error=status Status code (psa_status_t)\n");
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
TYPE_STATUS,
|
||||
} signed_value_type;
|
||||
|
||||
int process_signed(signed_value_type type, long min, long max, char **argp)
|
||||
{
|
||||
for (; *argp != NULL; argp++) {
|
||||
char buffer[200];
|
||||
char *end;
|
||||
long value = strtol(*argp, &end, 0);
|
||||
if (*end) {
|
||||
printf("Non-numeric value: %s\n", *argp);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (value < min || (errno == ERANGE && value < 0)) {
|
||||
printf("Value too small: %s\n", *argp);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (value > max || (errno == ERANGE && value > 0)) {
|
||||
printf("Value too large: %s\n", *argp);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case TYPE_STATUS:
|
||||
psa_snprint_status(buffer, sizeof(buffer),
|
||||
(psa_status_t) value);
|
||||
break;
|
||||
}
|
||||
puts(buffer);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
TYPE_ALGORITHM,
|
||||
TYPE_ECC_CURVE,
|
||||
TYPE_DH_GROUP,
|
||||
TYPE_KEY_TYPE,
|
||||
TYPE_KEY_USAGE,
|
||||
} unsigned_value_type;
|
||||
|
||||
int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
|
||||
{
|
||||
for (; *argp != NULL; argp++) {
|
||||
char buffer[200];
|
||||
char *end;
|
||||
unsigned long value = strtoul(*argp, &end, 0);
|
||||
if (*end) {
|
||||
printf("Non-numeric value: %s\n", *argp);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (value > max || errno == ERANGE) {
|
||||
printf("Value out of range: %s\n", *argp);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case TYPE_ALGORITHM:
|
||||
psa_snprint_algorithm(buffer, sizeof(buffer),
|
||||
(psa_algorithm_t) value);
|
||||
break;
|
||||
case TYPE_ECC_CURVE:
|
||||
psa_snprint_ecc_curve(buffer, sizeof(buffer),
|
||||
(psa_ecc_curve_t) value);
|
||||
break;
|
||||
case TYPE_DH_GROUP:
|
||||
psa_snprint_dh_group(buffer, sizeof(buffer),
|
||||
(psa_dh_group_t) value);
|
||||
break;
|
||||
case TYPE_KEY_TYPE:
|
||||
psa_snprint_key_type(buffer, sizeof(buffer),
|
||||
(psa_key_type_t) value);
|
||||
break;
|
||||
case TYPE_KEY_USAGE:
|
||||
psa_snprint_key_usage(buffer, sizeof(buffer),
|
||||
(psa_key_usage_t) value);
|
||||
break;
|
||||
}
|
||||
puts(buffer);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc <= 1 ||
|
||||
!strcmp(argv[1], "help") ||
|
||||
!strcmp(argv[1], "--help"))
|
||||
{
|
||||
usage(argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
|
||||
/* There's no way to obtain the actual range of a signed type,
|
||||
* so hard-code it here: psa_status_t is int32_t. */
|
||||
return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX,
|
||||
argv + 2);
|
||||
} else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
|
||||
return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1),
|
||||
argv + 2);
|
||||
} else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
|
||||
return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_curve_t) (-1),
|
||||
argv + 2);
|
||||
} else if (!strcmp(argv[1], "group") || !strcmp(argv[1], "dh_group")) {
|
||||
return process_unsigned(TYPE_DH_GROUP, (psa_dh_group_t) (-1),
|
||||
argv + 2);
|
||||
} else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
|
||||
return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1),
|
||||
argv + 2);
|
||||
} else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
|
||||
return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1),
|
||||
argv + 2);
|
||||
} else {
|
||||
printf("Unknown type: %s\n", argv[1]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
|
@ -8,11 +8,9 @@ set(MBEDTLS_DIR ../../../)
|
|||
# Add Mbed TLS as a subdirectory.
|
||||
add_subdirectory(${MBEDTLS_DIR} build)
|
||||
|
||||
# Link against all the Mbed TLS libraries.
|
||||
# Link against the Mbed Crypto library.
|
||||
set(libs
|
||||
mbedtls
|
||||
mbedcrypto
|
||||
mbedx509
|
||||
)
|
||||
|
||||
add_executable(cmake_subproject cmake_subproject.c)
|
||||
|
|
|
|||
|
|
@ -842,6 +842,14 @@ int query_config( const char *config )
|
|||
}
|
||||
#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
|
||||
|
||||
#if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
|
||||
if( strcmp( "MBEDTLS_CTR_DRBG_USE_128_BIT_KEY", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_CTR_DRBG_USE_128_BIT_KEY );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_CTR_DRBG_USE_128_BIT_KEY */
|
||||
|
||||
#if defined(MBEDTLS_ENABLE_WEAK_CIPHERSUITES)
|
||||
if( strcmp( "MBEDTLS_ENABLE_WEAK_CIPHERSUITES", config ) == 0 )
|
||||
{
|
||||
|
|
@ -1154,6 +1162,14 @@ int query_config( const char *config )
|
|||
}
|
||||
#endif /* MBEDTLS_ENTROPY_NV_SEED */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER)
|
||||
if( strcmp( "MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER */
|
||||
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
if( strcmp( "MBEDTLS_MEMORY_DEBUG", config ) == 0 )
|
||||
{
|
||||
|
|
@ -1234,6 +1250,22 @@ int query_config( const char *config )
|
|||
}
|
||||
#endif /* MBEDTLS_SHA256_SMALLER */
|
||||
|
||||
#if defined(MBEDTLS_SHA512_SMALLER)
|
||||
if( strcmp( "MBEDTLS_SHA512_SMALLER", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_SHA512_SMALLER );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_SHA512_SMALLER */
|
||||
|
||||
#if defined(MBEDTLS_SHA512_NO_SHA384)
|
||||
if( strcmp( "MBEDTLS_SHA512_NO_SHA384", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_SHA512_NO_SHA384 );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_SHA512_NO_SHA384 */
|
||||
|
||||
#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
|
||||
if( strcmp( "MBEDTLS_SSL_ALL_ALERT_MESSAGES", config ) == 0 )
|
||||
{
|
||||
|
|
@ -1242,22 +1274,6 @@ int query_config( const char *config )
|
|||
}
|
||||
#endif /* MBEDTLS_SSL_ALL_ALERT_MESSAGES */
|
||||
|
||||
#if defined(MBEDTLS_SSL_RECORD_CHECKING)
|
||||
if( strcmp( "MBEDTLS_SSL_RECORD_CHECKING", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_SSL_RECORD_CHECKING );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_RECORD_CHECKING */
|
||||
|
||||
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
||||
if( strcmp( "MBEDTLS_SSL_DTLS_CONNECTION_ID", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_SSL_DTLS_CONNECTION_ID );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
||||
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
if( strcmp( "MBEDTLS_SSL_ASYNC_PRIVATE", config ) == 0 )
|
||||
{
|
||||
|
|
@ -1266,14 +1282,6 @@ int query_config( const char *config )
|
|||
}
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
|
||||
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
|
||||
if( strcmp( "MBEDTLS_SSL_CONTEXT_SERIALIZATION", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_SSL_CONTEXT_SERIALIZATION );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
|
||||
|
||||
#if defined(MBEDTLS_SSL_DEBUG_ALL)
|
||||
if( strcmp( "MBEDTLS_SSL_DEBUG_ALL", config ) == 0 )
|
||||
{
|
||||
|
|
@ -1970,6 +1978,14 @@ int query_config( const char *config )
|
|||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
||||
if( strcmp( "MBEDTLS_PSA_CRYPTO_SE_C", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_PSA_CRYPTO_SE_C );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
|
||||
if( strcmp( "MBEDTLS_PSA_CRYPTO_STORAGE_C", config ) == 0 )
|
||||
{
|
||||
|
|
@ -2218,14 +2234,6 @@ int query_config( const char *config )
|
|||
}
|
||||
#endif /* MBEDTLS_CTR_DRBG_MAX_SEED_INPUT */
|
||||
|
||||
#if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
|
||||
if( strcmp( "MBEDTLS_CTR_DRBG_USE_128_BIT_KEY", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_CTR_DRBG_USE_128_BIT_KEY );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_CTR_DRBG_USE_128_BIT_KEY */
|
||||
|
||||
#if defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
|
||||
if( strcmp( "MBEDTLS_HMAC_DRBG_RESEED_INTERVAL", config ) == 0 )
|
||||
{
|
||||
|
|
@ -2538,30 +2546,6 @@ int query_config( const char *config )
|
|||
}
|
||||
#endif /* MBEDTLS_SSL_IN_CONTENT_LEN */
|
||||
|
||||
#if defined(MBEDTLS_SSL_CID_IN_LEN_MAX)
|
||||
if( strcmp( "MBEDTLS_SSL_CID_IN_LEN_MAX", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_SSL_CID_IN_LEN_MAX );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_CID_IN_LEN_MAX */
|
||||
|
||||
#if defined(MBEDTLS_SSL_CID_OUT_LEN_MAX)
|
||||
if( strcmp( "MBEDTLS_SSL_CID_OUT_LEN_MAX", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_SSL_CID_OUT_LEN_MAX );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_CID_OUT_LEN_MAX */
|
||||
|
||||
#if defined(MBEDTLS_SSL_CID_PADDING_GRANULARITY)
|
||||
if( strcmp( "MBEDTLS_SSL_CID_PADDING_GRANULARITY", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( MBEDTLS_SSL_CID_PADDING_GRANULARITY );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_CID_PADDING_GRANULARITY */
|
||||
|
||||
#if defined(MBEDTLS_SSL_OUT_CONTENT_LEN)
|
||||
if( strcmp( "MBEDTLS_SSL_OUT_CONTENT_LEN", config ) == 0 )
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue