Skip to content

Commit de8cefa

Browse files
wrltormath1
authored andcommitted
OmahaHashCalculator: replace OpenSSL hashing with libsodium
1 parent 8c5654d commit de8cefa

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

.github/workflows/c-cpp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- uses: actions/checkout@v2
1616
- name: install deps
1717
run: |
18-
sudo apt update && sudo apt install -y libunwind-dev && sudo apt install -y libblkid-dev libext2fs-dev libmount-dev curl unzip libdbus-glib-1-dev protobuf-compiler libbz2-dev libgflags-dev libssl-dev libgoogle-glog-dev libcurl4-openssl-dev libxml2-dev libprotobuf-dev cmake wget libtool autoconf libgtest-dev libgmock-dev libbrotli-dev libdivsufsort-dev
18+
sudo apt update && sudo apt install -y libunwind-dev && sudo apt install -y libblkid-dev libext2fs-dev libmount-dev curl unzip libdbus-glib-1-dev protobuf-compiler libbz2-dev libgflags-dev libssl-dev libgoogle-glog-dev libcurl4-openssl-dev libxml2-dev libprotobuf-dev cmake wget libtool autoconf libgtest-dev libgmock-dev libbrotli-dev libdivsufsort-dev libsodium-dev
1919
- name: prep rootdev
2020
run: |
2121
curl -sSL -o /tmp/seismograph.zip https://github.com/kinvolk/seismograph/archive/flatcar-master.zip

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ PKG_CHECK_MODULES([DEPS],
8181
libcrypto
8282
libcurl
8383
libglog
84+
libsodium
8485
libssl
8586
libxml-2.0
8687
protobuf])

src/update_engine/main.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <gflags/gflags.h>
66
#include <glib.h>
77
#include <glog/logging.h>
8+
#include <sodium.h>
89

910
#include "update_engine/certificate_checker.h"
1011
#include "update_engine/dbus_constants.h"
@@ -74,6 +75,8 @@ void SetupDbusService(UpdateEngineService* service) {
7475
} // namespace chromeos_update_engine
7576

7677
int main(int argc, char** argv) {
78+
PLOG_IF(FATAL, sodium_init() < 0 ) << "the cryptographic lib couldn't be initialized; it is not safe to use";
79+
7780
// Disable glog's default behavior of logging to files.
7881
FLAGS_logtostderr = true;
7982
GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);

src/update_engine/omaha_hash_calculator.cc

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,20 @@ class ScopedBioHandle {
5252
};
5353

5454
OmahaHashCalculator::OmahaHashCalculator() : valid_(false) {
55-
valid_ = (SHA256_Init(&ctx_) == 1);
56-
LOG_IF(ERROR, !valid_) << "SHA256_Init failed";
55+
valid_ = !crypto_hash_sha256_init(&hash_state_);
56+
LOG_IF(ERROR, !valid_) << "crypto_hash_sha256_init() failed";
5757
}
5858

5959
// Update is called with all of the data that should be hashed in order.
6060
// Mostly just passes the data through to OpenSSL's SHA256_Update()
6161
bool OmahaHashCalculator::Update(const char* data, size_t length) {
6262
TEST_AND_RETURN_FALSE(valid_);
6363
TEST_AND_RETURN_FALSE(hash_.empty());
64-
static_assert(sizeof(size_t) <= sizeof(unsigned long),
65-
"length param may be truncated in SHA256_Update");
66-
TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1);
64+
static_assert(sizeof(size_t) <= sizeof(unsigned long long),
65+
"length param may be truncated in crypto_hash_sha256_update");
66+
67+
TEST_AND_RETURN_FALSE(crypto_hash_sha256_update(&hash_state_,
68+
reinterpret_cast<const unsigned char *>(data), length) == 0);
6769
return true;
6870
}
6971

@@ -170,13 +172,14 @@ bool OmahaHashCalculator::Base64Decode(const string& raw_in,
170172
bool OmahaHashCalculator::Finalize() {
171173
TEST_AND_RETURN_FALSE(hash_.empty());
172174
TEST_AND_RETURN_FALSE(raw_hash_.empty());
173-
raw_hash_.resize(SHA256_DIGEST_LENGTH);
175+
raw_hash_.resize(crypto_hash_sha256_BYTES);
176+
174177
TEST_AND_RETURN_FALSE(
175-
SHA256_Final(reinterpret_cast<unsigned char*>(&raw_hash_[0]),
176-
&ctx_) == 1);
178+
crypto_hash_sha256_final(&hash_state_,
179+
reinterpret_cast<unsigned char*>(raw_hash_.data())) == 0);
177180

178181
// Convert raw_hash_ to base64 encoding and store it in hash_.
179-
return Base64Encode(&raw_hash_[0], raw_hash_.size(), &hash_);
182+
return Base64Encode(raw_hash_.data(), raw_hash_.size(), &hash_);
180183
}
181184

182185
bool OmahaHashCalculator::RawHashOfBytes(const char* data,
@@ -221,16 +224,16 @@ string OmahaHashCalculator::OmahaHashOfString(const string& str) {
221224
}
222225

223226
string OmahaHashCalculator::OmahaHashOfData(const vector<char>& data) {
224-
return OmahaHashOfBytes(&data[0], data.size());
227+
return OmahaHashOfBytes(data.data(), data.size());
225228
}
226229

227230
string OmahaHashCalculator::GetContext() const {
228-
return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_));
231+
return string(reinterpret_cast<const char*>(&hash_state_), sizeof(hash_state_));
229232
}
230233

231234
bool OmahaHashCalculator::SetContext(const std::string& context) {
232-
TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_));
233-
memcpy(&ctx_, context.data(), sizeof(ctx_));
235+
TEST_AND_RETURN_FALSE(context.size() == sizeof(hash_state_));
236+
memcpy(&hash_state_, context.data(), sizeof(hash_state_));
234237
return true;
235238
}
236239

src/update_engine/omaha_hash_calculator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <vector>
1111

1212
#include <glog/logging.h>
13-
#include <openssl/sha.h>
13+
#include <sodium.h>
1414

1515
#include "macros.h"
1616

@@ -94,7 +94,7 @@ class OmahaHashCalculator {
9494
bool valid_;
9595

9696
// The hash state used by OpenSSL
97-
SHA256_CTX ctx_;
97+
struct crypto_hash_sha256_state hash_state_;
9898
DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator);
9999
};
100100

0 commit comments

Comments
 (0)