blob: 991f68cdc8ff9cd1ff4ccabefdc88906357bbc02 [file] [log] [blame]
Juan Castillo7d37aa12015-04-02 15:44:20 +01001/*
Govindraj Rajad7f61812023-01-11 18:34:58 +00002 * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
Juan Castillo7d37aa12015-04-02 15:44:20 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo7d37aa12015-04-02 15:44:20 +01005 */
6
Sumit Garg7cda17b2019-11-15 10:43:00 +05307#include <assert.h>
Juan Castillo7d37aa12015-04-02 15:44:20 +01008#include <stddef.h>
9#include <string.h>
10
Juan Castillo649dbf62015-11-05 09:24:53 +000011/* mbed TLS headers */
Sumit Garg7cda17b2019-11-15 10:43:00 +053012#include <mbedtls/gcm.h>
Juan Castillo649dbf62015-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 Rajad7f61812023-01-11 18:34:58 +000017#include <mbedtls/version.h>
Madhukar Pappireddy93ee2792020-03-05 18:18:40 -060018#include <mbedtls/x509.h>
Juan Castillo7d37aa12015-04-02 15:44:20 +010019
Antonio Nino Diaz09d40e02018-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 Rajad7f61812023-01-11 18:34:58 +000023
Sumit Garg7cda17b2019-11-15 10:43:00 +053024#include <plat/common/platform.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000025
Juan Castillo649dbf62015-11-05 09:24:53 +000026#define LIB_NAME "mbed TLS"
Juan Castillo7d37aa12015-04-02 15:44:20 +010027
Manish V Badarkhe2bf4f272022-06-20 15:32:38 +010028#if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
29CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Manish V Badarkhe14db9632021-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 Badarkhe2bf4f272022-06-20 15:32:38 +010037#endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
38 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
Manish V Badarkhe14db9632021-10-06 23:41:50 +010039
Juan Castillo7d37aa12015-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 Castillo649dbf62015-11-05 09:24:53 +000062 /* Initialize mbed TLS */
Juan Castillo7d37aa12015-04-02 15:44:20 +010063 mbedtls_init();
64}
65
Manish V Badarkhe2bf4f272022-06-20 15:32:38 +010066#if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
67CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Juan Castillo7d37aa12015-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 Castillo649dbf62015-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 Mathew10012022017-05-31 10:35:27 +010083 mbedtls_pk_context pk = {0};
Juan Castillo7d37aa12015-04-02 15:44:20 +010084 int rc;
85 void *sig_opts = NULL;
Juan Castillo649dbf62015-11-05 09:24:53 +000086 const mbedtls_md_info_t *md_info;
Juan Castillo7d37aa12015-04-02 15:44:20 +010087 unsigned char *p, *end;
Juan Castillo649dbf62015-11-05 09:24:53 +000088 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Juan Castillo7d37aa12015-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 Castillo649dbf62015-11-05 09:24:53 +000093 rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
Juan Castillo7d37aa12015-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 Mathew10012022017-05-31 10:35:27 +010099 rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100100 if (rc != 0) {
101 return CRYPTO_ERR_SIGNATURE;
102 }
103
104 /* Parse the public key */
Juan Castillo649dbf62015-11-05 09:24:53 +0000105 mbedtls_pk_init(&pk);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100106 p = (unsigned char *)pk_ptr;
107 end = (unsigned char *)(p + pk_len);
Juan Castillo649dbf62015-11-05 09:24:53 +0000108 rc = mbedtls_pk_parse_subpubkey(&p, end, &pk);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100109 if (rc != 0) {
Soby Mathew10012022017-05-31 10:35:27 +0100110 rc = CRYPTO_ERR_SIGNATURE;
111 goto end2;
Juan Castillo7d37aa12015-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 Castillo649dbf62015-11-05 09:24:53 +0000118 rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100119 if (rc != 0) {
120 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew10012022017-05-31 10:35:27 +0100121 goto end1;
Juan Castillo7d37aa12015-04-02 15:44:20 +0100122 }
123 signature.p = p;
124
125 /* Calculate the hash of the data */
Juan Castillo649dbf62015-11-05 09:24:53 +0000126 md_info = mbedtls_md_info_from_type(md_alg);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100127 if (md_info == NULL) {
128 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew10012022017-05-31 10:35:27 +0100129 goto end1;
Juan Castillo7d37aa12015-04-02 15:44:20 +0100130 }
131 p = (unsigned char *)data_ptr;
Juan Castillo649dbf62015-11-05 09:24:53 +0000132 rc = mbedtls_md(md_info, p, data_len, hash);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100133 if (rc != 0) {
134 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew10012022017-05-31 10:35:27 +0100135 goto end1;
Juan Castillo7d37aa12015-04-02 15:44:20 +0100136 }
137
138 /* Verify the signature */
Juan Castillo649dbf62015-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 Castillo7d37aa12015-04-02 15:44:20 +0100142 if (rc != 0) {
143 rc = CRYPTO_ERR_SIGNATURE;
Soby Mathew10012022017-05-31 10:35:27 +0100144 goto end1;
Juan Castillo7d37aa12015-04-02 15:44:20 +0100145 }
146
147 /* Signature verification success */
148 rc = CRYPTO_SUCCESS;
149
Soby Mathew10012022017-05-31 10:35:27 +0100150end1:
Juan Castillo649dbf62015-11-05 09:24:53 +0000151 mbedtls_pk_free(&pk);
Soby Mathew10012022017-05-31 10:35:27 +0100152end2:
153 mbedtls_free(sig_opts);
Juan Castillo7d37aa12015-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 Castillo649dbf62015-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 Castillo7d37aa12015-04-02 15:44:20 +0100169 unsigned char *p, *end, *hash;
Juan Castillo649dbf62015-11-05 09:24:53 +0000170 unsigned char data_hash[MBEDTLS_MD_MAX_SIZE];
Juan Castillo7d37aa12015-04-02 15:44:20 +0100171 size_t len;
172 int rc;
173
Juan Castillo649dbf62015-11-05 09:24:53 +0000174 /* Digest info should be an MBEDTLS_ASN1_SEQUENCE */
Juan Castillo7d37aa12015-04-02 15:44:20 +0100175 p = (unsigned char *)digest_info_ptr;
Sandrine Bailleuxaa856912016-01-04 15:49:23 +0000176 end = p + digest_info_len;
Juan Castillo649dbf62015-11-05 09:24:53 +0000177 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
178 MBEDTLS_ASN1_SEQUENCE);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100179 if (rc != 0) {
180 return CRYPTO_ERR_HASH;
181 }
182
183 /* Get the hash algorithm */
Juan Castillo649dbf62015-11-05 09:24:53 +0000184 rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100185 if (rc != 0) {
186 return CRYPTO_ERR_HASH;
187 }
188
Juan Castillo649dbf62015-11-05 09:24:53 +0000189 rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100190 if (rc != 0) {
191 return CRYPTO_ERR_HASH;
192 }
193
Juan Castillo649dbf62015-11-05 09:24:53 +0000194 md_info = mbedtls_md_info_from_type(md_alg);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100195 if (md_info == NULL) {
196 return CRYPTO_ERR_HASH;
197 }
198
199 /* Hash should be octet string type */
Juan Castillo649dbf62015-11-05 09:24:53 +0000200 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100201 if (rc != 0) {
202 return CRYPTO_ERR_HASH;
203 }
204
205 /* Length of hash must match the algorithm's size */
Juan Castillo649dbf62015-11-05 09:24:53 +0000206 if (len != mbedtls_md_get_size(md_info)) {
Juan Castillo7d37aa12015-04-02 15:44:20 +0100207 return CRYPTO_ERR_HASH;
208 }
209 hash = p;
210
211 /* Calculate the hash of the data */
212 p = (unsigned char *)data_ptr;
Juan Castillo649dbf62015-11-05 09:24:53 +0000213 rc = mbedtls_md(md_info, p, data_len, data_hash);
Juan Castillo7d37aa12015-04-02 15:44:20 +0100214 if (rc != 0) {
215 return CRYPTO_ERR_HASH;
216 }
217
218 /* Compare values */
Antonio Nino Diazfabd21a2017-02-09 10:26:54 +0000219 rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info));
Juan Castillo7d37aa12015-04-02 15:44:20 +0100220 if (rc != 0) {
221 return CRYPTO_ERR_HASH;
222 }
223
224 return CRYPTO_SUCCESS;
225}
Manish V Badarkhe2bf4f272022-06-20 15:32:38 +0100226#endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
227 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
Juan Castillo7d37aa12015-04-02 15:44:20 +0100228
Manish V Badarkhe2bf4f272022-06-20 15:32:38 +0100229#if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
230CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Alexei Fedorov8c105292020-01-23 14:27:38 +0000231/*
Manish V Badarkhe14db9632021-10-06 23:41:50 +0100232 * Map a generic crypto message digest algorithm to the corresponding macro used
233 * by Mbed TLS.
234 */
235static inline mbedtls_md_type_t md_type(enum crypto_md_algo algo)
236{
237 switch (algo) {
238 case CRYPTO_MD_SHA512:
239 return MBEDTLS_MD_SHA512;
240 case CRYPTO_MD_SHA384:
241 return MBEDTLS_MD_SHA384;
242 case CRYPTO_MD_SHA256:
243 return MBEDTLS_MD_SHA256;
244 default:
245 /* Invalid hash algorithm. */
246 return MBEDTLS_MD_NONE;
247 }
248}
249
250/*
Alexei Fedorov8c105292020-01-23 14:27:38 +0000251 * Calculate a hash
252 *
253 * output points to the computed hash
254 */
Manish V Badarkhe14db9632021-10-06 23:41:50 +0100255static int calc_hash(enum crypto_md_algo md_algo, void *data_ptr,
256 unsigned int data_len,
257 unsigned char output[CRYPTO_MD_MAX_SIZE])
Alexei Fedorov8c105292020-01-23 14:27:38 +0000258{
259 const mbedtls_md_info_t *md_info;
260
Manish V Badarkhe14db9632021-10-06 23:41:50 +0100261 md_info = mbedtls_md_info_from_type(md_type(md_algo));
Alexei Fedorov8c105292020-01-23 14:27:38 +0000262 if (md_info == NULL) {
263 return CRYPTO_ERR_HASH;
264 }
265
Manish V Badarkhe14db9632021-10-06 23:41:50 +0100266 /*
267 * Calculate the hash of the data, it is safe to pass the
268 * 'output' hash buffer pointer considering its size is always
269 * bigger than or equal to MBEDTLS_MD_MAX_SIZE.
270 */
Alexei Fedorov8c105292020-01-23 14:27:38 +0000271 return mbedtls_md(md_info, data_ptr, data_len, output);
272}
Manish V Badarkhe2bf4f272022-06-20 15:32:38 +0100273#endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
274 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
Alexei Fedorov8c105292020-01-23 14:27:38 +0000275
Sumit Garg7cda17b2019-11-15 10:43:00 +0530276#if TF_MBEDTLS_USE_AES_GCM
277/*
278 * Stack based buffer allocation for decryption operation. It could
279 * be configured to balance stack usage vs execution speed.
280 */
281#define DEC_OP_BUF_SIZE 128
282
283static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
284 unsigned int key_len, const void *iv,
285 unsigned int iv_len, const void *tag,
286 unsigned int tag_len)
287{
288 mbedtls_gcm_context ctx;
289 mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
290 unsigned char buf[DEC_OP_BUF_SIZE];
291 unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
292 unsigned char *pt = data_ptr;
293 size_t dec_len;
294 int diff, i, rc;
Yann Gautierfd566c32024-01-19 17:44:41 +0100295 size_t output_length __unused;
Sumit Garg7cda17b2019-11-15 10:43:00 +0530296
297 mbedtls_gcm_init(&ctx);
298
299 rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
300 if (rc != 0) {
301 rc = CRYPTO_ERR_DECRYPTION;
302 goto exit_gcm;
303 }
304
Yann Gautierfd566c32024-01-19 17:44:41 +0100305#if (MBEDTLS_VERSION_MAJOR < 3)
Sumit Garg7cda17b2019-11-15 10:43:00 +0530306 rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
Yann Gautierfd566c32024-01-19 17:44:41 +0100307#else
308 rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len);
309#endif
Sumit Garg7cda17b2019-11-15 10:43:00 +0530310 if (rc != 0) {
311 rc = CRYPTO_ERR_DECRYPTION;
312 goto exit_gcm;
313 }
314
315 while (len > 0) {
316 dec_len = MIN(sizeof(buf), len);
317
Yann Gautierfd566c32024-01-19 17:44:41 +0100318#if (MBEDTLS_VERSION_MAJOR < 3)
Sumit Garg7cda17b2019-11-15 10:43:00 +0530319 rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
Yann Gautierfd566c32024-01-19 17:44:41 +0100320#else
321 rc = mbedtls_gcm_update(&ctx, pt, dec_len, buf, sizeof(buf), &output_length);
322#endif
323
Sumit Garg7cda17b2019-11-15 10:43:00 +0530324 if (rc != 0) {
325 rc = CRYPTO_ERR_DECRYPTION;
326 goto exit_gcm;
327 }
328
329 memcpy(pt, buf, dec_len);
330 pt += dec_len;
331 len -= dec_len;
332 }
333
Yann Gautierfd566c32024-01-19 17:44:41 +0100334#if (MBEDTLS_VERSION_MAJOR < 3)
Sumit Garg7cda17b2019-11-15 10:43:00 +0530335 rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
Yann Gautierfd566c32024-01-19 17:44:41 +0100336#else
337 rc = mbedtls_gcm_finish(&ctx, NULL, 0, &output_length, tag_buf, sizeof(tag_buf));
338#endif
339
Sumit Garg7cda17b2019-11-15 10:43:00 +0530340 if (rc != 0) {
341 rc = CRYPTO_ERR_DECRYPTION;
342 goto exit_gcm;
343 }
344
345 /* Check tag in "constant-time" */
346 for (diff = 0, i = 0; i < tag_len; i++)
347 diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];
348
349 if (diff != 0) {
350 rc = CRYPTO_ERR_DECRYPTION;
351 goto exit_gcm;
352 }
353
354 /* GCM decryption success */
355 rc = CRYPTO_SUCCESS;
356
357exit_gcm:
358 mbedtls_gcm_free(&ctx);
359 return rc;
360}
361
362/*
363 * Authenticated decryption of an image
364 */
365static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
366 size_t len, const void *key, unsigned int key_len,
367 unsigned int key_flags, const void *iv,
368 unsigned int iv_len, const void *tag,
369 unsigned int tag_len)
370{
371 int rc;
372
373 assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);
374
375 switch (dec_algo) {
376 case CRYPTO_GCM_DECRYPT:
377 rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
378 tag, tag_len);
379 if (rc != 0)
380 return rc;
381 break;
382 default:
383 return CRYPTO_ERR_DECRYPTION;
384 }
385
386 return CRYPTO_SUCCESS;
387}
388#endif /* TF_MBEDTLS_USE_AES_GCM */
389
Juan Castillo7d37aa12015-04-02 15:44:20 +0100390/*
391 * Register crypto library descriptor
392 */
Manish V Badarkhe2bf4f272022-06-20 15:32:38 +0100393#if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Sumit Garg7cda17b2019-11-15 10:43:00 +0530394#if TF_MBEDTLS_USE_AES_GCM
395REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
396 auth_decrypt);
Alexei Fedorov8c105292020-01-23 14:27:38 +0000397#else
Sumit Garg7cda17b2019-11-15 10:43:00 +0530398REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
399 NULL);
400#endif
Manish V Badarkhe2bf4f272022-06-20 15:32:38 +0100401#elif CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY
Sumit Garg7cda17b2019-11-15 10:43:00 +0530402#if TF_MBEDTLS_USE_AES_GCM
403REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash,
404 auth_decrypt);
405#else
406REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);
407#endif
Manish V Badarkhe2bf4f272022-06-20 15:32:38 +0100408#elif CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY
Manish V Badarkhe0aa0b3a2021-12-16 10:41:47 +0000409REGISTER_CRYPTO_LIB(LIB_NAME, init, calc_hash);
Manish V Badarkhe2bf4f272022-06-20 15:32:38 +0100410#endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */