blob: 114e6adf1031aaa80ecff71ff841a32f5d6fd52c [file] [log] [blame]
Juan Castilloa57a4d52015-04-02 15:44:20 +01001/*
Manish V Badarkhee112a5a2021-10-06 23:41:50 +01002 * Copyright (c) 2015-2021, 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
63/*
64 * Verify a signature.
65 *
66 * Parameters are passed using the DER encoding format following the ASN.1
67 * structures detailed above.
68 */
69static int verify_signature(void *data_ptr, unsigned int data_len,
70 void *sig_ptr, unsigned int sig_len,
71 void *sig_alg, unsigned int sig_alg_len,
72 void *pk_ptr, unsigned int pk_len)
73{
Juan Castillobae6b2a2015-11-05 09:24:53 +000074 mbedtls_asn1_buf sig_oid, sig_params;
75 mbedtls_asn1_buf signature;
76 mbedtls_md_type_t md_alg;
77 mbedtls_pk_type_t pk_alg;
Soby Mathew0a68d132017-05-31 10:35:27 +010078 mbedtls_pk_context pk = {0};
Juan Castilloa57a4d52015-04-02 15:44:20 +010079 int rc;
80 void *sig_opts = NULL;
Juan Castillobae6b2a2015-11-05 09:24:53 +000081 const mbedtls_md_info_t *md_info;
Juan Castilloa57a4d52015-04-02 15:44:20 +010082 unsigned char *p, *end;
Juan Castillobae6b2a2015-11-05 09:24:53 +000083 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Juan Castilloa57a4d52015-04-02 15:44:20 +010084
85 /* Get pointers to signature OID and parameters */
86 p = (unsigned char *)sig_alg;
87 end = (unsigned char *)(p + sig_alg_len);
Juan Castillobae6b2a2015-11-05 09:24:53 +000088 rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
Juan Castilloa57a4d52015-04-02 15:44:20 +010089 if (rc != 0) {
90 return CRYPTO_ERR_SIGNATURE;
91 }
92
93 /* Get the actual signature algorithm (MD + PK) */
Soby Mathew0a68d132017-05-31 10:35:27 +010094 rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
Juan Castilloa57a4d52015-04-02 15:44:20 +010095 if (rc != 0) {
96 return CRYPTO_ERR_SIGNATURE;
97 }
98
99 /* Parse the public key */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000100 mbedtls_pk_init(&pk);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100101 p = (unsigned char *)pk_ptr;
102 end = (unsigned char *)(p + pk_len);
Juan Castillobae6b2a2015-11-05 09:24:53 +0000103 rc = mbedtls_pk_parse_subpubkey(&p, end, &pk);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100104 if (rc != 0) {
Soby Mathew0a68d132017-05-31 10:35:27 +0100105 rc = CRYPTO_ERR_SIGNATURE;
106 goto end2;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100107 }
108
109 /* Get the signature (bitstring) */
110 p = (unsigned char *)sig_ptr;
111 end = (unsigned char *)(p + sig_len);
112 signature.tag = *p;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000113 rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100114 if (rc != 0) {
115 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100116 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100117 }
118 signature.p = p;
119
120 /* Calculate the hash of the data */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000121 md_info = mbedtls_md_info_from_type(md_alg);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100122 if (md_info == NULL) {
123 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100124 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100125 }
126 p = (unsigned char *)data_ptr;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000127 rc = mbedtls_md(md_info, p, data_len, hash);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100128 if (rc != 0) {
129 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100130 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100131 }
132
133 /* Verify the signature */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000134 rc = mbedtls_pk_verify_ext(pk_alg, sig_opts, &pk, md_alg, hash,
135 mbedtls_md_get_size(md_info),
136 signature.p, signature.len);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100137 if (rc != 0) {
138 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100139 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100140 }
141
142 /* Signature verification success */
143 rc = CRYPTO_SUCCESS;
144
Soby Mathew0a68d132017-05-31 10:35:27 +0100145end1:
Juan Castillobae6b2a2015-11-05 09:24:53 +0000146 mbedtls_pk_free(&pk);
Soby Mathew0a68d132017-05-31 10:35:27 +0100147end2:
148 mbedtls_free(sig_opts);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100149 return rc;
150}
151
152/*
153 * Match a hash
154 *
155 * Digest info is passed in DER format following the ASN.1 structure detailed
156 * above.
157 */
158static int verify_hash(void *data_ptr, unsigned int data_len,
159 void *digest_info_ptr, unsigned int digest_info_len)
160{
Juan Castillobae6b2a2015-11-05 09:24:53 +0000161 mbedtls_asn1_buf hash_oid, params;
162 mbedtls_md_type_t md_alg;
163 const mbedtls_md_info_t *md_info;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100164 unsigned char *p, *end, *hash;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000165 unsigned char data_hash[MBEDTLS_MD_MAX_SIZE];
Juan Castilloa57a4d52015-04-02 15:44:20 +0100166 size_t len;
167 int rc;
168
Juan Castillobae6b2a2015-11-05 09:24:53 +0000169 /* Digest info should be an MBEDTLS_ASN1_SEQUENCE */
Juan Castilloa57a4d52015-04-02 15:44:20 +0100170 p = (unsigned char *)digest_info_ptr;
Sandrine Bailleuxdf8de2d2016-01-04 15:49:23 +0000171 end = p + digest_info_len;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000172 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
173 MBEDTLS_ASN1_SEQUENCE);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100174 if (rc != 0) {
175 return CRYPTO_ERR_HASH;
176 }
177
178 /* Get the hash algorithm */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000179 rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100180 if (rc != 0) {
181 return CRYPTO_ERR_HASH;
182 }
183
Juan Castillobae6b2a2015-11-05 09:24:53 +0000184 rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100185 if (rc != 0) {
186 return CRYPTO_ERR_HASH;
187 }
188
Juan Castillobae6b2a2015-11-05 09:24:53 +0000189 md_info = mbedtls_md_info_from_type(md_alg);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100190 if (md_info == NULL) {
191 return CRYPTO_ERR_HASH;
192 }
193
194 /* Hash should be octet string type */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000195 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100196 if (rc != 0) {
197 return CRYPTO_ERR_HASH;
198 }
199
200 /* Length of hash must match the algorithm's size */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000201 if (len != mbedtls_md_get_size(md_info)) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100202 return CRYPTO_ERR_HASH;
203 }
204 hash = p;
205
206 /* Calculate the hash of the data */
207 p = (unsigned char *)data_ptr;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000208 rc = mbedtls_md(md_info, p, data_len, data_hash);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100209 if (rc != 0) {
210 return CRYPTO_ERR_HASH;
211 }
212
213 /* Compare values */
Antonio Nino Diaz0ca1afa2017-02-09 10:26:54 +0000214 rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info));
Juan Castilloa57a4d52015-04-02 15:44:20 +0100215 if (rc != 0) {
216 return CRYPTO_ERR_HASH;
217 }
218
219 return CRYPTO_SUCCESS;
220}
221
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000222#if MEASURED_BOOT
223/*
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100224 * Map a generic crypto message digest algorithm to the corresponding macro used
225 * by Mbed TLS.
226 */
227static inline mbedtls_md_type_t md_type(enum crypto_md_algo algo)
228{
229 switch (algo) {
230 case CRYPTO_MD_SHA512:
231 return MBEDTLS_MD_SHA512;
232 case CRYPTO_MD_SHA384:
233 return MBEDTLS_MD_SHA384;
234 case CRYPTO_MD_SHA256:
235 return MBEDTLS_MD_SHA256;
236 default:
237 /* Invalid hash algorithm. */
238 return MBEDTLS_MD_NONE;
239 }
240}
241
242/*
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000243 * Calculate a hash
244 *
245 * output points to the computed hash
246 */
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100247static int calc_hash(enum crypto_md_algo md_algo, void *data_ptr,
248 unsigned int data_len,
249 unsigned char output[CRYPTO_MD_MAX_SIZE])
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000250{
251 const mbedtls_md_info_t *md_info;
252
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100253 md_info = mbedtls_md_info_from_type(md_type(md_algo));
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000254 if (md_info == NULL) {
255 return CRYPTO_ERR_HASH;
256 }
257
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100258 /*
259 * Calculate the hash of the data, it is safe to pass the
260 * 'output' hash buffer pointer considering its size is always
261 * bigger than or equal to MBEDTLS_MD_MAX_SIZE.
262 */
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000263 return mbedtls_md(md_info, data_ptr, data_len, output);
264}
265#endif /* MEASURED_BOOT */
266
Sumit Garg392e4df2019-11-15 10:43:00 +0530267#if TF_MBEDTLS_USE_AES_GCM
268/*
269 * Stack based buffer allocation for decryption operation. It could
270 * be configured to balance stack usage vs execution speed.
271 */
272#define DEC_OP_BUF_SIZE 128
273
274static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
275 unsigned int key_len, const void *iv,
276 unsigned int iv_len, const void *tag,
277 unsigned int tag_len)
278{
279 mbedtls_gcm_context ctx;
280 mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
281 unsigned char buf[DEC_OP_BUF_SIZE];
282 unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
283 unsigned char *pt = data_ptr;
284 size_t dec_len;
285 int diff, i, rc;
286
287 mbedtls_gcm_init(&ctx);
288
289 rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
290 if (rc != 0) {
291 rc = CRYPTO_ERR_DECRYPTION;
292 goto exit_gcm;
293 }
294
295 rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
296 if (rc != 0) {
297 rc = CRYPTO_ERR_DECRYPTION;
298 goto exit_gcm;
299 }
300
301 while (len > 0) {
302 dec_len = MIN(sizeof(buf), len);
303
304 rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
305 if (rc != 0) {
306 rc = CRYPTO_ERR_DECRYPTION;
307 goto exit_gcm;
308 }
309
310 memcpy(pt, buf, dec_len);
311 pt += dec_len;
312 len -= dec_len;
313 }
314
315 rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
316 if (rc != 0) {
317 rc = CRYPTO_ERR_DECRYPTION;
318 goto exit_gcm;
319 }
320
321 /* Check tag in "constant-time" */
322 for (diff = 0, i = 0; i < tag_len; i++)
323 diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];
324
325 if (diff != 0) {
326 rc = CRYPTO_ERR_DECRYPTION;
327 goto exit_gcm;
328 }
329
330 /* GCM decryption success */
331 rc = CRYPTO_SUCCESS;
332
333exit_gcm:
334 mbedtls_gcm_free(&ctx);
335 return rc;
336}
337
338/*
339 * Authenticated decryption of an image
340 */
341static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
342 size_t len, const void *key, unsigned int key_len,
343 unsigned int key_flags, const void *iv,
344 unsigned int iv_len, const void *tag,
345 unsigned int tag_len)
346{
347 int rc;
348
349 assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);
350
351 switch (dec_algo) {
352 case CRYPTO_GCM_DECRYPT:
353 rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
354 tag, tag_len);
355 if (rc != 0)
356 return rc;
357 break;
358 default:
359 return CRYPTO_ERR_DECRYPTION;
360 }
361
362 return CRYPTO_SUCCESS;
363}
364#endif /* TF_MBEDTLS_USE_AES_GCM */
365
Juan Castilloa57a4d52015-04-02 15:44:20 +0100366/*
367 * Register crypto library descriptor
368 */
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000369#if MEASURED_BOOT
Sumit Garg392e4df2019-11-15 10:43:00 +0530370#if TF_MBEDTLS_USE_AES_GCM
371REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
372 auth_decrypt);
373#else
374REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
375 NULL);
376#endif
377#else /* MEASURED_BOOT */
378#if TF_MBEDTLS_USE_AES_GCM
379REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash,
380 auth_decrypt);
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000381#else
Sumit Garg392e4df2019-11-15 10:43:00 +0530382REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);
383#endif
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000384#endif /* MEASURED_BOOT */