Safe overloading with std::equal
Since C++20, there is a safer way to overload std::equal:
template< class InputIt1, class InputIt2 >
constexpr bool equal( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );
Because if they are different lengths, it won't run past on either
side. In theory, they should always be the same length.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I1d25331521c1590b5f448f93b657f58f9f06d778
diff --git a/integrity_algo.cpp b/integrity_algo.cpp
index 43035dc..dc87625 100644
--- a/integrity_algo.cpp
+++ b/integrity_algo.cpp
@@ -41,14 +41,16 @@
bool AlgoSHA1::verifyIntegrityData(
const std::vector<uint8_t>& packet, const size_t length,
- std::vector<uint8_t>::const_iterator integrityData) const
+ std::vector<uint8_t>::const_iterator integrityDataBegin,
+ std::vector<uint8_t>::const_iterator integrityDataEnd) const
{
auto output = generateHMAC(
packet.data() + message::parser::RMCP_SESSION_HEADER_SIZE, length);
// Verify if the generated integrity data for the packet and the received
// integrity data matches.
- return (std::equal(output.begin(), output.end(), integrityData));
+ return (std::equal(output.begin(), output.end(), integrityDataBegin,
+ integrityDataEnd));
}
std::vector<uint8_t>
@@ -105,14 +107,16 @@
bool AlgoSHA256::verifyIntegrityData(
const std::vector<uint8_t>& packet, const size_t length,
- std::vector<uint8_t>::const_iterator integrityData) const
+ std::vector<uint8_t>::const_iterator integrityDataBegin,
+ std::vector<uint8_t>::const_iterator integrityDataEnd) const
{
auto output = generateHMAC(
packet.data() + message::parser::RMCP_SESSION_HEADER_SIZE, length);
// Verify if the generated integrity data for the packet and the received
// integrity data matches.
- return (std::equal(output.begin(), output.end(), integrityData));
+ return (std::equal(output.begin(), output.end(), integrityDataBegin,
+ integrityDataEnd));
}
std::vector<uint8_t>
diff --git a/integrity_algo.hpp b/integrity_algo.hpp
index 3640ce2..d451d0e 100644
--- a/integrity_algo.hpp
+++ b/integrity_algo.hpp
@@ -62,14 +62,17 @@
*
* @param[in] packet - Incoming IPMI packet
* @param[in] packetLen - Packet length excluding authCode
- * @param[in] integrityData - Iterator to the authCode in the packet
+ * @param[in] integrityDataBegin - Begin iterator to the authCode in the
+ * packet
+ * @param[in] integrityDataEnd - End to the authCode in the packet
*
* @return true if authcode in the packet is equal to one generated
* using integrity algorithm on the packet data, false otherwise
*/
bool virtual verifyIntegrityData(
const std::vector<uint8_t>& packet, const size_t packetLen,
- std::vector<uint8_t>::const_iterator integrityData) const = 0;
+ std::vector<uint8_t>::const_iterator integrityDataBegin,
+ std::vector<uint8_t>::const_iterator integrityDataEnd) const = 0;
/**
* @brief Generate integrity data for the outgoing IPMI packet
@@ -178,14 +181,17 @@
* @param[in] packet - Incoming IPMI packet
* @param[in] length - Length of the data in the packet to calculate
* the integrity data
- * @param[in] integrityData - Iterator to the authCode in the packet
+ * @param[in] integrityDataBegin - Begin iterator to the authCode in the
+ * packet
+ * @param[in] integrityDataEnd - End to the authCode in the packet
*
* @return true if authcode in the packet is equal to one generated
* using integrity algorithm on the packet data, false otherwise
*/
bool verifyIntegrityData(
const std::vector<uint8_t>& packet, const size_t length,
- std::vector<uint8_t>::const_iterator integrityData) const override;
+ std::vector<uint8_t>::const_iterator integrityDataBegin,
+ std::vector<uint8_t>::const_iterator integrityDataEnd) const override;
/**
* @brief Generate integrity data for the outgoing IPMI packet
@@ -260,14 +266,17 @@
* @param[in] packet - Incoming IPMI packet
* @param[in] length - Length of the data in the packet to calculate
* the integrity data
- * @param[in] integrityData - Iterator to the authCode in the packet
+ * @param[in] integrityDataBegin - Begin iterator to the authCode in the
+ * packet
+ * @param[in] integrityDataEnd - End to the authCode in the packet
*
* @return true if authcode in the packet is equal to one generated
* using integrity algorithm on the packet data, false otherwise
*/
bool verifyIntegrityData(
const std::vector<uint8_t>& packet, const size_t length,
- std::vector<uint8_t>::const_iterator integrityData) const override;
+ std::vector<uint8_t>::const_iterator integrityDataBegin,
+ std::vector<uint8_t>::const_iterator integrityDataEnd) const override;
/**
* @brief Generate integrity data for the outgoing IPMI packet
diff --git a/message_parsers.cpp b/message_parsers.cpp
index d0fe344..c6dd4f8 100644
--- a/message_parsers.cpp
+++ b/message_parsers.cpp
@@ -366,7 +366,8 @@
size_t length = packet.size() - integrityAlgo->authCodeLength -
message::parser::RMCP_SESSION_HEADER_SIZE;
- return integrityAlgo->verifyIntegrityData(packet, length, integrityIter);
+ return integrityAlgo->verifyIntegrityData(packet, length, integrityIter,
+ packet.cend());
}
void addIntegrityData(std::vector<uint8_t>& packet,
diff --git a/test/cipher.cpp b/test/cipher.cpp
index 09bd479..fa3e020 100644
--- a/test/cipher.cpp
+++ b/test/cipher.cpp
@@ -131,7 +131,7 @@
auto check = algoPtr->verifyIntegrityData(
packet, packetSize - message::parser::RMCP_SESSION_HEADER_SIZE,
- integrityIter);
+ integrityIter, packet.cend());
EXPECT_EQ(true, check);
}
@@ -151,7 +151,7 @@
// Point to the integrity data in the packet
auto integrityIter = packet.cbegin();
- std::advance(integrityIter, packet.size());
+ std::advance(integrityIter, integrity.size());
/*
* Step-2 Invoke the verifyIntegrityData API and validate the response
@@ -168,7 +168,7 @@
// Verify the Integrity Data
auto check = algoPtr->verifyIntegrityData(
packet, packet.size() - message::parser::RMCP_SESSION_HEADER_SIZE,
- integrityIter);
+ integrityIter, packet.cend());
EXPECT_EQ(false, check);
}
@@ -295,7 +295,7 @@
auto check = algoPtr->verifyIntegrityData(
packet, packetSize - message::parser::RMCP_SESSION_HEADER_SIZE,
- integrityIter);
+ integrityIter, packet.cend());
EXPECT_EQ(true, check);
}
@@ -315,7 +315,7 @@
// Point to the integrity data in the packet
auto integrityIter = packet.cbegin();
- std::advance(integrityIter, packet.size());
+ std::advance(integrityIter, integrity.size());
/*
* Step-2 Invoke the verifyIntegrityData API and validate the response
@@ -333,7 +333,7 @@
// Verify the Integrity Data
auto check = algoPtr->verifyIntegrityData(
packet, packet.size() - message::parser::RMCP_SESSION_HEADER_SIZE,
- integrityIter);
+ integrityIter, packet.cend());
EXPECT_EQ(false, check);
}