xref: /rk3399_ARM-atf/docs/design/auth-framework.rst (revision f29213d9e3c82f8b43e42023d5b39e097d86ff18)
18aa05055SPaul BeesleyAuthentication Framework & Chain of Trust
28aa05055SPaul Beesley=========================================
340d553cfSPaul Beesley
440d553cfSPaul BeesleyThe aim of this document is to describe the authentication framework
540d553cfSPaul Beesleyimplemented in Trusted Firmware-A (TF-A). This framework fulfills the
640d553cfSPaul Beesleyfollowing requirements:
740d553cfSPaul Beesley
840d553cfSPaul Beesley#. It should be possible for a platform port to specify the Chain of Trust in
940d553cfSPaul Beesley   terms of certificate hierarchy and the mechanisms used to verify a
1040d553cfSPaul Beesley   particular image/certificate.
1140d553cfSPaul Beesley
1240d553cfSPaul Beesley#. The framework should distinguish between:
1340d553cfSPaul Beesley
1440d553cfSPaul Beesley   -  The mechanism used to encode and transport information, e.g. DER encoded
1540d553cfSPaul Beesley      X.509v3 certificates to ferry Subject Public Keys, hashes and non-volatile
1640d553cfSPaul Beesley      counters.
1740d553cfSPaul Beesley
1840d553cfSPaul Beesley   -  The mechanism used to verify the transported information i.e. the
1940d553cfSPaul Beesley      cryptographic libraries.
2040d553cfSPaul Beesley
2140d553cfSPaul BeesleyThe framework has been designed following a modular approach illustrated in the
2240d553cfSPaul Beesleynext diagram:
2340d553cfSPaul Beesley
2440d553cfSPaul Beesley::
2540d553cfSPaul Beesley
2640d553cfSPaul Beesley        +---------------+---------------+------------+
2740d553cfSPaul Beesley        | Trusted       | Trusted       | Trusted    |
2840d553cfSPaul Beesley        | Firmware      | Firmware      | Firmware   |
2940d553cfSPaul Beesley        | Generic       | IO Framework  | Platform   |
3040d553cfSPaul Beesley        | Code i.e.     | (IO)          | Port       |
3140d553cfSPaul Beesley        | BL1/BL2 (GEN) |               | (PP)       |
3240d553cfSPaul Beesley        +---------------+---------------+------------+
3340d553cfSPaul Beesley               ^               ^               ^
3440d553cfSPaul Beesley               |               |               |
3540d553cfSPaul Beesley               v               v               v
3640d553cfSPaul Beesley         +-----------+   +-----------+   +-----------+
3740d553cfSPaul Beesley         |           |   |           |   | Image     |
3840d553cfSPaul Beesley         | Crypto    |   | Auth      |   | Parser    |
3940d553cfSPaul Beesley         | Module    |<->| Module    |<->| Module    |
4040d553cfSPaul Beesley         | (CM)      |   | (AM)      |   | (IPM)     |
4140d553cfSPaul Beesley         |           |   |           |   |           |
4240d553cfSPaul Beesley         +-----------+   +-----------+   +-----------+
4340d553cfSPaul Beesley               ^                               ^
4440d553cfSPaul Beesley               |                               |
4540d553cfSPaul Beesley               v                               v
4640d553cfSPaul Beesley        +----------------+             +-----------------+
4740d553cfSPaul Beesley        | Cryptographic  |             | Image Parser    |
4840d553cfSPaul Beesley        | Libraries (CL) |             | Libraries (IPL) |
4940d553cfSPaul Beesley        +----------------+             +-----------------+
5040d553cfSPaul Beesley                      |                |
5140d553cfSPaul Beesley                      |                |
5240d553cfSPaul Beesley                      |                |
5340d553cfSPaul Beesley                      v                v
5440d553cfSPaul Beesley                     +-----------------+
5540d553cfSPaul Beesley                     | Misc. Libs e.g. |
5640d553cfSPaul Beesley                     | ASN.1 decoder   |
5740d553cfSPaul Beesley                     |                 |
5840d553cfSPaul Beesley                     +-----------------+
5940d553cfSPaul Beesley
6040d553cfSPaul Beesley        DIAGRAM 1.
6140d553cfSPaul Beesley
6240d553cfSPaul BeesleyThis document describes the inner details of the authentication framework and
6340d553cfSPaul Beesleythe abstraction mechanisms available to specify a Chain of Trust.
6440d553cfSPaul Beesley
6540d553cfSPaul BeesleyFramework design
6640d553cfSPaul Beesley----------------
6740d553cfSPaul Beesley
6840d553cfSPaul BeesleyThis section describes some aspects of the framework design and the rationale
6940d553cfSPaul Beesleybehind them. These aspects are key to verify a Chain of Trust.
7040d553cfSPaul Beesley
7140d553cfSPaul BeesleyChain of Trust
7240d553cfSPaul Beesley~~~~~~~~~~~~~~
7340d553cfSPaul Beesley
7440d553cfSPaul BeesleyA CoT is basically a sequence of authentication images which usually starts with
7540d553cfSPaul Beesleya root of trust and culminates in a single data image. The following diagram
7640d553cfSPaul Beesleyillustrates how this maps to a CoT for the BL31 image described in the
7740d553cfSPaul Beesley`TBBR-Client specification`_.
7840d553cfSPaul Beesley
7940d553cfSPaul Beesley::
8040d553cfSPaul Beesley
8140d553cfSPaul Beesley        +------------------+       +-------------------+
8240d553cfSPaul Beesley        | ROTPK/ROTPK Hash |------>| Trusted Key       |
8340d553cfSPaul Beesley        +------------------+       | Certificate       |
8440d553cfSPaul Beesley                                   | (Auth Image)      |
8540d553cfSPaul Beesley                                  /+-------------------+
8640d553cfSPaul Beesley                                 /            |
8740d553cfSPaul Beesley                                /             |
8840d553cfSPaul Beesley                               /              |
8940d553cfSPaul Beesley                              /               |
9040d553cfSPaul Beesley                             L                v
9140d553cfSPaul Beesley        +------------------+       +-------------------+
9240d553cfSPaul Beesley        | Trusted World    |------>| BL31 Key          |
9340d553cfSPaul Beesley        | Public Key       |       | Certificate       |
9440d553cfSPaul Beesley        +------------------+       | (Auth Image)      |
9540d553cfSPaul Beesley                                   +-------------------+
9640d553cfSPaul Beesley                                  /           |
9740d553cfSPaul Beesley                                 /            |
9840d553cfSPaul Beesley                                /             |
9940d553cfSPaul Beesley                               /              |
10040d553cfSPaul Beesley                              /               v
10140d553cfSPaul Beesley        +------------------+ L     +-------------------+
10240d553cfSPaul Beesley        | BL31 Content     |------>| BL31 Content      |
10340d553cfSPaul Beesley        | Certificate PK   |       | Certificate       |
10440d553cfSPaul Beesley        +------------------+       | (Auth Image)      |
10540d553cfSPaul Beesley                                   +-------------------+
10640d553cfSPaul Beesley                                  /           |
10740d553cfSPaul Beesley                                 /            |
10840d553cfSPaul Beesley                                /             |
10940d553cfSPaul Beesley                               /              |
11040d553cfSPaul Beesley                              /               v
11140d553cfSPaul Beesley        +------------------+ L     +-------------------+
11240d553cfSPaul Beesley        | BL31 Hash        |------>| BL31 Image        |
11340d553cfSPaul Beesley        |                  |       | (Data Image)      |
11440d553cfSPaul Beesley        +------------------+       |                   |
11540d553cfSPaul Beesley                                   +-------------------+
11640d553cfSPaul Beesley
11740d553cfSPaul Beesley        DIAGRAM 2.
11840d553cfSPaul Beesley
11940d553cfSPaul BeesleyThe root of trust is usually a public key (ROTPK) that has been burnt in the
12040d553cfSPaul Beesleyplatform and cannot be modified.
12140d553cfSPaul Beesley
12240d553cfSPaul BeesleyImage types
12340d553cfSPaul Beesley~~~~~~~~~~~
12440d553cfSPaul Beesley
12540d553cfSPaul BeesleyImages in a CoT are categorised as authentication and data images. An
12640d553cfSPaul Beesleyauthentication image contains information to authenticate a data image or
12740d553cfSPaul Beesleyanother authentication image. A data image is usually a boot loader binary, but
12840d553cfSPaul Beesleyit could be any other data that requires authentication.
12940d553cfSPaul Beesley
13040d553cfSPaul BeesleyComponent responsibilities
13140d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~
13240d553cfSPaul Beesley
13340d553cfSPaul BeesleyFor every image in a Chain of Trust, the following high level operations are
13440d553cfSPaul Beesleyperformed to verify it:
13540d553cfSPaul Beesley
13640d553cfSPaul Beesley#. Allocate memory for the image either statically or at runtime.
13740d553cfSPaul Beesley
13840d553cfSPaul Beesley#. Identify the image and load it in the allocated memory.
13940d553cfSPaul Beesley
14040d553cfSPaul Beesley#. Check the integrity of the image as per its type.
14140d553cfSPaul Beesley
14240d553cfSPaul Beesley#. Authenticate the image as per the cryptographic algorithms used.
14340d553cfSPaul Beesley
14440d553cfSPaul Beesley#. If the image is an authentication image, extract the information that will
14540d553cfSPaul Beesley   be used to authenticate the next image in the CoT.
14640d553cfSPaul Beesley
14740d553cfSPaul BeesleyIn Diagram 1, each component is responsible for one or more of these operations.
14840d553cfSPaul BeesleyThe responsibilities are briefly described below.
14940d553cfSPaul Beesley
15040d553cfSPaul BeesleyTF-A Generic code and IO framework (GEN/IO)
15140d553cfSPaul Beesley^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15240d553cfSPaul Beesley
15340d553cfSPaul BeesleyThese components are responsible for initiating the authentication process for a
15440d553cfSPaul Beesleyparticular image in BL1 or BL2. For each BL image that requires authentication,
15540d553cfSPaul Beesleythe Generic code asks recursively the Authentication module what is the parent
15640d553cfSPaul Beesleyimage until either an authenticated image or the ROT is reached. Then the
15740d553cfSPaul BeesleyGeneric code calls the IO framework to load the image and calls the
15840d553cfSPaul BeesleyAuthentication module to authenticate it, following the CoT from ROT to Image.
15940d553cfSPaul Beesley
16040d553cfSPaul BeesleyTF-A Platform Port (PP)
16140d553cfSPaul Beesley^^^^^^^^^^^^^^^^^^^^^^^
16240d553cfSPaul Beesley
16340d553cfSPaul BeesleyThe platform is responsible for:
16440d553cfSPaul Beesley
16540d553cfSPaul Beesley#. Specifying the CoT for each image that needs to be authenticated. Details of
16640d553cfSPaul Beesley   how a CoT can be specified by the platform are explained later. The platform
16740d553cfSPaul Beesley   also specifies the authentication methods and the parsing method used for
16840d553cfSPaul Beesley   each image.
16940d553cfSPaul Beesley
17040d553cfSPaul Beesley#. Statically allocating memory for each parameter in each image which is
17140d553cfSPaul Beesley   used for verifying the CoT, e.g. memory for public keys, hashes etc.
17240d553cfSPaul Beesley
17340d553cfSPaul Beesley#. Providing the ROTPK or a hash of it.
17440d553cfSPaul Beesley
17540d553cfSPaul Beesley#. Providing additional information to the IPM to enable it to identify and
17640d553cfSPaul Beesley   extract authentication parameters contained in an image, e.g. if the
17740d553cfSPaul Beesley   parameters are stored as X509v3 extensions, the corresponding OID must be
17840d553cfSPaul Beesley   provided.
17940d553cfSPaul Beesley
18040d553cfSPaul Beesley#. Fulfill any other memory requirements of the IPM and the CM (not currently
18140d553cfSPaul Beesley   described in this document).
18240d553cfSPaul Beesley
18340d553cfSPaul Beesley#. Export functions to verify an image which uses an authentication method that
18440d553cfSPaul Beesley   cannot be interpreted by the CM, e.g. if an image has to be verified using a
18540d553cfSPaul Beesley   NV counter, then the value of the counter to compare with can only be
18640d553cfSPaul Beesley   provided by the platform.
18740d553cfSPaul Beesley
18840d553cfSPaul Beesley#. Export a custom IPM if a proprietary image format is being used (described
18940d553cfSPaul Beesley   later).
19040d553cfSPaul Beesley
19140d553cfSPaul BeesleyAuthentication Module (AM)
19240d553cfSPaul Beesley^^^^^^^^^^^^^^^^^^^^^^^^^^
19340d553cfSPaul Beesley
19440d553cfSPaul BeesleyIt is responsible for:
19540d553cfSPaul Beesley
19640d553cfSPaul Beesley#. Providing the necessary abstraction mechanisms to describe a CoT. Amongst
19740d553cfSPaul Beesley   other things, the authentication and image parsing methods must be specified
19840d553cfSPaul Beesley   by the PP in the CoT.
19940d553cfSPaul Beesley
20040d553cfSPaul Beesley#. Verifying the CoT passed by GEN by utilising functionality exported by the
20140d553cfSPaul Beesley   PP, IPM and CM.
20240d553cfSPaul Beesley
20340d553cfSPaul Beesley#. Tracking which images have been verified. In case an image is a part of
20440d553cfSPaul Beesley   multiple CoTs then it should be verified only once e.g. the Trusted World
20540d553cfSPaul Beesley   Key Certificate in the TBBR-Client spec. contains information to verify
20640d553cfSPaul Beesley   SCP_BL2, BL31, BL32 each of which have a separate CoT. (This
20740d553cfSPaul Beesley   responsibility has not been described in this document but should be
20840d553cfSPaul Beesley   trivial to implement).
20940d553cfSPaul Beesley
21040d553cfSPaul Beesley#. Reusing memory meant for a data image to verify authentication images e.g.
21140d553cfSPaul Beesley   in the CoT described in Diagram 2, each certificate can be loaded and
21240d553cfSPaul Beesley   verified in the memory reserved by the platform for the BL31 image. By the
21340d553cfSPaul Beesley   time BL31 (the data image) is loaded, all information to authenticate it
21440d553cfSPaul Beesley   will have been extracted from the parent image i.e. BL31 content
21540d553cfSPaul Beesley   certificate. It is assumed that the size of an authentication image will
21640d553cfSPaul Beesley   never exceed the size of a data image. It should be possible to verify this
21740d553cfSPaul Beesley   at build time using asserts.
21840d553cfSPaul Beesley
21940d553cfSPaul BeesleyCryptographic Module (CM)
22040d553cfSPaul Beesley^^^^^^^^^^^^^^^^^^^^^^^^^
22140d553cfSPaul Beesley
22240d553cfSPaul BeesleyThe CM is responsible for providing an API to:
22340d553cfSPaul Beesley
22440d553cfSPaul Beesley#. Verify a digital signature.
22540d553cfSPaul Beesley#. Verify a hash.
22640d553cfSPaul Beesley
22740d553cfSPaul BeesleyThe CM does not include any cryptography related code, but it relies on an
22840d553cfSPaul Beesleyexternal library to perform the cryptographic operations. A Crypto-Library (CL)
22940d553cfSPaul Beesleylinking the CM and the external library must be implemented. The following
23040d553cfSPaul Beesleyfunctions must be provided by the CL:
23140d553cfSPaul Beesley
23240d553cfSPaul Beesley.. code:: c
23340d553cfSPaul Beesley
23440d553cfSPaul Beesley    void (*init)(void);
23540d553cfSPaul Beesley    int (*verify_signature)(void *data_ptr, unsigned int data_len,
23640d553cfSPaul Beesley                            void *sig_ptr, unsigned int sig_len,
23740d553cfSPaul Beesley                            void *sig_alg, unsigned int sig_alg_len,
23840d553cfSPaul Beesley                            void *pk_ptr, unsigned int pk_len);
23940d553cfSPaul Beesley    int (*verify_hash)(void *data_ptr, unsigned int data_len,
24040d553cfSPaul Beesley                       void *digest_info_ptr, unsigned int digest_info_len);
24140d553cfSPaul Beesley
24240d553cfSPaul BeesleyThese functions are registered in the CM using the macro:
24340d553cfSPaul Beesley
24440d553cfSPaul Beesley.. code:: c
24540d553cfSPaul Beesley
24640d553cfSPaul Beesley    REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash);
24740d553cfSPaul Beesley
24840d553cfSPaul Beesley``_name`` must be a string containing the name of the CL. This name is used for
24940d553cfSPaul Beesleydebugging purposes.
25040d553cfSPaul Beesley
25140d553cfSPaul BeesleyImage Parser Module (IPM)
25240d553cfSPaul Beesley^^^^^^^^^^^^^^^^^^^^^^^^^
25340d553cfSPaul Beesley
25440d553cfSPaul BeesleyThe IPM is responsible for:
25540d553cfSPaul Beesley
25640d553cfSPaul Beesley#. Checking the integrity of each image loaded by the IO framework.
25740d553cfSPaul Beesley#. Extracting parameters used for authenticating an image based upon a
25840d553cfSPaul Beesley   description provided by the platform in the CoT descriptor.
25940d553cfSPaul Beesley
26040d553cfSPaul BeesleyImages may have different formats (for example, authentication images could be
26140d553cfSPaul Beesleyx509v3 certificates, signed ELF files or any other platform specific format).
26240d553cfSPaul BeesleyThe IPM allows to register an Image Parser Library (IPL) for every image format
26340d553cfSPaul Beesleyused in the CoT. This library must implement the specific methods to parse the
26440d553cfSPaul Beesleyimage. The IPM obtains the image format from the CoT and calls the right IPL to
26540d553cfSPaul Beesleycheck the image integrity and extract the authentication parameters.
26640d553cfSPaul Beesley
26740d553cfSPaul BeesleySee Section "Describing the image parsing methods" for more details about the
26840d553cfSPaul Beesleymechanism the IPM provides to define and register IPLs.
26940d553cfSPaul Beesley
27040d553cfSPaul BeesleyAuthentication methods
27140d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~
27240d553cfSPaul Beesley
27340d553cfSPaul BeesleyThe AM supports the following authentication methods:
27440d553cfSPaul Beesley
27540d553cfSPaul Beesley#. Hash
27640d553cfSPaul Beesley#. Digital signature
27740d553cfSPaul Beesley
27840d553cfSPaul BeesleyThe platform may specify these methods in the CoT in case it decides to define
27940d553cfSPaul Beesleya custom CoT instead of reusing a predefined one.
28040d553cfSPaul Beesley
28140d553cfSPaul BeesleyIf a data image uses multiple methods, then all the methods must be a part of
28240d553cfSPaul Beesleythe same CoT. The number and type of parameters are method specific. These
28340d553cfSPaul Beesleyparameters should be obtained from the parent image using the IPM.
28440d553cfSPaul Beesley
28540d553cfSPaul Beesley#. Hash
28640d553cfSPaul Beesley
28740d553cfSPaul Beesley   Parameters:
28840d553cfSPaul Beesley
28940d553cfSPaul Beesley   #. A pointer to data to hash
29040d553cfSPaul Beesley   #. Length of the data
29140d553cfSPaul Beesley   #. A pointer to the hash
29240d553cfSPaul Beesley   #. Length of the hash
29340d553cfSPaul Beesley
29440d553cfSPaul Beesley   The hash will be represented by the DER encoding of the following ASN.1
29540d553cfSPaul Beesley   type:
29640d553cfSPaul Beesley
29740d553cfSPaul Beesley   ::
29840d553cfSPaul Beesley
29940d553cfSPaul Beesley       DigestInfo ::= SEQUENCE {
30040d553cfSPaul Beesley           digestAlgorithm  DigestAlgorithmIdentifier,
30140d553cfSPaul Beesley           digest           Digest
30240d553cfSPaul Beesley       }
30340d553cfSPaul Beesley
30440d553cfSPaul Beesley   This ASN.1 structure makes it possible to remove any assumption about the
30540d553cfSPaul Beesley   type of hash algorithm used as this information accompanies the hash. This
30640d553cfSPaul Beesley   should allow the Cryptography Library (CL) to support multiple hash
30740d553cfSPaul Beesley   algorithm implementations.
30840d553cfSPaul Beesley
30940d553cfSPaul Beesley#. Digital Signature
31040d553cfSPaul Beesley
31140d553cfSPaul Beesley   Parameters:
31240d553cfSPaul Beesley
31340d553cfSPaul Beesley   #. A pointer to data to sign
31440d553cfSPaul Beesley   #. Length of the data
31540d553cfSPaul Beesley   #. Public Key Algorithm
31640d553cfSPaul Beesley   #. Public Key value
31740d553cfSPaul Beesley   #. Digital Signature Algorithm
31840d553cfSPaul Beesley   #. Digital Signature value
31940d553cfSPaul Beesley
32040d553cfSPaul Beesley   The Public Key parameters will be represented by the DER encoding of the
32140d553cfSPaul Beesley   following ASN.1 type:
32240d553cfSPaul Beesley
32340d553cfSPaul Beesley   ::
32440d553cfSPaul Beesley
32540d553cfSPaul Beesley       SubjectPublicKeyInfo  ::=  SEQUENCE  {
32640d553cfSPaul Beesley           algorithm         AlgorithmIdentifier{PUBLIC-KEY,{PublicKeyAlgorithms}},
32740d553cfSPaul Beesley           subjectPublicKey  BIT STRING  }
32840d553cfSPaul Beesley
32940d553cfSPaul Beesley   The Digital Signature Algorithm will be represented by the DER encoding of
33040d553cfSPaul Beesley   the following ASN.1 types.
33140d553cfSPaul Beesley
33240d553cfSPaul Beesley   ::
33340d553cfSPaul Beesley
33440d553cfSPaul Beesley       AlgorithmIdentifier {ALGORITHM:IOSet } ::= SEQUENCE {
33540d553cfSPaul Beesley           algorithm         ALGORITHM.&id({IOSet}),
33640d553cfSPaul Beesley           parameters        ALGORITHM.&Type({IOSet}{@algorithm}) OPTIONAL
33740d553cfSPaul Beesley       }
33840d553cfSPaul Beesley
33940d553cfSPaul Beesley   The digital signature will be represented by:
34040d553cfSPaul Beesley
34140d553cfSPaul Beesley   ::
34240d553cfSPaul Beesley
34340d553cfSPaul Beesley       signature  ::=  BIT STRING
34440d553cfSPaul Beesley
34540d553cfSPaul BeesleyThe authentication framework will use the image descriptor to extract all the
34640d553cfSPaul Beesleyinformation related to authentication.
34740d553cfSPaul Beesley
34840d553cfSPaul BeesleySpecifying a Chain of Trust
34940d553cfSPaul Beesley---------------------------
35040d553cfSPaul Beesley
35140d553cfSPaul BeesleyA CoT can be described as a set of image descriptors linked together in a
35240d553cfSPaul Beesleyparticular order. The order dictates the sequence in which they must be
35340d553cfSPaul Beesleyverified. Each image has a set of properties which allow the AM to verify it.
35440d553cfSPaul BeesleyThese properties are described below.
35540d553cfSPaul Beesley
35640d553cfSPaul BeesleyThe PP is responsible for defining a single or multiple CoTs for a data image.
35740d553cfSPaul BeesleyUnless otherwise specified, the data structures described in the following
35840d553cfSPaul Beesleysections are populated by the PP statically.
35940d553cfSPaul Beesley
36040d553cfSPaul BeesleyDescribing the image parsing methods
36140d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36240d553cfSPaul Beesley
36340d553cfSPaul BeesleyThe parsing method refers to the format of a particular image. For example, an
36440d553cfSPaul Beesleyauthentication image that represents a certificate could be in the X.509v3
36540d553cfSPaul Beesleyformat. A data image that represents a boot loader stage could be in raw binary
36640d553cfSPaul Beesleyor ELF format. The IPM supports three parsing methods. An image has to use one
36740d553cfSPaul Beesleyof the three methods described below. An IPL is responsible for interpreting a
36840d553cfSPaul Beesleysingle parsing method. There has to be one IPL for every method used by the
36940d553cfSPaul Beesleyplatform.
37040d553cfSPaul Beesley
37140d553cfSPaul Beesley#. Raw format: This format is effectively a nop as an image using this method
37240d553cfSPaul Beesley   is treated as being in raw binary format e.g. boot loader images used by
37340d553cfSPaul Beesley   TF-A. This method should only be used by data images.
37440d553cfSPaul Beesley
37540d553cfSPaul Beesley#. X509V3 method: This method uses industry standards like X.509 to represent
37640d553cfSPaul Beesley   PKI certificates (authentication images). It is expected that open source
37740d553cfSPaul Beesley   libraries will be available which can be used to parse an image represented
37840d553cfSPaul Beesley   by this method. Such libraries can be used to write the corresponding IPL
37940d553cfSPaul Beesley   e.g. the X.509 parsing library code in mbed TLS.
38040d553cfSPaul Beesley
38140d553cfSPaul Beesley#. Platform defined method: This method caters for platform specific
38240d553cfSPaul Beesley   proprietary standards to represent authentication or data images. For
38340d553cfSPaul Beesley   example, The signature of a data image could be appended to the data image
38440d553cfSPaul Beesley   raw binary. A header could be prepended to the combined blob to specify the
38540d553cfSPaul Beesley   extents of each component. The platform will have to implement the
38640d553cfSPaul Beesley   corresponding IPL to interpret such a format.
38740d553cfSPaul Beesley
38840d553cfSPaul BeesleyThe following enum can be used to define these three methods.
38940d553cfSPaul Beesley
39040d553cfSPaul Beesley.. code:: c
39140d553cfSPaul Beesley
39240d553cfSPaul Beesley    typedef enum img_type_enum {
39340d553cfSPaul Beesley        IMG_RAW,            /* Binary image */
39440d553cfSPaul Beesley        IMG_PLAT,           /* Platform specific format */
39540d553cfSPaul Beesley        IMG_CERT,           /* X509v3 certificate */
39640d553cfSPaul Beesley        IMG_MAX_TYPES,
39740d553cfSPaul Beesley    } img_type_t;
39840d553cfSPaul Beesley
39940d553cfSPaul BeesleyAn IPL must provide functions with the following prototypes:
40040d553cfSPaul Beesley
40140d553cfSPaul Beesley.. code:: c
40240d553cfSPaul Beesley
40340d553cfSPaul Beesley    void init(void);
40440d553cfSPaul Beesley    int check_integrity(void *img, unsigned int img_len);
40540d553cfSPaul Beesley    int get_auth_param(const auth_param_type_desc_t *type_desc,
40640d553cfSPaul Beesley                          void *img, unsigned int img_len,
40740d553cfSPaul Beesley                          void **param, unsigned int *param_len);
40840d553cfSPaul Beesley
40940d553cfSPaul BeesleyAn IPL for each type must be registered using the following macro:
41040d553cfSPaul Beesley
41129c02529SPaul Beesley.. code:: c
41240d553cfSPaul Beesley
41340d553cfSPaul Beesley    REGISTER_IMG_PARSER_LIB(_type, _name, _init, _check_int, _get_param)
41440d553cfSPaul Beesley
41540d553cfSPaul Beesley-  ``_type``: one of the types described above.
41640d553cfSPaul Beesley-  ``_name``: a string containing the IPL name for debugging purposes.
41740d553cfSPaul Beesley-  ``_init``: initialization function pointer.
41840d553cfSPaul Beesley-  ``_check_int``: check image integrity function pointer.
41940d553cfSPaul Beesley-  ``_get_param``: extract authentication parameter function pointer.
42040d553cfSPaul Beesley
42140d553cfSPaul BeesleyThe ``init()`` function will be used to initialize the IPL.
42240d553cfSPaul Beesley
42340d553cfSPaul BeesleyThe ``check_integrity()`` function is passed a pointer to the memory where the
42440d553cfSPaul Beesleyimage has been loaded by the IO framework and the image length. It should ensure
42540d553cfSPaul Beesleythat the image is in the format corresponding to the parsing method and has not
42640d553cfSPaul Beesleybeen tampered with. For example, RFC-2459 describes a validation sequence for an
42740d553cfSPaul BeesleyX.509 certificate.
42840d553cfSPaul Beesley
42940d553cfSPaul BeesleyThe ``get_auth_param()`` function is passed a parameter descriptor containing
43040d553cfSPaul Beesleyinformation about the parameter (``type_desc`` and ``cookie``) to identify and
43140d553cfSPaul Beesleyextract the data corresponding to that parameter from an image. This data will
43240d553cfSPaul Beesleybe used to verify either the current or the next image in the CoT sequence.
43340d553cfSPaul Beesley
43440d553cfSPaul BeesleyEach image in the CoT will specify the parsing method it uses. This information
43540d553cfSPaul Beesleywill be used by the IPM to find the right parser descriptor for the image.
43640d553cfSPaul Beesley
43740d553cfSPaul BeesleyDescribing the authentication method(s)
43840d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43940d553cfSPaul Beesley
44040d553cfSPaul BeesleyAs part of the CoT, each image has to specify one or more authentication methods
44140d553cfSPaul Beesleywhich will be used to verify it. As described in the Section "Authentication
44240d553cfSPaul Beesleymethods", there are three methods supported by the AM.
44340d553cfSPaul Beesley
44440d553cfSPaul Beesley.. code:: c
44540d553cfSPaul Beesley
44640d553cfSPaul Beesley    typedef enum {
44740d553cfSPaul Beesley        AUTH_METHOD_NONE,
44840d553cfSPaul Beesley        AUTH_METHOD_HASH,
44940d553cfSPaul Beesley        AUTH_METHOD_SIG,
45040d553cfSPaul Beesley        AUTH_METHOD_NUM
45140d553cfSPaul Beesley    } auth_method_type_t;
45240d553cfSPaul Beesley
45340d553cfSPaul BeesleyThe AM defines the type of each parameter used by an authentication method. It
45440d553cfSPaul Beesleyuses this information to:
45540d553cfSPaul Beesley
45640d553cfSPaul Beesley#. Specify to the ``get_auth_param()`` function exported by the IPM, which
45740d553cfSPaul Beesley   parameter should be extracted from an image.
45840d553cfSPaul Beesley
45940d553cfSPaul Beesley#. Correctly marshall the parameters while calling the verification function
46040d553cfSPaul Beesley   exported by the CM and PP.
46140d553cfSPaul Beesley
46240d553cfSPaul Beesley#. Extract authentication parameters from a parent image in order to verify a
46340d553cfSPaul Beesley   child image e.g. to verify the certificate image, the public key has to be
46440d553cfSPaul Beesley   obtained from the parent image.
46540d553cfSPaul Beesley
46640d553cfSPaul Beesley.. code:: c
46740d553cfSPaul Beesley
46840d553cfSPaul Beesley    typedef enum {
46940d553cfSPaul Beesley        AUTH_PARAM_NONE,
47040d553cfSPaul Beesley        AUTH_PARAM_RAW_DATA,        /* Raw image data */
47140d553cfSPaul Beesley        AUTH_PARAM_SIG,         /* The image signature */
47240d553cfSPaul Beesley        AUTH_PARAM_SIG_ALG,     /* The image signature algorithm */
47340d553cfSPaul Beesley        AUTH_PARAM_HASH,        /* A hash (including the algorithm) */
47440d553cfSPaul Beesley        AUTH_PARAM_PUB_KEY,     /* A public key */
47540d553cfSPaul Beesley    } auth_param_type_t;
47640d553cfSPaul Beesley
47740d553cfSPaul BeesleyThe AM defines the following structure to identify an authentication parameter
47840d553cfSPaul Beesleyrequired to verify an image.
47940d553cfSPaul Beesley
48040d553cfSPaul Beesley.. code:: c
48140d553cfSPaul Beesley
48240d553cfSPaul Beesley    typedef struct auth_param_type_desc_s {
48340d553cfSPaul Beesley        auth_param_type_t type;
48440d553cfSPaul Beesley        void *cookie;
48540d553cfSPaul Beesley    } auth_param_type_desc_t;
48640d553cfSPaul Beesley
48740d553cfSPaul Beesley``cookie`` is used by the platform to specify additional information to the IPM
48840d553cfSPaul Beesleywhich enables it to uniquely identify the parameter that should be extracted
48940d553cfSPaul Beesleyfrom an image. For example, the hash of a BL3x image in its corresponding
49040d553cfSPaul Beesleycontent certificate is stored in an X509v3 custom extension field. An extension
49140d553cfSPaul Beesleyfield can only be identified using an OID. In this case, the ``cookie`` could
49240d553cfSPaul Beesleycontain the pointer to the OID defined by the platform for the hash extension
49340d553cfSPaul Beesleyfield while the ``type`` field could be set to ``AUTH_PARAM_HASH``. A value of 0 for
49440d553cfSPaul Beesleythe ``cookie`` field means that it is not used.
49540d553cfSPaul Beesley
49640d553cfSPaul BeesleyFor each method, the AM defines a structure with the parameters required to
49740d553cfSPaul Beesleyverify the image.
49840d553cfSPaul Beesley
49940d553cfSPaul Beesley.. code:: c
50040d553cfSPaul Beesley
50140d553cfSPaul Beesley    /*
50240d553cfSPaul Beesley     * Parameters for authentication by hash matching
50340d553cfSPaul Beesley     */
50440d553cfSPaul Beesley    typedef struct auth_method_param_hash_s {
50540d553cfSPaul Beesley        auth_param_type_desc_t *data;   /* Data to hash */
50640d553cfSPaul Beesley        auth_param_type_desc_t *hash;   /* Hash to match with */
50740d553cfSPaul Beesley    } auth_method_param_hash_t;
50840d553cfSPaul Beesley
50940d553cfSPaul Beesley    /*
51040d553cfSPaul Beesley     * Parameters for authentication by signature
51140d553cfSPaul Beesley     */
51240d553cfSPaul Beesley    typedef struct auth_method_param_sig_s {
51340d553cfSPaul Beesley        auth_param_type_desc_t *pk; /* Public key */
51440d553cfSPaul Beesley        auth_param_type_desc_t *sig;    /* Signature to check */
51540d553cfSPaul Beesley        auth_param_type_desc_t *alg;    /* Signature algorithm */
51640d553cfSPaul Beesley        auth_param_type_desc_t *tbs;    /* Data signed */
51740d553cfSPaul Beesley    } auth_method_param_sig_t;
51840d553cfSPaul Beesley
51940d553cfSPaul BeesleyThe AM defines the following structure to describe an authentication method for
52040d553cfSPaul Beesleyverifying an image
52140d553cfSPaul Beesley
52240d553cfSPaul Beesley.. code:: c
52340d553cfSPaul Beesley
52440d553cfSPaul Beesley    /*
52540d553cfSPaul Beesley     * Authentication method descriptor
52640d553cfSPaul Beesley     */
52740d553cfSPaul Beesley    typedef struct auth_method_desc_s {
52840d553cfSPaul Beesley        auth_method_type_t type;
52940d553cfSPaul Beesley        union {
53040d553cfSPaul Beesley            auth_method_param_hash_t hash;
53140d553cfSPaul Beesley            auth_method_param_sig_t sig;
53240d553cfSPaul Beesley        } param;
53340d553cfSPaul Beesley    } auth_method_desc_t;
53440d553cfSPaul Beesley
53540d553cfSPaul BeesleyUsing the method type specified in the ``type`` field, the AM finds out what field
53640d553cfSPaul Beesleyneeds to access within the ``param`` union.
53740d553cfSPaul Beesley
53840d553cfSPaul BeesleyStoring Authentication parameters
53940d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54040d553cfSPaul Beesley
54140d553cfSPaul BeesleyA parameter described by ``auth_param_type_desc_t`` to verify an image could be
54240d553cfSPaul Beesleyobtained from either the image itself or its parent image. The memory allocated
54340d553cfSPaul Beesleyfor loading the parent image will be reused for loading the child image. Hence
54440d553cfSPaul Beesleyparameters which are obtained from the parent for verifying a child image need
54540d553cfSPaul Beesleyto have memory allocated for them separately where they can be stored. This
54640d553cfSPaul Beesleymemory must be statically allocated by the platform port.
54740d553cfSPaul Beesley
54840d553cfSPaul BeesleyThe AM defines the following structure to store the data corresponding to an
54940d553cfSPaul Beesleyauthentication parameter.
55040d553cfSPaul Beesley
55140d553cfSPaul Beesley.. code:: c
55240d553cfSPaul Beesley
55340d553cfSPaul Beesley    typedef struct auth_param_data_desc_s {
55440d553cfSPaul Beesley        void *auth_param_ptr;
55540d553cfSPaul Beesley        unsigned int auth_param_len;
55640d553cfSPaul Beesley    } auth_param_data_desc_t;
55740d553cfSPaul Beesley
55840d553cfSPaul BeesleyThe ``auth_param_ptr`` field is initialized by the platform. The ``auth_param_len``
55940d553cfSPaul Beesleyfield is used to specify the length of the data in the memory.
56040d553cfSPaul Beesley
56140d553cfSPaul BeesleyFor parameters that can be obtained from the child image itself, the IPM is
56240d553cfSPaul Beesleyresponsible for populating the ``auth_param_ptr`` and ``auth_param_len`` fields
56340d553cfSPaul Beesleywhile executing the ``img_get_auth_param()`` function.
56440d553cfSPaul Beesley
56540d553cfSPaul BeesleyThe AM defines the following structure to enable an image to describe the
56640d553cfSPaul Beesleyparameters that should be extracted from it and used to verify the next image
56740d553cfSPaul Beesley(child) in a CoT.
56840d553cfSPaul Beesley
56940d553cfSPaul Beesley.. code:: c
57040d553cfSPaul Beesley
57140d553cfSPaul Beesley    typedef struct auth_param_desc_s {
57240d553cfSPaul Beesley        auth_param_type_desc_t type_desc;
57340d553cfSPaul Beesley        auth_param_data_desc_t data;
57440d553cfSPaul Beesley    } auth_param_desc_t;
57540d553cfSPaul Beesley
57640d553cfSPaul BeesleyDescribing an image in a CoT
57740d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57840d553cfSPaul Beesley
57940d553cfSPaul BeesleyAn image in a CoT is a consolidation of the following aspects of a CoT described
58040d553cfSPaul Beesleyabove.
58140d553cfSPaul Beesley
58240d553cfSPaul Beesley#. A unique identifier specified by the platform which allows the IO framework
58340d553cfSPaul Beesley   to locate the image in a FIP and load it in the memory reserved for the data
58440d553cfSPaul Beesley   image in the CoT.
58540d553cfSPaul Beesley
58640d553cfSPaul Beesley#. A parsing method which is used by the AM to find the appropriate IPM.
58740d553cfSPaul Beesley
58840d553cfSPaul Beesley#. Authentication methods and their parameters as described in the previous
58940d553cfSPaul Beesley   section. These are used to verify the current image.
59040d553cfSPaul Beesley
59140d553cfSPaul Beesley#. Parameters which are used to verify the next image in the current CoT. These
59240d553cfSPaul Beesley   parameters are specified only by authentication images and can be extracted
59340d553cfSPaul Beesley   from the current image once it has been verified.
59440d553cfSPaul Beesley
59540d553cfSPaul BeesleyThe following data structure describes an image in a CoT.
59640d553cfSPaul Beesley
59740d553cfSPaul Beesley.. code:: c
59840d553cfSPaul Beesley
59940d553cfSPaul Beesley    typedef struct auth_img_desc_s {
60040d553cfSPaul Beesley        unsigned int img_id;
60140d553cfSPaul Beesley        const struct auth_img_desc_s *parent;
60240d553cfSPaul Beesley        img_type_t img_type;
60340d553cfSPaul Beesley        const auth_method_desc_t *const img_auth_methods;
60440d553cfSPaul Beesley        const auth_param_desc_t *const authenticated_data;
60540d553cfSPaul Beesley    } auth_img_desc_t;
60640d553cfSPaul Beesley
60740d553cfSPaul BeesleyA CoT is defined as an array of pointers to ``auth_image_desc_t`` structures
60840d553cfSPaul Beesleylinked together by the ``parent`` field. Those nodes with no parent must be
60940d553cfSPaul Beesleyauthenticated using the ROTPK stored in the platform.
61040d553cfSPaul Beesley
61140d553cfSPaul BeesleyImplementation example
61240d553cfSPaul Beesley----------------------
61340d553cfSPaul Beesley
61440d553cfSPaul BeesleyThis section is a detailed guide explaining a trusted boot implementation using
61540d553cfSPaul Beesleythe authentication framework. This example corresponds to the Applicative
61640d553cfSPaul BeesleyFunctional Mode (AFM) as specified in the TBBR-Client document. It is
61740d553cfSPaul Beesleyrecommended to read this guide along with the source code.
61840d553cfSPaul Beesley
61940d553cfSPaul BeesleyThe TBBR CoT
62040d553cfSPaul Beesley~~~~~~~~~~~~
62140d553cfSPaul Beesley
62240d553cfSPaul BeesleyThe CoT can be found in ``drivers/auth/tbbr/tbbr_cot.c``. This CoT consists of
62340d553cfSPaul Beesleyan array of pointers to image descriptors and it is registered in the framework
62440d553cfSPaul Beesleyusing the macro ``REGISTER_COT(cot_desc)``, where 'cot_desc' must be the name
62540d553cfSPaul Beesleyof the array (passing a pointer or any other type of indirection will cause the
62640d553cfSPaul Beesleyregistration process to fail).
62740d553cfSPaul Beesley
62840d553cfSPaul BeesleyThe number of images participating in the boot process depends on the CoT.
62940d553cfSPaul BeesleyThere is, however, a minimum set of images that are mandatory in TF-A and thus
63040d553cfSPaul Beesleyall CoTs must present:
63140d553cfSPaul Beesley
63240d553cfSPaul Beesley-  ``BL2``
63340d553cfSPaul Beesley-  ``SCP_BL2`` (platform specific)
63440d553cfSPaul Beesley-  ``BL31``
63540d553cfSPaul Beesley-  ``BL32`` (optional)
63640d553cfSPaul Beesley-  ``BL33``
63740d553cfSPaul Beesley
63840d553cfSPaul BeesleyThe TBBR specifies the additional certificates that must accompany these images
63940d553cfSPaul Beesleyfor a proper authentication. Details about the TBBR CoT may be found in the
64040d553cfSPaul Beesley`Trusted Board Boot`_ document.
64140d553cfSPaul Beesley
64240d553cfSPaul BeesleyFollowing the `Platform Porting Guide`_, a platform must provide unique
64340d553cfSPaul Beesleyidentifiers for all the images and certificates that will be loaded during the
64440d553cfSPaul Beesleyboot process. If a platform is using the TBBR as a reference for trusted boot,
64540d553cfSPaul Beesleythese identifiers can be obtained from ``include/common/tbbr/tbbr_img_def.h``.
64640d553cfSPaul BeesleyArm platforms include this file in ``include/plat/arm/common/arm_def.h``. Other
64740d553cfSPaul Beesleyplatforms may also include this file or provide their own identifiers.
64840d553cfSPaul Beesley
64940d553cfSPaul Beesley**Important**: the authentication module uses these identifiers to index the
65040d553cfSPaul BeesleyCoT array, so the descriptors location in the array must match the identifiers.
65140d553cfSPaul Beesley
65240d553cfSPaul BeesleyEach image descriptor must specify:
65340d553cfSPaul Beesley
65440d553cfSPaul Beesley-  ``img_id``: the corresponding image unique identifier defined by the platform.
65540d553cfSPaul Beesley-  ``img_type``: the image parser module uses the image type to call the proper
65640d553cfSPaul Beesley   parsing library to check the image integrity and extract the required
65740d553cfSPaul Beesley   authentication parameters. Three types of images are currently supported:
65840d553cfSPaul Beesley
65940d553cfSPaul Beesley   -  ``IMG_RAW``: image is a raw binary. No parsing functions are available,
66040d553cfSPaul Beesley      other than reading the whole image.
66140d553cfSPaul Beesley   -  ``IMG_PLAT``: image format is platform specific. The platform may use this
66240d553cfSPaul Beesley      type for custom images not directly supported by the authentication
66340d553cfSPaul Beesley      framework.
66440d553cfSPaul Beesley   -  ``IMG_CERT``: image is an x509v3 certificate.
66540d553cfSPaul Beesley
66640d553cfSPaul Beesley-  ``parent``: pointer to the parent image descriptor. The parent will contain
66740d553cfSPaul Beesley   the information required to authenticate the current image. If the parent
66840d553cfSPaul Beesley   is NULL, the authentication parameters will be obtained from the platform
66940d553cfSPaul Beesley   (i.e. the BL2 and Trusted Key certificates are signed with the ROT private
67040d553cfSPaul Beesley   key, whose public part is stored in the platform).
67140d553cfSPaul Beesley-  ``img_auth_methods``: this points to an array which defines the
67240d553cfSPaul Beesley   authentication methods that must be checked to consider an image
67340d553cfSPaul Beesley   authenticated. Each method consists of a type and a list of parameter
67440d553cfSPaul Beesley   descriptors. A parameter descriptor consists of a type and a cookie which
67540d553cfSPaul Beesley   will point to specific information required to extract that parameter from
67640d553cfSPaul Beesley   the image (i.e. if the parameter is stored in an x509v3 extension, the
67740d553cfSPaul Beesley   cookie will point to the extension OID). Depending on the method type, a
67840d553cfSPaul Beesley   different number of parameters must be specified. This pointer should not be
67940d553cfSPaul Beesley   NULL.
68040d553cfSPaul Beesley   Supported methods are:
68140d553cfSPaul Beesley
68240d553cfSPaul Beesley   -  ``AUTH_METHOD_HASH``: the hash of the image must match the hash extracted
68340d553cfSPaul Beesley      from the parent image. The following parameter descriptors must be
68440d553cfSPaul Beesley      specified:
68540d553cfSPaul Beesley
68640d553cfSPaul Beesley      -  ``data``: data to be hashed (obtained from current image)
68740d553cfSPaul Beesley      -  ``hash``: reference hash (obtained from parent image)
68840d553cfSPaul Beesley
68940d553cfSPaul Beesley   -  ``AUTH_METHOD_SIG``: the image (usually a certificate) must be signed with
69040d553cfSPaul Beesley      the private key whose public part is extracted from the parent image (or
69140d553cfSPaul Beesley      the platform if the parent is NULL). The following parameter descriptors
69240d553cfSPaul Beesley      must be specified:
69340d553cfSPaul Beesley
69440d553cfSPaul Beesley      -  ``pk``: the public key (obtained from parent image)
69540d553cfSPaul Beesley      -  ``sig``: the digital signature (obtained from current image)
69640d553cfSPaul Beesley      -  ``alg``: the signature algorithm used (obtained from current image)
69740d553cfSPaul Beesley      -  ``data``: the data to be signed (obtained from current image)
69840d553cfSPaul Beesley
69940d553cfSPaul Beesley-  ``authenticated_data``: this array pointer indicates what authentication
70040d553cfSPaul Beesley   parameters must be extracted from an image once it has been authenticated.
70140d553cfSPaul Beesley   Each parameter consists of a parameter descriptor and the buffer
70240d553cfSPaul Beesley   address/size to store the parameter. The CoT is responsible for allocating
70340d553cfSPaul Beesley   the required memory to store the parameters. This pointer may be NULL.
70440d553cfSPaul Beesley
70540d553cfSPaul BeesleyIn the ``tbbr_cot.c`` file, a set of buffers are allocated to store the parameters
70640d553cfSPaul Beesleyextracted from the certificates. In the case of the TBBR CoT, these parameters
707*f29213d9SJustin Chadwellare hashes and public keys. In DER format, an RSA-4096 public key requires 550
70840d553cfSPaul Beesleybytes, and a hash requires 51 bytes. Depending on the CoT and the authentication
70940d553cfSPaul Beesleyprocess, some of the buffers may be reused at different stages during the boot.
71040d553cfSPaul Beesley
71140d553cfSPaul BeesleyNext in that file, the parameter descriptors are defined. These descriptors will
71240d553cfSPaul Beesleybe used to extract the parameter data from the corresponding image.
71340d553cfSPaul Beesley
71440d553cfSPaul BeesleyExample: the BL31 Chain of Trust
71540d553cfSPaul Beesley^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71640d553cfSPaul Beesley
71740d553cfSPaul BeesleyFour image descriptors form the BL31 Chain of Trust:
71840d553cfSPaul Beesley
71940d553cfSPaul Beesley.. code:: c
72040d553cfSPaul Beesley
72140d553cfSPaul Beesley    static const auth_img_desc_t trusted_key_cert = {
72240d553cfSPaul Beesley            .img_id = TRUSTED_KEY_CERT_ID,
72340d553cfSPaul Beesley            .img_type = IMG_CERT,
72440d553cfSPaul Beesley            .parent = NULL,
72540d553cfSPaul Beesley            .img_auth_methods =  (const auth_method_desc_t[AUTH_METHOD_NUM]) {
72640d553cfSPaul Beesley                    [0] = {
72740d553cfSPaul Beesley                            .type = AUTH_METHOD_SIG,
72840d553cfSPaul Beesley                            .param.sig = {
72940d553cfSPaul Beesley                                    .pk = &subject_pk,
73040d553cfSPaul Beesley                                    .sig = &sig,
73140d553cfSPaul Beesley                                    .alg = &sig_alg,
73240d553cfSPaul Beesley                                    .data = &raw_data
73340d553cfSPaul Beesley                            }
73440d553cfSPaul Beesley                    },
73540d553cfSPaul Beesley                    [1] = {
73640d553cfSPaul Beesley                            .type = AUTH_METHOD_NV_CTR,
73740d553cfSPaul Beesley                            .param.nv_ctr = {
73840d553cfSPaul Beesley                                    .cert_nv_ctr = &trusted_nv_ctr,
73940d553cfSPaul Beesley                                    .plat_nv_ctr = &trusted_nv_ctr
74040d553cfSPaul Beesley                            }
74140d553cfSPaul Beesley                    }
74240d553cfSPaul Beesley            },
74340d553cfSPaul Beesley            .authenticated_data = (const auth_param_desc_t[COT_MAX_VERIFIED_PARAMS]) {
74440d553cfSPaul Beesley                    [0] = {
74540d553cfSPaul Beesley                            .type_desc = &trusted_world_pk,
74640d553cfSPaul Beesley                            .data = {
74740d553cfSPaul Beesley                                    .ptr = (void *)trusted_world_pk_buf,
74840d553cfSPaul Beesley                                    .len = (unsigned int)PK_DER_LEN
74940d553cfSPaul Beesley                            }
75040d553cfSPaul Beesley                    },
75140d553cfSPaul Beesley                    [1] = {
75240d553cfSPaul Beesley                            .type_desc = &non_trusted_world_pk,
75340d553cfSPaul Beesley                            .data = {
75440d553cfSPaul Beesley                                    .ptr = (void *)non_trusted_world_pk_buf,
75540d553cfSPaul Beesley                                    .len = (unsigned int)PK_DER_LEN
75640d553cfSPaul Beesley                            }
75740d553cfSPaul Beesley                    }
75840d553cfSPaul Beesley            }
75940d553cfSPaul Beesley    };
76040d553cfSPaul Beesley    static const auth_img_desc_t soc_fw_key_cert = {
76140d553cfSPaul Beesley            .img_id = SOC_FW_KEY_CERT_ID,
76240d553cfSPaul Beesley            .img_type = IMG_CERT,
76340d553cfSPaul Beesley            .parent = &trusted_key_cert,
76440d553cfSPaul Beesley            .img_auth_methods =  (const auth_method_desc_t[AUTH_METHOD_NUM]) {
76540d553cfSPaul Beesley                    [0] = {
76640d553cfSPaul Beesley                            .type = AUTH_METHOD_SIG,
76740d553cfSPaul Beesley                            .param.sig = {
76840d553cfSPaul Beesley                                    .pk = &trusted_world_pk,
76940d553cfSPaul Beesley                                    .sig = &sig,
77040d553cfSPaul Beesley                                    .alg = &sig_alg,
77140d553cfSPaul Beesley                                    .data = &raw_data
77240d553cfSPaul Beesley                            }
77340d553cfSPaul Beesley                    },
77440d553cfSPaul Beesley                    [1] = {
77540d553cfSPaul Beesley                            .type = AUTH_METHOD_NV_CTR,
77640d553cfSPaul Beesley                            .param.nv_ctr = {
77740d553cfSPaul Beesley                                    .cert_nv_ctr = &trusted_nv_ctr,
77840d553cfSPaul Beesley                                    .plat_nv_ctr = &trusted_nv_ctr
77940d553cfSPaul Beesley                            }
78040d553cfSPaul Beesley                    }
78140d553cfSPaul Beesley            },
78240d553cfSPaul Beesley            .authenticated_data = (const auth_param_desc_t[COT_MAX_VERIFIED_PARAMS]) {
78340d553cfSPaul Beesley                    [0] = {
78440d553cfSPaul Beesley                            .type_desc = &soc_fw_content_pk,
78540d553cfSPaul Beesley                            .data = {
78640d553cfSPaul Beesley                                    .ptr = (void *)content_pk_buf,
78740d553cfSPaul Beesley                                    .len = (unsigned int)PK_DER_LEN
78840d553cfSPaul Beesley                            }
78940d553cfSPaul Beesley                    }
79040d553cfSPaul Beesley            }
79140d553cfSPaul Beesley    };
79240d553cfSPaul Beesley    static const auth_img_desc_t soc_fw_content_cert = {
79340d553cfSPaul Beesley            .img_id = SOC_FW_CONTENT_CERT_ID,
79440d553cfSPaul Beesley            .img_type = IMG_CERT,
79540d553cfSPaul Beesley            .parent = &soc_fw_key_cert,
79640d553cfSPaul Beesley            .img_auth_methods =  (const auth_method_desc_t[AUTH_METHOD_NUM]) {
79740d553cfSPaul Beesley                    [0] = {
79840d553cfSPaul Beesley                            .type = AUTH_METHOD_SIG,
79940d553cfSPaul Beesley                            .param.sig = {
80040d553cfSPaul Beesley                                    .pk = &soc_fw_content_pk,
80140d553cfSPaul Beesley                                    .sig = &sig,
80240d553cfSPaul Beesley                                    .alg = &sig_alg,
80340d553cfSPaul Beesley                                    .data = &raw_data
80440d553cfSPaul Beesley                            }
80540d553cfSPaul Beesley                    },
80640d553cfSPaul Beesley                    [1] = {
80740d553cfSPaul Beesley                            .type = AUTH_METHOD_NV_CTR,
80840d553cfSPaul Beesley                            .param.nv_ctr = {
80940d553cfSPaul Beesley                                    .cert_nv_ctr = &trusted_nv_ctr,
81040d553cfSPaul Beesley                                    .plat_nv_ctr = &trusted_nv_ctr
81140d553cfSPaul Beesley                            }
81240d553cfSPaul Beesley                    }
81340d553cfSPaul Beesley            },
81440d553cfSPaul Beesley            .authenticated_data = (const auth_param_desc_t[COT_MAX_VERIFIED_PARAMS]) {
81540d553cfSPaul Beesley                    [0] = {
81640d553cfSPaul Beesley                            .type_desc = &soc_fw_hash,
81740d553cfSPaul Beesley                            .data = {
81840d553cfSPaul Beesley                                    .ptr = (void *)soc_fw_hash_buf,
81940d553cfSPaul Beesley                                    .len = (unsigned int)HASH_DER_LEN
82040d553cfSPaul Beesley                            }
82140d553cfSPaul Beesley                    },
82240d553cfSPaul Beesley                    [1] = {
82340d553cfSPaul Beesley                            .type_desc = &soc_fw_config_hash,
82440d553cfSPaul Beesley                            .data = {
82540d553cfSPaul Beesley                                    .ptr = (void *)soc_fw_config_hash_buf,
82640d553cfSPaul Beesley                                    .len = (unsigned int)HASH_DER_LEN
82740d553cfSPaul Beesley                            }
82840d553cfSPaul Beesley                    }
82940d553cfSPaul Beesley            }
83040d553cfSPaul Beesley    };
83140d553cfSPaul Beesley    static const auth_img_desc_t bl31_image = {
83240d553cfSPaul Beesley            .img_id = BL31_IMAGE_ID,
83340d553cfSPaul Beesley            .img_type = IMG_RAW,
83440d553cfSPaul Beesley            .parent = &soc_fw_content_cert,
83540d553cfSPaul Beesley            .img_auth_methods =  (const auth_method_desc_t[AUTH_METHOD_NUM]) {
83640d553cfSPaul Beesley                    [0] = {
83740d553cfSPaul Beesley                            .type = AUTH_METHOD_HASH,
83840d553cfSPaul Beesley                            .param.hash = {
83940d553cfSPaul Beesley                                    .data = &raw_data,
84040d553cfSPaul Beesley                                    .hash = &soc_fw_hash
84140d553cfSPaul Beesley                            }
84240d553cfSPaul Beesley                    }
84340d553cfSPaul Beesley            }
84440d553cfSPaul Beesley    };
84540d553cfSPaul Beesley
84640d553cfSPaul BeesleyThe **Trusted Key certificate** is signed with the ROT private key and contains
84740d553cfSPaul Beesleythe Trusted World public key and the Non-Trusted World public key as x509v3
84840d553cfSPaul Beesleyextensions. This must be specified in the image descriptor using the
84940d553cfSPaul Beesley``img_auth_methods`` and ``authenticated_data`` arrays, respectively.
85040d553cfSPaul Beesley
85140d553cfSPaul BeesleyThe Trusted Key certificate is authenticated by checking its digital signature
85240d553cfSPaul Beesleyusing the ROTPK. Four parameters are required to check a signature: the public
85340d553cfSPaul Beesleykey, the algorithm, the signature and the data that has been signed. Therefore,
85440d553cfSPaul Beesleyfour parameter descriptors must be specified with the authentication method:
85540d553cfSPaul Beesley
85640d553cfSPaul Beesley-  ``subject_pk``: parameter descriptor of type ``AUTH_PARAM_PUB_KEY``. This type
85740d553cfSPaul Beesley   is used to extract a public key from the parent image. If the cookie is an
85840d553cfSPaul Beesley   OID, the key is extracted from the corresponding x509v3 extension. If the
85940d553cfSPaul Beesley   cookie is NULL, the subject public key is retrieved. In this case, because
86040d553cfSPaul Beesley   the parent image is NULL, the public key is obtained from the platform
86140d553cfSPaul Beesley   (this key will be the ROTPK).
86240d553cfSPaul Beesley-  ``sig``: parameter descriptor of type ``AUTH_PARAM_SIG``. It is used to extract
86340d553cfSPaul Beesley   the signature from the certificate.
86440d553cfSPaul Beesley-  ``sig_alg``: parameter descriptor of type ``AUTH_PARAM_SIG``. It is used to
86540d553cfSPaul Beesley   extract the signature algorithm from the certificate.
86640d553cfSPaul Beesley-  ``raw_data``: parameter descriptor of type ``AUTH_PARAM_RAW_DATA``. It is used
86740d553cfSPaul Beesley   to extract the data to be signed from the certificate.
86840d553cfSPaul Beesley
86940d553cfSPaul BeesleyOnce the signature has been checked and the certificate authenticated, the
87040d553cfSPaul BeesleyTrusted World public key needs to be extracted from the certificate. A new entry
87140d553cfSPaul Beesleyis created in the ``authenticated_data`` array for that purpose. In that entry,
87240d553cfSPaul Beesleythe corresponding parameter descriptor must be specified along with the buffer
87340d553cfSPaul Beesleyaddress to store the parameter value. In this case, the ``tz_world_pk`` descriptor
87440d553cfSPaul Beesleyis used to extract the public key from an x509v3 extension with OID
87540d553cfSPaul Beesley``TRUSTED_WORLD_PK_OID``. The BL31 key certificate will use this descriptor as
87640d553cfSPaul Beesleyparameter in the signature authentication method. The key is stored in the
87740d553cfSPaul Beesley``plat_tz_world_pk_buf`` buffer.
87840d553cfSPaul Beesley
87940d553cfSPaul BeesleyThe **BL31 Key certificate** is authenticated by checking its digital signature
88040d553cfSPaul Beesleyusing the Trusted World public key obtained previously from the Trusted Key
88140d553cfSPaul Beesleycertificate. In the image descriptor, we specify a single authentication method
88240d553cfSPaul Beesleyby signature whose public key is the ``tz_world_pk``. Once this certificate has
88340d553cfSPaul Beesleybeen authenticated, we have to extract the BL31 public key, stored in the
88440d553cfSPaul Beesleyextension specified by ``bl31_content_pk``. This key will be copied to the
88540d553cfSPaul Beesley``plat_content_pk`` buffer.
88640d553cfSPaul Beesley
88740d553cfSPaul BeesleyThe **BL31 certificate** is authenticated by checking its digital signature
88840d553cfSPaul Beesleyusing the BL31 public key obtained previously from the BL31 Key certificate.
88940d553cfSPaul BeesleyWe specify the authentication method using ``bl31_content_pk`` as public key.
89040d553cfSPaul BeesleyAfter authentication, we need to extract the BL31 hash, stored in the extension
89140d553cfSPaul Beesleyspecified by ``bl31_hash``. This hash will be copied to the ``plat_bl31_hash_buf``
89240d553cfSPaul Beesleybuffer.
89340d553cfSPaul Beesley
89440d553cfSPaul BeesleyThe **BL31 image** is authenticated by calculating its hash and matching it
89540d553cfSPaul Beesleywith the hash obtained from the BL31 certificate. The image descriptor contains
89640d553cfSPaul Beesleya single authentication method by hash. The parameters to the hash method are
89740d553cfSPaul Beesleythe reference hash, ``bl31_hash``, and the data to be hashed. In this case, it is
89840d553cfSPaul Beesleythe whole image, so we specify ``raw_data``.
89940d553cfSPaul Beesley
90040d553cfSPaul BeesleyThe image parser library
90140d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~
90240d553cfSPaul Beesley
90340d553cfSPaul BeesleyThe image parser module relies on libraries to check the image integrity and
90440d553cfSPaul Beesleyextract the authentication parameters. The number and type of parser libraries
90540d553cfSPaul Beesleydepend on the images used in the CoT. Raw images do not need a library, so
90640d553cfSPaul Beesleyonly an x509v3 library is required for the TBBR CoT.
90740d553cfSPaul Beesley
90840d553cfSPaul BeesleyArm platforms will use an x509v3 library based on mbed TLS. This library may be
90940d553cfSPaul Beesleyfound in ``drivers/auth/mbedtls/mbedtls_x509_parser.c``. It exports three
91040d553cfSPaul Beesleyfunctions:
91140d553cfSPaul Beesley
91240d553cfSPaul Beesley.. code:: c
91340d553cfSPaul Beesley
91440d553cfSPaul Beesley    void init(void);
91540d553cfSPaul Beesley    int check_integrity(void *img, unsigned int img_len);
91640d553cfSPaul Beesley    int get_auth_param(const auth_param_type_desc_t *type_desc,
91740d553cfSPaul Beesley                       void *img, unsigned int img_len,
91840d553cfSPaul Beesley                       void **param, unsigned int *param_len);
91940d553cfSPaul Beesley
92040d553cfSPaul BeesleyThe library is registered in the framework using the macro
92140d553cfSPaul Beesley``REGISTER_IMG_PARSER_LIB()``. Each time the image parser module needs to access
92240d553cfSPaul Beesleyan image of type ``IMG_CERT``, it will call the corresponding function exported
92340d553cfSPaul Beesleyin this file.
92440d553cfSPaul Beesley
92540d553cfSPaul BeesleyThe build system must be updated to include the corresponding library and
92640d553cfSPaul Beesleymbed TLS sources. Arm platforms use the ``arm_common.mk`` file to pull the
92740d553cfSPaul Beesleysources.
92840d553cfSPaul Beesley
92940d553cfSPaul BeesleyThe cryptographic library
93040d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~
93140d553cfSPaul Beesley
93240d553cfSPaul BeesleyThe cryptographic module relies on a library to perform the required operations,
93340d553cfSPaul Beesleyi.e. verify a hash or a digital signature. Arm platforms will use a library
93440d553cfSPaul Beesleybased on mbed TLS, which can be found in
93540d553cfSPaul Beesley``drivers/auth/mbedtls/mbedtls_crypto.c``. This library is registered in the
93640d553cfSPaul Beesleyauthentication framework using the macro ``REGISTER_CRYPTO_LIB()`` and exports
93740d553cfSPaul Beesleythree functions:
93840d553cfSPaul Beesley
93940d553cfSPaul Beesley.. code:: c
94040d553cfSPaul Beesley
94140d553cfSPaul Beesley    void init(void);
94240d553cfSPaul Beesley    int verify_signature(void *data_ptr, unsigned int data_len,
94340d553cfSPaul Beesley                         void *sig_ptr, unsigned int sig_len,
94440d553cfSPaul Beesley                         void *sig_alg, unsigned int sig_alg_len,
94540d553cfSPaul Beesley                         void *pk_ptr, unsigned int pk_len);
94640d553cfSPaul Beesley    int verify_hash(void *data_ptr, unsigned int data_len,
94740d553cfSPaul Beesley                    void *digest_info_ptr, unsigned int digest_info_len);
94840d553cfSPaul Beesley
949*f29213d9SJustin ChadwellThe mbedTLS library algorithm support is configured by both the
950*f29213d9SJustin Chadwell``TF_MBEDTLS_KEY_ALG`` and ``TF_MBEDTLS_KEY_SIZE`` variables.
951*f29213d9SJustin Chadwell
952*f29213d9SJustin Chadwell-  ``TF_MBEDTLS_KEY_ALG`` can take in 3 values: `rsa`, `ecdsa` or `rsa+ecdsa`.
953*f29213d9SJustin Chadwell   This variable allows the Makefile to include the corresponding sources in
954*f29213d9SJustin Chadwell   the build for the various algorithms. Setting the variable to `rsa+ecdsa`
955*f29213d9SJustin Chadwell   enables support for both rsa and ecdsa algorithms in the mbedTLS library.
956*f29213d9SJustin Chadwell
957*f29213d9SJustin Chadwell-  ``TF_MBEDTLS_KEY_SIZE`` sets the supported RSA key size for TFA. Valid values
958*f29213d9SJustin Chadwell   include 1024, 2048, 3072 and 4096.
95940d553cfSPaul Beesley
960e1c5026aSPaul Beesley.. note::
961e1c5026aSPaul Beesley   If code size is a concern, the build option ``MBEDTLS_SHA256_SMALLER`` can
962e1c5026aSPaul Beesley   be defined in the platform Makefile. It will make mbed TLS use an
963e1c5026aSPaul Beesley   implementation of SHA-256 with smaller memory footprint (~1.5 KB less) but
964e1c5026aSPaul Beesley   slower (~30%).
96540d553cfSPaul Beesley
96640d553cfSPaul Beesley--------------
96740d553cfSPaul Beesley
96840d553cfSPaul Beesley*Copyright (c) 2017-2019, Arm Limited and Contributors. All rights reserved.*
96940d553cfSPaul Beesley
97040d553cfSPaul Beesley.. _Trusted Board Boot: ./trusted-board-boot.rst
97140d553cfSPaul Beesley.. _Platform Porting Guide: ../getting_started/porting-guide.rst
97240d553cfSPaul Beesley.. _TBBR-Client specification: https://developer.arm.com/docs/den0006/latest/trusted-board-boot-requirements-client-tbbr-client-armv8-a
973