Skip to content

Commit 436b398

Browse files
[crypto] update mbedtls psa crypto context structs
1 parent e2e36d6 commit 436b398

11 files changed

Lines changed: 55 additions & 3 deletions

File tree

include/openthread/instance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extern "C" {
5252
*
5353
* @note This number versions both OpenThread platform and user APIs.
5454
*/
55-
#define OPENTHREAD_API_VERSION (586)
55+
#define OPENTHREAD_API_VERSION (587)
5656

5757
/**
5858
* @addtogroup api-instance

include/openthread/platform/crypto.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ typedef struct otCryptoKey
123123
* @struct otCryptoContext
124124
*
125125
* Stores the context object for platform APIs.
126+
*
127+
* If `OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT` is enabled, the platform allocates and populates this.
128+
* Otherwise OpenThread core allocates and populates this.
126129
*/
127130
typedef struct otCryptoContext
128131
{
@@ -325,6 +328,9 @@ void otPlatCryptoFree(void *aPtr);
325328
* @retval OT_ERROR_FAILED Failed to initialize HMAC operation.
326329
* @retval OT_ERROR_INVALID_ARGS @p aContext was NULL
327330
*
331+
* @note If `OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT` is enabled, @p aContext is populated by the platform.
332+
* Otherwise OpenThread core allocates and populates it.
333+
*
328334
* @note The platform driver shall point the context to the correct object such as psa_mac_operation_t or
329335
* mbedtls_md_context_t.
330336
*/
@@ -389,6 +395,9 @@ otError otPlatCryptoHmacSha256Finish(otCryptoContext *aContext, uint8_t *aBuf, s
389395
* @retval OT_ERROR_INVALID_ARGS @p aContext was NULL
390396
* @retval OT_ERROR_NO_BUFS Cannot allocate the context.
391397
*
398+
* @note If `OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT` is enabled, @p aContext is populated by the platform.
399+
* Otherwise OpenThread core allocates and populates it.
400+
*
392401
* @note The platform driver shall point the context to the correct object such as psa_key_id
393402
* or mbedtls_aes_context_t.
394403
*/
@@ -435,10 +444,13 @@ otError otPlatCryptoAesFree(otCryptoContext *aContext);
435444
*
436445
* @param[in] aContext Context for HKDF operation.
437446
*
438-
* @retval OT_ERROR_NONE Successfully Initialised AES operation.
439-
* @retval OT_ERROR_FAILED Failed to Initialise AES operation.
447+
* @retval OT_ERROR_NONE Successfully Initialised HKDF operation.
448+
* @retval OT_ERROR_FAILED Failed to Initialise HKDF operation.
440449
* @retval OT_ERROR_INVALID_ARGS @p aContext was NULL
441450
*
451+
* @note If `OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT` is enabled, @p aContext is populated by the platform.
452+
* Otherwise OpenThread core allocates and populates it.
453+
*
442454
* @note The platform driver shall point the context to the correct object such as psa_key_derivation_operation_t
443455
* or HmacSha256::Hash
444456
*/
@@ -499,6 +511,8 @@ otError otPlatCryptoHkdfDeinit(otCryptoContext *aContext);
499511
* @retval OT_ERROR_FAILED Failed to initialise SHA-256 operation.
500512
* @retval OT_ERROR_INVALID_ARGS @p aContext was NULL
501513
*
514+
* @note If `OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT` is enabled, @p aContext is populated by the platform.
515+
* Otherwise OpenThread core allocates and populates it.
502516
*
503517
* @note The platform driver shall point the context to the correct object such as psa_hash_operation_t
504518
* or mbedtls_sha256_context.

src/core/config/crypto.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@
5959
/** Use platform provided crypto library */
6060
#define OPENTHREAD_CONFIG_CRYPTO_LIB_PLATFORM 2
6161

62+
/**
63+
* @def OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
64+
*
65+
* Define to 1 to enable the platform to allocate crypto operation context.
66+
*/
67+
#ifndef OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
68+
#define OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT 0
69+
#endif
70+
6271
#if OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PLATFORM
6372

6473
/**

src/core/crypto/aes_ecb.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ namespace Crypto {
4040

4141
AesEcb::AesEcb(void)
4242
{
43+
#if OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
44+
mContext.mContext = nullptr;
45+
mContext.mContextSize = 0;
46+
#else
4347
mContext.mContext = mContextStorage;
4448
mContext.mContextSize = sizeof(mContextStorage);
49+
#endif
4550
SuccessOrAssert(otPlatCryptoAesInit(&mContext));
4651
}
4752

src/core/crypto/aes_ecb.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class AesEcb
8686

8787
private:
8888
otCryptoContext mContext;
89+
#if !OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
8990
OT_DEFINE_ALIGNED_VAR(mContextStorage, kAesContextSize, uint64_t);
91+
#endif
9092
};
9193

9294
/**

src/core/crypto/hkdf_sha256.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ namespace Crypto {
4444

4545
HkdfSha256::HkdfSha256(void)
4646
{
47+
#if OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
48+
mContext.mContext = nullptr;
49+
mContext.mContextSize = 0;
50+
#else
4751
mContext.mContext = mContextStorage;
4852
mContext.mContextSize = sizeof(mContextStorage);
53+
#endif
4954
SuccessOrAssert(otPlatCryptoHkdfInit(&mContext));
5055
}
5156

src/core/crypto/hkdf_sha256.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ class HkdfSha256
9494

9595
private:
9696
otCryptoContext mContext;
97+
#if !OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
9798
OT_DEFINE_ALIGNED_VAR(mContextStorage, kHkdfContextSize, uint64_t);
99+
#endif
98100
};
99101

100102
/**

src/core/crypto/hmac_sha256.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ namespace Crypto {
4141

4242
HmacSha256::HmacSha256(void)
4343
{
44+
#if OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
45+
mContext.mContext = nullptr;
46+
mContext.mContextSize = 0;
47+
#else
4448
mContext.mContext = mContextStorage;
4549
mContext.mContextSize = sizeof(mContextStorage);
50+
#endif
4651

4752
SuccessOrAssert(otPlatCryptoHmacSha256Init(&mContext));
4853
}

src/core/crypto/hmac_sha256.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ class HmacSha256
124124

125125
private:
126126
otCryptoContext mContext;
127+
#if !OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
127128
OT_DEFINE_ALIGNED_VAR(mContextStorage, kHmacSha256ContextSize, uint64_t);
129+
#endif
128130
};
129131

130132
/**

src/core/crypto/sha256.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ namespace Crypto {
4242

4343
Sha256::Sha256(void)
4444
{
45+
#if OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
46+
mContext.mContext = nullptr;
47+
mContext.mContextSize = 0;
48+
#else
4549
mContext.mContext = mContextStorage;
4650
mContext.mContextSize = sizeof(mContextStorage);
51+
#endif
52+
4753
SuccessOrAssert(otPlatCryptoSha256Init(&mContext));
4854
}
4955

0 commit comments

Comments
 (0)