blob: 0901d045abef85dd0be0b2dab602cb88c7383b43 [file] [log] [blame]
Juan Castilloa57a4d52015-04-02 15:44:20 +01001/*
Manish V Badarkhe92de80a2021-12-16 10:41:47 +00002 * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved.
Juan Castilloa57a4d52015-04-02 15:44:20 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castilloa57a4d52015-04-02 15:44:20 +01005 */
6
Sumit Garg392e4df2019-11-15 10:43:00 +05307#include <assert.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +01008#include <stddef.h>
9#include <string.h>
10
Juan Castillobae6b2a2015-11-05 09:24:53 +000011/* mbed TLS headers */
Sumit Garg392e4df2019-11-15 10:43:00 +053012#include <mbedtls/gcm.h>
Juan Castillobae6b2a2015-11-05 09:24:53 +000013#include <mbedtls/md.h>
14#include <mbedtls/memory_buffer_alloc.h>
15#include <mbedtls/oid.h>
16#include <mbedtls/platform.h>
Madhukar Pappireddy57eaae82020-03-05 18:18:40 -060017#include <mbedtls/x509.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +010018
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000019#include <common/debug.h>
20#include <drivers/auth/crypto_mod.h>
21#include <drivers/auth/mbedtls/mbedtls_common.h>
22#include <drivers/auth/mbedtls/mbedtls_config.h>
Sumit Garg392e4df2019-11-15 10:43:00 +053023#include <plat/common/platform.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024
Juan Castillobae6b2a2015-11-05 09:24:53 +000025#define LIB_NAME "mbed TLS"
Juan Castilloa57a4d52015-04-02 15:44:20 +010026
Manish V Badarkhee112a5a2021-10-06 23:41:50 +010027#if MEASURED_BOOT
28/*
29 * CRYPTO_MD_MAX_SIZE value is as per current stronger algorithm available
30 * so make sure that mbed TLS MD maximum size must be lesser than this.
31 */
32CASSERT(CRYPTO_MD_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE,
33 assert_mbedtls_md_size_overflow);
34
35#endif /* MEASURED_BOOT */
36
Juan Castilloa57a4d52015-04-02 15:44:20 +010037/*
38 * AlgorithmIdentifier ::= SEQUENCE {
39 * algorithm OBJECT IDENTIFIER,
40 * parameters ANY DEFINED BY algorithm OPTIONAL
41 * }
42 *
43 * SubjectPublicKeyInfo ::= SEQUENCE {
44 * algorithm AlgorithmIdentifier,
45 * subjectPublicKey BIT STRING
46 * }
47 *
48 * DigestInfo ::= SEQUENCE {
49 * digestAlgorithm AlgorithmIdentifier,
50 * digest OCTET STRING
51 * }
52 */
53
54/*
55 * Initialize the library and export the descriptor
56 */
57static void init(void)
58{
Juan Castillobae6b2a2015-11-05 09:24:53 +000059 /* Initialize mbed TLS */
Juan Castilloa57a4d52015-04-02 15:44:20 +010060 mbedtls_init();
61}
62
Manish V Badarkhe92de80a2021-12-16 10:41:47 +000063#if TRUSTED_BOARD_BOOT
Juan Castilloa57a4d52015-04-02 15:44:20 +010064/*
65 * Verify a signature.
66 *
67 * Parameters are passed using the DER encoding format following the ASN.1
68 * structures detailed above.
69 */
70static int verify_signature(void *data_ptr, unsigned int data_len,
71 void *sig_ptr, unsigned int sig_len,
72 void *sig_alg, unsigned int sig_alg_len,
73 void *pk_ptr, unsigned int pk_len)
74{
Juan Castillobae6b2a2015-11-05 09:24:53 +000075 mbedtls_asn1_buf sig_oid, sig_params;
76 mbedtls_asn1_buf signature;
77 mbedtls_md_type_t md_alg;
78 mbedtls_pk_type_t pk_alg;
Soby Mathew0a68d132017-05-31 10:35:27 +010079 mbedtls_pk_context pk = {0};
Juan Castilloa57a4d52015-04-02 15:44:20 +010080 int rc;
81 void *sig_opts = NULL;
Juan Castillobae6b2a2015-11-05 09:24:53 +000082 const mbedtls_md_info_t *md_info;
Juan Castilloa57a4d52015-04-02 15:44:20 +010083 unsigned char *p, *end;
Juan Castillobae6b2a2015-11-05 09:24:53 +000084 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Juan Castilloa57a4d52015-04-02 15:44:20 +010085
86 /* Get pointers to signature OID and parameters */
87 p = (unsigned char *)sig_alg;
88 end = (unsigned char *)(p + sig_alg_len);
Juan Castillobae6b2a2015-11-05 09:24:53 +000089 rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
Juan Castilloa57a4d52015-04-02 15:44:20 +010090 if (rc != 0) {
91 return CRYPTO_ERR_SIGNATURE;
92 }
93
94 /* Get the actual signature algorithm (MD + PK) */
Soby Mathew0a68d132017-05-31 10:35:27 +010095 rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
Juan Castilloa57a4d52015-04-02 15:44:20 +010096 if (rc != 0) {
97 return CRYPTO_ERR_SIGNATURE;
98 }
99
100 /* Parse the public key */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000101 mbedtls_pk_init(&pk);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100102 p = (unsigned char *)pk_ptr;
103 end = (unsigned char *)(p + pk_len);
Juan Castillobae6b2a2015-11-05 09:24:53 +0000104 rc = mbedtls_pk_parse_subpubkey(&p, end, &pk);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100105 if (rc != 0) {
Soby Mathew0a68d132017-05-31 10:35:27 +0100106 rc = CRYPTO_ERR_SIGNATURE;
107 goto end2;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100108 }
109
110 /* Get the signature (bitstring) */
111 p = (unsigned char *)sig_ptr;
112 end = (unsigned char *)(p + sig_len);
113 signature.tag = *p;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000114 rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100115 if (rc != 0) {
116 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100117 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100118 }
119 signature.p = p;
120
121 /* Calculate the hash of the data */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000122 md_info = mbedtls_md_info_from_type(md_alg);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100123 if (md_info == NULL) {
124 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100125 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100126 }
127 p = (unsigned char *)data_ptr;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000128 rc = mbedtls_md(md_info, p, data_len, hash);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100129 if (rc != 0) {
130 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100131 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100132 }
133
134 /* Verify the signature */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000135 rc = mbedtls_pk_verify_ext(pk_alg, sig_opts, &pk, md_alg, hash,
136 mbedtls_md_get_size(md_info),
137 signature.p, signature.len);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100138 if (rc != 0) {
139 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100140 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100141 }
142
143 /* Signature verification success */
144 rc = CRYPTO_SUCCESS;
145
Soby Mathew0a68d132017-05-31 10:35:27 +0100146end1:
Juan Castillobae6b2a2015-11-05 09:24:53 +0000147 mbedtls_pk_free(&pk);
Soby Mathew0a68d132017-05-31 10:35:27 +0100148end2:
149 mbedtls_free(sig_opts);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100150 return rc;
151}
152
153/*
154 * Match a hash
155 *
156 * Digest info is passed in DER format following the ASN.1 structure detailed
157 * above.
158 */
159static int verify_hash(void *data_ptr, unsigned int data_len,
160 void *digest_info_ptr, unsigned int digest_info_len)
161{
Juan Castillobae6b2a2015-11-05 09:24:53 +0000162 mbedtls_asn1_buf hash_oid, params;
163 mbedtls_md_type_t md_alg;
164 const mbedtls_md_info_t *md_info;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100165 unsigned char *p, *end, *hash;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000166 unsigned char data_hash[MBEDTLS_MD_MAX_SIZE];
Juan Castilloa57a4d52015-04-02 15:44:20 +0100167 size_t len;
168 int rc;
169
Juan Castillobae6b2a2015-11-05 09:24:53 +0000170 /* Digest info should be an MBEDTLS_ASN1_SEQUENCE */
Juan Castilloa57a4d52015-04-02 15:44:20 +0100171 p = (unsigned char *)digest_info_ptr;
Sandrine Bailleuxdf8de2d2016-01-04 15:49:23 +0000172 end = p + digest_info_len;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000173 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
174 MBEDTLS_ASN1_SEQUENCE);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100175 if (rc != 0) {
176 return CRYPTO_ERR_HASH;
177 }
178
179 /* Get the hash algorithm */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000180 rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100181 if (rc != 0) {
182 return CRYPTO_ERR_HASH;
183 }
184
Juan Castillobae6b2a2015-11-05 09:24:53 +0000185 rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100186 if (rc != 0) {
187 return CRYPTO_ERR_HASH;
188 }
189
Juan Castillobae6b2a2015-11-05 09:24:53 +0000190 md_info = mbedtls_md_info_from_type(md_alg);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100191 if (md_info == NULL) {
192 return CRYPTO_ERR_HASH;
193 }
194
195 /* Hash should be octet string type */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000196 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100197 if (rc != 0) {
198 return CRYPTO_ERR_HASH;
199 }
200
201 /* Length of hash must match the algorithm's size */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000202 if (len != mbedtls_md_get_size(md_info)) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100203 return CRYPTO_ERR_HASH;
204 }
205 hash = p;
206
207 /* Calculate the hash of the data */
208 p = (unsigned char *)data_ptr;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000209 rc = mbedtls_md(md_info, p, data_len, data_hash);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100210 if (rc != 0) {
211 return CRYPTO_ERR_HASH;
212 }
213
214 /* Compare values */
Antonio Nino Diaz0ca1afa2017-02-09 10:26:54 +0000215 rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info));
Juan Castilloa57a4d52015-04-02 15:44:20 +0100216 if (rc != 0) {
217 return CRYPTO_ERR_HASH;
218 }
219
220 return CRYPTO_SUCCESS;
221}
Manish V Badarkhe92de80a2021-12-16 10:41:47 +0000222#endif /* TRUSTED_BOARD_BOOT */
Juan Castilloa57a4d52015-04-02 15:44:20 +0100223
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000224#if MEASURED_BOOT
225/*
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100226 * Map a generic crypto message digest algorithm to the corresponding macro used
227 * by Mbed TLS.
228 */
229static inline mbedtls_md_type_t md_type(enum crypto_md_algo algo)
230{
231 switch (algo) {
232 case CRYPTO_MD_SHA512:
233 return MBEDTLS_MD_SHA512;
234 case CRYPTO_MD_SHA384:
235 return MBEDTLS_MD_SHA384;
236 case CRYPTO_MD_SHA256:
237 return MBEDTLS_MD_SHA256;
238 default:
239 /* Invalid hash algorithm. */
240 return MBEDTLS_MD_NONE;
241 }
242}
243
244/*
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000245 * Calculate a hash
246 *
247 * output points to the computed hash
248 */
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100249static int calc_hash(enum crypto_md_algo md_algo, void *data_ptr,
250 unsigned int data_len,
251 unsigned char output[CRYPTO_MD_MAX_SIZE])
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000252{
253 const mbedtls_md_info_t *md_info;
254
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100255 md_info = mbedtls_md_info_from_type(md_type(md_algo));
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000256 if (md_info == NULL) {
257 return CRYPTO_ERR_HASH;
258 }
259
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100260 /*
261 * Calculate the hash of the data, it is safe to pass the
262 * 'output' hash buffer pointer considering its size is always
263 * bigger than or equal to MBEDTLS_MD_MAX_SIZE.
264 */
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000265 return mbedtls_md(md_info, data_ptr, data_len, output);
266}
267#endif /* MEASURED_BOOT */
268
Sumit Garg392e4df2019-11-15 10:43:00 +0530269#if TF_MBEDTLS_USE_AES_GCM
270/*
271 * Stack based buffer allocation for decryption operation. It could
272 * be configured to balance stack usage vs execution speed.
273 */
274#define DEC_OP_BUF_SIZE 128
275
276static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
277 unsigned int key_len, const void *iv,
278 unsigned int iv_len, const void *tag,
279 unsigned int tag_len)
280{
281 mbedtls_gcm_context ctx;
282 mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
283 unsigned char buf[DEC_OP_BUF_SIZE];
284 unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
285 unsigned char *pt = data_ptr;
286 size_t dec_len;
287 int diff, i, rc;
288
289 mbedtls_gcm_init(&ctx);
290
291 rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
292 if (rc != 0) {
293 rc = CRYPTO_ERR_DECRYPTION;
294 goto exit_gcm;
295 }
296
297 rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
298 if (rc != 0) {
299 rc = CRYPTO_ERR_DECRYPTION;
300 goto exit_gcm;
301 }
302
303 while (len > 0) {
304 dec_len = MIN(sizeof(buf), len);
305
306 rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
307 if (rc != 0) {
308 rc = CRYPTO_ERR_DECRYPTION;
309 goto exit_gcm;
310 }
311
312 memcpy(pt, buf, dec_len);
313 pt += dec_len;
314 len -= dec_len;
315 }
316
317 rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
318 if (rc != 0) {
319 rc = CRYPTO_ERR_DECRYPTION;
320 goto exit_gcm;
321 }
322
323 /* Check tag in "constant-time" */
324 for (diff = 0, i = 0; i < tag_len; i++)
325 diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];
326
327 if (diff != 0) {
328 rc = CRYPTO_ERR_DECRYPTION;
329 goto exit_gcm;
330 }
331
332 /* GCM decryption success */
333 rc = CRYPTO_SUCCESS;
334
335exit_gcm:
336 mbedtls_gcm_free(&ctx);
337 return rc;
338}
339
340/*
341 * Authenticated decryption of an image
342 */
343static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
344 size_t len, const void *key, unsigned int key_len,
345 unsigned int key_flags, const void *iv,
346 unsigned int iv_len, const void *tag,
347 unsigned int tag_len)
348{
349 int rc;
350
351 assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);
352
353 switch (dec_algo) {
354 case CRYPTO_GCM_DECRYPT:
355 rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
356 tag, tag_len);
357 if (rc != 0)
358 return rc;
359 break;
360 default:
361 return CRYPTO_ERR_DECRYPTION;
362 }
363
364 return CRYPTO_SUCCESS;
365}
366#endif /* TF_MBEDTLS_USE_AES_GCM */
367
Juan Castilloa57a4d52015-04-02 15:44:20 +0100368/*
369 * Register crypto library descriptor
370 */
Manish V Badarkhe92de80a2021-12-16 10:41:47 +0000371#if MEASURED_BOOT && TRUSTED_BOARD_BOOT
Sumit Garg392e4df2019-11-15 10:43:00 +0530372#if TF_MBEDTLS_USE_AES_GCM
373REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
374 auth_decrypt);
375#else
376REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
377 NULL);
378#endif
Manish V Badarkhe92de80a2021-12-16 10:41:47 +0000379#elif TRUSTED_BOARD_BOOT
Sumit Garg392e4df2019-11-15 10:43:00 +0530380#if TF_MBEDTLS_USE_AES_GCM
381REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash,
382 auth_decrypt);
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000383#else
Sumit Garg392e4df2019-11-15 10:43:00 +0530384REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);
385#endif
Manish V Badarkhe92de80a2021-12-16 10:41:47 +0000386#elif MEASURED_BOOT
387REGISTER_CRYPTO_LIB(LIB_NAME, init, calc_hash);
388#endif /* MEASURED_BOOT && TRUSTED_BOARD_BOOT */