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