Skip to content

Commit 72868c6

Browse files
frkvnordicjm
authored andcommitted
[nrf noup] Add MBEDTLS_PSA_CRYPTO_CLIENT support for TLS/DTLS and X.509
-Change from MBEDTLS_PSA_CRYPTO_C to MBEDTLS_PSA_CRYPTO_CLIENT for TLS/DTLS and X.509 use-cases as well as wrapper APIs that the aforementioned features uses: - pk.h/pk.c/pk_internal.h/pk_wrap. - Public Key interface - md.c -Change from MBEDTLS_PSA_CRYPTO_C to MBEDTLS_PSA_CRYPTO_CLIENT to ensure psa_util_internal.h and psa_util.c is compiled in. This file contains functions relevant to changing from PSA error codes/ecc types etc. into legacy types e.g. in TLS/DTLS and X.509 use-cases. These files are used even in a core-less build -Add a version to the non-standard mechanism to use key enrollment algorithm to wrap certain key-types. This functionality is not available in PSA crypto provided by TF-M, and hence it is wrapped with a check if MBEDTLS_PSA_CRYPTO_C is enabled (meaning local build of the PSA core) -Note that this is a single commit to ensure bisectability Note: This noup patch will be reverted/changed with Mbed TLS 3.6.0 but is necessary right now to ensure core-less build of nrf_security for nRF54H20 device support. Signed-off-by: Frank Audun Kvamtrø <[email protected]>
1 parent c99e53f commit 72868c6

File tree

7 files changed

+30
-18
lines changed

7 files changed

+30
-18
lines changed

include/library/psa_util_internal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "psa/crypto.h"
1818

19-
#if defined(MBEDTLS_PSA_CRYPTO_C)
19+
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
2020

2121
/*************************************************************************
2222
* FFDH

include/mbedtls/pk.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "mbedtls/ecdsa.h"
2929
#endif
3030

31-
#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_PSA_CRYPTO_C)
31+
#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_PSA_CRYPTO_CLIENT)
3232
#include "psa/crypto.h"
3333
#endif
3434

@@ -245,7 +245,7 @@ typedef struct mbedtls_pk_context {
245245
* MBEDTLS_PK_USE_PSA_EC_DATA (as the public counterpart below) because,
246246
* when working with opaque keys, it can be used also in
247247
* mbedtls_pk_sign_ext for RSA keys. */
248-
#if defined(MBEDTLS_PSA_CRYPTO_C)
248+
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
249249
mbedtls_svc_key_id_t MBEDTLS_PRIVATE(priv_id); /**< Key ID for opaque keys */
250250
#endif /* MBEDTLS_PSA_CRYPTO_C */
251251
/* The following fields are meant for storing the public key in raw format

library/md.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include "mbedtls/sha512.h"
4242
#include "mbedtls/sha3.h"
4343

44-
#if defined(MBEDTLS_PSA_CRYPTO_C)
44+
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
4545
#include <psa/crypto.h>
4646
#include "md_psa.h"
4747
#include "psa_util_internal.h"
@@ -761,7 +761,7 @@ mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
761761
return md_info->type;
762762
}
763763

764-
#if defined(MBEDTLS_PSA_CRYPTO_C)
764+
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
765765
int mbedtls_md_error_from_psa(psa_status_t status)
766766
{
767767
return PSA_TO_MBEDTLS_ERR_LIST(status, psa_to_md_errors,

library/pk.c

+21-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "mbedtls/ecdsa.h"
2727
#endif
2828

29-
#if defined(MBEDTLS_PSA_CRYPTO_C)
29+
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
3030
#include "psa_util_internal.h"
3131
#include "md_psa.h"
3232
#endif
@@ -311,16 +311,22 @@ int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
311311
}
312312

313313
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
314-
psa_algorithm_t key_alg, key_alg2;
315314
psa_status_t status;
316315

317316
status = psa_get_key_attributes(ctx->priv_id, &attributes);
318317
if (status != PSA_SUCCESS) {
319318
return 0;
320319
}
321320

322-
key_alg = psa_get_key_algorithm(&attributes);
323-
key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
321+
psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
322+
/* Key's enrollment is available only when an Mbed TLS implementation of PSA
323+
* Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
324+
* Even though we don't officially support using other implementations of PSA
325+
* Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
326+
* separated. */
327+
#if defined(MBEDTLS_PSA_CRYPTO_C)
328+
psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
329+
#endif /* MBEDTLS_PSA_CRYPTO_C */
324330
key_usage = psa_get_key_usage_flags(&attributes);
325331
psa_reset_key_attributes(&attributes);
326332

@@ -329,34 +335,39 @@ int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
329335
}
330336

331337
/*
332-
* Common case: the key alg or alg2 only allows alg.
338+
* Common case: the key alg [or alg2] only allows alg.
333339
* This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
334340
* directly.
335341
* This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
336-
* a fixed hash on key_alg/key_alg2.
342+
* a fixed hash on key_alg [or key_alg2].
337343
*/
338-
if (alg == key_alg || alg == key_alg2) {
344+
if (alg == key_alg) {
345+
return 1;
346+
}
347+
#if defined(MBEDTLS_PSA_CRYPTO_C)
348+
if (alg == key_alg2) {
339349
return 1;
340350
}
351+
#endif /* MBEDTLS_PSA_CRYPTO_C */
341352

342353
/*
343-
* If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
354+
* If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
344355
* and alg is the same hash-and-sign family with any hash,
345356
* then alg is compliant with this key alg
346357
*/
347358
if (PSA_ALG_IS_SIGN_HASH(alg)) {
348-
349359
if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
350360
PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
351361
(alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
352362
return 1;
353363
}
354-
364+
#if defined(MBEDTLS_PSA_CRYPTO_C)
355365
if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
356366
PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
357367
(alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
358368
return 1;
359369
}
370+
#endif /* MBEDTLS_PSA_CRYPTO_C */
360371
}
361372

362373
return 0;

library/pk_internal.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020
#if defined(MBEDTLS_USE_PSA_CRYPTO)
2121
#include "psa/crypto.h"
22+
#include "psa/crypto_extra.h"
2223
#endif
2324

24-
#if defined(MBEDTLS_PSA_CRYPTO_C)
25+
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
2526
#include "psa_util_internal.h"
2627
#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
2728
#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \

library/pk_wrap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "pkwrite.h"
3131
#endif
3232

33-
#if defined(MBEDTLS_PSA_CRYPTO_C)
33+
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
3434
#include "psa_util_internal.h"
3535
#endif
3636

library/psa_util.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "common.h"
1010

11-
#if defined(MBEDTLS_PSA_CRYPTO_C)
11+
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
1212

1313
#include <psa/crypto.h>
1414

0 commit comments

Comments
 (0)