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 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_ |
| 18 | #define UPDATE_ENGINE_OMAHA_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 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 30 | // Omaha uses base64 encoded SHA-256 as the hash. This class provides a simple |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 31 | // wrapper around OpenSSL providing such a formatted hash of data 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 |
| 34 | // or more calls to hash(). |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | |
| 36 | namespace chromeos_update_engine { |
| 37 | |
| 38 | class OmahaHashCalculator { |
| 39 | public: |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 40 | OmahaHashCalculator(); |
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 |
| 53 | // OpenSSl that no more data will come in and base64 encodes the resulting |
| 54 | // hash. |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 55 | // Returns true on success. |
| 56 | bool Finalize(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 57 | |
| 58 | // Gets the hash. Finalize() must have been called. |
| 59 | const std::string& hash() const { |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 60 | DCHECK(!hash_.empty()) << "Call Finalize() first"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | return hash_; |
| 62 | } |
| 63 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 64 | const brillo::Blob& raw_hash() const { |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 65 | DCHECK(!raw_hash_.empty()) << "Call Finalize() first"; |
| 66 | return raw_hash_; |
| 67 | } |
| 68 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 69 | // Gets the current hash context. Note that the string will contain binary |
| 70 | // data (including \0 characters). |
| 71 | std::string GetContext() const; |
| 72 | |
| 73 | // Sets the current hash context. |context| must the string returned by a |
| 74 | // previous OmahaHashCalculator::GetContext method call. Returns true on |
| 75 | // success, and false otherwise. |
| 76 | bool SetContext(const std::string& context); |
| 77 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 78 | static bool RawHashOfBytes(const void* data, |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 79 | size_t length, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 80 | brillo::Blob* out_hash); |
| 81 | static bool RawHashOfData(const brillo::Blob& data, |
| 82 | brillo::Blob* out_hash); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 83 | static off_t RawHashOfFile(const std::string& name, off_t length, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 84 | brillo::Blob* out_hash); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 85 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 86 | // Used by tests |
| 87 | static std::string OmahaHashOfBytes(const void* data, size_t length); |
| 88 | static std::string OmahaHashOfString(const std::string& str); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 89 | static std::string OmahaHashOfData(const brillo::Blob& data); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 | |
| 91 | private: |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 92 | // If non-empty, the final base64 encoded hash and the raw hash. Will only be |
| 93 | // set to non-empty when Finalize is called. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 94 | std::string hash_; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 95 | brillo::Blob raw_hash_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 96 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 97 | // Init success |
| 98 | bool valid_; |
| 99 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 100 | // The hash state used by OpenSSL |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 101 | SHA256_CTX ctx_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 102 | DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator); |
| 103 | }; |
| 104 | |
| 105 | } // namespace chromeos_update_engine |
| 106 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 107 | #endif // UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_ |