blob: 29189b4371148c8fba8d5b3e4f471519820f35a3 [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
26[1] The TEE Cryptographic Operations API
27****************************************
28OP-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
47[2] The crypto services
48***********************
49All 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
60 compile-time to save space. See `LibTomCrypt` below.
61
62[3] crypto_*()
63**************
64The ``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
131
132[4] LibTomCrypt
133***************
134Some algorithms may be disabled at compile time if they are not needed, in order
135to reduce the size of the OP-TEE image and reduces its memory usage. This is
136done by setting the appropriate configuration variable. For example:
137
138.. code-block:: bash
139
140 $ make CFG_CRYPTO_AES=n # disable AES only
141 $ make CFG_CRYPTO_{AES,DES}=n # disable symmetric ciphers
142 $ make CFG_CRYPTO_{DSA,RSA,DH,ECC}=n # disable public key algorithms
143 $ make CFG_CRYPTO=n # disable all algorithms
144
145Please refer to `core/lib/libtomcrypt/sub.mk`_ for the list of all supported
146variables.
147
148Note that the application interface is **not** modified when algorithms are
149disabled. This means, for instance, that the functions ``TEE_CipherInit()``,
150``TEE_CipherUpdate()`` and ``TEE_CipherFinal()`` would remain present in
151``libutee.a`` even if all symmetric ciphers are disabled (they would simply
152return ``TEE_ERROR_NOT_IMPLEMENTED``).
153
Jorge Ramirez-Ortizb322f562022-02-17 12:09:29 +0100154Add a new software based crypto implementation
155**********************************************
156To add a new software based implementation, the default one in
157`core/lib/libtomcrypt`_ in combination with what is in `core/crypto`_ should be
158used as a reference. Here are the main things to consider when adding a new
159crypto provider:
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200160
161 - Put all the new code in its own directory under ``core/lib`` unless it is
162 code that will be used regardless of which crypto provider is in use. How
163 we are dealing with AES-GCM in `core/crypto`_ could serve as an example.
164
165 - Avoid modifying tee_svc_cryp.c_. It should not be needed.
166
167 - Although not all crypto families need to be defined, all are required for
168 compliance to the GlobalPlatform specification.
169
170 - If you intend to make some algorithms optional, please try to re-use the
171 same names for configuration variables as the default implementation.
172
Jorge Ramirez-Ortizb322f562022-02-17 12:09:29 +0100173[5] Support for crypto IC
174*************************
175Some cryptographic co-processors and secure elements are supported under a
176Generic Cryptographic Driver interface, connecting the TEE Crypto generic APIs
177to the HW driver interface. This interface is in
178`core/drivers/crypto/crypto_api`_ and should be followed when adding support for
179new devices.
180
181At the time of writing, OP-TEE does not support the `GP TEE Secure Element API`_
182and therefore the access to the secure element - the NXP EdgeLock® SE05x -
183follows the Cryptographic Operations API presenting a single session to the
184device. This session is shared with the normal world through the PKCS#11
185interface but also through a more generic interface (`libseetec`_) which allows
186clients to send Application Protocol Data Units (APDUs) directly to the device.
187
188Notice that cryptographic co-processors do not necessarily comply with all the
189GP requirements tested and covered by the OP-TEE sanity test suite
190(`optee_test`_). In those cases where the cryptographic operations are not
191supported - i.e: the SE05x does not implement all RSA key sizes - we opted for
192disabling those particular tests at build time rather than letting them fail.
193
Jorge Ramirez-Ortizd29554c2024-02-13 09:30:21 +0100194Some cryptographic co-processors may have limitations regarding the
195range of key sizes and supported ciphers. For instance, the AMD/Xilinx
196Versal ACAP Cryptographic driver may have constraints on key sizes,
197while NXP SE5X HSM modules may lack support for RSA or ECC. In such
198cases, especially when dealing with unsupported key sizes, it may be
199necessary to resort to a software implementation of the cipher,
200typically utilizing LibTomCrypt.
201
202.. note::
203 While the Hardware Security Modules or Cryptographic hardware
204 processors supported by OP-TEE may achieve FIPS 140-2 certification
205 at level 3, the software implementations of certain algorithms that
206 OP-TEE may fallback to cannot attain certification beyond level 2.
207
Jorge Ramirez-Ortiz91cb2b92022-12-09 12:25:17 +0100208NXP SE05X Family of Secure Elements
209***********************************
210
211This family of I2C bus devices are supported through the se050 cryptographic driver
212located at `core/drivers/crypto/se050`_. Before the REE boots, the session with
213the device is established using one of the OP-TEE supported I2C platform device
214drivers. Once the REE is up, the cryptographic driver can be configured to use
215the I2C driver in the REE (via RPC service) or continue using the one in OP-TEE.
216
217Unless the Secure Element owns the I2C bus (no other elements on the bus, no
218runtime-PM and so forth), it is recommended to route all traffic via the Normal
219World. Initial communication with the device is not data intensive and therefore
220slow I2C drivers - perhaps those not using DMA channels - do not represent much
221of a performance drag; the situation changes once clients start hammering the
222device.
223
224If using the REE for I2C transfers, it is also **imperative** to configure the
225driver so that the `GP Secure Channel Protocol 03`_ is enabled prior to exiting the
226Secure World; this way all communication between the processor and the secure
227element is encrypted and MAC authenticated. Please check the usage of the
228``CFG_CORE_SE05X_SCP03_EARLY`` configuration option.
229
230Aside of the secure element integration as an OP-TEE cryptographic driver,
231OP-TEE also presents an Application Protocol Data Units (APDU) interface to
232users via its OP-TEE client.
233
234.. figure:: ../images/crypto/drivers/se050_apdu_pta_interface.png
235 :figclass: align-center
236
237 Access to the Secure Element from libseetec and the APDU PTA.
238
239Using this interface, priviledged applications can control the Secure Element to
240inject or delete keys or certificates, encrypt, decrypt, sign and verify data
241and so forth. An application implementing a subset of those functions can be
242seen in this Foundries.io repository: `fio-se05x-cli`_
243
244This reference code is not fully functional in mainline as it's not yet possible
245to import keys and certificates from the Secure Element into OP-TEE's PKCS#11
246implementation. However, a user could still clear the Secure Element NVM memory
247and read certificates stored in it.
248
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200249.. Source files
250.. _core/crypto: https://github.com/OP-TEE/optee_os/blob/master/core/crypto
Jorge Ramirez-Ortizb322f562022-02-17 12:09:29 +0100251.. _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 +0100252.. _core/drivers/crypto/se050: https://github.com/OP-TEE/optee_os/blob/master/core/drivers/crypto/se050
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200253.. _crypto.c: https://github.com/OP-TEE/optee_os/blob/master/core/crypto/crypto.c
254.. _crypto.h: https://github.com/OP-TEE/optee_os/blob/master/core/include/crypto/crypto.h
255.. _core/lib/libtomcrypt: https://github.com/OP-TEE/optee_os/blob/master/core/lib/libtomcrypt
256.. _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 +0100257.. _libseetec: https://github.com/OP-TEE/optee_client/commit/f4f54e5a76641fda22a49f00294771f948cd4c92
258.. _optee_test: https://github.com/OP-TEE/optee_test
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200259.. _tee_api_operations.c: https://github.com/OP-TEE/optee_os/blob/master/lib/libutee/tee_api_operations.c
260.. _tee_svc_cryp.c: https://github.com/OP-TEE/optee_os/blob/master/core/tee/tee_svc_cryp.c
261.. _tee_svc_cryp.h: https://github.com/OP-TEE/optee_os/blob/master/core/include/tee/tee_svc_cryp.h
262.. _utee_syscalls.h: https://github.com/OP-TEE/optee_os/blob/master/lib/libutee/include/utee_syscalls.h
263.. _utee_syscalls_asm.S: https://github.com/OP-TEE/optee_os/blob/master/lib/libutee/arch/arm/utee_syscalls_asm.S
264
265.. Other links:
Jorge Ramirez-Ortiz91cb2b92022-12-09 12:25:17 +0100266.. _fio-se05x-cli: https://github.com/foundriesio/fio-se05x-cli
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200267.. _LibTomCrypt: https://github.com/libtom/libtomcrypt
Jorge Ramirez-Ortizb322f562022-02-17 12:09:29 +0100268.. _GP TEE Secure Element API: https://globalplatform.org/specs-library/tee-secure-element-api/
Jorge Ramirez-Ortiz91cb2b92022-12-09 12:25:17 +0100269.. _GP Secure Channel Protocol 03: https://globalplatform.org/wp-content/uploads/2019/03/GPC_2.2_D_SCP03_v1.0.pdf