auth-framework.rst 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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)(
  185. /* Data to verify. */
  186. void *data_ptr, unsigned int data_len,
  187. /* Bit string of the signature in DER format. */
  188. void *sig_ptr, unsigned int sig_len,
  189. /* ASN1 SignatureAlgorithm struct. */
  190. void *sig_alg, unsigned int sig_alg_len,
  191. /* ASN1 SubjectPublicKeyInfo struct. */
  192. void *pk_ptr, unsigned int pk_len);
  193. int (*calc_hash)(
  194. /* SHA256, SHA384 and SHA512 can be used. */
  195. enum crypto_md_algo alg
  196. /* Data to hash. */
  197. void *data_ptr, unsigned int data_len,
  198. /* Buffer to store the output. */
  199. unsigned char output[CRYPTO_MD_MAX_SIZE]);
  200. int (*verify_hash)(
  201. /* Data to verify. */
  202. void *data_ptr, unsigned int data_len,
  203. /* ASN1 DigestInfo struct. */
  204. void *digest_info_ptr, unsigned int digest_info_len);
  205. int (*auth_decrypt)(
  206. /* Currently AES-GCM is the only supported alg. */
  207. enum crypto_dec_algo dec_algo,
  208. /* Data to decrypt. */
  209. void *data_ptr, size_t len,
  210. /* Decryption key. */
  211. const void *key, unsigned int key_len,
  212. unsigned int key_flags,
  213. /* Initialization vector. */
  214. const void *iv, unsigned int iv_len,
  215. /* Authentication tag. */
  216. const void *tag, unsigned int tag_len);
  217. The above functions return values from the enum ``crypto_ret_value``.
  218. The functions are registered in the CM using the macro:
  219. .. code:: c
  220. REGISTER_CRYPTO_LIB(_name,
  221. _init,
  222. _verify_signature,
  223. _verify_hash,
  224. _calc_hash,
  225. _auth_decrypt,
  226. _convert_pk);
  227. ``_name`` must be a string containing the name of the CL. This name is used for
  228. debugging purposes.
  229. The ``_init`` function is used to perform any initialization required for
  230. the specific CM and CL.
  231. The ``_verify_signature`` function is used to verify certificates,
  232. and ``_verify_hash`` is used to verify raw images.
  233. The ``_calc_hash`` function is mainly used in the ``MEASURED_BOOT``
  234. and ``DRTM_SUPPORT`` features to calculate the hashes of various images/data.
  235. The ``_auth_decrypt`` function uses an authentication tag to perform
  236. authenticated decryption, providing guarantees on the authenticity
  237. of encrypted data. This function is used when the optional encrypted
  238. firmware feature is enabled, that is when ``ENCRYPT_BL31`` or
  239. ``ENCRYPT_BL32`` are set to ``1`` and ``DECRYPTION_SUPPORT`` is
  240. set to ``aes_gcm``.
  241. Optionally, a platform function can be provided to convert public key
  242. (_convert_pk). It is only used if the platform saves a hash of the ROTPK.
  243. Most platforms save the hash of the ROTPK, but some may save slightly different
  244. information - e.g the hash of the ROTPK plus some related information.
  245. Defining this function allows to transform the ROTPK used to verify
  246. the signature to the buffer (a platform specific public key) which
  247. hash is saved in OTP.
  248. .. code:: c
  249. int (*convert_pk)(void *full_pk_ptr, unsigned int full_pk_len,
  250. void **hashed_pk_ptr, unsigned int *hashed_pk_len);
  251. - ``full_pk_ptr``: Pointer to Distinguished Encoding Rules (DER) ROTPK.
  252. - ``full_pk_len``: DER ROTPK size.
  253. - ``hashed_pk_ptr``: to return a pointer to a buffer, which hash should be the one saved in OTP.
  254. - ``hashed_pk_len``: previous buffer size
  255. Image Parser Module (IPM)
  256. ^^^^^^^^^^^^^^^^^^^^^^^^^
  257. The IPM is responsible for:
  258. #. Checking the integrity of each image loaded by the IO framework.
  259. #. Extracting parameters used for authenticating an image based upon a
  260. description provided by the platform in the CoT descriptor.
  261. Images may have different formats (for example, authentication images could be
  262. x509v3 certificates, signed ELF files or any other platform specific format).
  263. The IPM allows to register an Image Parser Library (IPL) for every image format
  264. used in the CoT. This library must implement the specific methods to parse the
  265. image. The IPM obtains the image format from the CoT and calls the right IPL to
  266. check the image integrity and extract the authentication parameters.
  267. See Section "Describing the image parsing methods" for more details about the
  268. mechanism the IPM provides to define and register IPLs.
  269. Authentication methods
  270. ~~~~~~~~~~~~~~~~~~~~~~
  271. The AM supports the following authentication methods:
  272. #. Hash
  273. #. Digital signature
  274. The platform may specify these methods in the CoT in case it decides to define
  275. a custom CoT instead of reusing a predefined one.
  276. If a data image uses multiple methods, then all the methods must be a part of
  277. the same CoT. The number and type of parameters are method specific. These
  278. parameters should be obtained from the parent image using the IPM.
  279. #. Hash
  280. Parameters:
  281. #. A pointer to data to hash
  282. #. Length of the data
  283. #. A pointer to the hash
  284. #. Length of the hash
  285. The hash will be represented by the DER encoding of the following ASN.1
  286. type:
  287. ::
  288. DigestInfo ::= SEQUENCE {
  289. digestAlgorithm DigestAlgorithmIdentifier,
  290. digest Digest
  291. }
  292. This ASN.1 structure makes it possible to remove any assumption about the
  293. type of hash algorithm used as this information accompanies the hash. This
  294. should allow the Cryptography Library (CL) to support multiple hash
  295. algorithm implementations.
  296. #. Digital Signature
  297. Parameters:
  298. #. A pointer to data to sign
  299. #. Length of the data
  300. #. Public Key Algorithm
  301. #. Public Key value
  302. #. Digital Signature Algorithm
  303. #. Digital Signature value
  304. The Public Key parameters will be represented by the DER encoding of the
  305. following ASN.1 type:
  306. ::
  307. SubjectPublicKeyInfo ::= SEQUENCE {
  308. algorithm AlgorithmIdentifier{PUBLIC-KEY,{PublicKeyAlgorithms}},
  309. subjectPublicKey BIT STRING }
  310. The Digital Signature Algorithm will be represented by the DER encoding of
  311. the following ASN.1 types.
  312. ::
  313. AlgorithmIdentifier {ALGORITHM:IOSet } ::= SEQUENCE {
  314. algorithm ALGORITHM.&id({IOSet}),
  315. parameters ALGORITHM.&Type({IOSet}{@algorithm}) OPTIONAL
  316. }
  317. The digital signature will be represented by:
  318. ::
  319. signature ::= BIT STRING
  320. The authentication framework will use the image descriptor to extract all the
  321. information related to authentication.
  322. Specifying a Chain of Trust
  323. ---------------------------
  324. A CoT can be described as a set of image descriptors linked together in a
  325. particular order. The order dictates the sequence in which they must be
  326. verified. Each image has a set of properties which allow the AM to verify it.
  327. These properties are described below.
  328. The PP is responsible for defining a single or multiple CoTs for a data image.
  329. Unless otherwise specified, the data structures described in the following
  330. sections are populated by the PP statically.
  331. Describing the image parsing methods
  332. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  333. The parsing method refers to the format of a particular image. For example, an
  334. authentication image that represents a certificate could be in the X.509v3
  335. format. A data image that represents a boot loader stage could be in raw binary
  336. or ELF format. The IPM supports three parsing methods. An image has to use one
  337. of the three methods described below. An IPL is responsible for interpreting a
  338. single parsing method. There has to be one IPL for every method used by the
  339. platform.
  340. #. Raw format: This format is effectively a nop as an image using this method
  341. is treated as being in raw binary format e.g. boot loader images used by
  342. TF-A. This method should only be used by data images.
  343. #. X509V3 method: This method uses industry standards like X.509 to represent
  344. PKI certificates (authentication images). It is expected that open source
  345. libraries will be available which can be used to parse an image represented
  346. by this method. Such libraries can be used to write the corresponding IPL
  347. e.g. the X.509 parsing library code in mbed TLS.
  348. #. Platform defined method: This method caters for platform specific
  349. proprietary standards to represent authentication or data images. For
  350. example, The signature of a data image could be appended to the data image
  351. raw binary. A header could be prepended to the combined blob to specify the
  352. extents of each component. The platform will have to implement the
  353. corresponding IPL to interpret such a format.
  354. The following enum can be used to define these three methods.
  355. .. code:: c
  356. typedef enum img_type_enum {
  357. IMG_RAW, /* Binary image */
  358. IMG_PLAT, /* Platform specific format */
  359. IMG_CERT, /* X509v3 certificate */
  360. IMG_MAX_TYPES,
  361. } img_type_t;
  362. An IPL must provide functions with the following prototypes:
  363. .. code:: c
  364. void init(void);
  365. int check_integrity(void *img, unsigned int img_len);
  366. int get_auth_param(const auth_param_type_desc_t *type_desc,
  367. void *img, unsigned int img_len,
  368. void **param, unsigned int *param_len);
  369. An IPL for each type must be registered using the following macro:
  370. .. code:: c
  371. REGISTER_IMG_PARSER_LIB(_type, _name, _init, _check_int, _get_param)
  372. - ``_type``: one of the types described above.
  373. - ``_name``: a string containing the IPL name for debugging purposes.
  374. - ``_init``: initialization function pointer.
  375. - ``_check_int``: check image integrity function pointer.
  376. - ``_get_param``: extract authentication parameter function pointer.
  377. The ``init()`` function will be used to initialize the IPL.
  378. The ``check_integrity()`` function is passed a pointer to the memory where the
  379. image has been loaded by the IO framework and the image length. It should ensure
  380. that the image is in the format corresponding to the parsing method and has not
  381. been tampered with. For example, RFC-2459 describes a validation sequence for an
  382. X.509 certificate.
  383. The ``get_auth_param()`` function is passed a parameter descriptor containing
  384. information about the parameter (``type_desc`` and ``cookie``) to identify and
  385. extract the data corresponding to that parameter from an image. This data will
  386. be used to verify either the current or the next image in the CoT sequence.
  387. Each image in the CoT will specify the parsing method it uses. This information
  388. will be used by the IPM to find the right parser descriptor for the image.
  389. Describing the authentication method(s)
  390. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  391. As part of the CoT, each image has to specify one or more authentication methods
  392. which will be used to verify it. As described in the Section "Authentication
  393. methods", there are three methods supported by the AM.
  394. .. code:: c
  395. typedef enum {
  396. AUTH_METHOD_NONE,
  397. AUTH_METHOD_HASH,
  398. AUTH_METHOD_SIG,
  399. AUTH_METHOD_NUM
  400. } auth_method_type_t;
  401. The AM defines the type of each parameter used by an authentication method. It
  402. uses this information to:
  403. #. Specify to the ``get_auth_param()`` function exported by the IPM, which
  404. parameter should be extracted from an image.
  405. #. Correctly marshall the parameters while calling the verification function
  406. exported by the CM and PP.
  407. #. Extract authentication parameters from a parent image in order to verify a
  408. child image e.g. to verify the certificate image, the public key has to be
  409. obtained from the parent image.
  410. .. code:: c
  411. typedef enum {
  412. AUTH_PARAM_NONE,
  413. AUTH_PARAM_RAW_DATA, /* Raw image data */
  414. AUTH_PARAM_SIG, /* The image signature */
  415. AUTH_PARAM_SIG_ALG, /* The image signature algorithm */
  416. AUTH_PARAM_HASH, /* A hash (including the algorithm) */
  417. AUTH_PARAM_PUB_KEY, /* A public key */
  418. AUTH_PARAM_NV_CTR, /* A non-volatile counter */
  419. } auth_param_type_t;
  420. The AM defines the following structure to identify an authentication parameter
  421. required to verify an image.
  422. .. code:: c
  423. typedef struct auth_param_type_desc_s {
  424. auth_param_type_t type;
  425. void *cookie;
  426. } auth_param_type_desc_t;
  427. ``cookie`` is used by the platform to specify additional information to the IPM
  428. which enables it to uniquely identify the parameter that should be extracted
  429. from an image. For example, the hash of a BL3x image in its corresponding
  430. content certificate is stored in an X509v3 custom extension field. An extension
  431. field can only be identified using an OID. In this case, the ``cookie`` could
  432. contain the pointer to the OID defined by the platform for the hash extension
  433. field while the ``type`` field could be set to ``AUTH_PARAM_HASH``. A value of 0 for
  434. the ``cookie`` field means that it is not used.
  435. For each method, the AM defines a structure with the parameters required to
  436. verify the image.
  437. .. code:: c
  438. /*
  439. * Parameters for authentication by hash matching
  440. */
  441. typedef struct auth_method_param_hash_s {
  442. auth_param_type_desc_t *data; /* Data to hash */
  443. auth_param_type_desc_t *hash; /* Hash to match with */
  444. } auth_method_param_hash_t;
  445. /*
  446. * Parameters for authentication by signature
  447. */
  448. typedef struct auth_method_param_sig_s {
  449. auth_param_type_desc_t *pk; /* Public key */
  450. auth_param_type_desc_t *sig; /* Signature to check */
  451. auth_param_type_desc_t *alg; /* Signature algorithm */
  452. auth_param_type_desc_t *tbs; /* Data signed */
  453. } auth_method_param_sig_t;
  454. The AM defines the following structure to describe an authentication method for
  455. verifying an image
  456. .. code:: c
  457. /*
  458. * Authentication method descriptor
  459. */
  460. typedef struct auth_method_desc_s {
  461. auth_method_type_t type;
  462. union {
  463. auth_method_param_hash_t hash;
  464. auth_method_param_sig_t sig;
  465. } param;
  466. } auth_method_desc_t;
  467. Using the method type specified in the ``type`` field, the AM finds out what field
  468. needs to access within the ``param`` union.
  469. Storing Authentication parameters
  470. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  471. A parameter described by ``auth_param_type_desc_t`` to verify an image could be
  472. obtained from either the image itself or its parent image. The memory allocated
  473. for loading the parent image will be reused for loading the child image. Hence
  474. parameters which are obtained from the parent for verifying a child image need
  475. to have memory allocated for them separately where they can be stored. This
  476. memory must be statically allocated by the platform port.
  477. The AM defines the following structure to store the data corresponding to an
  478. authentication parameter.
  479. .. code:: c
  480. typedef struct auth_param_data_desc_s {
  481. void *auth_param_ptr;
  482. unsigned int auth_param_len;
  483. } auth_param_data_desc_t;
  484. The ``auth_param_ptr`` field is initialized by the platform. The ``auth_param_len``
  485. field is used to specify the length of the data in the memory.
  486. For parameters that can be obtained from the child image itself, the IPM is
  487. responsible for populating the ``auth_param_ptr`` and ``auth_param_len`` fields
  488. while executing the ``img_get_auth_param()`` function.
  489. The AM defines the following structure to enable an image to describe the
  490. parameters that should be extracted from it and used to verify the next image
  491. (child) in a CoT.
  492. .. code:: c
  493. typedef struct auth_param_desc_s {
  494. auth_param_type_desc_t type_desc;
  495. auth_param_data_desc_t data;
  496. } auth_param_desc_t;
  497. Describing an image in a CoT
  498. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  499. An image in a CoT is a consolidation of the following aspects of a CoT described
  500. above.
  501. #. A unique identifier specified by the platform which allows the IO framework
  502. to locate the image in a FIP and load it in the memory reserved for the data
  503. image in the CoT.
  504. #. A parsing method which is used by the AM to find the appropriate IPM.
  505. #. Authentication methods and their parameters as described in the previous
  506. section. These are used to verify the current image.
  507. #. Parameters which are used to verify the next image in the current CoT. These
  508. parameters are specified only by authentication images and can be extracted
  509. from the current image once it has been verified.
  510. The following data structure describes an image in a CoT.
  511. .. code:: c
  512. typedef struct auth_img_desc_s {
  513. unsigned int img_id;
  514. const struct auth_img_desc_s *parent;
  515. img_type_t img_type;
  516. const auth_method_desc_t *const img_auth_methods;
  517. const auth_param_desc_t *const authenticated_data;
  518. } auth_img_desc_t;
  519. A CoT is defined as an array of pointers to ``auth_image_desc_t`` structures
  520. linked together by the ``parent`` field. Those nodes with no parent must be
  521. authenticated using the ROTPK stored in the platform.
  522. Implementation example
  523. ----------------------
  524. This section is a detailed guide explaining a trusted boot implementation using
  525. the authentication framework. This example corresponds to the Applicative
  526. Functional Mode (AFM) as specified in the TBBR-Client document. It is
  527. recommended to read this guide along with the source code.
  528. The TBBR CoT
  529. ~~~~~~~~~~~~
  530. CoT specific to BL1 and BL2 can be found in ``drivers/auth/tbbr/tbbr_cot_bl1.c``
  531. and ``drivers/auth/tbbr/tbbr_cot_bl2.c`` respectively. The common CoT used across
  532. BL1 and BL2 can be found in ``drivers/auth/tbbr/tbbr_cot_common.c``.
  533. This CoT consists of an array of pointers to image descriptors and it is
  534. registered in the framework using the macro ``REGISTER_COT(cot_desc)``, where
  535. ``cot_desc`` must be the name of the array (passing a pointer or any other
  536. type of indirection will cause the registration process to fail).
  537. The number of images participating in the boot process depends on the CoT.
  538. There is, however, a minimum set of images that are mandatory in TF-A and thus
  539. all CoTs must present:
  540. - ``BL2``
  541. - ``SCP_BL2`` (platform specific)
  542. - ``BL31``
  543. - ``BL32`` (optional)
  544. - ``BL33``
  545. The TBBR specifies the additional certificates that must accompany these images
  546. for a proper authentication. Details about the TBBR CoT may be found in the
  547. :ref:`Trusted Board Boot` document.
  548. Following the :ref:`Porting Guide`, a platform must provide unique
  549. identifiers for all the images and certificates that will be loaded during the
  550. boot process. If a platform is using the TBBR as a reference for trusted boot,
  551. these identifiers can be obtained from ``include/common/tbbr/tbbr_img_def.h``.
  552. Arm platforms include this file in ``include/plat/arm/common/arm_def.h``. Other
  553. platforms may also include this file or provide their own identifiers.
  554. **Important**: the authentication module uses these identifiers to index the
  555. CoT array, so the descriptors location in the array must match the identifiers.
  556. Each image descriptor must specify:
  557. - ``img_id``: the corresponding image unique identifier defined by the platform.
  558. - ``img_type``: the image parser module uses the image type to call the proper
  559. parsing library to check the image integrity and extract the required
  560. authentication parameters. Three types of images are currently supported:
  561. - ``IMG_RAW``: image is a raw binary. No parsing functions are available,
  562. other than reading the whole image.
  563. - ``IMG_PLAT``: image format is platform specific. The platform may use this
  564. type for custom images not directly supported by the authentication
  565. framework.
  566. - ``IMG_CERT``: image is an x509v3 certificate.
  567. - ``parent``: pointer to the parent image descriptor. The parent will contain
  568. the information required to authenticate the current image. If the parent
  569. is NULL, the authentication parameters will be obtained from the platform
  570. (i.e. the BL2 and Trusted Key certificates are signed with the ROT private
  571. key, whose public part is stored in the platform).
  572. - ``img_auth_methods``: this points to an array which defines the
  573. authentication methods that must be checked to consider an image
  574. authenticated. Each method consists of a type and a list of parameter
  575. descriptors. A parameter descriptor consists of a type and a cookie which
  576. will point to specific information required to extract that parameter from
  577. the image (i.e. if the parameter is stored in an x509v3 extension, the
  578. cookie will point to the extension OID). Depending on the method type, a
  579. different number of parameters must be specified. This pointer should not be
  580. NULL.
  581. Supported methods are:
  582. - ``AUTH_METHOD_HASH``: the hash of the image must match the hash extracted
  583. from the parent image. The following parameter descriptors must be
  584. specified:
  585. - ``data``: data to be hashed (obtained from current image)
  586. - ``hash``: reference hash (obtained from parent image)
  587. - ``AUTH_METHOD_SIG``: the image (usually a certificate) must be signed with
  588. the private key whose public part is extracted from the parent image (or
  589. the platform if the parent is NULL). The following parameter descriptors
  590. must be specified:
  591. - ``pk``: the public key (obtained from parent image)
  592. - ``sig``: the digital signature (obtained from current image)
  593. - ``alg``: the signature algorithm used (obtained from current image)
  594. - ``data``: the data to be signed (obtained from current image)
  595. - ``authenticated_data``: this array pointer indicates what authentication
  596. parameters must be extracted from an image once it has been authenticated.
  597. Each parameter consists of a parameter descriptor and the buffer
  598. address/size to store the parameter. The CoT is responsible for allocating
  599. the required memory to store the parameters. This pointer may be NULL.
  600. In the ``tbbr_cot*.c`` file, a set of buffers are allocated to store the parameters
  601. extracted from the certificates. In the case of the TBBR CoT, these parameters
  602. are hashes and public keys. In DER format, an RSA-4096 public key requires 550
  603. bytes, and a hash requires 51 bytes. Depending on the CoT and the authentication
  604. process, some of the buffers may be reused at different stages during the boot.
  605. Next in that file, the parameter descriptors are defined. These descriptors will
  606. be used to extract the parameter data from the corresponding image.
  607. Example: the BL31 Chain of Trust
  608. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  609. Four image descriptors form the BL31 Chain of Trust:
  610. .. code:: c
  611. static const auth_img_desc_t trusted_key_cert = {
  612. .img_id = TRUSTED_KEY_CERT_ID,
  613. .img_type = IMG_CERT,
  614. .parent = NULL,
  615. .img_auth_methods = (const auth_method_desc_t[AUTH_METHOD_NUM]) {
  616. [0] = {
  617. .type = AUTH_METHOD_SIG,
  618. .param.sig = {
  619. .pk = &subject_pk,
  620. .sig = &sig,
  621. .alg = &sig_alg,
  622. .data = &raw_data
  623. }
  624. },
  625. [1] = {
  626. .type = AUTH_METHOD_NV_CTR,
  627. .param.nv_ctr = {
  628. .cert_nv_ctr = &trusted_nv_ctr,
  629. .plat_nv_ctr = &trusted_nv_ctr
  630. }
  631. }
  632. },
  633. .authenticated_data = (const auth_param_desc_t[COT_MAX_VERIFIED_PARAMS]) {
  634. [0] = {
  635. .type_desc = &trusted_world_pk,
  636. .data = {
  637. .ptr = (void *)trusted_world_pk_buf,
  638. .len = (unsigned int)PK_DER_LEN
  639. }
  640. },
  641. [1] = {
  642. .type_desc = &non_trusted_world_pk,
  643. .data = {
  644. .ptr = (void *)non_trusted_world_pk_buf,
  645. .len = (unsigned int)PK_DER_LEN
  646. }
  647. }
  648. }
  649. };
  650. static const auth_img_desc_t soc_fw_key_cert = {
  651. .img_id = SOC_FW_KEY_CERT_ID,
  652. .img_type = IMG_CERT,
  653. .parent = &trusted_key_cert,
  654. .img_auth_methods = (const auth_method_desc_t[AUTH_METHOD_NUM]) {
  655. [0] = {
  656. .type = AUTH_METHOD_SIG,
  657. .param.sig = {
  658. .pk = &trusted_world_pk,
  659. .sig = &sig,
  660. .alg = &sig_alg,
  661. .data = &raw_data
  662. }
  663. },
  664. [1] = {
  665. .type = AUTH_METHOD_NV_CTR,
  666. .param.nv_ctr = {
  667. .cert_nv_ctr = &trusted_nv_ctr,
  668. .plat_nv_ctr = &trusted_nv_ctr
  669. }
  670. }
  671. },
  672. .authenticated_data = (const auth_param_desc_t[COT_MAX_VERIFIED_PARAMS]) {
  673. [0] = {
  674. .type_desc = &soc_fw_content_pk,
  675. .data = {
  676. .ptr = (void *)content_pk_buf,
  677. .len = (unsigned int)PK_DER_LEN
  678. }
  679. }
  680. }
  681. };
  682. static const auth_img_desc_t soc_fw_content_cert = {
  683. .img_id = SOC_FW_CONTENT_CERT_ID,
  684. .img_type = IMG_CERT,
  685. .parent = &soc_fw_key_cert,
  686. .img_auth_methods = (const auth_method_desc_t[AUTH_METHOD_NUM]) {
  687. [0] = {
  688. .type = AUTH_METHOD_SIG,
  689. .param.sig = {
  690. .pk = &soc_fw_content_pk,
  691. .sig = &sig,
  692. .alg = &sig_alg,
  693. .data = &raw_data
  694. }
  695. },
  696. [1] = {
  697. .type = AUTH_METHOD_NV_CTR,
  698. .param.nv_ctr = {
  699. .cert_nv_ctr = &trusted_nv_ctr,
  700. .plat_nv_ctr = &trusted_nv_ctr
  701. }
  702. }
  703. },
  704. .authenticated_data = (const auth_param_desc_t[COT_MAX_VERIFIED_PARAMS]) {
  705. [0] = {
  706. .type_desc = &soc_fw_hash,
  707. .data = {
  708. .ptr = (void *)soc_fw_hash_buf,
  709. .len = (unsigned int)HASH_DER_LEN
  710. }
  711. },
  712. [1] = {
  713. .type_desc = &soc_fw_config_hash,
  714. .data = {
  715. .ptr = (void *)soc_fw_config_hash_buf,
  716. .len = (unsigned int)HASH_DER_LEN
  717. }
  718. }
  719. }
  720. };
  721. static const auth_img_desc_t bl31_image = {
  722. .img_id = BL31_IMAGE_ID,
  723. .img_type = IMG_RAW,
  724. .parent = &soc_fw_content_cert,
  725. .img_auth_methods = (const auth_method_desc_t[AUTH_METHOD_NUM]) {
  726. [0] = {
  727. .type = AUTH_METHOD_HASH,
  728. .param.hash = {
  729. .data = &raw_data,
  730. .hash = &soc_fw_hash
  731. }
  732. }
  733. }
  734. };
  735. The **Trusted Key certificate** is signed with the ROT private key and contains
  736. the Trusted World public key and the Non-Trusted World public key as x509v3
  737. extensions. This must be specified in the image descriptor using the
  738. ``img_auth_methods`` and ``authenticated_data`` arrays, respectively.
  739. The Trusted Key certificate is authenticated by checking its digital signature
  740. using the ROTPK. Four parameters are required to check a signature: the public
  741. key, the algorithm, the signature and the data that has been signed. Therefore,
  742. four parameter descriptors must be specified with the authentication method:
  743. - ``subject_pk``: parameter descriptor of type ``AUTH_PARAM_PUB_KEY``. This type
  744. is used to extract a public key from the parent image. If the cookie is an
  745. OID, the key is extracted from the corresponding x509v3 extension. If the
  746. cookie is NULL, the subject public key is retrieved. In this case, because
  747. the parent image is NULL, the public key is obtained from the platform
  748. (this key will be the ROTPK).
  749. - ``sig``: parameter descriptor of type ``AUTH_PARAM_SIG``. It is used to extract
  750. the signature from the certificate.
  751. - ``sig_alg``: parameter descriptor of type ``AUTH_PARAM_SIG``. It is used to
  752. extract the signature algorithm from the certificate.
  753. - ``raw_data``: parameter descriptor of type ``AUTH_PARAM_RAW_DATA``. It is used
  754. to extract the data to be signed from the certificate.
  755. Once the signature has been checked and the certificate authenticated, the
  756. Trusted World public key needs to be extracted from the certificate. A new entry
  757. is created in the ``authenticated_data`` array for that purpose. In that entry,
  758. the corresponding parameter descriptor must be specified along with the buffer
  759. address to store the parameter value. In this case, the ``trusted_world_pk``
  760. descriptor is used to extract the public key from an x509v3 extension with OID
  761. ``TRUSTED_WORLD_PK_OID``. The BL31 key certificate will use this descriptor as
  762. parameter in the signature authentication method. The key is stored in the
  763. ``trusted_world_pk_buf`` buffer.
  764. The **BL31 Key certificate** is authenticated by checking its digital signature
  765. using the Trusted World public key obtained previously from the Trusted Key
  766. certificate. In the image descriptor, we specify a single authentication method
  767. by signature whose public key is the ``trusted_world_pk``. Once this certificate
  768. has been authenticated, we have to extract the BL31 public key, stored in the
  769. extension specified by ``soc_fw_content_pk``. This key will be copied to the
  770. ``content_pk_buf`` buffer.
  771. The **BL31 certificate** is authenticated by checking its digital signature
  772. using the BL31 public key obtained previously from the BL31 Key certificate.
  773. We specify the authentication method using ``soc_fw_content_pk`` as public key.
  774. After authentication, we need to extract the BL31 hash, stored in the extension
  775. specified by ``soc_fw_hash``. This hash will be copied to the
  776. ``soc_fw_hash_buf`` buffer.
  777. The **BL31 image** is authenticated by calculating its hash and matching it
  778. with the hash obtained from the BL31 certificate. The image descriptor contains
  779. a single authentication method by hash. The parameters to the hash method are
  780. the reference hash, ``soc_fw_hash``, and the data to be hashed. In this case,
  781. it is the whole image, so we specify ``raw_data``.
  782. The image parser library
  783. ~~~~~~~~~~~~~~~~~~~~~~~~
  784. The image parser module relies on libraries to check the image integrity and
  785. extract the authentication parameters. The number and type of parser libraries
  786. depend on the images used in the CoT. Raw images do not need a library, so
  787. only an x509v3 library is required for the TBBR CoT.
  788. Arm platforms will use an x509v3 library based on mbed TLS. This library may be
  789. found in ``drivers/auth/mbedtls/mbedtls_x509_parser.c``. It exports three
  790. functions:
  791. .. code:: c
  792. void init(void);
  793. int check_integrity(void *img, unsigned int img_len);
  794. int get_auth_param(const auth_param_type_desc_t *type_desc,
  795. void *img, unsigned int img_len,
  796. void **param, unsigned int *param_len);
  797. The library is registered in the framework using the macro
  798. ``REGISTER_IMG_PARSER_LIB()``. Each time the image parser module needs to access
  799. an image of type ``IMG_CERT``, it will call the corresponding function exported
  800. in this file.
  801. The build system must be updated to include the corresponding library and
  802. mbed TLS sources. Arm platforms use the ``arm_common.mk`` file to pull the
  803. sources.
  804. The cryptographic library
  805. ~~~~~~~~~~~~~~~~~~~~~~~~~
  806. The cryptographic module relies on a library to perform essential operations
  807. such as verifying a hash or a digital signature.
  808. Arm platforms use a library based on mbedTLS located at
  809. ``drivers/auth/mbedtls/mbedtls_crypto.c``.
  810. Additionally, an experimental alternative library based on PSA Crypto
  811. is available at ``drivers/auth/mbedtls/mbedtls_psa_crypto.c``. In future,
  812. ``mbedtls_psa_crypto.c`` will replace ``mbedtls_crypto.c`` as the default Arm
  813. CM. Both libraries are registered in the authentication framework using
  814. the macro ``REGISTER_CRYPTO_LIB()``. These libraries implement the following
  815. exported functions, their implementations are compared side-by-side below:
  816. .. list-table:: Comparison of exported CM function implementations
  817. :widths: 20 40 40
  818. :header-rows: 1
  819. * - CM function
  820. - ``mbedtls_crypto.c``
  821. - ``mbedtls_psa_crypto.c``
  822. * - ``init``
  823. - Initialize the heap for mbedTLS.
  824. - Initialize the heap for mbedTLS and call ``psa_crypto_init``.
  825. * - ``verify_signature``
  826. - Use mbedTLS to parse the ASN1 inputs, and then use the mbedTLS pk module to verify the signature.
  827. - Use mbedTLS to parse the ASN1 inputs, use the mbedTLS pk module to parse the key,
  828. import it into the PSA key system and then use ``psa_verify_message`` to verify the signature.
  829. * - ``calc_hash``
  830. - Use the ``mbedtls_md`` API to calculate the hash of the given data.
  831. - Use ``psa_hash_compute`` to calculate the hash of the given data.
  832. * - ``verify_hash``
  833. - Use the ``mbedtls_md`` API to calculate the hash of the given data,
  834. and then compare it against the data which is to be verified.
  835. - Call ``psa_hash_compare``, which both calculates the hash of the given data and
  836. compares this hash against the data to be verified.
  837. * - ``auth_decrypt``
  838. - Use the ``mbedtls_gcm`` API to decrypt the data, and then verify the returned
  839. tag by comparing it to the inputted tag.
  840. - Load the key into the PSA key store, and then use ``psa_aead_verify`` to
  841. decrypt and verify the tag.
  842. The mbedTLS library algorithm support is configured by both the
  843. ``TF_MBEDTLS_KEY_ALG`` and ``TF_MBEDTLS_KEY_SIZE`` variables.
  844. - ``TF_MBEDTLS_KEY_ALG`` can take in 3 values: `rsa`, `ecdsa` or `rsa+ecdsa`.
  845. This variable allows the Makefile to include the corresponding sources in
  846. the build for the various algorithms. Setting the variable to `rsa+ecdsa`
  847. enables support for both rsa and ecdsa algorithms in the mbedTLS library.
  848. - ``TF_MBEDTLS_KEY_SIZE`` sets the supported RSA key size for TFA. Valid values
  849. include 1024, 2048, 3072 and 4096.
  850. - ``TF_MBEDTLS_USE_AES_GCM`` enables the authenticated decryption support based
  851. on AES-GCM algorithm. Valid values are 0 and 1.
  852. .. note::
  853. If code size is a concern, the build option ``MBEDTLS_SHA256_SMALLER`` can
  854. be defined in the platform Makefile. It will make mbed TLS use an
  855. implementation of SHA-256 with smaller memory footprint (~1.5 KB less) but
  856. slower (~30%).
  857. --------------
  858. *Copyright (c) 2017-2024, Arm Limited and Contributors. All rights reserved.*
  859. .. _TBBR-Client specification: https://developer.arm.com/docs/den0006/latest