Skip to content

Commit

Permalink
Replaced OpenSSL calls to EVP Digest calls
Browse files Browse the repository at this point in the history
Signed-off-by: Manoj Takasi <mtakasi@amd.com>
  • Loading branch information
Manoj Takasi committed Jan 8, 2025
1 parent e61519a commit 9c55ee2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
33 changes: 22 additions & 11 deletions src/runtime_src/core/pcie/tools/cloud-daemon/azure/azure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
#include <string.h>
#include <sys/stat.h>
#include <syslog.h>
#define OPENSSL_SUPPRESS_DEPRECATED
#include <openssl/sha.h>
#undef OPENSSL_SUPPRESS_DEPRECATED
#include <openssl/evp.h>
#include <curl/curl.h>

#include <cstdio>
Expand Down Expand Up @@ -563,42 +562,54 @@ int AzureDev::Sha256AndSplit(
std::vector<std::string> &output,
std::string &sha)
{

std::stringstream shastr;
unsigned pos = 0;
// Initialize openssl
SHA256_CTX context;
if (!SHA256_Init(&context)) {
EVP_MD_CTX *context;
if(!(context = EVP_MD_CTX_new())) {
std::cerr << "Failed to initialize OpenSSL context" << std::endl;
return 1;
}

if (!EVP_DigestInit(context, EVP_sha256())) {
std::cerr << "Unable to initiate SHA256" << std::endl;
return 1;
goto out;
}

unsigned pos = 0;

while (pos < input.size()) {
std::string segment = input.substr(pos, transfer_segment_size);

if(!SHA256_Update(&context, segment.c_str(), segment.size()))
if(!EVP_DigestUpdate(context, segment.c_str(), segment.size()))
{
std::cerr << "Unable to Update SHA256 buffer" << std::endl;
return 1;
goto out;
}
output.push_back(segment);
pos += transfer_segment_size;
}

// Get Final SHA
unsigned char result[SHA256_DIGEST_LENGTH];
if(!SHA256_Final(result, &context)) {
if(!EVP_DigestFinal(context, result, NULL)) {
std::cerr << "Error finalizing SHA256 calculation" << std::endl;
return 1;
goto out;
}

// Convert the byte array into a string
std::stringstream shastr;
shastr << std::hex << std::setfill('0');
for (auto &byte: result)
shastr << std::setw(2) << (int)byte;

sha = shastr.str();
EVP_MD_CTX_free(context);
return 0;

out:
if (context)
EVP_MD_CTX_free(context);
return 1;
}

void AzureDev::get_fpga_serialNo(std::string &fpgaSerialNo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
#include <iomanip>
#include <fstream>
#include <uuid/uuid.h>
#define OPENSSL_SUPPRESS_DEPRECATED
#include <openssl/md5.h>
#undef OPENSSL_SUPPRESS_DEPRECATED
#include <openssl/evp.h>
#include "xrt/detail/xclbin.h"
#include "container.h"

Expand Down Expand Up @@ -223,10 +222,10 @@ int Container::retrieve_xclbin(const xclBin *&orig, std::vector<char> &real_xclb
std::string Container::calculate_md5(char *buf, size_t len)
{
unsigned char s[16];
MD5_CTX context;
MD5_Init(&context);
MD5_Update(&context, buf, len);
MD5_Final(s, &context);
EVP_MD_CTX *context = EVP_MD_CTX_new();
EVP_DigestInit(context, EVP_md5());
EVP_DigestUpdate(context, buf, len);
EVP_DigestFinal(context, s, NULL);

std::stringstream md5;
md5 << std::hex << std::setfill('0');
Expand All @@ -235,6 +234,7 @@ std::string Container::calculate_md5(char *buf, size_t len)
md5 << std::setw(2) << (int)byte;
}

EVP_MD_CTX_free(context);
return md5.str();
}

Expand Down

0 comments on commit 9c55ee2

Please sign in to comment.