| 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 | #ifndef UPDATE_ENGINE_COMMON_HASH_CALCULATOR_H_ | 
|  | 18 | #define UPDATE_ENGINE_COMMON_HASH_CALCULATOR_H_ | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 |  | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 20 | #include <openssl/sha.h> | 
| Han Shen | 2643cb7 | 2012-06-26 14:45:33 -0700 | [diff] [blame] | 21 | #include <unistd.h> | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <string> | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 24 | #include <vector> | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 25 |  | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 26 | #include <base/logging.h> | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 27 | #include <base/macros.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 28 | #include <brillo/secure_blob.h> | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 29 |  | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 30 | // This class provides a simple wrapper around OpenSSL providing a hash of data | 
|  | 31 | // passed in. | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 32 | // The methods of this class must be called in a very specific order: First the | 
|  | 33 | // ctor (of course), then 0 or more calls to Update(), then Finalize(), then 0 | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 34 | // or more calls to raw_hash(). | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 |  | 
|  | 36 | namespace chromeos_update_engine { | 
|  | 37 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 38 | class HashCalculator { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 39 | public: | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 40 | HashCalculator(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 41 |  | 
|  | 42 | // Update is called with all of the data that should be hashed in order. | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 43 | // Update will read |length| bytes of |data|. | 
|  | 44 | // Returns true on success. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 45 | bool Update(const void* data, size_t length); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 46 |  | 
| Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 47 | // Updates the hash with up to |length| bytes of data from |file|. If |length| | 
|  | 48 | // is negative, reads in and updates with the whole file. Returns the number | 
|  | 49 | // of bytes that the hash was updated with, or -1 on error. | 
|  | 50 | off_t UpdateFile(const std::string& name, off_t length); | 
|  | 51 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 52 | // Call Finalize() when all data has been passed in. This method tells | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 53 | // OpenSSL that no more data will come in. | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 54 | // Returns true on success. | 
|  | 55 | bool Finalize(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 56 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 57 | const brillo::Blob& raw_hash() const { | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 58 | DCHECK(!raw_hash_.empty()) << "Call Finalize() first"; | 
|  | 59 | return raw_hash_; | 
|  | 60 | } | 
|  | 61 |  | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 62 | // Gets the current hash context. Note that the string will contain binary | 
|  | 63 | // data (including \0 characters). | 
|  | 64 | std::string GetContext() const; | 
|  | 65 |  | 
|  | 66 | // Sets the current hash context. |context| must the string returned by a | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 67 | // previous HashCalculator::GetContext method call. Returns true on success, | 
|  | 68 | // and false otherwise. | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 69 | bool SetContext(const std::string& context); | 
|  | 70 |  | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 71 | static bool RawHashOfBytes(const void* data, | 
| Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 72 | size_t length, | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 73 | brillo::Blob* out_hash); | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 74 | static bool RawHashOfData(const brillo::Blob& data, brillo::Blob* out_hash); | 
|  | 75 | static off_t RawHashOfFile(const std::string& name, | 
|  | 76 | off_t length, | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 77 | brillo::Blob* out_hash); | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 78 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 79 | private: | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 80 | // If non-empty, the final raw hash. Will only be set to non-empty when | 
|  | 81 | // Finalize is called. | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 82 | brillo::Blob raw_hash_; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 83 |  | 
| Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 84 | // Init success | 
|  | 85 | bool valid_; | 
|  | 86 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 87 | // The hash state used by OpenSSL | 
| Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 88 | SHA256_CTX ctx_; | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 89 | DISALLOW_COPY_AND_ASSIGN(HashCalculator); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 | }; | 
|  | 91 |  | 
|  | 92 | }  // namespace chromeos_update_engine | 
|  | 93 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 94 | #endif  // UPDATE_ENGINE_COMMON_HASH_CALCULATOR_H_ |