| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 |  | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 17 | #include "update_engine/omaha_hash_calculator.h" | 
|  | 18 |  | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 19 | #include <fcntl.h> | 
|  | 20 |  | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 21 | #include <base/logging.h> | 
| Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 22 | #include <base/posix/eintr_wrapper.h> | 
| Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 23 | #include <chromeos/data_encoding.h> | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 24 |  | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 25 | #include "update_engine/utils.h" | 
|  | 26 |  | 
|  | 27 | using std::string; | 
|  | 28 | using std::vector; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 29 |  | 
|  | 30 | namespace chromeos_update_engine { | 
|  | 31 |  | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 32 | OmahaHashCalculator::OmahaHashCalculator() : valid_(false) { | 
| Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 33 | valid_ = (SHA256_Init(&ctx_) == 1); | 
|  | 34 | LOG_IF(ERROR, !valid_) << "SHA256_Init failed"; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | } | 
|  | 36 |  | 
|  | 37 | // 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] | 38 | // Mostly just passes the data through to OpenSSL's SHA256_Update() | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 39 | bool OmahaHashCalculator::Update(const void* data, size_t length) { | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 40 | TEST_AND_RETURN_FALSE(valid_); | 
|  | 41 | TEST_AND_RETURN_FALSE(hash_.empty()); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 42 | static_assert(sizeof(size_t) <= sizeof(unsigned long),  // NOLINT(runtime/int) | 
|  | 43 | "length param may be truncated in SHA256_Update"); | 
| Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 44 | TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1); | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 45 | return true; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 48 | off_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 Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 55 | chromeos::Blob buffer(kBufferSize); | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 56 | 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 Frysinger | bcee2ca | 2014-05-14 16:28:23 -0400 | [diff] [blame] | 72 | IGNORE_EINTR(close(fd)); | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 73 | return bytes_processed; | 
|  | 74 | } | 
|  | 75 |  | 
| Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 76 | // Call Finalize() when all data has been passed in. This mostly just | 
|  | 77 | // calls OpenSSL's SHA256_Final() and then base64 encodes the hash. | 
|  | 78 | bool 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 Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 82 | TEST_AND_RETURN_FALSE(SHA256_Final(raw_hash_.data(), &ctx_) == 1); | 
| Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 83 |  | 
|  | 84 | // Convert raw_hash_ to base64 encoding and store it in hash_. | 
| Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 85 | hash_ = chromeos::data_encoding::Base64Encode(raw_hash_.data(), | 
|  | 86 | raw_hash_.size()); | 
|  | 87 | return true; | 
| Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 90 | bool OmahaHashCalculator::RawHashOfBytes(const void* data, | 
| Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 91 | size_t length, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 92 | chromeos::Blob* out_hash) { | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 93 | OmahaHashCalculator calc; | 
| Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 94 | TEST_AND_RETURN_FALSE(calc.Update(data, length)); | 
|  | 95 | TEST_AND_RETURN_FALSE(calc.Finalize()); | 
|  | 96 | *out_hash = calc.raw_hash(); | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 97 | return true; | 
|  | 98 | } | 
|  | 99 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 100 | bool OmahaHashCalculator::RawHashOfData(const chromeos::Blob& data, | 
|  | 101 | chromeos::Blob* out_hash) { | 
| Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 102 | return RawHashOfBytes(data.data(), data.size(), out_hash); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | off_t OmahaHashCalculator::RawHashOfFile(const string& name, off_t length, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 106 | chromeos::Blob* out_hash) { | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 107 | 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 Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 119 | string OmahaHashCalculator::OmahaHashOfBytes(const void* data, size_t length) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 120 | OmahaHashCalculator calc; | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 121 | calc.Update(data, length); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 122 | calc.Finalize(); | 
|  | 123 | return calc.hash(); | 
|  | 124 | } | 
|  | 125 |  | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 126 | string OmahaHashCalculator::OmahaHashOfString(const string& str) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 127 | return OmahaHashOfBytes(str.data(), str.size()); | 
|  | 128 | } | 
|  | 129 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 130 | string OmahaHashCalculator::OmahaHashOfData(const chromeos::Blob& data) { | 
|  | 131 | return OmahaHashOfBytes(data.data(), data.size()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 134 | string OmahaHashCalculator::GetContext() const { | 
|  | 135 | return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_)); | 
|  | 136 | } | 
|  | 137 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 138 | bool OmahaHashCalculator::SetContext(const string& context) { | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 139 | TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_)); | 
|  | 140 | memcpy(&ctx_, context.data(), sizeof(ctx_)); | 
|  | 141 | return true; | 
|  | 142 | } | 
|  | 143 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 144 | }  // namespace chromeos_update_engine |