blob: af175e4e3e274a0d9cb509d96fb3075f455bd48c [file] [log] [blame]
Gilad Ben-Yossef033327a2019-05-15 09:24:04 +03001/*
Govindraj Rajaa2872f92023-02-03 11:08:00 +00002 * Copyright (c) 2017-2023 ARM Limited and Contributors. All rights reserved.
Gilad Ben-Yossef033327a2019-05-15 09:24:04 +03003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <stddef.h>
9#include <string.h>
10
Claus Pedersen785e66c2022-09-12 22:42:58 +000011#include <platform_def.h>
12
Gilad Ben-Yossef033327a2019-05-15 09:24:04 +030013#include <drivers/arm/cryptocell/713/bsv_api.h>
14#include <drivers/arm/cryptocell/713/bsv_crypto_asym_api.h>
15#include <drivers/auth/crypto_mod.h>
16
17#include <mbedtls/oid.h>
Manish V Badarkheb55d0552021-10-11 19:46:08 +010018#include <mbedtls/x509.h>
Gilad Ben-Yossef033327a2019-05-15 09:24:04 +030019
20#define LIB_NAME "CryptoCell 713 SBROM"
21#define RSA_SALT_LEN 32
22#define RSA_EXPONENT 65537
23
24/*
25 * AlgorithmIdentifier ::= SEQUENCE {
26 * algorithm OBJECT IDENTIFIER,
27 * parameters ANY DEFINED BY algorithm OPTIONAL
28 * }
29 *
30 * SubjectPublicKeyInfo ::= SEQUENCE {
31 * algorithm AlgorithmIdentifier,
32 * subjectPublicKey BIT STRING
33 * }
34 *
35 * DigestInfo ::= SEQUENCE {
36 * digestAlgorithm AlgorithmIdentifier,
37 * digest OCTET STRING
38 * }
39 *
40 * RSASSA-PSS-params ::= SEQUENCE {
41 * hashAlgorithm [0] HashAlgorithm,
42 * maskGenAlgorithm [1] MaskGenAlgorithm,
43 * saltLength [2] INTEGER,
44 * trailerField [3] TrailerField DEFAULT trailerFieldBC
45 * }
46 */
47
48/*
49 * Initialize the library and export the descriptor
50 */
51static void init(void)
52{
53 CCError_t ret;
54 uint32_t lcs;
55
56 /* Initialize CC SBROM */
57 ret = CC_BsvInit((uintptr_t)PLAT_CRYPTOCELL_BASE);
58 if (ret != CC_OK) {
59 ERROR("CryptoCell CC_BsvInit() error %x\n", ret);
60 panic();
61 }
62
63 /* Initialize lifecycle state */
64 ret = CC_BsvGetAndInitLcs((uintptr_t)PLAT_CRYPTOCELL_BASE, &lcs);
65 if (ret != CC_OK) {
66 ERROR("CryptoCell CC_BsvGetAndInitLcs() error %x\n", ret);
67 panic();
68 }
69}
70
71/*
72 * Verify a signature.
73 *
74 * Parameters are passed using the DER encoding format following the ASN.1
75 * structures detailed above.
76 */
77static int verify_signature(void *data_ptr, unsigned int data_len,
78 void *sig_ptr, unsigned int sig_len,
79 void *sig_alg, unsigned int sig_alg_len,
80 void *pk_ptr, unsigned int pk_len)
81{
82 CCError_t error;
83 CCBsvNBuff_t NBuff;
84 CCBsvSignature_t signature;
Govindraj Rajaa2872f92023-02-03 11:08:00 +000085 int rc, exp, expected_salt_len;
Gilad Ben-Yossef033327a2019-05-15 09:24:04 +030086 mbedtls_asn1_buf sig_oid, alg_oid, params;
Govindraj Rajaa2872f92023-02-03 11:08:00 +000087 mbedtls_md_type_t md_alg, mgf1_hash_id;
Gilad Ben-Yossef033327a2019-05-15 09:24:04 +030088 mbedtls_pk_type_t pk_alg;
Govindraj Rajaa2872f92023-02-03 11:08:00 +000089
Gilad Ben-Yossef033327a2019-05-15 09:24:04 +030090 size_t len;
91 uint8_t *p, *end;
92 CCHashResult_t digest;
93 CCBool_t is_verified;
94 /* This is a rather large array, we don't want it on stack */
95 static uint32_t workspace[BSV_RSA_WORKSPACE_MIN_SIZE];
96
97 /* Verify the signature algorithm */
98 /* Get pointers to signature OID and parameters */
99 p = sig_alg;
100 end = p + sig_alg_len;
101 rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &params);
102 if (rc != 0)
103 return CRYPTO_ERR_SIGNATURE;
104
105 /* Get the actual signature algorithm (MD + PK) */
106 rc = mbedtls_oid_get_sig_alg(&sig_oid, &md_alg, &pk_alg);
107 if (rc != 0)
108 return CRYPTO_ERR_SIGNATURE;
109
110 /* The CryptoCell only supports RSASSA-PSS signature */
111 if (pk_alg != MBEDTLS_PK_RSASSA_PSS || md_alg != MBEDTLS_MD_NONE)
112 return CRYPTO_ERR_SIGNATURE;
113
114 /* Verify the RSASSA-PSS params */
115 /* The trailer field is verified to be 0xBC internally by this API */
116 rc = mbedtls_x509_get_rsassa_pss_params(&params, &md_alg,
Govindraj Rajaa2872f92023-02-03 11:08:00 +0000117 &mgf1_hash_id,
118 &expected_salt_len);
Gilad Ben-Yossef033327a2019-05-15 09:24:04 +0300119 if (rc != 0)
120 return CRYPTO_ERR_SIGNATURE;
121
122 /* The CryptoCell only supports SHA256 as hash algorithm */
123 if (md_alg != MBEDTLS_MD_SHA256 ||
Govindraj Rajaa2872f92023-02-03 11:08:00 +0000124 mgf1_hash_id != MBEDTLS_MD_SHA256)
Gilad Ben-Yossef033327a2019-05-15 09:24:04 +0300125 return CRYPTO_ERR_SIGNATURE;
126
Govindraj Rajaa2872f92023-02-03 11:08:00 +0000127 if (expected_salt_len != RSA_SALT_LEN)
Gilad Ben-Yossef033327a2019-05-15 09:24:04 +0300128 return CRYPTO_ERR_SIGNATURE;
129
130 /* Parse the public key */
131 p = pk_ptr;
132 end = p + pk_len;
133 rc = mbedtls_asn1_get_tag(&p, end, &len,
134 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
135 if (rc != 0)
136 return CRYPTO_ERR_SIGNATURE;
137
138 end = p + len;
139 rc = mbedtls_asn1_get_alg_null(&p, end, &alg_oid);
140 if (rc != 0)
141 return CRYPTO_ERR_SIGNATURE;
142
143 if (mbedtls_oid_get_pk_alg(&alg_oid, &pk_alg) != 0)
144 return CRYPTO_ERR_SIGNATURE;
145
146 if (pk_alg != MBEDTLS_PK_RSA)
147 return CRYPTO_ERR_SIGNATURE;
148
149 rc = mbedtls_asn1_get_bitstring_null(&p, end, &len);
150 if (rc != 0)
151 return CRYPTO_ERR_SIGNATURE;
152
153 rc = mbedtls_asn1_get_tag(&p, end, &len,
154 MBEDTLS_ASN1_CONSTRUCTED |
155 MBEDTLS_ASN1_SEQUENCE);
156 if (rc != 0)
157 return CRYPTO_ERR_SIGNATURE;
158
159 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER);
160 if (rc != 0)
161 return CRYPTO_ERR_SIGNATURE;
162
163 if (*p == 0) {
164 p++; len--;
165 }
166 if (len != BSV_CERT_RSA_KEY_SIZE_IN_BYTES || ((p + len) > end))
167 return CRYPTO_ERR_SIGNATURE;
168
169 /*
170 * Copy N from certificate.
171 */
172 memcpy(NBuff, p, BSV_CERT_RSA_KEY_SIZE_IN_BYTES);
173
174 /* Verify the RSA exponent */
175 p += len;
176 rc = mbedtls_asn1_get_int(&p, end, &exp);
177 if (rc != 0)
178 return CRYPTO_ERR_SIGNATURE;
179
180 if (exp != RSA_EXPONENT)
181 return CRYPTO_ERR_SIGNATURE;
182
183 /* Get the signature (bitstring) */
184 p = sig_ptr;
185 end = p + sig_len;
186 rc = mbedtls_asn1_get_bitstring_null(&p, end, &len);
187 if (rc != 0)
188 return CRYPTO_ERR_SIGNATURE;
189
190 if (len != BSV_CERT_RSA_KEY_SIZE_IN_BYTES || ((p + len) > end))
191 return CRYPTO_ERR_SIGNATURE;
192
193 /*
194 * Copy the signature (in BE format)
195 */
196 memcpy((uint8_t *)signature, p, BSV_CERT_RSA_KEY_SIZE_IN_BYTES);
197
198 error = CC_BsvSha256((uintptr_t)PLAT_CRYPTOCELL_BASE,
199 data_ptr, data_len, digest);
200 if (error != CC_OK)
201 return CRYPTO_ERR_SIGNATURE;
202
203 /* Verify the signature */
204 error = CC_BsvRsaPssVerify((uintptr_t)PLAT_CRYPTOCELL_BASE, NBuff,
205 NULL, signature, digest, workspace,
206 BSV_RSA_WORKSPACE_MIN_SIZE, &is_verified);
207 if ((error != CC_OK) || (is_verified != CC_TRUE))
208 return CRYPTO_ERR_SIGNATURE;
209
210 /* Signature verification success */
211 return CRYPTO_SUCCESS;
212}
213
214/*
215 * Match a hash
216 *
217 * Digest info is passed in DER format following the ASN.1 structure detailed
218 * above.
219 */
220static int verify_hash(void *data_ptr, unsigned int data_len,
221 void *digest_info_ptr, unsigned int digest_info_len)
222{
223 mbedtls_asn1_buf hash_oid, params;
224 mbedtls_md_type_t md_alg;
225 uint8_t *p, *end, *hash;
226 CCHashResult_t pubKeyHash;
227 size_t len;
228 int rc;
229 CCError_t error;
230
231 /* Digest info should be an MBEDTLS_ASN1_SEQUENCE */
232 p = digest_info_ptr;
233 end = p + digest_info_len;
234 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
235 MBEDTLS_ASN1_SEQUENCE);
236 if (rc != 0)
237 return CRYPTO_ERR_HASH;
238
239 /* Get the hash algorithm */
240 rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
241 if (rc != 0)
242 return CRYPTO_ERR_HASH;
243
244 rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
245 if (rc != 0)
246 return CRYPTO_ERR_HASH;
247 /* Verify that hash algorithm is SHA256 */
248 if (md_alg != MBEDTLS_MD_SHA256)
249 return CRYPTO_ERR_HASH;
250
251 /* Hash should be octet string type */
252 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
253 if (rc != 0)
254 return CRYPTO_ERR_HASH;
255
256 /* Length of hash must match the algorithm's size */
257 if (len != HASH_RESULT_SIZE_IN_BYTES)
258 return CRYPTO_ERR_HASH;
259
260 hash = p;
261 error = CC_BsvSha256((uintptr_t)PLAT_CRYPTOCELL_BASE, data_ptr,
262 data_len, pubKeyHash);
263 if (error != CC_OK)
264 return CRYPTO_ERR_HASH;
265
266 rc = memcmp(pubKeyHash, hash, HASH_RESULT_SIZE_IN_BYTES);
267 if (rc != 0)
268 return CRYPTO_ERR_HASH;
269
270 return CRYPTO_SUCCESS;
271}
272
273/*
274 * Register crypto library descriptor
275 */
276REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);