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