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.
Diff: OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp
- Revision:
- 33:a4c015046956
- Parent:
- 32:bce180b544ed
- Child:
- 34:11fffbe98ef9
diff -r bce180b544ed -r a4c015046956 OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp --- a/OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp Wed Mar 30 16:50:29 2016 -0500 +++ b/OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.cpp Thu Mar 31 11:56:01 2016 -0500 @@ -65,7 +65,7 @@ DS28E15_22_25::DS28E15_22_25(OneWireMaster& OW_master) : m_OW_master(OW_master) { - std::memset(manId, 0x00, manIdLen); + std::memset(manId, 0x00, manId.length); } DS28E15_22_25::DevicePages DS28E15_22_25::devicePages() @@ -137,6 +137,7 @@ { std::uint8_t buf[256], cs; int cnt = 0; + Mac mac; buf[cnt++] = CMD_WRITE_AUTH_PROTECT; buf[cnt++] = newProtection; @@ -161,13 +162,13 @@ return CommunicationError; ISha256MacCoprocessor::CmdResult result; - result = CalculateProtectionWriteMAC256(MacCoproc, newProtection, oldProtection, romId, manId, reinterpret_cast<std::uint8_t (&)[macLen]>(buf)); + result = CalculateProtectionWriteMAC256(MacCoproc, newProtection, oldProtection, romId, manId, mac); if (result != ISha256MacCoprocessor::Success) return OperationFailure; - cnt = macLen; + cnt = 0; // send the MAC - m_OW_master.OWWriteBlock(&buf[0], macLen); + m_OW_master.OWWriteBlock(mac, mac.length); // Read CRC and CS byte m_OW_master.OWReadBlock(&buf[cnt], 3); @@ -349,30 +350,24 @@ -ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateAuthMAC256(const ISha256MacCoprocessor & MacCoproc, unsigned int pageNum, const std::uint8_t (&challenge)[scratchpadSize], const std::uint8_t (&pageData)[pageSize], const RomId & romId, const std::uint8_t (&manId)[manIdLen], bool anon, std::uint8_t (&mac)[macLen]) +ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateAuthMAC256(const ISha256MacCoprocessor & MacCoproc, unsigned int pageNum, const Scratchpad & challenge, const Page & pageData, const RomId & romId, const ManId & manId, bool anon, Mac & mac) { - std::uint8_t pageBuf[pageSize], challengeBuf[scratchpadSize], AuthMAC_data[ISha256MacCoprocessor::AuthMAC_data_len]; - - // insert page data - std::memcpy(pageBuf, pageData, pageSize); - - // insert challenge - std::memcpy(challengeBuf, challenge, scratchpadSize); + ISha256MacCoprocessor::AuthMacData authMacData; // insert ROM number or FF if (anon) - std::memset(AuthMAC_data, 0xFF, RomId::byteLen); + std::memset(authMacData, 0xFF, RomId::byteLen); else - std::memcpy(AuthMAC_data, romId, RomId::byteLen); + std::memcpy(authMacData, romId, RomId::byteLen); - AuthMAC_data[10] = pageNum; + authMacData[10] = pageNum; - AuthMAC_data[9] = manId[0]; - AuthMAC_data[8] = manId[1]; + authMacData[9] = manId[0]; + authMacData[8] = manId[1]; - AuthMAC_data[11] = 0x00; + authMacData[11] = 0x00; - return MacCoproc.ComputeAndRead_AuthMAC(pageBuf, challengeBuf, AuthMAC_data, mac); + return MacCoproc.computeAuthMac(pageData, challenge, authMacData, mac); } //-------------------------------------------------------------------------- @@ -390,15 +385,15 @@ // Returns: true - page read has correct MAC // false - Failed to read page or incorrect MAC // -ISha256MacCoprocessor::CmdResult DS28E15_22_25::AuthVerify(const ISha256MacCoprocessor & MacCoproc, unsigned int page_num, const std::uint8_t (&challenge)[scratchpadSize], const std::uint8_t (&pageData)[pageSize], const RomId & romId, const std::uint8_t (&manId)[manIdLen], bool anon, const std::uint8_t (&mac)[macLen]) +ISha256MacCoprocessor::CmdResult DS28E15_22_25::AuthVerify(const ISha256MacCoprocessor & MacCoproc, unsigned int page_num, const Scratchpad & challenge, const Page & pageData, const RomId & romId, const ManId & manId, bool anon, const Mac & mac) { - std::uint8_t calc_mac[macLen]; + Mac calc_mac; ISha256MacCoprocessor::CmdResult result; result = CalculateAuthMAC256(MacCoproc, page_num, challenge, pageData, romId, manId, anon, calc_mac); if (result == ISha256MacCoprocessor::Success) { - if (memcmp(mac, calc_mac, macLen) != 0) - result = ISha256MacCoprocessor::OperationFailure; + if (mac != calc_mac) + result = ISha256MacCoprocessor::OperationFailure; } return result; } @@ -417,7 +412,7 @@ // Returns: true - page read has correct MAC // false - Failed to read page or incorrect MAC // -OneWireSlave::CmdResult DS28E15_22_25::ComputeReadPageMAC(unsigned int page_num, bool anon, std::uint8_t (&mac)[macLen]) const +OneWireSlave::CmdResult DS28E15_22_25::ComputeReadPageMAC(unsigned int page_num, bool anon, Mac & mac) const { std::uint8_t buf[256],cs; int cnt = 0; @@ -450,14 +445,14 @@ return OperationFailure; // read the MAC and CRC - m_OW_master.OWReadBlock(&buf[0], (macLen + 2)); + m_OW_master.OWReadBlock(&buf[0], (Mac::length + 2)); // check CRC16 - if (OneWireMaster::calculateCRC16(buf, 0, (macLen + 2)) != 0xB001) + if (OneWireMaster::calculateCRC16(buf, 0, (Mac::length + 2)) != 0xB001) return CommunicationError; // copy MAC to return buffer - memcpy(mac, buf, macLen); + memcpy(mac, buf, Mac::length); return Success; } @@ -519,7 +514,7 @@ // Return: true - select complete // false - error during select, device not present // -OneWireSlave::CmdResult DS28E15_22_25::WriteScratchpad(const uint8_t (&data)[scratchpadSize]) const +OneWireSlave::CmdResult DS28E15_22_25::WriteScratchpad(const Scratchpad & data) const { std::uint8_t buf[256]; int cnt = 0, offset; @@ -540,11 +535,11 @@ offset = cnt; // add the data - memcpy(&buf[cnt], data, scratchpadSize); - cnt += scratchpadSize; + memcpy(&buf[cnt], data, data.length); + cnt += data.length; // Send the data - m_OW_master.OWWriteBlock(data, scratchpadSize); + m_OW_master.OWWriteBlock(data, data.length); // Read CRC m_OW_master.OWReadBlock(&buf[cnt], 2); @@ -620,7 +615,7 @@ // Returns: true - block read and verified CRC // false - Failed to write block (no presence or invalid CRC16) // -OneWireSlave::CmdResult DS28E15_22_25::readPage(unsigned int page, std::uint8_t (&rdbuf)[pageSize], bool continuing) const +OneWireSlave::CmdResult DS28E15_22_25::readPage(unsigned int page, Page & rdbuf, bool continuing) const { std::uint8_t buf[256]; int cnt, offset; @@ -645,8 +640,8 @@ } // read data and CRC16 - m_OW_master.OWReadBlock(&buf[cnt], (pageSize + 2)); - cnt+=34; + m_OW_master.OWReadBlock(&buf[cnt], (rdbuf.length + 2)); + cnt += 34; // check the first CRC16 if (!continuing) @@ -660,7 +655,7 @@ return CommunicationError; // copy the data to the read buffer - memcpy(rdbuf, &buf[offset], pageSize); + memcpy(rdbuf, &buf[offset], rdbuf.length); return Success; } @@ -679,7 +674,7 @@ // Returns: true - block written // false - Failed to write block (no presence or invalid CRC16) // -OneWireSlave::CmdResult DS28E15_22_25::writeAuthSegmentMAC(unsigned int pageNum, unsigned int segmentNum, const std::uint8_t (&newData)[segmentSize], const std::uint8_t (&mac)[macLen], bool continuing) +OneWireSlave::CmdResult DS28E15_22_25::writeAuthSegmentMAC(unsigned int pageNum, unsigned int segmentNum, const Segment & newData, const Mac & mac, bool continuing) { std::uint8_t buf[256], cs; int cnt, i, offset; @@ -704,11 +699,11 @@ } // add the data - for (i = 0; i < 4; i++) + for (i = 0; i < newData.length; i++) buf[cnt++] = newData[i]; // Send data - m_OW_master.OWWriteBlock(newData, segmentSize); + m_OW_master.OWWriteBlock(newData, newData.length); // read first CRC byte m_OW_master.OWReadByte(buf[cnt++]); @@ -745,10 +740,10 @@ return CommunicationError; // transmit MAC as a block - m_OW_master.OWWriteBlock(mac, macLen); + m_OW_master.OWWriteBlock(mac, mac.length); // calculate CRC on MAC - CRC16 = OneWireMaster::calculateCRC16(mac, 0, macLen); + CRC16 = OneWireMaster::calculateCRC16(mac, 0, mac.length); // append read of CRC16 and CS byte m_OW_master.OWReadBlock(&buf[0], 3); @@ -797,9 +792,9 @@ // Returns: true - mac calculated // false - Failed to calculate // -ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateSegmentWriteMAC256(const ISha256MacCoprocessor & MacCoproc, unsigned int pageNum, unsigned int segmentNum, const std::uint8_t (&newData)[segmentSize], const std::uint8_t (&oldData)[segmentSize], const RomId & romId, const std::uint8_t (&manId)[manIdLen], std::uint8_t (&mac)[macLen]) +ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateSegmentWriteMAC256(const ISha256MacCoprocessor & MacCoproc, unsigned int pageNum, unsigned int segmentNum, const Segment & newData, const Segment & oldData, const RomId & romId, const ManId & manId, Mac & mac) { - std::uint8_t MT[ISha256MacCoprocessor::WriteMAC_data_len]; + ISha256MacCoprocessor::WriteMacData MT; // insert ROM number memcpy(&MT[0], romId, RomId::byteLen); @@ -810,20 +805,20 @@ MT[8] = manId[1]; // insert old data - memcpy(&MT[12], oldData, segmentSize); + memcpy(&MT[12], oldData, Segment::length); // insert new data - memcpy(&MT[16], newData, segmentSize); + memcpy(&MT[16], newData, Segment::length); - return MacCoproc.ComputeAndRead_WriteMAC(MT, mac); + return MacCoproc.computeWriteMac(MT, mac); } -ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateProtectionWriteMAC256(const ISha256MacCoprocessor & MacCoproc, std::uint8_t newProtection, std::uint8_t oldProtection, const RomId & romId, const std::uint8_t (&manId)[manIdLen], std::uint8_t (&mac)[macLen]) +ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateProtectionWriteMAC256(const ISha256MacCoprocessor & MacCoproc, std::uint8_t newProtection, std::uint8_t oldProtection, const RomId & romId, const ManId & manId, Mac & mac) { - std::uint8_t MT[ISha256MacCoprocessor::WriteMAC_data_len]; + ISha256MacCoprocessor::WriteMacData MT; // insert ROM number std::memcpy(MT, romId, RomId::byteLen); @@ -847,7 +842,7 @@ MT[19] = (newProtection & PROT_BIT_READ) ? 0x01 : 0x00; // compute the mac - return MacCoproc.ComputeAndRead_WriteMAC(MT, mac); + return MacCoproc.computeWriteMac(MT, mac); } //-------------------------------------------------------------------------- @@ -868,7 +863,7 @@ // Returns: true - block written // false - Failed to write block (no presence or invalid CRC16) // -OneWireSlave::CmdResult DS28E15_22_25::writeAuthSegment(const ISha256MacCoprocessor & MacCoproc, unsigned int pageNum, unsigned int segmentNum, const std::uint8_t (&newData)[segmentSize], const std::uint8_t (&oldData)[segmentSize], bool continuing) +OneWireSlave::CmdResult DS28E15_22_25::writeAuthSegment(const ISha256MacCoprocessor & MacCoproc, unsigned int pageNum, unsigned int segmentNum, const Segment & newData, const Segment & oldData, bool continuing) { std::uint8_t buf[256], cs; int cnt, offset; @@ -893,11 +888,11 @@ } // add the data - for (size_t i = 0; i < segmentSize; i++) + for (size_t i = 0; i < newData.length; i++) buf[cnt++] = newData[i]; // Send data - m_OW_master.OWWriteBlock(newData, segmentSize); + m_OW_master.OWWriteBlock(newData, newData.length); // read first CRC byte m_OW_master.OWReadByte(buf[cnt++]); @@ -935,16 +930,16 @@ // compute the mac ISha256MacCoprocessor::CmdResult result; - result = CalculateSegmentWriteMAC256(MacCoproc, pageNum, segmentNum, newData, oldData, romId, manId, reinterpret_cast<std::uint8_t (&)[macLen]>(buf)); + Mac mac; + result = CalculateSegmentWriteMAC256(MacCoproc, pageNum, segmentNum, newData, oldData, romId, manId, mac); if (result != ISha256MacCoprocessor::Success) return OperationFailure; // transmit MAC as a block - cnt = 0; - m_OW_master.OWWriteBlock(buf, macLen); + m_OW_master.OWWriteBlock(mac, mac.length); // calculate CRC on MAC - CRC16 = OneWireMaster::calculateCRC16(buf, 0, macLen); + CRC16 = OneWireMaster::calculateCRC16(mac, 0, mac.length); // append read of CRC16 and CS byte m_OW_master.OWReadBlock(&buf[0], 3); @@ -981,7 +976,7 @@ -OneWireSlave::CmdResult DS28E15_22_25::readSegment(unsigned int page, unsigned int segment, std::uint8_t (&data)[segmentSize]) const +OneWireSlave::CmdResult DS28E15_22_25::readSegment(unsigned int page, unsigned int segment, Segment & data) const { OneWireMaster::CmdResult result; std::uint8_t buf[2]; @@ -997,7 +992,7 @@ // Receive data if (result == OneWireMaster::Success) - result = m_OW_master.OWReadBlock(data, segmentSize); + result = m_OW_master.OWReadBlock(data, data.length); return (result == OneWireMaster::Success ? OneWireSlave::Success : OneWireSlave::CommunicationError); } @@ -1017,7 +1012,7 @@ // Returns: true - block written // false - Failed to write block (no presence or invalid CRC16) // -OneWireSlave::CmdResult DS28E15_22_25::writeSegment(unsigned int page, unsigned int block, const std::uint8_t (&data)[segmentSize], bool continuing) +OneWireSlave::CmdResult DS28E15_22_25::writeSegment(unsigned int page, unsigned int block, const Segment & data, bool continuing) { std::uint8_t buf[256], cs; int cnt, offset; @@ -1042,11 +1037,11 @@ } // add the data - for (size_t i = 0; i < segmentSize; i++) + for (size_t i = 0; i < data.length; i++) buf[cnt++] = data[i]; // Send data - m_OW_master.OWWriteBlock(data, segmentSize); + m_OW_master.OWWriteBlock(data, data.length); // Read CRC m_OW_master.OWReadBlock(&buf[cnt], 2); @@ -1096,23 +1091,17 @@ // Returns: true if compute successful // false failed to do compute // -ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateNextSecret(ISha256MacCoprocessor & MacCoproc, const std::uint8_t (&binding)[pageSize], const std::uint8_t (&partial)[scratchpadSize], const RomId & romId, const std::uint8_t (&manId)[manIdLen], unsigned int pageNum) +ISha256MacCoprocessor::CmdResult DS28E15_22_25::CalculateNextSecret(ISha256MacCoprocessor & MacCoproc, const Page & binding, const Scratchpad & partial, const RomId & romId, const ManId & manId, unsigned int pageNum) { - std::uint8_t devicePage[pageSize], deviceScratchpad[scratchpadSize], SSecret_data[ISha256MacCoprocessor::SSecret_data_len]; + ISha256MacCoprocessor::SlaveSecretData slaveSecretData; - // insert page data - memcpy(devicePage, binding, pageSize); - - // insert challenge - memcpy(deviceScratchpad, partial, scratchpadSize); + // insert ROM number + std::memcpy(slaveSecretData, romId, RomId::byteLen); - // insert ROM number - std::memcpy(SSecret_data, romId, RomId::byteLen); + slaveSecretData[11] = 0x00; + slaveSecretData[10] = pageNum; + slaveSecretData[9] = manId[0]; + slaveSecretData[8] = manId[1]; - SSecret_data[11] = 0x00; - SSecret_data[10] = pageNum; - SSecret_data[9] = manId[0]; - SSecret_data[8] = manId[1]; - - return MacCoproc.Compute_SSecret(devicePage, deviceScratchpad, SSecret_data); + return MacCoproc.computeSlaveSecret(binding, partial, slaveSecretData); }