Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.

The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface

Committer:
IanBenzMaxim
Date:
Fri May 29 16:19:22 2020 -0500
Revision:
12:7eb41621ba22
Parent:
8:5ea891c7d1a1
Updated to version 2.2.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 7:9cd16581b578 1 /*******************************************************************************
IanBenzMaxim 8:5ea891c7d1a1 2 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 7:9cd16581b578 3 *
IanBenzMaxim 7:9cd16581b578 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 7:9cd16581b578 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 7:9cd16581b578 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 7:9cd16581b578 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 7:9cd16581b578 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 7:9cd16581b578 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 7:9cd16581b578 10 *
IanBenzMaxim 7:9cd16581b578 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 7:9cd16581b578 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 7:9cd16581b578 13 *
IanBenzMaxim 7:9cd16581b578 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 7:9cd16581b578 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 7:9cd16581b578 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 7:9cd16581b578 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 7:9cd16581b578 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 7:9cd16581b578 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 7:9cd16581b578 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 7:9cd16581b578 21 *
IanBenzMaxim 7:9cd16581b578 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 7:9cd16581b578 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 7:9cd16581b578 24 * Products, Inc. Branding Policy.
IanBenzMaxim 7:9cd16581b578 25 *
IanBenzMaxim 7:9cd16581b578 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 7:9cd16581b578 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 7:9cd16581b578 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 7:9cd16581b578 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 7:9cd16581b578 30 * ownership rights.
IanBenzMaxim 7:9cd16581b578 31 *******************************************************************************/
IanBenzMaxim 7:9cd16581b578 32
IanBenzMaxim 8:5ea891c7d1a1 33 #ifndef MaximInterfaceCore_Ecc256_hpp
IanBenzMaxim 8:5ea891c7d1a1 34 #define MaximInterfaceCore_Ecc256_hpp
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 7:9cd16581b578 36 #include <stdint.h>
IanBenzMaxim 7:9cd16581b578 37 #include "Algorithm.hpp"
IanBenzMaxim 7:9cd16581b578 38 #include "array_span.hpp"
IanBenzMaxim 7:9cd16581b578 39 #include "Config.hpp"
IanBenzMaxim 7:9cd16581b578 40 #include "ManId.hpp"
IanBenzMaxim 7:9cd16581b578 41 #include "RomId.hpp"
IanBenzMaxim 7:9cd16581b578 42 #include "Unconstructible.hpp"
IanBenzMaxim 7:9cd16581b578 43
IanBenzMaxim 7:9cd16581b578 44 namespace MaximInterfaceCore {
IanBenzMaxim 7:9cd16581b578 45 namespace Ecc256 {
IanBenzMaxim 7:9cd16581b578 46
IanBenzMaxim 7:9cd16581b578 47 typedef array_span<uint_least8_t, 32> Scalar;
IanBenzMaxim 7:9cd16581b578 48
IanBenzMaxim 7:9cd16581b578 49 struct Point : Unconstructible {
IanBenzMaxim 7:9cd16581b578 50 struct const_span {
IanBenzMaxim 7:9cd16581b578 51 Scalar::const_span x;
IanBenzMaxim 7:9cd16581b578 52 Scalar::const_span y;
IanBenzMaxim 7:9cd16581b578 53 };
IanBenzMaxim 7:9cd16581b578 54
IanBenzMaxim 7:9cd16581b578 55 struct span {
IanBenzMaxim 7:9cd16581b578 56 Scalar::span x;
IanBenzMaxim 7:9cd16581b578 57 Scalar::span y;
IanBenzMaxim 7:9cd16581b578 58
IanBenzMaxim 7:9cd16581b578 59 operator const_span() const {
IanBenzMaxim 7:9cd16581b578 60 const const_span sp = {x, y};
IanBenzMaxim 7:9cd16581b578 61 return sp;
IanBenzMaxim 7:9cd16581b578 62 }
IanBenzMaxim 7:9cd16581b578 63 };
IanBenzMaxim 7:9cd16581b578 64
IanBenzMaxim 7:9cd16581b578 65 struct array {
IanBenzMaxim 7:9cd16581b578 66 Scalar::array x;
IanBenzMaxim 7:9cd16581b578 67 Scalar::array y;
IanBenzMaxim 7:9cd16581b578 68
IanBenzMaxim 7:9cd16581b578 69 operator span() {
IanBenzMaxim 7:9cd16581b578 70 const span sp = {x, y};
IanBenzMaxim 7:9cd16581b578 71 return sp;
IanBenzMaxim 7:9cd16581b578 72 }
IanBenzMaxim 7:9cd16581b578 73
IanBenzMaxim 7:9cd16581b578 74 operator const_span() const {
IanBenzMaxim 7:9cd16581b578 75 const const_span sp = {x, y};
IanBenzMaxim 7:9cd16581b578 76 return sp;
IanBenzMaxim 7:9cd16581b578 77 }
IanBenzMaxim 7:9cd16581b578 78 };
IanBenzMaxim 7:9cd16581b578 79 };
IanBenzMaxim 7:9cd16581b578 80
IanBenzMaxim 7:9cd16581b578 81 MaximInterfaceCore_EXPORT void copy(Point::const_span src, Point::span dst);
IanBenzMaxim 7:9cd16581b578 82
IanBenzMaxim 8:5ea891c7d1a1 83 MaximInterfaceCore_EXPORT bool equal(Point::const_span lhs,
IanBenzMaxim 8:5ea891c7d1a1 84 Point::const_span rhs);
IanBenzMaxim 8:5ea891c7d1a1 85
IanBenzMaxim 7:9cd16581b578 86 typedef Scalar PrivateKey;
IanBenzMaxim 7:9cd16581b578 87 typedef Point PublicKey;
IanBenzMaxim 7:9cd16581b578 88
IanBenzMaxim 7:9cd16581b578 89 struct KeyPair : Unconstructible {
IanBenzMaxim 7:9cd16581b578 90 struct const_span {
IanBenzMaxim 7:9cd16581b578 91 PrivateKey::const_span privateKey;
IanBenzMaxim 7:9cd16581b578 92 PublicKey::const_span publicKey;
IanBenzMaxim 7:9cd16581b578 93 };
IanBenzMaxim 7:9cd16581b578 94
IanBenzMaxim 7:9cd16581b578 95 struct span {
IanBenzMaxim 7:9cd16581b578 96 PrivateKey::span privateKey;
IanBenzMaxim 7:9cd16581b578 97 PublicKey::span publicKey;
IanBenzMaxim 7:9cd16581b578 98
IanBenzMaxim 7:9cd16581b578 99 operator const_span() const {
IanBenzMaxim 7:9cd16581b578 100 const const_span sp = {privateKey, publicKey};
IanBenzMaxim 7:9cd16581b578 101 return sp;
IanBenzMaxim 7:9cd16581b578 102 }
IanBenzMaxim 7:9cd16581b578 103 };
IanBenzMaxim 7:9cd16581b578 104
IanBenzMaxim 7:9cd16581b578 105 struct array {
IanBenzMaxim 7:9cd16581b578 106 PrivateKey::array privateKey;
IanBenzMaxim 7:9cd16581b578 107 PublicKey::array publicKey;
IanBenzMaxim 7:9cd16581b578 108
IanBenzMaxim 7:9cd16581b578 109 operator span() {
IanBenzMaxim 7:9cd16581b578 110 const span sp = {privateKey, publicKey};
IanBenzMaxim 7:9cd16581b578 111 return sp;
IanBenzMaxim 7:9cd16581b578 112 }
IanBenzMaxim 7:9cd16581b578 113
IanBenzMaxim 7:9cd16581b578 114 operator const_span() const {
IanBenzMaxim 7:9cd16581b578 115 const const_span sp = {privateKey, publicKey};
IanBenzMaxim 7:9cd16581b578 116 return sp;
IanBenzMaxim 7:9cd16581b578 117 }
IanBenzMaxim 7:9cd16581b578 118 };
IanBenzMaxim 7:9cd16581b578 119 };
IanBenzMaxim 7:9cd16581b578 120
IanBenzMaxim 7:9cd16581b578 121 MaximInterfaceCore_EXPORT void copy(KeyPair::const_span src, KeyPair::span dst);
IanBenzMaxim 7:9cd16581b578 122
IanBenzMaxim 8:5ea891c7d1a1 123 MaximInterfaceCore_EXPORT bool equal(KeyPair::const_span lhs,
IanBenzMaxim 8:5ea891c7d1a1 124 KeyPair::const_span rhs);
IanBenzMaxim 8:5ea891c7d1a1 125
IanBenzMaxim 7:9cd16581b578 126 struct Signature : Unconstructible {
IanBenzMaxim 7:9cd16581b578 127 struct const_span {
IanBenzMaxim 7:9cd16581b578 128 Scalar::const_span r;
IanBenzMaxim 7:9cd16581b578 129 Scalar::const_span s;
IanBenzMaxim 7:9cd16581b578 130 };
IanBenzMaxim 7:9cd16581b578 131
IanBenzMaxim 7:9cd16581b578 132 struct span {
IanBenzMaxim 7:9cd16581b578 133 Scalar::span r;
IanBenzMaxim 7:9cd16581b578 134 Scalar::span s;
IanBenzMaxim 7:9cd16581b578 135
IanBenzMaxim 7:9cd16581b578 136 operator const_span() const {
IanBenzMaxim 7:9cd16581b578 137 const const_span sp = {r, s};
IanBenzMaxim 7:9cd16581b578 138 return sp;
IanBenzMaxim 7:9cd16581b578 139 }
IanBenzMaxim 7:9cd16581b578 140 };
IanBenzMaxim 7:9cd16581b578 141
IanBenzMaxim 7:9cd16581b578 142 struct array {
IanBenzMaxim 7:9cd16581b578 143 Scalar::array r;
IanBenzMaxim 7:9cd16581b578 144 Scalar::array s;
IanBenzMaxim 7:9cd16581b578 145
IanBenzMaxim 7:9cd16581b578 146 operator span() {
IanBenzMaxim 7:9cd16581b578 147 const span sp = {r, s};
IanBenzMaxim 7:9cd16581b578 148 return sp;
IanBenzMaxim 7:9cd16581b578 149 }
IanBenzMaxim 7:9cd16581b578 150
IanBenzMaxim 7:9cd16581b578 151 operator const_span() const {
IanBenzMaxim 7:9cd16581b578 152 const const_span sp = {r, s};
IanBenzMaxim 7:9cd16581b578 153 return sp;
IanBenzMaxim 7:9cd16581b578 154 }
IanBenzMaxim 7:9cd16581b578 155 };
IanBenzMaxim 7:9cd16581b578 156 };
IanBenzMaxim 7:9cd16581b578 157
IanBenzMaxim 7:9cd16581b578 158 MaximInterfaceCore_EXPORT void copy(Signature::const_span src,
IanBenzMaxim 7:9cd16581b578 159 Signature::span dst);
IanBenzMaxim 7:9cd16581b578 160
IanBenzMaxim 8:5ea891c7d1a1 161 MaximInterfaceCore_EXPORT bool equal(Signature::const_span lhs,
IanBenzMaxim 8:5ea891c7d1a1 162 Signature::const_span rhs);
IanBenzMaxim 8:5ea891c7d1a1 163
IanBenzMaxim 7:9cd16581b578 164 /// Data used to create a device key certificate for ECC-256 authenticators.
IanBenzMaxim 7:9cd16581b578 165 class CertificateData {
IanBenzMaxim 7:9cd16581b578 166 public:
IanBenzMaxim 7:9cd16581b578 167 typedef array_span<uint_least8_t,
IanBenzMaxim 7:9cd16581b578 168 2 * Scalar::size + RomId::size + ManId::size>
IanBenzMaxim 7:9cd16581b578 169 Result;
IanBenzMaxim 7:9cd16581b578 170
IanBenzMaxim 7:9cd16581b578 171 CertificateData() : result_() {}
IanBenzMaxim 7:9cd16581b578 172
IanBenzMaxim 7:9cd16581b578 173 /// Formatted data result.
IanBenzMaxim 7:9cd16581b578 174 Result::const_span result() const { return result_; }
IanBenzMaxim 7:9cd16581b578 175
IanBenzMaxim 7:9cd16581b578 176 /// @name Public Key
IanBenzMaxim 7:9cd16581b578 177 /// @brief Public key of the device.
IanBenzMaxim 7:9cd16581b578 178 /// @{
IanBenzMaxim 7:9cd16581b578 179
IanBenzMaxim 7:9cd16581b578 180 /// Get mutable Public Key.
IanBenzMaxim 7:9cd16581b578 181 MaximInterfaceCore_EXPORT PublicKey::span publicKey();
IanBenzMaxim 7:9cd16581b578 182
IanBenzMaxim 7:9cd16581b578 183 /// Get immutable Public Key.
IanBenzMaxim 7:9cd16581b578 184 PublicKey::const_span publicKey() const {
IanBenzMaxim 7:9cd16581b578 185 return const_cast<CertificateData &>(*this).publicKey();
IanBenzMaxim 7:9cd16581b578 186 }
IanBenzMaxim 7:9cd16581b578 187
IanBenzMaxim 7:9cd16581b578 188 /// Set Public Key.
IanBenzMaxim 7:9cd16581b578 189 CertificateData & setPublicKey(PublicKey::const_span publicKey) {
IanBenzMaxim 7:9cd16581b578 190 copy(publicKey, this->publicKey());
IanBenzMaxim 7:9cd16581b578 191 return *this;
IanBenzMaxim 7:9cd16581b578 192 }
IanBenzMaxim 7:9cd16581b578 193
IanBenzMaxim 7:9cd16581b578 194 /// @}
IanBenzMaxim 7:9cd16581b578 195
IanBenzMaxim 7:9cd16581b578 196 /// @name ROM ID
IanBenzMaxim 7:9cd16581b578 197 /// @brief 1-Wire ROM ID of the device.
IanBenzMaxim 7:9cd16581b578 198 /// @{
IanBenzMaxim 7:9cd16581b578 199
IanBenzMaxim 7:9cd16581b578 200 /// Get mutable ROM ID.
IanBenzMaxim 7:9cd16581b578 201 RomId::span romId() {
IanBenzMaxim 7:9cd16581b578 202 return make_span(result_).subspan<romIdIdx, RomId::size>();
IanBenzMaxim 7:9cd16581b578 203 }
IanBenzMaxim 7:9cd16581b578 204
IanBenzMaxim 7:9cd16581b578 205 /// Get immutable ROM ID.
IanBenzMaxim 7:9cd16581b578 206 RomId::const_span romId() const {
IanBenzMaxim 7:9cd16581b578 207 return const_cast<CertificateData &>(*this).romId();
IanBenzMaxim 7:9cd16581b578 208 }
IanBenzMaxim 7:9cd16581b578 209
IanBenzMaxim 7:9cd16581b578 210 /// Set ROM ID.
IanBenzMaxim 7:9cd16581b578 211 CertificateData & setRomId(RomId::const_span romId) {
IanBenzMaxim 7:9cd16581b578 212 copy(romId, this->romId());
IanBenzMaxim 7:9cd16581b578 213 return *this;
IanBenzMaxim 7:9cd16581b578 214 }
IanBenzMaxim 7:9cd16581b578 215
IanBenzMaxim 7:9cd16581b578 216 /// @}
IanBenzMaxim 7:9cd16581b578 217
IanBenzMaxim 7:9cd16581b578 218 /// @name MAN ID
IanBenzMaxim 7:9cd16581b578 219 /// @brief Manufacturer ID of the device.
IanBenzMaxim 7:9cd16581b578 220 /// @{
IanBenzMaxim 7:9cd16581b578 221
IanBenzMaxim 7:9cd16581b578 222 /// Get mutable MAN ID.
IanBenzMaxim 7:9cd16581b578 223 ManId::span manId() {
IanBenzMaxim 7:9cd16581b578 224 return make_span(result_).subspan<manIdIdx, ManId::size>();
IanBenzMaxim 7:9cd16581b578 225 }
IanBenzMaxim 7:9cd16581b578 226
IanBenzMaxim 7:9cd16581b578 227 /// Get immutable MAN ID.
IanBenzMaxim 7:9cd16581b578 228 ManId::const_span manId() const {
IanBenzMaxim 7:9cd16581b578 229 return const_cast<CertificateData &>(*this).manId();
IanBenzMaxim 7:9cd16581b578 230 }
IanBenzMaxim 7:9cd16581b578 231
IanBenzMaxim 7:9cd16581b578 232 /// Set MAN ID.
IanBenzMaxim 7:9cd16581b578 233 CertificateData & setManId(ManId::const_span manId) {
IanBenzMaxim 7:9cd16581b578 234 copy(manId, this->manId());
IanBenzMaxim 7:9cd16581b578 235 return *this;
IanBenzMaxim 7:9cd16581b578 236 }
IanBenzMaxim 7:9cd16581b578 237
IanBenzMaxim 7:9cd16581b578 238 /// @}
IanBenzMaxim 7:9cd16581b578 239
IanBenzMaxim 7:9cd16581b578 240 private:
IanBenzMaxim 8:5ea891c7d1a1 241 static const size_t publicKeyIdx = 0;
IanBenzMaxim 8:5ea891c7d1a1 242 static const size_t romIdIdx = publicKeyIdx + 2 * Scalar::size;
IanBenzMaxim 8:5ea891c7d1a1 243 static const size_t manIdIdx = romIdIdx + RomId::size;
IanBenzMaxim 7:9cd16581b578 244
IanBenzMaxim 7:9cd16581b578 245 Result::array result_;
IanBenzMaxim 7:9cd16581b578 246 };
IanBenzMaxim 7:9cd16581b578 247
IanBenzMaxim 7:9cd16581b578 248 } // namespace Ecc256
IanBenzMaxim 7:9cd16581b578 249 } // namespace MaximInterfaceCore
IanBenzMaxim 7:9cd16581b578 250
IanBenzMaxim 7:9cd16581b578 251 #endif