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 #include <brillo/data_encoding.h>
24 
25 #include "update_engine/common/utils.h"
26 
27 using std::string;
28 
29 namespace chromeos_update_engine {
30 
HashCalculator()31 HashCalculator::HashCalculator() : valid_(false) {
32   valid_ = (SHA256_Init(&ctx_) == 1);
33   LOG_IF(ERROR, !valid_) << "SHA256_Init failed";
34 }
35 
36 // Update is called with all of the data that should be hashed in order.
37 // Mostly just passes the data through to OpenSSL's SHA256_Update()
Update(const void * data,size_t length)38 bool HashCalculator::Update(const void* data, size_t length) {
39   TEST_AND_RETURN_FALSE(valid_);
40   TEST_AND_RETURN_FALSE(hash_.empty());
41   static_assert(sizeof(size_t) <= sizeof(unsigned long),  // NOLINT(runtime/int)
42                 "length param may be truncated in SHA256_Update");
43   TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1);
44   return true;
45 }
46 
UpdateFile(const string & name,off_t length)47 off_t HashCalculator::UpdateFile(const string& name, off_t length) {
48   int fd = HANDLE_EINTR(open(name.c_str(), O_RDONLY));
49   if (fd < 0) {
50     return -1;
51   }
52 
53   const int kBufferSize = 128 * 1024;  // 128 KiB
54   brillo::Blob buffer(kBufferSize);
55   off_t bytes_processed = 0;
56   while (length < 0 || bytes_processed < length) {
57     off_t bytes_to_read = buffer.size();
58     if (length >= 0 && bytes_to_read > length - bytes_processed) {
59       bytes_to_read = length - bytes_processed;
60     }
61     ssize_t rc = HANDLE_EINTR(read(fd, buffer.data(), bytes_to_read));
62     if (rc == 0) {  // EOF
63       break;
64     }
65     if (rc < 0 || !Update(buffer.data(), rc)) {
66       bytes_processed = -1;
67       break;
68     }
69     bytes_processed += rc;
70   }
71   IGNORE_EINTR(close(fd));
72   return bytes_processed;
73 }
74 
75 // Call Finalize() when all data has been passed in. This mostly just
76 // calls OpenSSL's SHA256_Final() and then base64 encodes the hash.
Finalize()77 bool HashCalculator::Finalize() {
78   TEST_AND_RETURN_FALSE(hash_.empty());
79   TEST_AND_RETURN_FALSE(raw_hash_.empty());
80   raw_hash_.resize(SHA256_DIGEST_LENGTH);
81   TEST_AND_RETURN_FALSE(SHA256_Final(raw_hash_.data(), &ctx_) == 1);
82 
83   // Convert raw_hash_ to base64 encoding and store it in hash_.
84   hash_ = brillo::data_encoding::Base64Encode(raw_hash_.data(),
85                                               raw_hash_.size());
86   return true;
87 }
88 
RawHashOfBytes(const void * data,size_t length,brillo::Blob * out_hash)89 bool HashCalculator::RawHashOfBytes(const void* data,
90                                     size_t length,
91                                     brillo::Blob* out_hash) {
92   HashCalculator calc;
93   TEST_AND_RETURN_FALSE(calc.Update(data, length));
94   TEST_AND_RETURN_FALSE(calc.Finalize());
95   *out_hash = calc.raw_hash();
96   return true;
97 }
98 
RawHashOfData(const brillo::Blob & data,brillo::Blob * out_hash)99 bool HashCalculator::RawHashOfData(const brillo::Blob& data,
100                                    brillo::Blob* out_hash) {
101   return RawHashOfBytes(data.data(), data.size(), out_hash);
102 }
103 
RawHashOfFile(const string & name,off_t length,brillo::Blob * out_hash)104 off_t HashCalculator::RawHashOfFile(const string& name, 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 
HashOfBytes(const void * data,size_t length)118 string HashCalculator::HashOfBytes(const void* data, size_t length) {
119   HashCalculator calc;
120   calc.Update(data, length);
121   calc.Finalize();
122   return calc.hash();
123 }
124 
HashOfString(const string & str)125 string HashCalculator::HashOfString(const string& str) {
126   return HashOfBytes(str.data(), str.size());
127 }
128 
HashOfData(const brillo::Blob & data)129 string HashCalculator::HashOfData(const brillo::Blob& data) {
130   return HashOfBytes(data.data(), data.size());
131 }
132 
GetContext() const133 string HashCalculator::GetContext() const {
134   return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_));
135 }
136 
SetContext(const string & context)137 bool HashCalculator::SetContext(const string& context) {
138   TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_));
139   memcpy(&ctx_, context.data(), sizeof(ctx_));
140   return true;
141 }
142 
143 }  // namespace chromeos_update_engine
144