From e829cd6953afbd246fa0db60a296bdae8eaaec81 Mon Sep 17 00:00:00 2001 From: Archana Date: Fri, 24 Dec 2021 12:50:36 +0530 Subject: [PATCH] Driver Wrapper CodeGen Rev 1.1 (1) Add in driver jsons. (2) Improve Python scripts to take JSON file directory and template directory paths as arguments. (3) Add in file augment template files to template common functionality (4) render tempplates for Header files, ID generation and key management. (5) Changed driver ID nomenclature to be in synch with function names. Signed-off-by: Archana Signed-off-by: Asfandyar Orakzai --- .../data_files/driver_jsons/driverlist.json | 1 + .../mbedtls_test_opaque_driver.json | 18 ++ .../mbedtls_test_transparent_driver.json | 13 + .../driver_templates/OS-template-opaque.jinja | 10 + .../OS-template-transparent.jinja | 12 + .../psa_crypto_driver_wrappers.c.jinja | 254 ++++++++++-------- scripts/generate_driver_wrappers.py | 58 +++- tests/include/test/drivers/test_driver.h | 9 + 8 files changed, 247 insertions(+), 128 deletions(-) create mode 100644 scripts/data_files/driver_jsons/driverlist.json create mode 100644 scripts/data_files/driver_jsons/mbedtls_test_opaque_driver.json create mode 100644 scripts/data_files/driver_jsons/mbedtls_test_transparent_driver.json create mode 100644 scripts/data_files/driver_templates/OS-template-opaque.jinja create mode 100644 scripts/data_files/driver_templates/OS-template-transparent.jinja diff --git a/scripts/data_files/driver_jsons/driverlist.json b/scripts/data_files/driver_jsons/driverlist.json new file mode 100644 index 000000000..50ad81604 --- /dev/null +++ b/scripts/data_files/driver_jsons/driverlist.json @@ -0,0 +1 @@ +["mbedtls_test_opaque_driver.json","mbedtls_test_transparent_driver.json"] diff --git a/scripts/data_files/driver_jsons/mbedtls_test_opaque_driver.json b/scripts/data_files/driver_jsons/mbedtls_test_opaque_driver.json new file mode 100644 index 000000000..1322083c3 --- /dev/null +++ b/scripts/data_files/driver_jsons/mbedtls_test_opaque_driver.json @@ -0,0 +1,18 @@ +[{ + "prefix": "mbedtls_test", + "type": "opaque", + "location": "0x7fffff", + "headers": ["test/drivers/test_driver.h"], + "capabilities": [ + { + "_comment": "The mbedTLS opaque driver supports import key/export key/export_public key", + "depend_on": "defined(PSA_CRYPTO_DRIVER_TEST)", + "entry_points": ["import_key", "export_key", "export_public_key"] + }, + { + "_comment": "The mbedTLS opaque driver supports copy key/ get builtin key", + "depend_on": "defined(PSA_CRYPTO_DRIVER_TEST)", + "entry_points": ["copy_key","get_builtin_key"] + } + ] +}] diff --git a/scripts/data_files/driver_jsons/mbedtls_test_transparent_driver.json b/scripts/data_files/driver_jsons/mbedtls_test_transparent_driver.json new file mode 100644 index 000000000..dfa42340c --- /dev/null +++ b/scripts/data_files/driver_jsons/mbedtls_test_transparent_driver.json @@ -0,0 +1,13 @@ +[{ + "prefix": "mbedtls_test", + "type": "transparent", + "headers": ["test/drivers/test_driver.h"], + "capabilities": [ + { + "_comment": "The mbedTLS transparent driver supports import key/export key/export_public key", + "depend_on": "defined(PSA_CRYPTO_DRIVER_TEST)", + "entry_points": ["import_key", "export_key", "export_public_key"], + "fallback": true + } + ] +}] diff --git a/scripts/data_files/driver_templates/OS-template-opaque.jinja b/scripts/data_files/driver_templates/OS-template-opaque.jinja new file mode 100644 index 000000000..3d9724c94 --- /dev/null +++ b/scripts/data_files/driver_templates/OS-template-opaque.jinja @@ -0,0 +1,10 @@ +{% for driver in drivers if driver.type == "opaque" -%} +{% for capability in driver.capabilities if entry_point in capability.entry_points -%} +#if ({% if capability.depend_on is defined -%}{{ capability.depend_on }} {% else -%} {{ 1 }} {% endif %}) +{%- filter indent(width = nest_indent) %} +case {{ driver.location }}: + return({{driver.prefix}}_{{driver.type}}_{{entry_point}}({{entry_point_attributes(driver) | indent(20)}})); +{% endfilter -%} +#endif +{% endfor %} +{% endfor %} diff --git a/scripts/data_files/driver_templates/OS-template-transparent.jinja b/scripts/data_files/driver_templates/OS-template-transparent.jinja new file mode 100644 index 000000000..017c937cb --- /dev/null +++ b/scripts/data_files/driver_templates/OS-template-transparent.jinja @@ -0,0 +1,12 @@ +{% for driver in drivers if driver.type == "transparent" -%} +{% for capability in driver.capabilities if entry_point in capability.entry_points -%} +#if ({% if capability.depend_on is defined -%}{{ capability.depend_on }} {% else -%} {{ 1 }} {% endif %}) +{%- filter indent(width = nest_indent) %} +status = {{driver.prefix}}_{{driver.type}}_{{entry_point}}({{entry_point_attributes(driver) | indent(20)}}); + +if( status != PSA_ERROR_NOT_SUPPORTED ) + return( status ); +{% endfilter -%} +#endif +{% endfor %} +{% endfor %} diff --git a/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja b/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja index a5ae6a29e..3abd1eff1 100644 --- a/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja +++ b/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja @@ -19,6 +19,8 @@ * limitations under the License. */ + +/* BEGIN-common headers */ #include "common.h" #include "psa_crypto_aead.h" #include "psa_crypto_cipher.h" @@ -29,34 +31,30 @@ #include "psa_crypto_rsa.h" #include "mbedtls/platform.h" +/* END-common headers */ #if defined(MBEDTLS_PSA_CRYPTO_C) +/* BEGIN-driver headers */ #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) - -/* Include test driver definition when running tests */ -#if defined(PSA_CRYPTO_DRIVER_TEST) -#ifndef PSA_CRYPTO_DRIVER_PRESENT -#define PSA_CRYPTO_DRIVER_PRESENT -#endif -#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT -#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT -#endif -#include "test/drivers/test_driver.h" -#endif /* PSA_CRYPTO_DRIVER_TEST */ - -/* Repeat above block for each JSON-declared driver during autogeneration */ +{% for driver in drivers -%} +/* Headers for {{driver.prefix}} {{driver.type}} driver */ +{% for header in driver.headers -%} +#include "{{ header }}" +{% endfor %} +{% endfor %} #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ +/* END-driver headers */ /* Auto-generated values depending on which drivers are registered. * ID 0 is reserved for unallocated operations. * ID 1 is reserved for the Mbed TLS software driver. */ +/* BEGIN-driver id definition */ #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1) - -#if defined(PSA_CRYPTO_DRIVER_TEST) -#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2) -#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3) -#endif /* PSA_CRYPTO_DRIVER_TEST */ +{% for driver in drivers -%} +#define {{(driver.prefix + "_" + driver.type + "_driver_id").upper()}} ({{ loop.index + 1 }}) +{% endfor %} +/* END-driver id */ /* Support the 'old' SE interface when asked to */ #if defined(MBEDTLS_PSA_CRYPTO_SE_C) @@ -592,6 +590,16 @@ psa_status_t psa_driver_wrapper_import_key( size_t *key_buffer_length, size_t *bits ) { +{% set entry_point = "import_key" -%} +{% macro entry_point_attributes(driver) -%} +attributes, +data, +data_length, +key_buffer, +key_buffer_size, +key_buffer_length, +bits +{% endmacro %} psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime( attributes ) ); @@ -631,17 +639,13 @@ psa_status_t psa_driver_wrapper_import_key( /* Key is stored in the slot in export representation, so * cycle through all known transparent accelerators */ #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) -#if defined(PSA_CRYPTO_DRIVER_TEST) - status = mbedtls_test_transparent_import_key( - attributes, - data, data_length, - key_buffer, key_buffer_size, - key_buffer_length, bits ); - /* Declared with fallback == true */ - if( status != PSA_ERROR_NOT_SUPPORTED ) - return( status ); -#endif /* PSA_CRYPTO_DRIVER_TEST */ +/* BEGIN-Templating */ +{% with nest_indent=12 %} +{% include "OS-template-transparent.jinja" -%} +{% endwith -%} +/* END-Templating */ #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ + /* Fell through, meaning no accelerator supports this operation */ return( psa_import_key_into_slot( attributes, data, data_length, @@ -649,14 +653,11 @@ psa_status_t psa_driver_wrapper_import_key( key_buffer_length, bits ) ); /* Add cases for opaque driver here */ #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) -#if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TEST_DRIVER_LOCATION: - return( mbedtls_test_opaque_import_key( - attributes, - data, data_length, - key_buffer, key_buffer_size, - key_buffer_length, bits ) ); -#endif /* PSA_CRYPTO_DRIVER_TEST */ +/* BEGIN-Templating */ +{% with nest_indent=8 %} +{% include "OS-template-opaque.jinja" -%} +{% endwith -%} +/* END-Templating */ #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ default: (void)status; @@ -671,6 +672,15 @@ psa_status_t psa_driver_wrapper_export_key( uint8_t *data, size_t data_size, size_t *data_length ) { +{% set entry_point = "export_key" -%} +{% macro entry_point_attributes(driver) -%} +attributes, +key_buffer, +key_buffer_size, +data, +data_size, +data_length +{% endmacro %} psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime( attributes ) ); @@ -707,15 +717,11 @@ psa_status_t psa_driver_wrapper_export_key( /* Add cases for opaque driver here */ #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) -#if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TEST_DRIVER_LOCATION: - return( mbedtls_test_opaque_export_key( attributes, - key_buffer, - key_buffer_size, - data, - data_size, - data_length ) ); -#endif /* PSA_CRYPTO_DRIVER_TEST */ +/* BEGIN-Templating */ +{% with nest_indent=8 %} +{% include "OS-template-opaque.jinja" -%} +{% endwith -%} +/* END-Templating */ #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ default: /* Key is declared with a lifetime not known to us */ @@ -729,6 +735,15 @@ psa_status_t psa_driver_wrapper_export_public_key( uint8_t *data, size_t data_size, size_t *data_length ) { +{% set entry_point = "export_public_key" -%} +{% macro entry_point_attributes(driver) -%} +attributes, +key_buffer, +key_buffer_size, +data, +data_size, +data_length +{% endmacro %} psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime( attributes ) ); @@ -759,18 +774,11 @@ psa_status_t psa_driver_wrapper_export_public_key( /* Key is stored in the slot in export representation, so * cycle through all known transparent accelerators */ #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) -#if defined(PSA_CRYPTO_DRIVER_TEST) - status = mbedtls_test_transparent_export_public_key( - attributes, - key_buffer, - key_buffer_size, - data, - data_size, - data_length ); - /* Declared with fallback == true */ - if( status != PSA_ERROR_NOT_SUPPORTED ) - return( status ); -#endif /* PSA_CRYPTO_DRIVER_TEST */ +/* BEGIN-Templating */ +{% with nest_indent=12 %} +{% include "OS-template-transparent.jinja" -%} +{% endwith -%} +/* END-Templating */ #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ /* Fell through, meaning no accelerator supports this operation */ return( psa_export_public_key_internal( attributes, @@ -782,15 +790,11 @@ psa_status_t psa_driver_wrapper_export_public_key( /* Add cases for opaque driver here */ #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) -#if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TEST_DRIVER_LOCATION: - return( mbedtls_test_opaque_export_public_key( attributes, - key_buffer, - key_buffer_size, - data, - data_size, - data_length ) ); -#endif /* PSA_CRYPTO_DRIVER_TEST */ +/* BEGIN-Templating */ +{% with nest_indent=8 %} +{% include "OS-template-opaque.jinja" -%} +{% endwith -%} +/* END-Templating */ #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ default: /* Key is declared with a lifetime not known to us */ @@ -803,15 +807,23 @@ psa_status_t psa_driver_wrapper_get_builtin_key( psa_key_attributes_t *attributes, uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) { +{% set entry_point = "get_builtin_key" -%} +{% macro entry_point_attributes(driver) -%} +slot_number, +attributes, +key_buffer, +key_buffer_size, +key_buffer_length +{% endmacro %} psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); switch( location ) { #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TEST_DRIVER_LOCATION: - return( mbedtls_test_opaque_get_builtin_key( - slot_number, - attributes, - key_buffer, key_buffer_size, key_buffer_length ) ); +/* BEGIN-Templating */ +{% with nest_indent=8 %} +{% include "OS-template-opaque.jinja" -%} +{% endwith -%} +/* END-Templating */ #endif /* PSA_CRYPTO_DRIVER_TEST */ default: (void) slot_number; @@ -828,6 +840,15 @@ psa_status_t psa_driver_wrapper_copy_key( uint8_t *target_key_buffer, size_t target_key_buffer_size, size_t *target_key_buffer_length ) { +{% set entry_point = "copy_key" -%} +{% macro entry_point_attributes(driver) -%} +attributes, +source_key, +source_key_length, +target_key_buffer, +target_key_buffer_size, +target_key_buffer_length +{% endmacro %} psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); @@ -846,14 +867,11 @@ psa_status_t psa_driver_wrapper_copy_key( switch( location ) { #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) -#if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TEST_DRIVER_LOCATION: - return( mbedtls_test_opaque_copy_key( attributes, source_key, - source_key_length, - target_key_buffer, - target_key_buffer_size, - target_key_buffer_length) ); -#endif /* PSA_CRYPTO_DRIVER_TEST */ +/* BEGIN-Templating */ +{% with nest_indent=8 %} +{% include "OS-template-opaque.jinja" -%} +{% endwith -%} +/* END-Templating */ #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ default: (void)source_key; @@ -1068,7 +1086,7 @@ psa_status_t psa_driver_wrapper_cipher_encrypt_setup( alg ); /* Declared with fallback == true */ if( status == PSA_SUCCESS ) - operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID; if( status != PSA_ERROR_NOT_SUPPORTED ) return( status ); @@ -1100,7 +1118,7 @@ psa_status_t psa_driver_wrapper_cipher_encrypt_setup( alg ); if( status == PSA_SUCCESS ) - operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID; return( status ); #endif /* PSA_CRYPTO_DRIVER_TEST */ @@ -1141,7 +1159,7 @@ psa_status_t psa_driver_wrapper_cipher_decrypt_setup( alg ); /* Declared with fallback == true */ if( status == PSA_SUCCESS ) - operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID; if( status != PSA_ERROR_NOT_SUPPORTED ) return( status ); @@ -1172,7 +1190,7 @@ psa_status_t psa_driver_wrapper_cipher_decrypt_setup( alg ); if( status == PSA_SUCCESS ) - operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID; return( status ); #endif /* PSA_CRYPTO_DRIVER_TEST */ @@ -1204,12 +1222,12 @@ psa_status_t psa_driver_wrapper_cipher_set_iv( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_cipher_set_iv( &operation->ctx.transparent_test_driver_ctx, iv, iv_length ) ); - case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: + case MBEDTLS_TEST_OPAQUE_DRIVER_ID: return( mbedtls_test_opaque_cipher_set_iv( &operation->ctx.opaque_test_driver_ctx, iv, iv_length ) ); @@ -1245,13 +1263,13 @@ psa_status_t psa_driver_wrapper_cipher_update( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_cipher_update( &operation->ctx.transparent_test_driver_ctx, input, input_length, output, output_size, output_length ) ); - case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: + case MBEDTLS_TEST_OPAQUE_DRIVER_ID: return( mbedtls_test_opaque_cipher_update( &operation->ctx.opaque_test_driver_ctx, input, input_length, @@ -1287,12 +1305,12 @@ psa_status_t psa_driver_wrapper_cipher_finish( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_cipher_finish( &operation->ctx.transparent_test_driver_ctx, output, output_size, output_length ) ); - case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: + case MBEDTLS_TEST_OPAQUE_DRIVER_ID: return( mbedtls_test_opaque_cipher_finish( &operation->ctx.opaque_test_driver_ctx, output, output_size, output_length ) ); @@ -1321,7 +1339,7 @@ psa_status_t psa_driver_wrapper_cipher_abort( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: status = mbedtls_test_transparent_cipher_abort( &operation->ctx.transparent_test_driver_ctx ); mbedtls_platform_zeroize( @@ -1329,7 +1347,7 @@ psa_status_t psa_driver_wrapper_cipher_abort( sizeof( operation->ctx.transparent_test_driver_ctx ) ); return( status ); - case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: + case MBEDTLS_TEST_OPAQUE_DRIVER_ID: status = mbedtls_test_opaque_cipher_abort( &operation->ctx.opaque_test_driver_ctx ); mbedtls_platform_zeroize( @@ -1394,7 +1412,7 @@ psa_status_t psa_driver_wrapper_hash_setup( status = mbedtls_test_transparent_hash_setup( &operation->ctx.test_driver_ctx, alg ); if( status == PSA_SUCCESS ) - operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID; if( status != PSA_ERROR_NOT_SUPPORTED ) return( status ); @@ -1429,8 +1447,8 @@ psa_status_t psa_driver_wrapper_hash_clone( &target_operation->ctx.mbedtls_ctx ) ); #endif #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: - target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: + target_operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID; return( mbedtls_test_transparent_hash_clone( &source_operation->ctx.test_driver_ctx, &target_operation->ctx.test_driver_ctx ) ); @@ -1454,7 +1472,7 @@ psa_status_t psa_driver_wrapper_hash_update( input, input_length ) ); #endif #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_hash_update( &operation->ctx.test_driver_ctx, input, input_length ) ); @@ -1480,7 +1498,7 @@ psa_status_t psa_driver_wrapper_hash_finish( hash, hash_size, hash_length ) ); #endif #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_hash_finish( &operation->ctx.test_driver_ctx, hash, hash_size, hash_length ) ); @@ -1503,7 +1521,7 @@ psa_status_t psa_driver_wrapper_hash_abort( return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) ); #endif #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_hash_abort( &operation->ctx.test_driver_ctx ) ); #endif @@ -1650,7 +1668,7 @@ psa_status_t psa_driver_wrapper_aead_encrypt_setup( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID; status = mbedtls_test_transparent_aead_encrypt_setup( &operation->ctx.transparent_test_driver_ctx, attributes, key_buffer, key_buffer_size, @@ -1698,7 +1716,7 @@ psa_status_t psa_driver_wrapper_aead_decrypt_setup( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID; status = mbedtls_test_transparent_aead_decrypt_setup( &operation->ctx.transparent_test_driver_ctx, attributes, @@ -1747,7 +1765,7 @@ psa_status_t psa_driver_wrapper_aead_set_nonce( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_aead_set_nonce( &operation->ctx.transparent_test_driver_ctx, nonce, nonce_length ) ); @@ -1781,7 +1799,7 @@ psa_status_t psa_driver_wrapper_aead_set_lengths( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_aead_set_lengths( &operation->ctx.transparent_test_driver_ctx, ad_length, plaintext_length ) ); @@ -1815,7 +1833,7 @@ psa_status_t psa_driver_wrapper_aead_update_ad( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_aead_update_ad( &operation->ctx.transparent_test_driver_ctx, input, input_length ) ); @@ -1853,7 +1871,7 @@ psa_status_t psa_driver_wrapper_aead_update( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_aead_update( &operation->ctx.transparent_test_driver_ctx, input, input_length, output, output_size, @@ -1897,7 +1915,7 @@ psa_status_t psa_driver_wrapper_aead_finish( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_aead_finish( &operation->ctx.transparent_test_driver_ctx, ciphertext, ciphertext_size, @@ -1961,7 +1979,7 @@ psa_status_t psa_driver_wrapper_aead_verify( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_aead_verify( &operation->ctx.transparent_test_driver_ctx, plaintext, plaintext_size, @@ -1995,7 +2013,7 @@ psa_status_t psa_driver_wrapper_aead_abort( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_aead_abort( &operation->ctx.transparent_test_driver_ctx ) ); @@ -2104,7 +2122,7 @@ psa_status_t psa_driver_wrapper_mac_sign_setup( alg ); /* Declared with fallback == true */ if( status == PSA_SUCCESS ) - operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID; if( status != PSA_ERROR_NOT_SUPPORTED ) return( status ); @@ -2135,7 +2153,7 @@ psa_status_t psa_driver_wrapper_mac_sign_setup( alg ); if( status == PSA_SUCCESS ) - operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID; return( status ); #endif /* PSA_CRYPTO_DRIVER_TEST */ @@ -2176,7 +2194,7 @@ psa_status_t psa_driver_wrapper_mac_verify_setup( alg ); /* Declared with fallback == true */ if( status == PSA_SUCCESS ) - operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID; if( status != PSA_ERROR_NOT_SUPPORTED ) return( status ); @@ -2207,7 +2225,7 @@ psa_status_t psa_driver_wrapper_mac_verify_setup( alg ); if( status == PSA_SUCCESS ) - operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; + operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID; return( status ); #endif /* PSA_CRYPTO_DRIVER_TEST */ @@ -2238,12 +2256,12 @@ psa_status_t psa_driver_wrapper_mac_update( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_mac_update( &operation->ctx.transparent_test_driver_ctx, input, input_length ) ); - case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: + case MBEDTLS_TEST_OPAQUE_DRIVER_ID: return( mbedtls_test_opaque_mac_update( &operation->ctx.opaque_test_driver_ctx, input, input_length ) ); @@ -2272,12 +2290,12 @@ psa_status_t psa_driver_wrapper_mac_sign_finish( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_mac_sign_finish( &operation->ctx.transparent_test_driver_ctx, mac, mac_size, mac_length ) ); - case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: + case MBEDTLS_TEST_OPAQUE_DRIVER_ID: return( mbedtls_test_opaque_mac_sign_finish( &operation->ctx.opaque_test_driver_ctx, mac, mac_size, mac_length ) ); @@ -2306,12 +2324,12 @@ psa_status_t psa_driver_wrapper_mac_verify_finish( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_mac_verify_finish( &operation->ctx.transparent_test_driver_ctx, mac, mac_length ) ); - case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: + case MBEDTLS_TEST_OPAQUE_DRIVER_ID: return( mbedtls_test_opaque_mac_verify_finish( &operation->ctx.opaque_test_driver_ctx, mac, mac_length ) ); @@ -2336,10 +2354,10 @@ psa_status_t psa_driver_wrapper_mac_abort( #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) #if defined(PSA_CRYPTO_DRIVER_TEST) - case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID: return( mbedtls_test_transparent_mac_abort( &operation->ctx.transparent_test_driver_ctx ) ); - case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: + case MBEDTLS_TEST_OPAQUE_DRIVER_ID: return( mbedtls_test_opaque_mac_abort( &operation->ctx.opaque_test_driver_ctx ) ); #endif /* PSA_CRYPTO_DRIVER_TEST */ diff --git a/scripts/generate_driver_wrappers.py b/scripts/generate_driver_wrappers.py index 42331acc8..8e58b66f5 100755 --- a/scripts/generate_driver_wrappers.py +++ b/scripts/generate_driver_wrappers.py @@ -22,52 +22,90 @@ import sys import os +import json import argparse import jinja2 from mbedtls_dev import build_tree -def render(template_path: str) -> str: +def render(template_path: str, driver_jsoncontext: list) -> str: """ - Render template from the input file. + Render template from the input file and driver JSON. """ environment = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.dirname(template_path)), keep_trailing_newline=True) template = environment.get_template(os.path.basename(template_path)) - return template.render() + return template.render(drivers = driver_jsoncontext) -def generate_driver_wrapper_file(mbedtls_root: str, output_dir: str) -> None: + +def generate_driver_wrapper_file(template_dir: str, output_dir: str, driver_jsoncontext: list ) -> None: """ Generate the file psa_crypto_driver_wrapper.c. """ driver_wrapper_template_filename = \ - os.path.join(mbedtls_root, \ - "scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja") + os.path.join(template_dir, "psa_crypto_driver_wrappers.c.jinja") - result = render(driver_wrapper_template_filename) + result = render(driver_wrapper_template_filename, driver_jsoncontext) with open(os.path.join(output_dir, "psa_crypto_driver_wrappers.c"), 'w') as out_file: out_file.write(result) +def validate_mergedjson(merged_driverjson: list) -> int: + """ + Validate the merged Driver JSON for errors that we can catch early + """ + return 0 + + +def merge_driverjsonfiles(json_directory: str, jsondriverlistName: str) -> list: + """ + Merge driver JSON files into a single ordered JSON. + """ + result = list() + driverlist = list() + with open(os.path.join(json_directory, jsondriverlistName), 'r') as driverlistfile: + driverlist = json.load(driverlistfile) + for file_name in driverlist: + with open(os.path.join(json_directory, file_name), 'r') as infile: + result.extend(json.load(infile)) + + return result + + def main() -> int: """ Main with command line arguments. """ def_arg_mbedtls_root = build_tree.guess_mbedtls_root() def_arg_output_dir = os.path.join(def_arg_mbedtls_root, 'library') + def_arg_template_dir = os.path.join(def_arg_mbedtls_root, 'scripts/data_files/driver_templates/') + def_arg_json_dir = os.path.join(def_arg_mbedtls_root, 'scripts/data_files/driver_jsons/') parser = argparse.ArgumentParser() parser.add_argument('--mbedtls-root', nargs='?', default=def_arg_mbedtls_root, help='root directory of mbedtls source code') + parser.add_argument('--template_dir', nargs='?', default=def_arg_template_dir, + help='root directory of mbedtls source code') + parser.add_argument('--json_dir', nargs='?', default=def_arg_json_dir, + help='root directory of mbedtls source code') parser.add_argument('output_directory', nargs='?', default=def_arg_output_dir, help='output file\'s location') args = parser.parse_args() - mbedtls_root = os.path.abspath(args.mbedtls_root) - output_directory = args.output_directory + mbedtls_root = os.path.abspath(args.mbedtls_root) + output_directory = args.output_directory + template_directory = args.template_dir + json_directory = args.json_dir - generate_driver_wrapper_file(mbedtls_root, output_directory) + # load list of driver jsons from driverlist.json + merged_driverjson = merge_driverjsonfiles(json_directory, 'driverlist.json') + ret = validate_mergedjson(merged_driverjson) + if ret == 1: + print("Validation failed ") + return 1 + + generate_driver_wrapper_file(template_directory, output_directory, merged_driverjson) return 0 diff --git a/tests/include/test/drivers/test_driver.h b/tests/include/test/drivers/test_driver.h index 098b21abf..b3c29e433 100644 --- a/tests/include/test/drivers/test_driver.h +++ b/tests/include/test/drivers/test_driver.h @@ -20,6 +20,14 @@ #ifndef PSA_CRYPTO_TEST_DRIVER_H #define PSA_CRYPTO_TEST_DRIVER_H +#if defined(PSA_CRYPTO_DRIVER_TEST) +#ifndef PSA_CRYPTO_DRIVER_PRESENT +#define PSA_CRYPTO_DRIVER_PRESENT +#endif +#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT +#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT +#endif + #define PSA_CRYPTO_TEST_DRIVER_LOCATION 0x7fffff #include "test/drivers/aead.h" @@ -30,4 +38,5 @@ #include "test/drivers/signature.h" #include "test/drivers/asymmetric_encryption.h" +#endif /* PSA_CRYPTO_DRIVER_TEST */ #endif /* PSA_CRYPTO_TEST_DRIVER_H */