xref: /arm-trusted-firmware/docs/design/auth-framework.rst (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700SchasingluluAuthentication Framework & Chain of Trust
2*91f16700Schasinglulu=========================================
3*91f16700Schasinglulu
4*91f16700SchasingluluThe aim of this document is to describe the authentication framework
5*91f16700Schasingluluimplemented in Trusted Firmware-A (TF-A). This framework fulfills the
6*91f16700Schasinglulufollowing requirements:
7*91f16700Schasinglulu
8*91f16700Schasinglulu#. It should be possible for a platform port to specify the Chain of Trust in
9*91f16700Schasinglulu   terms of certificate hierarchy and the mechanisms used to verify a
10*91f16700Schasinglulu   particular image/certificate.
11*91f16700Schasinglulu
12*91f16700Schasinglulu#. The framework should distinguish between:
13*91f16700Schasinglulu
14*91f16700Schasinglulu   -  The mechanism used to encode and transport information, e.g. DER encoded
15*91f16700Schasinglulu      X.509v3 certificates to ferry Subject Public Keys, hashes and non-volatile
16*91f16700Schasinglulu      counters.
17*91f16700Schasinglulu
18*91f16700Schasinglulu   -  The mechanism used to verify the transported information i.e. the
19*91f16700Schasinglulu      cryptographic libraries.
20*91f16700Schasinglulu
21*91f16700SchasingluluThe framework has been designed following a modular approach illustrated in the
22*91f16700Schasinglulunext diagram:
23*91f16700Schasinglulu
24*91f16700Schasinglulu::
25*91f16700Schasinglulu
26*91f16700Schasinglulu        +---------------+---------------+------------+
27*91f16700Schasinglulu        | Trusted       | Trusted       | Trusted    |
28*91f16700Schasinglulu        | Firmware      | Firmware      | Firmware   |
29*91f16700Schasinglulu        | Generic       | IO Framework  | Platform   |
30*91f16700Schasinglulu        | Code i.e.     | (IO)          | Port       |
31*91f16700Schasinglulu        | BL1/BL2 (GEN) |               | (PP)       |
32*91f16700Schasinglulu        +---------------+---------------+------------+
33*91f16700Schasinglulu               ^               ^               ^
34*91f16700Schasinglulu               |               |               |
35*91f16700Schasinglulu               v               v               v
36*91f16700Schasinglulu         +-----------+   +-----------+   +-----------+
37*91f16700Schasinglulu         |           |   |           |   | Image     |
38*91f16700Schasinglulu         | Crypto    |   | Auth      |   | Parser    |
39*91f16700Schasinglulu         | Module    |<->| Module    |<->| Module    |
40*91f16700Schasinglulu         | (CM)      |   | (AM)      |   | (IPM)     |
41*91f16700Schasinglulu         |           |   |           |   |           |
42*91f16700Schasinglulu         +-----------+   +-----------+   +-----------+
43*91f16700Schasinglulu               ^                               ^
44*91f16700Schasinglulu               |                               |
45*91f16700Schasinglulu               v                               v
46*91f16700Schasinglulu        +----------------+             +-----------------+
47*91f16700Schasinglulu        | Cryptographic  |             | Image Parser    |
48*91f16700Schasinglulu        | Libraries (CL) |             | Libraries (IPL) |
49*91f16700Schasinglulu        +----------------+             +-----------------+
50*91f16700Schasinglulu                      |                |
51*91f16700Schasinglulu                      |                |
52*91f16700Schasinglulu                      |                |
53*91f16700Schasinglulu                      v                v
54*91f16700Schasinglulu                     +-----------------+
55*91f16700Schasinglulu                     | Misc. Libs e.g. |
56*91f16700Schasinglulu                     | ASN.1 decoder   |
57*91f16700Schasinglulu                     |                 |
58*91f16700Schasinglulu                     +-----------------+
59*91f16700Schasinglulu
60*91f16700Schasinglulu        DIAGRAM 1.
61*91f16700Schasinglulu
62*91f16700SchasingluluThis document describes the inner details of the authentication framework and
63*91f16700Schasingluluthe abstraction mechanisms available to specify a Chain of Trust.
64*91f16700Schasinglulu
65*91f16700SchasingluluFramework design
66*91f16700Schasinglulu----------------
67*91f16700Schasinglulu
68*91f16700SchasingluluThis section describes some aspects of the framework design and the rationale
69*91f16700Schasinglulubehind them. These aspects are key to verify a Chain of Trust.
70*91f16700Schasinglulu
71*91f16700SchasingluluChain of Trust
72*91f16700Schasinglulu~~~~~~~~~~~~~~
73*91f16700Schasinglulu
74*91f16700SchasingluluA CoT is basically a sequence of authentication images which usually starts with
75*91f16700Schasinglulua root of trust and culminates in a single data image. The following diagram
76*91f16700Schasingluluillustrates how this maps to a CoT for the BL31 image described in the
77*91f16700Schasinglulu`TBBR-Client specification`_.
78*91f16700Schasinglulu
79*91f16700Schasinglulu::
80*91f16700Schasinglulu
81*91f16700Schasinglulu        +------------------+       +-------------------+
82*91f16700Schasinglulu        | ROTPK/ROTPK Hash |------>| Trusted Key       |
83*91f16700Schasinglulu        +------------------+       | Certificate       |
84*91f16700Schasinglulu                                   | (Auth Image)      |
85*91f16700Schasinglulu                                  /+-------------------+
86*91f16700Schasinglulu                                 /            |
87*91f16700Schasinglulu                                /             |
88*91f16700Schasinglulu                               /              |
89*91f16700Schasinglulu                              /               |
90*91f16700Schasinglulu                             L                v
91*91f16700Schasinglulu        +------------------+       +-------------------+
92*91f16700Schasinglulu        | Trusted World    |------>| BL31 Key          |
93*91f16700Schasinglulu        | Public Key       |       | Certificate       |
94*91f16700Schasinglulu        +------------------+       | (Auth Image)      |
95*91f16700Schasinglulu                                   +-------------------+
96*91f16700Schasinglulu                                  /           |
97*91f16700Schasinglulu                                 /            |
98*91f16700Schasinglulu                                /             |
99*91f16700Schasinglulu                               /              |
100*91f16700Schasinglulu                              /               v
101*91f16700Schasinglulu        +------------------+ L     +-------------------+
102*91f16700Schasinglulu        | BL31 Content     |------>| BL31 Content      |
103*91f16700Schasinglulu        | Certificate PK   |       | Certificate       |
104*91f16700Schasinglulu        +------------------+       | (Auth Image)      |
105*91f16700Schasinglulu                                   +-------------------+
106*91f16700Schasinglulu                                  /           |
107*91f16700Schasinglulu                                 /            |
108*91f16700Schasinglulu                                /             |
109*91f16700Schasinglulu                               /              |
110*91f16700Schasinglulu                              /               v
111*91f16700Schasinglulu        +------------------+ L     +-------------------+
112*91f16700Schasinglulu        | BL31 Hash        |------>| BL31 Image        |
113*91f16700Schasinglulu        |                  |       | (Data Image)      |
114*91f16700Schasinglulu        +------------------+       |                   |
115*91f16700Schasinglulu                                   +-------------------+
116*91f16700Schasinglulu
117*91f16700Schasinglulu        DIAGRAM 2.
118*91f16700Schasinglulu
119*91f16700SchasingluluThe root of trust is usually a public key (ROTPK) that has been burnt in the
120*91f16700Schasingluluplatform and cannot be modified.
121*91f16700Schasinglulu
122*91f16700SchasingluluImage types
123*91f16700Schasinglulu~~~~~~~~~~~
124*91f16700Schasinglulu
125*91f16700SchasingluluImages in a CoT are categorised as authentication and data images. An
126*91f16700Schasingluluauthentication image contains information to authenticate a data image or
127*91f16700Schasingluluanother authentication image. A data image is usually a boot loader binary, but
128*91f16700Schasingluluit could be any other data that requires authentication.
129*91f16700Schasinglulu
130*91f16700SchasingluluComponent responsibilities
131*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~
132*91f16700Schasinglulu
133*91f16700SchasingluluFor every image in a Chain of Trust, the following high level operations are
134*91f16700Schasingluluperformed to verify it:
135*91f16700Schasinglulu
136*91f16700Schasinglulu#. Allocate memory for the image either statically or at runtime.
137*91f16700Schasinglulu
138*91f16700Schasinglulu#. Identify the image and load it in the allocated memory.
139*91f16700Schasinglulu
140*91f16700Schasinglulu#. Check the integrity of the image as per its type.
141*91f16700Schasinglulu
142*91f16700Schasinglulu#. Authenticate the image as per the cryptographic algorithms used.
143*91f16700Schasinglulu
144*91f16700Schasinglulu#. If the image is an authentication image, extract the information that will
145*91f16700Schasinglulu   be used to authenticate the next image in the CoT.
146*91f16700Schasinglulu
147*91f16700SchasingluluIn Diagram 1, each component is responsible for one or more of these operations.
148*91f16700SchasingluluThe responsibilities are briefly described below.
149*91f16700Schasinglulu
150*91f16700SchasingluluTF-A Generic code and IO framework (GEN/IO)
151*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
152*91f16700Schasinglulu
153*91f16700SchasingluluThese components are responsible for initiating the authentication process for a
154*91f16700Schasingluluparticular image in BL1 or BL2. For each BL image that requires authentication,
155*91f16700Schasingluluthe Generic code asks recursively the Authentication module what is the parent
156*91f16700Schasingluluimage until either an authenticated image or the ROT is reached. Then the
157*91f16700SchasingluluGeneric code calls the IO framework to load the image and calls the
158*91f16700SchasingluluAuthentication module to authenticate it, following the CoT from ROT to Image.
159*91f16700Schasinglulu
160*91f16700SchasingluluTF-A Platform Port (PP)
161*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^
162*91f16700Schasinglulu
163*91f16700SchasingluluThe platform is responsible for:
164*91f16700Schasinglulu
165*91f16700Schasinglulu#. Specifying the CoT for each image that needs to be authenticated. Details of
166*91f16700Schasinglulu   how a CoT can be specified by the platform are explained later. The platform
167*91f16700Schasinglulu   also specifies the authentication methods and the parsing method used for
168*91f16700Schasinglulu   each image.
169*91f16700Schasinglulu
170*91f16700Schasinglulu#. Statically allocating memory for each parameter in each image which is
171*91f16700Schasinglulu   used for verifying the CoT, e.g. memory for public keys, hashes etc.
172*91f16700Schasinglulu
173*91f16700Schasinglulu#. Providing the ROTPK or a hash of it.
174*91f16700Schasinglulu
175*91f16700Schasinglulu#. Providing additional information to the IPM to enable it to identify and
176*91f16700Schasinglulu   extract authentication parameters contained in an image, e.g. if the
177*91f16700Schasinglulu   parameters are stored as X509v3 extensions, the corresponding OID must be
178*91f16700Schasinglulu   provided.
179*91f16700Schasinglulu
180*91f16700Schasinglulu#. Fulfill any other memory requirements of the IPM and the CM (not currently
181*91f16700Schasinglulu   described in this document).
182*91f16700Schasinglulu
183*91f16700Schasinglulu#. Export functions to verify an image which uses an authentication method that
184*91f16700Schasinglulu   cannot be interpreted by the CM, e.g. if an image has to be verified using a
185*91f16700Schasinglulu   NV counter, then the value of the counter to compare with can only be
186*91f16700Schasinglulu   provided by the platform.
187*91f16700Schasinglulu
188*91f16700Schasinglulu#. Export a custom IPM if a proprietary image format is being used (described
189*91f16700Schasinglulu   later).
190*91f16700Schasinglulu
191*91f16700SchasingluluAuthentication Module (AM)
192*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^
193*91f16700Schasinglulu
194*91f16700SchasingluluIt is responsible for:
195*91f16700Schasinglulu
196*91f16700Schasinglulu#. Providing the necessary abstraction mechanisms to describe a CoT. Amongst
197*91f16700Schasinglulu   other things, the authentication and image parsing methods must be specified
198*91f16700Schasinglulu   by the PP in the CoT.
199*91f16700Schasinglulu
200*91f16700Schasinglulu#. Verifying the CoT passed by GEN by utilising functionality exported by the
201*91f16700Schasinglulu   PP, IPM and CM.
202*91f16700Schasinglulu
203*91f16700Schasinglulu#. Tracking which images have been verified. In case an image is a part of
204*91f16700Schasinglulu   multiple CoTs then it should be verified only once e.g. the Trusted World
205*91f16700Schasinglulu   Key Certificate in the TBBR-Client spec. contains information to verify
206*91f16700Schasinglulu   SCP_BL2, BL31, BL32 each of which have a separate CoT. (This
207*91f16700Schasinglulu   responsibility has not been described in this document but should be
208*91f16700Schasinglulu   trivial to implement).
209*91f16700Schasinglulu
210*91f16700Schasinglulu#. Reusing memory meant for a data image to verify authentication images e.g.
211*91f16700Schasinglulu   in the CoT described in Diagram 2, each certificate can be loaded and
212*91f16700Schasinglulu   verified in the memory reserved by the platform for the BL31 image. By the
213*91f16700Schasinglulu   time BL31 (the data image) is loaded, all information to authenticate it
214*91f16700Schasinglulu   will have been extracted from the parent image i.e. BL31 content
215*91f16700Schasinglulu   certificate. It is assumed that the size of an authentication image will
216*91f16700Schasinglulu   never exceed the size of a data image. It should be possible to verify this
217*91f16700Schasinglulu   at build time using asserts.
218*91f16700Schasinglulu
219*91f16700SchasingluluCryptographic Module (CM)
220*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^
221*91f16700Schasinglulu
222*91f16700SchasingluluThe CM is responsible for providing an API to:
223*91f16700Schasinglulu
224*91f16700Schasinglulu#. Verify a digital signature.
225*91f16700Schasinglulu#. Verify a hash.
226*91f16700Schasinglulu
227*91f16700SchasingluluThe CM does not include any cryptography related code, but it relies on an
228*91f16700Schasingluluexternal library to perform the cryptographic operations. A Crypto-Library (CL)
229*91f16700Schasinglululinking the CM and the external library must be implemented. The following
230*91f16700Schasinglulufunctions must be provided by the CL:
231*91f16700Schasinglulu
232*91f16700Schasinglulu.. code:: c
233*91f16700Schasinglulu
234*91f16700Schasinglulu    void (*init)(void);
235*91f16700Schasinglulu    int (*verify_signature)(void *data_ptr, unsigned int data_len,
236*91f16700Schasinglulu                            void *sig_ptr, unsigned int sig_len,
237*91f16700Schasinglulu                            void *sig_alg, unsigned int sig_alg_len,
238*91f16700Schasinglulu                            void *pk_ptr, unsigned int pk_len);
239*91f16700Schasinglulu    int (*calc_hash)(enum crypto_md_algo alg, void *data_ptr,
240*91f16700Schasinglulu                     unsigned int data_len,
241*91f16700Schasinglulu                     unsigned char output[CRYPTO_MD_MAX_SIZE])
242*91f16700Schasinglulu    int (*verify_hash)(void *data_ptr, unsigned int data_len,
243*91f16700Schasinglulu                       void *digest_info_ptr, unsigned int digest_info_len);
244*91f16700Schasinglulu    int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr,
245*91f16700Schasinglulu                        size_t len, const void *key, unsigned int key_len,
246*91f16700Schasinglulu                        unsigned int key_flags, const void *iv,
247*91f16700Schasinglulu                        unsigned int iv_len, const void *tag,
248*91f16700Schasinglulu                        unsigned int tag_len);
249*91f16700Schasinglulu
250*91f16700SchasingluluThese functions are registered in the CM using the macro:
251*91f16700Schasinglulu
252*91f16700Schasinglulu.. code:: c
253*91f16700Schasinglulu
254*91f16700Schasinglulu    REGISTER_CRYPTO_LIB(_name,
255*91f16700Schasinglulu                        _init,
256*91f16700Schasinglulu                        _verify_signature,
257*91f16700Schasinglulu                        _calc_hash,
258*91f16700Schasinglulu                        _verify_hash,
259*91f16700Schasinglulu                        _auth_decrypt,
260*91f16700Schasinglulu                        _convert_pk);
261*91f16700Schasinglulu
262*91f16700Schasinglulu``_name`` must be a string containing the name of the CL. This name is used for
263*91f16700Schasingluludebugging purposes.
264*91f16700Schasinglulu
265*91f16700SchasingluluCrypto module provides a function ``_calc_hash`` to calculate and
266*91f16700Schasinglulureturn the hash of the given data using the provided hash algorithm.
267*91f16700SchasingluluThis function is mainly used in the ``MEASURED_BOOT`` and ``DRTM_SUPPORT``
268*91f16700Schasinglulufeatures to calculate the hashes of various images/data.
269*91f16700Schasinglulu
270*91f16700SchasingluluOptionally, a platform function can be provided to convert public key
271*91f16700Schasinglulu(_convert_pk). It is only used if the platform saves a hash of the ROTPK.
272*91f16700SchasingluluMost platforms save the hash of the ROTPK, but some may save slightly different
273*91f16700Schasingluluinformation - e.g the hash of the ROTPK plus some related information.
274*91f16700SchasingluluDefining this function allows to transform the ROTPK used to verify
275*91f16700Schasingluluthe signature to the buffer (a platform specific public key) which
276*91f16700Schasingluluhash is saved in OTP.
277*91f16700Schasinglulu
278*91f16700Schasinglulu.. code:: c
279*91f16700Schasinglulu
280*91f16700Schasinglulu    int (*convert_pk)(void *full_pk_ptr, unsigned int full_pk_len,
281*91f16700Schasinglulu                      void **hashed_pk_ptr, unsigned int *hashed_pk_len);
282*91f16700Schasinglulu
283*91f16700Schasinglulu
284*91f16700Schasinglulu-  ``full_pk_ptr``: Pointer to Distinguished Encoding Rules (DER) ROTPK.
285*91f16700Schasinglulu-  ``full_pk_len``: DER ROTPK size.
286*91f16700Schasinglulu-  ``hashed_pk_ptr``: to return a pointer to a buffer, which hash should be the one saved in OTP.
287*91f16700Schasinglulu-  ``hashed_pk_len``: previous buffer size
288*91f16700Schasinglulu
289*91f16700SchasingluluImage Parser Module (IPM)
290*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^
291*91f16700Schasinglulu
292*91f16700SchasingluluThe IPM is responsible for:
293*91f16700Schasinglulu
294*91f16700Schasinglulu#. Checking the integrity of each image loaded by the IO framework.
295*91f16700Schasinglulu#. Extracting parameters used for authenticating an image based upon a
296*91f16700Schasinglulu   description provided by the platform in the CoT descriptor.
297*91f16700Schasinglulu
298*91f16700SchasingluluImages may have different formats (for example, authentication images could be
299*91f16700Schasinglulux509v3 certificates, signed ELF files or any other platform specific format).
300*91f16700SchasingluluThe IPM allows to register an Image Parser Library (IPL) for every image format
301*91f16700Schasingluluused in the CoT. This library must implement the specific methods to parse the
302*91f16700Schasingluluimage. The IPM obtains the image format from the CoT and calls the right IPL to
303*91f16700Schasinglulucheck the image integrity and extract the authentication parameters.
304*91f16700Schasinglulu
305*91f16700SchasingluluSee Section "Describing the image parsing methods" for more details about the
306*91f16700Schasinglulumechanism the IPM provides to define and register IPLs.
307*91f16700Schasinglulu
308*91f16700SchasingluluAuthentication methods
309*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~
310*91f16700Schasinglulu
311*91f16700SchasingluluThe AM supports the following authentication methods:
312*91f16700Schasinglulu
313*91f16700Schasinglulu#. Hash
314*91f16700Schasinglulu#. Digital signature
315*91f16700Schasinglulu
316*91f16700SchasingluluThe platform may specify these methods in the CoT in case it decides to define
317*91f16700Schasinglulua custom CoT instead of reusing a predefined one.
318*91f16700Schasinglulu
319*91f16700SchasingluluIf a data image uses multiple methods, then all the methods must be a part of
320*91f16700Schasingluluthe same CoT. The number and type of parameters are method specific. These
321*91f16700Schasingluluparameters should be obtained from the parent image using the IPM.
322*91f16700Schasinglulu
323*91f16700Schasinglulu#. Hash
324*91f16700Schasinglulu
325*91f16700Schasinglulu   Parameters:
326*91f16700Schasinglulu
327*91f16700Schasinglulu   #. A pointer to data to hash
328*91f16700Schasinglulu   #. Length of the data
329*91f16700Schasinglulu   #. A pointer to the hash
330*91f16700Schasinglulu   #. Length of the hash
331*91f16700Schasinglulu
332*91f16700Schasinglulu   The hash will be represented by the DER encoding of the following ASN.1
333*91f16700Schasinglulu   type:
334*91f16700Schasinglulu
335*91f16700Schasinglulu   ::
336*91f16700Schasinglulu
337*91f16700Schasinglulu       DigestInfo ::= SEQUENCE {
338*91f16700Schasinglulu           digestAlgorithm  DigestAlgorithmIdentifier,
339*91f16700Schasinglulu           digest           Digest
340*91f16700Schasinglulu       }
341*91f16700Schasinglulu
342*91f16700Schasinglulu   This ASN.1 structure makes it possible to remove any assumption about the
343*91f16700Schasinglulu   type of hash algorithm used as this information accompanies the hash. This
344*91f16700Schasinglulu   should allow the Cryptography Library (CL) to support multiple hash
345*91f16700Schasinglulu   algorithm implementations.
346*91f16700Schasinglulu
347*91f16700Schasinglulu#. Digital Signature
348*91f16700Schasinglulu
349*91f16700Schasinglulu   Parameters:
350*91f16700Schasinglulu
351*91f16700Schasinglulu   #. A pointer to data to sign
352*91f16700Schasinglulu   #. Length of the data
353*91f16700Schasinglulu   #. Public Key Algorithm
354*91f16700Schasinglulu   #. Public Key value
355*91f16700Schasinglulu   #. Digital Signature Algorithm
356*91f16700Schasinglulu   #. Digital Signature value
357*91f16700Schasinglulu
358*91f16700Schasinglulu   The Public Key parameters will be represented by the DER encoding of the
359*91f16700Schasinglulu   following ASN.1 type:
360*91f16700Schasinglulu
361*91f16700Schasinglulu   ::
362*91f16700Schasinglulu
363*91f16700Schasinglulu       SubjectPublicKeyInfo  ::=  SEQUENCE  {
364*91f16700Schasinglulu           algorithm         AlgorithmIdentifier{PUBLIC-KEY,{PublicKeyAlgorithms}},
365*91f16700Schasinglulu           subjectPublicKey  BIT STRING  }
366*91f16700Schasinglulu
367*91f16700Schasinglulu   The Digital Signature Algorithm will be represented by the DER encoding of
368*91f16700Schasinglulu   the following ASN.1 types.
369*91f16700Schasinglulu
370*91f16700Schasinglulu   ::
371*91f16700Schasinglulu
372*91f16700Schasinglulu       AlgorithmIdentifier {ALGORITHM:IOSet } ::= SEQUENCE {
373*91f16700Schasinglulu           algorithm         ALGORITHM.&id({IOSet}),
374*91f16700Schasinglulu           parameters        ALGORITHM.&Type({IOSet}{@algorithm}) OPTIONAL
375*91f16700Schasinglulu       }
376*91f16700Schasinglulu
377*91f16700Schasinglulu   The digital signature will be represented by:
378*91f16700Schasinglulu
379*91f16700Schasinglulu   ::
380*91f16700Schasinglulu
381*91f16700Schasinglulu       signature  ::=  BIT STRING
382*91f16700Schasinglulu
383*91f16700SchasingluluThe authentication framework will use the image descriptor to extract all the
384*91f16700Schasingluluinformation related to authentication.
385*91f16700Schasinglulu
386*91f16700SchasingluluSpecifying a Chain of Trust
387*91f16700Schasinglulu---------------------------
388*91f16700Schasinglulu
389*91f16700SchasingluluA CoT can be described as a set of image descriptors linked together in a
390*91f16700Schasingluluparticular order. The order dictates the sequence in which they must be
391*91f16700Schasingluluverified. Each image has a set of properties which allow the AM to verify it.
392*91f16700SchasingluluThese properties are described below.
393*91f16700Schasinglulu
394*91f16700SchasingluluThe PP is responsible for defining a single or multiple CoTs for a data image.
395*91f16700SchasingluluUnless otherwise specified, the data structures described in the following
396*91f16700Schasinglulusections are populated by the PP statically.
397*91f16700Schasinglulu
398*91f16700SchasingluluDescribing the image parsing methods
399*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
400*91f16700Schasinglulu
401*91f16700SchasingluluThe parsing method refers to the format of a particular image. For example, an
402*91f16700Schasingluluauthentication image that represents a certificate could be in the X.509v3
403*91f16700Schasingluluformat. A data image that represents a boot loader stage could be in raw binary
404*91f16700Schasingluluor ELF format. The IPM supports three parsing methods. An image has to use one
405*91f16700Schasingluluof the three methods described below. An IPL is responsible for interpreting a
406*91f16700Schasinglulusingle parsing method. There has to be one IPL for every method used by the
407*91f16700Schasingluluplatform.
408*91f16700Schasinglulu
409*91f16700Schasinglulu#. Raw format: This format is effectively a nop as an image using this method
410*91f16700Schasinglulu   is treated as being in raw binary format e.g. boot loader images used by
411*91f16700Schasinglulu   TF-A. This method should only be used by data images.
412*91f16700Schasinglulu
413*91f16700Schasinglulu#. X509V3 method: This method uses industry standards like X.509 to represent
414*91f16700Schasinglulu   PKI certificates (authentication images). It is expected that open source
415*91f16700Schasinglulu   libraries will be available which can be used to parse an image represented
416*91f16700Schasinglulu   by this method. Such libraries can be used to write the corresponding IPL
417*91f16700Schasinglulu   e.g. the X.509 parsing library code in mbed TLS.
418*91f16700Schasinglulu
419*91f16700Schasinglulu#. Platform defined method: This method caters for platform specific
420*91f16700Schasinglulu   proprietary standards to represent authentication or data images. For
421*91f16700Schasinglulu   example, The signature of a data image could be appended to the data image
422*91f16700Schasinglulu   raw binary. A header could be prepended to the combined blob to specify the
423*91f16700Schasinglulu   extents of each component. The platform will have to implement the
424*91f16700Schasinglulu   corresponding IPL to interpret such a format.
425*91f16700Schasinglulu
426*91f16700SchasingluluThe following enum can be used to define these three methods.
427*91f16700Schasinglulu
428*91f16700Schasinglulu.. code:: c
429*91f16700Schasinglulu
430*91f16700Schasinglulu    typedef enum img_type_enum {
431*91f16700Schasinglulu        IMG_RAW,            /* Binary image */
432*91f16700Schasinglulu        IMG_PLAT,           /* Platform specific format */
433*91f16700Schasinglulu        IMG_CERT,           /* X509v3 certificate */
434*91f16700Schasinglulu        IMG_MAX_TYPES,
435*91f16700Schasinglulu    } img_type_t;
436*91f16700Schasinglulu
437*91f16700SchasingluluAn IPL must provide functions with the following prototypes:
438*91f16700Schasinglulu
439*91f16700Schasinglulu.. code:: c
440*91f16700Schasinglulu
441*91f16700Schasinglulu    void init(void);
442*91f16700Schasinglulu    int check_integrity(void *img, unsigned int img_len);
443*91f16700Schasinglulu    int get_auth_param(const auth_param_type_desc_t *type_desc,
444*91f16700Schasinglulu                          void *img, unsigned int img_len,
445*91f16700Schasinglulu                          void **param, unsigned int *param_len);
446*91f16700Schasinglulu
447*91f16700SchasingluluAn IPL for each type must be registered using the following macro:
448*91f16700Schasinglulu
449*91f16700Schasinglulu.. code:: c
450*91f16700Schasinglulu
451*91f16700Schasinglulu    REGISTER_IMG_PARSER_LIB(_type, _name, _init, _check_int, _get_param)
452*91f16700Schasinglulu
453*91f16700Schasinglulu-  ``_type``: one of the types described above.
454*91f16700Schasinglulu-  ``_name``: a string containing the IPL name for debugging purposes.
455*91f16700Schasinglulu-  ``_init``: initialization function pointer.
456*91f16700Schasinglulu-  ``_check_int``: check image integrity function pointer.
457*91f16700Schasinglulu-  ``_get_param``: extract authentication parameter function pointer.
458*91f16700Schasinglulu
459*91f16700SchasingluluThe ``init()`` function will be used to initialize the IPL.
460*91f16700Schasinglulu
461*91f16700SchasingluluThe ``check_integrity()`` function is passed a pointer to the memory where the
462*91f16700Schasingluluimage has been loaded by the IO framework and the image length. It should ensure
463*91f16700Schasingluluthat the image is in the format corresponding to the parsing method and has not
464*91f16700Schasinglulubeen tampered with. For example, RFC-2459 describes a validation sequence for an
465*91f16700SchasingluluX.509 certificate.
466*91f16700Schasinglulu
467*91f16700SchasingluluThe ``get_auth_param()`` function is passed a parameter descriptor containing
468*91f16700Schasingluluinformation about the parameter (``type_desc`` and ``cookie``) to identify and
469*91f16700Schasingluluextract the data corresponding to that parameter from an image. This data will
470*91f16700Schasinglulube used to verify either the current or the next image in the CoT sequence.
471*91f16700Schasinglulu
472*91f16700SchasingluluEach image in the CoT will specify the parsing method it uses. This information
473*91f16700Schasingluluwill be used by the IPM to find the right parser descriptor for the image.
474*91f16700Schasinglulu
475*91f16700SchasingluluDescribing the authentication method(s)
476*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
477*91f16700Schasinglulu
478*91f16700SchasingluluAs part of the CoT, each image has to specify one or more authentication methods
479*91f16700Schasingluluwhich will be used to verify it. As described in the Section "Authentication
480*91f16700Schasinglulumethods", there are three methods supported by the AM.
481*91f16700Schasinglulu
482*91f16700Schasinglulu.. code:: c
483*91f16700Schasinglulu
484*91f16700Schasinglulu    typedef enum {
485*91f16700Schasinglulu        AUTH_METHOD_NONE,
486*91f16700Schasinglulu        AUTH_METHOD_HASH,
487*91f16700Schasinglulu        AUTH_METHOD_SIG,
488*91f16700Schasinglulu        AUTH_METHOD_NUM
489*91f16700Schasinglulu    } auth_method_type_t;
490*91f16700Schasinglulu
491*91f16700SchasingluluThe AM defines the type of each parameter used by an authentication method. It
492*91f16700Schasingluluuses this information to:
493*91f16700Schasinglulu
494*91f16700Schasinglulu#. Specify to the ``get_auth_param()`` function exported by the IPM, which
495*91f16700Schasinglulu   parameter should be extracted from an image.
496*91f16700Schasinglulu
497*91f16700Schasinglulu#. Correctly marshall the parameters while calling the verification function
498*91f16700Schasinglulu   exported by the CM and PP.
499*91f16700Schasinglulu
500*91f16700Schasinglulu#. Extract authentication parameters from a parent image in order to verify a
501*91f16700Schasinglulu   child image e.g. to verify the certificate image, the public key has to be
502*91f16700Schasinglulu   obtained from the parent image.
503*91f16700Schasinglulu
504*91f16700Schasinglulu.. code:: c
505*91f16700Schasinglulu
506*91f16700Schasinglulu    typedef enum {
507*91f16700Schasinglulu        AUTH_PARAM_NONE,
508*91f16700Schasinglulu        AUTH_PARAM_RAW_DATA,        /* Raw image data */
509*91f16700Schasinglulu        AUTH_PARAM_SIG,         /* The image signature */
510*91f16700Schasinglulu        AUTH_PARAM_SIG_ALG,     /* The image signature algorithm */
511*91f16700Schasinglulu        AUTH_PARAM_HASH,        /* A hash (including the algorithm) */
512*91f16700Schasinglulu        AUTH_PARAM_PUB_KEY,     /* A public key */
513*91f16700Schasinglulu    } auth_param_type_t;
514*91f16700Schasinglulu
515*91f16700SchasingluluThe AM defines the following structure to identify an authentication parameter
516*91f16700Schasinglulurequired to verify an image.
517*91f16700Schasinglulu
518*91f16700Schasinglulu.. code:: c
519*91f16700Schasinglulu
520*91f16700Schasinglulu    typedef struct auth_param_type_desc_s {
521*91f16700Schasinglulu        auth_param_type_t type;
522*91f16700Schasinglulu        void *cookie;
523*91f16700Schasinglulu    } auth_param_type_desc_t;
524*91f16700Schasinglulu
525*91f16700Schasinglulu``cookie`` is used by the platform to specify additional information to the IPM
526*91f16700Schasingluluwhich enables it to uniquely identify the parameter that should be extracted
527*91f16700Schasinglulufrom an image. For example, the hash of a BL3x image in its corresponding
528*91f16700Schasinglulucontent certificate is stored in an X509v3 custom extension field. An extension
529*91f16700Schasinglulufield can only be identified using an OID. In this case, the ``cookie`` could
530*91f16700Schasinglulucontain the pointer to the OID defined by the platform for the hash extension
531*91f16700Schasinglulufield while the ``type`` field could be set to ``AUTH_PARAM_HASH``. A value of 0 for
532*91f16700Schasingluluthe ``cookie`` field means that it is not used.
533*91f16700Schasinglulu
534*91f16700SchasingluluFor each method, the AM defines a structure with the parameters required to
535*91f16700Schasingluluverify the image.
536*91f16700Schasinglulu
537*91f16700Schasinglulu.. code:: c
538*91f16700Schasinglulu
539*91f16700Schasinglulu    /*
540*91f16700Schasinglulu     * Parameters for authentication by hash matching
541*91f16700Schasinglulu     */
542*91f16700Schasinglulu    typedef struct auth_method_param_hash_s {
543*91f16700Schasinglulu        auth_param_type_desc_t *data;   /* Data to hash */
544*91f16700Schasinglulu        auth_param_type_desc_t *hash;   /* Hash to match with */
545*91f16700Schasinglulu    } auth_method_param_hash_t;
546*91f16700Schasinglulu
547*91f16700Schasinglulu    /*
548*91f16700Schasinglulu     * Parameters for authentication by signature
549*91f16700Schasinglulu     */
550*91f16700Schasinglulu    typedef struct auth_method_param_sig_s {
551*91f16700Schasinglulu        auth_param_type_desc_t *pk; /* Public key */
552*91f16700Schasinglulu        auth_param_type_desc_t *sig;    /* Signature to check */
553*91f16700Schasinglulu        auth_param_type_desc_t *alg;    /* Signature algorithm */
554*91f16700Schasinglulu        auth_param_type_desc_t *tbs;    /* Data signed */
555*91f16700Schasinglulu    } auth_method_param_sig_t;
556*91f16700Schasinglulu
557*91f16700SchasingluluThe AM defines the following structure to describe an authentication method for
558*91f16700Schasingluluverifying an image
559*91f16700Schasinglulu
560*91f16700Schasinglulu.. code:: c
561*91f16700Schasinglulu
562*91f16700Schasinglulu    /*
563*91f16700Schasinglulu     * Authentication method descriptor
564*91f16700Schasinglulu     */
565*91f16700Schasinglulu    typedef struct auth_method_desc_s {
566*91f16700Schasinglulu        auth_method_type_t type;
567*91f16700Schasinglulu        union {
568*91f16700Schasinglulu            auth_method_param_hash_t hash;
569*91f16700Schasinglulu            auth_method_param_sig_t sig;
570*91f16700Schasinglulu        } param;
571*91f16700Schasinglulu    } auth_method_desc_t;
572*91f16700Schasinglulu
573*91f16700SchasingluluUsing the method type specified in the ``type`` field, the AM finds out what field
574*91f16700Schasingluluneeds to access within the ``param`` union.
575*91f16700Schasinglulu
576*91f16700SchasingluluStoring Authentication parameters
577*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
578*91f16700Schasinglulu
579*91f16700SchasingluluA parameter described by ``auth_param_type_desc_t`` to verify an image could be
580*91f16700Schasingluluobtained from either the image itself or its parent image. The memory allocated
581*91f16700Schasinglulufor loading the parent image will be reused for loading the child image. Hence
582*91f16700Schasingluluparameters which are obtained from the parent for verifying a child image need
583*91f16700Schasingluluto have memory allocated for them separately where they can be stored. This
584*91f16700Schasinglulumemory must be statically allocated by the platform port.
585*91f16700Schasinglulu
586*91f16700SchasingluluThe AM defines the following structure to store the data corresponding to an
587*91f16700Schasingluluauthentication parameter.
588*91f16700Schasinglulu
589*91f16700Schasinglulu.. code:: c
590*91f16700Schasinglulu
591*91f16700Schasinglulu    typedef struct auth_param_data_desc_s {
592*91f16700Schasinglulu        void *auth_param_ptr;
593*91f16700Schasinglulu        unsigned int auth_param_len;
594*91f16700Schasinglulu    } auth_param_data_desc_t;
595*91f16700Schasinglulu
596*91f16700SchasingluluThe ``auth_param_ptr`` field is initialized by the platform. The ``auth_param_len``
597*91f16700Schasinglulufield is used to specify the length of the data in the memory.
598*91f16700Schasinglulu
599*91f16700SchasingluluFor parameters that can be obtained from the child image itself, the IPM is
600*91f16700Schasingluluresponsible for populating the ``auth_param_ptr`` and ``auth_param_len`` fields
601*91f16700Schasingluluwhile executing the ``img_get_auth_param()`` function.
602*91f16700Schasinglulu
603*91f16700SchasingluluThe AM defines the following structure to enable an image to describe the
604*91f16700Schasingluluparameters that should be extracted from it and used to verify the next image
605*91f16700Schasinglulu(child) in a CoT.
606*91f16700Schasinglulu
607*91f16700Schasinglulu.. code:: c
608*91f16700Schasinglulu
609*91f16700Schasinglulu    typedef struct auth_param_desc_s {
610*91f16700Schasinglulu        auth_param_type_desc_t type_desc;
611*91f16700Schasinglulu        auth_param_data_desc_t data;
612*91f16700Schasinglulu    } auth_param_desc_t;
613*91f16700Schasinglulu
614*91f16700SchasingluluDescribing an image in a CoT
615*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~
616*91f16700Schasinglulu
617*91f16700SchasingluluAn image in a CoT is a consolidation of the following aspects of a CoT described
618*91f16700Schasingluluabove.
619*91f16700Schasinglulu
620*91f16700Schasinglulu#. A unique identifier specified by the platform which allows the IO framework
621*91f16700Schasinglulu   to locate the image in a FIP and load it in the memory reserved for the data
622*91f16700Schasinglulu   image in the CoT.
623*91f16700Schasinglulu
624*91f16700Schasinglulu#. A parsing method which is used by the AM to find the appropriate IPM.
625*91f16700Schasinglulu
626*91f16700Schasinglulu#. Authentication methods and their parameters as described in the previous
627*91f16700Schasinglulu   section. These are used to verify the current image.
628*91f16700Schasinglulu
629*91f16700Schasinglulu#. Parameters which are used to verify the next image in the current CoT. These
630*91f16700Schasinglulu   parameters are specified only by authentication images and can be extracted
631*91f16700Schasinglulu   from the current image once it has been verified.
632*91f16700Schasinglulu
633*91f16700SchasingluluThe following data structure describes an image in a CoT.
634*91f16700Schasinglulu
635*91f16700Schasinglulu.. code:: c
636*91f16700Schasinglulu
637*91f16700Schasinglulu    typedef struct auth_img_desc_s {
638*91f16700Schasinglulu        unsigned int img_id;
639*91f16700Schasinglulu        const struct auth_img_desc_s *parent;
640*91f16700Schasinglulu        img_type_t img_type;
641*91f16700Schasinglulu        const auth_method_desc_t *const img_auth_methods;
642*91f16700Schasinglulu        const auth_param_desc_t *const authenticated_data;
643*91f16700Schasinglulu    } auth_img_desc_t;
644*91f16700Schasinglulu
645*91f16700SchasingluluA CoT is defined as an array of pointers to ``auth_image_desc_t`` structures
646*91f16700Schasinglululinked together by the ``parent`` field. Those nodes with no parent must be
647*91f16700Schasingluluauthenticated using the ROTPK stored in the platform.
648*91f16700Schasinglulu
649*91f16700SchasingluluImplementation example
650*91f16700Schasinglulu----------------------
651*91f16700Schasinglulu
652*91f16700SchasingluluThis section is a detailed guide explaining a trusted boot implementation using
653*91f16700Schasingluluthe authentication framework. This example corresponds to the Applicative
654*91f16700SchasingluluFunctional Mode (AFM) as specified in the TBBR-Client document. It is
655*91f16700Schasinglulurecommended to read this guide along with the source code.
656*91f16700Schasinglulu
657*91f16700SchasingluluThe TBBR CoT
658*91f16700Schasinglulu~~~~~~~~~~~~
659*91f16700Schasinglulu
660*91f16700SchasingluluCoT specific to BL1 and BL2 can be found in ``drivers/auth/tbbr/tbbr_cot_bl1.c``
661*91f16700Schasingluluand ``drivers/auth/tbbr/tbbr_cot_bl2.c`` respectively. The common CoT used across
662*91f16700SchasingluluBL1 and BL2 can be found in ``drivers/auth/tbbr/tbbr_cot_common.c``.
663*91f16700SchasingluluThis CoT consists of an array of pointers to image descriptors and it is
664*91f16700Schasingluluregistered in the framework using the macro ``REGISTER_COT(cot_desc)``, where
665*91f16700Schasinglulu``cot_desc`` must be the name of the array (passing a pointer or any other
666*91f16700Schasinglulutype of indirection will cause the registration process to fail).
667*91f16700Schasinglulu
668*91f16700SchasingluluThe number of images participating in the boot process depends on the CoT.
669*91f16700SchasingluluThere is, however, a minimum set of images that are mandatory in TF-A and thus
670*91f16700Schasingluluall CoTs must present:
671*91f16700Schasinglulu
672*91f16700Schasinglulu-  ``BL2``
673*91f16700Schasinglulu-  ``SCP_BL2`` (platform specific)
674*91f16700Schasinglulu-  ``BL31``
675*91f16700Schasinglulu-  ``BL32`` (optional)
676*91f16700Schasinglulu-  ``BL33``
677*91f16700Schasinglulu
678*91f16700SchasingluluThe TBBR specifies the additional certificates that must accompany these images
679*91f16700Schasinglulufor a proper authentication. Details about the TBBR CoT may be found in the
680*91f16700Schasinglulu:ref:`Trusted Board Boot` document.
681*91f16700Schasinglulu
682*91f16700SchasingluluFollowing the :ref:`Porting Guide`, a platform must provide unique
683*91f16700Schasingluluidentifiers for all the images and certificates that will be loaded during the
684*91f16700Schasingluluboot process. If a platform is using the TBBR as a reference for trusted boot,
685*91f16700Schasingluluthese identifiers can be obtained from ``include/common/tbbr/tbbr_img_def.h``.
686*91f16700SchasingluluArm platforms include this file in ``include/plat/arm/common/arm_def.h``. Other
687*91f16700Schasingluluplatforms may also include this file or provide their own identifiers.
688*91f16700Schasinglulu
689*91f16700Schasinglulu**Important**: the authentication module uses these identifiers to index the
690*91f16700SchasingluluCoT array, so the descriptors location in the array must match the identifiers.
691*91f16700Schasinglulu
692*91f16700SchasingluluEach image descriptor must specify:
693*91f16700Schasinglulu
694*91f16700Schasinglulu-  ``img_id``: the corresponding image unique identifier defined by the platform.
695*91f16700Schasinglulu-  ``img_type``: the image parser module uses the image type to call the proper
696*91f16700Schasinglulu   parsing library to check the image integrity and extract the required
697*91f16700Schasinglulu   authentication parameters. Three types of images are currently supported:
698*91f16700Schasinglulu
699*91f16700Schasinglulu   -  ``IMG_RAW``: image is a raw binary. No parsing functions are available,
700*91f16700Schasinglulu      other than reading the whole image.
701*91f16700Schasinglulu   -  ``IMG_PLAT``: image format is platform specific. The platform may use this
702*91f16700Schasinglulu      type for custom images not directly supported by the authentication
703*91f16700Schasinglulu      framework.
704*91f16700Schasinglulu   -  ``IMG_CERT``: image is an x509v3 certificate.
705*91f16700Schasinglulu
706*91f16700Schasinglulu-  ``parent``: pointer to the parent image descriptor. The parent will contain
707*91f16700Schasinglulu   the information required to authenticate the current image. If the parent
708*91f16700Schasinglulu   is NULL, the authentication parameters will be obtained from the platform
709*91f16700Schasinglulu   (i.e. the BL2 and Trusted Key certificates are signed with the ROT private
710*91f16700Schasinglulu   key, whose public part is stored in the platform).
711*91f16700Schasinglulu-  ``img_auth_methods``: this points to an array which defines the
712*91f16700Schasinglulu   authentication methods that must be checked to consider an image
713*91f16700Schasinglulu   authenticated. Each method consists of a type and a list of parameter
714*91f16700Schasinglulu   descriptors. A parameter descriptor consists of a type and a cookie which
715*91f16700Schasinglulu   will point to specific information required to extract that parameter from
716*91f16700Schasinglulu   the image (i.e. if the parameter is stored in an x509v3 extension, the
717*91f16700Schasinglulu   cookie will point to the extension OID). Depending on the method type, a
718*91f16700Schasinglulu   different number of parameters must be specified. This pointer should not be
719*91f16700Schasinglulu   NULL.
720*91f16700Schasinglulu   Supported methods are:
721*91f16700Schasinglulu
722*91f16700Schasinglulu   -  ``AUTH_METHOD_HASH``: the hash of the image must match the hash extracted
723*91f16700Schasinglulu      from the parent image. The following parameter descriptors must be
724*91f16700Schasinglulu      specified:
725*91f16700Schasinglulu
726*91f16700Schasinglulu      -  ``data``: data to be hashed (obtained from current image)
727*91f16700Schasinglulu      -  ``hash``: reference hash (obtained from parent image)
728*91f16700Schasinglulu
729*91f16700Schasinglulu   -  ``AUTH_METHOD_SIG``: the image (usually a certificate) must be signed with
730*91f16700Schasinglulu      the private key whose public part is extracted from the parent image (or
731*91f16700Schasinglulu      the platform if the parent is NULL). The following parameter descriptors
732*91f16700Schasinglulu      must be specified:
733*91f16700Schasinglulu
734*91f16700Schasinglulu      -  ``pk``: the public key (obtained from parent image)
735*91f16700Schasinglulu      -  ``sig``: the digital signature (obtained from current image)
736*91f16700Schasinglulu      -  ``alg``: the signature algorithm used (obtained from current image)
737*91f16700Schasinglulu      -  ``data``: the data to be signed (obtained from current image)
738*91f16700Schasinglulu
739*91f16700Schasinglulu-  ``authenticated_data``: this array pointer indicates what authentication
740*91f16700Schasinglulu   parameters must be extracted from an image once it has been authenticated.
741*91f16700Schasinglulu   Each parameter consists of a parameter descriptor and the buffer
742*91f16700Schasinglulu   address/size to store the parameter. The CoT is responsible for allocating
743*91f16700Schasinglulu   the required memory to store the parameters. This pointer may be NULL.
744*91f16700Schasinglulu
745*91f16700SchasingluluIn the ``tbbr_cot*.c`` file, a set of buffers are allocated to store the parameters
746*91f16700Schasingluluextracted from the certificates. In the case of the TBBR CoT, these parameters
747*91f16700Schasingluluare hashes and public keys. In DER format, an RSA-4096 public key requires 550
748*91f16700Schasinglulubytes, and a hash requires 51 bytes. Depending on the CoT and the authentication
749*91f16700Schasingluluprocess, some of the buffers may be reused at different stages during the boot.
750*91f16700Schasinglulu
751*91f16700SchasingluluNext in that file, the parameter descriptors are defined. These descriptors will
752*91f16700Schasinglulube used to extract the parameter data from the corresponding image.
753*91f16700Schasinglulu
754*91f16700SchasingluluExample: the BL31 Chain of Trust
755*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
756*91f16700Schasinglulu
757*91f16700SchasingluluFour image descriptors form the BL31 Chain of Trust:
758*91f16700Schasinglulu
759*91f16700Schasinglulu.. code:: c
760*91f16700Schasinglulu
761*91f16700Schasinglulu    static const auth_img_desc_t trusted_key_cert = {
762*91f16700Schasinglulu            .img_id = TRUSTED_KEY_CERT_ID,
763*91f16700Schasinglulu            .img_type = IMG_CERT,
764*91f16700Schasinglulu            .parent = NULL,
765*91f16700Schasinglulu            .img_auth_methods =  (const auth_method_desc_t[AUTH_METHOD_NUM]) {
766*91f16700Schasinglulu                    [0] = {
767*91f16700Schasinglulu                            .type = AUTH_METHOD_SIG,
768*91f16700Schasinglulu                            .param.sig = {
769*91f16700Schasinglulu                                    .pk = &subject_pk,
770*91f16700Schasinglulu                                    .sig = &sig,
771*91f16700Schasinglulu                                    .alg = &sig_alg,
772*91f16700Schasinglulu                                    .data = &raw_data
773*91f16700Schasinglulu                            }
774*91f16700Schasinglulu                    },
775*91f16700Schasinglulu                    [1] = {
776*91f16700Schasinglulu                            .type = AUTH_METHOD_NV_CTR,
777*91f16700Schasinglulu                            .param.nv_ctr = {
778*91f16700Schasinglulu                                    .cert_nv_ctr = &trusted_nv_ctr,
779*91f16700Schasinglulu                                    .plat_nv_ctr = &trusted_nv_ctr
780*91f16700Schasinglulu                            }
781*91f16700Schasinglulu                    }
782*91f16700Schasinglulu            },
783*91f16700Schasinglulu            .authenticated_data = (const auth_param_desc_t[COT_MAX_VERIFIED_PARAMS]) {
784*91f16700Schasinglulu                    [0] = {
785*91f16700Schasinglulu                            .type_desc = &trusted_world_pk,
786*91f16700Schasinglulu                            .data = {
787*91f16700Schasinglulu                                    .ptr = (void *)trusted_world_pk_buf,
788*91f16700Schasinglulu                                    .len = (unsigned int)PK_DER_LEN
789*91f16700Schasinglulu                            }
790*91f16700Schasinglulu                    },
791*91f16700Schasinglulu                    [1] = {
792*91f16700Schasinglulu                            .type_desc = &non_trusted_world_pk,
793*91f16700Schasinglulu                            .data = {
794*91f16700Schasinglulu                                    .ptr = (void *)non_trusted_world_pk_buf,
795*91f16700Schasinglulu                                    .len = (unsigned int)PK_DER_LEN
796*91f16700Schasinglulu                            }
797*91f16700Schasinglulu                    }
798*91f16700Schasinglulu            }
799*91f16700Schasinglulu    };
800*91f16700Schasinglulu    static const auth_img_desc_t soc_fw_key_cert = {
801*91f16700Schasinglulu            .img_id = SOC_FW_KEY_CERT_ID,
802*91f16700Schasinglulu            .img_type = IMG_CERT,
803*91f16700Schasinglulu            .parent = &trusted_key_cert,
804*91f16700Schasinglulu            .img_auth_methods =  (const auth_method_desc_t[AUTH_METHOD_NUM]) {
805*91f16700Schasinglulu                    [0] = {
806*91f16700Schasinglulu                            .type = AUTH_METHOD_SIG,
807*91f16700Schasinglulu                            .param.sig = {
808*91f16700Schasinglulu                                    .pk = &trusted_world_pk,
809*91f16700Schasinglulu                                    .sig = &sig,
810*91f16700Schasinglulu                                    .alg = &sig_alg,
811*91f16700Schasinglulu                                    .data = &raw_data
812*91f16700Schasinglulu                            }
813*91f16700Schasinglulu                    },
814*91f16700Schasinglulu                    [1] = {
815*91f16700Schasinglulu                            .type = AUTH_METHOD_NV_CTR,
816*91f16700Schasinglulu                            .param.nv_ctr = {
817*91f16700Schasinglulu                                    .cert_nv_ctr = &trusted_nv_ctr,
818*91f16700Schasinglulu                                    .plat_nv_ctr = &trusted_nv_ctr
819*91f16700Schasinglulu                            }
820*91f16700Schasinglulu                    }
821*91f16700Schasinglulu            },
822*91f16700Schasinglulu            .authenticated_data = (const auth_param_desc_t[COT_MAX_VERIFIED_PARAMS]) {
823*91f16700Schasinglulu                    [0] = {
824*91f16700Schasinglulu                            .type_desc = &soc_fw_content_pk,
825*91f16700Schasinglulu                            .data = {
826*91f16700Schasinglulu                                    .ptr = (void *)content_pk_buf,
827*91f16700Schasinglulu                                    .len = (unsigned int)PK_DER_LEN
828*91f16700Schasinglulu                            }
829*91f16700Schasinglulu                    }
830*91f16700Schasinglulu            }
831*91f16700Schasinglulu    };
832*91f16700Schasinglulu    static const auth_img_desc_t soc_fw_content_cert = {
833*91f16700Schasinglulu            .img_id = SOC_FW_CONTENT_CERT_ID,
834*91f16700Schasinglulu            .img_type = IMG_CERT,
835*91f16700Schasinglulu            .parent = &soc_fw_key_cert,
836*91f16700Schasinglulu            .img_auth_methods =  (const auth_method_desc_t[AUTH_METHOD_NUM]) {
837*91f16700Schasinglulu                    [0] = {
838*91f16700Schasinglulu                            .type = AUTH_METHOD_SIG,
839*91f16700Schasinglulu                            .param.sig = {
840*91f16700Schasinglulu                                    .pk = &soc_fw_content_pk,
841*91f16700Schasinglulu                                    .sig = &sig,
842*91f16700Schasinglulu                                    .alg = &sig_alg,
843*91f16700Schasinglulu                                    .data = &raw_data
844*91f16700Schasinglulu                            }
845*91f16700Schasinglulu                    },
846*91f16700Schasinglulu                    [1] = {
847*91f16700Schasinglulu                            .type = AUTH_METHOD_NV_CTR,
848*91f16700Schasinglulu                            .param.nv_ctr = {
849*91f16700Schasinglulu                                    .cert_nv_ctr = &trusted_nv_ctr,
850*91f16700Schasinglulu                                    .plat_nv_ctr = &trusted_nv_ctr
851*91f16700Schasinglulu                            }
852*91f16700Schasinglulu                    }
853*91f16700Schasinglulu            },
854*91f16700Schasinglulu            .authenticated_data = (const auth_param_desc_t[COT_MAX_VERIFIED_PARAMS]) {
855*91f16700Schasinglulu                    [0] = {
856*91f16700Schasinglulu                            .type_desc = &soc_fw_hash,
857*91f16700Schasinglulu                            .data = {
858*91f16700Schasinglulu                                    .ptr = (void *)soc_fw_hash_buf,
859*91f16700Schasinglulu                                    .len = (unsigned int)HASH_DER_LEN
860*91f16700Schasinglulu                            }
861*91f16700Schasinglulu                    },
862*91f16700Schasinglulu                    [1] = {
863*91f16700Schasinglulu                            .type_desc = &soc_fw_config_hash,
864*91f16700Schasinglulu                            .data = {
865*91f16700Schasinglulu                                    .ptr = (void *)soc_fw_config_hash_buf,
866*91f16700Schasinglulu                                    .len = (unsigned int)HASH_DER_LEN
867*91f16700Schasinglulu                            }
868*91f16700Schasinglulu                    }
869*91f16700Schasinglulu            }
870*91f16700Schasinglulu    };
871*91f16700Schasinglulu    static const auth_img_desc_t bl31_image = {
872*91f16700Schasinglulu            .img_id = BL31_IMAGE_ID,
873*91f16700Schasinglulu            .img_type = IMG_RAW,
874*91f16700Schasinglulu            .parent = &soc_fw_content_cert,
875*91f16700Schasinglulu            .img_auth_methods =  (const auth_method_desc_t[AUTH_METHOD_NUM]) {
876*91f16700Schasinglulu                    [0] = {
877*91f16700Schasinglulu                            .type = AUTH_METHOD_HASH,
878*91f16700Schasinglulu                            .param.hash = {
879*91f16700Schasinglulu                                    .data = &raw_data,
880*91f16700Schasinglulu                                    .hash = &soc_fw_hash
881*91f16700Schasinglulu                            }
882*91f16700Schasinglulu                    }
883*91f16700Schasinglulu            }
884*91f16700Schasinglulu    };
885*91f16700Schasinglulu
886*91f16700SchasingluluThe **Trusted Key certificate** is signed with the ROT private key and contains
887*91f16700Schasingluluthe Trusted World public key and the Non-Trusted World public key as x509v3
888*91f16700Schasingluluextensions. This must be specified in the image descriptor using the
889*91f16700Schasinglulu``img_auth_methods`` and ``authenticated_data`` arrays, respectively.
890*91f16700Schasinglulu
891*91f16700SchasingluluThe Trusted Key certificate is authenticated by checking its digital signature
892*91f16700Schasingluluusing the ROTPK. Four parameters are required to check a signature: the public
893*91f16700Schasinglulukey, the algorithm, the signature and the data that has been signed. Therefore,
894*91f16700Schasinglulufour parameter descriptors must be specified with the authentication method:
895*91f16700Schasinglulu
896*91f16700Schasinglulu-  ``subject_pk``: parameter descriptor of type ``AUTH_PARAM_PUB_KEY``. This type
897*91f16700Schasinglulu   is used to extract a public key from the parent image. If the cookie is an
898*91f16700Schasinglulu   OID, the key is extracted from the corresponding x509v3 extension. If the
899*91f16700Schasinglulu   cookie is NULL, the subject public key is retrieved. In this case, because
900*91f16700Schasinglulu   the parent image is NULL, the public key is obtained from the platform
901*91f16700Schasinglulu   (this key will be the ROTPK).
902*91f16700Schasinglulu-  ``sig``: parameter descriptor of type ``AUTH_PARAM_SIG``. It is used to extract
903*91f16700Schasinglulu   the signature from the certificate.
904*91f16700Schasinglulu-  ``sig_alg``: parameter descriptor of type ``AUTH_PARAM_SIG``. It is used to
905*91f16700Schasinglulu   extract the signature algorithm from the certificate.
906*91f16700Schasinglulu-  ``raw_data``: parameter descriptor of type ``AUTH_PARAM_RAW_DATA``. It is used
907*91f16700Schasinglulu   to extract the data to be signed from the certificate.
908*91f16700Schasinglulu
909*91f16700SchasingluluOnce the signature has been checked and the certificate authenticated, the
910*91f16700SchasingluluTrusted World public key needs to be extracted from the certificate. A new entry
911*91f16700Schasingluluis created in the ``authenticated_data`` array for that purpose. In that entry,
912*91f16700Schasingluluthe corresponding parameter descriptor must be specified along with the buffer
913*91f16700Schasingluluaddress to store the parameter value. In this case, the ``trusted_world_pk``
914*91f16700Schasingluludescriptor is used to extract the public key from an x509v3 extension with OID
915*91f16700Schasinglulu``TRUSTED_WORLD_PK_OID``. The BL31 key certificate will use this descriptor as
916*91f16700Schasingluluparameter in the signature authentication method. The key is stored in the
917*91f16700Schasinglulu``trusted_world_pk_buf`` buffer.
918*91f16700Schasinglulu
919*91f16700SchasingluluThe **BL31 Key certificate** is authenticated by checking its digital signature
920*91f16700Schasingluluusing the Trusted World public key obtained previously from the Trusted Key
921*91f16700Schasinglulucertificate. In the image descriptor, we specify a single authentication method
922*91f16700Schasingluluby signature whose public key is the ``trusted_world_pk``. Once this certificate
923*91f16700Schasingluluhas been authenticated, we have to extract the BL31 public key, stored in the
924*91f16700Schasingluluextension specified by ``soc_fw_content_pk``. This key will be copied to the
925*91f16700Schasinglulu``content_pk_buf`` buffer.
926*91f16700Schasinglulu
927*91f16700SchasingluluThe **BL31 certificate** is authenticated by checking its digital signature
928*91f16700Schasingluluusing the BL31 public key obtained previously from the BL31 Key certificate.
929*91f16700SchasingluluWe specify the authentication method using ``soc_fw_content_pk`` as public key.
930*91f16700SchasingluluAfter authentication, we need to extract the BL31 hash, stored in the extension
931*91f16700Schasingluluspecified by ``soc_fw_hash``. This hash will be copied to the
932*91f16700Schasinglulu``soc_fw_hash_buf`` buffer.
933*91f16700Schasinglulu
934*91f16700SchasingluluThe **BL31 image** is authenticated by calculating its hash and matching it
935*91f16700Schasingluluwith the hash obtained from the BL31 certificate. The image descriptor contains
936*91f16700Schasinglulua single authentication method by hash. The parameters to the hash method are
937*91f16700Schasingluluthe reference hash, ``soc_fw_hash``, and the data to be hashed. In this case,
938*91f16700Schasingluluit is the whole image, so we specify ``raw_data``.
939*91f16700Schasinglulu
940*91f16700SchasingluluThe image parser library
941*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~
942*91f16700Schasinglulu
943*91f16700SchasingluluThe image parser module relies on libraries to check the image integrity and
944*91f16700Schasingluluextract the authentication parameters. The number and type of parser libraries
945*91f16700Schasingluludepend on the images used in the CoT. Raw images do not need a library, so
946*91f16700Schasingluluonly an x509v3 library is required for the TBBR CoT.
947*91f16700Schasinglulu
948*91f16700SchasingluluArm platforms will use an x509v3 library based on mbed TLS. This library may be
949*91f16700Schasinglulufound in ``drivers/auth/mbedtls/mbedtls_x509_parser.c``. It exports three
950*91f16700Schasinglulufunctions:
951*91f16700Schasinglulu
952*91f16700Schasinglulu.. code:: c
953*91f16700Schasinglulu
954*91f16700Schasinglulu    void init(void);
955*91f16700Schasinglulu    int check_integrity(void *img, unsigned int img_len);
956*91f16700Schasinglulu    int get_auth_param(const auth_param_type_desc_t *type_desc,
957*91f16700Schasinglulu                       void *img, unsigned int img_len,
958*91f16700Schasinglulu                       void **param, unsigned int *param_len);
959*91f16700Schasinglulu
960*91f16700SchasingluluThe library is registered in the framework using the macro
961*91f16700Schasinglulu``REGISTER_IMG_PARSER_LIB()``. Each time the image parser module needs to access
962*91f16700Schasingluluan image of type ``IMG_CERT``, it will call the corresponding function exported
963*91f16700Schasingluluin this file.
964*91f16700Schasinglulu
965*91f16700SchasingluluThe build system must be updated to include the corresponding library and
966*91f16700Schasinglulumbed TLS sources. Arm platforms use the ``arm_common.mk`` file to pull the
967*91f16700Schasinglulusources.
968*91f16700Schasinglulu
969*91f16700SchasingluluThe cryptographic library
970*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~
971*91f16700Schasinglulu
972*91f16700SchasingluluThe cryptographic module relies on a library to perform the required operations,
973*91f16700Schasinglului.e. verify a hash or a digital signature. Arm platforms will use a library
974*91f16700Schasinglulubased on mbed TLS, which can be found in
975*91f16700Schasinglulu``drivers/auth/mbedtls/mbedtls_crypto.c``. This library is registered in the
976*91f16700Schasingluluauthentication framework using the macro ``REGISTER_CRYPTO_LIB()`` and exports
977*91f16700Schasinglulubelow functions:
978*91f16700Schasinglulu
979*91f16700Schasinglulu.. code:: c
980*91f16700Schasinglulu
981*91f16700Schasinglulu    void init(void);
982*91f16700Schasinglulu    int verify_signature(void *data_ptr, unsigned int data_len,
983*91f16700Schasinglulu                         void *sig_ptr, unsigned int sig_len,
984*91f16700Schasinglulu                         void *sig_alg, unsigned int sig_alg_len,
985*91f16700Schasinglulu                         void *pk_ptr, unsigned int pk_len);
986*91f16700Schasinglulu    int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr,
987*91f16700Schasinglulu                             unsigned int data_len,
988*91f16700Schasinglulu                             unsigned char output[CRYPTO_MD_MAX_SIZE])
989*91f16700Schasinglulu    int verify_hash(void *data_ptr, unsigned int data_len,
990*91f16700Schasinglulu                    void *digest_info_ptr, unsigned int digest_info_len);
991*91f16700Schasinglulu    int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
992*91f16700Schasinglulu                     size_t len, const void *key, unsigned int key_len,
993*91f16700Schasinglulu                     unsigned int key_flags, const void *iv,
994*91f16700Schasinglulu                     unsigned int iv_len, const void *tag,
995*91f16700Schasinglulu                     unsigned int tag_len)
996*91f16700Schasinglulu
997*91f16700SchasingluluThe mbedTLS library algorithm support is configured by both the
998*91f16700Schasinglulu``TF_MBEDTLS_KEY_ALG`` and ``TF_MBEDTLS_KEY_SIZE`` variables.
999*91f16700Schasinglulu
1000*91f16700Schasinglulu-  ``TF_MBEDTLS_KEY_ALG`` can take in 3 values: `rsa`, `ecdsa` or `rsa+ecdsa`.
1001*91f16700Schasinglulu   This variable allows the Makefile to include the corresponding sources in
1002*91f16700Schasinglulu   the build for the various algorithms. Setting the variable to `rsa+ecdsa`
1003*91f16700Schasinglulu   enables support for both rsa and ecdsa algorithms in the mbedTLS library.
1004*91f16700Schasinglulu
1005*91f16700Schasinglulu-  ``TF_MBEDTLS_KEY_SIZE`` sets the supported RSA key size for TFA. Valid values
1006*91f16700Schasinglulu   include 1024, 2048, 3072 and 4096.
1007*91f16700Schasinglulu
1008*91f16700Schasinglulu-  ``TF_MBEDTLS_USE_AES_GCM`` enables the authenticated decryption support based
1009*91f16700Schasinglulu   on AES-GCM algorithm. Valid values are 0 and 1.
1010*91f16700Schasinglulu
1011*91f16700Schasinglulu.. note::
1012*91f16700Schasinglulu   If code size is a concern, the build option ``MBEDTLS_SHA256_SMALLER`` can
1013*91f16700Schasinglulu   be defined in the platform Makefile. It will make mbed TLS use an
1014*91f16700Schasinglulu   implementation of SHA-256 with smaller memory footprint (~1.5 KB less) but
1015*91f16700Schasinglulu   slower (~30%).
1016*91f16700Schasinglulu
1017*91f16700Schasinglulu--------------
1018*91f16700Schasinglulu
1019*91f16700Schasinglulu*Copyright (c) 2017-2023, Arm Limited and Contributors. All rights reserved.*
1020*91f16700Schasinglulu
1021*91f16700Schasinglulu.. _TBBR-Client specification: https://developer.arm.com/docs/den0006/latest/trusted-board-boot-requirements-client-tbbr-client-armv8-a
1022