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,off_t length,brillo::Blob * out_hash)98 off_t HashCalculator::RawHashOfFile(const string& name,
99                                     off_t length,
100                                     brillo::Blob* out_hash) {
101   HashCalculator calc;
102   off_t res = calc.UpdateFile(name, length);
103   if (res < 0) {
104     return res;
105   }
106   if (!calc.Finalize()) {
107     return -1;
108   }
109   *out_hash = calc.raw_hash();
110   return res;
111 }
112 
GetContext() const113 string HashCalculator::GetContext() const {
114   return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_));
115 }
116 
SetContext(const string & context)117 bool HashCalculator::SetContext(const string& context) {
118   TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_));
119   memcpy(&ctx_, context.data(), sizeof(ctx_));
120   return true;
121 }
122 
123 }  // namespace chromeos_update_engine
124