blob: 2fa8e63ee7a1ab9b1f31502d7cd445bc2e33e1cb [file] [log] [blame]
Manish V Badarkhe8fbea622023-09-06 10:22:19 +01001/*
2 * Copyright (c) 2023, 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/version.h>
18#include <mbedtls/x509.h>
Manish V Badarkhef179aa92023-09-06 11:01:37 +010019#include <psa/crypto.h>
20#include <psa/crypto_platform.h>
21#include <psa/crypto_types.h>
22#include <psa/crypto_values.h>
Manish V Badarkhe8fbea622023-09-06 10:22:19 +010023
24#include <common/debug.h>
25#include <drivers/auth/crypto_mod.h>
26#include <drivers/auth/mbedtls/mbedtls_common.h>
Manish V Badarkhe8fbea622023-09-06 10:22:19 +010027#include <plat/common/platform.h>
28
29#define LIB_NAME "mbed TLS PSA"
30
31#if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
32CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
33/*
34 * CRYPTO_MD_MAX_SIZE value is as per current stronger algorithm available
35 * so make sure that mbed TLS MD maximum size must be lesser than this.
36 */
37CASSERT(CRYPTO_MD_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE,
38 assert_mbedtls_md_size_overflow);
39
40#endif /*
41 * CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
42 * CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
43 */
44
Manish V Badarkhec381ea32023-09-06 12:00:20 +010045static inline psa_algorithm_t mbedtls_md_psa_alg_from_type(
46 mbedtls_md_type_t md_type)
47{
48 assert((md_type == MBEDTLS_MD_SHA256) ||
49 (md_type == MBEDTLS_MD_SHA384) ||
50 (md_type == MBEDTLS_MD_SHA512));
51
52 return PSA_ALG_CATEGORY_HASH | (psa_algorithm_t) (md_type + 0x5);
53}
54
Manish V Badarkhe8fbea622023-09-06 10:22:19 +010055/*
56 * AlgorithmIdentifier ::= SEQUENCE {
57 * algorithm OBJECT IDENTIFIER,
58 * parameters ANY DEFINED BY algorithm OPTIONAL
59 * }
60 *
61 * SubjectPublicKeyInfo ::= SEQUENCE {
62 * algorithm AlgorithmIdentifier,
63 * subjectPublicKey BIT STRING
64 * }
65 *
66 * DigestInfo ::= SEQUENCE {
67 * digestAlgorithm AlgorithmIdentifier,
68 * digest OCTET STRING
69 * }
70 */
Manish V Badarkhef179aa92023-09-06 11:01:37 +010071
72/*
73 * We pretend using an external RNG (through MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
74 * mbedTLS config option) so we need to provide an implementation of
75 * mbedtls_psa_external_get_random(). Provide a fake one, since we do not
76 * actually have any external RNG and TF-A itself doesn't engage in
77 * cryptographic operations that demands randomness.
78 */
79psa_status_t mbedtls_psa_external_get_random(
80 mbedtls_psa_external_random_context_t *context,
81 uint8_t *output, size_t output_size,
82 size_t *output_length)
83{
84 return PSA_ERROR_INSUFFICIENT_ENTROPY;
85}
Manish V Badarkhe8fbea622023-09-06 10:22:19 +010086
87/*
88 * Initialize the library and export the descriptor
89 */
90static void init(void)
91{
92 /* Initialize mbed TLS */
93 mbedtls_init();
Manish V Badarkhef179aa92023-09-06 11:01:37 +010094
95 /* Initialise PSA mbedTLS */
96 psa_status_t status = psa_crypto_init();
97
98 if (status != PSA_SUCCESS) {
99 ERROR("Failed to initialize %s crypto (%d).\n", LIB_NAME, status);
100 panic();
101 }
102
103 INFO("PSA crypto initialized successfully!\n");
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100104}
105
106#if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
107CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
Manish V Badarkhe3e642b42023-09-22 18:06:05 +0100108
109static void construct_psa_key_alg_and_type(mbedtls_pk_type_t pk_alg,
110 mbedtls_md_type_t md_alg,
111 psa_algorithm_t *psa_alg,
112 psa_key_type_t *psa_key_type)
113{
114 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
115
116 switch (pk_alg) {
117 case MBEDTLS_PK_RSASSA_PSS:
118 *psa_alg = PSA_ALG_RSA_PSS(psa_md_alg);
119 *psa_key_type = PSA_KEY_TYPE_RSA_PUBLIC_KEY;
120 break;
121 default:
122 *psa_alg = PSA_ALG_NONE;
123 *psa_key_type = PSA_KEY_TYPE_NONE;
124 break;
125 }
126}
127
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100128/*
129 * Verify a signature.
130 *
131 * Parameters are passed using the DER encoding format following the ASN.1
132 * structures detailed above.
133 */
134static int verify_signature(void *data_ptr, unsigned int data_len,
135 void *sig_ptr, unsigned int sig_len,
136 void *sig_alg, unsigned int sig_alg_len,
137 void *pk_ptr, unsigned int pk_len)
138{
139 mbedtls_asn1_buf sig_oid, sig_params;
140 mbedtls_asn1_buf signature;
141 mbedtls_md_type_t md_alg;
142 mbedtls_pk_type_t pk_alg;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100143 int rc;
144 void *sig_opts = NULL;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100145 unsigned char *p, *end;
Manish V Badarkhe3e642b42023-09-22 18:06:05 +0100146
147 /* construct PSA key algo and type */
148 psa_status_t status = PSA_SUCCESS;
149 psa_key_attributes_t psa_key_attr = PSA_KEY_ATTRIBUTES_INIT;
150 psa_key_id_t psa_key_id = PSA_KEY_ID_NULL;
151 psa_key_type_t psa_key_type;
152 psa_algorithm_t psa_alg;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100153
154 /* Get pointers to signature OID and parameters */
155 p = (unsigned char *)sig_alg;
156 end = (unsigned char *)(p + sig_alg_len);
157 rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
158 if (rc != 0) {
159 return CRYPTO_ERR_SIGNATURE;
160 }
161
162 /* Get the actual signature algorithm (MD + PK) */
163 rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
164 if (rc != 0) {
165 return CRYPTO_ERR_SIGNATURE;
166 }
167
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100168 /* Get the signature (bitstring) */
169 p = (unsigned char *)sig_ptr;
170 end = (unsigned char *)(p + sig_len);
171 signature.tag = *p;
172 rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
173 if ((rc != 0) || ((size_t)(end - p) != signature.len)) {
174 rc = CRYPTO_ERR_SIGNATURE;
Manish V Badarkhe3e642b42023-09-22 18:06:05 +0100175 goto end2;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100176 }
177 signature.p = p;
178
Manish V Badarkhe3e642b42023-09-22 18:06:05 +0100179 /* Convert this pk_alg and md_alg to PSA key type and key algorithm */
180 construct_psa_key_alg_and_type(pk_alg, md_alg,
181 &psa_alg, &psa_key_type);
182
183
184 if ((psa_alg == PSA_ALG_NONE) || (psa_key_type == PSA_KEY_TYPE_NONE)) {
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100185 rc = CRYPTO_ERR_SIGNATURE;
Manish V Badarkhe3e642b42023-09-22 18:06:05 +0100186 goto end2;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100187 }
Manish V Badarkhe3e642b42023-09-22 18:06:05 +0100188
189 /* filled-in key_attributes */
190 psa_set_key_algorithm(&psa_key_attr, psa_alg);
191 psa_set_key_type(&psa_key_attr, psa_key_type);
192 psa_set_key_usage_flags(&psa_key_attr, PSA_KEY_USAGE_VERIFY_MESSAGE);
193
194 /* Get the key_id using import API */
195 status = psa_import_key(&psa_key_attr,
196 pk_ptr,
197 (size_t)pk_len,
198 &psa_key_id);
199
200 if (status != PSA_SUCCESS) {
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100201 rc = CRYPTO_ERR_SIGNATURE;
Manish V Badarkhe3e642b42023-09-22 18:06:05 +0100202 goto end2;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100203 }
204
Manish V Badarkhe3e642b42023-09-22 18:06:05 +0100205 /*
206 * Hash calculation and Signature verification of the given data payload
207 * is wrapped under the psa_verify_message function.
208 */
209 status = psa_verify_message(psa_key_id, psa_alg,
210 data_ptr, data_len,
211 signature.p, signature.len);
212
213 if (status != PSA_SUCCESS) {
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100214 rc = CRYPTO_ERR_SIGNATURE;
215 goto end1;
216 }
217
218 /* Signature verification success */
219 rc = CRYPTO_SUCCESS;
220
221end1:
Manish V Badarkhe3e642b42023-09-22 18:06:05 +0100222 /*
223 * Destroy the key if it is created successfully
224 */
225 psa_destroy_key(psa_key_id);
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100226end2:
227 mbedtls_free(sig_opts);
228 return rc;
229}
230
231/*
232 * Match a hash
233 *
234 * Digest info is passed in DER format following the ASN.1 structure detailed
235 * above.
236 */
237static int verify_hash(void *data_ptr, unsigned int data_len,
238 void *digest_info_ptr, unsigned int digest_info_len)
239{
240 mbedtls_asn1_buf hash_oid, params;
241 mbedtls_md_type_t md_alg;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100242 unsigned char *p, *end, *hash;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100243 size_t len;
244 int rc;
Manish V Badarkhec381ea32023-09-06 12:00:20 +0100245 psa_status_t status;
246 psa_algorithm_t psa_md_alg;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100247
248 /*
249 * Digest info should be an MBEDTLS_ASN1_SEQUENCE, but padding after
250 * it is allowed. This is necessary to support multiple hash
251 * algorithms.
252 */
253 p = (unsigned char *)digest_info_ptr;
254 end = p + digest_info_len;
255 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
256 MBEDTLS_ASN1_SEQUENCE);
257 if (rc != 0) {
258 return CRYPTO_ERR_HASH;
259 }
260
261 end = p + len;
262
263 /* Get the hash algorithm */
264 rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
265 if (rc != 0) {
266 return CRYPTO_ERR_HASH;
267 }
268
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100269 /* Hash should be octet string type and consume all bytes */
270 rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
271 if ((rc != 0) || ((size_t)(end - p) != len)) {
272 return CRYPTO_ERR_HASH;
273 }
Manish V Badarkhec381ea32023-09-06 12:00:20 +0100274 hash = p;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100275
Manish V Badarkhec381ea32023-09-06 12:00:20 +0100276 rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
277 if (rc != 0) {
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100278 return CRYPTO_ERR_HASH;
279 }
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100280
Manish V Badarkhec381ea32023-09-06 12:00:20 +0100281 /* convert the md_alg to psa_algo */
282 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
283
284 /* Length of hash must match the algorithm's size */
285 if (len != PSA_HASH_LENGTH(psa_md_alg)) {
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100286 return CRYPTO_ERR_HASH;
287 }
288
Manish V Badarkhec381ea32023-09-06 12:00:20 +0100289 /*
290 * Calculate Hash and compare it against the retrieved hash from
291 * the certificate (one shot API).
292 */
293 status = psa_hash_compare(psa_md_alg,
294 data_ptr, (size_t)data_len,
295 (const uint8_t *)hash, len);
296
297 if (status != PSA_SUCCESS) {
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100298 return CRYPTO_ERR_HASH;
299 }
300
301 return CRYPTO_SUCCESS;
302}
303#endif /*
304 * CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
305 * CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
306 */
307
308#if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
309CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
310/*
311 * Map a generic crypto message digest algorithm to the corresponding macro used
312 * by Mbed TLS.
313 */
314static inline mbedtls_md_type_t md_type(enum crypto_md_algo algo)
315{
316 switch (algo) {
317 case CRYPTO_MD_SHA512:
318 return MBEDTLS_MD_SHA512;
319 case CRYPTO_MD_SHA384:
320 return MBEDTLS_MD_SHA384;
321 case CRYPTO_MD_SHA256:
322 return MBEDTLS_MD_SHA256;
323 default:
324 /* Invalid hash algorithm. */
325 return MBEDTLS_MD_NONE;
326 }
327}
328
329/*
330 * Calculate a hash
331 *
332 * output points to the computed hash
333 */
334static int calc_hash(enum crypto_md_algo md_algo, void *data_ptr,
335 unsigned int data_len,
336 unsigned char output[CRYPTO_MD_MAX_SIZE])
337{
Manish V Badarkhe4139d7b2023-09-06 12:05:05 +0100338 size_t hash_length;
339 psa_status_t status;
340 psa_algorithm_t psa_md_alg;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100341
Manish V Badarkhe4139d7b2023-09-06 12:05:05 +0100342 /* convert the md_alg to psa_algo */
343 psa_md_alg = mbedtls_md_psa_alg_from_type(md_type(md_algo));
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100344
345 /*
346 * Calculate the hash of the data, it is safe to pass the
347 * 'output' hash buffer pointer considering its size is always
348 * bigger than or equal to MBEDTLS_MD_MAX_SIZE.
349 */
Manish V Badarkhe4139d7b2023-09-06 12:05:05 +0100350 status = psa_hash_compute(psa_md_alg, data_ptr, (size_t)data_len,
351 (uint8_t *)output, CRYPTO_MD_MAX_SIZE,
352 &hash_length);
353 if (status != PSA_SUCCESS) {
354 return CRYPTO_ERR_HASH;
355 }
356
357 return CRYPTO_SUCCESS;
Manish V Badarkhe8fbea622023-09-06 10:22:19 +0100358}
359#endif /*
360 * CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
361 * CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
362 */
363
364#if TF_MBEDTLS_USE_AES_GCM
365/*
366 * Stack based buffer allocation for decryption operation. It could
367 * be configured to balance stack usage vs execution speed.
368 */
369#define DEC_OP_BUF_SIZE 128
370
371static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
372 unsigned int key_len, const void *iv,
373 unsigned int iv_len, const void *tag,
374 unsigned int tag_len)
375{
376 mbedtls_gcm_context ctx;
377 mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
378 unsigned char buf[DEC_OP_BUF_SIZE];
379 unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
380 unsigned char *pt = data_ptr;
381 size_t dec_len;
382 int diff, i, rc;
383 size_t output_length __unused;
384
385 mbedtls_gcm_init(&ctx);
386
387 rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
388 if (rc != 0) {
389 rc = CRYPTO_ERR_DECRYPTION;
390 goto exit_gcm;
391 }
392
393#if (MBEDTLS_VERSION_MAJOR < 3)
394 rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
395#else
396 rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len);
397#endif
398 if (rc != 0) {
399 rc = CRYPTO_ERR_DECRYPTION;
400 goto exit_gcm;
401 }
402
403 while (len > 0) {
404 dec_len = MIN(sizeof(buf), len);
405
406#if (MBEDTLS_VERSION_MAJOR < 3)
407 rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
408#else
409 rc = mbedtls_gcm_update(&ctx, pt, dec_len, buf, sizeof(buf), &output_length);
410#endif
411
412 if (rc != 0) {
413 rc = CRYPTO_ERR_DECRYPTION;
414 goto exit_gcm;
415 }
416
417 memcpy(pt, buf, dec_len);
418 pt += dec_len;
419 len -= dec_len;
420 }
421
422#if (MBEDTLS_VERSION_MAJOR < 3)
423 rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
424#else
425 rc = mbedtls_gcm_finish(&ctx, NULL, 0, &output_length, tag_buf, sizeof(tag_buf));
426#endif
427
428 if (rc != 0) {
429 rc = CRYPTO_ERR_DECRYPTION;
430 goto exit_gcm;
431 }
432
433 /* Check tag in "constant-time" */
434 for (diff = 0, i = 0; i < tag_len; i++)
435 diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];
436
437 if (diff != 0) {
438 rc = CRYPTO_ERR_DECRYPTION;
439 goto exit_gcm;
440 }
441
442 /* GCM decryption success */
443 rc = CRYPTO_SUCCESS;
444
445exit_gcm:
446 mbedtls_gcm_free(&ctx);
447 return rc;
448}
449
450/*
451 * Authenticated decryption of an image
452 */
453static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
454 size_t len, const void *key, unsigned int key_len,
455 unsigned int key_flags, const void *iv,
456 unsigned int iv_len, const void *tag,
457 unsigned int tag_len)
458{
459 int rc;
460
461 assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);
462
463 switch (dec_algo) {
464 case CRYPTO_GCM_DECRYPT:
465 rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
466 tag, tag_len);
467 if (rc != 0)
468 return rc;
469 break;
470 default:
471 return CRYPTO_ERR_DECRYPTION;
472 }
473
474 return CRYPTO_SUCCESS;
475}
476#endif /* TF_MBEDTLS_USE_AES_GCM */
477
478/*
479 * Register crypto library descriptor
480 */
481#if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
482#if TF_MBEDTLS_USE_AES_GCM
483REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
484 auth_decrypt, NULL);
485#else
486REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
487 NULL, NULL);
488#endif
489#elif CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY
490#if TF_MBEDTLS_USE_AES_GCM
491REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL,
492 auth_decrypt, NULL);
493#else
494REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL,
495 NULL, NULL);
496#endif
497#elif CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY
498REGISTER_CRYPTO_LIB(LIB_NAME, init, NULL, NULL, calc_hash, NULL, NULL);
499#endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */