auth-framework.rst 42 KB

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