Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ecc256.hpp Source File

Ecc256.hpp

00001 /*******************************************************************************
00002 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #ifndef MaximInterfaceCore_Ecc256_hpp
00034 #define MaximInterfaceCore_Ecc256_hpp
00035 
00036 #include <stdint.h>
00037 #include "Algorithm.hpp"
00038 #include "array_span.hpp"
00039 #include "Config.hpp"
00040 #include "ManId.hpp"
00041 #include "RomId.hpp"
00042 #include "Unconstructible.hpp"
00043 
00044 namespace MaximInterfaceCore {
00045 namespace Ecc256 {
00046 
00047 typedef array_span<uint_least8_t, 32> Scalar;
00048 
00049 struct Point : Unconstructible {
00050   struct const_span {
00051     Scalar::const_span x;
00052     Scalar::const_span y;
00053   };
00054 
00055   struct span {
00056     Scalar::span x;
00057     Scalar::span y;
00058 
00059     operator const_span() const {
00060       const const_span sp = {x, y};
00061       return sp;
00062     }
00063   };
00064 
00065   struct array {
00066     Scalar::array x;
00067     Scalar::array y;
00068 
00069     operator span() {
00070       const span sp = {x, y};
00071       return sp;
00072     }
00073 
00074     operator const_span() const {
00075       const const_span sp = {x, y};
00076       return sp;
00077     }
00078   };
00079 };
00080 
00081 MaximInterfaceCore_EXPORT void copy(Point::const_span src, Point::span dst);
00082 
00083 MaximInterfaceCore_EXPORT bool equal(Point::const_span lhs,
00084                                      Point::const_span rhs);
00085 
00086 typedef Scalar PrivateKey;
00087 typedef Point PublicKey;
00088 
00089 struct KeyPair : Unconstructible {
00090   struct const_span {
00091     PrivateKey::const_span privateKey;
00092     PublicKey::const_span publicKey;
00093   };
00094 
00095   struct span {
00096     PrivateKey::span privateKey;
00097     PublicKey::span publicKey;
00098 
00099     operator const_span() const {
00100       const const_span sp = {privateKey, publicKey};
00101       return sp;
00102     }
00103   };
00104 
00105   struct array {
00106     PrivateKey::array privateKey;
00107     PublicKey::array publicKey;
00108 
00109     operator span() {
00110       const span sp = {privateKey, publicKey};
00111       return sp;
00112     }
00113 
00114     operator const_span() const {
00115       const const_span sp = {privateKey, publicKey};
00116       return sp;
00117     }
00118   };
00119 };
00120 
00121 MaximInterfaceCore_EXPORT void copy(KeyPair::const_span src, KeyPair::span dst);
00122 
00123 MaximInterfaceCore_EXPORT bool equal(KeyPair::const_span lhs,
00124                                      KeyPair::const_span rhs);
00125 
00126 struct Signature : Unconstructible {
00127   struct const_span {
00128     Scalar::const_span r;
00129     Scalar::const_span s;
00130   };
00131 
00132   struct span {
00133     Scalar::span r;
00134     Scalar::span s;
00135 
00136     operator const_span() const {
00137       const const_span sp = {r, s};
00138       return sp;
00139     }
00140   };
00141 
00142   struct array {
00143     Scalar::array r;
00144     Scalar::array s;
00145 
00146     operator span() {
00147       const span sp = {r, s};
00148       return sp;
00149     }
00150 
00151     operator const_span() const {
00152       const const_span sp = {r, s};
00153       return sp;
00154     }
00155   };
00156 };
00157 
00158 MaximInterfaceCore_EXPORT void copy(Signature::const_span src,
00159                                     Signature::span dst);
00160 
00161 MaximInterfaceCore_EXPORT bool equal(Signature::const_span lhs,
00162                                      Signature::const_span rhs);
00163 
00164 /// Data used to create a device key certificate for ECC-256 authenticators.
00165 class CertificateData {
00166 public:
00167   typedef array_span<uint_least8_t,
00168                      2 * Scalar::size + RomId::size + ManId::size>
00169       Result;
00170 
00171   CertificateData() : result_() {}
00172 
00173   /// Formatted data result.
00174   Result::const_span result() const { return result_; }
00175 
00176   /// @name Public Key
00177   /// @brief Public key of the device.
00178   /// @{
00179 
00180   /// Get mutable Public Key.
00181   MaximInterfaceCore_EXPORT PublicKey::span publicKey();
00182 
00183   /// Get immutable Public Key.
00184   PublicKey::const_span publicKey() const {
00185     return const_cast<CertificateData &>(*this).publicKey();
00186   }
00187 
00188   /// Set Public Key.
00189   CertificateData & setPublicKey(PublicKey::const_span publicKey) {
00190     copy(publicKey, this->publicKey());
00191     return *this;
00192   }
00193 
00194   /// @}
00195 
00196   /// @name ROM ID
00197   /// @brief 1-Wire ROM ID of the device.
00198   /// @{
00199 
00200   /// Get mutable ROM ID.
00201   RomId::span romId() {
00202     return make_span(result_).subspan<romIdIdx, RomId::size>();
00203   }
00204 
00205   /// Get immutable ROM ID.
00206   RomId::const_span romId() const {
00207     return const_cast<CertificateData &>(*this).romId();
00208   }
00209 
00210   /// Set ROM ID.
00211   CertificateData & setRomId(RomId::const_span romId) {
00212     copy(romId, this->romId());
00213     return *this;
00214   }
00215 
00216   /// @}
00217 
00218   /// @name MAN ID
00219   /// @brief Manufacturer ID of the device.
00220   /// @{
00221 
00222   /// Get mutable MAN ID.
00223   ManId::span manId() {
00224     return make_span(result_).subspan<manIdIdx, ManId::size>();
00225   }
00226 
00227   /// Get immutable MAN ID.
00228   ManId::const_span manId() const {
00229     return const_cast<CertificateData &>(*this).manId();
00230   }
00231 
00232   /// Set MAN ID.
00233   CertificateData & setManId(ManId::const_span manId) {
00234     copy(manId, this->manId());
00235     return *this;
00236   }
00237 
00238   /// @}
00239 
00240 private:
00241   static const size_t publicKeyIdx = 0;
00242   static const size_t romIdIdx = publicKeyIdx + 2 * Scalar::size;
00243   static const size_t manIdIdx = romIdIdx + RomId::size;
00244 
00245   Result::array result_;
00246 };
00247 
00248 } // namespace Ecc256
00249 } // namespace MaximInterfaceCore
00250 
00251 #endif