blob: c7ee36fa71041a37069e5d7074e60cfa3d2577ec [file] [log] [blame]
Soby Mathew5d708002017-05-10 11:49:58 +01001/*
Madhukar Pappireddy57eaae82020-03-05 18:18:40 -06002 * Copyright (c) 2017-2020, 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;
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, &params);
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(&params, &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,
Gilad Ben-Yossefa6e53422019-09-15 13:29:29 +0300229 data_len, RSA_PSS);
Soby Mathew5d708002017-05-10 11:49:58 +0100230 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 */
243static 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, &params);
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 */
Sumit Garg392e4df2019-11-15 10:43:00 +0530305REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);
Soby Mathew5d708002017-05-10 11:49:58 +0100306