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