mirror of
https://github.com/espressif/openthread.git
synced 2026-10-02 22:07:36 +00:00
[crypto] add ContextWith template to simplify context allocation (#12885)
This commit introduces the `Context` and `ContextWith<kContextSize>` helper classes in the `Crypto` namespace to wrap `otCryptoContext` and manage its storage allocation. `ContextWith<kContextSize>` handles the buffer allocation based on the configuration `OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT`, automatically clearing and setting the buffer. The `AesEcb`, `HkdfSha256`, `HmacSha256`, and `Sha256` classes are updated to use the new `ContextWith` template for their `mContext` members. This simplifies their initialization sequences and constructors.
This commit is contained in:
@@ -272,7 +272,7 @@ exit:
|
|||||||
*
|
*
|
||||||
* @return The result of division and rounding to the closest integer.
|
* @return The result of division and rounding to the closest integer.
|
||||||
*/
|
*/
|
||||||
template <typename IntType> inline IntType DivideAndRoundToClosest(IntType aDividend, IntType aDivisor)
|
template <typename IntType> inline constexpr IntType DivideAndRoundToClosest(IntType aDividend, IntType aDivisor)
|
||||||
{
|
{
|
||||||
return (aDividend + (aDivisor / 2)) / aDivisor;
|
return (aDividend + (aDivisor / 2)) / aDivisor;
|
||||||
}
|
}
|
||||||
@@ -287,7 +287,7 @@ template <typename IntType> inline IntType DivideAndRoundToClosest(IntType aDivi
|
|||||||
*
|
*
|
||||||
* @return The result of division and rounding up.
|
* @return The result of division and rounding up.
|
||||||
*/
|
*/
|
||||||
template <typename IntType> inline IntType DivideAndRoundUp(IntType aDividend, IntType aDivisor)
|
template <typename IntType> inline constexpr IntType DivideAndRoundUp(IntType aDividend, IntType aDivisor)
|
||||||
{
|
{
|
||||||
return (aDividend + (aDivisor - 1)) / aDivisor;
|
return (aDividend + (aDivisor - 1)) / aDivisor;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,17 +38,7 @@
|
|||||||
namespace ot {
|
namespace ot {
|
||||||
namespace Crypto {
|
namespace Crypto {
|
||||||
|
|
||||||
AesEcb::AesEcb(void)
|
AesEcb::AesEcb(void) { SuccessOrAssert(otPlatCryptoAesInit(&mContext)); }
|
||||||
{
|
|
||||||
#if OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
|
|
||||||
mContext.mContext = nullptr;
|
|
||||||
mContext.mContextSize = 0;
|
|
||||||
#else
|
|
||||||
mContext.mContext = mContextStorage;
|
|
||||||
mContext.mContextSize = sizeof(mContextStorage);
|
|
||||||
#endif
|
|
||||||
SuccessOrAssert(otPlatCryptoAesInit(&mContext));
|
|
||||||
}
|
|
||||||
|
|
||||||
void AesEcb::SetKey(const Key &aKey) { SuccessOrAssert(otPlatCryptoAesSetKey(&mContext, &aKey)); }
|
void AesEcb::SetKey(const Key &aKey) { SuccessOrAssert(otPlatCryptoAesSetKey(&mContext, &aKey)); }
|
||||||
|
|
||||||
|
|||||||
@@ -85,10 +85,7 @@ public:
|
|||||||
void Encrypt(const uint8_t aInput[kBlockSize], uint8_t aOutput[kBlockSize]);
|
void Encrypt(const uint8_t aInput[kBlockSize], uint8_t aOutput[kBlockSize]);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
otCryptoContext mContext;
|
ContextWith<kAesContextSize> mContext;
|
||||||
#if !OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
|
|
||||||
OT_DEFINE_ALIGNED_VAR(mContextStorage, kAesContextSize, uint64_t);
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -42,17 +42,7 @@
|
|||||||
namespace ot {
|
namespace ot {
|
||||||
namespace Crypto {
|
namespace Crypto {
|
||||||
|
|
||||||
HkdfSha256::HkdfSha256(void)
|
HkdfSha256::HkdfSha256(void) { SuccessOrAssert(otPlatCryptoHkdfInit(&mContext)); }
|
||||||
{
|
|
||||||
#if OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
|
|
||||||
mContext.mContext = nullptr;
|
|
||||||
mContext.mContextSize = 0;
|
|
||||||
#else
|
|
||||||
mContext.mContext = mContextStorage;
|
|
||||||
mContext.mContextSize = sizeof(mContextStorage);
|
|
||||||
#endif
|
|
||||||
SuccessOrAssert(otPlatCryptoHkdfInit(&mContext));
|
|
||||||
}
|
|
||||||
|
|
||||||
HkdfSha256::~HkdfSha256(void) { SuccessOrAssert(otPlatCryptoHkdfDeinit(&mContext)); }
|
HkdfSha256::~HkdfSha256(void) { SuccessOrAssert(otPlatCryptoHkdfDeinit(&mContext)); }
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,7 @@
|
|||||||
#include "common/code_utils.hpp"
|
#include "common/code_utils.hpp"
|
||||||
#include "crypto/context_size.hpp"
|
#include "crypto/context_size.hpp"
|
||||||
#include "crypto/hmac_sha256.hpp"
|
#include "crypto/hmac_sha256.hpp"
|
||||||
|
#include "crypto/storage.hpp"
|
||||||
|
|
||||||
namespace ot {
|
namespace ot {
|
||||||
namespace Crypto {
|
namespace Crypto {
|
||||||
@@ -93,10 +94,7 @@ public:
|
|||||||
void Expand(const uint8_t *aInfo, uint16_t aInfoLength, uint8_t *aOutputKey, uint16_t aOutputKeyLength);
|
void Expand(const uint8_t *aInfo, uint16_t aInfoLength, uint8_t *aOutputKey, uint16_t aOutputKeyLength);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
otCryptoContext mContext;
|
ContextWith<kHkdfContextSize> mContext;
|
||||||
#if !OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
|
|
||||||
OT_DEFINE_ALIGNED_VAR(mContextStorage, kHkdfContextSize, uint64_t);
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -39,18 +39,7 @@
|
|||||||
namespace ot {
|
namespace ot {
|
||||||
namespace Crypto {
|
namespace Crypto {
|
||||||
|
|
||||||
HmacSha256::HmacSha256(void)
|
HmacSha256::HmacSha256(void) { SuccessOrAssert(otPlatCryptoHmacSha256Init(&mContext)); }
|
||||||
{
|
|
||||||
#if OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
|
|
||||||
mContext.mContext = nullptr;
|
|
||||||
mContext.mContextSize = 0;
|
|
||||||
#else
|
|
||||||
mContext.mContext = mContextStorage;
|
|
||||||
mContext.mContextSize = sizeof(mContextStorage);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SuccessOrAssert(otPlatCryptoHmacSha256Init(&mContext));
|
|
||||||
}
|
|
||||||
|
|
||||||
HmacSha256::~HmacSha256(void) { SuccessOrAssert(otPlatCryptoHmacSha256Deinit(&mContext)); }
|
HmacSha256::~HmacSha256(void) { SuccessOrAssert(otPlatCryptoHmacSha256Deinit(&mContext)); }
|
||||||
|
|
||||||
|
|||||||
@@ -123,10 +123,7 @@ public:
|
|||||||
void Finish(Hash &aHash);
|
void Finish(Hash &aHash);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
otCryptoContext mContext;
|
ContextWith<kHmacSha256ContextSize> mContext;
|
||||||
#if !OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
|
|
||||||
OT_DEFINE_ALIGNED_VAR(mContextStorage, kHmacSha256ContextSize, uint64_t);
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -40,18 +40,7 @@
|
|||||||
namespace ot {
|
namespace ot {
|
||||||
namespace Crypto {
|
namespace Crypto {
|
||||||
|
|
||||||
Sha256::Sha256(void)
|
Sha256::Sha256(void) { SuccessOrAssert(otPlatCryptoSha256Init(&mContext)); }
|
||||||
{
|
|
||||||
#if OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
|
|
||||||
mContext.mContext = nullptr;
|
|
||||||
mContext.mContextSize = 0;
|
|
||||||
#else
|
|
||||||
mContext.mContext = mContextStorage;
|
|
||||||
mContext.mContextSize = sizeof(mContextStorage);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SuccessOrAssert(otPlatCryptoSha256Init(&mContext));
|
|
||||||
}
|
|
||||||
|
|
||||||
Sha256::~Sha256(void) { SuccessOrAssert(otPlatCryptoSha256Deinit(&mContext)); }
|
Sha256::~Sha256(void) { SuccessOrAssert(otPlatCryptoSha256Deinit(&mContext)); }
|
||||||
|
|
||||||
|
|||||||
@@ -47,6 +47,7 @@
|
|||||||
#include "common/equatable.hpp"
|
#include "common/equatable.hpp"
|
||||||
#include "common/type_traits.hpp"
|
#include "common/type_traits.hpp"
|
||||||
#include "crypto/context_size.hpp"
|
#include "crypto/context_size.hpp"
|
||||||
|
#include "crypto/storage.hpp"
|
||||||
|
|
||||||
namespace ot {
|
namespace ot {
|
||||||
|
|
||||||
@@ -135,10 +136,7 @@ public:
|
|||||||
void Finish(Hash &aHash);
|
void Finish(Hash &aHash);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
otCryptoContext mContext;
|
ContextWith<kSha256ContextSize> mContext;
|
||||||
#if !OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
|
|
||||||
OT_DEFINE_ALIGNED_VAR(mContextStorage, kSha256ContextSize, uint64_t);
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -44,6 +44,7 @@
|
|||||||
#include "common/error.hpp"
|
#include "common/error.hpp"
|
||||||
#include "common/locator.hpp"
|
#include "common/locator.hpp"
|
||||||
#include "common/non_copyable.hpp"
|
#include "common/non_copyable.hpp"
|
||||||
|
#include "common/num_utils.hpp"
|
||||||
|
|
||||||
namespace ot {
|
namespace ot {
|
||||||
namespace Crypto {
|
namespace Crypto {
|
||||||
@@ -263,6 +264,60 @@ inline bool HasKey(KeyRef aKeyRef) { return otPlatCryptoHasKey(aKeyRef); }
|
|||||||
|
|
||||||
#endif // OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
|
#endif // OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a crypto context.
|
||||||
|
*/
|
||||||
|
class Context : public otCryptoContext
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Gets the pointer to the context buffer.
|
||||||
|
*
|
||||||
|
* @returns A pointer to the context buffer.
|
||||||
|
*/
|
||||||
|
void *GetContext(void) { return mContext; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the size of the context buffer.
|
||||||
|
*
|
||||||
|
* @returns The size of the context buffer in bytes.
|
||||||
|
*/
|
||||||
|
uint16_t GetSize(void) const { return mContextSize; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the context buffer.
|
||||||
|
*
|
||||||
|
* @param[in] aContext A pointer to the context buffer.
|
||||||
|
* @param[in] aSize The size of the context buffer in bytes.
|
||||||
|
*/
|
||||||
|
void SetContext(void *aContext, uint16_t aSize) { mContext = aContext, mContextSize = aSize; }
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a crypto context with a locally allocated buffer.
|
||||||
|
*
|
||||||
|
* @tparam kContextSize The size of the context buffer in bytes.
|
||||||
|
*/
|
||||||
|
template <uint16_t kContextSize> class ContextWith : public Context
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Initializes the context and the locally allocated buffer.
|
||||||
|
*/
|
||||||
|
ContextWith(void)
|
||||||
|
{
|
||||||
|
ClearAllBytes(*this);
|
||||||
|
#if !OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
|
||||||
|
SetContext(mStorage, kContextSize);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
#if !OPENTHREAD_CONFIG_CRYPTO_PLATFORM_ALLOCS_CONTEXT
|
||||||
|
uint64_t mStorage[DivideAndRoundUp<uint16_t>(kContextSize, sizeof(uint64_t))];
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a crypto key.
|
* Represents a crypto key.
|
||||||
*
|
*
|
||||||
@@ -393,6 +448,7 @@ private:
|
|||||||
|
|
||||||
} // namespace Crypto
|
} // namespace Crypto
|
||||||
|
|
||||||
|
DefineCoreType(otCryptoContext, Crypto::Context);
|
||||||
DefineCoreType(otCryptoKey, Crypto::Key);
|
DefineCoreType(otCryptoKey, Crypto::Key);
|
||||||
|
|
||||||
} // namespace ot
|
} // namespace ot
|
||||||
|
|||||||
Reference in New Issue
Block a user