1-Wire® library for mbed. Complete 1-Wire library that supports our silicon masters along with a bit-bang master on the MAX32600MBED platform with one common interface for mbed. Slave support has also been included and more slaves will be added as time permits.
Dependents: MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more
Superseded by MaximInterface.
OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.hpp@62:43039aeca2ab, 2016-04-19 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Tue Apr 19 10:05:06 2016 -0500
- Revision:
- 62:43039aeca2ab
- Parent:
- 60:e8244cbc7946
Added convenience functions for converting between segments and pages on DS28E15/22/25.
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 | 25:bdb1c5a53b58 | 26 | #ifndef DS28E15_22_25_H |
IanBenzMaxim | 25:bdb1c5a53b58 | 27 | #define DS28E15_22_25_H |
IanBenzMaxim | 25:bdb1c5a53b58 | 28 | |
IanBenzMaxim | 33:a4c015046956 | 29 | #include "array.hpp" |
IanBenzMaxim | 25:bdb1c5a53b58 | 30 | #include "OneWire_Masters/ISha256MacCoprocessor.hpp" |
IanBenzMaxim | 25:bdb1c5a53b58 | 31 | #include "OneWireSlave.hpp" |
IanBenzMaxim | 25:bdb1c5a53b58 | 32 | |
IanBenzMaxim | 27:d5aaefa252f1 | 33 | class OneWireMaster; |
IanBenzMaxim | 27:d5aaefa252f1 | 34 | |
IanBenzMaxim | 49:36954b62f503 | 35 | /// Interface to the DS28E15/22/25 series of authenticators |
IanBenzMaxim | 49:36954b62f503 | 36 | /// including low power variants. |
IanBenzMaxim | 25:bdb1c5a53b58 | 37 | class DS28E15_22_25 : public OneWireSlave |
IanBenzMaxim | 25:bdb1c5a53b58 | 38 | { |
IanBenzMaxim | 25:bdb1c5a53b58 | 39 | public: |
IanBenzMaxim | 49:36954b62f503 | 40 | /// Family code for each device. |
IanBenzMaxim | 34:11fffbe98ef9 | 41 | enum FamilyCode |
IanBenzMaxim | 25:bdb1c5a53b58 | 42 | { |
IanBenzMaxim | 25:bdb1c5a53b58 | 43 | DS28E25_FAMILY = 0x47, |
IanBenzMaxim | 25:bdb1c5a53b58 | 44 | DS28E22_FAMILY = 0x48, |
IanBenzMaxim | 25:bdb1c5a53b58 | 45 | DS28E15_FAMILY = 0x17, |
IanBenzMaxim | 25:bdb1c5a53b58 | 46 | UNKNOWN_FAMILY = 0 |
IanBenzMaxim | 25:bdb1c5a53b58 | 47 | }; |
IanBenzMaxim | 25:bdb1c5a53b58 | 48 | |
IanBenzMaxim | 49:36954b62f503 | 49 | /// Number for memory pages for each device. |
IanBenzMaxim | 34:11fffbe98ef9 | 50 | enum MemoryPages |
IanBenzMaxim | 25:bdb1c5a53b58 | 51 | { |
IanBenzMaxim | 25:bdb1c5a53b58 | 52 | DS28E25_PAGES = 16, |
IanBenzMaxim | 25:bdb1c5a53b58 | 53 | DS28E22_PAGES = 8, |
IanBenzMaxim | 25:bdb1c5a53b58 | 54 | DS28E15_PAGES = 2, |
IanBenzMaxim | 25:bdb1c5a53b58 | 55 | UNKNOWN_PAGES = 0 |
IanBenzMaxim | 25:bdb1c5a53b58 | 56 | }; |
IanBenzMaxim | 25:bdb1c5a53b58 | 57 | |
IanBenzMaxim | 49:36954b62f503 | 58 | /// Number of protection blocks for each device. |
IanBenzMaxim | 34:11fffbe98ef9 | 59 | enum ProtectionBlocks |
IanBenzMaxim | 25:bdb1c5a53b58 | 60 | { |
IanBenzMaxim | 25:bdb1c5a53b58 | 61 | DS28E25_BLOCKS = 8, |
IanBenzMaxim | 25:bdb1c5a53b58 | 62 | DS28E22_BLOCKS = 4, |
IanBenzMaxim | 25:bdb1c5a53b58 | 63 | DS28E15_BLOCKS = 4, |
IanBenzMaxim | 25:bdb1c5a53b58 | 64 | UNKNOWN_BLOCKS = 0 |
IanBenzMaxim | 25:bdb1c5a53b58 | 65 | }; |
IanBenzMaxim | 25:bdb1c5a53b58 | 66 | |
IanBenzMaxim | 62:43039aeca2ab | 67 | /// Holds the contents of a device memory segment. |
IanBenzMaxim | 62:43039aeca2ab | 68 | typedef array<std::uint8_t, 4> Segment; |
IanBenzMaxim | 62:43039aeca2ab | 69 | |
IanBenzMaxim | 49:36954b62f503 | 70 | /// Holds the contents of a device memory page. |
IanBenzMaxim | 62:43039aeca2ab | 71 | class Page |
IanBenzMaxim | 62:43039aeca2ab | 72 | { |
IanBenzMaxim | 62:43039aeca2ab | 73 | public: |
IanBenzMaxim | 62:43039aeca2ab | 74 | /// Length of the buffer in bytes. |
IanBenzMaxim | 62:43039aeca2ab | 75 | static const std::size_t length = 32; |
IanBenzMaxim | 62:43039aeca2ab | 76 | |
IanBenzMaxim | 62:43039aeca2ab | 77 | private: |
IanBenzMaxim | 62:43039aeca2ab | 78 | array<std::uint8_t, length> m_data; |
IanBenzMaxim | 62:43039aeca2ab | 79 | |
IanBenzMaxim | 62:43039aeca2ab | 80 | public: |
IanBenzMaxim | 62:43039aeca2ab | 81 | /// Built-in array representation. |
IanBenzMaxim | 62:43039aeca2ab | 82 | typedef array<std::uint8_t, length>::Buffer Buffer; |
IanBenzMaxim | 62:43039aeca2ab | 83 | |
IanBenzMaxim | 62:43039aeca2ab | 84 | Page() { } |
IanBenzMaxim | 62:43039aeca2ab | 85 | Page(const Page & page) : m_data(page.m_data) { } |
IanBenzMaxim | 62:43039aeca2ab | 86 | Page(const Buffer & dataBytes) : m_data(dataBytes) { } |
IanBenzMaxim | 62:43039aeca2ab | 87 | |
IanBenzMaxim | 62:43039aeca2ab | 88 | const Page & operator=(const Page & rhs) |
IanBenzMaxim | 62:43039aeca2ab | 89 | { |
IanBenzMaxim | 62:43039aeca2ab | 90 | this->m_data = rhs.m_data; |
IanBenzMaxim | 62:43039aeca2ab | 91 | return rhs; |
IanBenzMaxim | 62:43039aeca2ab | 92 | } |
IanBenzMaxim | 62:43039aeca2ab | 93 | |
IanBenzMaxim | 62:43039aeca2ab | 94 | bool operator==(const Page & rhs) const |
IanBenzMaxim | 62:43039aeca2ab | 95 | { |
IanBenzMaxim | 62:43039aeca2ab | 96 | return (this->m_data == rhs.m_data); |
IanBenzMaxim | 62:43039aeca2ab | 97 | } |
IanBenzMaxim | 62:43039aeca2ab | 98 | |
IanBenzMaxim | 62:43039aeca2ab | 99 | bool operator!=(const Page & rhs) const |
IanBenzMaxim | 62:43039aeca2ab | 100 | { |
IanBenzMaxim | 62:43039aeca2ab | 101 | return !operator==(rhs); |
IanBenzMaxim | 62:43039aeca2ab | 102 | } |
IanBenzMaxim | 62:43039aeca2ab | 103 | |
IanBenzMaxim | 62:43039aeca2ab | 104 | /// Conversion to array reference. |
IanBenzMaxim | 62:43039aeca2ab | 105 | operator Buffer &() |
IanBenzMaxim | 62:43039aeca2ab | 106 | { |
IanBenzMaxim | 62:43039aeca2ab | 107 | return m_data; |
IanBenzMaxim | 62:43039aeca2ab | 108 | } |
IanBenzMaxim | 62:43039aeca2ab | 109 | |
IanBenzMaxim | 62:43039aeca2ab | 110 | /// Conversion to const array reference. |
IanBenzMaxim | 62:43039aeca2ab | 111 | operator const Buffer &() const |
IanBenzMaxim | 62:43039aeca2ab | 112 | { |
IanBenzMaxim | 62:43039aeca2ab | 113 | return m_data; |
IanBenzMaxim | 62:43039aeca2ab | 114 | } |
IanBenzMaxim | 62:43039aeca2ab | 115 | |
IanBenzMaxim | 62:43039aeca2ab | 116 | /// Creates a segment representation from a subsection of the page data. |
IanBenzMaxim | 62:43039aeca2ab | 117 | /// @param segmentNum Segment number within page to copy from. |
IanBenzMaxim | 62:43039aeca2ab | 118 | /// @returns The copied segment data. |
IanBenzMaxim | 62:43039aeca2ab | 119 | Segment toSegment(unsigned int segmentNum) const; |
IanBenzMaxim | 62:43039aeca2ab | 120 | |
IanBenzMaxim | 62:43039aeca2ab | 121 | /// Copies segment data to the page. |
IanBenzMaxim | 62:43039aeca2ab | 122 | /// @param segmentNum Segment number within the page to copy to. |
IanBenzMaxim | 62:43039aeca2ab | 123 | /// @param[in] segment Segment to copy from. |
IanBenzMaxim | 62:43039aeca2ab | 124 | void fromSegment(unsigned int segmentNum, const Segment & segment); |
IanBenzMaxim | 62:43039aeca2ab | 125 | }; |
IanBenzMaxim | 49:36954b62f503 | 126 | |
IanBenzMaxim | 49:36954b62f503 | 127 | /// Holds the contents of the device scratchpad. |
IanBenzMaxim | 33:a4c015046956 | 128 | typedef array<std::uint8_t, 32> Scratchpad; |
IanBenzMaxim | 49:36954b62f503 | 129 | |
IanBenzMaxim | 49:36954b62f503 | 130 | /// Container for a SHA-256 MAC. |
IanBenzMaxim | 33:a4c015046956 | 131 | typedef array<std::uint8_t, 32> Mac; |
IanBenzMaxim | 49:36954b62f503 | 132 | |
IanBenzMaxim | 49:36954b62f503 | 133 | /// Container for a manufacturer ID. |
IanBenzMaxim | 33:a4c015046956 | 134 | typedef array<std::uint8_t, 2> ManId; |
IanBenzMaxim | 25:bdb1c5a53b58 | 135 | |
IanBenzMaxim | 50:e967f9befbd0 | 136 | /// Container for the device personality. |
IanBenzMaxim | 50:e967f9befbd0 | 137 | union Personality |
IanBenzMaxim | 50:e967f9befbd0 | 138 | { |
IanBenzMaxim | 50:e967f9befbd0 | 139 | array<std::uint8_t, 4>::Buffer bytes; |
IanBenzMaxim | 50:e967f9befbd0 | 140 | struct |
IanBenzMaxim | 50:e967f9befbd0 | 141 | { |
IanBenzMaxim | 50:e967f9befbd0 | 142 | std::uint8_t PB1; |
IanBenzMaxim | 50:e967f9befbd0 | 143 | std::uint8_t PB2; |
IanBenzMaxim | 50:e967f9befbd0 | 144 | ManId::Buffer manIdBytes; |
IanBenzMaxim | 62:43039aeca2ab | 145 | |
IanBenzMaxim | 62:43039aeca2ab | 146 | bool secretLocked() const { return (PB2 & 0x01); } |
IanBenzMaxim | 50:e967f9befbd0 | 147 | } fields; |
IanBenzMaxim | 50:e967f9befbd0 | 148 | }; |
IanBenzMaxim | 50:e967f9befbd0 | 149 | |
IanBenzMaxim | 49:36954b62f503 | 150 | /// Represents the status of a memory protection block. |
IanBenzMaxim | 34:11fffbe98ef9 | 151 | class BlockProtection |
IanBenzMaxim | 34:11fffbe98ef9 | 152 | { |
IanBenzMaxim | 34:11fffbe98ef9 | 153 | private: |
IanBenzMaxim | 60:e8244cbc7946 | 154 | static const std::uint8_t readProtectionMask = 0x80, writeProtectionMask = 0x40, eepromEmulationMask = 0x20, authProtectionMask = 0x10, blockNumMask = 0x0F; |
IanBenzMaxim | 34:11fffbe98ef9 | 155 | std::uint8_t m_status; |
IanBenzMaxim | 34:11fffbe98ef9 | 156 | |
IanBenzMaxim | 34:11fffbe98ef9 | 157 | public: |
IanBenzMaxim | 34:11fffbe98ef9 | 158 | BlockProtection() : m_status(0x00) { } |
IanBenzMaxim | 34:11fffbe98ef9 | 159 | BlockProtection(bool readProtection, bool writeProtection, bool eepromEmulation, bool authProtection, std::uint8_t blockNum); |
IanBenzMaxim | 34:11fffbe98ef9 | 160 | |
IanBenzMaxim | 49:36954b62f503 | 161 | /// Get the byte representation used by the device. |
IanBenzMaxim | 49:36954b62f503 | 162 | std::uint8_t statusByte() const { return m_status; } |
IanBenzMaxim | 49:36954b62f503 | 163 | /// Set the byte representation used by the device. |
IanBenzMaxim | 49:36954b62f503 | 164 | void setStatusByte(std::uint8_t status) { m_status = status; } |
IanBenzMaxim | 34:11fffbe98ef9 | 165 | |
IanBenzMaxim | 49:36954b62f503 | 166 | /// Get the Block Number which is indexed from zero. |
IanBenzMaxim | 34:11fffbe98ef9 | 167 | std::uint8_t blockNum() const { return (m_status & blockNumMask); } |
IanBenzMaxim | 49:36954b62f503 | 168 | /// Set the Block Number which is indexed from zero. |
IanBenzMaxim | 34:11fffbe98ef9 | 169 | void setBlockNum(std::uint8_t blockNum); |
IanBenzMaxim | 34:11fffbe98ef9 | 170 | |
IanBenzMaxim | 49:36954b62f503 | 171 | /// Get the Read Protection status. |
IanBenzMaxim | 49:36954b62f503 | 172 | /// @returns True if Read Protection is enabled. |
IanBenzMaxim | 34:11fffbe98ef9 | 173 | bool readProtection() const { return ((m_status & readProtectionMask) == readProtectionMask); } |
IanBenzMaxim | 49:36954b62f503 | 174 | /// Set the Read Protection status. |
IanBenzMaxim | 34:11fffbe98ef9 | 175 | void setReadProtection(bool readProtection); |
IanBenzMaxim | 34:11fffbe98ef9 | 176 | |
IanBenzMaxim | 49:36954b62f503 | 177 | /// Get the Write Protection status. |
IanBenzMaxim | 49:36954b62f503 | 178 | /// @returns True if Write Protection is enabled. |
IanBenzMaxim | 34:11fffbe98ef9 | 179 | bool writeProtection() const { return ((m_status & writeProtectionMask) == writeProtectionMask); } |
IanBenzMaxim | 49:36954b62f503 | 180 | /// Set the Write Protection status. |
IanBenzMaxim | 34:11fffbe98ef9 | 181 | void setWriteProtection(bool writeProtection); |
IanBenzMaxim | 34:11fffbe98ef9 | 182 | |
IanBenzMaxim | 49:36954b62f503 | 183 | /// Get the EEPROM Emulation Mode status. |
IanBenzMaxim | 49:36954b62f503 | 184 | /// @returns True if EEPROM Emulation Mode is enabled. |
IanBenzMaxim | 34:11fffbe98ef9 | 185 | bool eepromEmulation() const { return ((m_status & eepromEmulationMask) == eepromEmulationMask); } |
IanBenzMaxim | 49:36954b62f503 | 186 | /// Set the EEPROM Emulation Mode status. |
IanBenzMaxim | 34:11fffbe98ef9 | 187 | void setEepromEmulation(bool eepromEmulation); |
IanBenzMaxim | 34:11fffbe98ef9 | 188 | |
IanBenzMaxim | 49:36954b62f503 | 189 | /// Get the Authentication Protection status. |
IanBenzMaxim | 49:36954b62f503 | 190 | /// @returns True if Authentication Protection is enabled. |
IanBenzMaxim | 34:11fffbe98ef9 | 191 | bool authProtection() const { return ((m_status & authProtectionMask) == authProtectionMask); } |
IanBenzMaxim | 49:36954b62f503 | 192 | /// Set the Authentication Protection status. |
IanBenzMaxim | 34:11fffbe98ef9 | 193 | void setAuthProtection(bool authProtection); |
IanBenzMaxim | 34:11fffbe98ef9 | 194 | |
IanBenzMaxim | 49:36954b62f503 | 195 | /// Check if no protection options are enabled. |
IanBenzMaxim | 49:36954b62f503 | 196 | /// @returns True if no protection options are enabled. |
IanBenzMaxim | 34:11fffbe98ef9 | 197 | bool noProtection() const; |
IanBenzMaxim | 34:11fffbe98ef9 | 198 | |
IanBenzMaxim | 34:11fffbe98ef9 | 199 | bool operator==(const BlockProtection & rhs) const { return (this->m_status == rhs.m_status); } |
IanBenzMaxim | 34:11fffbe98ef9 | 200 | bool operator!=(const BlockProtection & rhs) const { return !operator==(rhs); } |
IanBenzMaxim | 34:11fffbe98ef9 | 201 | }; |
IanBenzMaxim | 34:11fffbe98ef9 | 202 | |
IanBenzMaxim | 49:36954b62f503 | 203 | /// Compute the MAC for an Authenticated Write to a memory segment. |
IanBenzMaxim | 49:36954b62f503 | 204 | /// @param MacCoproc Coprocessor with Slave Secret to use for the computation. |
IanBenzMaxim | 49:36954b62f503 | 205 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 49:36954b62f503 | 206 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 49:36954b62f503 | 207 | /// @param[in] newData New data to write to the segment. |
IanBenzMaxim | 49:36954b62f503 | 208 | /// @param[in] oldData Existing data contained in the segment. |
IanBenzMaxim | 49:36954b62f503 | 209 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 210 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 211 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 49:36954b62f503 | 212 | /// @returns The result code indicated by the coprocessor. |
IanBenzMaxim | 49:36954b62f503 | 213 | static ISha256MacCoprocessor::CmdResult computeSegmentWriteMac(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 49:36954b62f503 | 214 | unsigned int pageNum, |
IanBenzMaxim | 49:36954b62f503 | 215 | unsigned int segmentNum, |
IanBenzMaxim | 49:36954b62f503 | 216 | const Segment & newData, |
IanBenzMaxim | 49:36954b62f503 | 217 | const Segment & oldData, |
IanBenzMaxim | 49:36954b62f503 | 218 | const RomId & romId, |
IanBenzMaxim | 49:36954b62f503 | 219 | const ManId & manId, |
IanBenzMaxim | 49:36954b62f503 | 220 | Mac & mac); |
IanBenzMaxim | 49:36954b62f503 | 221 | |
IanBenzMaxim | 49:36954b62f503 | 222 | /// Compute the MAC for an Authenticated Write to a memory protection block. |
IanBenzMaxim | 49:36954b62f503 | 223 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 49:36954b62f503 | 224 | /// @param[in] newProtection New protection status to write. |
IanBenzMaxim | 49:36954b62f503 | 225 | /// @param[in] oldProtection Existing protection status in device. |
IanBenzMaxim | 49:36954b62f503 | 226 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 227 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 228 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 49:36954b62f503 | 229 | /// @returns The result code indicated by the coprocessor. |
IanBenzMaxim | 49:36954b62f503 | 230 | static ISha256MacCoprocessor::CmdResult computeProtectionWriteMac(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 49:36954b62f503 | 231 | const BlockProtection & newProtection, |
IanBenzMaxim | 49:36954b62f503 | 232 | const BlockProtection & oldProtection, |
IanBenzMaxim | 49:36954b62f503 | 233 | const RomId & romId, |
IanBenzMaxim | 49:36954b62f503 | 234 | const ManId & manId, |
IanBenzMaxim | 49:36954b62f503 | 235 | Mac & mac); |
IanBenzMaxim | 34:11fffbe98ef9 | 236 | |
IanBenzMaxim | 49:36954b62f503 | 237 | /// Compute the next secret from the existing secret. |
IanBenzMaxim | 49:36954b62f503 | 238 | /// @param MacCoproc Coprocessor with Master Secret to use for the operation. |
IanBenzMaxim | 49:36954b62f503 | 239 | /// Slave Secret will be updated with the computation result. |
IanBenzMaxim | 49:36954b62f503 | 240 | /// @param[in] bindingPage Binding data from a device memory page. |
IanBenzMaxim | 49:36954b62f503 | 241 | /// @param bindingPageNum Number of the page where the binding data is from. |
IanBenzMaxim | 49:36954b62f503 | 242 | /// @param[in] partialSecret Partial secret data from the device scratchpad. |
IanBenzMaxim | 49:36954b62f503 | 243 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 244 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 245 | /// @returns The result code indicated by the coprocessor. |
IanBenzMaxim | 49:36954b62f503 | 246 | static ISha256MacCoprocessor::CmdResult computeNextSecret(ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 49:36954b62f503 | 247 | const Page & bindingPage, |
IanBenzMaxim | 49:36954b62f503 | 248 | unsigned int bindingPageNum, |
IanBenzMaxim | 49:36954b62f503 | 249 | const Scratchpad & partialSecret, |
IanBenzMaxim | 49:36954b62f503 | 250 | const RomId & romId, |
IanBenzMaxim | 49:36954b62f503 | 251 | const ManId & manId); |
IanBenzMaxim | 49:36954b62f503 | 252 | |
IanBenzMaxim | 49:36954b62f503 | 253 | /// Compute a Page MAC for authentication. |
IanBenzMaxim | 49:36954b62f503 | 254 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 49:36954b62f503 | 255 | /// @param[in] pageData Data from a device memory page. |
IanBenzMaxim | 49:36954b62f503 | 256 | /// @param pageNum Number of the page to use data from. |
IanBenzMaxim | 49:36954b62f503 | 257 | /// @param[in] challenge Random challenge to prevent replay attacks. |
IanBenzMaxim | 49:36954b62f503 | 258 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 259 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 260 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 49:36954b62f503 | 261 | static ISha256MacCoprocessor::CmdResult computeAuthMac(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 49:36954b62f503 | 262 | const Page & pageData, |
IanBenzMaxim | 49:36954b62f503 | 263 | unsigned int pageNum, |
IanBenzMaxim | 49:36954b62f503 | 264 | const Scratchpad & challenge, |
IanBenzMaxim | 49:36954b62f503 | 265 | const RomId & romId, |
IanBenzMaxim | 49:36954b62f503 | 266 | const ManId & manId, |
IanBenzMaxim | 49:36954b62f503 | 267 | Mac & mac); |
IanBenzMaxim | 49:36954b62f503 | 268 | |
IanBenzMaxim | 49:36954b62f503 | 269 | /// Compute a Page MAC for authentication using anonymous mode. |
IanBenzMaxim | 49:36954b62f503 | 270 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 49:36954b62f503 | 271 | /// @param[in] pageData Data from a device memory page. |
IanBenzMaxim | 49:36954b62f503 | 272 | /// @param pageNum Number of the page to use data from. |
IanBenzMaxim | 49:36954b62f503 | 273 | /// @param[in] challenge Random challenge to prevent replay attacks. |
IanBenzMaxim | 49:36954b62f503 | 274 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 275 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 49:36954b62f503 | 276 | static ISha256MacCoprocessor::CmdResult computeAuthMacAnon(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 49:36954b62f503 | 277 | const Page & pageData, |
IanBenzMaxim | 49:36954b62f503 | 278 | unsigned int pageNum, |
IanBenzMaxim | 49:36954b62f503 | 279 | const Scratchpad & challenge, |
IanBenzMaxim | 49:36954b62f503 | 280 | const ManId & manId, |
IanBenzMaxim | 49:36954b62f503 | 281 | Mac & mac); |
IanBenzMaxim | 49:36954b62f503 | 282 | |
IanBenzMaxim | 62:43039aeca2ab | 283 | /// Number of segments per page. |
IanBenzMaxim | 62:43039aeca2ab | 284 | static const unsigned int segmentsPerPage = (Page::length / Segment::length); |
IanBenzMaxim | 62:43039aeca2ab | 285 | |
IanBenzMaxim | 49:36954b62f503 | 286 | /// Manufacturer ID |
IanBenzMaxim | 33:a4c015046956 | 287 | ManId manId; |
IanBenzMaxim | 25:bdb1c5a53b58 | 288 | |
IanBenzMaxim | 49:36954b62f503 | 289 | /// Enable low voltage timing |
IanBenzMaxim | 34:11fffbe98ef9 | 290 | bool lowVoltage; |
IanBenzMaxim | 25:bdb1c5a53b58 | 291 | |
IanBenzMaxim | 49:36954b62f503 | 292 | /// @param OW_master 1-Wire Master to use for communication with DS28E15/22/25. |
IanBenzMaxim | 49:36954b62f503 | 293 | /// @param lowVoltage Enable low voltage timing. |
IanBenzMaxim | 34:11fffbe98ef9 | 294 | DS28E15_22_25(OneWireMaster & OW_master, bool lowVoltage = false); |
IanBenzMaxim | 25:bdb1c5a53b58 | 295 | |
IanBenzMaxim | 25:bdb1c5a53b58 | 296 | // Const member functions should not affect the state of the memory, block protection, or secret on the DS28Exx. |
IanBenzMaxim | 25:bdb1c5a53b58 | 297 | // Scratchpad on the DS28Exx is considered mutable. |
IanBenzMaxim | 25:bdb1c5a53b58 | 298 | |
IanBenzMaxim | 49:36954b62f503 | 299 | /// Get the number of memory pages from the ROM ID of this device. |
IanBenzMaxim | 34:11fffbe98ef9 | 300 | MemoryPages memoryPages(); |
IanBenzMaxim | 49:36954b62f503 | 301 | |
IanBenzMaxim | 49:36954b62f503 | 302 | /// Get the number of protection blocks from the ROM ID of this device. |
IanBenzMaxim | 34:11fffbe98ef9 | 303 | ProtectionBlocks protectionBlocks(); |
IanBenzMaxim | 25:bdb1c5a53b58 | 304 | |
IanBenzMaxim | 49:36954b62f503 | 305 | /// Perform Load and Lock Secret command on the device. |
IanBenzMaxim | 49:36954b62f503 | 306 | /// @note The secret should already be stored in the scratchpad on the device. |
IanBenzMaxim | 49:36954b62f503 | 307 | /// @param lock Prevent further changes to the secret on the device after loading. |
IanBenzMaxim | 34:11fffbe98ef9 | 308 | CmdResult loadSecret(bool lock); |
IanBenzMaxim | 49:36954b62f503 | 309 | |
IanBenzMaxim | 49:36954b62f503 | 310 | /// Perform Write Scratchpad operation on the device. |
IanBenzMaxim | 49:36954b62f503 | 311 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 312 | /// @param[in] data Data to write to the scratchpad. |
IanBenzMaxim | 34:11fffbe98ef9 | 313 | CmdResult writeScratchpad(const Scratchpad & data) const; |
IanBenzMaxim | 49:36954b62f503 | 314 | |
IanBenzMaxim | 49:36954b62f503 | 315 | /// Perform a Read Scratchpad operation on the device. |
IanBenzMaxim | 49:36954b62f503 | 316 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 317 | /// @param[out] data Buffer to read data from the scratchpad into. |
IanBenzMaxim | 49:36954b62f503 | 318 | CmdResult readScratchpad(Scratchpad & data) const; |
IanBenzMaxim | 49:36954b62f503 | 319 | |
IanBenzMaxim | 49:36954b62f503 | 320 | /// Read memory segment using the Read Memory command on the device. |
IanBenzMaxim | 49:36954b62f503 | 321 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 322 | /// @param pageNum Page number for read operation. |
IanBenzMaxim | 49:36954b62f503 | 323 | /// @param segmentNum Segment number within page for read operation. |
IanBenzMaxim | 49:36954b62f503 | 324 | /// @param[out] data Buffer to read data from the segment into. |
IanBenzMaxim | 50:e967f9befbd0 | 325 | /// @param continuing True if continuing a previous Read Memory command. |
IanBenzMaxim | 50:e967f9befbd0 | 326 | /// False to begin a new command. |
IanBenzMaxim | 50:e967f9befbd0 | 327 | CmdResult readSegment(unsigned int pageNum, unsigned int segmentNum, Segment & data, bool continuing = false) const; |
IanBenzMaxim | 49:36954b62f503 | 328 | |
IanBenzMaxim | 49:36954b62f503 | 329 | /// Write memory segment using the Write Memory command. |
IanBenzMaxim | 49:36954b62f503 | 330 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 331 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 49:36954b62f503 | 332 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 49:36954b62f503 | 333 | /// @param[in] data Data to write to the memory segment. |
IanBenzMaxim | 49:36954b62f503 | 334 | /// @param continuing True to continue writing with the next sequential segment. |
IanBenzMaxim | 49:36954b62f503 | 335 | /// False to begin a new command. |
IanBenzMaxim | 34:11fffbe98ef9 | 336 | CmdResult writeSegment(unsigned int pageNum, unsigned int segmentNum, const Segment & data, bool continuing = false); |
IanBenzMaxim | 49:36954b62f503 | 337 | |
IanBenzMaxim | 49:36954b62f503 | 338 | /// Read memory page using the Read Memory command on the device. |
IanBenzMaxim | 49:36954b62f503 | 339 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 340 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 49:36954b62f503 | 341 | /// @param[out] rdbuf Buffer to read data from the page into. |
IanBenzMaxim | 49:36954b62f503 | 342 | /// @param continuing True if continuing a previous Read Memory command. |
IanBenzMaxim | 49:36954b62f503 | 343 | /// False to begin a new command. |
IanBenzMaxim | 34:11fffbe98ef9 | 344 | CmdResult readPage(unsigned int pageNum, Page & rdbuf, bool continuing = false) const; |
IanBenzMaxim | 49:36954b62f503 | 345 | |
IanBenzMaxim | 49:36954b62f503 | 346 | /// Perform a Compute and Lock Secret command on the device. |
IanBenzMaxim | 49:36954b62f503 | 347 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 348 | /// @param pageNum Page number to use as the binding data. |
IanBenzMaxim | 49:36954b62f503 | 349 | /// @param lock Prevent further changes to the secret on the device after computing. |
IanBenzMaxim | 34:11fffbe98ef9 | 350 | CmdResult computeSecret(unsigned int pageNum, bool lock); |
IanBenzMaxim | 49:36954b62f503 | 351 | |
IanBenzMaxim | 49:36954b62f503 | 352 | /// Perform a Compute Page MAC command on the device. |
IanBenzMaxim | 49:36954b62f503 | 353 | /// Read back the MAC and verify the CRC16. |
IanBenzMaxim | 49:36954b62f503 | 354 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 355 | /// @param pageNum Page number to use for the computation. |
IanBenzMaxim | 49:36954b62f503 | 356 | /// @param anon True to compute in anonymous mode where ROM ID is not used. |
IanBenzMaxim | 49:36954b62f503 | 357 | /// @param[out] mac The device computed MAC. |
IanBenzMaxim | 49:36954b62f503 | 358 | CmdResult computeReadPageMac(unsigned int pageNum, bool anon, Mac & mac) const; |
IanBenzMaxim | 49:36954b62f503 | 359 | |
IanBenzMaxim | 49:36954b62f503 | 360 | /// Read the status of a memory protection block using the Read Status command. |
IanBenzMaxim | 49:36954b62f503 | 361 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 362 | /// @param blockNum Block number to to read status of. |
IanBenzMaxim | 49:36954b62f503 | 363 | /// @param[out] protection Receives protection status read from device. |
IanBenzMaxim | 34:11fffbe98ef9 | 364 | CmdResult readBlockProtection(unsigned int blockNum, BlockProtection & protection); |
IanBenzMaxim | 49:36954b62f503 | 365 | |
IanBenzMaxim | 50:e967f9befbd0 | 366 | /// @{ |
IanBenzMaxim | 50:e967f9befbd0 | 367 | /// Read the status of all memory protection blocks using the Read Status command. |
IanBenzMaxim | 50:e967f9befbd0 | 368 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 50:e967f9befbd0 | 369 | /// @param[out] protection Receives protection statuses read from device. |
IanBenzMaxim | 51:a65f031e997b | 370 | CmdResult readAllBlockProtection(BlockProtection (&protection)[DS28E15_BLOCKS]) const; |
IanBenzMaxim | 51:a65f031e997b | 371 | CmdResult readAllBlockProtection(BlockProtection (&protection)[DS28E25_BLOCKS]) const; |
IanBenzMaxim | 50:e967f9befbd0 | 372 | /// @} |
IanBenzMaxim | 50:e967f9befbd0 | 373 | |
IanBenzMaxim | 50:e967f9befbd0 | 374 | /// Read the personality bytes using the Read Status command. |
IanBenzMaxim | 50:e967f9befbd0 | 375 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 50:e967f9befbd0 | 376 | /// @param[out] personality Receives personality read from device. |
IanBenzMaxim | 51:a65f031e997b | 377 | CmdResult readPersonality(Personality & personality) const; |
IanBenzMaxim | 50:e967f9befbd0 | 378 | |
IanBenzMaxim | 49:36954b62f503 | 379 | /// Update the status of a memory protection block using the Write Page Protection command. |
IanBenzMaxim | 49:36954b62f503 | 380 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 381 | /// @param[in] Desired protection status for the block. |
IanBenzMaxim | 49:36954b62f503 | 382 | /// It is not possible to disable existing protections. |
IanBenzMaxim | 49:36954b62f503 | 383 | /// @param continuing True to continue a previous Write Page Protection command. |
IanBenzMaxim | 49:36954b62f503 | 384 | /// False to begin a new command. |
IanBenzMaxim | 50:e967f9befbd0 | 385 | CmdResult writeBlockProtection(const BlockProtection & protection); |
IanBenzMaxim | 49:36954b62f503 | 386 | |
IanBenzMaxim | 49:36954b62f503 | 387 | /// Update the status of a memory protection block using the Authenticated Write Page Protection command. |
IanBenzMaxim | 49:36954b62f503 | 388 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 389 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 49:36954b62f503 | 390 | /// @param[in] newProtection New protection status to write. |
IanBenzMaxim | 49:36954b62f503 | 391 | /// @param[in] oldProtection Existing protection status in device. |
IanBenzMaxim | 49:36954b62f503 | 392 | /// @param continuing True to continue a previous Authenticated Write Page Protection command. |
IanBenzMaxim | 49:36954b62f503 | 393 | /// False to begin a new command. |
IanBenzMaxim | 34:11fffbe98ef9 | 394 | CmdResult writeAuthBlockProtection(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 34:11fffbe98ef9 | 395 | const BlockProtection & newProtection, |
IanBenzMaxim | 50:e967f9befbd0 | 396 | const BlockProtection & oldProtection); |
IanBenzMaxim | 49:36954b62f503 | 397 | |
IanBenzMaxim | 49:36954b62f503 | 398 | /// Write memory segment with authentication using the Authenticated Write Memory command. |
IanBenzMaxim | 49:36954b62f503 | 399 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 400 | /// @param MacCoproc Coprocessor to use for Write MAC computation. |
IanBenzMaxim | 49:36954b62f503 | 401 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 49:36954b62f503 | 402 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 49:36954b62f503 | 403 | /// @param[in] newData New data to write to the segment. |
IanBenzMaxim | 49:36954b62f503 | 404 | /// @param[in] oldData Existing data contained in the segment. |
IanBenzMaxim | 49:36954b62f503 | 405 | /// @param continuing True to continue writing with the next sequential segment. |
IanBenzMaxim | 49:36954b62f503 | 406 | /// False to begin a new command. |
IanBenzMaxim | 25:bdb1c5a53b58 | 407 | CmdResult writeAuthSegment(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 34:11fffbe98ef9 | 408 | unsigned int pageNum, |
IanBenzMaxim | 34:11fffbe98ef9 | 409 | unsigned int segmentNum, |
IanBenzMaxim | 34:11fffbe98ef9 | 410 | const Segment & newData, |
IanBenzMaxim | 34:11fffbe98ef9 | 411 | const Segment & oldData, |
IanBenzMaxim | 34:11fffbe98ef9 | 412 | bool continuing = false); |
IanBenzMaxim | 49:36954b62f503 | 413 | |
IanBenzMaxim | 49:36954b62f503 | 414 | /// Write memory segment with authentication using the Authenticated Write Memory command. |
IanBenzMaxim | 49:36954b62f503 | 415 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 416 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 49:36954b62f503 | 417 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 49:36954b62f503 | 418 | /// @param[in] newData New data to write to the segment. |
IanBenzMaxim | 49:36954b62f503 | 419 | /// @param[in] mac Write MAC computed for this operation. |
IanBenzMaxim | 49:36954b62f503 | 420 | /// @param continuing True to continue writing with the next sequential segment. |
IanBenzMaxim | 49:36954b62f503 | 421 | /// False to begin a new command. |
IanBenzMaxim | 34:11fffbe98ef9 | 422 | CmdResult writeAuthSegmentMac(unsigned int pageNum, |
IanBenzMaxim | 25:bdb1c5a53b58 | 423 | unsigned int segmentNum, |
IanBenzMaxim | 33:a4c015046956 | 424 | const Segment & newData, |
IanBenzMaxim | 33:a4c015046956 | 425 | const Mac & mac, |
IanBenzMaxim | 34:11fffbe98ef9 | 426 | bool continuing = false); |
IanBenzMaxim | 25:bdb1c5a53b58 | 427 | |
IanBenzMaxim | 25:bdb1c5a53b58 | 428 | private: |
IanBenzMaxim | 34:11fffbe98ef9 | 429 | static const unsigned int shaComputationDelayMs = 3; |
IanBenzMaxim | 34:11fffbe98ef9 | 430 | static const unsigned int eepromWriteDelayMs = 10; |
IanBenzMaxim | 34:11fffbe98ef9 | 431 | unsigned int secretEepromWriteDelayMs() const { return (lowVoltage ? 200 : 100); } |
IanBenzMaxim | 34:11fffbe98ef9 | 432 | |
IanBenzMaxim | 25:bdb1c5a53b58 | 433 | OneWireMaster & m_OW_master; |
IanBenzMaxim | 49:36954b62f503 | 434 | |
IanBenzMaxim | 49:36954b62f503 | 435 | /// Read status bytes which are either personality or block protection. |
IanBenzMaxim | 49:36954b62f503 | 436 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 437 | /// @param personality True to read personality or false to read block protection. |
IanBenzMaxim | 49:36954b62f503 | 438 | /// @param allpages True to read all pages or false to read one page. |
IanBenzMaxim | 49:36954b62f503 | 439 | /// @param pageNum Page number if reading block protection. |
IanBenzMaxim | 49:36954b62f503 | 440 | /// @param rdbuf Buffer to receive data read from device. |
IanBenzMaxim | 50:e967f9befbd0 | 441 | CmdResult readStatus(bool personality, bool allpages, unsigned int blockNum, std::uint8_t * rdbuf) const; |
IanBenzMaxim | 50:e967f9befbd0 | 442 | |
IanBenzMaxim | 51:a65f031e997b | 443 | /// Extends readStatus() to use the BlockProtection wrapper. |
IanBenzMaxim | 51:a65f031e997b | 444 | /// @see readAllBlockProtection(BlockProtection (&)[DS28E15_BLOCKS]) |
IanBenzMaxim | 51:a65f031e997b | 445 | template <ProtectionBlocks blocks> CmdResult readAllBlockProtection(BlockProtection (&protection)[blocks]) const; |
IanBenzMaxim | 25:bdb1c5a53b58 | 446 | }; |
IanBenzMaxim | 25:bdb1c5a53b58 | 447 | |
IanBenzMaxim | 49:36954b62f503 | 448 | #endif |