blob: de6e0f9376b98a686855a1e1cf7f822faa61b768 [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>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070023#include <brillo/data_encoding.h>
Darin Petkov36a58222010-10-07 22:00:09 -070024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/utils.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070026
27using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000028
29namespace chromeos_update_engine {
30
Alex Deymo39910dc2015-11-09 17:04:30 -080031HashCalculator::HashCalculator() : valid_(false) {
Darin Petkovd22cb292010-09-29 10:02:29 -070032 valid_ = (SHA256_Init(&ctx_) == 1);
33 LOG_IF(ERROR, !valid_) << "SHA256_Init failed";
rspangler@google.com49fdf182009-10-10 00:57:34 +000034}
35
36// Update is called with all of the data that should be hashed in order.
Darin Petkovd22cb292010-09-29 10:02:29 -070037// Mostly just passes the data through to OpenSSL's SHA256_Update()
Alex Deymo39910dc2015-11-09 17:04:30 -080038bool HashCalculator::Update(const void* data, size_t length) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070039 TEST_AND_RETURN_FALSE(valid_);
40 TEST_AND_RETURN_FALSE(hash_.empty());
Alex Vakulenkod2779df2014-06-16 13:19:00 -070041 static_assert(sizeof(size_t) <= sizeof(unsigned long), // NOLINT(runtime/int)
42 "length param may be truncated in SHA256_Update");
Darin Petkovd22cb292010-09-29 10:02:29 -070043 TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070044 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +000045}
46
Alex Deymo39910dc2015-11-09 17:04:30 -080047off_t HashCalculator::UpdateFile(const string& name, off_t length) {
Darin Petkov36a58222010-10-07 22:00:09 -070048 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
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070054 brillo::Blob buffer(kBufferSize);
Darin Petkov36a58222010-10-07 22:00:09 -070055 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 }
Mike Frysingerbcee2ca2014-05-14 16:28:23 -040071 IGNORE_EINTR(close(fd));
Darin Petkov36a58222010-10-07 22:00:09 -070072 return bytes_processed;
73}
74
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070075// Call Finalize() when all data has been passed in. This mostly just
76// calls OpenSSL's SHA256_Final() and then base64 encodes the hash.
Alex Deymo39910dc2015-11-09 17:04:30 -080077bool HashCalculator::Finalize() {
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070078 TEST_AND_RETURN_FALSE(hash_.empty());
79 TEST_AND_RETURN_FALSE(raw_hash_.empty());
80 raw_hash_.resize(SHA256_DIGEST_LENGTH);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080081 TEST_AND_RETURN_FALSE(SHA256_Final(raw_hash_.data(), &ctx_) == 1);
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070082
83 // Convert raw_hash_ to base64 encoding and store it in hash_.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070084 hash_ = brillo::data_encoding::Base64Encode(raw_hash_.data(),
85 raw_hash_.size());
Alex Vakulenko981a9fb2015-02-09 12:51:24 -080086 return true;
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070087}
88
Alex Deymo39910dc2015-11-09 17:04:30 -080089bool HashCalculator::RawHashOfBytes(const void* data,
90 size_t length,
91 brillo::Blob* out_hash) {
92 HashCalculator calc;
Darin Petkovadb3cef2011-01-13 16:16:08 -080093 TEST_AND_RETURN_FALSE(calc.Update(data, length));
94 TEST_AND_RETURN_FALSE(calc.Finalize());
95 *out_hash = calc.raw_hash();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070096 return true;
97}
98
Alex Deymo39910dc2015-11-09 17:04:30 -080099bool HashCalculator::RawHashOfData(const brillo::Blob& data,
100 brillo::Blob* out_hash) {
Darin Petkovadb3cef2011-01-13 16:16:08 -0800101 return RawHashOfBytes(data.data(), data.size(), out_hash);
102}
103
Alex Deymo39910dc2015-11-09 17:04:30 -0800104off_t HashCalculator::RawHashOfFile(const string& name, off_t length,
105 brillo::Blob* out_hash) {
106 HashCalculator calc;
Darin Petkov698d0412010-10-13 10:59:44 -0700107 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
Alex Deymo39910dc2015-11-09 17:04:30 -0800118string HashCalculator::HashOfBytes(const void* data, size_t length) {
119 HashCalculator calc;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800120 calc.Update(data, length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000121 calc.Finalize();
122 return calc.hash();
123}
124
Alex Deymo39910dc2015-11-09 17:04:30 -0800125string HashCalculator::HashOfString(const string& str) {
126 return HashOfBytes(str.data(), str.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127}
128
Alex Deymo39910dc2015-11-09 17:04:30 -0800129string HashCalculator::HashOfData(const brillo::Blob& data) {
130 return HashOfBytes(data.data(), data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000131}
132
Alex Deymo39910dc2015-11-09 17:04:30 -0800133string HashCalculator::GetContext() const {
Darin Petkov73058b42010-10-06 16:32:19 -0700134 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_));
135}
136
Alex Deymo39910dc2015-11-09 17:04:30 -0800137bool HashCalculator::SetContext(const string& context) {
Darin Petkov73058b42010-10-06 16:32:19 -0700138 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_));
139 memcpy(&ctx_, context.data(), sizeof(ctx_));
140 return true;
141}
142
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143} // namespace chromeos_update_engine