blob: 05a8f45038d107307fcb164d4ba7173a8388ff09 [file] [log] [blame]
Juan Castillo8e55d932015-04-02 09:48:16 +01001/*
Lauren Wehrmeister351f2f22025-04-28 15:41:23 -05002 * Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved.
Juan Castillo8e55d932015-04-02 09:48:16 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo8e55d932015-04-02 09:48:16 +01005 */
6
7#include <assert.h>
Juan Castillo8e55d932015-04-02 09:48:16 +01008#include <stdint.h>
9#include <string.h>
10
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <platform_def.h>
12
13#include <common/debug.h>
14#include <common/tbbr/cot_def.h>
15#include <drivers/auth/auth_common.h>
16#include <drivers/auth/auth_mod.h>
Lauren Wehrmeister351f2f22025-04-28 15:41:23 -050017#include <drivers/auth/auth_util.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018#include <drivers/auth/crypto_mod.h>
19#include <drivers/auth/img_parser_mod.h>
Manish V Badarkhe04005b72021-06-20 22:29:22 +010020#include <drivers/fwu/fwu.h>
Louis Mayencourt944ade82019-08-08 12:03:26 +010021#include <lib/fconf/fconf_tbbr_getter.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000022#include <plat/common/platform.h>
23
Manish V Badarkhecaf03fd2023-04-11 12:55:07 +010024#include <tools_share/zero_oid.h>
25
Juan Castillobfb7fa62016-01-22 11:05:57 +000026/* ASN.1 tags */
27#define ASN1_INTEGER 0x02
28
dp-armb3e85802016-12-12 14:48:13 +000029#pragma weak plat_set_nv_ctr2
30
Juan Castillo8e55d932015-04-02 09:48:16 +010031static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
32 const auth_param_type_desc_t *b)
33{
34 if ((a->type == b->type) && (a->cookie == b->cookie)) {
35 return 0;
36 }
37 return 1;
38}
39
40/*
41 * This function obtains the requested authentication parameter data from the
42 * information extracted from the parent image after its authentication.
43 */
44static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
45 const auth_img_desc_t *img_desc,
46 void **param, unsigned int *len)
47{
48 int i;
49
Joel Hutton69931af2019-03-11 11:37:38 +000050 if (img_desc->authenticated_data == NULL)
51 return 1;
52
Juan Castillo8e55d932015-04-02 09:48:16 +010053 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
54 if (0 == cmp_auth_param_type_desc(param_type_desc,
55 img_desc->authenticated_data[i].type_desc)) {
56 *param = img_desc->authenticated_data[i].data.ptr;
57 *len = img_desc->authenticated_data[i].data.len;
58 return 0;
59 }
60 }
61
62 return 1;
63}
64
65/*
66 * Authenticate an image by matching the data hash
67 *
68 * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
69 * this method, the image must contain:
70 *
71 * - The data to calculate the hash from
72 *
73 * The parent image must contain:
74 *
75 * - The hash to be matched with (including hash algorithm)
76 *
77 * For a successful authentication, both hashes must match. The function calls
78 * the crypto-module to check this matching.
79 *
80 * Parameters:
81 * param: parameters to perform the hash authentication
82 * img_desc: pointer to image descriptor so we can know the image type
83 * and parent image
84 * img: pointer to image in memory
85 * img_len: length of image (in bytes)
86 *
87 * Return:
88 * 0 = success, Otherwise = error
89 */
90static int auth_hash(const auth_method_param_hash_t *param,
91 const auth_img_desc_t *img_desc,
92 void *img, unsigned int img_len)
93{
94 void *data_ptr, *hash_der_ptr;
95 unsigned int data_len, hash_der_len;
Sandrine Bailleux98913152023-11-09 14:26:42 +010096 int rc;
Juan Castillo8e55d932015-04-02 09:48:16 +010097
98 /* Get the hash from the parent image. This hash will be DER encoded
99 * and contain the hash algorithm */
100 rc = auth_get_param(param->hash, img_desc->parent,
101 &hash_der_ptr, &hash_der_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100102 if (rc != 0) {
103 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
104 __func__, __LINE__, rc);
105 return rc;
106 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100107
108 /* Get the data to be hashed from the current image */
109 rc = img_parser_get_auth_param(img_desc->img_type, param->data,
110 img, img_len, &data_ptr, &data_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100111 if (rc != 0) {
112 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
113 __func__, __LINE__, rc);
114 return rc;
115 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100116
117 /* Ask the crypto module to verify this hash */
118 rc = crypto_mod_verify_hash(data_ptr, data_len,
119 hash_der_ptr, hash_der_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100120 if (rc != 0) {
121 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
122 __func__, __LINE__, rc);
123 return rc;
124 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100125
Sandrine Bailleux98913152023-11-09 14:26:42 +0100126 return 0;
Juan Castillo8e55d932015-04-02 09:48:16 +0100127}
128
129/*
130 * Authenticate by digital signature
131 *
132 * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
133 * this method, the image must contain:
134 *
135 * - Data to be signed
136 * - Signature
137 * - Signature algorithm
138 *
139 * We rely on the image parser module to extract this data from the image.
140 * The parent image must contain:
141 *
142 * - Public key (or a hash of it)
143 *
144 * If the parent image contains only a hash of the key, we will try to obtain
145 * the public key from the image itself (i.e. self-signed certificates). In that
146 * case, the signature verification is considered just an integrity check and
147 * the authentication is established by calculating the hash of the key and
148 * comparing it with the hash obtained from the parent.
149 *
150 * If the image has no parent (NULL), it means it has to be authenticated using
151 * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
152 * or a hash of it.
153 *
154 * Return: 0 = success, Otherwise = error
155 */
156static int auth_signature(const auth_method_param_sig_t *param,
157 const auth_img_desc_t *img_desc,
158 void *img, unsigned int img_len)
159{
Robin van der Gracht5cea3132023-09-13 10:02:38 +0200160 void *data_ptr, *pk_ptr, *cnv_pk_ptr, *pk_plat_ptr, *sig_ptr, *sig_alg_ptr, *pk_oid;
161 unsigned int data_len, pk_len, cnv_pk_len, pk_plat_len, sig_len, sig_alg_len;
Juan Castillo8e55d932015-04-02 09:48:16 +0100162 unsigned int flags = 0;
Sandrine Bailleux98913152023-11-09 14:26:42 +0100163 int rc;
Juan Castillo8e55d932015-04-02 09:48:16 +0100164
165 /* Get the data to be signed from current image */
166 rc = img_parser_get_auth_param(img_desc->img_type, param->data,
167 img, img_len, &data_ptr, &data_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100168 if (rc != 0) {
169 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
170 __func__, __LINE__, rc);
171 return rc;
172 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100173
174 /* Get the signature from current image */
175 rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
176 img, img_len, &sig_ptr, &sig_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100177 if (rc != 0) {
178 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
179 __func__, __LINE__, rc);
180 return rc;
181 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100182
183 /* Get the signature algorithm from current image */
184 rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
185 img, img_len, &sig_alg_ptr, &sig_alg_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100186 if (rc != 0) {
187 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
188 __func__, __LINE__, rc);
189 return rc;
190 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100191
Lauren Wehrmeister351f2f22025-04-28 15:41:23 -0500192 /*
193 * Set Zero-OID for ROTPK(subject key) as a the certificate
194 * does not hold Key-OID information for ROTPK.
195 */
196 if (param->pk->cookie != NULL) {
197 pk_oid = param->pk->cookie;
198 } else {
199 pk_oid = ZERO_OID;
200 }
201
202 set_current_pk_oid(pk_oid);
203
Juan Castillo8e55d932015-04-02 09:48:16 +0100204 /* Get the public key from the parent. If there is no parent (NULL),
205 * the certificate has been signed with the ROTPK, so we have to get
206 * the PK from the platform */
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000207 if (img_desc->parent != NULL) {
Juan Castillo8e55d932015-04-02 09:48:16 +0100208 rc = auth_get_param(param->pk, img_desc->parent,
209 &pk_ptr, &pk_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100210 if (rc != 0) {
211 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
212 __func__, __LINE__, rc);
213 return rc;
214 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100215 } else {
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000216 /*
217 * Root certificates are signed with the ROTPK, so we have to
218 * get it from the platform.
219 */
220 rc = plat_get_rotpk_info(param->pk->cookie, &pk_plat_ptr,
221 &pk_plat_len, &flags);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100222 if (rc != 0) {
223 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
224 __func__, __LINE__, rc);
225 return rc;
226 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100227
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000228 assert(is_rotpk_flags_valid(flags));
229
230 /* Also retrieve the key from the image. */
231 rc = img_parser_get_auth_param(img_desc->img_type,
232 param->pk, img, img_len,
233 &pk_ptr, &pk_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100234 if (rc != 0) {
235 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
236 __func__, __LINE__, rc);
237 return rc;
238 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100239
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000240 /*
241 * Validate the certificate's key against the platform ROTPK.
242 *
243 * Platform may store key in one of the following way -
244 * 1. Hash of ROTPK
245 * 2. Hash if prefixed, suffixed or modified ROTPK
246 * 3. Full ROTPK
247 */
248 if ((flags & ROTPK_NOT_DEPLOYED) != 0U) {
Soby Mathew2f7ed052016-05-24 15:05:15 +0100249 NOTICE("ROTPK is not deployed on platform. "
250 "Skipping ROTPK verification.\n");
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000251 } else if ((flags & ROTPK_IS_HASH) != 0U) {
252 /*
253 * platform may store the hash of a prefixed,
254 * suffixed or modified pk
255 */
Robin van der Gracht5cea3132023-09-13 10:02:38 +0200256 rc = crypto_mod_convert_pk(pk_ptr, pk_len, &cnv_pk_ptr, &cnv_pk_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100257 if (rc != 0) {
258 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
259 __func__, __LINE__, rc);
260 return rc;
261 }
Nicolas Toromanoff7f95ac82020-11-09 12:14:52 +0100262
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000263 /*
264 * The hash of the certificate's public key must match
265 * the hash of the ROTPK.
266 */
Robin van der Gracht5cea3132023-09-13 10:02:38 +0200267 rc = crypto_mod_verify_hash(cnv_pk_ptr, cnv_pk_len,
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000268 pk_plat_ptr, pk_plat_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100269 if (rc != 0) {
270 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
271 __func__, __LINE__, rc);
272 return rc;
273 }
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000274 } else {
275 /* Platform supports full ROTPK */
276 if ((pk_len != pk_plat_len) ||
277 (memcmp(pk_plat_ptr, pk_ptr, pk_len) != 0)) {
278 ERROR("plat and cert ROTPK len mismatch\n");
279 return -1;
280 }
Soby Mathew2f7ed052016-05-24 15:05:15 +0100281 }
Manish V Badarkhecaf03fd2023-04-11 12:55:07 +0100282
283 /*
Manish V Badarkhecaf03fd2023-04-11 12:55:07 +0100284 * Public key is verified at this stage, notify platform
285 * to measure and publish it.
286 */
287 rc = plat_mboot_measure_key(pk_oid, pk_ptr, pk_len);
288 if (rc != 0) {
Sandrine Bailleux98913152023-11-09 14:26:42 +0100289 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
290 __func__, __LINE__, rc);
Manish V Badarkhecaf03fd2023-04-11 12:55:07 +0100291 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100292 }
293
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000294 /* Ask the crypto module to verify the signature */
295 rc = crypto_mod_verify_signature(data_ptr, data_len,
296 sig_ptr, sig_len,
297 sig_alg_ptr, sig_alg_len,
298 pk_ptr, pk_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100299 if (rc != 0) {
300 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
301 __func__, __LINE__, rc);
302 return rc;
303 }
Manish V Badarkhee984bc72023-03-10 19:00:02 +0000304
Sandrine Bailleux98913152023-11-09 14:26:42 +0100305 return 0;
Juan Castillo8e55d932015-04-02 09:48:16 +0100306}
307
308/*
Juan Castillobfb7fa62016-01-22 11:05:57 +0000309 * Authenticate by Non-Volatile counter
310 *
311 * To protect the system against rollback, the platform includes a non-volatile
312 * counter whose value can only be increased. All certificates include a counter
313 * value that should not be lower than the value stored in the platform. If the
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100314 * value is larger, the counter in the platform must be updated to the new value
315 * (provided it has been authenticated).
Juan Castillobfb7fa62016-01-22 11:05:57 +0000316 *
317 * Return: 0 = success, Otherwise = error
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100318 * Returns additionally,
319 * cert_nv_ctr -> NV counter value present in the certificate
320 * need_nv_ctr_upgrade = 0 -> platform NV counter upgrade is not needed
321 * need_nv_ctr_upgrade = 1 -> platform NV counter upgrade is needed
Juan Castillobfb7fa62016-01-22 11:05:57 +0000322 */
323static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
324 const auth_img_desc_t *img_desc,
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100325 void *img, unsigned int img_len,
326 unsigned int *cert_nv_ctr,
327 bool *need_nv_ctr_upgrade)
Juan Castillobfb7fa62016-01-22 11:05:57 +0000328{
Demi Marie Obenour745f6f52022-12-09 18:21:47 -0500329 unsigned char *p;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000330 void *data_ptr = NULL;
331 unsigned int data_len, len, i;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100332 unsigned int plat_nv_ctr;
Sandrine Bailleux98913152023-11-09 14:26:42 +0100333 int rc;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000334
335 /* Get the counter value from current image. The AM expects the IPM
336 * to return the counter value as a DER encoded integer */
337 rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
338 img, img_len, &data_ptr, &data_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100339 if (rc != 0) {
340 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
341 __func__, __LINE__, rc);
342 return rc;
343 }
Juan Castillobfb7fa62016-01-22 11:05:57 +0000344
345 /* Parse the DER encoded integer */
346 assert(data_ptr);
Demi Marie Obenour745f6f52022-12-09 18:21:47 -0500347 p = (unsigned char *)data_ptr;
348
349 /*
350 * Integers must be at least 3 bytes: 1 for tag, 1 for length, and 1
351 * for value. The first byte (tag) must be ASN1_INTEGER.
352 */
353 if ((data_len < 3) || (*p != ASN1_INTEGER)) {
Juan Castillobfb7fa62016-01-22 11:05:57 +0000354 /* Invalid ASN.1 integer */
355 return 1;
356 }
357 p++;
358
Demi Marie Obenour745f6f52022-12-09 18:21:47 -0500359 /*
360 * NV-counters are unsigned integers up to 31 bits. Trailing
361 * padding is not allowed.
362 */
363 len = (unsigned int)*p;
364 if ((len > 4) || (data_len - 2 != len)) {
Juan Castillobfb7fa62016-01-22 11:05:57 +0000365 return 1;
366 }
367 p++;
368
369 /* Check the number is not negative */
370 if (*p & 0x80) {
371 return 1;
372 }
373
374 /* Convert to unsigned int. This code is for a little-endian CPU */
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100375 *cert_nv_ctr = 0;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000376 for (i = 0; i < len; i++) {
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100377 *cert_nv_ctr = (*cert_nv_ctr << 8) | *p++;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000378 }
379
380 /* Get the counter from the platform */
381 rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100382 if (rc != 0) {
383 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
384 __func__, __LINE__, rc);
385 return rc;
386 }
Juan Castillobfb7fa62016-01-22 11:05:57 +0000387
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100388 if (*cert_nv_ctr < plat_nv_ctr) {
Juan Castillobfb7fa62016-01-22 11:05:57 +0000389 /* Invalid NV-counter */
390 return 1;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100391 } else if (*cert_nv_ctr > plat_nv_ctr) {
Manish V Badarkhe04005b72021-06-20 22:29:22 +0100392#if PSA_FWU_SUPPORT && IMAGE_BL2
Sughosh Ganu84b2f7d2024-02-01 16:59:01 +0530393 if (fwu_get_active_bank_state() == FWU_BANK_STATE_ACCEPTED) {
394 *need_nv_ctr_upgrade = true;
395 } else {
396 *need_nv_ctr_upgrade = false;
397 }
398#else
399 *need_nv_ctr_upgrade = true;
Manish V Badarkhe04005b72021-06-20 22:29:22 +0100400#endif /* PSA_FWU_SUPPORT && IMAGE_BL2 */
Juan Castillobfb7fa62016-01-22 11:05:57 +0000401 }
402
403 return 0;
404}
405
dp-armb3e85802016-12-12 14:48:13 +0000406int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
407 unsigned int nv_ctr)
408{
409 return plat_set_nv_ctr(cookie, nv_ctr);
410}
411
Juan Castillobfb7fa62016-01-22 11:05:57 +0000412/*
Juan Castillo8e55d932015-04-02 09:48:16 +0100413 * Return the parent id in the output parameter '*parent_id'
414 *
415 * Return value:
416 * 0 = Image has parent, 1 = Image has no parent or parent is authenticated
417 */
418int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
419{
420 const auth_img_desc_t *img_desc = NULL;
421
422 assert(parent_id != NULL);
Juan Castillo8e55d932015-04-02 09:48:16 +0100423 /* Get the image descriptor */
Louis Mayencourt944ade82019-08-08 12:03:26 +0100424 img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
Juan Castillo8e55d932015-04-02 09:48:16 +0100425
426 /* Check if the image has no parent (ROT) */
427 if (img_desc->parent == NULL) {
428 *parent_id = 0;
429 return 1;
430 }
431
432 /* Check if the parent has already been authenticated */
433 if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
434 *parent_id = 0;
435 return 1;
436 }
437
438 *parent_id = img_desc->parent->img_id;
439 return 0;
440}
441
442/*
443 * Initialize the different modules in the authentication framework
444 */
445void auth_mod_init(void)
446{
447 /* Check we have a valid CoT registered */
448 assert(cot_desc_ptr != NULL);
449
Juan Castillo8e55d932015-04-02 09:48:16 +0100450 /* Image parser module */
451 img_parser_init();
452}
453
454/*
455 * Authenticate a certificate/image
456 *
457 * Return: 0 = success, Otherwise = error
458 */
459int auth_mod_verify_img(unsigned int img_id,
460 void *img_ptr,
461 unsigned int img_len)
462{
463 const auth_img_desc_t *img_desc = NULL;
Manish V Badarkhecaf03fd2023-04-11 12:55:07 +0100464 const auth_param_type_desc_t *type_desc = NULL;
Juan Castillo8e55d932015-04-02 09:48:16 +0100465 const auth_method_desc_t *auth_method = NULL;
466 void *param_ptr;
467 unsigned int param_len;
468 int rc, i;
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100469 unsigned int cert_nv_ctr = 0;
470 bool need_nv_ctr_upgrade = false;
471 bool sig_auth_done = false;
472 const auth_method_param_nv_ctr_t *nv_ctr_param = NULL;
Juan Castillo8e55d932015-04-02 09:48:16 +0100473
474 /* Get the image descriptor from the chain of trust */
Louis Mayencourt944ade82019-08-08 12:03:26 +0100475 img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
Juan Castillo8e55d932015-04-02 09:48:16 +0100476
477 /* Ask the parser to check the image integrity */
478 rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100479 if (rc != 0) {
480 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
481 __func__, __LINE__, rc);
482 return rc;
483 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100484
485 /* Authenticate the image using the methods indicated in the image
486 * descriptor. */
Joel Hutton8634cff2019-04-09 09:25:55 +0100487 if (img_desc->img_auth_methods == NULL)
Joel Hutton69931af2019-03-11 11:37:38 +0000488 return 1;
Juan Castillo8e55d932015-04-02 09:48:16 +0100489 for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
490 auth_method = &img_desc->img_auth_methods[i];
491 switch (auth_method->type) {
492 case AUTH_METHOD_NONE:
493 rc = 0;
494 break;
495 case AUTH_METHOD_HASH:
496 rc = auth_hash(&auth_method->param.hash,
497 img_desc, img_ptr, img_len);
498 break;
499 case AUTH_METHOD_SIG:
500 rc = auth_signature(&auth_method->param.sig,
501 img_desc, img_ptr, img_len);
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100502 sig_auth_done = true;
Juan Castillo8e55d932015-04-02 09:48:16 +0100503 break;
Juan Castillobfb7fa62016-01-22 11:05:57 +0000504 case AUTH_METHOD_NV_CTR:
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100505 nv_ctr_param = &auth_method->param.nv_ctr;
506 rc = auth_nvctr(nv_ctr_param,
507 img_desc, img_ptr, img_len,
508 &cert_nv_ctr, &need_nv_ctr_upgrade);
Juan Castillobfb7fa62016-01-22 11:05:57 +0000509 break;
Juan Castillo8e55d932015-04-02 09:48:16 +0100510 default:
511 /* Unknown authentication method */
512 rc = 1;
513 break;
514 }
Sandrine Bailleux98913152023-11-09 14:26:42 +0100515 if (rc != 0) {
516 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
517 __func__, __LINE__, rc);
518 return rc;
519 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100520 }
521
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100522 /*
523 * Do platform NV counter upgrade only if the certificate gets
524 * authenticated, and platform NV-counter upgrade is needed.
525 */
526 if (need_nv_ctr_upgrade && sig_auth_done) {
527 rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie,
528 img_desc, cert_nv_ctr);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100529 if (rc != 0) {
530 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
531 __func__, __LINE__, rc);
532 return rc;
533 }
Manish V Badarkhe5989c562021-04-25 16:32:11 +0100534 }
535
Juan Castillo8e55d932015-04-02 09:48:16 +0100536 /* Extract the parameters indicated in the image descriptor to
537 * authenticate the children images. */
Joel Hutton69931af2019-03-11 11:37:38 +0000538 if (img_desc->authenticated_data != NULL) {
539 for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
540 if (img_desc->authenticated_data[i].type_desc == NULL) {
541 continue;
542 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100543
Joel Hutton69931af2019-03-11 11:37:38 +0000544 /* Get the parameter from the image parser module */
545 rc = img_parser_get_auth_param(img_desc->img_type,
546 img_desc->authenticated_data[i].type_desc,
547 img_ptr, img_len, &param_ptr, &param_len);
Sandrine Bailleux98913152023-11-09 14:26:42 +0100548 if (rc != 0) {
549 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
550 __func__, __LINE__, rc);
551 return rc;
552 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100553
Joel Hutton69931af2019-03-11 11:37:38 +0000554 /* Check parameter size */
555 if (param_len > img_desc->authenticated_data[i].data.len) {
556 return 1;
557 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100558
Joel Hutton69931af2019-03-11 11:37:38 +0000559 /* Copy the parameter for later use */
560 memcpy((void *)img_desc->authenticated_data[i].data.ptr,
561 (void *)param_ptr, param_len);
Manish V Badarkhecaf03fd2023-04-11 12:55:07 +0100562
563 /*
564 * If this is a public key then measure and publicise
565 * it.
566 */
567 type_desc = img_desc->authenticated_data[i].type_desc;
568 if (type_desc->type == AUTH_PARAM_PUB_KEY) {
569 rc = plat_mboot_measure_key(type_desc->cookie,
570 param_ptr,
571 param_len);
572 if (rc != 0) {
Sandrine Bailleux98913152023-11-09 14:26:42 +0100573 VERBOSE("[TBB] %s():%d failed with error code %d.\n",
574 __func__, __LINE__, rc);
Manish V Badarkhecaf03fd2023-04-11 12:55:07 +0100575 }
576 }
Joel Hutton69931af2019-03-11 11:37:38 +0000577 }
Juan Castillo8e55d932015-04-02 09:48:16 +0100578 }
579
580 /* Mark image as authenticated */
581 auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
582
583 return 0;
584}