Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 1 | /* |
Govindraj Raja | a2872f9 | 2023-02-03 11:08:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2023, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 10 | #include <mbedtls/oid.h> |
| 11 | #include <mbedtls/x509.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <arch_helpers.h> |
| 14 | #include <common/debug.h> |
Gilad Ben-Yossef | 6fbe0fc | 2019-05-14 14:47:36 +0300 | [diff] [blame] | 15 | #include <drivers/arm/cryptocell/712/crypto_driver.h> |
| 16 | #include <drivers/arm/cryptocell/712/rsa.h> |
| 17 | #include <drivers/arm/cryptocell/712/sbrom_bsv_api.h> |
| 18 | #include <drivers/arm/cryptocell/712/secureboot_base_func.h> |
| 19 | #include <drivers/arm/cryptocell/712/secureboot_gen_defs.h> |
| 20 | #include <drivers/arm/cryptocell/712/util.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 21 | #include <drivers/auth/crypto_mod.h> |
| 22 | #include <drivers/auth/mbedtls/mbedtls_common.h> |
| 23 | #include <lib/utils.h> |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 24 | |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 25 | #include <platform_def.h> |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 26 | |
Gilad Ben-Yossef | 6fbe0fc | 2019-05-14 14:47:36 +0300 | [diff] [blame] | 27 | #define LIB_NAME "CryptoCell 712 SBROM" |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 28 | #define RSA_SALT_LEN 32 |
| 29 | #define RSA_EXPONENT 65537 |
| 30 | |
| 31 | /* |
| 32 | * AlgorithmIdentifier ::= SEQUENCE { |
| 33 | * algorithm OBJECT IDENTIFIER, |
| 34 | * parameters ANY DEFINED BY algorithm OPTIONAL |
| 35 | * } |
| 36 | * |
| 37 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 38 | * algorithm AlgorithmIdentifier, |
| 39 | * subjectPublicKey BIT STRING |
| 40 | * } |
| 41 | * |
| 42 | * DigestInfo ::= SEQUENCE { |
| 43 | * digestAlgorithm AlgorithmIdentifier, |
| 44 | * digest OCTET STRING |
| 45 | * } |
| 46 | * |
| 47 | * RSASSA-PSS-params ::= SEQUENCE { |
| 48 | * hashAlgorithm [0] HashAlgorithm, |
| 49 | * maskGenAlgorithm [1] MaskGenAlgorithm, |
| 50 | * saltLength [2] INTEGER, |
| 51 | * trailerField [3] TrailerField DEFAULT trailerFieldBC |
| 52 | * } |
| 53 | */ |
| 54 | |
| 55 | /* |
| 56 | * Initialize the library and export the descriptor |
| 57 | */ |
| 58 | static void init(void) |
| 59 | { |
| 60 | CCError_t ret; |
Soby Mathew | d349023 | 2017-06-05 15:55:59 +0100 | [diff] [blame] | 61 | uint32_t lcs; |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 62 | |
| 63 | /* Initialize CC SBROM */ |
| 64 | ret = CC_BsvSbromInit((uintptr_t)PLAT_CRYPTOCELL_BASE); |
| 65 | if (ret != CC_OK) { |
| 66 | ERROR("CryptoCell CC_BsvSbromInit() error %x\n", ret); |
| 67 | panic(); |
| 68 | } |
Soby Mathew | d349023 | 2017-06-05 15:55:59 +0100 | [diff] [blame] | 69 | |
| 70 | /* Initialize lifecycle state */ |
| 71 | ret = CC_BsvLcsGetAndInit((uintptr_t)PLAT_CRYPTOCELL_BASE, &lcs); |
| 72 | if (ret != CC_OK) { |
| 73 | ERROR("CryptoCell CC_BsvLcsGetAndInit() error %x\n", ret); |
| 74 | panic(); |
| 75 | } |
| 76 | |
| 77 | /* If the lifecyclestate is `SD`, then stop further execution */ |
| 78 | if (lcs == CC_BSV_SECURITY_DISABLED_LCS) { |
| 79 | ERROR("CryptoCell LCS is security-disabled\n"); |
| 80 | panic(); |
| 81 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Verify a signature. |
| 86 | * |
| 87 | * Parameters are passed using the DER encoding format following the ASN.1 |
| 88 | * structures detailed above. |
| 89 | */ |
| 90 | static int verify_signature(void *data_ptr, unsigned int data_len, |
| 91 | void *sig_ptr, unsigned int sig_len, |
| 92 | void *sig_alg, unsigned int sig_alg_len, |
| 93 | void *pk_ptr, unsigned int pk_len) |
| 94 | { |
| 95 | CCError_t error; |
| 96 | CCSbNParams_t pk; |
| 97 | CCSbSignature_t signature; |
Govindraj Raja | a2872f9 | 2023-02-03 11:08:00 +0000 | [diff] [blame] | 98 | int rc, exp, expected_salt_len; |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 99 | mbedtls_asn1_buf sig_oid, alg_oid, params; |
Govindraj Raja | a2872f9 | 2023-02-03 11:08:00 +0000 | [diff] [blame] | 100 | mbedtls_md_type_t md_alg, mgf1_hash_id; |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 101 | mbedtls_pk_type_t pk_alg; |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 102 | size_t len; |
| 103 | uint8_t *p, *end; |
| 104 | /* Temp buf to store the public key modulo (N) in LE format */ |
| 105 | uint32_t RevN[SB_RSA_MOD_SIZE_IN_WORDS]; |
| 106 | |
| 107 | /* Verify the signature algorithm */ |
| 108 | /* Get pointers to signature OID and parameters */ |
| 109 | p = sig_alg; |
| 110 | end = p + sig_alg_len; |
| 111 | rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, ¶ms); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 112 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 113 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 114 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 115 | |
| 116 | /* Get the actual signature algorithm (MD + PK) */ |
| 117 | rc = mbedtls_oid_get_sig_alg(&sig_oid, &md_alg, &pk_alg); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 118 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 119 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 120 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 121 | |
| 122 | /* The CryptoCell only supports RSASSA-PSS signature */ |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 123 | if ((pk_alg != MBEDTLS_PK_RSASSA_PSS) || (md_alg != MBEDTLS_MD_NONE)) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 124 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 125 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 126 | |
| 127 | /* Verify the RSASSA-PSS params */ |
| 128 | /* The trailer field is verified to be 0xBC internally by this API */ |
| 129 | rc = mbedtls_x509_get_rsassa_pss_params(¶ms, &md_alg, |
Govindraj Raja | a2872f9 | 2023-02-03 11:08:00 +0000 | [diff] [blame] | 130 | &mgf1_hash_id, |
| 131 | &expected_salt_len); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 132 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 133 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 134 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 135 | |
| 136 | /* The CryptoCell only supports SHA256 as hash algorithm */ |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 137 | if ((md_alg != MBEDTLS_MD_SHA256) || (mgf1_hash_id != MBEDTLS_MD_SHA256)) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 138 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 139 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 140 | |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 141 | if (expected_salt_len != RSA_SALT_LEN) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 142 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 143 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 144 | |
| 145 | /* Parse the public key */ |
| 146 | p = pk_ptr; |
| 147 | end = p + pk_len; |
| 148 | rc = mbedtls_asn1_get_tag(&p, end, &len, |
| 149 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 150 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 151 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 152 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 153 | |
| 154 | end = p + len; |
| 155 | rc = mbedtls_asn1_get_alg_null(&p, end, &alg_oid); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 156 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 157 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 158 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 159 | |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 160 | if (mbedtls_oid_get_pk_alg(&alg_oid, &pk_alg) != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 161 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 162 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 163 | |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 164 | if (pk_alg != MBEDTLS_PK_RSA) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 165 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 166 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 167 | |
| 168 | rc = mbedtls_asn1_get_bitstring_null(&p, end, &len); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 169 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 170 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 171 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 172 | |
| 173 | rc = mbedtls_asn1_get_tag(&p, end, &len, |
| 174 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 175 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 176 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 177 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 178 | |
| 179 | rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 180 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 181 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 182 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 183 | |
| 184 | if (*p == 0) { |
| 185 | p++; len--; |
| 186 | } |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 187 | |
| 188 | if (len != RSA_MOD_SIZE_IN_BYTES || ((p + len) > end)) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 189 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 190 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * The CCSbVerifySignature() API expects N and Np in BE format and |
| 194 | * the signature in LE format. Copy N from certificate. |
| 195 | */ |
| 196 | memcpy(pk.N, p, RSA_MOD_SIZE_IN_BYTES); |
| 197 | |
| 198 | /* Verify the RSA exponent */ |
| 199 | p += len; |
| 200 | rc = mbedtls_asn1_get_int(&p, end, &exp); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 201 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 202 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 203 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 204 | |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 205 | if (exp != RSA_EXPONENT) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 206 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 207 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * Calculate the Np (Barrett n' value). The RSA_CalcNp() API expects |
| 211 | * N in LE format. Hence reverse N into a temporary buffer `RevN`. |
| 212 | */ |
| 213 | UTIL_ReverseMemCopy((uint8_t *)RevN, (uint8_t *)pk.N, sizeof(RevN)); |
| 214 | |
| 215 | RSA_CalcNp((uintptr_t)PLAT_CRYPTOCELL_BASE, RevN, pk.Np); |
| 216 | |
| 217 | /* Np is in LE format. Reverse it to BE */ |
| 218 | UTIL_ReverseBuff((uint8_t *)pk.Np, sizeof(pk.Np)); |
| 219 | |
| 220 | /* Get the signature (bitstring) */ |
| 221 | p = sig_ptr; |
| 222 | end = p + sig_len; |
| 223 | rc = mbedtls_asn1_get_bitstring_null(&p, end, &len); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 224 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 225 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 226 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 227 | |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 228 | if (len != RSA_MOD_SIZE_IN_BYTES || ((p + len) > end)) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 229 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 230 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * The signature is BE format. Convert it to LE before calling |
| 234 | * CCSbVerifySignature(). |
| 235 | */ |
| 236 | UTIL_ReverseMemCopy((uint8_t *)signature.sig, p, RSA_MOD_SIZE_IN_BYTES); |
| 237 | |
| 238 | /* |
| 239 | * CryptoCell utilises DMA internally to transfer data. Flush the data |
| 240 | * from caches. |
| 241 | */ |
| 242 | flush_dcache_range((uintptr_t)data_ptr, data_len); |
| 243 | |
| 244 | /* Verify the signature */ |
| 245 | error = CCSbVerifySignature((uintptr_t)PLAT_CRYPTOCELL_BASE, |
| 246 | (uint32_t *)data_ptr, &pk, &signature, |
Gilad Ben-Yossef | a6e5342 | 2019-09-15 13:29:29 +0300 | [diff] [blame] | 247 | data_len, RSA_PSS); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 248 | if (error != CC_OK) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 249 | return CRYPTO_ERR_SIGNATURE; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 250 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 251 | |
| 252 | /* Signature verification success */ |
| 253 | return CRYPTO_SUCCESS; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Match a hash |
| 258 | * |
| 259 | * Digest info is passed in DER format following the ASN.1 structure detailed |
| 260 | * above. |
| 261 | */ |
| 262 | static int verify_hash(void *data_ptr, unsigned int data_len, |
| 263 | void *digest_info_ptr, unsigned int digest_info_len) |
| 264 | { |
| 265 | mbedtls_asn1_buf hash_oid, params; |
| 266 | mbedtls_md_type_t md_alg; |
| 267 | uint8_t *p, *end, *hash; |
| 268 | CCHashResult_t pubKeyHash; |
| 269 | size_t len; |
| 270 | int rc; |
| 271 | CCError_t error; |
| 272 | |
| 273 | /* Digest info should be an MBEDTLS_ASN1_SEQUENCE */ |
| 274 | p = digest_info_ptr; |
| 275 | end = p + digest_info_len; |
| 276 | rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | |
| 277 | MBEDTLS_ASN1_SEQUENCE); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 278 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 279 | return CRYPTO_ERR_HASH; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 280 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 281 | |
| 282 | /* Get the hash algorithm */ |
| 283 | rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, ¶ms); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 284 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 285 | return CRYPTO_ERR_HASH; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 286 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 287 | |
| 288 | rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 289 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 290 | return CRYPTO_ERR_HASH; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 293 | /* Verify that hash algorithm is SHA256 */ |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 294 | if (md_alg != MBEDTLS_MD_SHA256) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 295 | return CRYPTO_ERR_HASH; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 296 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 297 | |
| 298 | /* Hash should be octet string type */ |
| 299 | rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 300 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 301 | return CRYPTO_ERR_HASH; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 302 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 303 | |
| 304 | /* Length of hash must match the algorithm's size */ |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 305 | if (len != HASH_RESULT_SIZE_IN_BYTES) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 306 | return CRYPTO_ERR_HASH; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 307 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * CryptoCell utilises DMA internally to transfer data. Flush the data |
| 311 | * from caches. |
| 312 | */ |
| 313 | flush_dcache_range((uintptr_t)data_ptr, data_len); |
| 314 | |
| 315 | hash = p; |
| 316 | error = SBROM_CryptoHash((uintptr_t)PLAT_CRYPTOCELL_BASE, |
| 317 | (uintptr_t)data_ptr, data_len, pubKeyHash); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 318 | if (error != CC_OK) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 319 | return CRYPTO_ERR_HASH; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 320 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 321 | |
| 322 | rc = memcmp(pubKeyHash, hash, HASH_RESULT_SIZE_IN_BYTES); |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 323 | if (rc != 0) { |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 324 | return CRYPTO_ERR_HASH; |
Govindraj Raja | fd49e8b | 2023-02-12 20:19:31 +0000 | [diff] [blame] | 325 | } |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 326 | |
| 327 | return CRYPTO_SUCCESS; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Register crypto library descriptor |
| 332 | */ |
Yann Gautier | 2b6673d | 2023-03-15 11:31:25 +0100 | [diff] [blame] | 333 | REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL, NULL, NULL); |
Soby Mathew | 5d70800 | 2017-05-10 11:49:58 +0100 | [diff] [blame] | 334 | |