Skip to content

Commit ee61113

Browse files
[crypto] update mbedtls psa crypto context structs
1 parent 12aa812 commit ee61113

10 files changed

Lines changed: 56 additions & 4 deletions

File tree

include/openthread/platform/crypto.h

Lines changed: 18 additions & 4 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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS` is enabled, platform allocates and populates this.
128+
* Otherwise it is input.
126129
*/
127130
typedef struct otCryptoContext
128131
{
@@ -319,12 +322,15 @@ void otPlatCryptoFree(void *aPtr);
319322
/**
320323
* Initialize the HMAC operation.
321324
*
322-
* @param[in] aContext Context for HMAC operation.
325+
* @param[in,out] aContext Context for HMAC operation.
323326
*
324327
* @retval OT_ERROR_NONE Successfully initialized HMAC operation.
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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS` is enabled, @p aContext is output and platform MUST
332+
* populate it. Otherwise @p aContext is input.
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
*/
@@ -382,13 +388,16 @@ otError otPlatCryptoHmacSha256Finish(otCryptoContext *aContext, uint8_t *aBuf, s
382388
/**
383389
* Initialise the AES operation.
384390
*
385-
* @param[in] aContext Context for AES operation.
391+
* @param[in,out] aContext Context for AES operation.
386392
*
387393
* @retval OT_ERROR_NONE Successfully Initialised AES operation.
388394
* @retval OT_ERROR_FAILED Failed to Initialise AES operation.
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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS` is enabled, @p aContext is output and platform MUST
399+
* populate it. Otherwise @p aContext is input.
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
*/
@@ -433,12 +442,15 @@ otError otPlatCryptoAesFree(otCryptoContext *aContext);
433442
/**
434443
* Initialise the HKDF context.
435444
*
436-
* @param[in] aContext Context for HKDF operation.
445+
* @param[in,out] aContext Context for HKDF operation.
437446
*
438447
* @retval OT_ERROR_NONE Successfully Initialised AES operation.
439448
* @retval OT_ERROR_FAILED Failed to Initialise AES operation.
440449
* @retval OT_ERROR_INVALID_ARGS @p aContext was NULL
441450
*
451+
* @note If `OPENTHREAD_CONFIG_PLATFORM_ALLOCS_CRYPTO_CONTEXTS` is enabled, @p aContext is output and platform MUST
452+
* populate it. Otherwise @p aContext is input.
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
*/
@@ -493,12 +505,14 @@ otError otPlatCryptoHkdfDeinit(otCryptoContext *aContext);
493505
/**
494506
* Initialise the SHA-256 operation.
495507
*
496-
* @param[in] aContext Context for SHA-256 operation.
508+
* @param[in,out] aContext Context for SHA-256 operation.
497509
*
498510
* @retval OT_ERROR_NONE Successfully initialised SHA-256 operation.
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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS` is enabled, @p aContext is output and platform MUST
515+
* populate it. Otherwise @p aContext is input.
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/platform.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@
177177
#define OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE 0
178178
#endif
179179

180+
/**
181+
* @def OPENTHREAD_CONFIG_PLATFORM_ALLOCS_CRYPTO_CONTEXTS
182+
*
183+
* Define to 1 to enable platform allocation of crypto contexts.
184+
*/
185+
#ifndef OPENTHREAD_CONFIG_PLATFORM_ALLOCS_CRYPTO_CONTEXTS
186+
#define OPENTHREAD_CONFIG_PLATFORM_ALLOCS_CRYPTO_CONTEXTS 0
187+
#endif
188+
180189
#if OPENTHREAD_CONFIG_PLATFORM_RADIO_PROPRIETARY_SUPPORT
181190
#if (!defined(OPENTHREAD_CONFIG_PLATFORM_RADIO_PROPRIETARY_CHANNEL_PAGE) || \
182191
!defined(OPENTHREAD_CONFIG_PLATFORM_RADIO_PROPRIETARY_CHANNEL_MIN) || \

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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS
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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS
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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS
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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS
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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS
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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS
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_PLATFORM_ALLOCS_CRYPTO_CONTEXTS
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

src/core/crypto/sha256.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ class Sha256
136136

137137
private:
138138
otCryptoContext mContext;
139+
#if !OPENTHREAD_CONFIG_PLATFORM_ALLOCS_CRYPTO_CONTEXTS
139140
OT_DEFINE_ALIGNED_VAR(mContextStorage, kSha256ContextSize, uint64_t);
141+
#endif
140142
};
141143

142144
/**

0 commit comments

Comments
 (0)