[mbedtls] move heap into otInstance. (#1996)

This commit is contained in:
Buke Po
2017-07-14 08:37:12 -07:00
committed by Jonathan Hui
parent 4a1bbc8044
commit d4836a4426
5 changed files with 30 additions and 16 deletions
+8 -4
View File
@@ -38,20 +38,22 @@
#include <mbedtls/platform.h>
#include "heap.hpp"
#include "openthread-instance.h"
#include "openthread-single-instance.h"
#if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
namespace ot {
namespace Crypto {
static Heap sHeap;
static void *CAlloc(size_t aCount, size_t aSize)
{
return sHeap.CAlloc(aCount, aSize);
return otGetInstance()->mMbedTlsHeap.CAlloc(aCount, aSize);
}
static void Free(void *aPointer)
{
sHeap.Free(aPointer);
return otGetInstance()->mMbedTlsHeap.Free(aPointer);
}
MbedTls::MbedTls(void)
@@ -61,3 +63,5 @@ MbedTls::MbedTls(void)
} // namespace Crypto
} // namespace ot
#endif // #if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
+4
View File
@@ -34,6 +34,8 @@
#ifndef OT_MBEDTLS_HPP_
#define OT_MBEDTLS_HPP_
#if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
namespace ot {
namespace Crypto {
@@ -66,4 +68,6 @@ public:
} // namespace Crypto
} // namespace ot
#endif // #if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
#endif // OT_MBEDTLS_HPP_
+2
View File
@@ -49,6 +49,7 @@
#include "api/link_raw.hpp"
#endif
#include "coap/coap.hpp"
#include "crypto/heap.hpp"
#include "crypto/mbedtls.hpp"
#include "net/ip6.hpp"
#include "thread/thread_netif.hpp"
@@ -79,6 +80,7 @@ typedef struct otInstance
#if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
ot::Crypto::MbedTls mMbedTls;
ot::Crypto::Heap mMbedTlsHeap;
#endif
ot::Ip6::Ip6 mIp6;
ot::ThreadNetif mThreadNetif;
+2 -2
View File
@@ -93,7 +93,7 @@ void TestAllocateRandomly(size_t aSizeLimit, unsigned int aSeed)
do
{
size_t size = sizeof(Node) + rand() % aSizeLimit;
size_t size = sizeof(Node) + static_cast<size_t>(rand()) % aSizeLimit;
Log("TestAllocateRandomly allocating %zu bytes...", size);
last->mNext = static_cast<Node *>(heap.CAlloc(1, size));
@@ -109,7 +109,7 @@ void TestAllocateRandomly(size_t aSizeLimit, unsigned int aSeed)
++nnodes;
// 50% probability to randomly free a node.
size_t freeIndex = rand() % (nnodes * 2);
size_t freeIndex = static_cast<size_t>(rand()) % (nnodes * 2);
if (freeIndex > nnodes)
{
+14 -10
View File
@@ -63,19 +63,23 @@ void TestHmacSha256(void)
};
otInstance *instance = testInitInstance();
ot::Crypto::HmacSha256 hmac;
uint8_t hash[ot::Crypto::HmacSha256::kHashSize];
VerifyOrQuit(instance != NULL, "Null OpenThread instance");
for (int i = 0; tests[i].key != NULL; i++)
// Make sure hmac is destructed before freeing instance.
{
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);
ot::Crypto::HmacSha256 hmac;
uint8_t hash[ot::Crypto::HmacSha256::kHashSize];
VerifyOrQuit(memcmp(hash, tests[i].hash, sizeof(tests[i].hash)) == 0,
"HMAC-SHA-256 failed\n");
VerifyOrQuit(instance != NULL, "Null OpenThread instance");
for (int i = 0; tests[i].key != NULL; 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(memcmp(hash, tests[i].hash, sizeof(tests[i].hash)) == 0,
"HMAC-SHA-256 failed\n");
}
}
testFreeInstance(instance);