1 /*
2 * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <stddef.h>
8 #include <string.h>
9
10 #include <platform_def.h>
11
12 #include <arch_helpers.h>
13 #include <common/debug.h>
14 #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>
20 #include <drivers/auth/crypto_mod.h>
21 #include <drivers/auth/mbedtls/mbedtls_common.h>
22 #include <lib/utils.h>
23
24 #include <mbedtls/oid.h>
25 #include <mbedtls/x509.h>
26
27 #define LIB_NAME "CryptoCell 712 SBROM"
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 */
init(void)58 static void init(void)
59 {
60 CCError_t ret;
61 uint32_t lcs;
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 }
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 }
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 */
verify_signature(void * data_ptr,unsigned int data_len,void * sig_ptr,unsigned int sig_len,void * sig_alg,unsigned int sig_alg_len,void * pk_ptr,unsigned int pk_len)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;
98 int rc, exp;
99 mbedtls_asn1_buf sig_oid, alg_oid, params;
100 mbedtls_md_type_t md_alg;
101 mbedtls_pk_type_t pk_alg;
102 mbedtls_pk_rsassa_pss_options pss_opts;
103 size_t len;
104 uint8_t *p, *end;
105 /* Temp buf to store the public key modulo (N) in LE format */
106 uint32_t RevN[SB_RSA_MOD_SIZE_IN_WORDS];
107
108 /* Verify the signature algorithm */
109 /* Get pointers to signature OID and parameters */
110 p = sig_alg;
111 end = p + sig_alg_len;
112 rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, ¶ms);
113 if (rc != 0)
114 return CRYPTO_ERR_SIGNATURE;
115
116 /* Get the actual signature algorithm (MD + PK) */
117 rc = mbedtls_oid_get_sig_alg(&sig_oid, &md_alg, &pk_alg);
118 if (rc != 0)
119 return CRYPTO_ERR_SIGNATURE;
120
121 /* The CryptoCell only supports RSASSA-PSS signature */
122 if (pk_alg != MBEDTLS_PK_RSASSA_PSS || md_alg != MBEDTLS_MD_NONE)
123 return CRYPTO_ERR_SIGNATURE;
124
125 /* Verify the RSASSA-PSS params */
126 /* The trailer field is verified to be 0xBC internally by this API */
127 rc = mbedtls_x509_get_rsassa_pss_params(¶ms, &md_alg,
128 &pss_opts.mgf1_hash_id,
129 &pss_opts.expected_salt_len);
130 if (rc != 0)
131 return CRYPTO_ERR_SIGNATURE;
132
133 /* The CryptoCell only supports SHA256 as hash algorithm */
134 if (md_alg != MBEDTLS_MD_SHA256 || pss_opts.mgf1_hash_id != MBEDTLS_MD_SHA256)
135 return CRYPTO_ERR_SIGNATURE;
136
137 if (pss_opts.expected_salt_len != RSA_SALT_LEN)
138 return CRYPTO_ERR_SIGNATURE;
139
140 /* Parse the public key */
141 p = pk_ptr;
142 end = p + pk_len;
143 rc = mbedtls_asn1_get_tag(&p, end, &len,
144 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
145 if (rc != 0)
146 return CRYPTO_ERR_SIGNATURE;
147
148 end = p + len;
149 rc = mbedtls_asn1_get_alg_null(&p, end, &alg_oid);
150 if (rc != 0)
151 return CRYPTO_ERR_SIGNATURE;
152
153 if (mbedtls_oid_get_pk_alg(&alg_oid, &pk_alg) != 0)
154 return CRYPTO_ERR_SIGNATURE;
155
156 if (pk_alg != MBEDTLS_PK_RSA)
157 return CRYPTO_ERR_SIGNATURE;
158
159 rc = mbedtls_asn1_get_bitstring_null(&p, end, &len);
160 if (rc != 0)
161 return CRYPTO_ERR_SIGNATURE;
162
163 rc = mbedtls_asn1_get_tag(&p, end, &len,
164 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
165 if (rc != 0)
166 return CRYPTO_ERR_SIGNATURE;
167
168 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER);
169 if (rc != 0)
170 return CRYPTO_ERR_SIGNATURE;
171
172 if (*p == 0) {
173 p++; len--;
174 }
175 if (len != RSA_MOD_SIZE_IN_BYTES || ((p + len) > end))
176 return CRYPTO_ERR_SIGNATURE;
177
178 /*
179 * The CCSbVerifySignature() API expects N and Np in BE format and
180 * the signature in LE format. Copy N from certificate.
181 */
182 memcpy(pk.N, p, RSA_MOD_SIZE_IN_BYTES);
183
184 /* Verify the RSA exponent */
185 p += len;
186 rc = mbedtls_asn1_get_int(&p, end, &exp);
187 if (rc != 0)
188 return CRYPTO_ERR_SIGNATURE;
189
190 if (exp != RSA_EXPONENT)
191 return CRYPTO_ERR_SIGNATURE;
192
193 /*
194 * Calculate the Np (Barrett n' value). The RSA_CalcNp() API expects
195 * N in LE format. Hence reverse N into a temporary buffer `RevN`.
196 */
197 UTIL_ReverseMemCopy((uint8_t *)RevN, (uint8_t *)pk.N, sizeof(RevN));
198
199 RSA_CalcNp((uintptr_t)PLAT_CRYPTOCELL_BASE, RevN, pk.Np);
200
201 /* Np is in LE format. Reverse it to BE */
202 UTIL_ReverseBuff((uint8_t *)pk.Np, sizeof(pk.Np));
203
204 /* Get the signature (bitstring) */
205 p = sig_ptr;
206 end = p + sig_len;
207 rc = mbedtls_asn1_get_bitstring_null(&p, end, &len);
208 if (rc != 0)
209 return CRYPTO_ERR_SIGNATURE;
210
211 if (len != RSA_MOD_SIZE_IN_BYTES || ((p + len) > end))
212 return CRYPTO_ERR_SIGNATURE;
213
214 /*
215 * The signature is BE format. Convert it to LE before calling
216 * CCSbVerifySignature().
217 */
218 UTIL_ReverseMemCopy((uint8_t *)signature.sig, p, RSA_MOD_SIZE_IN_BYTES);
219
220 /*
221 * CryptoCell utilises DMA internally to transfer data. Flush the data
222 * from caches.
223 */
224 flush_dcache_range((uintptr_t)data_ptr, data_len);
225
226 /* Verify the signature */
227 error = CCSbVerifySignature((uintptr_t)PLAT_CRYPTOCELL_BASE,
228 (uint32_t *)data_ptr, &pk, &signature,
229 data_len, RSA_PSS);
230 if (error != CC_OK)
231 return CRYPTO_ERR_SIGNATURE;
232
233 /* Signature verification success */
234 return CRYPTO_SUCCESS;
235 }
236
237 /*
238 * Match a hash
239 *
240 * Digest info is passed in DER format following the ASN.1 structure detailed
241 * above.
242 */
verify_hash(void * data_ptr,unsigned int data_len,void * digest_info_ptr,unsigned int digest_info_len)243 static int verify_hash(void *data_ptr, unsigned int data_len,
244 void *digest_info_ptr, unsigned int digest_info_len)
245 {
246 mbedtls_asn1_buf hash_oid, params;
247 mbedtls_md_type_t md_alg;
248 uint8_t *p, *end, *hash;
249 CCHashResult_t pubKeyHash;
250 size_t len;
251 int rc;
252 CCError_t error;
253
254 /* Digest info should be an MBEDTLS_ASN1_SEQUENCE */
255 p = digest_info_ptr;
256 end = p + digest_info_len;
257 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
258 MBEDTLS_ASN1_SEQUENCE);
259 if (rc != 0)
260 return CRYPTO_ERR_HASH;
261
262 /* Get the hash algorithm */
263 rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, ¶ms);
264 if (rc != 0)
265 return CRYPTO_ERR_HASH;
266
267 rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
268 if (rc != 0)
269 return CRYPTO_ERR_HASH;
270 /* Verify that hash algorithm is SHA256 */
271 if (md_alg != MBEDTLS_MD_SHA256)
272 return CRYPTO_ERR_HASH;
273
274 /* Hash should be octet string type */
275 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
276 if (rc != 0)
277 return CRYPTO_ERR_HASH;
278
279 /* Length of hash must match the algorithm's size */
280 if (len != HASH_RESULT_SIZE_IN_BYTES)
281 return CRYPTO_ERR_HASH;
282
283 /*
284 * CryptoCell utilises DMA internally to transfer data. Flush the data
285 * from caches.
286 */
287 flush_dcache_range((uintptr_t)data_ptr, data_len);
288
289 hash = p;
290 error = SBROM_CryptoHash((uintptr_t)PLAT_CRYPTOCELL_BASE,
291 (uintptr_t)data_ptr, data_len, pubKeyHash);
292 if (error != CC_OK)
293 return CRYPTO_ERR_HASH;
294
295 rc = memcmp(pubKeyHash, hash, HASH_RESULT_SIZE_IN_BYTES);
296 if (rc != 0)
297 return CRYPTO_ERR_HASH;
298
299 return CRYPTO_SUCCESS;
300 }
301
302 /*
303 * Register crypto library descriptor
304 */
305 REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);
306
307