105799ae0SJuan Castillo /* 2f1e693a7SManish V Badarkhe * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved. 305799ae0SJuan Castillo * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 505799ae0SJuan Castillo */ 605799ae0SJuan Castillo 705799ae0SJuan Castillo #include <assert.h> 805799ae0SJuan Castillo #include <stdint.h> 905799ae0SJuan Castillo #include <string.h> 1005799ae0SJuan Castillo 1109d40e0eSAntonio Nino Diaz #include <platform_def.h> 1209d40e0eSAntonio Nino Diaz 1309d40e0eSAntonio Nino Diaz #include <common/debug.h> 1409d40e0eSAntonio Nino Diaz #include <common/tbbr/cot_def.h> 1509d40e0eSAntonio Nino Diaz #include <drivers/auth/auth_common.h> 1609d40e0eSAntonio Nino Diaz #include <drivers/auth/auth_mod.h> 1709d40e0eSAntonio Nino Diaz #include <drivers/auth/crypto_mod.h> 1809d40e0eSAntonio Nino Diaz #include <drivers/auth/img_parser_mod.h> 19c0bfc88fSManish V Badarkhe #include <drivers/fwu/fwu.h> 20ab1981dbSLouis Mayencourt #include <lib/fconf/fconf_tbbr_getter.h> 2109d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 2209d40e0eSAntonio Nino Diaz 239eaa5a09SManish V Badarkhe #include <tools_share/zero_oid.h> 249eaa5a09SManish V Badarkhe 2548279d52SJuan Castillo /* ASN.1 tags */ 2648279d52SJuan Castillo #define ASN1_INTEGER 0x02 2748279d52SJuan Castillo 28d35dee23Sdp-arm #pragma weak plat_set_nv_ctr2 29d35dee23Sdp-arm 3005799ae0SJuan Castillo static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a, 3105799ae0SJuan Castillo const auth_param_type_desc_t *b) 3205799ae0SJuan Castillo { 3305799ae0SJuan Castillo if ((a->type == b->type) && (a->cookie == b->cookie)) { 3405799ae0SJuan Castillo return 0; 3505799ae0SJuan Castillo } 3605799ae0SJuan Castillo return 1; 3705799ae0SJuan Castillo } 3805799ae0SJuan Castillo 3905799ae0SJuan Castillo /* 4005799ae0SJuan Castillo * This function obtains the requested authentication parameter data from the 4105799ae0SJuan Castillo * information extracted from the parent image after its authentication. 4205799ae0SJuan Castillo */ 4305799ae0SJuan Castillo static int auth_get_param(const auth_param_type_desc_t *param_type_desc, 4405799ae0SJuan Castillo const auth_img_desc_t *img_desc, 4505799ae0SJuan Castillo void **param, unsigned int *len) 4605799ae0SJuan Castillo { 4705799ae0SJuan Castillo int i; 4805799ae0SJuan Castillo 4930070427SJoel Hutton if (img_desc->authenticated_data == NULL) 5030070427SJoel Hutton return 1; 5130070427SJoel Hutton 5205799ae0SJuan Castillo for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) { 5305799ae0SJuan Castillo if (0 == cmp_auth_param_type_desc(param_type_desc, 5405799ae0SJuan Castillo img_desc->authenticated_data[i].type_desc)) { 5505799ae0SJuan Castillo *param = img_desc->authenticated_data[i].data.ptr; 5605799ae0SJuan Castillo *len = img_desc->authenticated_data[i].data.len; 5705799ae0SJuan Castillo return 0; 5805799ae0SJuan Castillo } 5905799ae0SJuan Castillo } 6005799ae0SJuan Castillo 6105799ae0SJuan Castillo return 1; 6205799ae0SJuan Castillo } 6305799ae0SJuan Castillo 6405799ae0SJuan Castillo /* 6505799ae0SJuan Castillo * Authenticate an image by matching the data hash 6605799ae0SJuan Castillo * 6705799ae0SJuan Castillo * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using 6805799ae0SJuan Castillo * this method, the image must contain: 6905799ae0SJuan Castillo * 7005799ae0SJuan Castillo * - The data to calculate the hash from 7105799ae0SJuan Castillo * 7205799ae0SJuan Castillo * The parent image must contain: 7305799ae0SJuan Castillo * 7405799ae0SJuan Castillo * - The hash to be matched with (including hash algorithm) 7505799ae0SJuan Castillo * 7605799ae0SJuan Castillo * For a successful authentication, both hashes must match. The function calls 7705799ae0SJuan Castillo * the crypto-module to check this matching. 7805799ae0SJuan Castillo * 7905799ae0SJuan Castillo * Parameters: 8005799ae0SJuan Castillo * param: parameters to perform the hash authentication 8105799ae0SJuan Castillo * img_desc: pointer to image descriptor so we can know the image type 8205799ae0SJuan Castillo * and parent image 8305799ae0SJuan Castillo * img: pointer to image in memory 8405799ae0SJuan Castillo * img_len: length of image (in bytes) 8505799ae0SJuan Castillo * 8605799ae0SJuan Castillo * Return: 8705799ae0SJuan Castillo * 0 = success, Otherwise = error 8805799ae0SJuan Castillo */ 8905799ae0SJuan Castillo static int auth_hash(const auth_method_param_hash_t *param, 9005799ae0SJuan Castillo const auth_img_desc_t *img_desc, 9105799ae0SJuan Castillo void *img, unsigned int img_len) 9205799ae0SJuan Castillo { 9305799ae0SJuan Castillo void *data_ptr, *hash_der_ptr; 9405799ae0SJuan Castillo unsigned int data_len, hash_der_len; 95575c1469SSandrine Bailleux int rc; 9605799ae0SJuan Castillo 9705799ae0SJuan Castillo /* Get the hash from the parent image. This hash will be DER encoded 9805799ae0SJuan Castillo * and contain the hash algorithm */ 9905799ae0SJuan Castillo rc = auth_get_param(param->hash, img_desc->parent, 10005799ae0SJuan Castillo &hash_der_ptr, &hash_der_len); 101575c1469SSandrine Bailleux if (rc != 0) { 102575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 103575c1469SSandrine Bailleux __func__, __LINE__, rc); 104575c1469SSandrine Bailleux return rc; 105575c1469SSandrine Bailleux } 10605799ae0SJuan Castillo 10705799ae0SJuan Castillo /* Get the data to be hashed from the current image */ 10805799ae0SJuan Castillo rc = img_parser_get_auth_param(img_desc->img_type, param->data, 10905799ae0SJuan Castillo img, img_len, &data_ptr, &data_len); 110575c1469SSandrine Bailleux if (rc != 0) { 111575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 112575c1469SSandrine Bailleux __func__, __LINE__, rc); 113575c1469SSandrine Bailleux return rc; 114575c1469SSandrine Bailleux } 11505799ae0SJuan Castillo 11605799ae0SJuan Castillo /* Ask the crypto module to verify this hash */ 11705799ae0SJuan Castillo rc = crypto_mod_verify_hash(data_ptr, data_len, 11805799ae0SJuan Castillo hash_der_ptr, hash_der_len); 119575c1469SSandrine Bailleux if (rc != 0) { 120575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 121575c1469SSandrine Bailleux __func__, __LINE__, rc); 12205799ae0SJuan Castillo return rc; 12305799ae0SJuan Castillo } 12405799ae0SJuan Castillo 125575c1469SSandrine Bailleux return 0; 126575c1469SSandrine Bailleux } 127575c1469SSandrine Bailleux 12805799ae0SJuan Castillo /* 12905799ae0SJuan Castillo * Authenticate by digital signature 13005799ae0SJuan Castillo * 13105799ae0SJuan Castillo * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using 13205799ae0SJuan Castillo * this method, the image must contain: 13305799ae0SJuan Castillo * 13405799ae0SJuan Castillo * - Data to be signed 13505799ae0SJuan Castillo * - Signature 13605799ae0SJuan Castillo * - Signature algorithm 13705799ae0SJuan Castillo * 13805799ae0SJuan Castillo * We rely on the image parser module to extract this data from the image. 13905799ae0SJuan Castillo * The parent image must contain: 14005799ae0SJuan Castillo * 14105799ae0SJuan Castillo * - Public key (or a hash of it) 14205799ae0SJuan Castillo * 14305799ae0SJuan Castillo * If the parent image contains only a hash of the key, we will try to obtain 14405799ae0SJuan Castillo * the public key from the image itself (i.e. self-signed certificates). In that 14505799ae0SJuan Castillo * case, the signature verification is considered just an integrity check and 14605799ae0SJuan Castillo * the authentication is established by calculating the hash of the key and 14705799ae0SJuan Castillo * comparing it with the hash obtained from the parent. 14805799ae0SJuan Castillo * 14905799ae0SJuan Castillo * If the image has no parent (NULL), it means it has to be authenticated using 15005799ae0SJuan Castillo * the ROTPK stored in the platform. Again, this ROTPK could be the key itself 15105799ae0SJuan Castillo * or a hash of it. 15205799ae0SJuan Castillo * 15305799ae0SJuan Castillo * Return: 0 = success, Otherwise = error 15405799ae0SJuan Castillo */ 15505799ae0SJuan Castillo static int auth_signature(const auth_method_param_sig_t *param, 15605799ae0SJuan Castillo const auth_img_desc_t *img_desc, 15705799ae0SJuan Castillo void *img, unsigned int img_len) 15805799ae0SJuan Castillo { 1591046b418SRobin van der Gracht void *data_ptr, *pk_ptr, *cnv_pk_ptr, *pk_plat_ptr, *sig_ptr, *sig_alg_ptr, *pk_oid; 1601046b418SRobin van der Gracht unsigned int data_len, pk_len, cnv_pk_len, pk_plat_len, sig_len, sig_alg_len; 16105799ae0SJuan Castillo unsigned int flags = 0; 162575c1469SSandrine Bailleux int rc; 16305799ae0SJuan Castillo 16405799ae0SJuan Castillo /* Get the data to be signed from current image */ 16505799ae0SJuan Castillo rc = img_parser_get_auth_param(img_desc->img_type, param->data, 16605799ae0SJuan Castillo img, img_len, &data_ptr, &data_len); 167575c1469SSandrine Bailleux if (rc != 0) { 168575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 169575c1469SSandrine Bailleux __func__, __LINE__, rc); 170575c1469SSandrine Bailleux return rc; 171575c1469SSandrine Bailleux } 17205799ae0SJuan Castillo 17305799ae0SJuan Castillo /* Get the signature from current image */ 17405799ae0SJuan Castillo rc = img_parser_get_auth_param(img_desc->img_type, param->sig, 17505799ae0SJuan Castillo img, img_len, &sig_ptr, &sig_len); 176575c1469SSandrine Bailleux if (rc != 0) { 177575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 178575c1469SSandrine Bailleux __func__, __LINE__, rc); 179575c1469SSandrine Bailleux return rc; 180575c1469SSandrine Bailleux } 18105799ae0SJuan Castillo 18205799ae0SJuan Castillo /* Get the signature algorithm from current image */ 18305799ae0SJuan Castillo rc = img_parser_get_auth_param(img_desc->img_type, param->alg, 18405799ae0SJuan Castillo img, img_len, &sig_alg_ptr, &sig_alg_len); 185575c1469SSandrine Bailleux if (rc != 0) { 186575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 187575c1469SSandrine Bailleux __func__, __LINE__, rc); 188575c1469SSandrine Bailleux return rc; 189575c1469SSandrine Bailleux } 19005799ae0SJuan Castillo 19105799ae0SJuan Castillo /* Get the public key from the parent. If there is no parent (NULL), 19205799ae0SJuan Castillo * the certificate has been signed with the ROTPK, so we have to get 19305799ae0SJuan Castillo * the PK from the platform */ 194f1e693a7SManish V Badarkhe if (img_desc->parent != NULL) { 19505799ae0SJuan Castillo rc = auth_get_param(param->pk, img_desc->parent, 19605799ae0SJuan Castillo &pk_ptr, &pk_len); 197575c1469SSandrine Bailleux if (rc != 0) { 198575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 199575c1469SSandrine Bailleux __func__, __LINE__, rc); 200575c1469SSandrine Bailleux return rc; 201575c1469SSandrine Bailleux } 20205799ae0SJuan Castillo } else { 203f1e693a7SManish V Badarkhe /* 204f1e693a7SManish V Badarkhe * Root certificates are signed with the ROTPK, so we have to 205f1e693a7SManish V Badarkhe * get it from the platform. 206f1e693a7SManish V Badarkhe */ 207f1e693a7SManish V Badarkhe rc = plat_get_rotpk_info(param->pk->cookie, &pk_plat_ptr, 208f1e693a7SManish V Badarkhe &pk_plat_len, &flags); 209575c1469SSandrine Bailleux if (rc != 0) { 210575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 211575c1469SSandrine Bailleux __func__, __LINE__, rc); 212575c1469SSandrine Bailleux return rc; 213575c1469SSandrine Bailleux } 21405799ae0SJuan Castillo 215f1e693a7SManish V Badarkhe assert(is_rotpk_flags_valid(flags)); 216f1e693a7SManish V Badarkhe 217f1e693a7SManish V Badarkhe /* Also retrieve the key from the image. */ 21805799ae0SJuan Castillo rc = img_parser_get_auth_param(img_desc->img_type, 21905799ae0SJuan Castillo param->pk, img, img_len, 22005799ae0SJuan Castillo &pk_ptr, &pk_len); 221575c1469SSandrine Bailleux if (rc != 0) { 222575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 223575c1469SSandrine Bailleux __func__, __LINE__, rc); 224575c1469SSandrine Bailleux return rc; 225575c1469SSandrine Bailleux } 22605799ae0SJuan Castillo 227f1e693a7SManish V Badarkhe /* 228f1e693a7SManish V Badarkhe * Validate the certificate's key against the platform ROTPK. 229f1e693a7SManish V Badarkhe * 230f1e693a7SManish V Badarkhe * Platform may store key in one of the following way - 231f1e693a7SManish V Badarkhe * 1. Hash of ROTPK 232f1e693a7SManish V Badarkhe * 2. Hash if prefixed, suffixed or modified ROTPK 233f1e693a7SManish V Badarkhe * 3. Full ROTPK 234f1e693a7SManish V Badarkhe */ 235f1e693a7SManish V Badarkhe if ((flags & ROTPK_NOT_DEPLOYED) != 0U) { 23604943d33SSoby Mathew NOTICE("ROTPK is not deployed on platform. " 23704943d33SSoby Mathew "Skipping ROTPK verification.\n"); 238f1e693a7SManish V Badarkhe } else if ((flags & ROTPK_IS_HASH) != 0U) { 239f1e693a7SManish V Badarkhe /* 240f1e693a7SManish V Badarkhe * platform may store the hash of a prefixed, 241f1e693a7SManish V Badarkhe * suffixed or modified pk 242f1e693a7SManish V Badarkhe */ 2431046b418SRobin van der Gracht rc = crypto_mod_convert_pk(pk_ptr, pk_len, &cnv_pk_ptr, &cnv_pk_len); 244575c1469SSandrine Bailleux if (rc != 0) { 245575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 246575c1469SSandrine Bailleux __func__, __LINE__, rc); 247575c1469SSandrine Bailleux return rc; 248575c1469SSandrine Bailleux } 24940f9f644SNicolas Toromanoff 250f1e693a7SManish V Badarkhe /* 251f1e693a7SManish V Badarkhe * The hash of the certificate's public key must match 252f1e693a7SManish V Badarkhe * the hash of the ROTPK. 253f1e693a7SManish V Badarkhe */ 2541046b418SRobin van der Gracht rc = crypto_mod_verify_hash(cnv_pk_ptr, cnv_pk_len, 255f1e693a7SManish V Badarkhe pk_plat_ptr, pk_plat_len); 256575c1469SSandrine Bailleux if (rc != 0) { 257575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 258575c1469SSandrine Bailleux __func__, __LINE__, rc); 259575c1469SSandrine Bailleux return rc; 260575c1469SSandrine Bailleux } 26105799ae0SJuan Castillo } else { 262f1e693a7SManish V Badarkhe /* Platform supports full ROTPK */ 263f1e693a7SManish V Badarkhe if ((pk_len != pk_plat_len) || 264f1e693a7SManish V Badarkhe (memcmp(pk_plat_ptr, pk_ptr, pk_len) != 0)) { 265f1e693a7SManish V Badarkhe ERROR("plat and cert ROTPK len mismatch\n"); 266f1e693a7SManish V Badarkhe return -1; 267f1e693a7SManish V Badarkhe } 268f1e693a7SManish V Badarkhe } 2699eaa5a09SManish V Badarkhe 2709eaa5a09SManish V Badarkhe /* 2719eaa5a09SManish V Badarkhe * Set Zero-OID for ROTPK(subject key) as a the certificate 2729eaa5a09SManish V Badarkhe * does not hold Key-OID information for ROTPK. 2739eaa5a09SManish V Badarkhe */ 2749eaa5a09SManish V Badarkhe if (param->pk->cookie != NULL) { 2759eaa5a09SManish V Badarkhe pk_oid = param->pk->cookie; 2769eaa5a09SManish V Badarkhe } else { 2779eaa5a09SManish V Badarkhe pk_oid = ZERO_OID; 2789eaa5a09SManish V Badarkhe } 2799eaa5a09SManish V Badarkhe 2809eaa5a09SManish V Badarkhe /* 2819eaa5a09SManish V Badarkhe * Public key is verified at this stage, notify platform 2829eaa5a09SManish V Badarkhe * to measure and publish it. 2839eaa5a09SManish V Badarkhe */ 2849eaa5a09SManish V Badarkhe rc = plat_mboot_measure_key(pk_oid, pk_ptr, pk_len); 2859eaa5a09SManish V Badarkhe if (rc != 0) { 286575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 287575c1469SSandrine Bailleux __func__, __LINE__, rc); 2889eaa5a09SManish V Badarkhe } 289f1e693a7SManish V Badarkhe } 290f1e693a7SManish V Badarkhe 29105799ae0SJuan Castillo /* Ask the crypto module to verify the signature */ 29205799ae0SJuan Castillo rc = crypto_mod_verify_signature(data_ptr, data_len, 29305799ae0SJuan Castillo sig_ptr, sig_len, 29405799ae0SJuan Castillo sig_alg_ptr, sig_alg_len, 29505799ae0SJuan Castillo pk_ptr, pk_len); 296575c1469SSandrine Bailleux if (rc != 0) { 297575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 298575c1469SSandrine Bailleux __func__, __LINE__, rc); 29905799ae0SJuan Castillo return rc; 30005799ae0SJuan Castillo } 30105799ae0SJuan Castillo 302575c1469SSandrine Bailleux return 0; 303575c1469SSandrine Bailleux } 304575c1469SSandrine Bailleux 30505799ae0SJuan Castillo /* 30648279d52SJuan Castillo * Authenticate by Non-Volatile counter 30748279d52SJuan Castillo * 30848279d52SJuan Castillo * To protect the system against rollback, the platform includes a non-volatile 30948279d52SJuan Castillo * counter whose value can only be increased. All certificates include a counter 31048279d52SJuan Castillo * value that should not be lower than the value stored in the platform. If the 311a2a5a945SManish V Badarkhe * value is larger, the counter in the platform must be updated to the new value 312a2a5a945SManish V Badarkhe * (provided it has been authenticated). 31348279d52SJuan Castillo * 31448279d52SJuan Castillo * Return: 0 = success, Otherwise = error 315a2a5a945SManish V Badarkhe * Returns additionally, 316a2a5a945SManish V Badarkhe * cert_nv_ctr -> NV counter value present in the certificate 317a2a5a945SManish V Badarkhe * need_nv_ctr_upgrade = 0 -> platform NV counter upgrade is not needed 318a2a5a945SManish V Badarkhe * need_nv_ctr_upgrade = 1 -> platform NV counter upgrade is needed 31948279d52SJuan Castillo */ 32048279d52SJuan Castillo static int auth_nvctr(const auth_method_param_nv_ctr_t *param, 32148279d52SJuan Castillo const auth_img_desc_t *img_desc, 322a2a5a945SManish V Badarkhe void *img, unsigned int img_len, 323a2a5a945SManish V Badarkhe unsigned int *cert_nv_ctr, 324a2a5a945SManish V Badarkhe bool *need_nv_ctr_upgrade) 32548279d52SJuan Castillo { 326abb8f936SDemi Marie Obenour unsigned char *p; 32748279d52SJuan Castillo void *data_ptr = NULL; 32848279d52SJuan Castillo unsigned int data_len, len, i; 329a2a5a945SManish V Badarkhe unsigned int plat_nv_ctr; 330575c1469SSandrine Bailleux int rc; 33148279d52SJuan Castillo 33248279d52SJuan Castillo /* Get the counter value from current image. The AM expects the IPM 33348279d52SJuan Castillo * to return the counter value as a DER encoded integer */ 33448279d52SJuan Castillo rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr, 33548279d52SJuan Castillo img, img_len, &data_ptr, &data_len); 336575c1469SSandrine Bailleux if (rc != 0) { 337575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 338575c1469SSandrine Bailleux __func__, __LINE__, rc); 339575c1469SSandrine Bailleux return rc; 340575c1469SSandrine Bailleux } 34148279d52SJuan Castillo 34248279d52SJuan Castillo /* Parse the DER encoded integer */ 34348279d52SJuan Castillo assert(data_ptr); 344abb8f936SDemi Marie Obenour p = (unsigned char *)data_ptr; 345abb8f936SDemi Marie Obenour 346abb8f936SDemi Marie Obenour /* 347abb8f936SDemi Marie Obenour * Integers must be at least 3 bytes: 1 for tag, 1 for length, and 1 348abb8f936SDemi Marie Obenour * for value. The first byte (tag) must be ASN1_INTEGER. 349abb8f936SDemi Marie Obenour */ 350abb8f936SDemi Marie Obenour if ((data_len < 3) || (*p != ASN1_INTEGER)) { 35148279d52SJuan Castillo /* Invalid ASN.1 integer */ 35248279d52SJuan Castillo return 1; 35348279d52SJuan Castillo } 35448279d52SJuan Castillo p++; 35548279d52SJuan Castillo 356abb8f936SDemi Marie Obenour /* 357abb8f936SDemi Marie Obenour * NV-counters are unsigned integers up to 31 bits. Trailing 358abb8f936SDemi Marie Obenour * padding is not allowed. 359abb8f936SDemi Marie Obenour */ 360abb8f936SDemi Marie Obenour len = (unsigned int)*p; 361abb8f936SDemi Marie Obenour if ((len > 4) || (data_len - 2 != len)) { 36248279d52SJuan Castillo return 1; 36348279d52SJuan Castillo } 36448279d52SJuan Castillo p++; 36548279d52SJuan Castillo 36648279d52SJuan Castillo /* Check the number is not negative */ 36748279d52SJuan Castillo if (*p & 0x80) { 36848279d52SJuan Castillo return 1; 36948279d52SJuan Castillo } 37048279d52SJuan Castillo 37148279d52SJuan Castillo /* Convert to unsigned int. This code is for a little-endian CPU */ 372a2a5a945SManish V Badarkhe *cert_nv_ctr = 0; 37348279d52SJuan Castillo for (i = 0; i < len; i++) { 374a2a5a945SManish V Badarkhe *cert_nv_ctr = (*cert_nv_ctr << 8) | *p++; 37548279d52SJuan Castillo } 37648279d52SJuan Castillo 37748279d52SJuan Castillo /* Get the counter from the platform */ 37848279d52SJuan Castillo rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr); 379575c1469SSandrine Bailleux if (rc != 0) { 380575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 381575c1469SSandrine Bailleux __func__, __LINE__, rc); 382575c1469SSandrine Bailleux return rc; 383575c1469SSandrine Bailleux } 38448279d52SJuan Castillo 385a2a5a945SManish V Badarkhe if (*cert_nv_ctr < plat_nv_ctr) { 38648279d52SJuan Castillo /* Invalid NV-counter */ 38748279d52SJuan Castillo return 1; 388a2a5a945SManish V Badarkhe } else if (*cert_nv_ctr > plat_nv_ctr) { 389c0bfc88fSManish V Badarkhe #if PSA_FWU_SUPPORT && IMAGE_BL2 390*56724d09SSughosh Ganu if (fwu_get_active_bank_state() == FWU_BANK_STATE_ACCEPTED) { 391*56724d09SSughosh Ganu *need_nv_ctr_upgrade = true; 392*56724d09SSughosh Ganu } else { 393*56724d09SSughosh Ganu *need_nv_ctr_upgrade = false; 394*56724d09SSughosh Ganu } 395*56724d09SSughosh Ganu #else 396*56724d09SSughosh Ganu *need_nv_ctr_upgrade = true; 397c0bfc88fSManish V Badarkhe #endif /* PSA_FWU_SUPPORT && IMAGE_BL2 */ 39848279d52SJuan Castillo } 39948279d52SJuan Castillo 40048279d52SJuan Castillo return 0; 40148279d52SJuan Castillo } 40248279d52SJuan Castillo 403d35dee23Sdp-arm int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused, 404d35dee23Sdp-arm unsigned int nv_ctr) 405d35dee23Sdp-arm { 406d35dee23Sdp-arm return plat_set_nv_ctr(cookie, nv_ctr); 407d35dee23Sdp-arm } 408d35dee23Sdp-arm 40948279d52SJuan Castillo /* 41005799ae0SJuan Castillo * Return the parent id in the output parameter '*parent_id' 41105799ae0SJuan Castillo * 41205799ae0SJuan Castillo * Return value: 41305799ae0SJuan Castillo * 0 = Image has parent, 1 = Image has no parent or parent is authenticated 41405799ae0SJuan Castillo */ 41505799ae0SJuan Castillo int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id) 41605799ae0SJuan Castillo { 41705799ae0SJuan Castillo const auth_img_desc_t *img_desc = NULL; 41805799ae0SJuan Castillo 41905799ae0SJuan Castillo assert(parent_id != NULL); 42005799ae0SJuan Castillo /* Get the image descriptor */ 421ab1981dbSLouis Mayencourt img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id); 42205799ae0SJuan Castillo 42305799ae0SJuan Castillo /* Check if the image has no parent (ROT) */ 42405799ae0SJuan Castillo if (img_desc->parent == NULL) { 42505799ae0SJuan Castillo *parent_id = 0; 42605799ae0SJuan Castillo return 1; 42705799ae0SJuan Castillo } 42805799ae0SJuan Castillo 42905799ae0SJuan Castillo /* Check if the parent has already been authenticated */ 43005799ae0SJuan Castillo if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) { 43105799ae0SJuan Castillo *parent_id = 0; 43205799ae0SJuan Castillo return 1; 43305799ae0SJuan Castillo } 43405799ae0SJuan Castillo 43505799ae0SJuan Castillo *parent_id = img_desc->parent->img_id; 43605799ae0SJuan Castillo return 0; 43705799ae0SJuan Castillo } 43805799ae0SJuan Castillo 43905799ae0SJuan Castillo /* 44005799ae0SJuan Castillo * Initialize the different modules in the authentication framework 44105799ae0SJuan Castillo */ 44205799ae0SJuan Castillo void auth_mod_init(void) 44305799ae0SJuan Castillo { 44405799ae0SJuan Castillo /* Check we have a valid CoT registered */ 44505799ae0SJuan Castillo assert(cot_desc_ptr != NULL); 44605799ae0SJuan Castillo 44705799ae0SJuan Castillo /* Image parser module */ 44805799ae0SJuan Castillo img_parser_init(); 44905799ae0SJuan Castillo } 45005799ae0SJuan Castillo 45105799ae0SJuan Castillo /* 45205799ae0SJuan Castillo * Authenticate a certificate/image 45305799ae0SJuan Castillo * 45405799ae0SJuan Castillo * Return: 0 = success, Otherwise = error 45505799ae0SJuan Castillo */ 45605799ae0SJuan Castillo int auth_mod_verify_img(unsigned int img_id, 45705799ae0SJuan Castillo void *img_ptr, 45805799ae0SJuan Castillo unsigned int img_len) 45905799ae0SJuan Castillo { 46005799ae0SJuan Castillo const auth_img_desc_t *img_desc = NULL; 4619eaa5a09SManish V Badarkhe const auth_param_type_desc_t *type_desc = NULL; 46205799ae0SJuan Castillo const auth_method_desc_t *auth_method = NULL; 46305799ae0SJuan Castillo void *param_ptr; 46405799ae0SJuan Castillo unsigned int param_len; 46505799ae0SJuan Castillo int rc, i; 466a2a5a945SManish V Badarkhe unsigned int cert_nv_ctr = 0; 467a2a5a945SManish V Badarkhe bool need_nv_ctr_upgrade = false; 468a2a5a945SManish V Badarkhe bool sig_auth_done = false; 469a2a5a945SManish V Badarkhe const auth_method_param_nv_ctr_t *nv_ctr_param = NULL; 47005799ae0SJuan Castillo 47105799ae0SJuan Castillo /* Get the image descriptor from the chain of trust */ 472ab1981dbSLouis Mayencourt img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id); 47305799ae0SJuan Castillo 47405799ae0SJuan Castillo /* Ask the parser to check the image integrity */ 47505799ae0SJuan Castillo rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len); 476575c1469SSandrine Bailleux if (rc != 0) { 477575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 478575c1469SSandrine Bailleux __func__, __LINE__, rc); 479575c1469SSandrine Bailleux return rc; 480575c1469SSandrine Bailleux } 48105799ae0SJuan Castillo 48205799ae0SJuan Castillo /* Authenticate the image using the methods indicated in the image 48305799ae0SJuan Castillo * descriptor. */ 48430070427SJoel Hutton if (img_desc->img_auth_methods == NULL) 48530070427SJoel Hutton return 1; 48605799ae0SJuan Castillo for (i = 0 ; i < AUTH_METHOD_NUM ; i++) { 48705799ae0SJuan Castillo auth_method = &img_desc->img_auth_methods[i]; 48805799ae0SJuan Castillo switch (auth_method->type) { 48905799ae0SJuan Castillo case AUTH_METHOD_NONE: 49005799ae0SJuan Castillo rc = 0; 49105799ae0SJuan Castillo break; 49205799ae0SJuan Castillo case AUTH_METHOD_HASH: 49305799ae0SJuan Castillo rc = auth_hash(&auth_method->param.hash, 49405799ae0SJuan Castillo img_desc, img_ptr, img_len); 49505799ae0SJuan Castillo break; 49605799ae0SJuan Castillo case AUTH_METHOD_SIG: 49705799ae0SJuan Castillo rc = auth_signature(&auth_method->param.sig, 49805799ae0SJuan Castillo img_desc, img_ptr, img_len); 499a2a5a945SManish V Badarkhe sig_auth_done = true; 50005799ae0SJuan Castillo break; 50148279d52SJuan Castillo case AUTH_METHOD_NV_CTR: 502a2a5a945SManish V Badarkhe nv_ctr_param = &auth_method->param.nv_ctr; 503a2a5a945SManish V Badarkhe rc = auth_nvctr(nv_ctr_param, 504a2a5a945SManish V Badarkhe img_desc, img_ptr, img_len, 505a2a5a945SManish V Badarkhe &cert_nv_ctr, &need_nv_ctr_upgrade); 50648279d52SJuan Castillo break; 50705799ae0SJuan Castillo default: 50805799ae0SJuan Castillo /* Unknown authentication method */ 50905799ae0SJuan Castillo rc = 1; 51005799ae0SJuan Castillo break; 51105799ae0SJuan Castillo } 512575c1469SSandrine Bailleux if (rc != 0) { 513575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 514575c1469SSandrine Bailleux __func__, __LINE__, rc); 515575c1469SSandrine Bailleux return rc; 516575c1469SSandrine Bailleux } 51705799ae0SJuan Castillo } 51805799ae0SJuan Castillo 519a2a5a945SManish V Badarkhe /* 520a2a5a945SManish V Badarkhe * Do platform NV counter upgrade only if the certificate gets 521a2a5a945SManish V Badarkhe * authenticated, and platform NV-counter upgrade is needed. 522a2a5a945SManish V Badarkhe */ 523a2a5a945SManish V Badarkhe if (need_nv_ctr_upgrade && sig_auth_done) { 524a2a5a945SManish V Badarkhe rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie, 525a2a5a945SManish V Badarkhe img_desc, cert_nv_ctr); 526575c1469SSandrine Bailleux if (rc != 0) { 527575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 528575c1469SSandrine Bailleux __func__, __LINE__, rc); 529575c1469SSandrine Bailleux return rc; 530575c1469SSandrine Bailleux } 531a2a5a945SManish V Badarkhe } 532a2a5a945SManish V Badarkhe 53305799ae0SJuan Castillo /* Extract the parameters indicated in the image descriptor to 53405799ae0SJuan Castillo * authenticate the children images. */ 53530070427SJoel Hutton if (img_desc->authenticated_data != NULL) { 53605799ae0SJuan Castillo for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) { 53705799ae0SJuan Castillo if (img_desc->authenticated_data[i].type_desc == NULL) { 53805799ae0SJuan Castillo continue; 53905799ae0SJuan Castillo } 54005799ae0SJuan Castillo 54105799ae0SJuan Castillo /* Get the parameter from the image parser module */ 54205799ae0SJuan Castillo rc = img_parser_get_auth_param(img_desc->img_type, 54305799ae0SJuan Castillo img_desc->authenticated_data[i].type_desc, 54405799ae0SJuan Castillo img_ptr, img_len, ¶m_ptr, ¶m_len); 545575c1469SSandrine Bailleux if (rc != 0) { 546575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 547575c1469SSandrine Bailleux __func__, __LINE__, rc); 548575c1469SSandrine Bailleux return rc; 549575c1469SSandrine Bailleux } 55005799ae0SJuan Castillo 55105799ae0SJuan Castillo /* Check parameter size */ 55205799ae0SJuan Castillo if (param_len > img_desc->authenticated_data[i].data.len) { 55305799ae0SJuan Castillo return 1; 55405799ae0SJuan Castillo } 55505799ae0SJuan Castillo 55605799ae0SJuan Castillo /* Copy the parameter for later use */ 55705799ae0SJuan Castillo memcpy((void *)img_desc->authenticated_data[i].data.ptr, 55805799ae0SJuan Castillo (void *)param_ptr, param_len); 5599eaa5a09SManish V Badarkhe 5609eaa5a09SManish V Badarkhe /* 5619eaa5a09SManish V Badarkhe * If this is a public key then measure and publicise 5629eaa5a09SManish V Badarkhe * it. 5639eaa5a09SManish V Badarkhe */ 5649eaa5a09SManish V Badarkhe type_desc = img_desc->authenticated_data[i].type_desc; 5659eaa5a09SManish V Badarkhe if (type_desc->type == AUTH_PARAM_PUB_KEY) { 5669eaa5a09SManish V Badarkhe rc = plat_mboot_measure_key(type_desc->cookie, 5679eaa5a09SManish V Badarkhe param_ptr, 5689eaa5a09SManish V Badarkhe param_len); 5699eaa5a09SManish V Badarkhe if (rc != 0) { 570575c1469SSandrine Bailleux VERBOSE("[TBB] %s():%d failed with error code %d.\n", 571575c1469SSandrine Bailleux __func__, __LINE__, rc); 5729eaa5a09SManish V Badarkhe } 5739eaa5a09SManish V Badarkhe } 57405799ae0SJuan Castillo } 57530070427SJoel Hutton } 57605799ae0SJuan Castillo 57705799ae0SJuan Castillo /* Mark image as authenticated */ 57805799ae0SJuan Castillo auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED; 57905799ae0SJuan Castillo 58005799ae0SJuan Castillo return 0; 58105799ae0SJuan Castillo } 582