xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_psa_crypto.c (revision 7731465252bd82ce97620a327f3b5d8905f8bdb1)
1 /*
2  * Copyright (c) 2023-2024, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stddef.h>
9 #include <string.h>
10 
11 /* mbed TLS headers */
12 #include <mbedtls/gcm.h>
13 #include <mbedtls/md.h>
14 #include <mbedtls/memory_buffer_alloc.h>
15 #include <mbedtls/oid.h>
16 #include <mbedtls/platform.h>
17 #include <mbedtls/psa_util.h>
18 #include <mbedtls/version.h>
19 #include <mbedtls/x509.h>
20 #include <psa/crypto.h>
21 #include <psa/crypto_platform.h>
22 #include <psa/crypto_types.h>
23 #include <psa/crypto_values.h>
24 
25 #include <common/debug.h>
26 #include <drivers/auth/crypto_mod.h>
27 #include <drivers/auth/mbedtls/mbedtls_common.h>
28 #include <plat/common/platform.h>
29 
30 #define LIB_NAME		"mbed TLS PSA"
31 
32 /* Maximum length of R_S pair in the ECDSA signature in bytes */
33 #define MAX_ECDSA_R_S_PAIR_LEN	64U
34 
35 /* Size of ASN.1 length and tag in bytes*/
36 #define SIZE_OF_ASN1_LEN	1U
37 #define SIZE_OF_ASN1_TAG	1U
38 
39 #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
40 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
41 /*
42  * CRYPTO_MD_MAX_SIZE value is as per current stronger algorithm available
43  * so make sure that mbed TLS MD maximum size must be lesser than this.
44  */
45 CASSERT(CRYPTO_MD_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE,
46 	assert_mbedtls_md_size_overflow);
47 
48 #endif /*
49 	* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
50 	* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
51 	*/
52 
53 /*
54  * AlgorithmIdentifier  ::=  SEQUENCE  {
55  *     algorithm               OBJECT IDENTIFIER,
56  *     parameters              ANY DEFINED BY algorithm OPTIONAL
57  * }
58  *
59  * SubjectPublicKeyInfo  ::=  SEQUENCE  {
60  *     algorithm            AlgorithmIdentifier,
61  *     subjectPublicKey     BIT STRING
62  * }
63  *
64  * DigestInfo ::= SEQUENCE {
65  *     digestAlgorithm AlgorithmIdentifier,
66  *     digest OCTET STRING
67  * }
68  */
69 
70 /*
71  * We pretend using an external RNG (through MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
72  * mbedTLS config option) so we need to provide an implementation of
73  * mbedtls_psa_external_get_random(). Provide a fake one, since we do not
74  * actually have any external RNG and TF-A itself doesn't engage in
75  * cryptographic operations that demands randomness.
76  */
77 psa_status_t mbedtls_psa_external_get_random(
78 			mbedtls_psa_external_random_context_t *context,
79 			uint8_t *output, size_t output_size,
80 			size_t *output_length)
81 {
82 	return PSA_ERROR_INSUFFICIENT_ENTROPY;
83 }
84 
85 /*
86  * Initialize the library and export the descriptor
87  */
88 static void init(void)
89 {
90 	/* Initialize mbed TLS */
91 	mbedtls_init();
92 
93 	/* Initialise PSA mbedTLS */
94 	psa_status_t status = psa_crypto_init();
95 
96 	if (status != PSA_SUCCESS) {
97 		ERROR("Failed to initialize %s crypto (%d).\n", LIB_NAME, status);
98 		panic();
99 	}
100 
101 	INFO("PSA crypto initialized successfully!\n");
102 }
103 
104 #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
105 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
106 
107 static void construct_psa_key_alg_and_type(mbedtls_pk_type_t pk_alg,
108 					   mbedtls_md_type_t md_alg,
109 					   psa_ecc_family_t psa_ecc_family,
110 					   psa_algorithm_t *psa_alg,
111 					   psa_key_type_t *psa_key_type)
112 {
113 	psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
114 
115 	switch (pk_alg) {
116 	case MBEDTLS_PK_RSASSA_PSS:
117 		*psa_alg = PSA_ALG_RSA_PSS(psa_md_alg);
118 		*psa_key_type = PSA_KEY_TYPE_RSA_PUBLIC_KEY;
119 		break;
120 	case MBEDTLS_PK_ECDSA:
121 		*psa_alg = PSA_ALG_ECDSA(psa_md_alg);
122 		*psa_key_type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(psa_ecc_family);
123 		break;
124 	default:
125 		*psa_alg = PSA_ALG_NONE;
126 		*psa_key_type = PSA_KEY_TYPE_NONE;
127 		break;
128 	}
129 }
130 
131 
132 #if TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA || \
133 TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA
134 
135 /*
136  * This is a helper function to detect padding byte (if the MSB bit of the
137  * first data byte is set to 1, for example 0x80) and on detection, ignore the
138  * padded byte(0x00) and increase the buffer pointer beyond padded byte and
139  * decrease the length of the buffer by 1.
140  *
141  * On Success returns 0, error otherwise.
142  **/
143 static inline int ignore_asn1_int_padding_byte(unsigned char **buf_start,
144 					       size_t *buf_len)
145 {
146 	unsigned char *local_buf = *buf_start;
147 
148 	/* Check for negative number */
149 	if ((local_buf[0] & 0x80U) != 0U) {
150 		return -1;
151 	}
152 
153 	if ((local_buf[0] == 0U) && (local_buf[1] > 0x7FU) &&
154 	    (*buf_len > 1U)) {
155 		*buf_start = &local_buf[1];
156 		(*buf_len)--;
157 	}
158 
159 	return 0;
160 }
161 
162 /*
163  * This is a helper function that gets a pointer to the encoded ECDSA publicKey
164  * and its length (as per RFC5280) and returns corresponding decoded publicKey
165  * and its length. As well, it retrieves the family of ECC key in the PSA
166  * format.
167  *
168  * This function returns error(CRYPTO_ERR_SIGNATURE) on ASN.1 parsing failure,
169  * otherwise success(0).
170  **/
171 static int get_ecdsa_pkinfo_from_asn1(unsigned char **pk_start,
172 				      unsigned int *pk_len,
173 				      psa_ecc_family_t *psa_ecc_family)
174 {
175 	mbedtls_asn1_buf alg_oid, alg_params;
176 	mbedtls_ecp_group_id grp_id;
177 	int rc;
178 	unsigned char *pk_end;
179 	size_t len;
180 	size_t curve_bits;
181 	unsigned char *pk_ptr = *pk_start;
182 
183 	pk_end = pk_ptr + *pk_len;
184 	rc = mbedtls_asn1_get_tag(&pk_ptr, pk_end, &len,
185 				  MBEDTLS_ASN1_CONSTRUCTED |
186 				  MBEDTLS_ASN1_SEQUENCE);
187 	if (rc != 0) {
188 		return CRYPTO_ERR_SIGNATURE;
189 	}
190 
191 	pk_end = pk_ptr + len;
192 	rc = mbedtls_asn1_get_alg(&pk_ptr, pk_end, &alg_oid, &alg_params);
193 	if (rc != 0) {
194 		return CRYPTO_ERR_SIGNATURE;
195 	}
196 
197 	if (alg_params.tag == MBEDTLS_ASN1_OID) {
198 		if (mbedtls_oid_get_ec_grp(&alg_params, &grp_id) != 0) {
199 			return CRYPTO_ERR_SIGNATURE;
200 		}
201 		*psa_ecc_family = mbedtls_ecc_group_to_psa(grp_id,
202 							   &curve_bits);
203 	} else {
204 		return CRYPTO_ERR_SIGNATURE;
205 	}
206 
207 	pk_end = pk_ptr + len - (alg_oid.len + alg_params.len +
208 		 2 * (SIZE_OF_ASN1_LEN + SIZE_OF_ASN1_TAG));
209 	rc = mbedtls_asn1_get_bitstring_null(&pk_ptr, pk_end, &len);
210 	if (rc != 0) {
211 		return CRYPTO_ERR_SIGNATURE;
212 	}
213 
214 	*pk_start = pk_ptr;
215 	*pk_len = len;
216 
217 	return rc;
218 }
219 
220 /*
221  * Ecdsa-Sig-Value  ::=  SEQUENCE  {
222  *   r     INTEGER,
223  *   s     INTEGER
224  * }
225  *
226  * This helper function that gets a pointer to the encoded ECDSA signature and
227  * its length (as per RFC5280) and returns corresponding decoded signature
228  * (R_S pair) and its size.
229  *
230  * This function returns error(CRYPTO_ERR_SIGNATURE) on ASN.1 parsing failure,
231  * otherwise success(0).
232  **/
233 static int get_ecdsa_signature_from_asn1(unsigned char *sig_ptr,
234 					 size_t *sig_len,
235 					 unsigned char *r_s_pair)
236 {
237 	int rc;
238 	unsigned char *sig_end;
239 	size_t len, r_len, s_len;
240 
241 	sig_end = sig_ptr + *sig_len;
242 	rc = mbedtls_asn1_get_tag(&sig_ptr, sig_end, &len,
243 				  MBEDTLS_ASN1_CONSTRUCTED |
244 				  MBEDTLS_ASN1_SEQUENCE);
245 	if (rc != 0) {
246 		return CRYPTO_ERR_SIGNATURE;
247 	}
248 
249 	sig_end = sig_ptr + len;
250 	rc = mbedtls_asn1_get_tag(&sig_ptr, sig_end, &r_len,
251 				  MBEDTLS_ASN1_INTEGER);
252 	if (rc != 0) {
253 		return CRYPTO_ERR_SIGNATURE;
254 	}
255 
256 	if (ignore_asn1_int_padding_byte(&sig_ptr, &r_len) != 0) {
257 		return CRYPTO_ERR_SIGNATURE;
258 	}
259 
260 	(void)memcpy((void *)&r_s_pair[0], (const void *)sig_ptr, r_len);
261 
262 	sig_ptr = sig_ptr + r_len;
263 	sig_end = sig_ptr + len - (r_len + (SIZE_OF_ASN1_LEN +
264 		  SIZE_OF_ASN1_TAG));
265 	rc = mbedtls_asn1_get_tag(&sig_ptr, sig_end, &s_len,
266 				  MBEDTLS_ASN1_INTEGER);
267 	if (rc != 0) {
268 		return CRYPTO_ERR_SIGNATURE;
269 	}
270 
271 	if (ignore_asn1_int_padding_byte(&sig_ptr, &s_len) != 0) {
272 		return CRYPTO_ERR_SIGNATURE;
273 	}
274 
275 	(void)memcpy((void *)&r_s_pair[r_len], (const void *)sig_ptr, s_len);
276 
277 	*sig_len = s_len + r_len;
278 
279 	return 0;
280 }
281 #endif /*
282 	* TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA || \
283 	* TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA
284 	**/
285 
286 /*
287  * This is a helper function that adjusts the start of the pk_start to point to
288  * the subjectPublicKey bytes within the SubjectPublicKeyInfo block.
289  *
290  *  SubjectPublicKeyInfo  ::=  SEQUENCE  {
291  *       algorithm            AlgorithmIdentifier,
292  *       subjectPublicKey     BIT STRING }
293  *
294  * This function returns error(CRYPTO_ERR_SIGNATURE) on ASN.1 parsing failure,
295  * otherwise success(0).
296  **/
297 static int pk_bytes_from_subpubkey(unsigned char **pk_start,
298 				   unsigned int *pk_len)
299 {
300 	mbedtls_asn1_buf alg_oid, alg_params;
301 	int rc;
302 	unsigned char *pk_end;
303 	size_t len;
304 	unsigned char *pk_ptr = *pk_start;
305 
306 	pk_end = pk_ptr + *pk_len;
307 	rc = mbedtls_asn1_get_tag(&pk_ptr, pk_end, &len,
308 				  MBEDTLS_ASN1_CONSTRUCTED |
309 				  MBEDTLS_ASN1_SEQUENCE);
310 	if (rc != 0) {
311 		return CRYPTO_ERR_SIGNATURE;
312 	}
313 
314 	pk_end = pk_ptr + len;
315 	rc = mbedtls_asn1_get_alg(&pk_ptr, pk_end, &alg_oid, &alg_params);
316 	if (rc != 0) {
317 		return CRYPTO_ERR_SIGNATURE;
318 	}
319 	pk_end = pk_ptr + len - (alg_oid.len + alg_params.len +
320 		 2 * (SIZE_OF_ASN1_LEN + SIZE_OF_ASN1_TAG));
321 	rc = mbedtls_asn1_get_bitstring_null(&pk_ptr, pk_end, &len);
322 	if (rc != 0) {
323 		return CRYPTO_ERR_SIGNATURE;
324 	}
325 
326 	*pk_start = pk_ptr;
327 	*pk_len = len;
328 
329 	return rc;
330 }
331 
332 /*
333  * NOTE: This has been made internal in mbedtls 3.6.0 and the mbedtls team has
334  * advised that it's better to copy out the declaration than it would be to
335  * update to 3.5.2, where this function is exposed.
336  */
337 int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid,
338 			     const mbedtls_x509_buf *sig_params,
339 			     mbedtls_md_type_t *md_alg,
340 			     mbedtls_pk_type_t *pk_alg,
341 			     void **sig_opts);
342 /*
343  * Verify a signature.
344  *
345  * Parameters are passed using the DER encoding format following the ASN.1
346  * structures detailed above.
347  */
348 static int verify_signature(void *data_ptr, unsigned int data_len,
349 			    void *sig_ptr, unsigned int sig_len,
350 			    void *sig_alg, unsigned int sig_alg_len,
351 			    void *pk_ptr, unsigned int pk_len)
352 {
353 	mbedtls_asn1_buf sig_oid, sig_params;
354 	mbedtls_asn1_buf signature;
355 	mbedtls_md_type_t md_alg;
356 	mbedtls_pk_type_t pk_alg;
357 	int rc;
358 	void *sig_opts = NULL;
359 	unsigned char *p, *end;
360 	unsigned char *local_sig_ptr;
361 	size_t local_sig_len;
362 	psa_ecc_family_t psa_ecc_family = 0U;
363 	__unused unsigned char reformatted_sig[MAX_ECDSA_R_S_PAIR_LEN] = {0};
364 
365 	/* construct PSA key algo and type */
366 	psa_status_t status = PSA_SUCCESS;
367 	psa_key_attributes_t psa_key_attr = PSA_KEY_ATTRIBUTES_INIT;
368 	psa_key_id_t psa_key_id = PSA_KEY_ID_NULL;
369 	psa_key_type_t psa_key_type;
370 	psa_algorithm_t psa_alg;
371 
372 	/* Get pointers to signature OID and parameters */
373 	p = (unsigned char *)sig_alg;
374 	end = (unsigned char *)(p + sig_alg_len);
375 	rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
376 	if (rc != 0) {
377 		return CRYPTO_ERR_SIGNATURE;
378 	}
379 
380 	/* Get the actual signature algorithm (MD + PK) */
381 	rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
382 	if (rc != 0) {
383 		return CRYPTO_ERR_SIGNATURE;
384 	}
385 
386 	/* Get the signature (bitstring) */
387 	p = (unsigned char *)sig_ptr;
388 	end = (unsigned char *)(p + sig_len);
389 	signature.tag = *p;
390 	rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
391 	if ((rc != 0) || ((size_t)(end - p) != signature.len)) {
392 		rc = CRYPTO_ERR_SIGNATURE;
393 		goto end2;
394 	}
395 
396 	local_sig_ptr = p;
397 	local_sig_len = signature.len;
398 
399 #if TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA || \
400 TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA
401 	if (pk_alg == MBEDTLS_PK_ECDSA) {
402 		rc = get_ecdsa_signature_from_asn1(local_sig_ptr,
403 						   &local_sig_len,
404 						   reformatted_sig);
405 		if (rc != 0) {
406 			goto end2;
407 		}
408 
409 		local_sig_ptr = reformatted_sig;
410 
411 		rc = get_ecdsa_pkinfo_from_asn1((unsigned char **)&pk_ptr,
412 						&pk_len,
413 						&psa_ecc_family);
414 		if (rc != 0) {
415 			goto end2;
416 		}
417 	}
418 #endif /*
419 	* TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA || \
420 	* TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA
421 	**/
422 
423 	/* Convert this pk_alg and md_alg to PSA key type and key algorithm */
424 	construct_psa_key_alg_and_type(pk_alg, md_alg, psa_ecc_family,
425 				       &psa_alg, &psa_key_type);
426 
427 
428 	if ((psa_alg == PSA_ALG_NONE) || (psa_key_type == PSA_KEY_TYPE_NONE)) {
429 		rc = CRYPTO_ERR_SIGNATURE;
430 		goto end2;
431 	}
432 
433 	/* filled-in key_attributes */
434 	psa_set_key_algorithm(&psa_key_attr, psa_alg);
435 	psa_set_key_type(&psa_key_attr, psa_key_type);
436 	psa_set_key_usage_flags(&psa_key_attr, PSA_KEY_USAGE_VERIFY_MESSAGE);
437 
438 	/*
439 	 * Note: In the implementation of the psa_import_key function in
440 	 * version 3.6.0, the function expects the starting pointer of the
441 	 * subject public key instead of the starting point of
442 	 * SubjectPublicKeyInfo.
443 	 * This is only needed while dealing with RSASSA_PSS (RSA Signature
444 	 * scheme with Appendix based on Probabilistic Signature Scheme)
445 	 * algorithm.
446 	 */
447 	if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
448 		rc = pk_bytes_from_subpubkey((unsigned char **) &pk_ptr, &pk_len);
449 		if (rc != 0) {
450 			goto end2;
451 		}
452 	}
453 
454 	/* Get the key_id using import API */
455 	status = psa_import_key(&psa_key_attr,
456 				pk_ptr,
457 				(size_t)pk_len,
458 				&psa_key_id);
459 
460 	if (status != PSA_SUCCESS) {
461 		rc = CRYPTO_ERR_SIGNATURE;
462 		goto end2;
463 	}
464 
465 	/*
466 	 * Hash calculation and Signature verification of the given data payload
467 	 * is wrapped under the psa_verify_message function.
468 	 */
469 	status = psa_verify_message(psa_key_id, psa_alg,
470 				    data_ptr, data_len,
471 				    local_sig_ptr, local_sig_len);
472 
473 	if (status != PSA_SUCCESS) {
474 		rc = CRYPTO_ERR_SIGNATURE;
475 		goto end1;
476 	}
477 
478 	/* Signature verification success */
479 	rc = CRYPTO_SUCCESS;
480 
481 end1:
482 	/*
483 	 * Destroy the key if it is created successfully
484 	 */
485 	psa_destroy_key(psa_key_id);
486 end2:
487 	mbedtls_free(sig_opts);
488 	return rc;
489 }
490 
491 /*
492  * Match a hash
493  *
494  * Digest info is passed in DER format following the ASN.1 structure detailed
495  * above.
496  */
497 static int verify_hash(void *data_ptr, unsigned int data_len,
498 		       void *digest_info_ptr, unsigned int digest_info_len)
499 {
500 	mbedtls_asn1_buf hash_oid, params;
501 	mbedtls_md_type_t md_alg;
502 	unsigned char *p, *end, *hash;
503 	size_t len;
504 	int rc;
505 	psa_status_t status;
506 	psa_algorithm_t psa_md_alg;
507 
508 	/*
509 	 * Digest info should be an MBEDTLS_ASN1_SEQUENCE, but padding after
510 	 * it is allowed.  This is necessary to support multiple hash
511 	 * algorithms.
512 	 */
513 	p = (unsigned char *)digest_info_ptr;
514 	end = p + digest_info_len;
515 	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
516 				  MBEDTLS_ASN1_SEQUENCE);
517 	if (rc != 0) {
518 		return CRYPTO_ERR_HASH;
519 	}
520 
521 	end = p + len;
522 
523 	/* Get the hash algorithm */
524 	rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
525 	if (rc != 0) {
526 		return CRYPTO_ERR_HASH;
527 	}
528 
529 	/* Hash should be octet string type and consume all bytes */
530 	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
531 	if ((rc != 0) || ((size_t)(end - p) != len)) {
532 		return CRYPTO_ERR_HASH;
533 	}
534 	hash = p;
535 
536 	rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
537 	if (rc != 0) {
538 		return CRYPTO_ERR_HASH;
539 	}
540 
541 	/* convert the md_alg to psa_algo */
542 	psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
543 
544 	/* Length of hash must match the algorithm's size */
545 	if (len != PSA_HASH_LENGTH(psa_md_alg)) {
546 		return CRYPTO_ERR_HASH;
547 	}
548 
549 	/*
550 	 * Calculate Hash and compare it against the retrieved hash from
551 	 * the certificate (one shot API).
552 	 */
553 	status = psa_hash_compare(psa_md_alg,
554 				  data_ptr, (size_t)data_len,
555 				  (const uint8_t *)hash, len);
556 
557 	if (status != PSA_SUCCESS) {
558 		return CRYPTO_ERR_HASH;
559 	}
560 
561 	return CRYPTO_SUCCESS;
562 }
563 #endif /*
564 	* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
565 	* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
566 	*/
567 
568 #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
569 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
570 /*
571  * Map a generic crypto message digest algorithm to the corresponding macro used
572  * by Mbed TLS.
573  */
574 static inline mbedtls_md_type_t md_type(enum crypto_md_algo algo)
575 {
576 	switch (algo) {
577 	case CRYPTO_MD_SHA512:
578 		return MBEDTLS_MD_SHA512;
579 	case CRYPTO_MD_SHA384:
580 		return MBEDTLS_MD_SHA384;
581 	case CRYPTO_MD_SHA256:
582 		return MBEDTLS_MD_SHA256;
583 	default:
584 		/* Invalid hash algorithm. */
585 		return MBEDTLS_MD_NONE;
586 	}
587 }
588 
589 /*
590  * Calculate a hash
591  *
592  * output points to the computed hash
593  */
594 static int calc_hash(enum crypto_md_algo md_algo, void *data_ptr,
595 		     unsigned int data_len,
596 		     unsigned char output[CRYPTO_MD_MAX_SIZE])
597 {
598 	size_t hash_length;
599 	psa_status_t status;
600 	psa_algorithm_t psa_md_alg;
601 
602 	/* convert the md_alg to psa_algo */
603 	psa_md_alg = mbedtls_md_psa_alg_from_type(md_type(md_algo));
604 
605 	/*
606 	 * Calculate the hash of the data, it is safe to pass the
607 	 * 'output' hash buffer pointer considering its size is always
608 	 * bigger than or equal to MBEDTLS_MD_MAX_SIZE.
609 	 */
610 	status = psa_hash_compute(psa_md_alg, data_ptr, (size_t)data_len,
611 				  (uint8_t *)output, CRYPTO_MD_MAX_SIZE,
612 				  &hash_length);
613 	if (status != PSA_SUCCESS) {
614 		return CRYPTO_ERR_HASH;
615 	}
616 
617 	return CRYPTO_SUCCESS;
618 }
619 #endif /*
620 	* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
621 	* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
622 	*/
623 
624 #if TF_MBEDTLS_USE_AES_GCM
625 /*
626  * Stack based buffer allocation for decryption operation. It could
627  * be configured to balance stack usage vs execution speed.
628  */
629 #define DEC_OP_BUF_SIZE		128
630 
631 static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
632 			   unsigned int key_len, const void *iv,
633 			   unsigned int iv_len, const void *tag,
634 			   unsigned int tag_len)
635 {
636 	mbedtls_gcm_context ctx;
637 	mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
638 	unsigned char buf[DEC_OP_BUF_SIZE];
639 	unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
640 	unsigned char *pt = data_ptr;
641 	size_t dec_len;
642 	int diff, i, rc;
643 	size_t output_length __unused;
644 
645 	mbedtls_gcm_init(&ctx);
646 
647 	rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
648 	if (rc != 0) {
649 		rc = CRYPTO_ERR_DECRYPTION;
650 		goto exit_gcm;
651 	}
652 
653 #if (MBEDTLS_VERSION_MAJOR < 3)
654 	rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
655 #else
656 	rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len);
657 #endif
658 	if (rc != 0) {
659 		rc = CRYPTO_ERR_DECRYPTION;
660 		goto exit_gcm;
661 	}
662 
663 	while (len > 0) {
664 		dec_len = MIN(sizeof(buf), len);
665 
666 #if (MBEDTLS_VERSION_MAJOR < 3)
667 		rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
668 #else
669 		rc = mbedtls_gcm_update(&ctx, pt, dec_len, buf, sizeof(buf), &output_length);
670 #endif
671 
672 		if (rc != 0) {
673 			rc = CRYPTO_ERR_DECRYPTION;
674 			goto exit_gcm;
675 		}
676 
677 		memcpy(pt, buf, dec_len);
678 		pt += dec_len;
679 		len -= dec_len;
680 	}
681 
682 #if (MBEDTLS_VERSION_MAJOR < 3)
683 	rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
684 #else
685 	rc = mbedtls_gcm_finish(&ctx, NULL, 0, &output_length, tag_buf, sizeof(tag_buf));
686 #endif
687 
688 	if (rc != 0) {
689 		rc = CRYPTO_ERR_DECRYPTION;
690 		goto exit_gcm;
691 	}
692 
693 	/* Check tag in "constant-time" */
694 	for (diff = 0, i = 0; i < tag_len; i++)
695 		diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];
696 
697 	if (diff != 0) {
698 		rc = CRYPTO_ERR_DECRYPTION;
699 		goto exit_gcm;
700 	}
701 
702 	/* GCM decryption success */
703 	rc = CRYPTO_SUCCESS;
704 
705 exit_gcm:
706 	mbedtls_gcm_free(&ctx);
707 	return rc;
708 }
709 
710 /*
711  * Authenticated decryption of an image
712  */
713 static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
714 			size_t len, const void *key, unsigned int key_len,
715 			unsigned int key_flags, const void *iv,
716 			unsigned int iv_len, const void *tag,
717 			unsigned int tag_len)
718 {
719 	int rc;
720 
721 	assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);
722 
723 	switch (dec_algo) {
724 	case CRYPTO_GCM_DECRYPT:
725 		rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
726 				     tag, tag_len);
727 		if (rc != 0)
728 			return rc;
729 		break;
730 	default:
731 		return CRYPTO_ERR_DECRYPTION;
732 	}
733 
734 	return CRYPTO_SUCCESS;
735 }
736 #endif /* TF_MBEDTLS_USE_AES_GCM */
737 
738 /*
739  * Register crypto library descriptor
740  */
741 #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
742 #if TF_MBEDTLS_USE_AES_GCM
743 REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
744 		    auth_decrypt, NULL);
745 #else
746 REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
747 		    NULL, NULL);
748 #endif
749 #elif CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY
750 #if TF_MBEDTLS_USE_AES_GCM
751 REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL,
752 		    auth_decrypt, NULL);
753 #else
754 REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL,
755 		    NULL, NULL);
756 #endif
757 #elif CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY
758 REGISTER_CRYPTO_LIB(LIB_NAME, init, NULL, NULL, calc_hash, NULL, NULL);
759 #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
760