blob: cb01a56516e37eff6fac9767c32e090efb6e9f7c [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
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070017#include "update_engine/omaha_hash_calculator.h"
18
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
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070025#include "update_engine/utils.h"
26
27using std::string;
28using std::vector;
rspangler@google.com49fdf182009-10-10 00:57:34 +000029
30namespace chromeos_update_engine {
31
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070032OmahaHashCalculator::OmahaHashCalculator() : valid_(false) {
Darin Petkovd22cb292010-09-29 10:02:29 -070033 valid_ = (SHA256_Init(&ctx_) == 1);
34 LOG_IF(ERROR, !valid_) << "SHA256_Init failed";
rspangler@google.com49fdf182009-10-10 00:57:34 +000035}
36
37// Update is called with all of the data that should be hashed in order.
Darin Petkovd22cb292010-09-29 10:02:29 -070038// Mostly just passes the data through to OpenSSL's SHA256_Update()
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080039bool OmahaHashCalculator::Update(const void* data, size_t length) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070040 TEST_AND_RETURN_FALSE(valid_);
41 TEST_AND_RETURN_FALSE(hash_.empty());
Alex Vakulenkod2779df2014-06-16 13:19:00 -070042 static_assert(sizeof(size_t) <= sizeof(unsigned long), // NOLINT(runtime/int)
43 "length param may be truncated in SHA256_Update");
Darin Petkovd22cb292010-09-29 10:02:29 -070044 TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070045 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +000046}
47
Darin Petkov36a58222010-10-07 22:00:09 -070048off_t OmahaHashCalculator::UpdateFile(const string& name, off_t length) {
49 int fd = HANDLE_EINTR(open(name.c_str(), O_RDONLY));
50 if (fd < 0) {
51 return -1;
52 }
53
54 const int kBufferSize = 128 * 1024; // 128 KiB
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070055 brillo::Blob buffer(kBufferSize);
Darin Petkov36a58222010-10-07 22:00:09 -070056 off_t bytes_processed = 0;
57 while (length < 0 || bytes_processed < length) {
58 off_t bytes_to_read = buffer.size();
59 if (length >= 0 && bytes_to_read > length - bytes_processed) {
60 bytes_to_read = length - bytes_processed;
61 }
62 ssize_t rc = HANDLE_EINTR(read(fd, buffer.data(), bytes_to_read));
63 if (rc == 0) { // EOF
64 break;
65 }
66 if (rc < 0 || !Update(buffer.data(), rc)) {
67 bytes_processed = -1;
68 break;
69 }
70 bytes_processed += rc;
71 }
Mike Frysingerbcee2ca2014-05-14 16:28:23 -040072 IGNORE_EINTR(close(fd));
Darin Petkov36a58222010-10-07 22:00:09 -070073 return bytes_processed;
74}
75
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070076// Call Finalize() when all data has been passed in. This mostly just
77// calls OpenSSL's SHA256_Final() and then base64 encodes the hash.
78bool OmahaHashCalculator::Finalize() {
79 TEST_AND_RETURN_FALSE(hash_.empty());
80 TEST_AND_RETURN_FALSE(raw_hash_.empty());
81 raw_hash_.resize(SHA256_DIGEST_LENGTH);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080082 TEST_AND_RETURN_FALSE(SHA256_Final(raw_hash_.data(), &ctx_) == 1);
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070083
84 // Convert raw_hash_ to base64 encoding and store it in hash_.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070085 hash_ = brillo::data_encoding::Base64Encode(raw_hash_.data(),
86 raw_hash_.size());
Alex Vakulenko981a9fb2015-02-09 12:51:24 -080087 return true;
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070088}
89
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080090bool OmahaHashCalculator::RawHashOfBytes(const void* data,
Darin Petkovadb3cef2011-01-13 16:16:08 -080091 size_t length,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070092 brillo::Blob* out_hash) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070093 OmahaHashCalculator calc;
Darin Petkovadb3cef2011-01-13 16:16:08 -080094 TEST_AND_RETURN_FALSE(calc.Update(data, length));
95 TEST_AND_RETURN_FALSE(calc.Finalize());
96 *out_hash = calc.raw_hash();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070097 return true;
98}
99
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700100bool OmahaHashCalculator::RawHashOfData(const brillo::Blob& data,
101 brillo::Blob* out_hash) {
Darin Petkovadb3cef2011-01-13 16:16:08 -0800102 return RawHashOfBytes(data.data(), data.size(), out_hash);
103}
104
105off_t OmahaHashCalculator::RawHashOfFile(const string& name, off_t length,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700106 brillo::Blob* out_hash) {
Darin Petkov698d0412010-10-13 10:59:44 -0700107 OmahaHashCalculator calc;
108 off_t res = calc.UpdateFile(name, length);
109 if (res < 0) {
110 return res;
111 }
112 if (!calc.Finalize()) {
113 return -1;
114 }
115 *out_hash = calc.raw_hash();
116 return res;
117}
118
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800119string OmahaHashCalculator::OmahaHashOfBytes(const void* data, size_t length) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120 OmahaHashCalculator calc;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800121 calc.Update(data, length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122 calc.Finalize();
123 return calc.hash();
124}
125
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700126string OmahaHashCalculator::OmahaHashOfString(const string& str) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127 return OmahaHashOfBytes(str.data(), str.size());
128}
129
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700130string OmahaHashCalculator::OmahaHashOfData(const brillo::Blob& data) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800131 return OmahaHashOfBytes(data.data(), data.size());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132}
133
Darin Petkov73058b42010-10-06 16:32:19 -0700134string OmahaHashCalculator::GetContext() const {
135 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_));
136}
137
Alex Deymof329b932014-10-30 01:37:48 -0700138bool OmahaHashCalculator::SetContext(const string& context) {
Darin Petkov73058b42010-10-06 16:32:19 -0700139 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_));
140 memcpy(&ctx_, context.data(), sizeof(ctx_));
141 return true;
142}
143
rspangler@google.com49fdf182009-10-10 00:57:34 +0000144} // namespace chromeos_update_engine