blob: 384316fa10dcad62bf70e8b24431e57c28f72be6 [file] [log] [blame]
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001.. _cryptographic_implementation:
2
3############################
4Cryptographic implementation
5############################
6This document describes how the TEE Cryptographic Operations API is implemented,
7how the default crypto provider may be configured at compile time, and how it
8may be replaced by another implementation.
9
10Overview
11********
12There are several layers from the Trusted Application to the actual crypto
13algorithms. Most of the crypto code runs in kernel mode inside the TEE core.
14Here is a schematic view of a typical call to the crypto API. The numbers in
15square brackets ([1], [2]...) refer to the sections below.
16
17.. code-block:: none
18
19 - some_function() (Trusted App) -
20 [1] TEE_*() User space (libutee.a)
21 ------- utee_*() ----------------------------------------------
22 [2] tee_svc_*() Kernel space
23 [3] crypto_*() (libtomcrypt.a and crypto.c)
24 [4] /* LibTomCrypt */ (libtomcrypt.a)
25
Etienne Carrierec5b978f2025-03-28 14:06:25 +010026The TEE Cryptographic Operations API
27************************************
Joakim Bech8e5c5b32018-10-25 08:18:32 +020028OP-TEE implements the Cryptographic Operations API defined by the GlobalPlatform
29association in the :ref:`tee_internal_core_api`. This includes cryptographic
30functions that span various cryptographic needs: message digests, symmetric
31ciphers, message authentication codes (MAC), authenticated encryption,
32asymmetric operations (encryption/decryption or signing/verifying), key
33derivation, and random data generation. These functions make up the TEE
34Cryptographic Operations API.
35
36The Internal API is implemented in tee_api_operations.c_, which is compiled into
37a static library: ``${O}/ta_arm{32,64}-lib/libutee/libutee.a``.
38
39Most API functions perform some parameter checking and manipulations, then
40invoke some *utee\_\** function to switch to kernel mode and perform the
41low-level work.
42
43The *utee\_\** functions are declared in utee_syscalls.h_ and implemented in
44utee_syscalls_asm.S_ They are simple system call wrappers which use the *SVC*
45instruction to switch to the appropriate system service in the OP-TEE kernel.
46
Etienne Carrierec5b978f2025-03-28 14:06:25 +010047The crypto services
48*******************
Joakim Bech8e5c5b32018-10-25 08:18:32 +020049All cryptography-related system calls are declared in tee_svc_cryp.h_ and
50implemented in tee_svc_cryp.c_. In addition to dealing with the usual work
51required at the user/kernel interface (checking parameters and copying memory
52buffers between user and kernel space), the system calls invoke a private
53abstraction layer: the **Crypto API**, which is declared in crypto.h_. It serves
54two main purposes:
55
56 1. Allow for alternative implementations, such as hardware-accelerated
57 versions.
58
59 2. Provide an easy way to disable some families of algorithms at
Etienne Carriere9e574932025-03-27 18:45:28 +010060 compile-time to save space. See sw_crypto_lib_ below.
Joakim Bech8e5c5b32018-10-25 08:18:32 +020061
Etienne Carrierec5b978f2025-03-28 14:06:25 +010062crypto_*()
63**********
Joakim Bech8e5c5b32018-10-25 08:18:32 +020064The ``crypto_*()`` functions implement the actual algorithms and helper
65functions. TEE Core has one global active implementation of this interface. The
66default implementation, mostly based on LibTomCrypt_, is as follows:
67
68.. code-block:: c
69 :caption: File: core/crypto/crypto.c
Jorge Ramirez-Ortiz6cb630f2024-02-12 09:21:47 +010070
Joakim Bech8e5c5b32018-10-25 08:18:32 +020071 /*
72 * Default implementation for all functions in crypto.h
73 */
Jorge Ramirez-Ortiz6cb630f2024-02-12 09:21:47 +010074
Joakim Bech8e5c5b32018-10-25 08:18:32 +020075 #if !defined(_CFG_CRYPTO_WITH_HASH)
76 TEE_Result crypto_hash_get_ctx_size(uint32_t algo __unused,
77 size_t *size __unused)
78 {
79 return TEE_ERROR_NOT_IMPLEMENTED;
80 }
81 ...
82 #endif /*_CFG_CRYPTO_WITH_HASH*/
Jorge Ramirez-Ortiz6cb630f2024-02-12 09:21:47 +010083
Joakim Bech8e5c5b32018-10-25 08:18:32 +020084.. code-block:: c
85 :caption: File: core/lib/libtomcrypt/tee_ltc_provider.c
Jorge Ramirez-Ortiz6cb630f2024-02-12 09:21:47 +010086
Joakim Bech8e5c5b32018-10-25 08:18:32 +020087 #if defined(_CFG_CRYPTO_WITH_HASH)
88 TEE_Result crypto_hash_get_ctx_size(uint32_t algo, size_t *size)
89 {
90 /* ... */
91 return TEE_SUCCESS;
92 }
Jorge Ramirez-Ortiz6cb630f2024-02-12 09:21:47 +010093
Joakim Bech8e5c5b32018-10-25 08:18:32 +020094 #endif /*_CFG_CRYPTO_WITH_HASH*/
Jorge Ramirez-Ortiz6cb630f2024-02-12 09:21:47 +010095
Joakim Bech8e5c5b32018-10-25 08:18:32 +020096As shown above, families of algorithms can be disabled and crypto.c_ will
97provide default null implementations that will return
98``TEE_ERROR_NOT_IMPLEMENTED``.
99
100Public/private key format
101*************************
102crypto.h_ uses implementation-specific types to hold key data for asymmetric
103algorithms. For instance, here is how a public RSA key is represented:
104
105.. code-block:: c
106 :caption: File: core/include/crypto/crypto.h
107
108 struct rsa_public_key {
109 struct bignum *e; /* Public exponent */
110 struct bignum *n; /* Modulus */
111 };
112
113This is also how such keys are stored inside the TEE object attributes
114(``TEE_ATTR_RSA_PUBLIC_KEY`` in this case). ``struct bignum`` is an opaque type,
115known to the underlying implementation only. ``struct bignum_ops`` provides
116functions so that the system services can manipulate data of this type. This
117includes allocation/deallocation, copy, and conversion to or from the big endian
118binary format.
119
120.. code-block:: c
121 :caption: File: core/include/crypto/crypto.h
122
123 struct bignum *crypto_bignum_allocate(size_t size_bits);
124
125 TEE_Result crypto_bignum_bin2bn(const uint8_t *from, size_t fromsize,
126 struct bignum *to);
127
128 void crypto_bignum_bn2bin(const struct bignum *from, uint8_t *to);
129 /*...*/
130
Etienne Carriere9e574932025-03-27 18:45:28 +0100131.. _sw_crypto_lib:
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200132
Etienne Carrierec5b978f2025-03-28 14:06:25 +0100133Software crypto library
134***********************
Etienne Carriere9e574932025-03-27 18:45:28 +0100135
136LibTomCrypt is the default software implementation for cryptographic
137operations. When so, this is stated by OP-TEE configuration directives
138``CFG_CRYPTOLIB_NAME=tomcrypt`` and
139``CFG_CRYPTOLIB_DIR=core/lib/libtomcrypt``.
140
141Mbed TLS can be embedded in OP-TEE core as a software cryptographic library
142alternative to LibTomCrypt. See :ref:`libmbedtls`.
143
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200144Some algorithms may be disabled at compile time if they are not needed, in order
145to reduce the size of the OP-TEE image and reduces its memory usage. This is
146done by setting the appropriate configuration variable. For example:
147
148.. code-block:: bash
149
150 $ make CFG_CRYPTO_AES=n # disable AES only
151 $ make CFG_CRYPTO_{AES,DES}=n # disable symmetric ciphers
152 $ make CFG_CRYPTO_{DSA,RSA,DH,ECC}=n # disable public key algorithms
153 $ make CFG_CRYPTO=n # disable all algorithms
154
Etienne Carriere96b96c22025-03-27 18:47:11 +0100155Please refer to `core/crypto.mk` for the list of all supported
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200156variables.
157
158Note that the application interface is **not** modified when algorithms are
159disabled. This means, for instance, that the functions ``TEE_CipherInit()``,
160``TEE_CipherUpdate()`` and ``TEE_CipherFinal()`` would remain present in
161``libutee.a`` even if all symmetric ciphers are disabled (they would simply
162return ``TEE_ERROR_NOT_IMPLEMENTED``).
163
Jorge Ramirez-Ortizb322f562022-02-17 12:09:29 +0100164Add a new software based crypto implementation
165**********************************************
166To add a new software based implementation, the default one in
167`core/lib/libtomcrypt`_ in combination with what is in `core/crypto`_ should be
168used as a reference. Here are the main things to consider when adding a new
169crypto provider:
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200170
171 - Put all the new code in its own directory under ``core/lib`` unless it is
172 code that will be used regardless of which crypto provider is in use. How
173 we are dealing with AES-GCM in `core/crypto`_ could serve as an example.
174
175 - Avoid modifying tee_svc_cryp.c_. It should not be needed.
176
177 - Although not all crypto families need to be defined, all are required for
178 compliance to the GlobalPlatform specification.
179
180 - If you intend to make some algorithms optional, please try to re-use the
181 same names for configuration variables as the default implementation.
182
Etienne Carrierec5b978f2025-03-28 14:06:25 +0100183Support for crypto IC
184*********************
Jorge Ramirez-Ortizb322f562022-02-17 12:09:29 +0100185Some cryptographic co-processors and secure elements are supported under a
186Generic Cryptographic Driver interface, connecting the TEE Crypto generic APIs
187to the HW driver interface. This interface is in
188`core/drivers/crypto/crypto_api`_ and should be followed when adding support for
189new devices.
190
191At the time of writing, OP-TEE does not support the `GP TEE Secure Element API`_
192and therefore the access to the secure element - the NXP EdgeLock® SE05x -
193follows the Cryptographic Operations API presenting a single session to the
194device. This session is shared with the normal world through the PKCS#11
195interface but also through a more generic interface (`libseetec`_) which allows
196clients to send Application Protocol Data Units (APDUs) directly to the device.
197
198Notice that cryptographic co-processors do not necessarily comply with all the
199GP requirements tested and covered by the OP-TEE sanity test suite
200(`optee_test`_). In those cases where the cryptographic operations are not
201supported - i.e: the SE05x does not implement all RSA key sizes - we opted for
202disabling those particular tests at build time rather than letting them fail.
203
Jorge Ramirez-Ortizd29554c2024-02-13 09:30:21 +0100204Some cryptographic co-processors may have limitations regarding the
205range of key sizes and supported ciphers. For instance, the AMD/Xilinx
206Versal ACAP Cryptographic driver may have constraints on key sizes,
207while NXP SE5X HSM modules may lack support for RSA or ECC. In such
208cases, especially when dealing with unsupported key sizes, it may be
209necessary to resort to a software implementation of the cipher,
210typically utilizing LibTomCrypt.
211
212.. note::
213 While the Hardware Security Modules or Cryptographic hardware
214 processors supported by OP-TEE may achieve FIPS 140-2 certification
215 at level 3, the software implementations of certain algorithms that
216 OP-TEE may fallback to cannot attain certification beyond level 2.
217
Jorge Ramirez-Ortiz91cb2b92022-12-09 12:25:17 +0100218NXP SE05X Family of Secure Elements
219***********************************
220
221This family of I2C bus devices are supported through the se050 cryptographic driver
222located at `core/drivers/crypto/se050`_. Before the REE boots, the session with
223the device is established using one of the OP-TEE supported I2C platform device
224drivers. Once the REE is up, the cryptographic driver can be configured to use
225the I2C driver in the REE (via RPC service) or continue using the one in OP-TEE.
226
227Unless the Secure Element owns the I2C bus (no other elements on the bus, no
228runtime-PM and so forth), it is recommended to route all traffic via the Normal
229World. Initial communication with the device is not data intensive and therefore
230slow I2C drivers - perhaps those not using DMA channels - do not represent much
231of a performance drag; the situation changes once clients start hammering the
232device.
233
234If using the REE for I2C transfers, it is also **imperative** to configure the
235driver so that the `GP Secure Channel Protocol 03`_ is enabled prior to exiting the
236Secure World; this way all communication between the processor and the secure
237element is encrypted and MAC authenticated. Please check the usage of the
238``CFG_CORE_SE05X_SCP03_EARLY`` configuration option.
239
240Aside of the secure element integration as an OP-TEE cryptographic driver,
241OP-TEE also presents an Application Protocol Data Units (APDU) interface to
242users via its OP-TEE client.
243
244.. figure:: ../images/crypto/drivers/se050_apdu_pta_interface.png
245 :figclass: align-center
246
247 Access to the Secure Element from libseetec and the APDU PTA.
248
249Using this interface, priviledged applications can control the Secure Element to
250inject or delete keys or certificates, encrypt, decrypt, sign and verify data
251and so forth. An application implementing a subset of those functions can be
252seen in this Foundries.io repository: `fio-se05x-cli`_
253
254This reference code is not fully functional in mainline as it's not yet possible
255to import keys and certificates from the Secure Element into OP-TEE's PKCS#11
256implementation. However, a user could still clear the Secure Element NVM memory
257and read certificates stored in it.
258
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200259.. Source files
260.. _core/crypto: https://github.com/OP-TEE/optee_os/blob/master/core/crypto
Jorge Ramirez-Ortizb322f562022-02-17 12:09:29 +0100261.. _core/drivers/crypto/crypto_api: https://github.com/OP-TEE/optee_os/blob/master/core/drivers/crypto/crypto_api
Jorge Ramirez-Ortiz91cb2b92022-12-09 12:25:17 +0100262.. _core/drivers/crypto/se050: https://github.com/OP-TEE/optee_os/blob/master/core/drivers/crypto/se050
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200263.. _crypto.c: https://github.com/OP-TEE/optee_os/blob/master/core/crypto/crypto.c
264.. _crypto.h: https://github.com/OP-TEE/optee_os/blob/master/core/include/crypto/crypto.h
265.. _core/lib/libtomcrypt: https://github.com/OP-TEE/optee_os/blob/master/core/lib/libtomcrypt
266.. _core/lib/libtomcrypt/sub.mk: https://github.com/OP-TEE/optee_os/blob/master/core/lib/libtomcrypt/sub.mk
Jorge Ramirez-Ortizb322f562022-02-17 12:09:29 +0100267.. _libseetec: https://github.com/OP-TEE/optee_client/commit/f4f54e5a76641fda22a49f00294771f948cd4c92
268.. _optee_test: https://github.com/OP-TEE/optee_test
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200269.. _tee_api_operations.c: https://github.com/OP-TEE/optee_os/blob/master/lib/libutee/tee_api_operations.c
270.. _tee_svc_cryp.c: https://github.com/OP-TEE/optee_os/blob/master/core/tee/tee_svc_cryp.c
271.. _tee_svc_cryp.h: https://github.com/OP-TEE/optee_os/blob/master/core/include/tee/tee_svc_cryp.h
272.. _utee_syscalls.h: https://github.com/OP-TEE/optee_os/blob/master/lib/libutee/include/utee_syscalls.h
273.. _utee_syscalls_asm.S: https://github.com/OP-TEE/optee_os/blob/master/lib/libutee/arch/arm/utee_syscalls_asm.S
274
275.. Other links:
Jorge Ramirez-Ortiz91cb2b92022-12-09 12:25:17 +0100276.. _fio-se05x-cli: https://github.com/foundriesio/fio-se05x-cli
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200277.. _LibTomCrypt: https://github.com/libtom/libtomcrypt
Jorge Ramirez-Ortizb322f562022-02-17 12:09:29 +0100278.. _GP TEE Secure Element API: https://globalplatform.org/specs-library/tee-secure-element-api/
Jorge Ramirez-Ortiz91cb2b92022-12-09 12:25:17 +0100279.. _GP Secure Channel Protocol 03: https://globalplatform.org/wp-content/uploads/2019/03/GPC_2.2_D_SCP03_v1.0.pdf