| 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 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/hash_calculator.h" | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 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> | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 23 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 24 | #include "update_engine/common/utils.h" | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 25 |  | 
 | 26 | using std::string; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 |  | 
 | 28 | namespace chromeos_update_engine { | 
 | 29 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | HashCalculator::HashCalculator() : valid_(false) { | 
| Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 31 |   valid_ = (SHA256_Init(&ctx_) == 1); | 
 | 32 |   LOG_IF(ERROR, !valid_) << "SHA256_Init failed"; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | } | 
 | 34 |  | 
 | 35 | // 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] | 36 | // Mostly just passes the data through to OpenSSL's SHA256_Update() | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 37 | bool HashCalculator::Update(const void* data, size_t length) { | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 38 |   TEST_AND_RETURN_FALSE(valid_); | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 39 |   TEST_AND_RETURN_FALSE(raw_hash_.empty()); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 40 |   static_assert(sizeof(size_t) <= sizeof(unsigned long),  // NOLINT(runtime/int) | 
 | 41 |                 "length param may be truncated in SHA256_Update"); | 
| Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 42 |   TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1); | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 43 |   return true; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 44 | } | 
 | 45 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 46 | off_t HashCalculator::UpdateFile(const string& name, off_t length) { | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 47 |   int fd = HANDLE_EINTR(open(name.c_str(), O_RDONLY)); | 
 | 48 |   if (fd < 0) { | 
 | 49 |     return -1; | 
 | 50 |   } | 
 | 51 |  | 
 | 52 |   const int kBufferSize = 128 * 1024;  // 128 KiB | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 53 |   brillo::Blob buffer(kBufferSize); | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 54 |   off_t bytes_processed = 0; | 
 | 55 |   while (length < 0 || bytes_processed < length) { | 
 | 56 |     off_t bytes_to_read = buffer.size(); | 
 | 57 |     if (length >= 0 && bytes_to_read > length - bytes_processed) { | 
 | 58 |       bytes_to_read = length - bytes_processed; | 
 | 59 |     } | 
 | 60 |     ssize_t rc = HANDLE_EINTR(read(fd, buffer.data(), bytes_to_read)); | 
 | 61 |     if (rc == 0) {  // EOF | 
 | 62 |       break; | 
 | 63 |     } | 
 | 64 |     if (rc < 0 || !Update(buffer.data(), rc)) { | 
 | 65 |       bytes_processed = -1; | 
 | 66 |       break; | 
 | 67 |     } | 
 | 68 |     bytes_processed += rc; | 
 | 69 |   } | 
| Mike Frysinger | bcee2ca | 2014-05-14 16:28:23 -0400 | [diff] [blame] | 70 |   IGNORE_EINTR(close(fd)); | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 71 |   return bytes_processed; | 
 | 72 | } | 
 | 73 |  | 
| Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 74 | // Call Finalize() when all data has been passed in. This mostly just | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 75 | // calls OpenSSL's SHA256_Final(). | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 76 | bool HashCalculator::Finalize() { | 
| Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 77 |   TEST_AND_RETURN_FALSE(raw_hash_.empty()); | 
 | 78 |   raw_hash_.resize(SHA256_DIGEST_LENGTH); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 79 |   TEST_AND_RETURN_FALSE(SHA256_Final(raw_hash_.data(), &ctx_) == 1); | 
| Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 80 |   return true; | 
| Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 81 | } | 
 | 82 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 83 | bool HashCalculator::RawHashOfBytes(const void* data, | 
 | 84 |                                     size_t length, | 
 | 85 |                                     brillo::Blob* out_hash) { | 
 | 86 |   HashCalculator calc; | 
| Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 87 |   TEST_AND_RETURN_FALSE(calc.Update(data, length)); | 
 | 88 |   TEST_AND_RETURN_FALSE(calc.Finalize()); | 
 | 89 |   *out_hash = calc.raw_hash(); | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 90 |   return true; | 
 | 91 | } | 
 | 92 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 93 | bool HashCalculator::RawHashOfData(const brillo::Blob& data, | 
 | 94 |                                    brillo::Blob* out_hash) { | 
| Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 95 |   return RawHashOfBytes(data.data(), data.size(), out_hash); | 
 | 96 | } | 
 | 97 |  | 
| Kelvin Zhang | 9105f4b | 2021-04-26 13:44:49 -0400 | [diff] [blame] | 98 | bool HashCalculator::RawHashOfFile(const string& name, brillo::Blob* out_hash) { | 
 | 99 |   const auto file_size = utils::FileSize(name); | 
 | 100 |   return RawHashOfFile(name, file_size, out_hash) == file_size; | 
 | 101 | } | 
 | 102 |  | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 103 | off_t HashCalculator::RawHashOfFile(const string& name, | 
 | 104 |                                     off_t length, | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 105 |                                     brillo::Blob* out_hash) { | 
 | 106 |   HashCalculator calc; | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 107 |   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 Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 118 | string HashCalculator::GetContext() const { | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 119 |   return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_)); | 
 | 120 | } | 
 | 121 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 122 | bool HashCalculator::SetContext(const string& context) { | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 123 |   TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_)); | 
 | 124 |   memcpy(&ctx_, context.data(), sizeof(ctx_)); | 
 | 125 |   return true; | 
 | 126 | } | 
 | 127 |  | 
| Kelvin Zhang | 760c334 | 2022-02-08 13:41:49 -0800 | [diff] [blame] | 128 | std::string HashCalculator::SHA256Digest(std::string_view blob) { | 
 | 129 |   std::vector<unsigned char> hash; | 
 | 130 |   HashCalculator::RawHashOfBytes(blob.data(), blob.size(), &hash); | 
 | 131 |   return HexEncode(hash); | 
 | 132 | } | 
 | 133 |  | 
 | 134 | std::string HashCalculator::SHA256Digest(std::vector<unsigned char> blob) { | 
 | 135 |   return SHA256Digest(ToStringView(blob)); | 
 | 136 | } | 
 | 137 |  | 
 | 138 | std::string HashCalculator::SHA256Digest(std::vector<char> blob) { | 
 | 139 |   return SHA256Digest(ToStringView(blob)); | 
 | 140 | } | 
 | 141 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 142 | }  // namespace chromeos_update_engine |