blob: bba13f8bb39f1ae136245732c78d3b5f997525d7 [file] [log] [blame]
Soby Mathew5d708002017-05-10 11:49:58 +01001/*
Govindraj Rajaa2872f92023-02-03 11:08:00 +00002 * Copyright (c) 2017-2023, ARM Limited and Contributors. All rights reserved.
Soby Mathew5d708002017-05-10 11:49:58 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Soby Mathew5d708002017-05-10 11:49:58 +01007#include <stddef.h>
8#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
Govindraj Rajafd49e8b2023-02-12 20:19:31 +000010#include <mbedtls/oid.h>
11#include <mbedtls/x509.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
13#include <arch_helpers.h>
14#include <common/debug.h>
Gilad Ben-Yossef6fbe0fc2019-05-14 14:47:36 +030015#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 Diaze0f90632018-12-14 00:18:21 +000021#include <drivers/auth/crypto_mod.h>
22#include <drivers/auth/mbedtls/mbedtls_common.h>
23#include <lib/utils.h>
Soby Mathew5d708002017-05-10 11:49:58 +010024
Govindraj Rajafd49e8b2023-02-12 20:19:31 +000025#include <platform_def.h>
Soby Mathew5d708002017-05-10 11:49:58 +010026
Gilad Ben-Yossef6fbe0fc2019-05-14 14:47:36 +030027#define LIB_NAME "CryptoCell 712 SBROM"
Soby Mathew5d708002017-05-10 11:49:58 +010028#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 */
58static void init(void)
59{
60 CCError_t ret;
Soby Mathewd3490232017-06-05 15:55:59 +010061 uint32_t lcs;
Soby Mathew5d708002017-05-10 11:49:58 +010062
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 Mathewd3490232017-06-05 15:55:59 +010069
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 Mathew5d708002017-05-10 11:49:58 +010082}
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 */
90static 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 Rajaa2872f92023-02-03 11:08:00 +000098 int rc, exp, expected_salt_len;
Soby Mathew5d708002017-05-10 11:49:58 +010099 mbedtls_asn1_buf sig_oid, alg_oid, params;
Govindraj Rajaa2872f92023-02-03 11:08:00 +0000100 mbedtls_md_type_t md_alg, mgf1_hash_id;
Soby Mathew5d708002017-05-10 11:49:58 +0100101 mbedtls_pk_type_t pk_alg;
Soby Mathew5d708002017-05-10 11:49:58 +0100102 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, &params);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000112 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100113 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000114 }
Soby Mathew5d708002017-05-10 11:49:58 +0100115
116 /* Get the actual signature algorithm (MD + PK) */
117 rc = mbedtls_oid_get_sig_alg(&sig_oid, &md_alg, &pk_alg);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000118 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100119 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000120 }
Soby Mathew5d708002017-05-10 11:49:58 +0100121
122 /* The CryptoCell only supports RSASSA-PSS signature */
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000123 if ((pk_alg != MBEDTLS_PK_RSASSA_PSS) || (md_alg != MBEDTLS_MD_NONE)) {
Soby Mathew5d708002017-05-10 11:49:58 +0100124 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000125 }
Soby Mathew5d708002017-05-10 11:49:58 +0100126
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(&params, &md_alg,
Govindraj Rajaa2872f92023-02-03 11:08:00 +0000130 &mgf1_hash_id,
131 &expected_salt_len);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000132 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100133 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000134 }
Soby Mathew5d708002017-05-10 11:49:58 +0100135
136 /* The CryptoCell only supports SHA256 as hash algorithm */
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000137 if ((md_alg != MBEDTLS_MD_SHA256) || (mgf1_hash_id != MBEDTLS_MD_SHA256)) {
Soby Mathew5d708002017-05-10 11:49:58 +0100138 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000139 }
Soby Mathew5d708002017-05-10 11:49:58 +0100140
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000141 if (expected_salt_len != RSA_SALT_LEN) {
Soby Mathew5d708002017-05-10 11:49:58 +0100142 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000143 }
Soby Mathew5d708002017-05-10 11:49:58 +0100144
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 Rajafd49e8b2023-02-12 20:19:31 +0000150 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100151 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000152 }
Soby Mathew5d708002017-05-10 11:49:58 +0100153
154 end = p + len;
155 rc = mbedtls_asn1_get_alg_null(&p, end, &alg_oid);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000156 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100157 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000158 }
Soby Mathew5d708002017-05-10 11:49:58 +0100159
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000160 if (mbedtls_oid_get_pk_alg(&alg_oid, &pk_alg) != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100161 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000162 }
Soby Mathew5d708002017-05-10 11:49:58 +0100163
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000164 if (pk_alg != MBEDTLS_PK_RSA) {
Soby Mathew5d708002017-05-10 11:49:58 +0100165 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000166 }
Soby Mathew5d708002017-05-10 11:49:58 +0100167
168 rc = mbedtls_asn1_get_bitstring_null(&p, end, &len);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000169 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100170 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000171 }
Soby Mathew5d708002017-05-10 11:49:58 +0100172
173 rc = mbedtls_asn1_get_tag(&p, end, &len,
174 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000175 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100176 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000177 }
Soby Mathew5d708002017-05-10 11:49:58 +0100178
179 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000180 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100181 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000182 }
Soby Mathew5d708002017-05-10 11:49:58 +0100183
184 if (*p == 0) {
185 p++; len--;
186 }
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000187
188 if (len != RSA_MOD_SIZE_IN_BYTES || ((p + len) > end)) {
Soby Mathew5d708002017-05-10 11:49:58 +0100189 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000190 }
Soby Mathew5d708002017-05-10 11:49:58 +0100191
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 Rajafd49e8b2023-02-12 20:19:31 +0000201 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100202 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000203 }
Soby Mathew5d708002017-05-10 11:49:58 +0100204
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000205 if (exp != RSA_EXPONENT) {
Soby Mathew5d708002017-05-10 11:49:58 +0100206 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000207 }
Soby Mathew5d708002017-05-10 11:49:58 +0100208
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 Rajafd49e8b2023-02-12 20:19:31 +0000224 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100225 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000226 }
Soby Mathew5d708002017-05-10 11:49:58 +0100227
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000228 if (len != RSA_MOD_SIZE_IN_BYTES || ((p + len) > end)) {
Soby Mathew5d708002017-05-10 11:49:58 +0100229 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000230 }
Soby Mathew5d708002017-05-10 11:49:58 +0100231
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-Yossefa6e53422019-09-15 13:29:29 +0300247 data_len, RSA_PSS);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000248 if (error != CC_OK) {
Soby Mathew5d708002017-05-10 11:49:58 +0100249 return CRYPTO_ERR_SIGNATURE;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000250 }
Soby Mathew5d708002017-05-10 11:49:58 +0100251
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 */
262static 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 Rajafd49e8b2023-02-12 20:19:31 +0000278 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100279 return CRYPTO_ERR_HASH;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000280 }
Soby Mathew5d708002017-05-10 11:49:58 +0100281
282 /* Get the hash algorithm */
283 rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000284 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100285 return CRYPTO_ERR_HASH;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000286 }
Soby Mathew5d708002017-05-10 11:49:58 +0100287
288 rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000289 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100290 return CRYPTO_ERR_HASH;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000291 }
292
Soby Mathew5d708002017-05-10 11:49:58 +0100293 /* Verify that hash algorithm is SHA256 */
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000294 if (md_alg != MBEDTLS_MD_SHA256) {
Soby Mathew5d708002017-05-10 11:49:58 +0100295 return CRYPTO_ERR_HASH;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000296 }
Soby Mathew5d708002017-05-10 11:49:58 +0100297
298 /* Hash should be octet string type */
299 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000300 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100301 return CRYPTO_ERR_HASH;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000302 }
Soby Mathew5d708002017-05-10 11:49:58 +0100303
304 /* Length of hash must match the algorithm's size */
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000305 if (len != HASH_RESULT_SIZE_IN_BYTES) {
Soby Mathew5d708002017-05-10 11:49:58 +0100306 return CRYPTO_ERR_HASH;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000307 }
Soby Mathew5d708002017-05-10 11:49:58 +0100308
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 Rajafd49e8b2023-02-12 20:19:31 +0000318 if (error != CC_OK) {
Soby Mathew5d708002017-05-10 11:49:58 +0100319 return CRYPTO_ERR_HASH;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000320 }
Soby Mathew5d708002017-05-10 11:49:58 +0100321
322 rc = memcmp(pubKeyHash, hash, HASH_RESULT_SIZE_IN_BYTES);
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000323 if (rc != 0) {
Soby Mathew5d708002017-05-10 11:49:58 +0100324 return CRYPTO_ERR_HASH;
Govindraj Rajafd49e8b2023-02-12 20:19:31 +0000325 }
Soby Mathew5d708002017-05-10 11:49:58 +0100326
327 return CRYPTO_SUCCESS;
328}
329
330/*
331 * Register crypto library descriptor
332 */
Yann Gautier2b6673d2023-03-15 11:31:25 +0100333REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL, NULL, NULL);
Soby Mathew5d708002017-05-10 11:49:58 +0100334