blob: b13a460b4b52d60d26bae5b24eb8f7cf84baffc9 [file] [log] [blame]
Juan Castilloa57a4d52015-04-02 15:44:20 +01001/*
Govindraj Raja9c7dfb02023-01-11 18:34:58 +00002 * Copyright (c) 2015-2023, 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>
Govindraj Raja9c7dfb02023-01-11 18:34:58 +000017#include <mbedtls/version.h>
Madhukar Pappireddy57eaae82020-03-05 18:18:40 -060018#include <mbedtls/x509.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +010019
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <common/debug.h>
21#include <drivers/auth/crypto_mod.h>
22#include <drivers/auth/mbedtls/mbedtls_common.h>
Govindraj Raja9c7dfb02023-01-11 18:34:58 +000023
Sumit Garg392e4df2019-11-15 10:43:00 +053024#include <plat/common/platform.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000025
Juan Castillobae6b2a2015-11-05 09:24:53 +000026#define LIB_NAME "mbed TLS"
Juan Castilloa57a4d52015-04-02 15:44:20 +010027
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +010028#if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
29CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Manish V Badarkhee112a5a2021-10-06 23:41:50 +010030/*
31 * CRYPTO_MD_MAX_SIZE value is as per current stronger algorithm available
32 * so make sure that mbed TLS MD maximum size must be lesser than this.
33 */
34CASSERT(CRYPTO_MD_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE,
35 assert_mbedtls_md_size_overflow);
36
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +010037#endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
38 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
Manish V Badarkhee112a5a2021-10-06 23:41:50 +010039
Juan Castilloa57a4d52015-04-02 15:44:20 +010040/*
41 * AlgorithmIdentifier ::= SEQUENCE {
42 * algorithm OBJECT IDENTIFIER,
43 * parameters ANY DEFINED BY algorithm OPTIONAL
44 * }
45 *
46 * SubjectPublicKeyInfo ::= SEQUENCE {
47 * algorithm AlgorithmIdentifier,
48 * subjectPublicKey BIT STRING
49 * }
50 *
51 * DigestInfo ::= SEQUENCE {
52 * digestAlgorithm AlgorithmIdentifier,
53 * digest OCTET STRING
54 * }
55 */
56
57/*
58 * Initialize the library and export the descriptor
59 */
60static void init(void)
61{
Juan Castillobae6b2a2015-11-05 09:24:53 +000062 /* Initialize mbed TLS */
Juan Castilloa57a4d52015-04-02 15:44:20 +010063 mbedtls_init();
64}
65
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +010066#if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
67CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Juan Castilloa57a4d52015-04-02 15:44:20 +010068/*
69 * Verify a signature.
70 *
71 * Parameters are passed using the DER encoding format following the ASN.1
72 * structures detailed above.
73 */
74static int verify_signature(void *data_ptr, unsigned int data_len,
75 void *sig_ptr, unsigned int sig_len,
76 void *sig_alg, unsigned int sig_alg_len,
77 void *pk_ptr, unsigned int pk_len)
78{
Juan Castillobae6b2a2015-11-05 09:24:53 +000079 mbedtls_asn1_buf sig_oid, sig_params;
80 mbedtls_asn1_buf signature;
81 mbedtls_md_type_t md_alg;
82 mbedtls_pk_type_t pk_alg;
Soby Mathew0a68d132017-05-31 10:35:27 +010083 mbedtls_pk_context pk = {0};
Juan Castilloa57a4d52015-04-02 15:44:20 +010084 int rc;
85 void *sig_opts = NULL;
Juan Castillobae6b2a2015-11-05 09:24:53 +000086 const mbedtls_md_info_t *md_info;
Juan Castilloa57a4d52015-04-02 15:44:20 +010087 unsigned char *p, *end;
Juan Castillobae6b2a2015-11-05 09:24:53 +000088 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Juan Castilloa57a4d52015-04-02 15:44:20 +010089
90 /* Get pointers to signature OID and parameters */
91 p = (unsigned char *)sig_alg;
92 end = (unsigned char *)(p + sig_alg_len);
Juan Castillobae6b2a2015-11-05 09:24:53 +000093 rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
Juan Castilloa57a4d52015-04-02 15:44:20 +010094 if (rc != 0) {
95 return CRYPTO_ERR_SIGNATURE;
96 }
97
98 /* Get the actual signature algorithm (MD + PK) */
Soby Mathew0a68d132017-05-31 10:35:27 +010099 rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100100 if (rc != 0) {
101 return CRYPTO_ERR_SIGNATURE;
102 }
103
104 /* Parse the public key */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000105 mbedtls_pk_init(&pk);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100106 p = (unsigned char *)pk_ptr;
107 end = (unsigned char *)(p + pk_len);
Juan Castillobae6b2a2015-11-05 09:24:53 +0000108 rc = mbedtls_pk_parse_subpubkey(&p, end, &pk);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100109 if (rc != 0) {
Soby Mathew0a68d132017-05-31 10:35:27 +0100110 rc = CRYPTO_ERR_SIGNATURE;
111 goto end2;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100112 }
113
114 /* Get the signature (bitstring) */
115 p = (unsigned char *)sig_ptr;
116 end = (unsigned char *)(p + sig_len);
117 signature.tag = *p;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000118 rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
Demi Marie Obenour447adc02022-12-08 15:24:10 -0500119 if ((rc != 0) || ((size_t)(end - p) != signature.len)) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100120 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100121 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100122 }
123 signature.p = p;
124
125 /* Calculate the hash of the data */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000126 md_info = mbedtls_md_info_from_type(md_alg);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100127 if (md_info == NULL) {
128 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100129 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100130 }
131 p = (unsigned char *)data_ptr;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000132 rc = mbedtls_md(md_info, p, data_len, hash);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100133 if (rc != 0) {
134 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100135 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100136 }
137
138 /* Verify the signature */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000139 rc = mbedtls_pk_verify_ext(pk_alg, sig_opts, &pk, md_alg, hash,
140 mbedtls_md_get_size(md_info),
141 signature.p, signature.len);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100142 if (rc != 0) {
143 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew0a68d132017-05-31 10:35:27 +0100144 goto end1;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100145 }
146
147 /* Signature verification success */
148 rc = CRYPTO_SUCCESS;
149
Soby Mathew0a68d132017-05-31 10:35:27 +0100150end1:
Juan Castillobae6b2a2015-11-05 09:24:53 +0000151 mbedtls_pk_free(&pk);
Soby Mathew0a68d132017-05-31 10:35:27 +0100152end2:
153 mbedtls_free(sig_opts);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100154 return rc;
155}
156
157/*
158 * Match a hash
159 *
160 * Digest info is passed in DER format following the ASN.1 structure detailed
161 * above.
162 */
163static int verify_hash(void *data_ptr, unsigned int data_len,
164 void *digest_info_ptr, unsigned int digest_info_len)
165{
Juan Castillobae6b2a2015-11-05 09:24:53 +0000166 mbedtls_asn1_buf hash_oid, params;
167 mbedtls_md_type_t md_alg;
168 const mbedtls_md_info_t *md_info;
Juan Castilloa57a4d52015-04-02 15:44:20 +0100169 unsigned char *p, *end, *hash;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000170 unsigned char data_hash[MBEDTLS_MD_MAX_SIZE];
Juan Castilloa57a4d52015-04-02 15:44:20 +0100171 size_t len;
172 int rc;
173
Demi Marie Obenourae266642022-12-08 15:24:01 -0500174 /*
175 * Digest info should be an MBEDTLS_ASN1_SEQUENCE
176 * and consume all bytes.
177 */
Juan Castilloa57a4d52015-04-02 15:44:20 +0100178 p = (unsigned char *)digest_info_ptr;
Sandrine Bailleuxdf8de2d2016-01-04 15:49:23 +0000179 end = p + digest_info_len;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000180 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
181 MBEDTLS_ASN1_SEQUENCE);
Demi Marie Obenourae266642022-12-08 15:24:01 -0500182 if (rc != 0 || ((size_t)(end - p) != len)) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100183 return CRYPTO_ERR_HASH;
184 }
185
186 /* Get the hash algorithm */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000187 rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100188 if (rc != 0) {
189 return CRYPTO_ERR_HASH;
190 }
191
Juan Castillobae6b2a2015-11-05 09:24:53 +0000192 rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100193 if (rc != 0) {
194 return CRYPTO_ERR_HASH;
195 }
196
Juan Castillobae6b2a2015-11-05 09:24:53 +0000197 md_info = mbedtls_md_info_from_type(md_alg);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100198 if (md_info == NULL) {
199 return CRYPTO_ERR_HASH;
200 }
201
Demi Marie Obenourae266642022-12-08 15:24:01 -0500202 /* Hash should be octet string type and consume all bytes */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000203 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
Demi Marie Obenourae266642022-12-08 15:24:01 -0500204 if ((rc != 0) || ((size_t)(end - p) != len)) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100205 return CRYPTO_ERR_HASH;
206 }
207
208 /* Length of hash must match the algorithm's size */
Juan Castillobae6b2a2015-11-05 09:24:53 +0000209 if (len != mbedtls_md_get_size(md_info)) {
Juan Castilloa57a4d52015-04-02 15:44:20 +0100210 return CRYPTO_ERR_HASH;
211 }
212 hash = p;
213
214 /* Calculate the hash of the data */
215 p = (unsigned char *)data_ptr;
Juan Castillobae6b2a2015-11-05 09:24:53 +0000216 rc = mbedtls_md(md_info, p, data_len, data_hash);
Juan Castilloa57a4d52015-04-02 15:44:20 +0100217 if (rc != 0) {
218 return CRYPTO_ERR_HASH;
219 }
220
221 /* Compare values */
Antonio Nino Diaz0ca1afa2017-02-09 10:26:54 +0000222 rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info));
Juan Castilloa57a4d52015-04-02 15:44:20 +0100223 if (rc != 0) {
224 return CRYPTO_ERR_HASH;
225 }
226
227 return CRYPTO_SUCCESS;
228}
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100229#endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
230 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
Juan Castilloa57a4d52015-04-02 15:44:20 +0100231
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100232#if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
233CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000234/*
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100235 * Map a generic crypto message digest algorithm to the corresponding macro used
236 * by Mbed TLS.
237 */
238static inline mbedtls_md_type_t md_type(enum crypto_md_algo algo)
239{
240 switch (algo) {
241 case CRYPTO_MD_SHA512:
242 return MBEDTLS_MD_SHA512;
243 case CRYPTO_MD_SHA384:
244 return MBEDTLS_MD_SHA384;
245 case CRYPTO_MD_SHA256:
246 return MBEDTLS_MD_SHA256;
247 default:
248 /* Invalid hash algorithm. */
249 return MBEDTLS_MD_NONE;
250 }
251}
252
253/*
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000254 * Calculate a hash
255 *
256 * output points to the computed hash
257 */
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100258static int calc_hash(enum crypto_md_algo md_algo, void *data_ptr,
259 unsigned int data_len,
260 unsigned char output[CRYPTO_MD_MAX_SIZE])
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000261{
262 const mbedtls_md_info_t *md_info;
263
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100264 md_info = mbedtls_md_info_from_type(md_type(md_algo));
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000265 if (md_info == NULL) {
266 return CRYPTO_ERR_HASH;
267 }
268
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100269 /*
270 * Calculate the hash of the data, it is safe to pass the
271 * 'output' hash buffer pointer considering its size is always
272 * bigger than or equal to MBEDTLS_MD_MAX_SIZE.
273 */
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000274 return mbedtls_md(md_info, data_ptr, data_len, output);
275}
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100276#endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
277 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000278
Sumit Garg392e4df2019-11-15 10:43:00 +0530279#if TF_MBEDTLS_USE_AES_GCM
280/*
281 * Stack based buffer allocation for decryption operation. It could
282 * be configured to balance stack usage vs execution speed.
283 */
284#define DEC_OP_BUF_SIZE 128
285
286static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
287 unsigned int key_len, const void *iv,
288 unsigned int iv_len, const void *tag,
289 unsigned int tag_len)
290{
291 mbedtls_gcm_context ctx;
292 mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
293 unsigned char buf[DEC_OP_BUF_SIZE];
294 unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
295 unsigned char *pt = data_ptr;
296 size_t dec_len;
297 int diff, i, rc;
298
299 mbedtls_gcm_init(&ctx);
300
301 rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
302 if (rc != 0) {
303 rc = CRYPTO_ERR_DECRYPTION;
304 goto exit_gcm;
305 }
306
307 rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
308 if (rc != 0) {
309 rc = CRYPTO_ERR_DECRYPTION;
310 goto exit_gcm;
311 }
312
313 while (len > 0) {
314 dec_len = MIN(sizeof(buf), len);
315
316 rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
317 if (rc != 0) {
318 rc = CRYPTO_ERR_DECRYPTION;
319 goto exit_gcm;
320 }
321
322 memcpy(pt, buf, dec_len);
323 pt += dec_len;
324 len -= dec_len;
325 }
326
327 rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
328 if (rc != 0) {
329 rc = CRYPTO_ERR_DECRYPTION;
330 goto exit_gcm;
331 }
332
333 /* Check tag in "constant-time" */
334 for (diff = 0, i = 0; i < tag_len; i++)
335 diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];
336
337 if (diff != 0) {
338 rc = CRYPTO_ERR_DECRYPTION;
339 goto exit_gcm;
340 }
341
342 /* GCM decryption success */
343 rc = CRYPTO_SUCCESS;
344
345exit_gcm:
346 mbedtls_gcm_free(&ctx);
347 return rc;
348}
349
350/*
351 * Authenticated decryption of an image
352 */
353static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
354 size_t len, const void *key, unsigned int key_len,
355 unsigned int key_flags, const void *iv,
356 unsigned int iv_len, const void *tag,
357 unsigned int tag_len)
358{
359 int rc;
360
361 assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);
362
363 switch (dec_algo) {
364 case CRYPTO_GCM_DECRYPT:
365 rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
366 tag, tag_len);
367 if (rc != 0)
368 return rc;
369 break;
370 default:
371 return CRYPTO_ERR_DECRYPTION;
372 }
373
374 return CRYPTO_SUCCESS;
375}
376#endif /* TF_MBEDTLS_USE_AES_GCM */
377
Juan Castilloa57a4d52015-04-02 15:44:20 +0100378/*
379 * Register crypto library descriptor
380 */
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100381#if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Sumit Garg392e4df2019-11-15 10:43:00 +0530382#if TF_MBEDTLS_USE_AES_GCM
383REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
384 auth_decrypt);
385#else
386REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
387 NULL);
388#endif
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100389#elif CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY
Sumit Garg392e4df2019-11-15 10:43:00 +0530390#if TF_MBEDTLS_USE_AES_GCM
391REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash,
392 auth_decrypt);
Alexei Fedorov913cb7e2020-01-23 14:27:38 +0000393#else
Sumit Garg392e4df2019-11-15 10:43:00 +0530394REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);
395#endif
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100396#elif CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY
Manish V Badarkhe92de80a2021-12-16 10:41:47 +0000397REGISTER_CRYPTO_LIB(LIB_NAME, init, calc_hash);
Manish V Badarkhec9fdaf62022-06-20 15:32:38 +0100398#endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */