blob: 25eb6bcb6d9384303eddd1b1000e8a5b0e77867b [file] [log] [blame]
Soby Mathew5d708002017-05-10 11:49:58 +01001/*
Gilad Ben-Yosseffbb88a32019-05-14 10:48:18 +03002 * Copyright (c) 2017-2019, 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>
25
Gilad Ben-Yossef6fbe0fc2019-05-14 14:47:36 +030026#define LIB_NAME "CryptoCell 712 SBROM"
Soby Mathew5d708002017-05-10 11:49:58 +010027#define RSA_SALT_LEN 32
28#define RSA_EXPONENT 65537
29
30/*
31 * AlgorithmIdentifier ::= SEQUENCE {
32 * algorithm OBJECT IDENTIFIER,
33 * parameters ANY DEFINED BY algorithm OPTIONAL
34 * }
35 *
36 * SubjectPublicKeyInfo ::= SEQUENCE {
37 * algorithm AlgorithmIdentifier,
38 * subjectPublicKey BIT STRING
39 * }
40 *
41 * DigestInfo ::= SEQUENCE {
42 * digestAlgorithm AlgorithmIdentifier,
43 * digest OCTET STRING
44 * }
45 *
46 * RSASSA-PSS-params ::= SEQUENCE {
47 * hashAlgorithm [0] HashAlgorithm,
48 * maskGenAlgorithm [1] MaskGenAlgorithm,
49 * saltLength [2] INTEGER,
50 * trailerField [3] TrailerField DEFAULT trailerFieldBC
51 * }
52 */
53
54/*
55 * Initialize the library and export the descriptor
56 */
57static void init(void)
58{
59 CCError_t ret;
Soby Mathewd3490232017-06-05 15:55:59 +010060 uint32_t lcs;
Soby Mathew5d708002017-05-10 11:49:58 +010061
62 /* Initialize CC SBROM */
63 ret = CC_BsvSbromInit((uintptr_t)PLAT_CRYPTOCELL_BASE);
64 if (ret != CC_OK) {
65 ERROR("CryptoCell CC_BsvSbromInit() error %x\n", ret);
66 panic();
67 }
Soby Mathewd3490232017-06-05 15:55:59 +010068
69 /* Initialize lifecycle state */
70 ret = CC_BsvLcsGetAndInit((uintptr_t)PLAT_CRYPTOCELL_BASE, &lcs);
71 if (ret != CC_OK) {
72 ERROR("CryptoCell CC_BsvLcsGetAndInit() error %x\n", ret);
73 panic();
74 }
75
76 /* If the lifecyclestate is `SD`, then stop further execution */
77 if (lcs == CC_BSV_SECURITY_DISABLED_LCS) {
78 ERROR("CryptoCell LCS is security-disabled\n");
79 panic();
80 }
Soby Mathew5d708002017-05-10 11:49:58 +010081}
82
83/*
84 * Verify a signature.
85 *
86 * Parameters are passed using the DER encoding format following the ASN.1
87 * structures detailed above.
88 */
89static int verify_signature(void *data_ptr, unsigned int data_len,
90 void *sig_ptr, unsigned int sig_len,
91 void *sig_alg, unsigned int sig_alg_len,
92 void *pk_ptr, unsigned int pk_len)
93{
94 CCError_t error;
95 CCSbNParams_t pk;
96 CCSbSignature_t signature;
97 int rc, exp;
98 mbedtls_asn1_buf sig_oid, alg_oid, params;
99 mbedtls_md_type_t md_alg;
100 mbedtls_pk_type_t pk_alg;
101 mbedtls_pk_rsassa_pss_options pss_opts;
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, &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 */
121 if (pk_alg != MBEDTLS_PK_RSASSA_PSS || md_alg != MBEDTLS_MD_NONE)
122 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,
127 &pss_opts.mgf1_hash_id,
128 &pss_opts.expected_salt_len);
129 if (rc != 0)
130 return CRYPTO_ERR_SIGNATURE;
131
132 /* The CryptoCell only supports SHA256 as hash algorithm */
133 if (md_alg != MBEDTLS_MD_SHA256 || pss_opts.mgf1_hash_id != MBEDTLS_MD_SHA256)
134 return CRYPTO_ERR_SIGNATURE;
135
136 if (pss_opts.expected_salt_len != RSA_SALT_LEN)
137 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 */
304REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash);
305