rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // 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 Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 5 | #include "update_engine/omaha_hash_calculator.h" |
| 6 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 7 | #include <fcntl.h> |
| 8 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 9 | #include <base/logging.h> |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 10 | #include <base/posix/eintr_wrapper.h> |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame^] | 11 | #include <chromeos/data_encoding.h> |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 12 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 13 | #include "update_engine/utils.h" |
| 14 | |
| 15 | using std::string; |
| 16 | using std::vector; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 20 | OmahaHashCalculator::OmahaHashCalculator() : valid_(false) { |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 21 | valid_ = (SHA256_Init(&ctx_) == 1); |
| 22 | LOG_IF(ERROR, !valid_) << "SHA256_Init failed"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | // Update is called with all of the data that should be hashed in order. |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 26 | // Mostly just passes the data through to OpenSSL's SHA256_Update() |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 27 | bool OmahaHashCalculator::Update(const char* data, size_t length) { |
| 28 | TEST_AND_RETURN_FALSE(valid_); |
| 29 | TEST_AND_RETURN_FALSE(hash_.empty()); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 30 | static_assert(sizeof(size_t) <= sizeof(unsigned long), // NOLINT(runtime/int) |
| 31 | "length param may be truncated in SHA256_Update"); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 32 | TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 33 | return true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 36 | off_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 Frysinger | bcee2ca | 2014-05-14 16:28:23 -0400 | [diff] [blame] | 60 | IGNORE_EINTR(close(fd)); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 61 | return bytes_processed; |
| 62 | } |
| 63 | |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 64 | // Call Finalize() when all data has been passed in. This mostly just |
| 65 | // calls OpenSSL's SHA256_Final() and then base64 encodes the hash. |
| 66 | bool 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 Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame^] | 75 | hash_ = chromeos::data_encoding::Base64Encode(raw_hash_.data(), |
| 76 | raw_hash_.size()); |
| 77 | return true; |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 80 | bool OmahaHashCalculator::RawHashOfBytes(const char* data, |
| 81 | size_t length, |
| 82 | vector<char>* out_hash) { |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 83 | OmahaHashCalculator calc; |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 84 | TEST_AND_RETURN_FALSE(calc.Update(data, length)); |
| 85 | TEST_AND_RETURN_FALSE(calc.Finalize()); |
| 86 | *out_hash = calc.raw_hash(); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 90 | bool OmahaHashCalculator::RawHashOfData(const vector<char>& data, |
| 91 | vector<char>* out_hash) { |
| 92 | return RawHashOfBytes(data.data(), data.size(), out_hash); |
| 93 | } |
| 94 | |
| 95 | off_t OmahaHashCalculator::RawHashOfFile(const string& name, off_t length, |
| 96 | vector<char>* out_hash) { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 97 | 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 Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 109 | string OmahaHashCalculator::OmahaHashOfBytes( |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | 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 Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 117 | string OmahaHashCalculator::OmahaHashOfString(const string& str) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 118 | return OmahaHashOfBytes(str.data(), str.size()); |
| 119 | } |
| 120 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 121 | string OmahaHashCalculator::OmahaHashOfData(const vector<char>& data) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 122 | return OmahaHashOfBytes(&data[0], data.size()); |
| 123 | } |
| 124 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 125 | string OmahaHashCalculator::GetContext() const { |
| 126 | return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_)); |
| 127 | } |
| 128 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 129 | bool OmahaHashCalculator::SetContext(const string& context) { |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 130 | TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_)); |
| 131 | memcpy(&ctx_, context.data(), sizeof(ctx_)); |
| 132 | return true; |
| 133 | } |
| 134 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 135 | } // namespace chromeos_update_engine |