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 // This is a C++ header-only implementation of Grisu2 algorithm from the publication:
IanBenzMaxim 0:33d4e66780c0 16 // Loitsch, Florian. "Printing floating-point numbers quickly and accurately with
IanBenzMaxim 0:33d4e66780c0 17 // integers." ACM Sigplan Notices 45.6 (2010): 233-243.
IanBenzMaxim 0:33d4e66780c0 18
IanBenzMaxim 0:33d4e66780c0 19 #ifndef RAPIDJSON_DTOA_
IanBenzMaxim 0:33d4e66780c0 20 #define RAPIDJSON_DTOA_
IanBenzMaxim 0:33d4e66780c0 21
IanBenzMaxim 0:33d4e66780c0 22 #include "itoa.h" // GetDigitsLut()
IanBenzMaxim 0:33d4e66780c0 23 #include "diyfp.h"
IanBenzMaxim 0:33d4e66780c0 24 #include "ieee754.h"
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 #ifdef __GNUC__
IanBenzMaxim 0:33d4e66780c0 30 RAPIDJSON_DIAG_PUSH
IanBenzMaxim 0:33d4e66780c0 31 RAPIDJSON_DIAG_OFF(effc++)
IanBenzMaxim 0:33d4e66780c0 32 RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124
IanBenzMaxim 0:33d4e66780c0 33 #endif
IanBenzMaxim 0:33d4e66780c0 34
IanBenzMaxim 0:33d4e66780c0 35 inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
IanBenzMaxim 0:33d4e66780c0 36 while (rest < wp_w && delta - rest >= ten_kappa &&
IanBenzMaxim 0:33d4e66780c0 37 (rest + ten_kappa < wp_w || /// closer
IanBenzMaxim 0:33d4e66780c0 38 wp_w - rest > rest + ten_kappa - wp_w)) {
IanBenzMaxim 0:33d4e66780c0 39 buffer[len - 1]--;
IanBenzMaxim 0:33d4e66780c0 40 rest += ten_kappa;
IanBenzMaxim 0:33d4e66780c0 41 }
IanBenzMaxim 0:33d4e66780c0 42 }
IanBenzMaxim 0:33d4e66780c0 43
IanBenzMaxim 0:33d4e66780c0 44 inline unsigned CountDecimalDigit32(uint32_t n) {
IanBenzMaxim 0:33d4e66780c0 45 // Simple pure C++ implementation was faster than __builtin_clz version in this situation.
IanBenzMaxim 0:33d4e66780c0 46 if (n < 10) return 1;
IanBenzMaxim 0:33d4e66780c0 47 if (n < 100) return 2;
IanBenzMaxim 0:33d4e66780c0 48 if (n < 1000) return 3;
IanBenzMaxim 0:33d4e66780c0 49 if (n < 10000) return 4;
IanBenzMaxim 0:33d4e66780c0 50 if (n < 100000) return 5;
IanBenzMaxim 0:33d4e66780c0 51 if (n < 1000000) return 6;
IanBenzMaxim 0:33d4e66780c0 52 if (n < 10000000) return 7;
IanBenzMaxim 0:33d4e66780c0 53 if (n < 100000000) return 8;
IanBenzMaxim 0:33d4e66780c0 54 // Will not reach 10 digits in DigitGen()
IanBenzMaxim 0:33d4e66780c0 55 //if (n < 1000000000) return 9;
IanBenzMaxim 0:33d4e66780c0 56 //return 10;
IanBenzMaxim 0:33d4e66780c0 57 return 9;
IanBenzMaxim 0:33d4e66780c0 58 }
IanBenzMaxim 0:33d4e66780c0 59
IanBenzMaxim 0:33d4e66780c0 60 inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) {
IanBenzMaxim 0:33d4e66780c0 61 static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
IanBenzMaxim 0:33d4e66780c0 62 const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
IanBenzMaxim 0:33d4e66780c0 63 const DiyFp wp_w = Mp - W;
IanBenzMaxim 0:33d4e66780c0 64 uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
IanBenzMaxim 0:33d4e66780c0 65 uint64_t p2 = Mp.f & (one.f - 1);
IanBenzMaxim 0:33d4e66780c0 66 unsigned kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
IanBenzMaxim 0:33d4e66780c0 67 *len = 0;
IanBenzMaxim 0:33d4e66780c0 68
IanBenzMaxim 0:33d4e66780c0 69 while (kappa > 0) {
IanBenzMaxim 0:33d4e66780c0 70 uint32_t d = 0;
IanBenzMaxim 0:33d4e66780c0 71 switch (kappa) {
IanBenzMaxim 0:33d4e66780c0 72 case 9: d = p1 / 100000000; p1 %= 100000000; break;
IanBenzMaxim 0:33d4e66780c0 73 case 8: d = p1 / 10000000; p1 %= 10000000; break;
IanBenzMaxim 0:33d4e66780c0 74 case 7: d = p1 / 1000000; p1 %= 1000000; break;
IanBenzMaxim 0:33d4e66780c0 75 case 6: d = p1 / 100000; p1 %= 100000; break;
IanBenzMaxim 0:33d4e66780c0 76 case 5: d = p1 / 10000; p1 %= 10000; break;
IanBenzMaxim 0:33d4e66780c0 77 case 4: d = p1 / 1000; p1 %= 1000; break;
IanBenzMaxim 0:33d4e66780c0 78 case 3: d = p1 / 100; p1 %= 100; break;
IanBenzMaxim 0:33d4e66780c0 79 case 2: d = p1 / 10; p1 %= 10; break;
IanBenzMaxim 0:33d4e66780c0 80 case 1: d = p1; p1 = 0; break;
IanBenzMaxim 0:33d4e66780c0 81 default:;
IanBenzMaxim 0:33d4e66780c0 82 }
IanBenzMaxim 0:33d4e66780c0 83 if (d || *len)
IanBenzMaxim 0:33d4e66780c0 84 buffer[(*len)++] = static_cast<char>('0' + static_cast<char>(d));
IanBenzMaxim 0:33d4e66780c0 85 kappa--;
IanBenzMaxim 0:33d4e66780c0 86 uint64_t tmp = (static_cast<uint64_t>(p1) << -one.e) + p2;
IanBenzMaxim 0:33d4e66780c0 87 if (tmp <= delta) {
IanBenzMaxim 0:33d4e66780c0 88 *K += kappa;
IanBenzMaxim 0:33d4e66780c0 89 GrisuRound(buffer, *len, delta, tmp, static_cast<uint64_t>(kPow10[kappa]) << -one.e, wp_w.f);
IanBenzMaxim 0:33d4e66780c0 90 return;
IanBenzMaxim 0:33d4e66780c0 91 }
IanBenzMaxim 0:33d4e66780c0 92 }
IanBenzMaxim 0:33d4e66780c0 93
IanBenzMaxim 0:33d4e66780c0 94 // kappa = 0
IanBenzMaxim 0:33d4e66780c0 95 for (;;) {
IanBenzMaxim 0:33d4e66780c0 96 p2 *= 10;
IanBenzMaxim 0:33d4e66780c0 97 delta *= 10;
IanBenzMaxim 0:33d4e66780c0 98 char d = static_cast<char>(p2 >> -one.e);
IanBenzMaxim 0:33d4e66780c0 99 if (d || *len)
IanBenzMaxim 0:33d4e66780c0 100 buffer[(*len)++] = static_cast<char>('0' + d);
IanBenzMaxim 0:33d4e66780c0 101 p2 &= one.f - 1;
IanBenzMaxim 0:33d4e66780c0 102 kappa--;
IanBenzMaxim 0:33d4e66780c0 103 if (p2 < delta) {
IanBenzMaxim 0:33d4e66780c0 104 *K += kappa;
IanBenzMaxim 0:33d4e66780c0 105 int index = -static_cast<int>(kappa);
IanBenzMaxim 0:33d4e66780c0 106 GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[-static_cast<int>(kappa)] : 0));
IanBenzMaxim 0:33d4e66780c0 107 return;
IanBenzMaxim 0:33d4e66780c0 108 }
IanBenzMaxim 0:33d4e66780c0 109 }
IanBenzMaxim 0:33d4e66780c0 110 }
IanBenzMaxim 0:33d4e66780c0 111
IanBenzMaxim 0:33d4e66780c0 112 inline void Grisu2(double value, char* buffer, int* length, int* K) {
IanBenzMaxim 0:33d4e66780c0 113 const DiyFp v(value);
IanBenzMaxim 0:33d4e66780c0 114 DiyFp w_m, w_p;
IanBenzMaxim 0:33d4e66780c0 115 v.NormalizedBoundaries(&w_m, &w_p);
IanBenzMaxim 0:33d4e66780c0 116
IanBenzMaxim 0:33d4e66780c0 117 const DiyFp c_mk = GetCachedPower(w_p.e, K);
IanBenzMaxim 0:33d4e66780c0 118 const DiyFp W = v.Normalize() * c_mk;
IanBenzMaxim 0:33d4e66780c0 119 DiyFp Wp = w_p * c_mk;
IanBenzMaxim 0:33d4e66780c0 120 DiyFp Wm = w_m * c_mk;
IanBenzMaxim 0:33d4e66780c0 121 Wm.f++;
IanBenzMaxim 0:33d4e66780c0 122 Wp.f--;
IanBenzMaxim 0:33d4e66780c0 123 DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
IanBenzMaxim 0:33d4e66780c0 124 }
IanBenzMaxim 0:33d4e66780c0 125
IanBenzMaxim 0:33d4e66780c0 126 inline char* WriteExponent(int K, char* buffer) {
IanBenzMaxim 0:33d4e66780c0 127 if (K < 0) {
IanBenzMaxim 0:33d4e66780c0 128 *buffer++ = '-';
IanBenzMaxim 0:33d4e66780c0 129 K = -K;
IanBenzMaxim 0:33d4e66780c0 130 }
IanBenzMaxim 0:33d4e66780c0 131
IanBenzMaxim 0:33d4e66780c0 132 if (K >= 100) {
IanBenzMaxim 0:33d4e66780c0 133 *buffer++ = static_cast<char>('0' + static_cast<char>(K / 100));
IanBenzMaxim 0:33d4e66780c0 134 K %= 100;
IanBenzMaxim 0:33d4e66780c0 135 const char* d = GetDigitsLut() + K * 2;
IanBenzMaxim 0:33d4e66780c0 136 *buffer++ = d[0];
IanBenzMaxim 0:33d4e66780c0 137 *buffer++ = d[1];
IanBenzMaxim 0:33d4e66780c0 138 }
IanBenzMaxim 0:33d4e66780c0 139 else if (K >= 10) {
IanBenzMaxim 0:33d4e66780c0 140 const char* d = GetDigitsLut() + K * 2;
IanBenzMaxim 0:33d4e66780c0 141 *buffer++ = d[0];
IanBenzMaxim 0:33d4e66780c0 142 *buffer++ = d[1];
IanBenzMaxim 0:33d4e66780c0 143 }
IanBenzMaxim 0:33d4e66780c0 144 else
IanBenzMaxim 0:33d4e66780c0 145 *buffer++ = static_cast<char>('0' + static_cast<char>(K));
IanBenzMaxim 0:33d4e66780c0 146
IanBenzMaxim 0:33d4e66780c0 147 return buffer;
IanBenzMaxim 0:33d4e66780c0 148 }
IanBenzMaxim 0:33d4e66780c0 149
IanBenzMaxim 0:33d4e66780c0 150 inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) {
IanBenzMaxim 0:33d4e66780c0 151 const int kk = length + k; // 10^(kk-1) <= v < 10^kk
IanBenzMaxim 0:33d4e66780c0 152
IanBenzMaxim 0:33d4e66780c0 153 if (0 <= k && kk <= 21) {
IanBenzMaxim 0:33d4e66780c0 154 // 1234e7 -> 12340000000
IanBenzMaxim 0:33d4e66780c0 155 for (int i = length; i < kk; i++)
IanBenzMaxim 0:33d4e66780c0 156 buffer[i] = '0';
IanBenzMaxim 0:33d4e66780c0 157 buffer[kk] = '.';
IanBenzMaxim 0:33d4e66780c0 158 buffer[kk + 1] = '0';
IanBenzMaxim 0:33d4e66780c0 159 return &buffer[kk + 2];
IanBenzMaxim 0:33d4e66780c0 160 }
IanBenzMaxim 0:33d4e66780c0 161 else if (0 < kk && kk <= 21) {
IanBenzMaxim 0:33d4e66780c0 162 // 1234e-2 -> 12.34
IanBenzMaxim 0:33d4e66780c0 163 std::memmove(&buffer[kk + 1], &buffer[kk], static_cast<size_t>(length - kk));
IanBenzMaxim 0:33d4e66780c0 164 buffer[kk] = '.';
IanBenzMaxim 0:33d4e66780c0 165 if (0 > k + maxDecimalPlaces) {
IanBenzMaxim 0:33d4e66780c0 166 // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1
IanBenzMaxim 0:33d4e66780c0 167 // Remove extra trailing zeros (at least one) after truncation.
IanBenzMaxim 0:33d4e66780c0 168 for (int i = kk + maxDecimalPlaces; i > kk + 1; i--)
IanBenzMaxim 0:33d4e66780c0 169 if (buffer[i] != '0')
IanBenzMaxim 0:33d4e66780c0 170 return &buffer[i + 1];
IanBenzMaxim 0:33d4e66780c0 171 return &buffer[kk + 2]; // Reserve one zero
IanBenzMaxim 0:33d4e66780c0 172 }
IanBenzMaxim 0:33d4e66780c0 173 else
IanBenzMaxim 0:33d4e66780c0 174 return &buffer[length + 1];
IanBenzMaxim 0:33d4e66780c0 175 }
IanBenzMaxim 0:33d4e66780c0 176 else if (-6 < kk && kk <= 0) {
IanBenzMaxim 0:33d4e66780c0 177 // 1234e-6 -> 0.001234
IanBenzMaxim 0:33d4e66780c0 178 const int offset = 2 - kk;
IanBenzMaxim 0:33d4e66780c0 179 std::memmove(&buffer[offset], &buffer[0], static_cast<size_t>(length));
IanBenzMaxim 0:33d4e66780c0 180 buffer[0] = '0';
IanBenzMaxim 0:33d4e66780c0 181 buffer[1] = '.';
IanBenzMaxim 0:33d4e66780c0 182 for (int i = 2; i < offset; i++)
IanBenzMaxim 0:33d4e66780c0 183 buffer[i] = '0';
IanBenzMaxim 0:33d4e66780c0 184 if (length - kk > maxDecimalPlaces) {
IanBenzMaxim 0:33d4e66780c0 185 // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1
IanBenzMaxim 0:33d4e66780c0 186 // Remove extra trailing zeros (at least one) after truncation.
IanBenzMaxim 0:33d4e66780c0 187 for (int i = maxDecimalPlaces + 1; i > 2; i--)
IanBenzMaxim 0:33d4e66780c0 188 if (buffer[i] != '0')
IanBenzMaxim 0:33d4e66780c0 189 return &buffer[i + 1];
IanBenzMaxim 0:33d4e66780c0 190 return &buffer[3]; // Reserve one zero
IanBenzMaxim 0:33d4e66780c0 191 }
IanBenzMaxim 0:33d4e66780c0 192 else
IanBenzMaxim 0:33d4e66780c0 193 return &buffer[length + offset];
IanBenzMaxim 0:33d4e66780c0 194 }
IanBenzMaxim 0:33d4e66780c0 195 else if (kk < -maxDecimalPlaces) {
IanBenzMaxim 0:33d4e66780c0 196 // Truncate to zero
IanBenzMaxim 0:33d4e66780c0 197 buffer[0] = '0';
IanBenzMaxim 0:33d4e66780c0 198 buffer[1] = '.';
IanBenzMaxim 0:33d4e66780c0 199 buffer[2] = '0';
IanBenzMaxim 0:33d4e66780c0 200 return &buffer[3];
IanBenzMaxim 0:33d4e66780c0 201 }
IanBenzMaxim 0:33d4e66780c0 202 else if (length == 1) {
IanBenzMaxim 0:33d4e66780c0 203 // 1e30
IanBenzMaxim 0:33d4e66780c0 204 buffer[1] = 'e';
IanBenzMaxim 0:33d4e66780c0 205 return WriteExponent(kk - 1, &buffer[2]);
IanBenzMaxim 0:33d4e66780c0 206 }
IanBenzMaxim 0:33d4e66780c0 207 else {
IanBenzMaxim 0:33d4e66780c0 208 // 1234e30 -> 1.234e33
IanBenzMaxim 0:33d4e66780c0 209 std::memmove(&buffer[2], &buffer[1], static_cast<size_t>(length - 1));
IanBenzMaxim 0:33d4e66780c0 210 buffer[1] = '.';
IanBenzMaxim 0:33d4e66780c0 211 buffer[length + 1] = 'e';
IanBenzMaxim 0:33d4e66780c0 212 return WriteExponent(kk - 1, &buffer[0 + length + 2]);
IanBenzMaxim 0:33d4e66780c0 213 }
IanBenzMaxim 0:33d4e66780c0 214 }
IanBenzMaxim 0:33d4e66780c0 215
IanBenzMaxim 0:33d4e66780c0 216 inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) {
IanBenzMaxim 0:33d4e66780c0 217 RAPIDJSON_ASSERT(maxDecimalPlaces >= 1);
IanBenzMaxim 0:33d4e66780c0 218 Double d(value);
IanBenzMaxim 0:33d4e66780c0 219 if (d.IsZero()) {
IanBenzMaxim 0:33d4e66780c0 220 if (d.Sign())
IanBenzMaxim 0:33d4e66780c0 221 *buffer++ = '-'; // -0.0, Issue #289
IanBenzMaxim 0:33d4e66780c0 222 buffer[0] = '0';
IanBenzMaxim 0:33d4e66780c0 223 buffer[1] = '.';
IanBenzMaxim 0:33d4e66780c0 224 buffer[2] = '0';
IanBenzMaxim 0:33d4e66780c0 225 return &buffer[3];
IanBenzMaxim 0:33d4e66780c0 226 }
IanBenzMaxim 0:33d4e66780c0 227 else {
IanBenzMaxim 0:33d4e66780c0 228 if (value < 0) {
IanBenzMaxim 0:33d4e66780c0 229 *buffer++ = '-';
IanBenzMaxim 0:33d4e66780c0 230 value = -value;
IanBenzMaxim 0:33d4e66780c0 231 }
IanBenzMaxim 0:33d4e66780c0 232 int length, K;
IanBenzMaxim 0:33d4e66780c0 233 Grisu2(value, buffer, &length, &K);
IanBenzMaxim 0:33d4e66780c0 234 return Prettify(buffer, length, K, maxDecimalPlaces);
IanBenzMaxim 0:33d4e66780c0 235 }
IanBenzMaxim 0:33d4e66780c0 236 }
IanBenzMaxim 0:33d4e66780c0 237
IanBenzMaxim 0:33d4e66780c0 238 #ifdef __GNUC__
IanBenzMaxim 0:33d4e66780c0 239 RAPIDJSON_DIAG_POP
IanBenzMaxim 0:33d4e66780c0 240 #endif
IanBenzMaxim 0:33d4e66780c0 241
IanBenzMaxim 0:33d4e66780c0 242 } // namespace internal
IanBenzMaxim 0:33d4e66780c0 243 RAPIDJSON_NAMESPACE_END
IanBenzMaxim 0:33d4e66780c0 244
IanBenzMaxim 0:33d4e66780c0 245 #endif // RAPIDJSON_DTOA_