blob: 142f364778031d8e67923ecfedee78dd8e9ad1fe [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
10#include <platform_def.h>
11
12#include <arch_helpers.h>
13#include <common/debug.h>
Gilad Ben-Yossef6fbe0fc2019-05-14 14:47:36 +030014#include <drivers/arm/cryptocell/712/crypto_driver.h>
15#include <drivers/arm/cryptocell/712/rsa.h>
16#include <drivers/arm/cryptocell/712/sbrom_bsv_api.h>
17#include <drivers/arm/cryptocell/712/secureboot_base_func.h>
18#include <drivers/arm/cryptocell/712/secureboot_gen_defs.h>
19#include <drivers/arm/cryptocell/712/util.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <drivers/auth/crypto_mod.h>
21#include <drivers/auth/mbedtls/mbedtls_common.h>
22#include <lib/utils.h>
Soby Mathew5d708002017-05-10 11:49:58 +010023
24#include <mbedtls/oid.h>
Madhukar Pappireddy57eaae82020-03-05 18:18:40 -060025#include <mbedtls/x509.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);
112 if (rc != 0)
113 return CRYPTO_ERR_SIGNATURE;
114
115 /* Get the actual signature algorithm (MD + PK) */
116 rc = mbedtls_oid_get_sig_alg(&sig_oid, &md_alg, &pk_alg);
117 if (rc != 0)
118 return CRYPTO_ERR_SIGNATURE;
119
120 /* The CryptoCell only supports RSASSA-PSS signature */
Govindraj Rajaa2872f92023-02-03 11:08:00 +0000121 if ((pk_alg != MBEDTLS_PK_RSASSA_PSS) || (md_alg != MBEDTLS_MD_NONE))
Soby Mathew5d708002017-05-10 11:49:58 +0100122 return CRYPTO_ERR_SIGNATURE;
123
124 /* Verify the RSASSA-PSS params */
125 /* The trailer field is verified to be 0xBC internally by this API */
126 rc = mbedtls_x509_get_rsassa_pss_params(&params, &md_alg,
Govindraj Rajaa2872f92023-02-03 11:08:00 +0000127 &mgf1_hash_id,
128 &expected_salt_len);
Soby Mathew5d708002017-05-10 11:49:58 +0100129 if (rc != 0)
130 return CRYPTO_ERR_SIGNATURE;
131
132 /* The CryptoCell only supports SHA256 as hash algorithm */
Govindraj Rajaa2872f92023-02-03 11:08:00 +0000133 if ((md_alg != MBEDTLS_MD_SHA256) || (mgf1_hash_id != MBEDTLS_MD_SHA256))
Soby Mathew5d708002017-05-10 11:49:58 +0100134 return CRYPTO_ERR_SIGNATURE;
135
Govindraj Rajaa2872f92023-02-03 11:08:00 +0000136 if (expected_salt_len != RSA_SALT_LEN)
Soby Mathew5d708002017-05-10 11:49:58 +0100137 return CRYPTO_ERR_SIGNATURE;
138
139 /* Parse the public key */
140 p = pk_ptr;
141 end = p + pk_len;
142 rc = mbedtls_asn1_get_tag(&p, end, &len,
143 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
144 if (rc != 0)
145 return CRYPTO_ERR_SIGNATURE;
146
147 end = p + len;
148 rc = mbedtls_asn1_get_alg_null(&p, end, &alg_oid);
149 if (rc != 0)
150 return CRYPTO_ERR_SIGNATURE;
151
152 if (mbedtls_oid_get_pk_alg(&alg_oid, &pk_alg) != 0)
153 return CRYPTO_ERR_SIGNATURE;
154
155 if (pk_alg != MBEDTLS_PK_RSA)
156 return CRYPTO_ERR_SIGNATURE;
157
158 rc = mbedtls_asn1_get_bitstring_null(&p, end, &len);
159 if (rc != 0)
160 return CRYPTO_ERR_SIGNATURE;
161
162 rc = mbedtls_asn1_get_tag(&p, end, &len,
163 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
164 if (rc != 0)
165 return CRYPTO_ERR_SIGNATURE;
166
167 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER);
168 if (rc != 0)
169 return CRYPTO_ERR_SIGNATURE;
170
171 if (*p == 0) {
172 p++; len--;
173 }
174 if (len != RSA_MOD_SIZE_IN_BYTES || ((p + len) > end))
175 return CRYPTO_ERR_SIGNATURE;
176
177 /*
178 * The CCSbVerifySignature() API expects N and Np in BE format and
179 * the signature in LE format. Copy N from certificate.
180 */
181 memcpy(pk.N, p, RSA_MOD_SIZE_IN_BYTES);
182
183 /* Verify the RSA exponent */
184 p += len;
185 rc = mbedtls_asn1_get_int(&p, end, &exp);
186 if (rc != 0)
187 return CRYPTO_ERR_SIGNATURE;
188
189 if (exp != RSA_EXPONENT)
190 return CRYPTO_ERR_SIGNATURE;
191
192 /*
193 * Calculate the Np (Barrett n' value). The RSA_CalcNp() API expects
194 * N in LE format. Hence reverse N into a temporary buffer `RevN`.
195 */
196 UTIL_ReverseMemCopy((uint8_t *)RevN, (uint8_t *)pk.N, sizeof(RevN));
197
198 RSA_CalcNp((uintptr_t)PLAT_CRYPTOCELL_BASE, RevN, pk.Np);
199
200 /* Np is in LE format. Reverse it to BE */
201 UTIL_ReverseBuff((uint8_t *)pk.Np, sizeof(pk.Np));
202
203 /* Get the signature (bitstring) */
204 p = sig_ptr;
205 end = p + sig_len;
206 rc = mbedtls_asn1_get_bitstring_null(&p, end, &len);
207 if (rc != 0)
208 return CRYPTO_ERR_SIGNATURE;
209
210 if (len != RSA_MOD_SIZE_IN_BYTES || ((p + len) > end))
211 return CRYPTO_ERR_SIGNATURE;
212
213 /*
214 * The signature is BE format. Convert it to LE before calling
215 * CCSbVerifySignature().
216 */
217 UTIL_ReverseMemCopy((uint8_t *)signature.sig, p, RSA_MOD_SIZE_IN_BYTES);
218
219 /*
220 * CryptoCell utilises DMA internally to transfer data. Flush the data
221 * from caches.
222 */
223 flush_dcache_range((uintptr_t)data_ptr, data_len);
224
225 /* Verify the signature */
226 error = CCSbVerifySignature((uintptr_t)PLAT_CRYPTOCELL_BASE,
227 (uint32_t *)data_ptr, &pk, &signature,
Gilad Ben-Yossefa6e53422019-09-15 13:29:29 +0300228 data_len, RSA_PSS);
Soby Mathew5d708002017-05-10 11:49:58 +0100229 if (error != CC_OK)
230 return CRYPTO_ERR_SIGNATURE;
231
232 /* Signature verification success */
233 return CRYPTO_SUCCESS;
234}
235
236/*
237 * Match a hash
238 *
239 * Digest info is passed in DER format following the ASN.1 structure detailed
240 * above.
241 */
242static int verify_hash(void *data_ptr, unsigned int data_len,
243 void *digest_info_ptr, unsigned int digest_info_len)
244{
245 mbedtls_asn1_buf hash_oid, params;
246 mbedtls_md_type_t md_alg;
247 uint8_t *p, *end, *hash;
248 CCHashResult_t pubKeyHash;
249 size_t len;
250 int rc;
251 CCError_t error;
252
253 /* Digest info should be an MBEDTLS_ASN1_SEQUENCE */
254 p = digest_info_ptr;
255 end = p + digest_info_len;
256 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
257 MBEDTLS_ASN1_SEQUENCE);
258 if (rc != 0)
259 return CRYPTO_ERR_HASH;
260
261 /* Get the hash algorithm */
262 rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
263 if (rc != 0)
264 return CRYPTO_ERR_HASH;
265
266 rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
267 if (rc != 0)
268 return CRYPTO_ERR_HASH;
269 /* Verify that hash algorithm is SHA256 */
270 if (md_alg != MBEDTLS_MD_SHA256)
271 return CRYPTO_ERR_HASH;
272
273 /* Hash should be octet string type */
274 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
275 if (rc != 0)
276 return CRYPTO_ERR_HASH;
277
278 /* Length of hash must match the algorithm's size */
279 if (len != HASH_RESULT_SIZE_IN_BYTES)
280 return CRYPTO_ERR_HASH;
281
282 /*
283 * CryptoCell utilises DMA internally to transfer data. Flush the data
284 * from caches.
285 */
286 flush_dcache_range((uintptr_t)data_ptr, data_len);
287
288 hash = p;
289 error = SBROM_CryptoHash((uintptr_t)PLAT_CRYPTOCELL_BASE,
290 (uintptr_t)data_ptr, data_len, pubKeyHash);
291 if (error != CC_OK)
292 return CRYPTO_ERR_HASH;
293
294 rc = memcmp(pubKeyHash, hash, HASH_RESULT_SIZE_IN_BYTES);
295 if (rc != 0)
296 return CRYPTO_ERR_HASH;
297
298 return CRYPTO_SUCCESS;
299}
300
301/*
302 * Register crypto library descriptor
303 */
Sumit Garg392e4df2019-11-15 10:43:00 +0530304REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);
Soby Mathew5d708002017-05-10 11:49:58 +0100305