1 //
2 // Copyright (C) 2009 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/common/hash_calculator.h"
18
19 #include <fcntl.h>
20
21 #include <base/logging.h>
22 #include <base/posix/eintr_wrapper.h>
23
24 #include "update_engine/common/utils.h"
25
26 using std::string;
27
28 namespace chromeos_update_engine {
29
HashCalculator()30 HashCalculator::HashCalculator() : valid_(false) {
31 valid_ = (SHA256_Init(&ctx_) == 1);
32 LOG_IF(ERROR, !valid_) << "SHA256_Init failed";
33 }
34
35 // Update is called with all of the data that should be hashed in order.
36 // Mostly just passes the data through to OpenSSL's SHA256_Update()
Update(const void * data,size_t length)37 bool HashCalculator::Update(const void* data, size_t length) {
38 TEST_AND_RETURN_FALSE(valid_);
39 TEST_AND_RETURN_FALSE(raw_hash_.empty());
40 static_assert(sizeof(size_t) <= sizeof(unsigned long), // NOLINT(runtime/int)
41 "length param may be truncated in SHA256_Update");
42 TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1);
43 return true;
44 }
45
UpdateFile(const string & name,off_t length)46 off_t HashCalculator::UpdateFile(const string& name, off_t length) {
47 int fd = HANDLE_EINTR(open(name.c_str(), O_RDONLY));
48 if (fd < 0) {
49 return -1;
50 }
51
52 const int kBufferSize = 128 * 1024; // 128 KiB
53 brillo::Blob buffer(kBufferSize);
54 off_t bytes_processed = 0;
55 while (length < 0 || bytes_processed < length) {
56 off_t bytes_to_read = buffer.size();
57 if (length >= 0 && bytes_to_read > length - bytes_processed) {
58 bytes_to_read = length - bytes_processed;
59 }
60 ssize_t rc = HANDLE_EINTR(read(fd, buffer.data(), bytes_to_read));
61 if (rc == 0) { // EOF
62 break;
63 }
64 if (rc < 0 || !Update(buffer.data(), rc)) {
65 bytes_processed = -1;
66 break;
67 }
68 bytes_processed += rc;
69 }
70 IGNORE_EINTR(close(fd));
71 return bytes_processed;
72 }
73
74 // Call Finalize() when all data has been passed in. This mostly just
75 // calls OpenSSL's SHA256_Final().
Finalize()76 bool HashCalculator::Finalize() {
77 TEST_AND_RETURN_FALSE(raw_hash_.empty());
78 raw_hash_.resize(SHA256_DIGEST_LENGTH);
79 TEST_AND_RETURN_FALSE(SHA256_Final(raw_hash_.data(), &ctx_) == 1);
80 return true;
81 }
82
RawHashOfBytes(const void * data,size_t length,brillo::Blob * out_hash)83 bool HashCalculator::RawHashOfBytes(const void* data,
84 size_t length,
85 brillo::Blob* out_hash) {
86 HashCalculator calc;
87 TEST_AND_RETURN_FALSE(calc.Update(data, length));
88 TEST_AND_RETURN_FALSE(calc.Finalize());
89 *out_hash = calc.raw_hash();
90 return true;
91 }
92
RawHashOfData(const brillo::Blob & data,brillo::Blob * out_hash)93 bool HashCalculator::RawHashOfData(const brillo::Blob& data,
94 brillo::Blob* out_hash) {
95 return RawHashOfBytes(data.data(), data.size(), out_hash);
96 }
97
RawHashOfFile(const string & name,brillo::Blob * out_hash)98 bool HashCalculator::RawHashOfFile(const string& name, brillo::Blob* out_hash) {
99 const auto file_size = utils::FileSize(name);
100 return RawHashOfFile(name, file_size, out_hash) == file_size;
101 }
102
RawHashOfFile(const string & name,off_t length,brillo::Blob * out_hash)103 off_t HashCalculator::RawHashOfFile(const string& name,
104 off_t length,
105 brillo::Blob* out_hash) {
106 HashCalculator calc;
107 off_t res = calc.UpdateFile(name, length);
108 if (res < 0) {
109 return res;
110 }
111 if (!calc.Finalize()) {
112 return -1;
113 }
114 *out_hash = calc.raw_hash();
115 return res;
116 }
117
GetContext() const118 string HashCalculator::GetContext() const {
119 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_));
120 }
121
SetContext(const string & context)122 bool HashCalculator::SetContext(const string& context) {
123 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_));
124 memcpy(&ctx_, context.data(), sizeof(ctx_));
125 return true;
126 }
127
SHA256Digest(std::string_view blob)128 std::string HashCalculator::SHA256Digest(std::string_view blob) {
129 std::vector<unsigned char> hash;
130 HashCalculator::RawHashOfBytes(blob.data(), blob.size(), &hash);
131 return HexEncode(hash);
132 }
133
SHA256Digest(std::vector<unsigned char> blob)134 std::string HashCalculator::SHA256Digest(std::vector<unsigned char> blob) {
135 return SHA256Digest(ToStringView(blob));
136 }
137
SHA256Digest(std::vector<char> blob)138 std::string HashCalculator::SHA256Digest(std::vector<char> blob) {
139 return SHA256Digest(ToStringView(blob));
140 }
141
142 } // namespace chromeos_update_engine
143