xref: /aosp_15_r20/external/mbedtls/docs/architecture/alternative-implementations.md (revision 62c56f9862f102b96d72393aff6076c951fb8148)
1*62c56f98SSadaf EbrahimiAlternative implementations of Mbed TLS functionality
2*62c56f98SSadaf Ebrahimi=====================================================
3*62c56f98SSadaf Ebrahimi
4*62c56f98SSadaf EbrahimiThis document describes how parts of the Mbed TLS functionality can be replaced at compile time to integrate the library on a platform.
5*62c56f98SSadaf Ebrahimi
6*62c56f98SSadaf EbrahimiThis document is an overview. It is not exhaustive. Please consult the documentation of individual modules and read the library header files for more details.
7*62c56f98SSadaf Ebrahimi
8*62c56f98SSadaf Ebrahimi## Platform integration
9*62c56f98SSadaf Ebrahimi
10*62c56f98SSadaf EbrahimiMbed TLS works out of the box on Unix/Linux/POSIX-like systems and on Windows. On embedded platforms, you may need to customize some aspects of how Mbed TLS interacts with the underlying platform. This section discusses the main areas that can be configured.
11*62c56f98SSadaf Ebrahimi
12*62c56f98SSadaf EbrahimiThe platform module (`include/mbedtls/platform.h`) controls how Mbed TLS accesses standard library features such as memory management (`calloc`, `free`), `printf`, `exit`. You can define custom functions instead of the ones from the C standard library through `MBEDTLS_PLATFORM_XXX` options in the configuration file. Many options have two mechanisms: either define `MBEDTLS_PLATFORM_XXX_MACRO` to the name of a function to call instead of the standard function `xxx`, or define `MBEDTLS_PLATFORM_XXX_ALT` and [register an alternative implementation during the platform setup](#alternative-implementations-of-platform-functions).
13*62c56f98SSadaf Ebrahimi
14*62c56f98SSadaf EbrahimiThe storage of the non-volatile seed for random generation, enabled with `MBEDTLS_ENTROPY_NV_SEED`, is also controlled via the platform module.
15*62c56f98SSadaf Ebrahimi
16*62c56f98SSadaf EbrahimiFor timing functions, you can [declare an alternative implementation of the timing module](#module-alternative-implementations).
17*62c56f98SSadaf Ebrahimi
18*62c56f98SSadaf EbrahimiOn multithreaded platforms, [declare an alternative implementation of the threading module](#module-alternative-implementations).
19*62c56f98SSadaf Ebrahimi
20*62c56f98SSadaf EbrahimiTo configure entropy sources (hardware random generators), see the `MBEDTLS_ENTROPY_XXX` options in the configuration file.
21*62c56f98SSadaf Ebrahimi
22*62c56f98SSadaf EbrahimiFor networking, the `net_sockets` module does not currently support alternative implementations. If this module does not work on your platform, disable `MBEDTLS_NET_C` and use custom functions for TLS.
23*62c56f98SSadaf Ebrahimi
24*62c56f98SSadaf EbrahimiIf your platform has a cryptographic accelerator, you can use it via a [PSA driver](#psa-cryptography-drivers) or declare an [alternative implementation of the corresponding module(s)](#module-alternative-implementations) or [of specific functions](#function-alternative-implementations). PSA drivers will ultimately replace the alternative implementation mechanism, but alternative implementation will remain supported in at least all Mbed TLS versions of the form 3.x. The interface of PSA drivers is currently still experimental and subject to change.
25*62c56f98SSadaf Ebrahimi
26*62c56f98SSadaf Ebrahimi## PSA cryptography drivers
27*62c56f98SSadaf Ebrahimi
28*62c56f98SSadaf EbrahimiOn platforms where a hardware cryptographic engine is present, you can implement a driver for this engine in the PSA interface. Drivers are supported for cryptographic operations with transparent keys (keys available in cleartext), for cryptographic operations with opaque keys (keys that are only available inside the cryptographic engine), and for random generation. Calls to `psa_xxx` functions that perform cryptographic operations are directed to drivers instead of the built-in code as applicable. See the [PSA cryptography driver interface specification](docs/proposed/psa-driver-interface.md), the [Mbed TLS PSA driver developer guide](docs/proposed/psa-driver-developer-guide.md) and the [Mbed TLS PSA driver integration guide](docs/proposed/psa-driver-integration-guide.md) for more information.
29*62c56f98SSadaf Ebrahimi
30*62c56f98SSadaf EbrahimiAs of Mbed TLS 3.0, this interface is still experimental and subject to change, and not all operations support drivers yet. The configuration option `MBEDTLS_USE_PSA_CRYPTO` causes parts of the `mbedtls_xxx` API to use PSA crypto and therefore to support drivers, however it is not yet compatible with all drivers.
31*62c56f98SSadaf Ebrahimi
32*62c56f98SSadaf Ebrahimi## Module alternative implementations
33*62c56f98SSadaf Ebrahimi
34*62c56f98SSadaf EbrahimiYou can replace the code of some modules of Mbed TLS at compile time by a custom implementation. This is possible for low-level cryptography modules (symmetric algorithms, DHM, RSA, ECP, ECJPAKE) and for some platform-related modules (threading, timing). Such custom implementations are called “alternative implementations”, or “ALT implementations” for short.
35*62c56f98SSadaf Ebrahimi
36*62c56f98SSadaf EbrahimiThe general principle of an alternative implementation is:
37*62c56f98SSadaf Ebrahimi* Enable `MBEDTLS_XXX_ALT` in the compile-time configuration where XXX is the module name. For example, `MBEDTLS_AES_ALT` for an implementation of the AES module. This is in addition to enabling `MBEDTLS_XXX_C`.
38*62c56f98SSadaf Ebrahimi* Create a header file `xxx_alt.h` that defines the context type(s) used by the module. For example, `mbedtls_aes_context` for AES.
39*62c56f98SSadaf Ebrahimi* Implement all the functions from the module, i.e. the functions declared in `include/mbedtls/xxx.h`.
40*62c56f98SSadaf Ebrahimi
41*62c56f98SSadaf EbrahimiSee https://mbed-tls.readthedocs.io/en/latest/kb/development/hw_acc_guidelines for a more detailed guide.
42*62c56f98SSadaf Ebrahimi
43*62c56f98SSadaf Ebrahimi### Constraints on context types
44*62c56f98SSadaf Ebrahimi
45*62c56f98SSadaf EbrahimiGenerally, alternative implementations can define their context types to any C type except incomplete and array types (although they would normally be `struct` types). This section lists some known limitations where the context type needs to be a structure with certain fields.
46*62c56f98SSadaf Ebrahimi
47*62c56f98SSadaf EbrahimiWhere a context type needs to have a certain field, the field must have the same type and semantics as in the built-in implementation, but does not need to be at the same position in the structure. Furthermore, unless otherwise indicated, only read access is necessary: the field can be `const`, and modifications to it do not need to be supported. For example, if an alternative implementation of asymmetric cryptography uses a different representation of large integers, it is sufficient to provide a read-only copy of the fields listed here of type `mbedtls_mpi`.
48*62c56f98SSadaf Ebrahimi
49*62c56f98SSadaf Ebrahimi* AES: if `MBEDTLS_AESNI_C` or `MBEDTLS_PADLOCK_C` is enabled, `mbedtls_aes_context` must have the fields `nr` and `rk`.
50*62c56f98SSadaf Ebrahimi* DHM: if `MBEDTLS_DEBUG_C` is enabled, `mbedtls_dhm_context` must have the fields `P`, `Q`, `G`, `GX`, `GY` and `K`.
51*62c56f98SSadaf Ebrahimi* ECP: `mbedtls_ecp_group` must have the fields `id`, `P`, `A`, `B`, `G`, `N`, `pbits` and `nbits`.
52*62c56f98SSadaf Ebrahimi    * If `MBEDTLS_PK_PARSE_EC_EXTENDED` is enabled, those fields must be writable, and `mbedtls_ecp_point_read_binary()` must support a group structure where only `P`, `pbits`, `A` and `B` are set.
53*62c56f98SSadaf Ebrahimi
54*62c56f98SSadaf EbrahimiIt must be possible to move a context object in memory (except during the execution of a library function that takes this context as an argument). (This is necessary, for example, to support applications that populate a context on the stack of an inner function and then copy the context upwards through the call chain, or applications written in a language with automatic memory management that can move objects on the heap.) That is, call sequences like the following must work:
55*62c56f98SSadaf Ebrahimi```
56*62c56f98SSadaf Ebrahimimbedtls_xxx_context ctx1, ctx2;
57*62c56f98SSadaf Ebrahimimbedtls_xxx_init(&ctx1);
58*62c56f98SSadaf Ebrahimimbedtls_xxx_setup(&ctx1, …);
59*62c56f98SSadaf Ebrahimictx2 = ctx1;
60*62c56f98SSadaf Ebrahimimemset(&ctx1, 0, sizeof(ctx1));
61*62c56f98SSadaf Ebrahimimbedtls_xxx_do_stuff(&ctx2, …);
62*62c56f98SSadaf Ebrahimimbedtls_xxx_free(&ctx2);
63*62c56f98SSadaf Ebrahimi```
64*62c56f98SSadaf EbrahimiIn practice, this means that a pointer to a context or to a part of a context does not remain valid across function calls. Alternative implementations do not need to support copying of contexts: contexts can only be cloned through explicit `clone()` functions.
65*62c56f98SSadaf Ebrahimi
66*62c56f98SSadaf Ebrahimi## Function alternative implementations
67*62c56f98SSadaf Ebrahimi
68*62c56f98SSadaf EbrahimiIn some cases, it is possible to replace a single function or a small set of functions instead of [providing an alternative implementation of the whole module](#module-alternative-implementations).
69*62c56f98SSadaf Ebrahimi
70*62c56f98SSadaf Ebrahimi### Alternative implementations of cryptographic functions
71*62c56f98SSadaf Ebrahimi
72*62c56f98SSadaf EbrahimiOptions to replace individual functions of cryptographic modules generally have a name obtained by upper-casing the function name and appending `_ALT`. If the function name contains `_internal`, `_ext` or `_ret`, this is removed in the `_ALT` symbol. When the corresponding option is enabled, the built-in implementation of the function will not be compiled, and you must provide an alternative implementation at link time.
73*62c56f98SSadaf Ebrahimi
74*62c56f98SSadaf EbrahimiFor example, enable `MBEDTLS_AES_ENCRYPT_ALT` at compile time and provide your own implementation of `mbedtls_aes_encrypt()` to provide an accelerated implementation of AES encryption that is compatible with the built-in key schedule. If you wish to implement key schedule differently, you can also enable `MBEDTLS_AES_SETKEY_ENC_ALT` and implement `mbedtls_aes_setkey_enc()`.
75*62c56f98SSadaf Ebrahimi
76*62c56f98SSadaf EbrahimiAnother example: enable `MBEDTLS_SHA256_PROCESS_ALT` and implement `mbedtls_internal_sha256_process()` to provide an accelerated implementation of SHA-256 and SHA-224.
77*62c56f98SSadaf Ebrahimi
78*62c56f98SSadaf EbrahimiNote that since alternative implementations of individual functions cooperate with the built-in implementation of other functions, you must use the same layout for context objects as the built-in implementation. If you want to use different context types, you need to [provide an alternative implementation of the whole module](#module-alternative-implementations).
79*62c56f98SSadaf Ebrahimi
80*62c56f98SSadaf Ebrahimi### Alternative implementations of platform functions
81*62c56f98SSadaf Ebrahimi
82*62c56f98SSadaf EbrahimiSeveral platform functions can be reconfigured dynamically by following the process described here. To reconfigure how Mbed TLS calls the standard library function `xxx()`:
83*62c56f98SSadaf Ebrahimi
84*62c56f98SSadaf Ebrahimi* Define the symbol `MBEDTLS_PLATFORM_XXX_ALT` at compile time.
85*62c56f98SSadaf Ebrahimi* During the initialization of your application, set the global variable `mbedtls_xxx` to an alternative implementation of `xxx()`.
86*62c56f98SSadaf Ebrahimi
87*62c56f98SSadaf EbrahimiFor example, to provide a custom `printf` function at run time, enable `MBEDTLS_PLATFORM_PRINTF_ALT` at compile time and assign to `mbedtls_printf` during the initialization of your application.
88*62c56f98SSadaf Ebrahimi
89*62c56f98SSadaf EbrahimiMerely enabling `MBEDTLS_PLATFORM_XXX_ALT` does not change the behavior: by default, `mbedtls_xxx` points to the standard function `xxx`.
90*62c56f98SSadaf Ebrahimi
91*62c56f98SSadaf EbrahimiNote that there are variations on the naming pattern. For example, some configurable functions are activated in pairs, such as `mbedtls_calloc` and `mbedtls_free` via `MBEDTLS_PLATFORM_MEMORY`. Consult the documentation of individual configuration options and of the platform module for details.
92