Aleksandrs Gumenuks / MaximInterface_Extended

Dependents:   mbed_DS28EC20_GPIO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ecc256.hpp Source File

Ecc256.hpp

00001 /*******************************************************************************
00002 * Copyright (C) 2017 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 MaximInterface_Ecc256
00034 #define MaximInterface_Ecc256
00035 
00036 #include <stdint.h>
00037 #include "array_span.hpp"
00038 #include "Export.h"
00039 #include "ManId.hpp"
00040 #include "RomId.hpp"
00041 
00042 namespace MaximInterface {
00043 namespace Ecc256 {
00044 
00045 typedef array_span<uint_least8_t, 32> Scalar;
00046 
00047 struct Point {
00048   struct const_span {
00049     Scalar::const_span x;
00050     Scalar::const_span y;
00051   };
00052 
00053   struct span {
00054     Scalar::span x;
00055     Scalar::span y;
00056 
00057     operator const_span() const {
00058       const const_span sp = {x, y};
00059       return sp;
00060     }
00061   };
00062 
00063   struct array {
00064     Scalar::array x;
00065     Scalar::array y;
00066 
00067     operator span() {
00068       const span sp = {x, y};
00069       return sp;
00070     }
00071     
00072     operator const_span() const {
00073       const const_span sp = {x, y};
00074       return sp;
00075     }
00076   };
00077   
00078 private:
00079   Point(); // deleted
00080 };
00081 
00082 MaximInterface_EXPORT void copy(Point::const_span src, Point::span dst);
00083 
00084 typedef Scalar PrivateKey;
00085 typedef Point PublicKey;
00086 
00087 struct KeyPair {
00088   struct const_span {
00089     PrivateKey::const_span privateKey;
00090     PublicKey::const_span publicKey;
00091   };
00092 
00093   struct span {
00094     PrivateKey::span privateKey;
00095     PublicKey::span publicKey;
00096 
00097     operator const_span() const {
00098       const const_span sp = {privateKey, publicKey};
00099       return sp;
00100     }
00101   };
00102 
00103   struct array {
00104     PrivateKey::array privateKey;
00105     PublicKey::array publicKey;
00106 
00107     operator span() {
00108       const span sp = {privateKey, publicKey};
00109       return sp;
00110     }
00111     
00112     operator const_span() const {
00113       const const_span sp = {privateKey, publicKey};
00114       return sp;
00115     }
00116   };
00117   
00118 private:
00119   KeyPair(); // deleted
00120 };
00121 
00122 MaximInterface_EXPORT void copy(KeyPair::const_span src, KeyPair::span dst);
00123 
00124 struct Signature {
00125   struct const_span {
00126     Scalar::const_span r;
00127     Scalar::const_span s;
00128   };
00129 
00130   struct span {
00131     Scalar::span r;
00132     Scalar::span s;
00133 
00134     operator const_span() const {
00135       const const_span sp = {r, s};
00136       return sp;
00137     }
00138   };
00139 
00140   struct array {
00141     Scalar::array r;
00142     Scalar::array s;
00143 
00144     operator span() {
00145       const span sp = {r, s};
00146       return sp;
00147     }
00148     
00149     operator const_span() const {
00150       const const_span sp = {r, s};
00151       return sp;
00152     }
00153   };
00154   
00155 private:
00156   Signature(); // deleted
00157 };
00158 
00159 MaximInterface_EXPORT void copy(Signature::const_span src, Signature::span dst);
00160 
00161 /// Data used to create a device key certificate for ECC-256 authenticators.
00162 class CertificateData {
00163 public:
00164   typedef array_span<uint_least8_t, 2 * Scalar::size + RomId::size + ManId::size>
00165       Result;
00166 
00167   CertificateData() : result_() {}
00168 
00169   /// Formatted data result.
00170   Result::const_span result() const { return result_; }
00171 
00172   /// @name Public Key
00173   /// @brief Public key of the device.
00174   /// @{
00175   
00176   /// Get mutable Public Key.
00177   MaximInterface_EXPORT PublicKey::span publicKey();
00178   
00179   /// Get immutable Public Key.
00180   PublicKey::const_span publicKey() const {
00181     return const_cast<CertificateData &>(*this).publicKey();
00182   }
00183   
00184   /// Set Public Key.
00185   CertificateData & setPublicKey(PublicKey::const_span publicKey) {
00186     copy(publicKey, this->publicKey());
00187     return *this;
00188   }
00189   
00190   /// @}
00191 
00192   /// @name ROM ID
00193   /// @brief 1-Wire ROM ID of the device.
00194   /// @{
00195   
00196   /// Get mutable ROM ID.
00197   RomId::span romId() {
00198     return make_span(result_).subspan<romIdIdx, RomId::size>();
00199   }
00200   
00201   /// Get immutable ROM ID.
00202   RomId::const_span romId() const {
00203     return const_cast<CertificateData &>(*this).romId();
00204   }
00205   
00206   /// Set ROM ID.
00207   CertificateData & setRomId(RomId::const_span romId) {
00208     copy(romId, this->romId());
00209     return *this;
00210   }
00211   
00212   /// @}
00213 
00214   /// @name MAN ID
00215   /// @brief Manufacturer ID of the device.
00216   /// @{
00217   
00218   /// Get mutable MAN ID.
00219   ManId::span manId() {
00220     return make_span(result_).subspan<manIdIdx, ManId::size>();
00221   }
00222   
00223   /// Get immutable MAN ID.
00224   ManId::const_span manId() const {
00225     return const_cast<CertificateData &>(*this).manId();
00226   }
00227   
00228   /// Set MAN ID.
00229   CertificateData & setManId(ManId::const_span manId) {
00230     copy(manId, this->manId());
00231     return *this;
00232   }
00233   
00234   /// @}
00235 
00236 private:
00237   typedef Result::span::index_type index;
00238 
00239   static const index publicKeyIdx = 0;
00240   static const index romIdIdx = publicKeyIdx + 2 * Scalar::size;
00241   static const index manIdIdx = romIdIdx + RomId::size;
00242 
00243   Result::array result_;
00244 };
00245 
00246 } // namespace Ecc256
00247 } // namespace MaximInterface
00248 
00249 #endif