diff options
author | Saqib Khan <khansa@us.ibm.com> | 2017-09-19 14:23:28 -0500 |
---|---|---|
committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2017-10-04 02:53:17 +0000 |
commit | 26a960d011a05ca27018ccedcacb3377f0ff6575 (patch) | |
tree | 7b9710390b6e66ab48277ea487468293088db94e | |
parent | ee590c744d2bc28bbe0a3aaad1eb4c3568414f5b (diff) | |
download | phosphor-bmc-code-mgmt-26a960d011a05ca27018ccedcacb3377f0ff6575.tar.gz phosphor-bmc-code-mgmt-26a960d011a05ca27018ccedcacb3377f0ff6575.zip |
Use sha512 to calculate the versionID for images.
- We needed a hash algorithm that can be replicated on
multiple platforms and across multiple programming languages.
- This would allow the user to calculate the versionID by
executing the following command to obtain correct versionID.
echo -n "<versionID>" | sha512sum | cut -b 1-8
- This is part of the change required for openbmc/openbmc#2323
Change-Id: Ibc96a2871f07cb549c482c06dec7090e6ba3a766
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
-rwxr-xr-x | Makefile.am | 4 | ||||
-rwxr-xr-x | test/Makefile.am | 2 | ||||
-rwxr-xr-x | test/utest.cpp | 21 | ||||
-rw-r--r-- | version.cpp | 20 |
4 files changed, 33 insertions, 14 deletions
diff --git a/Makefile.am b/Makefile.am index f17ca41..c434f21 100755 --- a/Makefile.am +++ b/Makefile.am @@ -52,7 +52,9 @@ generic_ldflags = \ $(PHOSPHOR_DBUS_INTERFACES_LIBS) \ $(SDBUSPLUS_LIBS) \ $(PHOSPHOR_LOGGING_LIBS) \ - -lstdc++fs + -lstdc++fs \ + -lssl \ + -lcrypto xyz/openbmc_project/Software/Version/error.hpp: ${top_srcdir}/xyz/openbmc_project/Software/Version.errors.yaml @mkdir -p `dirname $@` diff --git a/test/Makefile.am b/test/Makefile.am index ab98534..6923088 100755 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -12,7 +12,7 @@ utest_CXXFLAGS = $(PTHREAD_CFLAGS) $(PHOSPHOR_LOGGING_CFLAGS) \ $(PHOSPHOR_DBUS_INTERFACES_CFLAGS) utest_LDFLAGS = -lgtest_main -lgtest $(PTHREAD_LIBS) \ $(PHOSPHOR_DBUS_INTERFACES_LIBS) $(OESDK_TESTCASE_FLAGS) \ - $(PHOSPHOR_LOGGING_LIBS) -lstdc++fs + $(PHOSPHOR_LOGGING_LIBS) -lstdc++fs -lssl -lcrypto utest_SOURCES = utest.cpp utest_LDADD = $(top_builddir)/phosphor_version_software_manager-version.o diff --git a/test/utest.cpp b/test/utest.cpp index 3195a2f..f777742 100755 --- a/test/utest.cpp +++ b/test/utest.cpp @@ -6,6 +6,7 @@ #include <iostream> #include <sstream> #include <string> +#include <openssl/sha.h> using namespace phosphor::software::manager; namespace fs = std::experimental::filesystem; @@ -56,12 +57,18 @@ TEST_F(VersionTest, TestGetValue) /** @brief Make sure we correctly get the Id from getId()*/ TEST_F(VersionTest, TestGetId) { - std::stringstream hexId; auto version = "test-id"; - - hexId << std::hex << ((std::hash<std::string> {}( - version)) & 0xFFFFFFFF); - - EXPECT_EQ(Version::getId(version), hexId.str()); - + unsigned char digest[SHA512_DIGEST_LENGTH]; + SHA512_CTX ctx; + SHA512_Init(&ctx); + SHA512_Update(&ctx, version, strlen(version)); + SHA512_Final(digest, &ctx); + char mdString[SHA512_DIGEST_LENGTH*2+1]; + for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) + { + snprintf(&mdString[i*2], 3, "%02x", (unsigned int)digest[i]); + } + std::string hexId = std::string(mdString); + hexId = hexId.substr(0, 8); + EXPECT_EQ(Version::getId(version), hexId); } diff --git a/version.cpp b/version.cpp index e6dd9de..133e5a4 100644 --- a/version.cpp +++ b/version.cpp @@ -6,6 +6,7 @@ #include <phosphor-logging/log.hpp> #include "config.h" #include "version.hpp" +#include <openssl/sha.h> namespace phosphor { @@ -59,7 +60,6 @@ std::string Version::getValue(const std::string& manifestFilePath, std::string Version::getId(const std::string& version) { - std::stringstream hexId; if (version.empty()) { @@ -67,10 +67,20 @@ std::string Version::getId(const std::string& version) throw std::runtime_error("Version is empty"); } - // Only want 8 hex digits. - hexId << std::hex << ((std::hash<std::string> {}( - version)) & 0xFFFFFFFF); - return hexId.str(); + unsigned char digest[SHA512_DIGEST_LENGTH]; + SHA512_CTX ctx; + SHA512_Init(&ctx); + SHA512_Update(&ctx, version.c_str(), strlen(version.c_str())); + SHA512_Final(digest, &ctx); + char mdString[SHA512_DIGEST_LENGTH*2+1]; + for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) + { + snprintf(&mdString[i*2], 3, "%02x", (unsigned int)digest[i]); + } + + // Only need 8 hex digits. + std::string hexId = std::string(mdString); + return (hexId.substr(0, 8)); } std::string Version::getBMCVersion(const std::string& releaseFilePath) |