mirror of
https://github.com/espressif/openthread.git
synced 2026-08-23 02:39:52 +00:00
[crypto] add new flavors of 'Update()' for SHA-256 and HMAC computation (#6002)
This commit updates `Sha256` and `HmacSha256` classes adding new flavors of `Update()` method which inputs bytes from a given buffer, or from an object, or read from a `Message` for hash calculation. This commit also updates unit test to add more test-cases (from RFC 4231) for HMAC and adds new set of test-cases for SHA-256. The test also covers calculating hash over data read directly from a `Message`.
This commit is contained in:
@@ -54,6 +54,13 @@
|
||||
|
||||
namespace ot {
|
||||
|
||||
namespace Crypto {
|
||||
|
||||
class Sha256;
|
||||
class HmacSha256;
|
||||
|
||||
} // namespace Crypto
|
||||
|
||||
/**
|
||||
* @addtogroup core-message
|
||||
*
|
||||
@@ -294,6 +301,8 @@ protected:
|
||||
class Message : public Buffer
|
||||
{
|
||||
friend class Checksum;
|
||||
friend class Crypto::HmacSha256;
|
||||
friend class Crypto::Sha256;
|
||||
friend class MessagePool;
|
||||
friend class MessageQueue;
|
||||
friend class PriorityQueue;
|
||||
|
||||
@@ -75,13 +75,13 @@ void HkdfSha256::Expand(const uint8_t *aInfo, uint16_t aInfoLength, uint8_t *aOu
|
||||
|
||||
if (iter != 0)
|
||||
{
|
||||
hmac.Update(hash.GetBytes(), sizeof(hash));
|
||||
hmac.Update(hash);
|
||||
}
|
||||
|
||||
hmac.Update(aInfo, aInfoLength);
|
||||
|
||||
iter++;
|
||||
hmac.Update(&iter, sizeof(iter));
|
||||
hmac.Update(iter);
|
||||
hmac.Finish(hash);
|
||||
|
||||
copyLength = (aOutputKeyLength > sizeof(hash)) ? sizeof(hash) : aOutputKeyLength;
|
||||
|
||||
@@ -33,6 +33,8 @@
|
||||
|
||||
#include "hmac_sha256.hpp"
|
||||
|
||||
#include "common/message.hpp"
|
||||
|
||||
namespace ot {
|
||||
namespace Crypto {
|
||||
|
||||
@@ -54,9 +56,22 @@ void HmacSha256::Start(const uint8_t *aKey, uint16_t aKeyLength)
|
||||
mbedtls_md_hmac_starts(&mContext, aKey, aKeyLength);
|
||||
}
|
||||
|
||||
void HmacSha256::Update(const uint8_t *aBuf, uint16_t aBufLength)
|
||||
void HmacSha256::Update(const void *aBuf, uint16_t aBufLength)
|
||||
{
|
||||
mbedtls_md_hmac_update(&mContext, aBuf, aBufLength);
|
||||
mbedtls_md_hmac_update(&mContext, reinterpret_cast<const uint8_t *>(aBuf), aBufLength);
|
||||
}
|
||||
|
||||
void HmacSha256::Update(const Message &aMessage, uint16_t aOffset, uint16_t aLength)
|
||||
{
|
||||
Message::Chunk chunk;
|
||||
|
||||
aMessage.GetFirstChunk(aOffset, aLength, chunk);
|
||||
|
||||
while (chunk.GetLength() > 0)
|
||||
{
|
||||
Update(chunk.GetData(), chunk.GetLength());
|
||||
aMessage.GetNextChunk(aLength, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
void HmacSha256::Finish(Hash &aHash)
|
||||
|
||||
@@ -43,6 +43,9 @@
|
||||
#include "crypto/sha256.hpp"
|
||||
|
||||
namespace ot {
|
||||
|
||||
class Message;
|
||||
|
||||
namespace Crypto {
|
||||
|
||||
/**
|
||||
@@ -66,19 +69,19 @@ public:
|
||||
typedef Sha256::Hash Hash;
|
||||
|
||||
/**
|
||||
* Constructor for initialization of mbedtls_md_context_t.
|
||||
* Constructor for `HmacSha256`.
|
||||
*
|
||||
*/
|
||||
HmacSha256(void);
|
||||
|
||||
/**
|
||||
* Destructor for freeing of mbedtls_md_context_t.
|
||||
* Destructor for `HmacSha256`.
|
||||
*
|
||||
*/
|
||||
~HmacSha256(void);
|
||||
|
||||
/**
|
||||
* This method sets the key.
|
||||
* This method sets the key and starts the HMAC computation.
|
||||
*
|
||||
* @param[in] aKey A pointer to the key.
|
||||
* @param[in] aKeyLength The key length in bytes.
|
||||
@@ -93,7 +96,31 @@ public:
|
||||
* @param[in] aBufLength The length of @p aBuf in bytes.
|
||||
*
|
||||
*/
|
||||
void Update(const uint8_t *aBuf, uint16_t aBufLength);
|
||||
void Update(const void *aBuf, uint16_t aBufLength);
|
||||
|
||||
/**
|
||||
* This method inputs an object (treated as a sequence of bytes) into the HMAC computation.
|
||||
*
|
||||
* @tparam ObjectType The object type.
|
||||
*
|
||||
* @param[in] aObject A reference to the object.
|
||||
*
|
||||
*/
|
||||
template <typename ObjectType> void Update(const ObjectType &aObject)
|
||||
{
|
||||
static_assert(!TypeTraits::IsPointer<ObjectType>::kValue, "ObjectType must not be a pointer");
|
||||
return Update(&aObject, sizeof(ObjectType));
|
||||
}
|
||||
|
||||
/**
|
||||
* This method inputs the bytes read from a given message into the HMAC computation.
|
||||
*
|
||||
* @param[in] aMessage The message to read the data from.
|
||||
* @param[in] aOffset The offset into @p aMessage to start to read.
|
||||
* @param[in] aLength The number of bytes to read.
|
||||
*
|
||||
*/
|
||||
void Update(const Message &aMessage, uint16_t aOffset, uint16_t aLength);
|
||||
|
||||
/**
|
||||
* This method finalizes the hash computation.
|
||||
|
||||
@@ -33,6 +33,8 @@
|
||||
|
||||
#include "sha256.hpp"
|
||||
|
||||
#include "common/message.hpp"
|
||||
|
||||
namespace ot {
|
||||
namespace Crypto {
|
||||
|
||||
@@ -51,9 +53,22 @@ void Sha256::Start(void)
|
||||
mbedtls_sha256_starts_ret(&mContext, 0);
|
||||
}
|
||||
|
||||
void Sha256::Update(const uint8_t *aBuf, uint16_t aBufLength)
|
||||
void Sha256::Update(const void *aBuf, uint16_t aBufLength)
|
||||
{
|
||||
mbedtls_sha256_update_ret(&mContext, aBuf, aBufLength);
|
||||
mbedtls_sha256_update_ret(&mContext, reinterpret_cast<const uint8_t *>(aBuf), aBufLength);
|
||||
}
|
||||
|
||||
void Sha256::Update(const Message &aMessage, uint16_t aOffset, uint16_t aLength)
|
||||
{
|
||||
Message::Chunk chunk;
|
||||
|
||||
aMessage.GetFirstChunk(aOffset, aLength, chunk);
|
||||
|
||||
while (chunk.GetLength() > 0)
|
||||
{
|
||||
Update(chunk.GetData(), chunk.GetLength());
|
||||
aMessage.GetNextChunk(aLength, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
void Sha256::Finish(Hash &aHash)
|
||||
|
||||
@@ -44,8 +44,12 @@
|
||||
|
||||
#include "common/clearable.hpp"
|
||||
#include "common/equatable.hpp"
|
||||
#include "common/type_traits.hpp"
|
||||
|
||||
namespace ot {
|
||||
|
||||
class Message;
|
||||
|
||||
namespace Crypto {
|
||||
|
||||
/**
|
||||
@@ -84,13 +88,13 @@ public:
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor for initializing mbedtls_sha256_context.
|
||||
* Constructor for `Sha256` object.
|
||||
*
|
||||
*/
|
||||
Sha256(void);
|
||||
|
||||
/**
|
||||
* Destructor for freeing mbedtls_sha256_context.
|
||||
* Destructor for `Sha256` object.
|
||||
*
|
||||
*/
|
||||
~Sha256(void);
|
||||
@@ -108,7 +112,31 @@ public:
|
||||
* @param[in] aBufLength The length of @p aBuf in bytes.
|
||||
*
|
||||
*/
|
||||
void Update(const uint8_t *aBuf, uint16_t aBufLength);
|
||||
void Update(const void *aBuf, uint16_t aBufLength);
|
||||
|
||||
/**
|
||||
* This method inputs an object (treated as a sequence of bytes) into the SHA-256 computation.
|
||||
*
|
||||
* @tparam ObjectType The object type.
|
||||
*
|
||||
* @param[in] aObject A reference to the object.
|
||||
*
|
||||
*/
|
||||
template <typename ObjectType> void Update(const ObjectType &aObject)
|
||||
{
|
||||
static_assert(!TypeTraits::IsPointer<ObjectType>::kValue, "ObjectType must not be a pointer");
|
||||
return Update(&aObject, sizeof(ObjectType));
|
||||
}
|
||||
|
||||
/**
|
||||
* This method inputs the bytes read from a given message into the SHA-256 computation.
|
||||
*
|
||||
* @param[in] aMessage The message to read the data from.
|
||||
* @param[in] aOffset The offset into @p aMessage to start to read.
|
||||
* @param[in] aLength The number of bytes to read.
|
||||
*
|
||||
*/
|
||||
void Update(const Message &aMessage, uint16_t aOffset, uint16_t aLength);
|
||||
|
||||
/**
|
||||
* This method finalizes the hash computation.
|
||||
|
||||
@@ -291,7 +291,7 @@ void ComputeJoinerId(const Mac::ExtAddress &aEui64, Mac::ExtAddress &aJoinerId)
|
||||
Crypto::Sha256::Hash hash;
|
||||
|
||||
sha256.Start();
|
||||
sha256.Update(aEui64.m8, sizeof(aEui64));
|
||||
sha256.Update(aEui64);
|
||||
sha256.Finish(hash);
|
||||
|
||||
memcpy(&aJoinerId, hash.GetBytes(), sizeof(aJoinerId));
|
||||
|
||||
@@ -163,8 +163,8 @@ void KeyManager::ComputeKeys(uint32_t aKeySequence, HashKeys &aHashKeys)
|
||||
hmac.Start(mMasterKey.m8, sizeof(mMasterKey.m8));
|
||||
|
||||
Encoding::BigEndian::WriteUint32(aKeySequence, keySequenceBytes);
|
||||
hmac.Update(keySequenceBytes, sizeof(keySequenceBytes));
|
||||
hmac.Update(kThreadString, sizeof(kThreadString));
|
||||
hmac.Update(keySequenceBytes);
|
||||
hmac.Update(kThreadString);
|
||||
|
||||
hmac.Finish(aHashKeys.mHash);
|
||||
}
|
||||
|
||||
@@ -294,9 +294,9 @@ otError Slaac::GenerateIid(Ip6::NetifUnicastAddress &aAddress,
|
||||
sha256.Update(aNetworkId, aNetworkIdLength);
|
||||
}
|
||||
|
||||
sha256.Update(netIface, sizeof(netIface));
|
||||
sha256.Update(reinterpret_cast<uint8_t *>(&dadCounter), sizeof(dadCounter));
|
||||
sha256.Update(secretKey.m8, sizeof(IidSecretKey));
|
||||
sha256.Update(netIface);
|
||||
sha256.Update(dadCounter);
|
||||
sha256.Update(secretKey);
|
||||
sha256.Finish(hash);
|
||||
|
||||
aAddress.GetAddress().GetIid().SetBytes(hash.GetBytes());
|
||||
|
||||
@@ -101,7 +101,7 @@ void TestEcdsaVector(void)
|
||||
|
||||
printf("\nHash the message ----------------------------------------------------------\n");
|
||||
sha256.Start();
|
||||
sha256.Update(kMessage, sizeof(kMessage));
|
||||
sha256.Update(kMessage);
|
||||
sha256.Finish(hash);
|
||||
|
||||
DumpBuffer("Hash", hash.GetBytes(), sizeof(hash));
|
||||
@@ -153,7 +153,7 @@ void TestEdsaKeyGenerationSignAndVerify(void)
|
||||
|
||||
printf("\nHash the message ----------------------------------------------------------\n");
|
||||
sha256.Start();
|
||||
sha256.Update(reinterpret_cast<const uint8_t *>(kMessage), sizeof(kMessage) - 1);
|
||||
sha256.Update(kMessage, sizeof(kMessage) - 1);
|
||||
sha256.Finish(hash);
|
||||
|
||||
DumpBuffer("Hash", hash.GetBytes(), sizeof(hash));
|
||||
@@ -167,7 +167,7 @@ void TestEdsaKeyGenerationSignAndVerify(void)
|
||||
printf("\nSignature was verified successfully.");
|
||||
|
||||
sha256.Start();
|
||||
sha256.Update(reinterpret_cast<const uint8_t *>(kMessage), sizeof(kMessage)); // include null char
|
||||
sha256.Update(kMessage, sizeof(kMessage)); // include null char
|
||||
sha256.Finish(hash);
|
||||
VerifyOrQuit(publicKey.Verify(hash, signature) != OT_ERROR_NONE,
|
||||
"PublicKey::Verify() passed for invalid signature");
|
||||
|
||||
+239
-33
@@ -29,60 +29,266 @@
|
||||
#include <openthread/config.h>
|
||||
|
||||
#include "common/debug.hpp"
|
||||
#include "common/message.hpp"
|
||||
#include "crypto/hmac_sha256.hpp"
|
||||
#include "crypto/sha256.hpp"
|
||||
|
||||
#include "test_platform.h"
|
||||
#include "test_util.h"
|
||||
|
||||
void TestHmacSha256(void)
|
||||
namespace ot {
|
||||
|
||||
void TestSha256(void)
|
||||
{
|
||||
static const struct
|
||||
const char kData1[] = "abc";
|
||||
|
||||
const otCryptoSha256Hash kHash1 = {{
|
||||
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
|
||||
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad,
|
||||
}};
|
||||
|
||||
const char kData2[] = "";
|
||||
|
||||
const otCryptoSha256Hash kHash2 = {{
|
||||
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
|
||||
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
|
||||
}};
|
||||
|
||||
const char kData3[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
|
||||
const otCryptoSha256Hash kHash3 = {{
|
||||
0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
|
||||
0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1,
|
||||
}};
|
||||
|
||||
const char kData4[] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmno"
|
||||
"pqrsmnopqrstnopqrstu";
|
||||
|
||||
const otCryptoSha256Hash kHash4 = {{
|
||||
0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37,
|
||||
0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1,
|
||||
}};
|
||||
|
||||
struct TestCase
|
||||
{
|
||||
const char * key;
|
||||
const char * data;
|
||||
otCryptoSha256Hash hash;
|
||||
} tests[] = {
|
||||
{
|
||||
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
|
||||
"Hi There",
|
||||
{{
|
||||
0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
|
||||
0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7,
|
||||
}},
|
||||
},
|
||||
{
|
||||
nullptr,
|
||||
nullptr,
|
||||
{},
|
||||
},
|
||||
const char * mData; // (null-terminated string).
|
||||
otCryptoSha256Hash mHash;
|
||||
};
|
||||
|
||||
otInstance *instance = testInitInstance();
|
||||
static const TestCase kTestCases[] = {
|
||||
{kData1, kHash1},
|
||||
{kData2, kHash2},
|
||||
{kData3, kHash3},
|
||||
{kData4, kHash4},
|
||||
};
|
||||
|
||||
// Make sure hmac is destructed before freeing instance.
|
||||
printf("TestSha256\n");
|
||||
|
||||
Instance * instance = testInitInstance();
|
||||
MessagePool *messagePool;
|
||||
Message * message;
|
||||
uint16_t offsets[OT_ARRAY_LENGTH(kTestCases)];
|
||||
uint8_t index;
|
||||
|
||||
VerifyOrQuit(instance != nullptr, "Null OpenThread instance");
|
||||
|
||||
messagePool = &instance->Get<MessagePool>();
|
||||
VerifyOrQuit((message = messagePool->New(Message::kTypeIp6, 0)) != nullptr, "Message::New failed");
|
||||
|
||||
for (const TestCase &testCase : kTestCases)
|
||||
{
|
||||
ot::Crypto::HmacSha256 hmac;
|
||||
ot::Crypto::HmacSha256::Hash hash;
|
||||
Crypto::Sha256 sha256;
|
||||
Crypto::Sha256::Hash hash;
|
||||
|
||||
VerifyOrQuit(instance != nullptr, "Null OpenThread instance");
|
||||
sha256.Start();
|
||||
sha256.Update(testCase.mData, static_cast<uint16_t>(strlen(testCase.mData)));
|
||||
sha256.Finish(hash);
|
||||
|
||||
for (int i = 0; tests[i].key != nullptr; i++)
|
||||
{
|
||||
hmac.Start(reinterpret_cast<const uint8_t *>(tests[i].key), static_cast<uint16_t>(strlen(tests[i].key)));
|
||||
hmac.Update(reinterpret_cast<const uint8_t *>(tests[i].data), static_cast<uint16_t>(strlen(tests[i].data)));
|
||||
hmac.Finish(hash);
|
||||
VerifyOrQuit(hash == static_cast<const Crypto::HmacSha256::Hash &>(testCase.mHash), "HMAC-SHA-256 failed");
|
||||
}
|
||||
|
||||
VerifyOrQuit(hash == static_cast<const ot::Crypto::HmacSha256::Hash &>(tests[i].hash),
|
||||
"HMAC-SHA-256 failed");
|
||||
}
|
||||
// Append all test case `mData` in the message.
|
||||
|
||||
index = 0;
|
||||
|
||||
for (const TestCase &testCase : kTestCases)
|
||||
{
|
||||
SuccessOrQuit(message->Append("Hello"), "Message::Append() failed");
|
||||
offsets[index++] = message->GetLength();
|
||||
SuccessOrQuit(message->AppendBytes(testCase.mData, static_cast<uint16_t>(strlen(testCase.mData))),
|
||||
"Message::AppendBytes() failed");
|
||||
SuccessOrQuit(message->Append("There!"), "Message::Append() failed");
|
||||
}
|
||||
|
||||
index = 0;
|
||||
|
||||
for (const TestCase &testCase : kTestCases)
|
||||
{
|
||||
Crypto::Sha256 sha256;
|
||||
Crypto::Sha256::Hash hash;
|
||||
|
||||
sha256.Start();
|
||||
sha256.Update(*message, offsets[index++], static_cast<uint16_t>(strlen(testCase.mData)));
|
||||
sha256.Finish(hash);
|
||||
|
||||
VerifyOrQuit(hash == static_cast<const Crypto::HmacSha256::Hash &>(testCase.mHash), "HMAC-SHA-256 failed");
|
||||
}
|
||||
|
||||
testFreeInstance(instance);
|
||||
}
|
||||
|
||||
void TestHmacSha256(void)
|
||||
{
|
||||
struct TestCase
|
||||
{
|
||||
const void * mKey;
|
||||
uint16_t mKeyLength;
|
||||
const void * mData;
|
||||
uint16_t mDataLength;
|
||||
otCryptoSha256Hash mHash;
|
||||
};
|
||||
|
||||
// Test-cases from RFC 4231.
|
||||
|
||||
const uint8_t kKey1[] = {
|
||||
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
||||
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
||||
};
|
||||
|
||||
const char kData1[] = "Hi There";
|
||||
|
||||
const otCryptoSha256Hash kHash1 = {{
|
||||
0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
|
||||
0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7,
|
||||
}};
|
||||
|
||||
const char kKey2[] = "Jefe";
|
||||
const char kData2[] = "what do ya want for nothing?";
|
||||
|
||||
const otCryptoSha256Hash kHash2 = {{
|
||||
0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7,
|
||||
0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83, 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43,
|
||||
}};
|
||||
|
||||
const uint8_t kKey3[] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||||
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
|
||||
|
||||
const uint8_t kData3[] = {
|
||||
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
|
||||
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
|
||||
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
|
||||
};
|
||||
|
||||
const otCryptoSha256Hash kHash3 = {{
|
||||
0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, 0xeb, 0xd0, 0x91, 0x81, 0xa7,
|
||||
0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe,
|
||||
}};
|
||||
|
||||
const uint8_t kKey4[] = {
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
|
||||
0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
|
||||
};
|
||||
|
||||
const uint8_t kData4[] = {
|
||||
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
|
||||
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
|
||||
0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
|
||||
};
|
||||
|
||||
const otCryptoSha256Hash kHash4 = {{
|
||||
0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc, 0x81, 0x98, 0x99, 0xf2, 0x08, 0x3a,
|
||||
0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07, 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b,
|
||||
}};
|
||||
|
||||
const uint8_t kKey5[] = {
|
||||
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||||
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||||
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||||
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||||
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||||
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||||
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||||
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
||||
};
|
||||
|
||||
const char kData5[] = "This is a test using a larger than block-size key and a larger than block-size data. The "
|
||||
"key needs to be hashed before being used by the HMAC algorithm.";
|
||||
|
||||
const otCryptoSha256Hash kHash5 = {{
|
||||
0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44,
|
||||
0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2,
|
||||
}};
|
||||
|
||||
static const TestCase kTestCases[] = {
|
||||
{kKey1, sizeof(kKey1), kData1, sizeof(kData1) - 1, kHash1},
|
||||
{kKey2, sizeof(kKey2) - 1, kData2, sizeof(kData2) - 1, kHash2},
|
||||
{kKey3, sizeof(kKey3), kData3, sizeof(kData3), kHash3},
|
||||
{kKey4, sizeof(kKey4), kData4, sizeof(kData4), kHash4},
|
||||
{kKey5, sizeof(kKey5), kData5, sizeof(kData5) - 1, kHash5},
|
||||
};
|
||||
|
||||
Instance * instance = testInitInstance();
|
||||
MessagePool *messagePool;
|
||||
Message * message;
|
||||
uint16_t offsets[OT_ARRAY_LENGTH(kTestCases)];
|
||||
uint8_t index;
|
||||
|
||||
printf("TestHmacSha256\n");
|
||||
|
||||
VerifyOrQuit(instance != nullptr, "Null OpenThread instance");
|
||||
|
||||
messagePool = &instance->Get<MessagePool>();
|
||||
VerifyOrQuit((message = messagePool->New(Message::kTypeIp6, 0)) != nullptr, "Message::New failed");
|
||||
|
||||
for (const TestCase &testCase : kTestCases)
|
||||
{
|
||||
Crypto::HmacSha256 hmac;
|
||||
Crypto::HmacSha256::Hash hash;
|
||||
|
||||
hmac.Start(reinterpret_cast<const uint8_t *>(testCase.mKey), testCase.mKeyLength);
|
||||
hmac.Update(testCase.mData, testCase.mDataLength);
|
||||
hmac.Finish(hash);
|
||||
|
||||
VerifyOrQuit(hash == static_cast<const Crypto::HmacSha256::Hash &>(testCase.mHash), "HMAC-SHA-256 failed");
|
||||
}
|
||||
|
||||
// Append all test case `mData` in the message.
|
||||
|
||||
index = 0;
|
||||
|
||||
for (const TestCase &testCase : kTestCases)
|
||||
{
|
||||
SuccessOrQuit(message->Append("Hello"), "Message::Append() failed");
|
||||
offsets[index++] = message->GetLength();
|
||||
SuccessOrQuit(message->AppendBytes(testCase.mData, testCase.mDataLength), "Message::AppendBytes() failed");
|
||||
SuccessOrQuit(message->Append("There"), "Message::Append() failed");
|
||||
}
|
||||
|
||||
index = 0;
|
||||
|
||||
for (const TestCase &testCase : kTestCases)
|
||||
{
|
||||
Crypto::HmacSha256 hmac;
|
||||
Crypto::HmacSha256::Hash hash;
|
||||
|
||||
hmac.Start(reinterpret_cast<const uint8_t *>(testCase.mKey), testCase.mKeyLength);
|
||||
hmac.Update(*message, offsets[index++], testCase.mDataLength);
|
||||
hmac.Finish(hash);
|
||||
|
||||
VerifyOrQuit(hash == static_cast<const Crypto::HmacSha256::Hash &>(testCase.mHash), "HMAC-SHA-256 failed");
|
||||
}
|
||||
|
||||
message->Free();
|
||||
|
||||
testFreeInstance(instance);
|
||||
}
|
||||
|
||||
} // namespace ot
|
||||
|
||||
int main(void)
|
||||
{
|
||||
TestHmacSha256();
|
||||
ot::TestSha256();
|
||||
ot::TestHmacSha256();
|
||||
printf("All tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user