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.
rapidjson/stream.h@0:33d4e66780c0, 2017-02-24 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Fri Feb 24 11:23:12 2017 -0600
- Revision:
- 0:33d4e66780c0
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New 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 | #include "rapidjson.h" |
IanBenzMaxim | 0:33d4e66780c0 | 16 | |
IanBenzMaxim | 0:33d4e66780c0 | 17 | #ifndef RAPIDJSON_STREAM_H_ |
IanBenzMaxim | 0:33d4e66780c0 | 18 | #define RAPIDJSON_STREAM_H_ |
IanBenzMaxim | 0:33d4e66780c0 | 19 | |
IanBenzMaxim | 0:33d4e66780c0 | 20 | #include "encodings.h" |
IanBenzMaxim | 0:33d4e66780c0 | 21 | |
IanBenzMaxim | 0:33d4e66780c0 | 22 | RAPIDJSON_NAMESPACE_BEGIN |
IanBenzMaxim | 0:33d4e66780c0 | 23 | |
IanBenzMaxim | 0:33d4e66780c0 | 24 | /////////////////////////////////////////////////////////////////////////////// |
IanBenzMaxim | 0:33d4e66780c0 | 25 | // Stream |
IanBenzMaxim | 0:33d4e66780c0 | 26 | |
IanBenzMaxim | 0:33d4e66780c0 | 27 | /*! \class rapidjson::Stream |
IanBenzMaxim | 0:33d4e66780c0 | 28 | \brief Concept for reading and writing characters. |
IanBenzMaxim | 0:33d4e66780c0 | 29 | |
IanBenzMaxim | 0:33d4e66780c0 | 30 | For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd(). |
IanBenzMaxim | 0:33d4e66780c0 | 31 | |
IanBenzMaxim | 0:33d4e66780c0 | 32 | For write-only stream, only need to implement Put() and Flush(). |
IanBenzMaxim | 0:33d4e66780c0 | 33 | |
IanBenzMaxim | 0:33d4e66780c0 | 34 | \code |
IanBenzMaxim | 0:33d4e66780c0 | 35 | concept Stream { |
IanBenzMaxim | 0:33d4e66780c0 | 36 | typename Ch; //!< Character type of the stream. |
IanBenzMaxim | 0:33d4e66780c0 | 37 | |
IanBenzMaxim | 0:33d4e66780c0 | 38 | //! Read the current character from stream without moving the read cursor. |
IanBenzMaxim | 0:33d4e66780c0 | 39 | Ch Peek() const; |
IanBenzMaxim | 0:33d4e66780c0 | 40 | |
IanBenzMaxim | 0:33d4e66780c0 | 41 | //! Read the current character from stream and moving the read cursor to next character. |
IanBenzMaxim | 0:33d4e66780c0 | 42 | Ch Take(); |
IanBenzMaxim | 0:33d4e66780c0 | 43 | |
IanBenzMaxim | 0:33d4e66780c0 | 44 | //! Get the current read cursor. |
IanBenzMaxim | 0:33d4e66780c0 | 45 | //! \return Number of characters read from start. |
IanBenzMaxim | 0:33d4e66780c0 | 46 | size_t Tell(); |
IanBenzMaxim | 0:33d4e66780c0 | 47 | |
IanBenzMaxim | 0:33d4e66780c0 | 48 | //! Begin writing operation at the current read pointer. |
IanBenzMaxim | 0:33d4e66780c0 | 49 | //! \return The begin writer pointer. |
IanBenzMaxim | 0:33d4e66780c0 | 50 | Ch* PutBegin(); |
IanBenzMaxim | 0:33d4e66780c0 | 51 | |
IanBenzMaxim | 0:33d4e66780c0 | 52 | //! Write a character. |
IanBenzMaxim | 0:33d4e66780c0 | 53 | void Put(Ch c); |
IanBenzMaxim | 0:33d4e66780c0 | 54 | |
IanBenzMaxim | 0:33d4e66780c0 | 55 | //! Flush the buffer. |
IanBenzMaxim | 0:33d4e66780c0 | 56 | void Flush(); |
IanBenzMaxim | 0:33d4e66780c0 | 57 | |
IanBenzMaxim | 0:33d4e66780c0 | 58 | //! End the writing operation. |
IanBenzMaxim | 0:33d4e66780c0 | 59 | //! \param begin The begin write pointer returned by PutBegin(). |
IanBenzMaxim | 0:33d4e66780c0 | 60 | //! \return Number of characters written. |
IanBenzMaxim | 0:33d4e66780c0 | 61 | size_t PutEnd(Ch* begin); |
IanBenzMaxim | 0:33d4e66780c0 | 62 | } |
IanBenzMaxim | 0:33d4e66780c0 | 63 | \endcode |
IanBenzMaxim | 0:33d4e66780c0 | 64 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 65 | |
IanBenzMaxim | 0:33d4e66780c0 | 66 | //! Provides additional information for stream. |
IanBenzMaxim | 0:33d4e66780c0 | 67 | /*! |
IanBenzMaxim | 0:33d4e66780c0 | 68 | By using traits pattern, this type provides a default configuration for stream. |
IanBenzMaxim | 0:33d4e66780c0 | 69 | For custom stream, this type can be specialized for other configuration. |
IanBenzMaxim | 0:33d4e66780c0 | 70 | See TEST(Reader, CustomStringStream) in readertest.cpp for example. |
IanBenzMaxim | 0:33d4e66780c0 | 71 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 72 | template<typename Stream> |
IanBenzMaxim | 0:33d4e66780c0 | 73 | struct StreamTraits { |
IanBenzMaxim | 0:33d4e66780c0 | 74 | //! Whether to make local copy of stream for optimization during parsing. |
IanBenzMaxim | 0:33d4e66780c0 | 75 | /*! |
IanBenzMaxim | 0:33d4e66780c0 | 76 | By default, for safety, streams do not use local copy optimization. |
IanBenzMaxim | 0:33d4e66780c0 | 77 | Stream that can be copied fast should specialize this, like StreamTraits<StringStream>. |
IanBenzMaxim | 0:33d4e66780c0 | 78 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 79 | enum { copyOptimization = 0 }; |
IanBenzMaxim | 0:33d4e66780c0 | 80 | }; |
IanBenzMaxim | 0:33d4e66780c0 | 81 | |
IanBenzMaxim | 0:33d4e66780c0 | 82 | //! Reserve n characters for writing to a stream. |
IanBenzMaxim | 0:33d4e66780c0 | 83 | template<typename Stream> |
IanBenzMaxim | 0:33d4e66780c0 | 84 | inline void PutReserve(Stream& stream, size_t count) { |
IanBenzMaxim | 0:33d4e66780c0 | 85 | (void)stream; |
IanBenzMaxim | 0:33d4e66780c0 | 86 | (void)count; |
IanBenzMaxim | 0:33d4e66780c0 | 87 | } |
IanBenzMaxim | 0:33d4e66780c0 | 88 | |
IanBenzMaxim | 0:33d4e66780c0 | 89 | //! Write character to a stream, presuming buffer is reserved. |
IanBenzMaxim | 0:33d4e66780c0 | 90 | template<typename Stream> |
IanBenzMaxim | 0:33d4e66780c0 | 91 | inline void PutUnsafe(Stream& stream, typename Stream::Ch c) { |
IanBenzMaxim | 0:33d4e66780c0 | 92 | stream.Put(c); |
IanBenzMaxim | 0:33d4e66780c0 | 93 | } |
IanBenzMaxim | 0:33d4e66780c0 | 94 | |
IanBenzMaxim | 0:33d4e66780c0 | 95 | //! Put N copies of a character to a stream. |
IanBenzMaxim | 0:33d4e66780c0 | 96 | template<typename Stream, typename Ch> |
IanBenzMaxim | 0:33d4e66780c0 | 97 | inline void PutN(Stream& stream, Ch c, size_t n) { |
IanBenzMaxim | 0:33d4e66780c0 | 98 | PutReserve(stream, n); |
IanBenzMaxim | 0:33d4e66780c0 | 99 | for (size_t i = 0; i < n; i++) |
IanBenzMaxim | 0:33d4e66780c0 | 100 | PutUnsafe(stream, c); |
IanBenzMaxim | 0:33d4e66780c0 | 101 | } |
IanBenzMaxim | 0:33d4e66780c0 | 102 | |
IanBenzMaxim | 0:33d4e66780c0 | 103 | /////////////////////////////////////////////////////////////////////////////// |
IanBenzMaxim | 0:33d4e66780c0 | 104 | // StringStream |
IanBenzMaxim | 0:33d4e66780c0 | 105 | |
IanBenzMaxim | 0:33d4e66780c0 | 106 | //! Read-only string stream. |
IanBenzMaxim | 0:33d4e66780c0 | 107 | /*! \note implements Stream concept |
IanBenzMaxim | 0:33d4e66780c0 | 108 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 109 | template <typename Encoding> |
IanBenzMaxim | 0:33d4e66780c0 | 110 | struct GenericStringStream { |
IanBenzMaxim | 0:33d4e66780c0 | 111 | typedef typename Encoding::Ch Ch; |
IanBenzMaxim | 0:33d4e66780c0 | 112 | |
IanBenzMaxim | 0:33d4e66780c0 | 113 | GenericStringStream(const Ch *src) : src_(src), head_(src) {} |
IanBenzMaxim | 0:33d4e66780c0 | 114 | |
IanBenzMaxim | 0:33d4e66780c0 | 115 | Ch Peek() const { return *src_; } |
IanBenzMaxim | 0:33d4e66780c0 | 116 | Ch Take() { return *src_++; } |
IanBenzMaxim | 0:33d4e66780c0 | 117 | size_t Tell() const { return static_cast<size_t>(src_ - head_); } |
IanBenzMaxim | 0:33d4e66780c0 | 118 | |
IanBenzMaxim | 0:33d4e66780c0 | 119 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } |
IanBenzMaxim | 0:33d4e66780c0 | 120 | void Put(Ch) { RAPIDJSON_ASSERT(false); } |
IanBenzMaxim | 0:33d4e66780c0 | 121 | void Flush() { RAPIDJSON_ASSERT(false); } |
IanBenzMaxim | 0:33d4e66780c0 | 122 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } |
IanBenzMaxim | 0:33d4e66780c0 | 123 | |
IanBenzMaxim | 0:33d4e66780c0 | 124 | const Ch* src_; //!< Current read position. |
IanBenzMaxim | 0:33d4e66780c0 | 125 | const Ch* head_; //!< Original head of the string. |
IanBenzMaxim | 0:33d4e66780c0 | 126 | }; |
IanBenzMaxim | 0:33d4e66780c0 | 127 | |
IanBenzMaxim | 0:33d4e66780c0 | 128 | template <typename Encoding> |
IanBenzMaxim | 0:33d4e66780c0 | 129 | struct StreamTraits<GenericStringStream<Encoding> > { |
IanBenzMaxim | 0:33d4e66780c0 | 130 | enum { copyOptimization = 1 }; |
IanBenzMaxim | 0:33d4e66780c0 | 131 | }; |
IanBenzMaxim | 0:33d4e66780c0 | 132 | |
IanBenzMaxim | 0:33d4e66780c0 | 133 | //! String stream with UTF8 encoding. |
IanBenzMaxim | 0:33d4e66780c0 | 134 | typedef GenericStringStream<UTF8<> > StringStream; |
IanBenzMaxim | 0:33d4e66780c0 | 135 | |
IanBenzMaxim | 0:33d4e66780c0 | 136 | /////////////////////////////////////////////////////////////////////////////// |
IanBenzMaxim | 0:33d4e66780c0 | 137 | // InsituStringStream |
IanBenzMaxim | 0:33d4e66780c0 | 138 | |
IanBenzMaxim | 0:33d4e66780c0 | 139 | //! A read-write string stream. |
IanBenzMaxim | 0:33d4e66780c0 | 140 | /*! This string stream is particularly designed for in-situ parsing. |
IanBenzMaxim | 0:33d4e66780c0 | 141 | \note implements Stream concept |
IanBenzMaxim | 0:33d4e66780c0 | 142 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 143 | template <typename Encoding> |
IanBenzMaxim | 0:33d4e66780c0 | 144 | struct GenericInsituStringStream { |
IanBenzMaxim | 0:33d4e66780c0 | 145 | typedef typename Encoding::Ch Ch; |
IanBenzMaxim | 0:33d4e66780c0 | 146 | |
IanBenzMaxim | 0:33d4e66780c0 | 147 | GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {} |
IanBenzMaxim | 0:33d4e66780c0 | 148 | |
IanBenzMaxim | 0:33d4e66780c0 | 149 | // Read |
IanBenzMaxim | 0:33d4e66780c0 | 150 | Ch Peek() { return *src_; } |
IanBenzMaxim | 0:33d4e66780c0 | 151 | Ch Take() { return *src_++; } |
IanBenzMaxim | 0:33d4e66780c0 | 152 | size_t Tell() { return static_cast<size_t>(src_ - head_); } |
IanBenzMaxim | 0:33d4e66780c0 | 153 | |
IanBenzMaxim | 0:33d4e66780c0 | 154 | // Write |
IanBenzMaxim | 0:33d4e66780c0 | 155 | void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; } |
IanBenzMaxim | 0:33d4e66780c0 | 156 | |
IanBenzMaxim | 0:33d4e66780c0 | 157 | Ch* PutBegin() { return dst_ = src_; } |
IanBenzMaxim | 0:33d4e66780c0 | 158 | size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); } |
IanBenzMaxim | 0:33d4e66780c0 | 159 | void Flush() {} |
IanBenzMaxim | 0:33d4e66780c0 | 160 | |
IanBenzMaxim | 0:33d4e66780c0 | 161 | Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; } |
IanBenzMaxim | 0:33d4e66780c0 | 162 | void Pop(size_t count) { dst_ -= count; } |
IanBenzMaxim | 0:33d4e66780c0 | 163 | |
IanBenzMaxim | 0:33d4e66780c0 | 164 | Ch* src_; |
IanBenzMaxim | 0:33d4e66780c0 | 165 | Ch* dst_; |
IanBenzMaxim | 0:33d4e66780c0 | 166 | Ch* head_; |
IanBenzMaxim | 0:33d4e66780c0 | 167 | }; |
IanBenzMaxim | 0:33d4e66780c0 | 168 | |
IanBenzMaxim | 0:33d4e66780c0 | 169 | template <typename Encoding> |
IanBenzMaxim | 0:33d4e66780c0 | 170 | struct StreamTraits<GenericInsituStringStream<Encoding> > { |
IanBenzMaxim | 0:33d4e66780c0 | 171 | enum { copyOptimization = 1 }; |
IanBenzMaxim | 0:33d4e66780c0 | 172 | }; |
IanBenzMaxim | 0:33d4e66780c0 | 173 | |
IanBenzMaxim | 0:33d4e66780c0 | 174 | //! Insitu string stream with UTF8 encoding. |
IanBenzMaxim | 0:33d4e66780c0 | 175 | typedef GenericInsituStringStream<UTF8<> > InsituStringStream; |
IanBenzMaxim | 0:33d4e66780c0 | 176 | |
IanBenzMaxim | 0:33d4e66780c0 | 177 | RAPIDJSON_NAMESPACE_END |
IanBenzMaxim | 0:33d4e66780c0 | 178 | |
IanBenzMaxim | 0:33d4e66780c0 | 179 | #endif // RAPIDJSON_STREAM_H_ |