Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Authenticators/DS28E15_22_25/DS28E15_22_25.h@74:23be10c32fa3, 2016-05-13 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Fri May 13 07:48:35 2016 -0500
- Revision:
- 74:23be10c32fa3
- Parent:
- 73:2cecc1372acc
- Child:
- 75:8b627804927c
Assimilated indentation and braces.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 25:bdb1c5a53b58 | 1 | //------------Copyright (C) 2013 Maxim Integrated Products -------------- |
IanBenzMaxim | 25:bdb1c5a53b58 | 2 | // |
IanBenzMaxim | 25:bdb1c5a53b58 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 25:bdb1c5a53b58 | 4 | // copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 25:bdb1c5a53b58 | 5 | // to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 25:bdb1c5a53b58 | 6 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 25:bdb1c5a53b58 | 7 | // and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 25:bdb1c5a53b58 | 8 | // Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 25:bdb1c5a53b58 | 9 | // |
IanBenzMaxim | 25:bdb1c5a53b58 | 10 | // The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 25:bdb1c5a53b58 | 11 | // in all copies or substantial portions of the Software. |
IanBenzMaxim | 25:bdb1c5a53b58 | 12 | // |
IanBenzMaxim | 25:bdb1c5a53b58 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 25:bdb1c5a53b58 | 14 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 25:bdb1c5a53b58 | 15 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 25:bdb1c5a53b58 | 16 | // IN NO EVENT SHALL MAXIM INTEGRATED PRODCUTS BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 25:bdb1c5a53b58 | 17 | // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 25:bdb1c5a53b58 | 18 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 25:bdb1c5a53b58 | 19 | // OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 25:bdb1c5a53b58 | 20 | // |
IanBenzMaxim | 25:bdb1c5a53b58 | 21 | // Except as contained in this notice, the name of Maxim Integrated Products |
IanBenzMaxim | 25:bdb1c5a53b58 | 22 | // shall not be used except as stated in the Maxim Integrated Products |
IanBenzMaxim | 25:bdb1c5a53b58 | 23 | // Branding Policy. |
IanBenzMaxim | 25:bdb1c5a53b58 | 24 | // --------------------------------------------------------------------------- |
IanBenzMaxim | 25:bdb1c5a53b58 | 25 | |
IanBenzMaxim | 73:2cecc1372acc | 26 | #ifndef OneWire_Authenticators_DS28E15_22_25 |
IanBenzMaxim | 73:2cecc1372acc | 27 | #define OneWire_Authenticators_DS28E15_22_25 |
IanBenzMaxim | 25:bdb1c5a53b58 | 28 | |
IanBenzMaxim | 73:2cecc1372acc | 29 | #include "array.h" |
IanBenzMaxim | 73:2cecc1372acc | 30 | #include "Authenticators/ISha256MacCoproc.h" |
IanBenzMaxim | 73:2cecc1372acc | 31 | #include "OneWireSlave.h" |
IanBenzMaxim | 73:2cecc1372acc | 32 | |
IanBenzMaxim | 73:2cecc1372acc | 33 | namespace OneWire |
IanBenzMaxim | 73:2cecc1372acc | 34 | { |
IanBenzMaxim | 74:23be10c32fa3 | 35 | namespace Masters { class OneWireMaster; } |
IanBenzMaxim | 25:bdb1c5a53b58 | 36 | |
IanBenzMaxim | 74:23be10c32fa3 | 37 | namespace Authenticators |
IanBenzMaxim | 74:23be10c32fa3 | 38 | { |
IanBenzMaxim | 74:23be10c32fa3 | 39 | /// Interface to the DS28E15/22/25 series of authenticators |
IanBenzMaxim | 74:23be10c32fa3 | 40 | /// including low power variants. |
IanBenzMaxim | 74:23be10c32fa3 | 41 | class DS28E15_22_25 : public OneWireSlave |
IanBenzMaxim | 73:2cecc1372acc | 42 | { |
IanBenzMaxim | 74:23be10c32fa3 | 43 | public: |
IanBenzMaxim | 74:23be10c32fa3 | 44 | /// Family code for each device. |
IanBenzMaxim | 74:23be10c32fa3 | 45 | enum FamilyCode |
IanBenzMaxim | 74:23be10c32fa3 | 46 | { |
IanBenzMaxim | 74:23be10c32fa3 | 47 | DS28E25_FAMILY = 0x47, |
IanBenzMaxim | 74:23be10c32fa3 | 48 | DS28E22_FAMILY = 0x48, |
IanBenzMaxim | 74:23be10c32fa3 | 49 | DS28E15_FAMILY = 0x17, |
IanBenzMaxim | 74:23be10c32fa3 | 50 | UNKNOWN_FAMILY = 0 |
IanBenzMaxim | 74:23be10c32fa3 | 51 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 52 | |
IanBenzMaxim | 74:23be10c32fa3 | 53 | /// Number for memory pages for each device. |
IanBenzMaxim | 74:23be10c32fa3 | 54 | enum MemoryPages |
IanBenzMaxim | 74:23be10c32fa3 | 55 | { |
IanBenzMaxim | 74:23be10c32fa3 | 56 | DS28E25_PAGES = 16, |
IanBenzMaxim | 74:23be10c32fa3 | 57 | DS28E22_PAGES = 8, |
IanBenzMaxim | 74:23be10c32fa3 | 58 | DS28E15_PAGES = 2, |
IanBenzMaxim | 74:23be10c32fa3 | 59 | UNKNOWN_PAGES = 0 |
IanBenzMaxim | 74:23be10c32fa3 | 60 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 61 | |
IanBenzMaxim | 74:23be10c32fa3 | 62 | /// Number of protection blocks for each device. |
IanBenzMaxim | 74:23be10c32fa3 | 63 | enum ProtectionBlocks |
IanBenzMaxim | 74:23be10c32fa3 | 64 | { |
IanBenzMaxim | 74:23be10c32fa3 | 65 | DS28E25_BLOCKS = 8, |
IanBenzMaxim | 74:23be10c32fa3 | 66 | DS28E22_BLOCKS = 4, |
IanBenzMaxim | 74:23be10c32fa3 | 67 | DS28E15_BLOCKS = 4, |
IanBenzMaxim | 74:23be10c32fa3 | 68 | UNKNOWN_BLOCKS = 0 |
IanBenzMaxim | 74:23be10c32fa3 | 69 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 70 | |
IanBenzMaxim | 74:23be10c32fa3 | 71 | /// Holds the contents of a device memory segment. |
IanBenzMaxim | 74:23be10c32fa3 | 72 | typedef array<uint8_t, 4> Segment; |
IanBenzMaxim | 74:23be10c32fa3 | 73 | |
IanBenzMaxim | 74:23be10c32fa3 | 74 | /// Holds the contents of a device memory page. |
IanBenzMaxim | 74:23be10c32fa3 | 75 | class Page |
IanBenzMaxim | 74:23be10c32fa3 | 76 | { |
IanBenzMaxim | 74:23be10c32fa3 | 77 | public: |
IanBenzMaxim | 74:23be10c32fa3 | 78 | /// Length of the buffer in bytes. |
IanBenzMaxim | 74:23be10c32fa3 | 79 | static const size_t length = 32; |
IanBenzMaxim | 74:23be10c32fa3 | 80 | |
IanBenzMaxim | 74:23be10c32fa3 | 81 | private: |
IanBenzMaxim | 74:23be10c32fa3 | 82 | array<uint8_t, length> m_data; |
IanBenzMaxim | 74:23be10c32fa3 | 83 | |
IanBenzMaxim | 74:23be10c32fa3 | 84 | public: |
IanBenzMaxim | 74:23be10c32fa3 | 85 | /// Built-in array representation. |
IanBenzMaxim | 74:23be10c32fa3 | 86 | typedef array<uint8_t, length>::Buffer Buffer; |
IanBenzMaxim | 74:23be10c32fa3 | 87 | |
IanBenzMaxim | 74:23be10c32fa3 | 88 | Page() { } |
IanBenzMaxim | 74:23be10c32fa3 | 89 | Page(const Page & page) : m_data(page.m_data) { } |
IanBenzMaxim | 74:23be10c32fa3 | 90 | Page(const Buffer & dataBytes) : m_data(dataBytes) { } |
IanBenzMaxim | 74:23be10c32fa3 | 91 | |
IanBenzMaxim | 74:23be10c32fa3 | 92 | const Page & operator=(const Page & rhs) |
IanBenzMaxim | 74:23be10c32fa3 | 93 | { |
IanBenzMaxim | 74:23be10c32fa3 | 94 | this->m_data = rhs.m_data; |
IanBenzMaxim | 74:23be10c32fa3 | 95 | return rhs; |
IanBenzMaxim | 74:23be10c32fa3 | 96 | } |
IanBenzMaxim | 74:23be10c32fa3 | 97 | |
IanBenzMaxim | 74:23be10c32fa3 | 98 | bool operator==(const Page & rhs) const |
IanBenzMaxim | 74:23be10c32fa3 | 99 | { |
IanBenzMaxim | 74:23be10c32fa3 | 100 | return (this->m_data == rhs.m_data); |
IanBenzMaxim | 74:23be10c32fa3 | 101 | } |
IanBenzMaxim | 74:23be10c32fa3 | 102 | |
IanBenzMaxim | 74:23be10c32fa3 | 103 | bool operator!=(const Page & rhs) const |
IanBenzMaxim | 74:23be10c32fa3 | 104 | { |
IanBenzMaxim | 74:23be10c32fa3 | 105 | return !operator==(rhs); |
IanBenzMaxim | 74:23be10c32fa3 | 106 | } |
IanBenzMaxim | 74:23be10c32fa3 | 107 | |
IanBenzMaxim | 74:23be10c32fa3 | 108 | /// Conversion to array reference. |
IanBenzMaxim | 74:23be10c32fa3 | 109 | operator Buffer &() |
IanBenzMaxim | 74:23be10c32fa3 | 110 | { |
IanBenzMaxim | 74:23be10c32fa3 | 111 | return m_data; |
IanBenzMaxim | 74:23be10c32fa3 | 112 | } |
IanBenzMaxim | 74:23be10c32fa3 | 113 | |
IanBenzMaxim | 74:23be10c32fa3 | 114 | /// Conversion to const array reference. |
IanBenzMaxim | 74:23be10c32fa3 | 115 | operator const Buffer &() const |
IanBenzMaxim | 74:23be10c32fa3 | 116 | { |
IanBenzMaxim | 74:23be10c32fa3 | 117 | return m_data; |
IanBenzMaxim | 74:23be10c32fa3 | 118 | } |
IanBenzMaxim | 74:23be10c32fa3 | 119 | |
IanBenzMaxim | 74:23be10c32fa3 | 120 | /// Creates a segment representation from a subsection of the page data. |
IanBenzMaxim | 74:23be10c32fa3 | 121 | /// @param segmentNum Segment number within page to copy from. |
IanBenzMaxim | 74:23be10c32fa3 | 122 | /// @returns The copied segment data. |
IanBenzMaxim | 74:23be10c32fa3 | 123 | Segment toSegment(unsigned int segmentNum) const; |
IanBenzMaxim | 74:23be10c32fa3 | 124 | |
IanBenzMaxim | 74:23be10c32fa3 | 125 | /// Copies segment data to the page. |
IanBenzMaxim | 74:23be10c32fa3 | 126 | /// @param segmentNum Segment number within the page to copy to. |
IanBenzMaxim | 74:23be10c32fa3 | 127 | /// @param[in] segment Segment to copy from. |
IanBenzMaxim | 74:23be10c32fa3 | 128 | void fromSegment(unsigned int segmentNum, const Segment & segment); |
IanBenzMaxim | 74:23be10c32fa3 | 129 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 130 | |
IanBenzMaxim | 74:23be10c32fa3 | 131 | /// Holds the contents of the device scratchpad. |
IanBenzMaxim | 74:23be10c32fa3 | 132 | typedef array<uint8_t, 32> Scratchpad; |
IanBenzMaxim | 74:23be10c32fa3 | 133 | |
IanBenzMaxim | 74:23be10c32fa3 | 134 | /// Container for a SHA-256 MAC. |
IanBenzMaxim | 74:23be10c32fa3 | 135 | typedef array<uint8_t, 32> Mac; |
IanBenzMaxim | 74:23be10c32fa3 | 136 | |
IanBenzMaxim | 74:23be10c32fa3 | 137 | /// Container for a manufacturer ID. |
IanBenzMaxim | 74:23be10c32fa3 | 138 | typedef array<uint8_t, 2> ManId; |
IanBenzMaxim | 74:23be10c32fa3 | 139 | |
IanBenzMaxim | 74:23be10c32fa3 | 140 | /// Container for the device personality. |
IanBenzMaxim | 74:23be10c32fa3 | 141 | union Personality |
IanBenzMaxim | 74:23be10c32fa3 | 142 | { |
IanBenzMaxim | 74:23be10c32fa3 | 143 | array<uint8_t, 4>::Buffer bytes; |
IanBenzMaxim | 74:23be10c32fa3 | 144 | struct |
IanBenzMaxim | 74:23be10c32fa3 | 145 | { |
IanBenzMaxim | 74:23be10c32fa3 | 146 | uint8_t PB1; |
IanBenzMaxim | 74:23be10c32fa3 | 147 | uint8_t PB2; |
IanBenzMaxim | 74:23be10c32fa3 | 148 | ManId::Buffer manIdBytes; |
IanBenzMaxim | 74:23be10c32fa3 | 149 | |
IanBenzMaxim | 74:23be10c32fa3 | 150 | bool secretLocked() const { return (PB2 & 0x01); } |
IanBenzMaxim | 74:23be10c32fa3 | 151 | } fields; |
IanBenzMaxim | 74:23be10c32fa3 | 152 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 153 | |
IanBenzMaxim | 74:23be10c32fa3 | 154 | /// Represents the status of a memory protection block. |
IanBenzMaxim | 74:23be10c32fa3 | 155 | class BlockProtection |
IanBenzMaxim | 74:23be10c32fa3 | 156 | { |
IanBenzMaxim | 74:23be10c32fa3 | 157 | private: |
IanBenzMaxim | 74:23be10c32fa3 | 158 | static const uint8_t readProtectionMask = 0x80, writeProtectionMask = 0x40, eepromEmulationMask = 0x20, authProtectionMask = 0x10, blockNumMask = 0x0F; |
IanBenzMaxim | 74:23be10c32fa3 | 159 | uint8_t m_status; |
IanBenzMaxim | 74:23be10c32fa3 | 160 | |
IanBenzMaxim | 74:23be10c32fa3 | 161 | public: |
IanBenzMaxim | 74:23be10c32fa3 | 162 | BlockProtection() : m_status(0x00) { } |
IanBenzMaxim | 74:23be10c32fa3 | 163 | BlockProtection(bool readProtection, bool writeProtection, bool eepromEmulation, bool authProtection, uint8_t blockNum); |
IanBenzMaxim | 74:23be10c32fa3 | 164 | |
IanBenzMaxim | 74:23be10c32fa3 | 165 | /// Get the byte representation used by the device. |
IanBenzMaxim | 74:23be10c32fa3 | 166 | uint8_t statusByte() const { return m_status; } |
IanBenzMaxim | 74:23be10c32fa3 | 167 | /// Set the byte representation used by the device. |
IanBenzMaxim | 74:23be10c32fa3 | 168 | void setStatusByte(uint8_t status) { m_status = status; } |
IanBenzMaxim | 74:23be10c32fa3 | 169 | |
IanBenzMaxim | 74:23be10c32fa3 | 170 | /// Get the Block Number which is indexed from zero. |
IanBenzMaxim | 74:23be10c32fa3 | 171 | uint8_t blockNum() const { return (m_status & blockNumMask); } |
IanBenzMaxim | 74:23be10c32fa3 | 172 | /// Set the Block Number which is indexed from zero. |
IanBenzMaxim | 74:23be10c32fa3 | 173 | void setBlockNum(uint8_t blockNum); |
IanBenzMaxim | 74:23be10c32fa3 | 174 | |
IanBenzMaxim | 74:23be10c32fa3 | 175 | /// Get the Read Protection status. |
IanBenzMaxim | 74:23be10c32fa3 | 176 | /// @returns True if Read Protection is enabled. |
IanBenzMaxim | 74:23be10c32fa3 | 177 | bool readProtection() const { return ((m_status & readProtectionMask) == readProtectionMask); } |
IanBenzMaxim | 74:23be10c32fa3 | 178 | /// Set the Read Protection status. |
IanBenzMaxim | 74:23be10c32fa3 | 179 | void setReadProtection(bool readProtection); |
IanBenzMaxim | 74:23be10c32fa3 | 180 | |
IanBenzMaxim | 74:23be10c32fa3 | 181 | /// Get the Write Protection status. |
IanBenzMaxim | 74:23be10c32fa3 | 182 | /// @returns True if Write Protection is enabled. |
IanBenzMaxim | 74:23be10c32fa3 | 183 | bool writeProtection() const { return ((m_status & writeProtectionMask) == writeProtectionMask); } |
IanBenzMaxim | 74:23be10c32fa3 | 184 | /// Set the Write Protection status. |
IanBenzMaxim | 74:23be10c32fa3 | 185 | void setWriteProtection(bool writeProtection); |
IanBenzMaxim | 74:23be10c32fa3 | 186 | |
IanBenzMaxim | 74:23be10c32fa3 | 187 | /// Get the EEPROM Emulation Mode status. |
IanBenzMaxim | 74:23be10c32fa3 | 188 | /// @returns True if EEPROM Emulation Mode is enabled. |
IanBenzMaxim | 74:23be10c32fa3 | 189 | bool eepromEmulation() const { return ((m_status & eepromEmulationMask) == eepromEmulationMask); } |
IanBenzMaxim | 74:23be10c32fa3 | 190 | /// Set the EEPROM Emulation Mode status. |
IanBenzMaxim | 74:23be10c32fa3 | 191 | void setEepromEmulation(bool eepromEmulation); |
IanBenzMaxim | 74:23be10c32fa3 | 192 | |
IanBenzMaxim | 74:23be10c32fa3 | 193 | /// Get the Authentication Protection status. |
IanBenzMaxim | 74:23be10c32fa3 | 194 | /// @returns True if Authentication Protection is enabled. |
IanBenzMaxim | 74:23be10c32fa3 | 195 | bool authProtection() const { return ((m_status & authProtectionMask) == authProtectionMask); } |
IanBenzMaxim | 74:23be10c32fa3 | 196 | /// Set the Authentication Protection status. |
IanBenzMaxim | 74:23be10c32fa3 | 197 | void setAuthProtection(bool authProtection); |
IanBenzMaxim | 74:23be10c32fa3 | 198 | |
IanBenzMaxim | 74:23be10c32fa3 | 199 | /// Check if no protection options are enabled. |
IanBenzMaxim | 74:23be10c32fa3 | 200 | /// @returns True if no protection options are enabled. |
IanBenzMaxim | 74:23be10c32fa3 | 201 | bool noProtection() const; |
IanBenzMaxim | 74:23be10c32fa3 | 202 | |
IanBenzMaxim | 74:23be10c32fa3 | 203 | bool operator==(const BlockProtection & rhs) const { return (this->m_status == rhs.m_status); } |
IanBenzMaxim | 74:23be10c32fa3 | 204 | bool operator!=(const BlockProtection & rhs) const { return !operator==(rhs); } |
IanBenzMaxim | 74:23be10c32fa3 | 205 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 206 | |
IanBenzMaxim | 74:23be10c32fa3 | 207 | /// Compute the MAC for an Authenticated Write to a memory segment. |
IanBenzMaxim | 74:23be10c32fa3 | 208 | /// @param MacCoproc Coprocessor with Slave Secret to use for the computation. |
IanBenzMaxim | 74:23be10c32fa3 | 209 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 74:23be10c32fa3 | 210 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 74:23be10c32fa3 | 211 | /// @param[in] newData New data to write to the segment. |
IanBenzMaxim | 74:23be10c32fa3 | 212 | /// @param[in] oldData Existing data contained in the segment. |
IanBenzMaxim | 74:23be10c32fa3 | 213 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 74:23be10c32fa3 | 214 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 74:23be10c32fa3 | 215 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 74:23be10c32fa3 | 216 | /// @returns The result code indicated by the coprocessor. |
IanBenzMaxim | 74:23be10c32fa3 | 217 | static ISha256MacCoproc::CmdResult computeSegmentWriteMac(const ISha256MacCoproc & MacCoproc, |
IanBenzMaxim | 74:23be10c32fa3 | 218 | unsigned int pageNum, |
IanBenzMaxim | 74:23be10c32fa3 | 219 | unsigned int segmentNum, |
IanBenzMaxim | 74:23be10c32fa3 | 220 | const Segment & newData, |
IanBenzMaxim | 74:23be10c32fa3 | 221 | const Segment & oldData, |
IanBenzMaxim | 74:23be10c32fa3 | 222 | const RomId & romId, |
IanBenzMaxim | 74:23be10c32fa3 | 223 | const ManId & manId, |
IanBenzMaxim | 74:23be10c32fa3 | 224 | Mac & mac); |
IanBenzMaxim | 74:23be10c32fa3 | 225 | |
IanBenzMaxim | 74:23be10c32fa3 | 226 | /// Compute the MAC for an Authenticated Write to a memory protection block. |
IanBenzMaxim | 74:23be10c32fa3 | 227 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 74:23be10c32fa3 | 228 | /// @param[in] newProtection New protection status to write. |
IanBenzMaxim | 74:23be10c32fa3 | 229 | /// @param[in] oldProtection Existing protection status in device. |
IanBenzMaxim | 74:23be10c32fa3 | 230 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 74:23be10c32fa3 | 231 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 74:23be10c32fa3 | 232 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 74:23be10c32fa3 | 233 | /// @returns The result code indicated by the coprocessor. |
IanBenzMaxim | 74:23be10c32fa3 | 234 | static ISha256MacCoproc::CmdResult computeProtectionWriteMac(const ISha256MacCoproc & MacCoproc, |
IanBenzMaxim | 74:23be10c32fa3 | 235 | const BlockProtection & newProtection, |
IanBenzMaxim | 74:23be10c32fa3 | 236 | const BlockProtection & oldProtection, |
IanBenzMaxim | 74:23be10c32fa3 | 237 | const RomId & romId, |
IanBenzMaxim | 74:23be10c32fa3 | 238 | const ManId & manId, |
IanBenzMaxim | 74:23be10c32fa3 | 239 | Mac & mac); |
IanBenzMaxim | 34:11fffbe98ef9 | 240 | |
IanBenzMaxim | 74:23be10c32fa3 | 241 | /// Compute the next secret from the existing secret. |
IanBenzMaxim | 74:23be10c32fa3 | 242 | /// @param MacCoproc Coprocessor with Master Secret to use for the operation. |
IanBenzMaxim | 74:23be10c32fa3 | 243 | /// Slave Secret will be updated with the computation result. |
IanBenzMaxim | 74:23be10c32fa3 | 244 | /// @param[in] bindingPage Binding data from a device memory page. |
IanBenzMaxim | 74:23be10c32fa3 | 245 | /// @param bindingPageNum Number of the page where the binding data is from. |
IanBenzMaxim | 74:23be10c32fa3 | 246 | /// @param[in] partialSecret Partial secret data from the device scratchpad. |
IanBenzMaxim | 74:23be10c32fa3 | 247 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 74:23be10c32fa3 | 248 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 74:23be10c32fa3 | 249 | /// @returns The result code indicated by the coprocessor. |
IanBenzMaxim | 74:23be10c32fa3 | 250 | static ISha256MacCoproc::CmdResult computeNextSecret(ISha256MacCoproc & MacCoproc, |
IanBenzMaxim | 74:23be10c32fa3 | 251 | const Page & bindingPage, |
IanBenzMaxim | 74:23be10c32fa3 | 252 | unsigned int bindingPageNum, |
IanBenzMaxim | 74:23be10c32fa3 | 253 | const Scratchpad & partialSecret, |
IanBenzMaxim | 74:23be10c32fa3 | 254 | const RomId & romId, |
IanBenzMaxim | 74:23be10c32fa3 | 255 | const ManId & manId); |
IanBenzMaxim | 74:23be10c32fa3 | 256 | |
IanBenzMaxim | 74:23be10c32fa3 | 257 | /// Compute a Page MAC for authentication. |
IanBenzMaxim | 74:23be10c32fa3 | 258 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 74:23be10c32fa3 | 259 | /// @param[in] pageData Data from a device memory page. |
IanBenzMaxim | 74:23be10c32fa3 | 260 | /// @param pageNum Number of the page to use data from. |
IanBenzMaxim | 74:23be10c32fa3 | 261 | /// @param[in] challenge Random challenge to prevent replay attacks. |
IanBenzMaxim | 74:23be10c32fa3 | 262 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 74:23be10c32fa3 | 263 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 74:23be10c32fa3 | 264 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 74:23be10c32fa3 | 265 | static ISha256MacCoproc::CmdResult computeAuthMac(const ISha256MacCoproc & MacCoproc, |
IanBenzMaxim | 74:23be10c32fa3 | 266 | const Page & pageData, |
IanBenzMaxim | 74:23be10c32fa3 | 267 | unsigned int pageNum, |
IanBenzMaxim | 74:23be10c32fa3 | 268 | const Scratchpad & challenge, |
IanBenzMaxim | 74:23be10c32fa3 | 269 | const RomId & romId, |
IanBenzMaxim | 74:23be10c32fa3 | 270 | const ManId & manId, |
IanBenzMaxim | 74:23be10c32fa3 | 271 | Mac & mac); |
IanBenzMaxim | 74:23be10c32fa3 | 272 | |
IanBenzMaxim | 74:23be10c32fa3 | 273 | /// Compute a Page MAC for authentication using anonymous mode. |
IanBenzMaxim | 74:23be10c32fa3 | 274 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 74:23be10c32fa3 | 275 | /// @param[in] pageData Data from a device memory page. |
IanBenzMaxim | 74:23be10c32fa3 | 276 | /// @param pageNum Number of the page to use data from. |
IanBenzMaxim | 74:23be10c32fa3 | 277 | /// @param[in] challenge Random challenge to prevent replay attacks. |
IanBenzMaxim | 74:23be10c32fa3 | 278 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 74:23be10c32fa3 | 279 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 74:23be10c32fa3 | 280 | static ISha256MacCoproc::CmdResult computeAuthMacAnon(const ISha256MacCoproc & MacCoproc, |
IanBenzMaxim | 74:23be10c32fa3 | 281 | const Page & pageData, |
IanBenzMaxim | 74:23be10c32fa3 | 282 | unsigned int pageNum, |
IanBenzMaxim | 74:23be10c32fa3 | 283 | const Scratchpad & challenge, |
IanBenzMaxim | 74:23be10c32fa3 | 284 | const ManId & manId, |
IanBenzMaxim | 74:23be10c32fa3 | 285 | Mac & mac); |
IanBenzMaxim | 74:23be10c32fa3 | 286 | |
IanBenzMaxim | 74:23be10c32fa3 | 287 | /// Number of segments per page. |
IanBenzMaxim | 74:23be10c32fa3 | 288 | static const unsigned int segmentsPerPage = (Page::length / Segment::length); |
IanBenzMaxim | 74:23be10c32fa3 | 289 | |
IanBenzMaxim | 74:23be10c32fa3 | 290 | /// Manufacturer ID |
IanBenzMaxim | 74:23be10c32fa3 | 291 | ManId manId; |
IanBenzMaxim | 74:23be10c32fa3 | 292 | |
IanBenzMaxim | 74:23be10c32fa3 | 293 | /// Enable low voltage timing |
IanBenzMaxim | 74:23be10c32fa3 | 294 | bool lowVoltage; |
IanBenzMaxim | 74:23be10c32fa3 | 295 | |
IanBenzMaxim | 74:23be10c32fa3 | 296 | /// @param OW_master 1-Wire Master to use for communication with DS28E15/22/25. |
IanBenzMaxim | 74:23be10c32fa3 | 297 | /// @param lowVoltage Enable low voltage timing. |
IanBenzMaxim | 74:23be10c32fa3 | 298 | DS28E15_22_25(Masters::OneWireMaster & OW_master, bool lowVoltage = false); |
IanBenzMaxim | 74:23be10c32fa3 | 299 | |
IanBenzMaxim | 74:23be10c32fa3 | 300 | // Const member functions should not affect the state of the memory, block protection, or secret on the DS28Exx. |
IanBenzMaxim | 74:23be10c32fa3 | 301 | // Scratchpad on the DS28Exx is considered mutable. |
IanBenzMaxim | 74:23be10c32fa3 | 302 | |
IanBenzMaxim | 74:23be10c32fa3 | 303 | /// Get the number of memory pages from the ROM ID of this device. |
IanBenzMaxim | 74:23be10c32fa3 | 304 | MemoryPages memoryPages(); |
IanBenzMaxim | 74:23be10c32fa3 | 305 | |
IanBenzMaxim | 74:23be10c32fa3 | 306 | /// Get the number of protection blocks from the ROM ID of this device. |
IanBenzMaxim | 74:23be10c32fa3 | 307 | ProtectionBlocks protectionBlocks(); |
IanBenzMaxim | 74:23be10c32fa3 | 308 | |
IanBenzMaxim | 74:23be10c32fa3 | 309 | /// Perform Load and Lock Secret command on the device. |
IanBenzMaxim | 74:23be10c32fa3 | 310 | /// @note The secret should already be stored in the scratchpad on the device. |
IanBenzMaxim | 74:23be10c32fa3 | 311 | /// @param lock Prevent further changes to the secret on the device after loading. |
IanBenzMaxim | 74:23be10c32fa3 | 312 | CmdResult loadSecret(bool lock); |
IanBenzMaxim | 74:23be10c32fa3 | 313 | |
IanBenzMaxim | 74:23be10c32fa3 | 314 | /// Perform Write Scratchpad operation on the device. |
IanBenzMaxim | 74:23be10c32fa3 | 315 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 316 | /// @param[in] data Data to write to the scratchpad. |
IanBenzMaxim | 74:23be10c32fa3 | 317 | CmdResult writeScratchpad(const Scratchpad & data) const; |
IanBenzMaxim | 74:23be10c32fa3 | 318 | |
IanBenzMaxim | 74:23be10c32fa3 | 319 | /// Perform a Read Scratchpad operation on the device. |
IanBenzMaxim | 74:23be10c32fa3 | 320 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 321 | /// @param[out] data Buffer to read data from the scratchpad into. |
IanBenzMaxim | 74:23be10c32fa3 | 322 | CmdResult readScratchpad(Scratchpad & data) const; |
IanBenzMaxim | 74:23be10c32fa3 | 323 | |
IanBenzMaxim | 74:23be10c32fa3 | 324 | /// Read memory segment using the Read Memory command on the device. |
IanBenzMaxim | 74:23be10c32fa3 | 325 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 326 | /// @param pageNum Page number for read operation. |
IanBenzMaxim | 74:23be10c32fa3 | 327 | /// @param segmentNum Segment number within page for read operation. |
IanBenzMaxim | 74:23be10c32fa3 | 328 | /// @param[out] data Buffer to read data from the segment into. |
IanBenzMaxim | 74:23be10c32fa3 | 329 | /// @param continuing True if continuing a previous Read Memory command. |
IanBenzMaxim | 74:23be10c32fa3 | 330 | /// False to begin a new command. |
IanBenzMaxim | 74:23be10c32fa3 | 331 | CmdResult readSegment(unsigned int pageNum, unsigned int segmentNum, Segment & data, bool continuing = false) const; |
IanBenzMaxim | 74:23be10c32fa3 | 332 | |
IanBenzMaxim | 74:23be10c32fa3 | 333 | /// Write memory segment using the Write Memory command. |
IanBenzMaxim | 74:23be10c32fa3 | 334 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 335 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 74:23be10c32fa3 | 336 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 74:23be10c32fa3 | 337 | /// @param[in] data Data to write to the memory segment. |
IanBenzMaxim | 74:23be10c32fa3 | 338 | /// @param continuing True to continue writing with the next sequential segment. |
IanBenzMaxim | 74:23be10c32fa3 | 339 | /// False to begin a new command. |
IanBenzMaxim | 74:23be10c32fa3 | 340 | CmdResult writeSegment(unsigned int pageNum, unsigned int segmentNum, const Segment & data, bool continuing = false); |
IanBenzMaxim | 73:2cecc1372acc | 341 | |
IanBenzMaxim | 74:23be10c32fa3 | 342 | /// Read memory page using the Read Memory command on the device. |
IanBenzMaxim | 74:23be10c32fa3 | 343 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 344 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 74:23be10c32fa3 | 345 | /// @param[out] rdbuf Buffer to read data from the page into. |
IanBenzMaxim | 74:23be10c32fa3 | 346 | /// @param continuing True if continuing a previous Read Memory command. |
IanBenzMaxim | 74:23be10c32fa3 | 347 | /// False to begin a new command. |
IanBenzMaxim | 74:23be10c32fa3 | 348 | CmdResult readPage(unsigned int pageNum, Page & rdbuf, bool continuing = false) const; |
IanBenzMaxim | 74:23be10c32fa3 | 349 | |
IanBenzMaxim | 74:23be10c32fa3 | 350 | /// Perform a Compute and Lock Secret command on the device. |
IanBenzMaxim | 74:23be10c32fa3 | 351 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 352 | /// @param pageNum Page number to use as the binding data. |
IanBenzMaxim | 74:23be10c32fa3 | 353 | /// @param lock Prevent further changes to the secret on the device after computing. |
IanBenzMaxim | 74:23be10c32fa3 | 354 | CmdResult computeSecret(unsigned int pageNum, bool lock); |
IanBenzMaxim | 74:23be10c32fa3 | 355 | |
IanBenzMaxim | 74:23be10c32fa3 | 356 | /// Perform a Compute Page MAC command on the device. |
IanBenzMaxim | 74:23be10c32fa3 | 357 | /// Read back the MAC and verify the CRC16. |
IanBenzMaxim | 74:23be10c32fa3 | 358 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 359 | /// @param pageNum Page number to use for the computation. |
IanBenzMaxim | 74:23be10c32fa3 | 360 | /// @param anon True to compute in anonymous mode where ROM ID is not used. |
IanBenzMaxim | 74:23be10c32fa3 | 361 | /// @param[out] mac The device computed MAC. |
IanBenzMaxim | 74:23be10c32fa3 | 362 | CmdResult computeReadPageMac(unsigned int pageNum, bool anon, Mac & mac) const; |
IanBenzMaxim | 74:23be10c32fa3 | 363 | |
IanBenzMaxim | 74:23be10c32fa3 | 364 | /// Read the status of a memory protection block using the Read Status command. |
IanBenzMaxim | 74:23be10c32fa3 | 365 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 366 | /// @param blockNum Block number to to read status of. |
IanBenzMaxim | 74:23be10c32fa3 | 367 | /// @param[out] protection Receives protection status read from device. |
IanBenzMaxim | 74:23be10c32fa3 | 368 | CmdResult readBlockProtection(unsigned int blockNum, BlockProtection & protection); |
IanBenzMaxim | 74:23be10c32fa3 | 369 | |
IanBenzMaxim | 74:23be10c32fa3 | 370 | /// @{ |
IanBenzMaxim | 74:23be10c32fa3 | 371 | /// Read the status of all memory protection blocks using the Read Status command. |
IanBenzMaxim | 74:23be10c32fa3 | 372 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 373 | /// @param[out] protection Receives protection statuses read from device. |
IanBenzMaxim | 74:23be10c32fa3 | 374 | CmdResult readAllBlockProtection(BlockProtection(&protection)[DS28E15_BLOCKS]) const; |
IanBenzMaxim | 74:23be10c32fa3 | 375 | CmdResult readAllBlockProtection(BlockProtection(&protection)[DS28E25_BLOCKS]) const; |
IanBenzMaxim | 74:23be10c32fa3 | 376 | /// @} |
IanBenzMaxim | 74:23be10c32fa3 | 377 | |
IanBenzMaxim | 74:23be10c32fa3 | 378 | /// Read the personality bytes using the Read Status command. |
IanBenzMaxim | 74:23be10c32fa3 | 379 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 380 | /// @param[out] personality Receives personality read from device. |
IanBenzMaxim | 74:23be10c32fa3 | 381 | CmdResult readPersonality(Personality & personality) const; |
IanBenzMaxim | 74:23be10c32fa3 | 382 | |
IanBenzMaxim | 74:23be10c32fa3 | 383 | /// Update the status of a memory protection block using the Write Page Protection command. |
IanBenzMaxim | 74:23be10c32fa3 | 384 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 385 | /// @param[in] Desired protection status for the block. |
IanBenzMaxim | 74:23be10c32fa3 | 386 | /// It is not possible to disable existing protections. |
IanBenzMaxim | 74:23be10c32fa3 | 387 | /// @param continuing True to continue a previous Write Page Protection command. |
IanBenzMaxim | 74:23be10c32fa3 | 388 | /// False to begin a new command. |
IanBenzMaxim | 74:23be10c32fa3 | 389 | CmdResult writeBlockProtection(const BlockProtection & protection); |
IanBenzMaxim | 74:23be10c32fa3 | 390 | |
IanBenzMaxim | 74:23be10c32fa3 | 391 | /// Update the status of a memory protection block using the Authenticated Write Page Protection command. |
IanBenzMaxim | 74:23be10c32fa3 | 392 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 393 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 74:23be10c32fa3 | 394 | /// @param[in] newProtection New protection status to write. |
IanBenzMaxim | 74:23be10c32fa3 | 395 | /// @param[in] oldProtection Existing protection status in device. |
IanBenzMaxim | 74:23be10c32fa3 | 396 | /// @param continuing True to continue a previous Authenticated Write Page Protection command. |
IanBenzMaxim | 74:23be10c32fa3 | 397 | /// False to begin a new command. |
IanBenzMaxim | 74:23be10c32fa3 | 398 | CmdResult writeAuthBlockProtection(const ISha256MacCoproc & MacCoproc, |
IanBenzMaxim | 74:23be10c32fa3 | 399 | const BlockProtection & newProtection, |
IanBenzMaxim | 74:23be10c32fa3 | 400 | const BlockProtection & oldProtection); |
IanBenzMaxim | 74:23be10c32fa3 | 401 | |
IanBenzMaxim | 74:23be10c32fa3 | 402 | /// Write memory segment with authentication using the Authenticated Write Memory command. |
IanBenzMaxim | 74:23be10c32fa3 | 403 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 404 | /// @param MacCoproc Coprocessor to use for Write MAC computation. |
IanBenzMaxim | 74:23be10c32fa3 | 405 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 74:23be10c32fa3 | 406 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 74:23be10c32fa3 | 407 | /// @param[in] newData New data to write to the segment. |
IanBenzMaxim | 74:23be10c32fa3 | 408 | /// @param[in] oldData Existing data contained in the segment. |
IanBenzMaxim | 74:23be10c32fa3 | 409 | /// @param continuing True to continue writing with the next sequential segment. |
IanBenzMaxim | 74:23be10c32fa3 | 410 | /// False to begin a new command. |
IanBenzMaxim | 74:23be10c32fa3 | 411 | CmdResult writeAuthSegment(const ISha256MacCoproc & MacCoproc, |
IanBenzMaxim | 74:23be10c32fa3 | 412 | unsigned int pageNum, |
IanBenzMaxim | 74:23be10c32fa3 | 413 | unsigned int segmentNum, |
IanBenzMaxim | 74:23be10c32fa3 | 414 | const Segment & newData, |
IanBenzMaxim | 74:23be10c32fa3 | 415 | const Segment & oldData, |
IanBenzMaxim | 74:23be10c32fa3 | 416 | bool continuing = false); |
IanBenzMaxim | 74:23be10c32fa3 | 417 | |
IanBenzMaxim | 74:23be10c32fa3 | 418 | /// Write memory segment with authentication using the Authenticated Write Memory command. |
IanBenzMaxim | 74:23be10c32fa3 | 419 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 420 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 74:23be10c32fa3 | 421 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 74:23be10c32fa3 | 422 | /// @param[in] newData New data to write to the segment. |
IanBenzMaxim | 74:23be10c32fa3 | 423 | /// @param[in] mac Write MAC computed for this operation. |
IanBenzMaxim | 74:23be10c32fa3 | 424 | /// @param continuing True to continue writing with the next sequential segment. |
IanBenzMaxim | 74:23be10c32fa3 | 425 | /// False to begin a new command. |
IanBenzMaxim | 74:23be10c32fa3 | 426 | CmdResult writeAuthSegmentMac(unsigned int pageNum, |
IanBenzMaxim | 74:23be10c32fa3 | 427 | unsigned int segmentNum, |
IanBenzMaxim | 74:23be10c32fa3 | 428 | const Segment & newData, |
IanBenzMaxim | 74:23be10c32fa3 | 429 | const Mac & mac, |
IanBenzMaxim | 74:23be10c32fa3 | 430 | bool continuing = false); |
IanBenzMaxim | 74:23be10c32fa3 | 431 | |
IanBenzMaxim | 74:23be10c32fa3 | 432 | private: |
IanBenzMaxim | 74:23be10c32fa3 | 433 | static const unsigned int shaComputationDelayMs = 3; |
IanBenzMaxim | 74:23be10c32fa3 | 434 | static const unsigned int eepromWriteDelayMs = 10; |
IanBenzMaxim | 74:23be10c32fa3 | 435 | unsigned int secretEepromWriteDelayMs() const { return (lowVoltage ? 200 : 100); } |
IanBenzMaxim | 74:23be10c32fa3 | 436 | |
IanBenzMaxim | 74:23be10c32fa3 | 437 | Masters::OneWireMaster & m_OW_master; |
IanBenzMaxim | 74:23be10c32fa3 | 438 | |
IanBenzMaxim | 74:23be10c32fa3 | 439 | /// Read status bytes which are either personality or block protection. |
IanBenzMaxim | 74:23be10c32fa3 | 440 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 74:23be10c32fa3 | 441 | /// @param personality True to read personality or false to read block protection. |
IanBenzMaxim | 74:23be10c32fa3 | 442 | /// @param allpages True to read all pages or false to read one page. |
IanBenzMaxim | 74:23be10c32fa3 | 443 | /// @param pageNum Page number if reading block protection. |
IanBenzMaxim | 74:23be10c32fa3 | 444 | /// @param rdbuf Buffer to receive data read from device. |
IanBenzMaxim | 74:23be10c32fa3 | 445 | CmdResult readStatus(bool personality, bool allpages, unsigned int blockNum, uint8_t * rdbuf) const; |
IanBenzMaxim | 74:23be10c32fa3 | 446 | |
IanBenzMaxim | 74:23be10c32fa3 | 447 | /// Extends readStatus() to use the BlockProtection wrapper. |
IanBenzMaxim | 74:23be10c32fa3 | 448 | /// @see readAllBlockProtection(BlockProtection (&)[DS28E15_BLOCKS]) |
IanBenzMaxim | 74:23be10c32fa3 | 449 | template <ProtectionBlocks blocks> CmdResult readAllBlockProtection(BlockProtection(&protection)[blocks]) const; |
IanBenzMaxim | 74:23be10c32fa3 | 450 | }; |
IanBenzMaxim | 74:23be10c32fa3 | 451 | } |
IanBenzMaxim | 73:2cecc1372acc | 452 | } |
IanBenzMaxim | 25:bdb1c5a53b58 | 453 | |
IanBenzMaxim | 49:36954b62f503 | 454 | #endif |