mirror of
https://github.com/espressif/openthread.git
synced 2026-08-12 13:47:47 +00:00
Add constructors and destructors to crypto objects (#1306)
* Add constructors and destructors to crypto objects Ensures mbedtls init and free functions are called when crypto objects enter and fall out of scope. * fixed trailing space
This commit is contained in:
committed by
Jonathan Hui
parent
0823158ac8
commit
11c1b4982d
@@ -36,9 +36,13 @@
|
||||
namespace Thread {
|
||||
namespace Crypto {
|
||||
|
||||
void AesEcb::SetKey(const uint8_t *aKey, uint16_t aKeyLength)
|
||||
AesEcb::AesEcb()
|
||||
{
|
||||
mbedtls_aes_init(&mContext);
|
||||
}
|
||||
|
||||
void AesEcb::SetKey(const uint8_t *aKey, uint16_t aKeyLength)
|
||||
{
|
||||
mbedtls_aes_setkey_enc(&mContext, aKey, aKeyLength);
|
||||
}
|
||||
|
||||
@@ -47,5 +51,10 @@ void AesEcb::Encrypt(const uint8_t aInput[kBlockSize], uint8_t aOutput[kBlockSiz
|
||||
mbedtls_aes_crypt_ecb(&mContext, MBEDTLS_AES_ENCRYPT, aInput, aOutput);
|
||||
}
|
||||
|
||||
AesEcb::~AesEcb()
|
||||
{
|
||||
mbedtls_aes_free(&mContext);
|
||||
}
|
||||
|
||||
} // namespace Crypto
|
||||
} // namespace Thread
|
||||
|
||||
@@ -58,6 +58,18 @@ public:
|
||||
kBlockSize = 16, ///< AES-128 block size (bytes).
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor to initialize the mbedtls_aes_context.
|
||||
*
|
||||
*/
|
||||
AesEcb();
|
||||
|
||||
/**
|
||||
* Destructor to free the mbedtls_aes_context.
|
||||
*
|
||||
*/
|
||||
~AesEcb();
|
||||
|
||||
/**
|
||||
* This method sets the key.
|
||||
*
|
||||
|
||||
@@ -36,12 +36,21 @@
|
||||
namespace Thread {
|
||||
namespace Crypto {
|
||||
|
||||
void HmacSha256::Start(const uint8_t *aKey, uint16_t aKeyLength)
|
||||
HmacSha256::HmacSha256()
|
||||
{
|
||||
const mbedtls_md_info_t *mdInfo = NULL;
|
||||
mbedtls_md_init(&mContext);
|
||||
mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
|
||||
mbedtls_md_setup(&mContext, mdInfo, 1);
|
||||
}
|
||||
|
||||
HmacSha256::~HmacSha256()
|
||||
{
|
||||
mbedtls_md_free(&mContext);
|
||||
}
|
||||
|
||||
void HmacSha256::Start(const uint8_t *aKey, uint16_t aKeyLength)
|
||||
{
|
||||
mbedtls_md_hmac_starts(&mContext, aKey, aKeyLength);
|
||||
}
|
||||
|
||||
@@ -53,7 +62,6 @@ void HmacSha256::Update(const uint8_t *aBuf, uint16_t aBufLength)
|
||||
void HmacSha256::Finish(uint8_t aHash[kHashSize])
|
||||
{
|
||||
mbedtls_md_hmac_finish(&mContext, aHash);
|
||||
mbedtls_md_free(&mContext);
|
||||
}
|
||||
|
||||
} // namespace Crypto
|
||||
|
||||
@@ -60,6 +60,18 @@ public:
|
||||
kHashSize = 32, ///< SHA-256 hash size (bytes)
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor for initialization of mbedtls_md_context_t.
|
||||
*
|
||||
*/
|
||||
HmacSha256();
|
||||
|
||||
/**
|
||||
* Destructor for freeing of mbedtls_md_context_t.
|
||||
*
|
||||
*/
|
||||
~HmacSha256();
|
||||
|
||||
/**
|
||||
* This method sets the key.
|
||||
*
|
||||
|
||||
@@ -36,9 +36,18 @@
|
||||
namespace Thread {
|
||||
namespace Crypto {
|
||||
|
||||
void Sha256::Start(void)
|
||||
Sha256::Sha256()
|
||||
{
|
||||
mbedtls_sha256_init(&mContext);
|
||||
}
|
||||
|
||||
Sha256::~Sha256()
|
||||
{
|
||||
mbedtls_sha256_free(&mContext);
|
||||
}
|
||||
|
||||
void Sha256::Start(void)
|
||||
{
|
||||
mbedtls_sha256_starts(&mContext, 0);
|
||||
}
|
||||
|
||||
@@ -50,7 +59,6 @@ void Sha256::Update(const uint8_t *aBuf, uint16_t aBufLength)
|
||||
void Sha256::Finish(uint8_t aHash[kHashSize])
|
||||
{
|
||||
mbedtls_sha256_finish(&mContext, aHash);
|
||||
mbedtls_sha256_free(&mContext);
|
||||
}
|
||||
|
||||
} // namespace Crypto
|
||||
|
||||
@@ -60,6 +60,18 @@ public:
|
||||
kHashSize = 32, ///< SHA-256 hash size (bytes)
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor for initializing mbedtls_sha256_context.
|
||||
*
|
||||
*/
|
||||
Sha256();
|
||||
|
||||
/**
|
||||
* Destructor for freeing mbedtls_sha256_context.
|
||||
*
|
||||
*/
|
||||
~Sha256();
|
||||
|
||||
/**
|
||||
* This method starts the SHA-256 computation.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user