blob: ebfdb6e92d3d297e125b8ff46858616eb5982ab6 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/hash_calculator.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070018
Darin Petkov36a58222010-10-07 22:00:09 -070019#include <fcntl.h>
20
Darin Petkov36a58222010-10-07 22:00:09 -070021#include <base/logging.h>
Chris Sosafc661a12013-02-26 14:43:21 -080022#include <base/posix/eintr_wrapper.h>
Darin Petkov36a58222010-10-07 22:00:09 -070023
Alex Deymo39910dc2015-11-09 17:04:30 -080024#include "update_engine/common/utils.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070025
26using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000027
28namespace chromeos_update_engine {
29
Alex Deymo39910dc2015-11-09 17:04:30 -080030HashCalculator::HashCalculator() : valid_(false) {
Darin Petkovd22cb292010-09-29 10:02:29 -070031 valid_ = (SHA256_Init(&ctx_) == 1);
32 LOG_IF(ERROR, !valid_) << "SHA256_Init failed";
rspangler@google.com49fdf182009-10-10 00:57:34 +000033}
34
35// Update is called with all of the data that should be hashed in order.
Darin Petkovd22cb292010-09-29 10:02:29 -070036// Mostly just passes the data through to OpenSSL's SHA256_Update()
Alex Deymo39910dc2015-11-09 17:04:30 -080037bool HashCalculator::Update(const void* data, size_t length) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070038 TEST_AND_RETURN_FALSE(valid_);
Sen Jiang2703ef42017-03-16 13:36:21 -070039 TEST_AND_RETURN_FALSE(raw_hash_.empty());
Alex Vakulenkod2779df2014-06-16 13:19:00 -070040 static_assert(sizeof(size_t) <= sizeof(unsigned long), // NOLINT(runtime/int)
41 "length param may be truncated in SHA256_Update");
Darin Petkovd22cb292010-09-29 10:02:29 -070042 TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070043 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +000044}
45
Alex Deymo39910dc2015-11-09 17:04:30 -080046off_t HashCalculator::UpdateFile(const string& name, off_t length) {
Darin Petkov36a58222010-10-07 22:00:09 -070047 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
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070053 brillo::Blob buffer(kBufferSize);
Darin Petkov36a58222010-10-07 22:00:09 -070054 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 }
Mike Frysingerbcee2ca2014-05-14 16:28:23 -040070 IGNORE_EINTR(close(fd));
Darin Petkov36a58222010-10-07 22:00:09 -070071 return bytes_processed;
72}
73
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070074// Call Finalize() when all data has been passed in. This mostly just
Sen Jiang2703ef42017-03-16 13:36:21 -070075// calls OpenSSL's SHA256_Final().
Alex Deymo39910dc2015-11-09 17:04:30 -080076bool HashCalculator::Finalize() {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070077 TEST_AND_RETURN_FALSE(raw_hash_.empty());
78 raw_hash_.resize(SHA256_DIGEST_LENGTH);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080079 TEST_AND_RETURN_FALSE(SHA256_Final(raw_hash_.data(), &ctx_) == 1);
Alex Vakulenko981a9fb2015-02-09 12:51:24 -080080 return true;
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070081}
82
Alex Deymo39910dc2015-11-09 17:04:30 -080083bool HashCalculator::RawHashOfBytes(const void* data,
84 size_t length,
85 brillo::Blob* out_hash) {
86 HashCalculator calc;
Darin Petkovadb3cef2011-01-13 16:16:08 -080087 TEST_AND_RETURN_FALSE(calc.Update(data, length));
88 TEST_AND_RETURN_FALSE(calc.Finalize());
89 *out_hash = calc.raw_hash();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070090 return true;
91}
92
Alex Deymo39910dc2015-11-09 17:04:30 -080093bool HashCalculator::RawHashOfData(const brillo::Blob& data,
94 brillo::Blob* out_hash) {
Darin Petkovadb3cef2011-01-13 16:16:08 -080095 return RawHashOfBytes(data.data(), data.size(), out_hash);
96}
97
Alex Deymo39910dc2015-11-09 17:04:30 -080098off_t HashCalculator::RawHashOfFile(const string& name, off_t length,
99 brillo::Blob* out_hash) {
100 HashCalculator calc;
Darin Petkov698d0412010-10-13 10:59:44 -0700101 off_t res = calc.UpdateFile(name, length);
102 if (res < 0) {
103 return res;
104 }
105 if (!calc.Finalize()) {
106 return -1;
107 }
108 *out_hash = calc.raw_hash();
109 return res;
110}
111
Alex Deymo39910dc2015-11-09 17:04:30 -0800112string HashCalculator::GetContext() const {
Darin Petkov73058b42010-10-06 16:32:19 -0700113 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_));
114}
115
Alex Deymo39910dc2015-11-09 17:04:30 -0800116bool HashCalculator::SetContext(const string& context) {
Darin Petkov73058b42010-10-06 16:32:19 -0700117 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_));
118 memcpy(&ctx_, context.data(), sizeof(ctx_));
119 return true;
120}
121
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122} // namespace chromeos_update_engine