Skip to content

Commit d472a8f

Browse files
authored
Merge pull request #7980 from xkqian/codesize_exclude_psa_wrappers
Codesize exclude psa wrappers
2 parents fc9fc33 + db3035b commit d472a8f

19 files changed

+394
-694
lines changed

3rdparty/p256-m/p256-m_driver_entrypoints.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
#include "p256-m_driver_entrypoints.h"
2323
#include "p256-m/p256-m.h"
2424
#include "psa/crypto.h"
25-
#include "psa_crypto_driver_wrappers.h"
2625
#include <stddef.h>
2726
#include <string.h>
27+
#include "psa_crypto_driver_wrappers_no_static.h"
2828

2929
#if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
3030

docs/architecture/psa-crypto-implementation-structure.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ psa_status_t psa_api( ... )
4949
```
5050
The code of most PSA APIs is expected to match precisely the above layout. However, it is likely that the code structure of some APIs will be more complicated with several calls to the driver interface, mainly to encompass a larger variety of hardware designs. For example, to encompass hardware accelerators that are capable of verifying a MAC and those that are only capable of computing a MAC, the psa_mac_verify() API could call first psa_driver_wrapper_mac_verify() and then fallback to psa_driver_wrapper_mac_compute().
5151
52-
The implementations of `psa_driver_wrapper_<entry_point>` functions are generated by the build system based on the JSON driver description files of the various PSA drivers making up the Mbed TLS PSA Cryptography API implementation. The implementations are generated in a psa_crypto_driver_wrappers.c C file and the function prototypes declared in a psa_crypto_driver_wrappers.h header file.
52+
The implementations of `psa_driver_wrapper_<entry_point>` functions are generated by the build system based on the JSON driver description files of the various PSA drivers making up the Mbed TLS PSA Cryptography API implementation. The implementations are splited into two parts. The static ones are generated in a psa_crypto_driver_wrappers.h header file, the non-static ones are generated in a psa_crypto_driver_wrappers_no_static.c C file and the function prototypes declared in a psa_crypto_driver_wrappers_no_static.h header file.
5353
5454
The psa_driver_wrapper_<entry_point>() functions dispatch cryptographic operations to accelerator drivers, secure element drivers as well as to the software implementations of cryptographic operations.
5555
@@ -139,7 +139,7 @@ Some mechanisms require other mechanisms. For example, you can't do GCM without
139139
The general structure of a cryptographic operation function is:
140140
141141
1. API function defined in `library/psa_crypto.c`. The entry point performs generic checks that don't depend on whether the mechanism is implemented in software or in a driver and looks up keys in the key store.
142-
2. Driver dispatch code in `scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja` or files included from there.
142+
2. Driver dispatch code in `scripts/data_files/driver_templates/psa_crypto_driver_wrappers.h.jinja`, `scripts/data_files/driver_templates/psa_crypto_driver_wrappers_no_static.c.jinja` or files included from there.
143143
3. Built-in implementation in `library/psa_crypto_*.c` (with function declarations in the corresponding `.h` file). These files typically contain the implementation of modes of operation over basic building blocks that are defined elsewhere. For example, HMAC is implemented in `library/psa_crypto_mac.c` but the underlying hash functions are implemented in `library/sha*.c` and `library/md*.c`.
144144
4. Basic cryptographic building blocks in `library/*.c`.
145145

docs/proposed/psa-driver-wrappers-codegen-migration-guide.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Migrating to an auto generated psa_crypto_driver_wrappers.c file
1+
Migrating to an auto generated psa_crypto_driver_wrappers.h file
22
================================================================
33

4-
This document describes how to migrate to the auto generated psa_crypto_driver_wrappers.c file.
4+
This document describes how to migrate to the auto generated psa_crypto_driver_wrappers.h file.
55
It is meant to give the library user migration guidelines while the Mbed TLS project tides over multiple minor revs of version 1.0, after which this will be merged into psa-driver-interface.md.
66

77
For a practical guide with a description of the current state of drivers Mbed TLS, see our [PSA Cryptoprocessor driver development examples](../psa-driver-example-and-guide.html).
@@ -27,10 +27,10 @@ While that is the larger goal, for version 1.1 here's what's changed
2727

2828
#### What's changed
2929

30-
(1) psa_crypto_driver_wrappers.c will from this point on be auto generated.
31-
(2) The auto generation is based on the template file at **scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja**.
32-
(3) The driver JSONS to be used for generating the psa_crypto_driver_wrappers.c file can be found at **scripts/data_files/driver_jsons/** as their default location, this path includes the schemas against which the driver schemas will be validated (driver_opaque_schema.json, driver_transparent_schema.json) and a driverlist.json which specifies the drivers to be considered and the order in which they want to be called into. The default location for driverlist.json and driver JSONS can be overloaded by passing an argument --json-dir while running the script generate_driver_wrappers.py.
33-
(4) While the complete driver wrapper templating support is yet to come in, if the library user sees a need to patch psa_crypto_driver_wrappers.c file, the user will need to patch into the template file as needed (psa_crypto_driver_wrappers.c.jinja).
30+
(1) psa_crypto_driver_wrappers.h will from this point on be auto generated.
31+
(2) The auto generation is based on the template file at **scripts/data_files/driver_templates/psa_crypto_driver_wrappers.h.jinja**.
32+
(3) The driver JSONS to be used for generating the psa_crypto_driver_wrappers.h file can be found at **scripts/data_files/driver_jsons/** as their default location, this path includes the schemas against which the driver schemas will be validated (driver_opaque_schema.json, driver_transparent_schema.json) and a driverlist.json which specifies the drivers to be considered and the order in which they want to be called into. The default location for driverlist.json and driver JSONS can be overloaded by passing an argument --json-dir while running the script generate_driver_wrappers.py.
33+
(4) While the complete driver wrapper templating support is yet to come in, if the library user sees a need to patch psa_crypto_driver_wrappers.h file, the user will need to patch into the template file as needed (psa_crypto_driver_wrappers.h.jinja).
3434

3535
#### How to set your driver up
3636

docs/psa-driver-example-and-guide.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Include any header files required by the driver in `psa_crypto_driver_wrappers.h
7474

7575

7676
**4. For each operation being accelerated, locate the function in the driver dispatch layer that corresponds to the entry point of that operation.** \
77-
The file `psa_crypto_driver_wrappers.c.jinja` contains the driver wrapper functions. For the entry points that have driver wrapper auto-generation implemented, the functions have been replaced with `jinja` templating logic. While the file has a `.jinja` extension, the driver wrapper functions for the remaining entry points are simple C functions. The names of these functions are of the form `psa_driver_wrapper` followed by the entry point name. So, for example, the function `psa_driver_wrapper_sign_hash()` corresponds to the `sign_hash` entry point.
77+
The file `psa_crypto_driver_wrappers.h.jinja` and `psa_crypto_driver_wrappers_no_static.c.jinja` contains the driver wrapper functions. For the entry points that have driver wrapper auto-generation implemented, the functions have been replaced with `jinja` templating logic. While the file has a `.jinja` extension, the driver wrapper functions for the remaining entry points are simple C functions. The names of these functions are of the form `psa_driver_wrapper` followed by the entry point name. So, for example, the function `psa_driver_wrapper_sign_hash()` corresponds to the `sign_hash` entry point.
7878

7979
**5. If a driver entry point function has been provided then ensure it has the same signature as the driver wrapper function.** \
8080
If one has not been provided then write one. Its name should begin with the driver prefix, followed by transparent/opaque (depending on driver type), and end with the entry point name. It should have the same signature as the driver wrapper function. The purpose of the entry point function is to take arguments in PSA format for the implemented operation and return outputs/status codes in PSA format. \
@@ -153,7 +153,7 @@ The p256-m driver implements four entry points: `generate_key`, `key_agreement`,
153153
There are no entry points for `sign_message` and `verify_message`, which are not necessary for a sign-and-hash algorithm. The core still implements these functions by doing the hashes and then calling the sign/verify-hash entry points.
154154
The driver entry point functions can be found in `p256m_driver_entrypoints.[hc]`. These functions act as an interface between Mbed TLS and p256-m; converting between PSA and p256-m argument formats and performing sanity checks. If the driver's status codes differ from PSA's, it is recommended to implement a status code translation function. The function `p256_to_psa_error()` converts error codes returned by p256-m into PSA error codes.
155155
156-
The driver wrapper functions in `psa_crypto_driver_wrappers.c.jinja` for all four entry points have also been modified. The code block below shows the additions made to `psa_driver_wrapper_sign_hash()`. In adherence to the defined process, all code related to the driver call is placed within a check for `MBEDTLS_PSA_P256M_DRIVER_ENABLED`. p256-m only supports non-deterministic ECDSA using keys based on NIST P256; these constraints are enforced through checks (see the `if` statement). Checks that involve accessing key attributes, (e.g. checking key type or bits) **must** be performed in the driver wrapper. This is because this information is marked private and may not be accessed outside the library. Other checks can be performed here or in the entry point function. The status returned by the driver is propagated up the call hierarchy **unless** the driver does not support the operation (i.e. return `PSA_ERROR_NOT_SUPPORTED`). In that case the next available driver/built-in implementation is called.
156+
The driver wrapper functions in `psa_crypto_driver_wrappers.h.jinja` for all four entry points have also been modified. The code block below shows the additions made to `psa_driver_wrapper_sign_hash()`. In adherence to the defined process, all code related to the driver call is placed within a check for `MBEDTLS_PSA_P256M_DRIVER_ENABLED`. p256-m only supports non-deterministic ECDSA using keys based on NIST P256; these constraints are enforced through checks (see the `if` statement). Checks that involve accessing key attributes, (e.g. checking key type or bits) **must** be performed in the driver wrapper. This is because this information is marked private and may not be accessed outside the library. Other checks can be performed here or in the entry point function. The status returned by the driver is propagated up the call hierarchy **unless** the driver does not support the operation (i.e. return `PSA_ERROR_NOT_SUPPORTED`). In that case the next available driver/built-in implementation is called.
157157
158158
```
159159
#if defined (MBEDTLS_PSA_P256M_DRIVER_ENABLED)

include/psa/crypto_driver_contexts_composites.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ typedef mbedtls_psa_pake_operation_t
121121
*
122122
* The union members are the driver's context structures, and the member names
123123
* are formatted as `'drivername'_ctx`. This allows for procedural generation
124-
* of both this file and the content of psa_crypto_driver_wrappers.c */
124+
* of both this file and the content of psa_crypto_driver_wrappers.h */
125125

126126
typedef union {
127127
unsigned dummy; /* Make sure this union is always non-empty */

include/psa/crypto_driver_contexts_primitives.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ typedef struct {
9494
*
9595
* The union members are the driver's context structures, and the member names
9696
* are formatted as `'drivername'_ctx`. This allows for procedural generation
97-
* of both this file and the content of psa_crypto_driver_wrappers.c */
97+
* of both this file and the content of psa_crypto_driver_wrappers.h */
9898

9999
typedef union {
100100
unsigned dummy; /* Make sure this union is always non-empty */

library/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ libmbed*
66
/error.c
77
/version_features.c
88
/ssl_debug_helpers_generated.c
9-
/psa_crypto_driver_wrappers.c
9+
/psa_crypto_driver_wrappers.h
10+
/psa_crypto_driver_wrappers_no_static.c
1011
###END_GENERATED_FILES###

library/CMakeLists.txt

+6-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ set(src_crypto
6767
psa_crypto_aead.c
6868
psa_crypto_cipher.c
6969
psa_crypto_client.c
70-
psa_crypto_driver_wrappers.c
70+
psa_crypto_driver_wrappers_no_static.c
7171
psa_crypto_ecp.c
7272
psa_crypto_ffdh.c
7373
psa_crypto_hash.c
@@ -174,22 +174,24 @@ if(GEN_FILES)
174174

175175
add_custom_command(
176176
OUTPUT
177-
${CMAKE_CURRENT_BINARY_DIR}/psa_crypto_driver_wrappers.c
177+
${CMAKE_CURRENT_BINARY_DIR}/psa_crypto_driver_wrappers.h
178+
${CMAKE_CURRENT_BINARY_DIR}/psa_crypto_driver_wrappers_no_static.c
178179
COMMAND
179180
${MBEDTLS_PYTHON_EXECUTABLE}
180181
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py
181182
${CMAKE_CURRENT_BINARY_DIR}
182183
DEPENDS
183184
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py
184-
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja
185+
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.h.jinja
186+
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers_no_static.c.jinja
185187
)
186188

187189

188190
else()
189191
link_to_source(error.c)
190192
link_to_source(version_features.c)
191193
link_to_source(ssl_debug_helpers_generated.c)
192-
link_to_source(psa_crypto_driver_wrappers.c)
194+
link_to_source(psa_crypto_driver_wrappers_no_static.c)
193195
endif()
194196

195197
if(CMAKE_COMPILER_IS_GNUCC)

library/Makefile

+13-6
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ OBJS_CRYPTO= \
136136
psa_crypto_aead.o \
137137
psa_crypto_cipher.o \
138138
psa_crypto_client.o \
139-
psa_crypto_driver_wrappers.o \
139+
psa_crypto_driver_wrappers_no_static.o \
140140
psa_crypto_ecp.o \
141141
psa_crypto_ffdh.o \
142142
psa_crypto_hash.o \
@@ -316,7 +316,8 @@ libmbedcrypto.dll: $(OBJS_CRYPTO)
316316
GENERATED_FILES = \
317317
error.c version_features.c \
318318
ssl_debug_helpers_generated.c \
319-
psa_crypto_driver_wrappers.c
319+
psa_crypto_driver_wrappers.h \
320+
psa_crypto_driver_wrappers_no_static.c
320321
generated_files: $(GENERATED_FILES)
321322

322323
# See root Makefile
@@ -352,12 +353,18 @@ version_features.c:
352353
echo " Gen $@"
353354
$(PERL) ../scripts/generate_features.pl
354355

355-
psa_crypto_driver_wrappers.c: $(gen_file_dep) ../scripts/generate_driver_wrappers.py
356-
psa_crypto_driver_wrappers.c: $(gen_file_dep) ../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja
357-
psa_crypto_driver_wrappers.c:
358-
echo " Gen $@"
356+
GENERATED_WRAPPER_FILES = \
357+
psa_crypto_driver_wrappers.h \
358+
psa_crypto_driver_wrappers_no_static.c
359+
$(GENERATED_WRAPPER_FILES): ../scripts/generate_driver_wrappers.py
360+
$(GENERATED_WRAPPER_FILES): ../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.h.jinja
361+
$(GENERATED_WRAPPER_FILES): ../scripts/data_files/driver_templates/psa_crypto_driver_wrappers_no_static.c.jinja
362+
$(GENERATED_WRAPPER_FILES):
363+
echo " Gen $(GENERATED_WRAPPER_FILES)"
359364
$(PYTHON) ../scripts/generate_driver_wrappers.py
360365

366+
psa_crypto.o:psa_crypto_driver_wrappers.h
367+
361368
clean:
362369
ifndef WINDOWS
363370
rm -f *.o libmbed*

library/psa_crypto.c

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "psa_crypto_core.h"
3535
#include "psa_crypto_invasive.h"
3636
#include "psa_crypto_driver_wrappers.h"
37+
#include "psa_crypto_driver_wrappers_no_static.h"
3738
#include "psa_crypto_ecp.h"
3839
#include "psa_crypto_ffdh.h"
3940
#include "psa_crypto_hash.h"

0 commit comments

Comments
 (0)