blob: f6455b9ed5afbbd4b3e13122fb3a70c033028006 [file] [log] [blame] [view]
Juan Castillod337aaa2015-06-10 15:27:52 +01001Abstracting a Chain of Trust
2============================
3
4Contents :
5
61. [Introduction](#1--introduction)
72. [Framework design](#2--framework-design)
83. [Specifying a Chain of Trust](#3--specifying-a-chain-of-trust)
94. [Implementation example](#4--implementation-example)
10
11
121. Introduction
13----------------
14
15The aim of this document is to describe the authentication framework implemented
16in the Trusted Firmware. This framework fulfills the following requirements:
17
181. It should be possible for a platform port to specify the Chain of Trust in
19 terms of certificate hierarchy and the mechanisms used to verify a
20 particular image/certificate.
21
222. The framework should distinguish between:
23
24 - The mechanism used to encode and transport information, e.g. DER encoded
25 X.509v3 certificates to ferry Subject Public Keys, hashes and non-volatile
26 counters.
27
28 - The mechanism used to verify the transported information i.e. the
29 cryptographic libraries.
30
31The framework has been designed following a modular approach illustrated in the
32next diagram:
33
34```
35 +---------------+---------------+------------+
36 | Trusted | Trusted | Trusted |
37 | Firmware | Firmware | Firmware |
38 | Generic | IO Framework | Platform |
39 | Code i.e. | (IO) | Port |
40 | BL1/BL2 (GEN) | | (PP) |
41 +---------------+---------------+------------+
42 ^ ^ ^
43 | | |
44 v v v
45 +-----------+ +-----------+ +-----------+
46 | | | | | Image |
47 | Crypto | | Auth | | Parser |
48 | Module |<->| Module |<->| Module |
49 | (CM) | | (AM) | | (IPM) |
50 | | | | | |
51 +-----------+ +-----------+ +-----------+
52 ^ ^
53 | |
54 v v
55 +----------------+ +-----------------+
56 | Cryptographic | | Image Parser |
57 | Libraries (CL) | | Libraries (IPL) |
58 +----------------+ +-----------------+
59 | |
60 | |
61 | |
62 v v
63 +-----------------+
64 | Misc. Libs e.g. |
65 | ASN.1 decoder |
66 | |
67 +-----------------+
68
69 DIAGRAM 1.
70```
71
72This document describes the inner details of the authentication framework and
73the abstraction mechanisms available to specify a Chain of Trust.
74
75
762. Framework design
77--------------------
78
79This section describes some aspects of the framework design and the rationale
80behind them. These aspects are key to verify a Chain of Trust.
81
82### 2.1 Chain of Trust
83
84A CoT is basically a sequence of authentication images which usually starts with
85a root of trust and culminates in a single data image. The following diagram
86illustrates how this maps to a CoT for the BL3-1 image described in the
87TBBR-Client specification.
88
89```
90 +------------------+ +-------------------+
91 | ROTPK/ROTPK Hash |------>| Trusted Key |
92 +------------------+ | Certificate |
93 | (Auth Image) |
94 /+-------------------+
95 / |
96 / |
97 / |
98 / |
99 L v
100 +------------------+ +-------------------+
101 | Trusted World |------>| BL3-1 Key |
102 | Public Key | | Certificate |
103 +------------------+ | (Auth Image) |
104 +-------------------+
105 / |
106 / |
107 / |
108 / |
109 / v
110 +------------------+ L +-------------------+
111 | BL3-1 Content |------>| BL3-1 Content |
112 | Certificate PK | | Certificate |
113 +------------------+ | (Auth Image) |
114 +-------------------+
115 / |
116 / |
117 / |
118 / |
119 / v
120 +------------------+ L +-------------------+
121 | BL3-1 Hash |------>| BL3-1 Image |
122 | | | (Data Image) |
123 +------------------+ | |
124 +-------------------+
125
126 DIAGRAM 2.
127```
128
129The root of trust is usually a public key (ROTPK) that has been burnt in the
130platform and cannot be modified.
131
132### 2.2 Image types
133
134Images in a CoT are categorised as authentication and data images. An
135authentication image contains information to authenticate a data image or
136another authentication image. A data image is usually a boot loader binary, but
137it could be any other data that requires authentication.
138
139### 2.3 Component responsibilities
140
141For every image in a Chain of Trust, the following high level operations are
142performed to verify it:
143
1441. Allocate memory for the image either statically or at runtime.
145
1462. Identify the image and load it in the allocated memory.
147
1483. Check the integrity of the image as per its type.
149
1504. Authenticate the image as per the cryptographic algorithms used.
151
1525. If the image is an authentication image, extract the information that will
153 be used to authenticate the next image in the CoT.
154
155In Diagram 1, each component is responsible for one or more of these operations.
156The responsibilities are briefly described below.
157
158
159#### 2.2.1 TF Generic code and IO framework (GEN/IO)
160
161These components are responsible for initiating the authentication process for a
162particular image in BL1 or BL2. For each BL image that requires authentication,
163the Generic code asks recursively the Authentication module what is the parent
164image until either an authenticated image or the ROT is reached. Then the
165Generic code calls the IO framewotk to load the image and calls the
166Authentication module to authenticate it, following the CoT from ROT to Image.
167
168
169#### 2.2.2 TF Platform Port (PP)
170
171The platform is responsible for:
172
1731. Specifying the CoT for each image that needs to be authenticated. Details of
174 how a CoT can be specified by the platform are explained later. The platform
175 also specifies the authentication methods and the parsing method used for
176 each image.
177
1782. Statically allocating memory for each parameter in each image which is
179 used for verifying the CoT, e.g. memory for public keys, hashes etc.
180
1813. Providing the ROTPK or a hash of it.
182
1834. Providing additional information to the IPM to enable it to identify and
184 extract authentication parameters contained in an image, e.g. if the
185 parameters are stored as X509v3 extensions, the corresponding OID must be
186 provided.
187
1885. Fulfill any other memory requirements of the IPM and the CM (not currently
189 described in this document).
190
1916. Export functions to verify an image which uses an authentication method that
192 cannot be interpreted by the CM, e.g. if an image has to be verified using a
193 NV counter, then the value of the counter to compare with can only be
194 provided by the platform.
195
1967. Export a custom IPM if a proprietary image format is being used (described
197 later).
198
199
200#### 2.2.3 Authentication Module (AM)
201
202It is responsible for:
203
2041. Providing the necessary abstraction mechanisms to describe a CoT. Amongst
205 other things, the authentication and image parsing methods must be specified
206 by the PP in the CoT.
207
2082. Verifying the CoT passed by GEN by utilising functionality exported by the
209 PP, IPM and CM.
210
2113. Tracking which images have been verified. In case an image is a part of
212 multiple CoTs then it should be verified only once e.g. the Trusted World
213 Key Certificate in the TBBR-Client spec. contains information to verify
Juan Castillof59821d2015-12-10 15:49:17 +0000214 SCP_BL2, BL3-1, BL3-2 each of which have a separate CoT. (This
215 responsibility has not been described in this document but should be
216 trivial to implement).
Juan Castillod337aaa2015-06-10 15:27:52 +0100217
2184. Reusing memory meant for a data image to verify authentication images e.g.
219 in the CoT described in Diagram 2, each certificate can be loaded and
220 verified in the memory reserved by the platform for the BL3-1 image. By the
221 time BL3-1 (the data image) is loaded, all information to authenticate it
222 will have been extracted from the parent image i.e. BL3-1 content
223 certificate. It is assumed that the size of an authentication image will
224 never exceed the size of a data image. It should be possible to verify this
225 at build time using asserts.
226
227
228#### 2.2.4 Cryptographic Module (CM)
229
230The CM is responsible for providing an API to:
231
2321. Verify a digital signature.
2332. Verify a hash.
234
235The CM does not include any cryptography related code, but it relies on an
236external library to perform the cryptographic operations. A Crypto-Library (CL)
237linking the CM and the external library must be implemented. The following
238functions must be provided by the CL:
239
240```
241void (*init)(void);
242int (*verify_signature)(void *data_ptr, unsigned int data_len,
243 void *sig_ptr, unsigned int sig_len,
244 void *sig_alg, unsigned int sig_alg_len,
245 void *pk_ptr, unsigned int pk_len);
246int (*verify_hash)(void *data_ptr, unsigned int data_len,
247 void *digest_info_ptr, unsigned int digest_info_len);
248```
249
250These functions are registered in the CM using the macro:
251```
252REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash);
253```
254
255`_name` must be a string containing the name of the CL. This name is used for
256debugging purposes.
257
258#### 2.2.5 Image Parser Module (IPM)
259
260The IPM is responsible for:
261
2621. Checking the integrity of each image loaded by the IO framework.
2632. Extracting parameters used for authenticating an image based upon a
264 description provided by the platform in the CoT descriptor.
265
266Images may have different formats (for example, authentication images could be
267x509v3 certificates, signed ELF files or any other platform specific format).
268The IPM allows to register an Image Parser Library (IPL) for every image format
269used in the CoT. This library must implement the specific methods to parse the
270image. The IPM obtains the image format from the CoT and calls the right IPL to
271check the image integrity and extract the authentication parameters.
272
273See Section "Describing the image parsing methods" for more details about the
274mechanism the IPM provides to define and register IPLs.
275
276
277### 2.3 Authentication methods
278
279The AM supports the following authentication methods:
280
2811. Hash
2822. Digital signature
283
284The platform may specify these methods in the CoT in case it decides to define
285a custom CoT instead of reusing a predefined one.
286
287If a data image uses multiple methods, then all the methods must be a part of
288the same CoT. The number and type of parameters are method specific. These
289parameters should be obtained from the parent image using the IPM.
290
2911. Hash
292
293 Parameters:
294
295 1. A pointer to data to hash
296 2. Length of the data
297 4. A pointer to the hash
298 5. Length of the hash
299
300 The hash will be represented by the DER encoding of the following ASN.1
301 type:
302
303 ```
304 DigestInfo ::= SEQUENCE {
305 digestAlgorithm DigestAlgorithmIdentifier,
306 digest Digest
307 }
308 ```
309
310 This ASN.1 structure makes it possible to remove any assumption about the
311 type of hash algorithm used as this information accompanies the hash. This
312 should allow the Cryptography Library (CL) to support multiple hash
313 algorithm implementations.
314
3152. Digital Signature
316
317 Parameters:
318
319 1. A pointer to data to sign
320 2. Length of the data
321 3. Public Key Algorithm
322 4. Public Key value
323 5. Digital Signature Algorithm
324 6. Digital Signature value
325
326 The Public Key parameters will be represented by the DER encoding of the
327 following ASN.1 type:
328
329 ```
330 SubjectPublicKeyInfo ::= SEQUENCE {
331 algorithm AlgorithmIdentifier{PUBLIC-KEY,{PublicKeyAlgorithms}},
332 subjectPublicKey BIT STRING }
333 ```
334
335 The Digital Signature Algorithm will be represented by the DER encoding of
336 the following ASN.1 types.
337
338 ```
339 AlgorithmIdentifier {ALGORITHM:IOSet } ::= SEQUENCE {
340 algorithm ALGORITHM.&id({IOSet}),
341 parameters ALGORITHM.&Type({IOSet}{@algorithm}) OPTIONAL
342 }
343 ```
344
345 The digital signature will be represented by:
346 ```
347 signature ::= BIT STRING
348 ```
349
350The authentication framework will use the image descriptor to extract all the
351information related to authentication.
352
353
3543. Specifying a Chain of Trust
355-------------------------------
356
357A CoT can be described as a set of image descriptors linked together in a
358particular order. The order dictates the sequence in which they must be
359verified. Each image has a set of properties which allow the AM to verify it.
360These properties are described below.
361
362The PP is responsible for defining a single or multiple CoTs for a data image.
363Unless otherwise specified, the data structures described in the following
364sections are populated by the PP statically.
365
366
367### 3.1 Describing the image parsing methods
368
369The parsing method refers to the format of a particular image. For example, an
370authentication image that represents a certificate could be in the X.509v3
371format. A data image that represents a boot loader stage could be in raw binary
372or ELF format. The IPM supports three parsing methods. An image has to use one
373of the three methods described below. An IPL is responsible for interpreting a
374single parsing method. There has to be one IPL for every method used by the
375platform.
376
3771. Raw format: This format is effectively a nop as an image using this method
378 is treated as being in raw binary format e.g. boot loader images used by ARM
379 TF. This method should only be used by data images.
380
3812. X509V3 method: This method uses industry standards like X.509 to represent
382 PKI certificates (authentication images). It is expected that open source
383 libraries will be available which can be used to parse an image represented
384 by this method. Such libraries can be used to write the corresponding IPL
Juan Castillo649dbf62015-11-05 09:24:53 +0000385 e.g. the X.509 parsing library code in mbed TLS.
Juan Castillod337aaa2015-06-10 15:27:52 +0100386
3873. Platform defined method: This method caters for platform specific
388 proprietary standards to represent authentication or data images. For
389 example, The signature of a data image could be appended to the data image
390 raw binary. A header could be prepended to the combined blob to specify the
391 extents of each component. The platform will have to implement the
392 corresponding IPL to interpret such a format.
393
394The following enum can be used to define these three methods.
395
396```
397typedef enum img_type_enum {
398 IMG_RAW, /* Binary image */
399 IMG_PLAT, /* Platform specific format */
400 IMG_CERT, /* X509v3 certificate */
401 IMG_MAX_TYPES,
402} img_type_t;
403```
404
405An IPL must provide functions with the following prototypes:
406
407```
408void init(void);
409int check_integrity(void *img, unsigned int img_len);
410int get_auth_param(const auth_param_type_desc_t *type_desc,
411 void *img, unsigned int img_len,
412 void **param, unsigned int *param_len);
413```
414
415An IPL for each type must be registered using the following macro:
416
417```
418REGISTER_IMG_PARSER_LIB(_type, _name, _init, _check_int, _get_param)
419```
420
421* `_type`: one of the types described above.
422* `_name`: a string containing the IPL name for debugging purposes.
423* `_init`: initialization function pointer.
424* `_check_int`: check image integrity function pointer.
425* `_get_param`: extract authentication parameter funcion pointer.
426
427The `init()` function will be used to initialize the IPL.
428
429The `check_integrity()` function is passed a pointer to the memory where the
430image has been loaded by the IO framework and the image length. It should ensure
431that the image is in the format corresponding to the parsing method and has not
432been tampered with. For example, RFC-2459 describes a validation sequence for an
433X.509 certificate.
434
435The `get_auth_param()` function is passed a parameter descriptor containing
436information about the parameter (`type_desc` and `cookie`) to identify and
437extract the data corresponding to that parameter from an image. This data will
438be used to verify either the current or the next image in the CoT sequence.
439
440Each image in the CoT will specify the parsing method it uses. This information
441will be used by the IPM to find the right parser descriptor for the image.
442
443
444### 3.2 Describing the authentication method(s)
445
446As part of the CoT, each image has to specify one or more authentication methods
447which will be used to verify it. As described in the Section "Authentication
448methods", there are three methods supported by the AM.
449
450```
451typedef enum {
452 AUTH_METHOD_NONE,
453 AUTH_METHOD_HASH,
454 AUTH_METHOD_SIG,
455 AUTH_METHOD_NUM
456} auth_method_type_t;
457```
458
459The AM defines the type of each parameter used by an authentication method. It
460uses this information to:
461
4621. Specify to the `get_auth_param()` function exported by the IPM, which
463 parameter should be extracted from an image.
464
4652. Correctly marshall the parameters while calling the verification function
466 exported by the CM and PP.
467
4683. Extract authentication parameters from a parent image in order to verify a
469 child image e.g. to verify the certificate image, the public key has to be
470 obtained from the parent image.
471
472```
473typedef enum {
474 AUTH_PARAM_NONE,
475 AUTH_PARAM_RAW_DATA, /* Raw image data */
476 AUTH_PARAM_SIG, /* The image signature */
477 AUTH_PARAM_SIG_ALG, /* The image signature algorithm */
478 AUTH_PARAM_HASH, /* A hash (including the algorithm) */
479 AUTH_PARAM_PUB_KEY, /* A public key */
480} auth_param_type_t;
481```
482
483The AM defines the following structure to identify an authentication parameter
484required to verify an image.
485
486```
487typedef struct auth_param_type_desc_s {
488 auth_param_type_t type;
489 void *cookie;
490} auth_param_type_desc_t;
491```
492
493`cookie` is used by the platform to specify additional information to the IPM
494which enables it to uniquely identify the parameter that should be extracted
495from an image. For example, the hash of a BL3-x image in its corresponding
496content certificate is stored in an X509v3 custom extension field. An extension
497field can only be identified using an OID. In this case, the `cookie` could
498contain the pointer to the OID defined by the platform for the hash extension
499field while the `type` field could be set to `AUTH_PARAM_HASH`. A value of 0 for
500the `cookie` field means that it is not used.
501
502For each method, the AM defines a structure with the parameters required to
503verify the image.
504
505```
506/*
507 * Parameters for authentication by hash matching
508 */
509typedef struct auth_method_param_hash_s {
510 auth_param_type_desc_t *data; /* Data to hash */
511 auth_param_type_desc_t *hash; /* Hash to match with */
512} auth_method_param_hash_t;
513
514/*
515 * Parameters for authentication by signature
516 */
517typedef struct auth_method_param_sig_s {
518 auth_param_type_desc_t *pk; /* Public key */
519 auth_param_type_desc_t *sig; /* Signature to check */
520 auth_param_type_desc_t *alg; /* Signature algorithm */
521 auth_param_type_desc_t *tbs; /* Data signed */
522} auth_method_param_sig_t;
523
524```
525
526The AM defines the following structure to describe an authentication method for
527verifying an image
528
529```
530/*
531 * Authentication method descriptor
532 */
533typedef struct auth_method_desc_s {
534 auth_method_type_t type;
535 union {
536 auth_method_param_hash_t hash;
537 auth_method_param_sig_t sig;
538 } param;
539} auth_method_desc_t;
540```
541
542Using the method type specified in the `type` field, the AM finds out what field
543needs to access within the `param` union.
544
545### 3.3 Storing Authentication parameters
546
547A parameter described by `auth_param_type_desc_t` to verify an image could be
548obtained from either the image itself or its parent image. The memory allocated
549for loading the parent image will be reused for loading the child image. Hence
550parameters which are obtained from the parent for verifying a child image need
551to have memory allocated for them separately where they can be stored. This
552memory must be statically allocated by the platform port.
553
554The AM defines the following structure to store the data corresponding to an
555authentication parameter.
556
557```
558typedef struct auth_param_data_desc_s {
559 void *auth_param_ptr;
560 unsigned int auth_param_len;
561} auth_param_data_desc_t;
562```
563
564The `auth_param_ptr` field is initialized by the platform. The `auth_param_len`
565field is used to specify the length of the data in the memory.
566
567For parameters that can be obtained from the child image itself, the IPM is
568responsible for populating the `auth_param_ptr` and `auth_param_len` fields
569while executing the `img_get_auth_param()` function.
570
571The AM defines the following structure to enable an image to describe the
572parameters that should be extracted from it and used to verify the next image
573(child) in a CoT.
574
575```
576typedef struct auth_param_desc_s {
577 auth_param_type_desc_t type_desc;
578 auth_param_data_desc_t data;
579} auth_param_desc_t;
580```
581
582### 3.4 Describing an image in a CoT
583
584An image in a CoT is a consolidation of the following aspects of a CoT described
585above.
586
5871. A unique identifier specified by the platform which allows the IO framework
588 to locate the image in a FIP and load it in the memory reserved for the data
589 image in the CoT.
590
5912. A parsing method which is used by the AM to find the appropriate IPM.
592
5933. Authentication methods and their parameters as described in the previous
594 section. These are used to verify the current image.
595
5964. Parameters which are used to verify the next image in the current CoT. These
597 parameters are specified only by authentication images and can be extracted
598 from the current image once it has been verified.
599
600The following data structure describes an image in a CoT.
601```
602typedef struct auth_img_desc_s {
603 unsigned int img_id;
604 const struct auth_img_desc_s *parent;
605 img_type_t img_type;
606 auth_method_desc_t img_auth_methods[AUTH_METHOD_NUM];
607 auth_param_desc_t authenticated_data[COT_MAX_VERIFIED_PARAMS];
608} auth_img_desc_t;
609```
610A CoT is defined as an array of `auth_image_desc_t` structures linked together
611by the `parent` field. Those nodes with no parent must be authenticated using
612the ROTPK stored in the platform.
613
614
6154. Implementation example
616--------------------------
617
618This section is a detailed guide explaining a trusted boot implementation using
619the authentication framework. This example corresponds to the Applicative
620Functional Mode (AFM) as specified in the TBBR-Client document. It is
621recommended to read this guide along with the source code.
622
623### 4.1 The TBBR CoT
624
625The CoT can be found in `drivers/auth/tbbr/tbbr_cot.c`. This CoT consists of an
626array of image descriptors and it is registered in the framework using the macro
627`REGISTER_COT(cot_desc)`, where 'cot_desc' must be the name of the array
628(passing a pointer or any other type of indirection will cause the registration
629process to fail).
630
631The number of images participating in the boot process depends on the CoT. There
632is, however, a minimum set of images that are mandatory in the Trusted Firmware
633and thus all CoTs must present:
634
635* `BL2`
Juan Castillof59821d2015-12-10 15:49:17 +0000636* `SCP_BL2` (platform specific)
Juan Castillod337aaa2015-06-10 15:27:52 +0100637* `BL3-1`
638* `BL3-2` (optional)
639* `BL3-3`
640
641The TBBR specifies the additional certificates that must accompany these images
642for a proper authentication. Details about the TBBR CoT may be found in the
643[Trusted Board Boot] document.
644
645Following the [Platform Porting Guide], a platform must provide unique
646identifiers for all the images and certificates that will be loaded during the
647boot process. If a platform is using the TBBR as a reference for trusted boot,
648these identifiers can be obtained from `include/common/tbbr/tbbr_img_def.h`.
649ARM platforms include this file in `include/plat/arm/common/arm_def.h`. Other
650platforms may also include this file or provide their own identifiers.
651
652**Important**: the authentication module uses these identifiers to index the
653CoT array, so the descriptors location in the array must match the identifiers.
654
655Each image descriptor must specify:
656
657* `img_id`: the corresponding image unique identifier defined by the platform.
658* `img_type`: the image parser module uses the image type to call the proper
659 parsing library to check the image integrity and extract the required
660 authentication parameters. Three types of images are currently supported:
661 * `IMG_RAW`: image is a raw binary. No parsing functions are available,
662 other than reading the whole image.
663 * `IMG_PLAT`: image format is platform specific. The platform may use this
664 type for custom images not directly supported by the authentication
665 framework.
666 * `IMG_CERT`: image is an x509v3 certificate.
667* `parent`: pointer to the parent image descriptor. The parent will contain
668 the information required to authenticate the current image. If the parent
669 is NULL, the authentication parameters will be obtained from the platform
670 (i.e. the BL2 and Trusted Key certificates are signed with the ROT private
671 key, whose public part is stored in the platform).
672* `img_auth_methods`: this array defines the authentication methods that must
673 be checked to consider an image authenticated. Each method consists of a
674 type and a list of parameter descriptors. A parameter descriptor consists of
675 a type and a cookie which will point to specific information required to
676 extract that parameter from the image (i.e. if the parameter is stored in an
677 x509v3 extension, the cookie will point to the extension OID). Depending on
678 the method type, a different number of parameters must be specified.
679 Supported methods are:
680 * `AUTH_METHOD_HASH`: the hash of the image must match the hash extracted
681 from the parent image. The following parameter descriptors must be
682 specified:
683 * `data`: data to be hashed (obtained from current image)
684 * `hash`: reference hash (obtained from parent image)
685 * `AUTH_METHOD_SIG`: the image (usually a certificate) must be signed with
686 the private key whose public part is extracted from the parent image (or
687 the platform if the parent is NULL). The following parameter descriptors
688 must be specified:
689 * `pk`: the public key (obtained from parent image)
690 * `sig`: the digital signature (obtained from current image)
691 * `alg`: the signature algorithm used (obtained from current image)
692 * `data`: the data to be signed (obtained from current image)
693* `authenticated_data`: this array indicates what authentication parameters
694 must be extracted from an image once it has been authenticated. Each
695 parameter consists of a parameter descriptor and the buffer address/size
696 to store the parameter. The CoT is responsible for allocating the required
697 memory to store the parameters.
698
699In the `tbbr_cot.c` file, a set of buffers are allocated to store the parameters
700extracted from the certificates. In the case of the TBBR CoT, these parameters
701are hashes and public keys. In DER format, an RSA-2048 public key requires 294
702bytes, and a hash requires 51 bytes. Depending on the CoT and the authentication
703process, some of the buffers may be reused at different stages during the boot.
704
705Next in that file, the parameter descriptors are defined. These descriptors will
706be used to extract the parameter data from the corresponding image.
707
708#### 4.1.1 Example: the BL3-1 Chain of Trust
709
710Four image descriptors form the BL3-1 Chain of Trust:
711
712```
713[TRUSTED_KEY_CERT_ID] = {
714 .img_id = TRUSTED_KEY_CERT_ID,
715 .img_type = IMG_CERT,
716 .parent = NULL,
717 .img_auth_methods = {
718 [0] = {
719 .type = AUTH_METHOD_SIG,
720 .param.sig = {
721 .pk = &subject_pk,
722 .sig = &sig,
723 .alg = &sig_alg,
724 .data = &raw_data,
725 }
726 }
727 },
728 .authenticated_data = {
729 [0] = {
Juan Castillo516beb52015-12-03 10:19:21 +0000730 .type_desc = &trusted_world_pk,
Juan Castillod337aaa2015-06-10 15:27:52 +0100731 .data = {
Juan Castillo516beb52015-12-03 10:19:21 +0000732 .ptr = (void *)trusted_world_pk_buf,
Juan Castillod337aaa2015-06-10 15:27:52 +0100733 .len = (unsigned int)PK_DER_LEN
734 }
735 },
736 [1] = {
Juan Castillo516beb52015-12-03 10:19:21 +0000737 .type_desc = &non_trusted_world_pk,
Juan Castillod337aaa2015-06-10 15:27:52 +0100738 .data = {
Juan Castillo516beb52015-12-03 10:19:21 +0000739 .ptr = (void *)non_trusted_world_pk_buf,
Juan Castillod337aaa2015-06-10 15:27:52 +0100740 .len = (unsigned int)PK_DER_LEN
741 }
742 }
743 }
744},
Juan Castillo516beb52015-12-03 10:19:21 +0000745[SOC_FW_KEY_CERT_ID] = {
746 .img_id = SOC_FW_KEY_CERT_ID,
Juan Castillod337aaa2015-06-10 15:27:52 +0100747 .img_type = IMG_CERT,
748 .parent = &cot_desc[TRUSTED_KEY_CERT_ID],
749 .img_auth_methods = {
750 [0] = {
751 .type = AUTH_METHOD_SIG,
752 .param.sig = {
Juan Castillo516beb52015-12-03 10:19:21 +0000753 .pk = &trusted_world_pk,
Juan Castillod337aaa2015-06-10 15:27:52 +0100754 .sig = &sig,
755 .alg = &sig_alg,
756 .data = &raw_data,
757 }
758 }
759 },
760 .authenticated_data = {
761 [0] = {
Juan Castillo516beb52015-12-03 10:19:21 +0000762 .type_desc = &soc_fw_content_pk,
Juan Castillod337aaa2015-06-10 15:27:52 +0100763 .data = {
Juan Castillo516beb52015-12-03 10:19:21 +0000764 .ptr = (void *)content_pk_buf,
Juan Castillod337aaa2015-06-10 15:27:52 +0100765 .len = (unsigned int)PK_DER_LEN
766 }
767 }
768 }
769},
Juan Castillo516beb52015-12-03 10:19:21 +0000770[SOC_FW_CONTENT_CERT_ID] = {
771 .img_id = SOC_FW_CONTENT_CERT_ID,
Juan Castillod337aaa2015-06-10 15:27:52 +0100772 .img_type = IMG_CERT,
Juan Castillo516beb52015-12-03 10:19:21 +0000773 .parent = &cot_desc[SOC_FW_KEY_CERT_ID],
Juan Castillod337aaa2015-06-10 15:27:52 +0100774 .img_auth_methods = {
775 [0] = {
776 .type = AUTH_METHOD_SIG,
777 .param.sig = {
Juan Castillo516beb52015-12-03 10:19:21 +0000778 .pk = &soc_fw_content_pk,
Juan Castillod337aaa2015-06-10 15:27:52 +0100779 .sig = &sig,
780 .alg = &sig_alg,
781 .data = &raw_data,
782 }
783 }
784 },
785 .authenticated_data = {
786 [0] = {
Juan Castillo516beb52015-12-03 10:19:21 +0000787 .type_desc = &soc_fw_hash,
Juan Castillod337aaa2015-06-10 15:27:52 +0100788 .data = {
Juan Castillo516beb52015-12-03 10:19:21 +0000789 .ptr = (void *)soc_fw_hash_buf,
Juan Castillod337aaa2015-06-10 15:27:52 +0100790 .len = (unsigned int)HASH_DER_LEN
791 }
792 }
793 }
794},
795[BL31_IMAGE_ID] = {
796 .img_id = BL31_IMAGE_ID,
797 .img_type = IMG_RAW,
Juan Castillo516beb52015-12-03 10:19:21 +0000798 .parent = &cot_desc[SOC_FW_CONTENT_CERT_ID],
Juan Castillod337aaa2015-06-10 15:27:52 +0100799 .img_auth_methods = {
800 [0] = {
801 .type = AUTH_METHOD_HASH,
802 .param.hash = {
803 .data = &raw_data,
Juan Castillo516beb52015-12-03 10:19:21 +0000804 .hash = &soc_fw_hash,
Juan Castillod337aaa2015-06-10 15:27:52 +0100805 }
806 }
807 }
808}
809```
810The **Trusted Key certificate** is signed with the ROT private key and contains
811the Trusted World public key and the Non-Trusted World public key as x509v3
812extensions. This must be specified in the image descriptor using the
813`img_auth_methods` and `authenticated_data` arrays, respectively.
814
815The Trusted Key certificate is authenticated by checking its digital signature
816using the ROTPK. Four parameters are required to check a signature: the public
817key, the algorithm, the signature and the data that has been signed. Therefore,
818four parameter descriptors must be specified with the authentication method:
819
820* `subject_pk`: parameter descriptor of type `AUTH_PARAM_PUB_KEY`. This type
821 is used to extract a public key from the parent image. If the cookie is an
822 OID, the key is extracted from the corresponding x509v3 extension. If the
823 cookie is NULL, the subject public key is retrieved. In this case, because
824 the parent image is NULL, the public key is obtained from the platform
825 (this key will be the ROTPK).
826* `sig`: parameter descriptor of type `AUTH_PARAM_SIG`. It is used to extract
827 the signature from the certificate.
828* `sig_alg`: parameter descriptor of type `AUTH_PARAM_SIG`. It is used to
829 extract the signature algorithm from the certificate.
830* `raw_data`: parameter descriptor of type `AUTH_PARAM_RAW_DATA`. It is used
831 to extract the data to be signed from the certificate.
832
833Once the signature has been checked and the certificate authenticated, the
834Trusted World public key needs to be extracted from the certificate. A new entry
835is created in the `authenticated_data` array for that purpose. In that entry,
836the corresponding parameter descriptor must be specified along with the buffer
837address to store the parameter value. In this case, the `tz_world_pk` descriptor
838is used to extract the public key from an x509v3 extension with OID
Juan Castillo516beb52015-12-03 10:19:21 +0000839`TRUSTED_WORLD_PK_OID`. The BL3-1 key certificate will use this descriptor as
Juan Castillod337aaa2015-06-10 15:27:52 +0100840parameter in the signature authentication method. The key is stored in the
841`plat_tz_world_pk_buf` buffer.
842
843The **BL3-1 Key certificate** is authenticated by checking its digital signature
844using the Trusted World public key obtained previously from the Trusted Key
845certificate. In the image descriptor, we specify a single authentication method
846by signature whose public key is the `tz_world_pk`. Once this certificate has
847been authenticated, we have to extract the BL3-1 public key, stored in the
848extension specified by `bl31_content_pk`. This key will be copied to the
849`plat_content_pk` buffer.
850
851The **BL3-1 certificate** is authenticated by checking its digital signature
852using the BL3-1 public key obtained previously from the BL3-1 Key certificate.
853We specify the authentication method using `bl31_content_pk` as public key.
854After authentication, we need to extract the BL3-1 hash, stored in the extension
855specified by `bl31_hash`. This hash will be copied to the `plat_bl31_hash_buf`
856buffer.
857
858The **BL3-1 image** is authenticated by calculating its hash and matching it
859with the hash obtained from the BL3-1 certificate. The image descriptor contains
860a single authentication method by hash. The parameters to the hash method are
861the reference hash, `bl31_hash`, and the data to be hashed. In this case, it is
862the whole image, so we specify `raw_data`.
863
864### 4.2 The image parser library
865
866The image parser module relies on libraries to check the image integrity and
867extract the authentication parameters. The number and type of parser libraries
868depend on the images used in the CoT. Raw images do not need a library, so
869only an x509v3 library is required for the TBBR CoT.
870
Juan Castillo649dbf62015-11-05 09:24:53 +0000871ARM platforms will use an x509v3 library based on mbed TLS. This library may be
Juan Castillod337aaa2015-06-10 15:27:52 +0100872found in `drivers/auth/mbedtls/mbedtls_x509_parser.c`. It exports three
873functions:
874
875```
876void init(void);
877int check_integrity(void *img, unsigned int img_len);
878int get_auth_param(const auth_param_type_desc_t *type_desc,
879 void *img, unsigned int img_len,
880 void **param, unsigned int *param_len);
881```
882
883The library is registered in the framework using the macro
884`REGISTER_IMG_PARSER_LIB()`. Each time the image parser module needs to access
885an image of type `IMG_CERT`, it will call the corresponding function exported
886in this file.
887
888The build system must be updated to include the corresponding library and
Juan Castillo649dbf62015-11-05 09:24:53 +0000889mbed TLS sources. ARM platforms use the `arm_common.mk` file to pull the
890sources.
Juan Castillod337aaa2015-06-10 15:27:52 +0100891
892### 4.3 The cryptographic library
893
894The cryptographic module relies on a library to perform the required operations,
895i.e. verify a hash or a digital signature. ARM platforms will use a library
Juan Castillo649dbf62015-11-05 09:24:53 +0000896based on mbed TLS, which can be found in
897`drivers/auth/mbedtls/mbedtls_crypto.c`. This library is registered in the
898authentication framework using the macro `REGISTER_CRYPTO_LIB()` and exports
899three functions:
Juan Castillod337aaa2015-06-10 15:27:52 +0100900
901```
902void init(void);
903int verify_signature(void *data_ptr, unsigned int data_len,
904 void *sig_ptr, unsigned int sig_len,
905 void *sig_alg, unsigned int sig_alg_len,
906 void *pk_ptr, unsigned int pk_len);
907int verify_hash(void *data_ptr, unsigned int data_len,
908 void *digest_info_ptr, unsigned int digest_info_len);
909```
910
911The key algorithm (rsa, ecdsa) must be specified in the build system using the
912`MBEDTLS_KEY_ALG` variable, so the Makefile can include the corresponding
913sources in the build.
914
915- - - - - - - - - - - - - - - - - - - - - - - - - -
916
917_Copyright (c) 2015, ARM Limited and Contributors. All rights reserved._
918
919
920[Trusted Board Boot]: ./trusted-board-boot.md
921[Platform Porting Guide]: ./porting-guide.md