blob: 49f2dae3b9e141b39431371400a6a5c4eb27df8f [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -07005#include "update_engine/omaha_hash_calculator.h"
6
Darin Petkov36a58222010-10-07 22:00:09 -07007#include <fcntl.h>
8
Darin Petkov36a58222010-10-07 22:00:09 -07009#include <base/logging.h>
Chris Sosafc661a12013-02-26 14:43:21 -080010#include <base/posix/eintr_wrapper.h>
Alex Vakulenko981a9fb2015-02-09 12:51:24 -080011#include <chromeos/data_encoding.h>
Darin Petkov36a58222010-10-07 22:00:09 -070012
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070013#include "update_engine/utils.h"
14
15using std::string;
16using std::vector;
rspangler@google.com49fdf182009-10-10 00:57:34 +000017
18namespace chromeos_update_engine {
19
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070020OmahaHashCalculator::OmahaHashCalculator() : valid_(false) {
Darin Petkovd22cb292010-09-29 10:02:29 -070021 valid_ = (SHA256_Init(&ctx_) == 1);
22 LOG_IF(ERROR, !valid_) << "SHA256_Init failed";
rspangler@google.com49fdf182009-10-10 00:57:34 +000023}
24
25// Update is called with all of the data that should be hashed in order.
Darin Petkovd22cb292010-09-29 10:02:29 -070026// Mostly just passes the data through to OpenSSL's SHA256_Update()
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070027bool OmahaHashCalculator::Update(const char* data, size_t length) {
28 TEST_AND_RETURN_FALSE(valid_);
29 TEST_AND_RETURN_FALSE(hash_.empty());
Alex Vakulenkod2779df2014-06-16 13:19:00 -070030 static_assert(sizeof(size_t) <= sizeof(unsigned long), // NOLINT(runtime/int)
31 "length param may be truncated in SHA256_Update");
Darin Petkovd22cb292010-09-29 10:02:29 -070032 TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070033 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +000034}
35
Darin Petkov36a58222010-10-07 22:00:09 -070036off_t OmahaHashCalculator::UpdateFile(const string& name, off_t length) {
37 int fd = HANDLE_EINTR(open(name.c_str(), O_RDONLY));
38 if (fd < 0) {
39 return -1;
40 }
41
42 const int kBufferSize = 128 * 1024; // 128 KiB
43 vector<char> buffer(kBufferSize);
44 off_t bytes_processed = 0;
45 while (length < 0 || bytes_processed < length) {
46 off_t bytes_to_read = buffer.size();
47 if (length >= 0 && bytes_to_read > length - bytes_processed) {
48 bytes_to_read = length - bytes_processed;
49 }
50 ssize_t rc = HANDLE_EINTR(read(fd, buffer.data(), bytes_to_read));
51 if (rc == 0) { // EOF
52 break;
53 }
54 if (rc < 0 || !Update(buffer.data(), rc)) {
55 bytes_processed = -1;
56 break;
57 }
58 bytes_processed += rc;
59 }
Mike Frysingerbcee2ca2014-05-14 16:28:23 -040060 IGNORE_EINTR(close(fd));
Darin Petkov36a58222010-10-07 22:00:09 -070061 return bytes_processed;
62}
63
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070064// Call Finalize() when all data has been passed in. This mostly just
65// calls OpenSSL's SHA256_Final() and then base64 encodes the hash.
66bool OmahaHashCalculator::Finalize() {
67 TEST_AND_RETURN_FALSE(hash_.empty());
68 TEST_AND_RETURN_FALSE(raw_hash_.empty());
69 raw_hash_.resize(SHA256_DIGEST_LENGTH);
70 TEST_AND_RETURN_FALSE(
71 SHA256_Final(reinterpret_cast<unsigned char*>(&raw_hash_[0]),
72 &ctx_) == 1);
73
74 // Convert raw_hash_ to base64 encoding and store it in hash_.
Alex Vakulenko981a9fb2015-02-09 12:51:24 -080075 hash_ = chromeos::data_encoding::Base64Encode(raw_hash_.data(),
76 raw_hash_.size());
77 return true;
Andrew de los Reyes89f17be2010-10-22 13:39:09 -070078}
79
Darin Petkovadb3cef2011-01-13 16:16:08 -080080bool OmahaHashCalculator::RawHashOfBytes(const char* data,
81 size_t length,
82 vector<char>* out_hash) {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070083 OmahaHashCalculator calc;
Darin Petkovadb3cef2011-01-13 16:16:08 -080084 TEST_AND_RETURN_FALSE(calc.Update(data, length));
85 TEST_AND_RETURN_FALSE(calc.Finalize());
86 *out_hash = calc.raw_hash();
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070087 return true;
88}
89
Darin Petkovadb3cef2011-01-13 16:16:08 -080090bool OmahaHashCalculator::RawHashOfData(const vector<char>& data,
91 vector<char>* out_hash) {
92 return RawHashOfBytes(data.data(), data.size(), out_hash);
93}
94
95off_t OmahaHashCalculator::RawHashOfFile(const string& name, off_t length,
96 vector<char>* out_hash) {
Darin Petkov698d0412010-10-13 10:59:44 -070097 OmahaHashCalculator calc;
98 off_t res = calc.UpdateFile(name, length);
99 if (res < 0) {
100 return res;
101 }
102 if (!calc.Finalize()) {
103 return -1;
104 }
105 *out_hash = calc.raw_hash();
106 return res;
107}
108
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700109string OmahaHashCalculator::OmahaHashOfBytes(
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110 const void* data, size_t length) {
111 OmahaHashCalculator calc;
112 calc.Update(reinterpret_cast<const char*>(data), length);
113 calc.Finalize();
114 return calc.hash();
115}
116
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700117string OmahaHashCalculator::OmahaHashOfString(const string& str) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000118 return OmahaHashOfBytes(str.data(), str.size());
119}
120
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700121string OmahaHashCalculator::OmahaHashOfData(const vector<char>& data) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122 return OmahaHashOfBytes(&data[0], data.size());
123}
124
Darin Petkov73058b42010-10-06 16:32:19 -0700125string OmahaHashCalculator::GetContext() const {
126 return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_));
127}
128
Alex Deymof329b932014-10-30 01:37:48 -0700129bool OmahaHashCalculator::SetContext(const string& context) {
Darin Petkov73058b42010-10-06 16:32:19 -0700130 TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_));
131 memcpy(&ctx_, context.data(), sizeof(ctx_));
132 return true;
133}
134
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135} // namespace chromeos_update_engine