DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

The MAXREFDES155# is an internet-of-things (IoT) embedded-security reference design, built to authenticate and control a sensing node using elliptic-curve-based public-key cryptography with control and notification from a web server.

The hardware includes an ARM® mbed™ shield and attached sensor endpoint. The shield contains a DS2476 DeepCover® ECDSA/SHA-2 coprocessor, Wifi communication, LCD push-button controls, and status LEDs. The sensor endpoint is attached to the shield using a 300mm cable and contains a DS28C36 DeepCover ECDSA/SHA-2 authenticator, IR-thermal sensor, and aiming laser for the IR sensor. The MAXREFDES155# is equipped with a standard Arduino® form-factor shield connector for immediate testing using an mbed board such as the MAX32600MBED#. The combination of these two devices represent an IoT device. Communication to the web server is accomplished with the shield Wifi circuitry. Communication from the shield to the attached sensor module is accomplished over I2C . The sensor module represents an IoT endpoint that generates small data with a requirement for message authenticity/integrity and secure on/off operational control.

The design is hierarchical with each mbed platform and shield communicating data from the sensor node to a web server that maintains a centralized log and dispatches notifications as necessary. The simplicity of this design enables rapid integration into any star-topology IoT network to provide security with the low overhead and cost provided by the ECDSA-P256 asymmetric-key and SHA-256 symmetric-key algorithms.

More information about the MAXREFDES155# is available on the Maxim Integrated website.

Committer:
IanBenzMaxim
Date:
Fri Feb 24 11:23:12 2017 -0600
Revision:
0:33d4e66780c0
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 0:33d4e66780c0 1 // Tencent is pleased to support the open source community by making RapidJSON available.
IanBenzMaxim 0:33d4e66780c0 2 //
IanBenzMaxim 0:33d4e66780c0 3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
IanBenzMaxim 0:33d4e66780c0 4 //
IanBenzMaxim 0:33d4e66780c0 5 // Licensed under the MIT License (the "License"); you may not use this file except
IanBenzMaxim 0:33d4e66780c0 6 // in compliance with the License. You may obtain a copy of the License at
IanBenzMaxim 0:33d4e66780c0 7 //
IanBenzMaxim 0:33d4e66780c0 8 // http://opensource.org/licenses/MIT
IanBenzMaxim 0:33d4e66780c0 9 //
IanBenzMaxim 0:33d4e66780c0 10 // Unless required by applicable law or agreed to in writing, software distributed
IanBenzMaxim 0:33d4e66780c0 11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
IanBenzMaxim 0:33d4e66780c0 12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
IanBenzMaxim 0:33d4e66780c0 13 // specific language governing permissions and limitations under the License.
IanBenzMaxim 0:33d4e66780c0 14
IanBenzMaxim 0:33d4e66780c0 15 #ifndef RAPIDJSON_INTERNAL_STACK_H_
IanBenzMaxim 0:33d4e66780c0 16 #define RAPIDJSON_INTERNAL_STACK_H_
IanBenzMaxim 0:33d4e66780c0 17
IanBenzMaxim 0:33d4e66780c0 18 #include "../allocators.h"
IanBenzMaxim 0:33d4e66780c0 19 #include "swap.h"
IanBenzMaxim 0:33d4e66780c0 20
IanBenzMaxim 0:33d4e66780c0 21 #if defined(__clang__)
IanBenzMaxim 0:33d4e66780c0 22 RAPIDJSON_DIAG_PUSH
IanBenzMaxim 0:33d4e66780c0 23 RAPIDJSON_DIAG_OFF(c++98-compat)
IanBenzMaxim 0:33d4e66780c0 24 #endif
IanBenzMaxim 0:33d4e66780c0 25
IanBenzMaxim 0:33d4e66780c0 26 RAPIDJSON_NAMESPACE_BEGIN
IanBenzMaxim 0:33d4e66780c0 27 namespace internal {
IanBenzMaxim 0:33d4e66780c0 28
IanBenzMaxim 0:33d4e66780c0 29 ///////////////////////////////////////////////////////////////////////////////
IanBenzMaxim 0:33d4e66780c0 30 // Stack
IanBenzMaxim 0:33d4e66780c0 31
IanBenzMaxim 0:33d4e66780c0 32 //! A type-unsafe stack for storing different types of data.
IanBenzMaxim 0:33d4e66780c0 33 /*! \tparam Allocator Allocator for allocating stack memory.
IanBenzMaxim 0:33d4e66780c0 34 */
IanBenzMaxim 0:33d4e66780c0 35 template <typename Allocator>
IanBenzMaxim 0:33d4e66780c0 36 class Stack {
IanBenzMaxim 0:33d4e66780c0 37 public:
IanBenzMaxim 0:33d4e66780c0 38 // Optimization note: Do not allocate memory for stack_ in constructor.
IanBenzMaxim 0:33d4e66780c0 39 // Do it lazily when first Push() -> Expand() -> Resize().
IanBenzMaxim 0:33d4e66780c0 40 Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
IanBenzMaxim 0:33d4e66780c0 41 }
IanBenzMaxim 0:33d4e66780c0 42
IanBenzMaxim 0:33d4e66780c0 43 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
IanBenzMaxim 0:33d4e66780c0 44 Stack(Stack&& rhs)
IanBenzMaxim 0:33d4e66780c0 45 : allocator_(rhs.allocator_),
IanBenzMaxim 0:33d4e66780c0 46 ownAllocator_(rhs.ownAllocator_),
IanBenzMaxim 0:33d4e66780c0 47 stack_(rhs.stack_),
IanBenzMaxim 0:33d4e66780c0 48 stackTop_(rhs.stackTop_),
IanBenzMaxim 0:33d4e66780c0 49 stackEnd_(rhs.stackEnd_),
IanBenzMaxim 0:33d4e66780c0 50 initialCapacity_(rhs.initialCapacity_)
IanBenzMaxim 0:33d4e66780c0 51 {
IanBenzMaxim 0:33d4e66780c0 52 rhs.allocator_ = 0;
IanBenzMaxim 0:33d4e66780c0 53 rhs.ownAllocator_ = 0;
IanBenzMaxim 0:33d4e66780c0 54 rhs.stack_ = 0;
IanBenzMaxim 0:33d4e66780c0 55 rhs.stackTop_ = 0;
IanBenzMaxim 0:33d4e66780c0 56 rhs.stackEnd_ = 0;
IanBenzMaxim 0:33d4e66780c0 57 rhs.initialCapacity_ = 0;
IanBenzMaxim 0:33d4e66780c0 58 }
IanBenzMaxim 0:33d4e66780c0 59 #endif
IanBenzMaxim 0:33d4e66780c0 60
IanBenzMaxim 0:33d4e66780c0 61 ~Stack() {
IanBenzMaxim 0:33d4e66780c0 62 Destroy();
IanBenzMaxim 0:33d4e66780c0 63 }
IanBenzMaxim 0:33d4e66780c0 64
IanBenzMaxim 0:33d4e66780c0 65 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
IanBenzMaxim 0:33d4e66780c0 66 Stack& operator=(Stack&& rhs) {
IanBenzMaxim 0:33d4e66780c0 67 if (&rhs != this)
IanBenzMaxim 0:33d4e66780c0 68 {
IanBenzMaxim 0:33d4e66780c0 69 Destroy();
IanBenzMaxim 0:33d4e66780c0 70
IanBenzMaxim 0:33d4e66780c0 71 allocator_ = rhs.allocator_;
IanBenzMaxim 0:33d4e66780c0 72 ownAllocator_ = rhs.ownAllocator_;
IanBenzMaxim 0:33d4e66780c0 73 stack_ = rhs.stack_;
IanBenzMaxim 0:33d4e66780c0 74 stackTop_ = rhs.stackTop_;
IanBenzMaxim 0:33d4e66780c0 75 stackEnd_ = rhs.stackEnd_;
IanBenzMaxim 0:33d4e66780c0 76 initialCapacity_ = rhs.initialCapacity_;
IanBenzMaxim 0:33d4e66780c0 77
IanBenzMaxim 0:33d4e66780c0 78 rhs.allocator_ = 0;
IanBenzMaxim 0:33d4e66780c0 79 rhs.ownAllocator_ = 0;
IanBenzMaxim 0:33d4e66780c0 80 rhs.stack_ = 0;
IanBenzMaxim 0:33d4e66780c0 81 rhs.stackTop_ = 0;
IanBenzMaxim 0:33d4e66780c0 82 rhs.stackEnd_ = 0;
IanBenzMaxim 0:33d4e66780c0 83 rhs.initialCapacity_ = 0;
IanBenzMaxim 0:33d4e66780c0 84 }
IanBenzMaxim 0:33d4e66780c0 85 return *this;
IanBenzMaxim 0:33d4e66780c0 86 }
IanBenzMaxim 0:33d4e66780c0 87 #endif
IanBenzMaxim 0:33d4e66780c0 88
IanBenzMaxim 0:33d4e66780c0 89 void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT {
IanBenzMaxim 0:33d4e66780c0 90 internal::Swap(allocator_, rhs.allocator_);
IanBenzMaxim 0:33d4e66780c0 91 internal::Swap(ownAllocator_, rhs.ownAllocator_);
IanBenzMaxim 0:33d4e66780c0 92 internal::Swap(stack_, rhs.stack_);
IanBenzMaxim 0:33d4e66780c0 93 internal::Swap(stackTop_, rhs.stackTop_);
IanBenzMaxim 0:33d4e66780c0 94 internal::Swap(stackEnd_, rhs.stackEnd_);
IanBenzMaxim 0:33d4e66780c0 95 internal::Swap(initialCapacity_, rhs.initialCapacity_);
IanBenzMaxim 0:33d4e66780c0 96 }
IanBenzMaxim 0:33d4e66780c0 97
IanBenzMaxim 0:33d4e66780c0 98 void Clear() { stackTop_ = stack_; }
IanBenzMaxim 0:33d4e66780c0 99
IanBenzMaxim 0:33d4e66780c0 100 void ShrinkToFit() {
IanBenzMaxim 0:33d4e66780c0 101 if (Empty()) {
IanBenzMaxim 0:33d4e66780c0 102 // If the stack is empty, completely deallocate the memory.
IanBenzMaxim 0:33d4e66780c0 103 Allocator::Free(stack_);
IanBenzMaxim 0:33d4e66780c0 104 stack_ = 0;
IanBenzMaxim 0:33d4e66780c0 105 stackTop_ = 0;
IanBenzMaxim 0:33d4e66780c0 106 stackEnd_ = 0;
IanBenzMaxim 0:33d4e66780c0 107 }
IanBenzMaxim 0:33d4e66780c0 108 else
IanBenzMaxim 0:33d4e66780c0 109 Resize(GetSize());
IanBenzMaxim 0:33d4e66780c0 110 }
IanBenzMaxim 0:33d4e66780c0 111
IanBenzMaxim 0:33d4e66780c0 112 // Optimization note: try to minimize the size of this function for force inline.
IanBenzMaxim 0:33d4e66780c0 113 // Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
IanBenzMaxim 0:33d4e66780c0 114 template<typename T>
IanBenzMaxim 0:33d4e66780c0 115 RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) {
IanBenzMaxim 0:33d4e66780c0 116 // Expand the stack if needed
IanBenzMaxim 0:33d4e66780c0 117 if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_))
IanBenzMaxim 0:33d4e66780c0 118 Expand<T>(count);
IanBenzMaxim 0:33d4e66780c0 119 }
IanBenzMaxim 0:33d4e66780c0 120
IanBenzMaxim 0:33d4e66780c0 121 template<typename T>
IanBenzMaxim 0:33d4e66780c0 122 RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) {
IanBenzMaxim 0:33d4e66780c0 123 Reserve<T>(count);
IanBenzMaxim 0:33d4e66780c0 124 return PushUnsafe<T>(count);
IanBenzMaxim 0:33d4e66780c0 125 }
IanBenzMaxim 0:33d4e66780c0 126
IanBenzMaxim 0:33d4e66780c0 127 template<typename T>
IanBenzMaxim 0:33d4e66780c0 128 RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) {
IanBenzMaxim 0:33d4e66780c0 129 RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_);
IanBenzMaxim 0:33d4e66780c0 130 T* ret = reinterpret_cast<T*>(stackTop_);
IanBenzMaxim 0:33d4e66780c0 131 stackTop_ += sizeof(T) * count;
IanBenzMaxim 0:33d4e66780c0 132 return ret;
IanBenzMaxim 0:33d4e66780c0 133 }
IanBenzMaxim 0:33d4e66780c0 134
IanBenzMaxim 0:33d4e66780c0 135 template<typename T>
IanBenzMaxim 0:33d4e66780c0 136 T* Pop(size_t count) {
IanBenzMaxim 0:33d4e66780c0 137 RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
IanBenzMaxim 0:33d4e66780c0 138 stackTop_ -= count * sizeof(T);
IanBenzMaxim 0:33d4e66780c0 139 return reinterpret_cast<T*>(stackTop_);
IanBenzMaxim 0:33d4e66780c0 140 }
IanBenzMaxim 0:33d4e66780c0 141
IanBenzMaxim 0:33d4e66780c0 142 template<typename T>
IanBenzMaxim 0:33d4e66780c0 143 T* Top() {
IanBenzMaxim 0:33d4e66780c0 144 RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
IanBenzMaxim 0:33d4e66780c0 145 return reinterpret_cast<T*>(stackTop_ - sizeof(T));
IanBenzMaxim 0:33d4e66780c0 146 }
IanBenzMaxim 0:33d4e66780c0 147
IanBenzMaxim 0:33d4e66780c0 148 template<typename T>
IanBenzMaxim 0:33d4e66780c0 149 const T* Top() const {
IanBenzMaxim 0:33d4e66780c0 150 RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
IanBenzMaxim 0:33d4e66780c0 151 return reinterpret_cast<T*>(stackTop_ - sizeof(T));
IanBenzMaxim 0:33d4e66780c0 152 }
IanBenzMaxim 0:33d4e66780c0 153
IanBenzMaxim 0:33d4e66780c0 154 template<typename T>
IanBenzMaxim 0:33d4e66780c0 155 T* End() { return reinterpret_cast<T*>(stackTop_); }
IanBenzMaxim 0:33d4e66780c0 156
IanBenzMaxim 0:33d4e66780c0 157 template<typename T>
IanBenzMaxim 0:33d4e66780c0 158 const T* End() const { return reinterpret_cast<T*>(stackTop_); }
IanBenzMaxim 0:33d4e66780c0 159
IanBenzMaxim 0:33d4e66780c0 160 template<typename T>
IanBenzMaxim 0:33d4e66780c0 161 T* Bottom() { return reinterpret_cast<T*>(stack_); }
IanBenzMaxim 0:33d4e66780c0 162
IanBenzMaxim 0:33d4e66780c0 163 template<typename T>
IanBenzMaxim 0:33d4e66780c0 164 const T* Bottom() const { return reinterpret_cast<T*>(stack_); }
IanBenzMaxim 0:33d4e66780c0 165
IanBenzMaxim 0:33d4e66780c0 166 bool HasAllocator() const {
IanBenzMaxim 0:33d4e66780c0 167 return allocator_ != 0;
IanBenzMaxim 0:33d4e66780c0 168 }
IanBenzMaxim 0:33d4e66780c0 169
IanBenzMaxim 0:33d4e66780c0 170 Allocator& GetAllocator() {
IanBenzMaxim 0:33d4e66780c0 171 RAPIDJSON_ASSERT(allocator_);
IanBenzMaxim 0:33d4e66780c0 172 return *allocator_;
IanBenzMaxim 0:33d4e66780c0 173 }
IanBenzMaxim 0:33d4e66780c0 174
IanBenzMaxim 0:33d4e66780c0 175 bool Empty() const { return stackTop_ == stack_; }
IanBenzMaxim 0:33d4e66780c0 176 size_t GetSize() const { return static_cast<size_t>(stackTop_ - stack_); }
IanBenzMaxim 0:33d4e66780c0 177 size_t GetCapacity() const { return static_cast<size_t>(stackEnd_ - stack_); }
IanBenzMaxim 0:33d4e66780c0 178
IanBenzMaxim 0:33d4e66780c0 179 private:
IanBenzMaxim 0:33d4e66780c0 180 template<typename T>
IanBenzMaxim 0:33d4e66780c0 181 void Expand(size_t count) {
IanBenzMaxim 0:33d4e66780c0 182 // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
IanBenzMaxim 0:33d4e66780c0 183 size_t newCapacity;
IanBenzMaxim 0:33d4e66780c0 184 if (stack_ == 0) {
IanBenzMaxim 0:33d4e66780c0 185 if (!allocator_)
IanBenzMaxim 0:33d4e66780c0 186 ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
IanBenzMaxim 0:33d4e66780c0 187 newCapacity = initialCapacity_;
IanBenzMaxim 0:33d4e66780c0 188 } else {
IanBenzMaxim 0:33d4e66780c0 189 newCapacity = GetCapacity();
IanBenzMaxim 0:33d4e66780c0 190 newCapacity += (newCapacity + 1) / 2;
IanBenzMaxim 0:33d4e66780c0 191 }
IanBenzMaxim 0:33d4e66780c0 192 size_t newSize = GetSize() + sizeof(T) * count;
IanBenzMaxim 0:33d4e66780c0 193 if (newCapacity < newSize)
IanBenzMaxim 0:33d4e66780c0 194 newCapacity = newSize;
IanBenzMaxim 0:33d4e66780c0 195
IanBenzMaxim 0:33d4e66780c0 196 Resize(newCapacity);
IanBenzMaxim 0:33d4e66780c0 197 }
IanBenzMaxim 0:33d4e66780c0 198
IanBenzMaxim 0:33d4e66780c0 199 void Resize(size_t newCapacity) {
IanBenzMaxim 0:33d4e66780c0 200 const size_t size = GetSize(); // Backup the current size
IanBenzMaxim 0:33d4e66780c0 201 stack_ = static_cast<char*>(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
IanBenzMaxim 0:33d4e66780c0 202 stackTop_ = stack_ + size;
IanBenzMaxim 0:33d4e66780c0 203 stackEnd_ = stack_ + newCapacity;
IanBenzMaxim 0:33d4e66780c0 204 }
IanBenzMaxim 0:33d4e66780c0 205
IanBenzMaxim 0:33d4e66780c0 206 void Destroy() {
IanBenzMaxim 0:33d4e66780c0 207 Allocator::Free(stack_);
IanBenzMaxim 0:33d4e66780c0 208 RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
IanBenzMaxim 0:33d4e66780c0 209 }
IanBenzMaxim 0:33d4e66780c0 210
IanBenzMaxim 0:33d4e66780c0 211 // Prohibit copy constructor & assignment operator.
IanBenzMaxim 0:33d4e66780c0 212 Stack(const Stack&);
IanBenzMaxim 0:33d4e66780c0 213 Stack& operator=(const Stack&);
IanBenzMaxim 0:33d4e66780c0 214
IanBenzMaxim 0:33d4e66780c0 215 Allocator* allocator_;
IanBenzMaxim 0:33d4e66780c0 216 Allocator* ownAllocator_;
IanBenzMaxim 0:33d4e66780c0 217 char *stack_;
IanBenzMaxim 0:33d4e66780c0 218 char *stackTop_;
IanBenzMaxim 0:33d4e66780c0 219 char *stackEnd_;
IanBenzMaxim 0:33d4e66780c0 220 size_t initialCapacity_;
IanBenzMaxim 0:33d4e66780c0 221 };
IanBenzMaxim 0:33d4e66780c0 222
IanBenzMaxim 0:33d4e66780c0 223 } // namespace internal
IanBenzMaxim 0:33d4e66780c0 224 RAPIDJSON_NAMESPACE_END
IanBenzMaxim 0:33d4e66780c0 225
IanBenzMaxim 0:33d4e66780c0 226 #if defined(__clang__)
IanBenzMaxim 0:33d4e66780c0 227 RAPIDJSON_DIAG_POP
IanBenzMaxim 0:33d4e66780c0 228 #endif
IanBenzMaxim 0:33d4e66780c0 229
IanBenzMaxim 0:33d4e66780c0 230 #endif // RAPIDJSON_STACK_H_