rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_ |
| 6 | #define UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame^] | 8 | #include <openssl/sha.h> |
Han Shen | 2643cb7 | 2012-06-26 14:45:33 -0700 | [diff] [blame] | 9 | #include <unistd.h> |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame^] | 10 | |
| 11 | #include <string> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 12 | #include <vector> |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 13 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 14 | #include <base/logging.h> |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame^] | 15 | #include <base/macros.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 17 | // 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] | 18 | // wrapper around OpenSSL providing such a formatted hash of data passed in. |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 19 | // The methods of this class must be called in a very specific order: First the |
| 20 | // ctor (of course), then 0 or more calls to Update(), then Finalize(), then 0 |
| 21 | // or more calls to hash(). |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | |
| 23 | namespace chromeos_update_engine { |
| 24 | |
| 25 | class OmahaHashCalculator { |
| 26 | public: |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 27 | OmahaHashCalculator(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | |
| 29 | // 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] | 30 | // Update will read |length| bytes of |data|. |
| 31 | // Returns true on success. |
| 32 | bool Update(const char* data, size_t length); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 34 | // Updates the hash with up to |length| bytes of data from |file|. If |length| |
| 35 | // is negative, reads in and updates with the whole file. Returns the number |
| 36 | // of bytes that the hash was updated with, or -1 on error. |
| 37 | off_t UpdateFile(const std::string& name, off_t length); |
| 38 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 39 | // Call Finalize() when all data has been passed in. This method tells |
| 40 | // OpenSSl that no more data will come in and base64 encodes the resulting |
| 41 | // hash. |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 42 | // Returns true on success. |
| 43 | bool Finalize(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 44 | |
| 45 | // Gets the hash. Finalize() must have been called. |
| 46 | const std::string& hash() const { |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 47 | DCHECK(!hash_.empty()) << "Call Finalize() first"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 48 | return hash_; |
| 49 | } |
| 50 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 51 | const std::vector<char>& raw_hash() const { |
| 52 | DCHECK(!raw_hash_.empty()) << "Call Finalize() first"; |
| 53 | return raw_hash_; |
| 54 | } |
| 55 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 56 | // Gets the current hash context. Note that the string will contain binary |
| 57 | // data (including \0 characters). |
| 58 | std::string GetContext() const; |
| 59 | |
| 60 | // Sets the current hash context. |context| must the string returned by a |
| 61 | // previous OmahaHashCalculator::GetContext method call. Returns true on |
| 62 | // success, and false otherwise. |
| 63 | bool SetContext(const std::string& context); |
| 64 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 65 | static bool RawHashOfBytes(const char* data, |
| 66 | size_t length, |
| 67 | std::vector<char>* out_hash); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 68 | static bool RawHashOfData(const std::vector<char>& data, |
| 69 | std::vector<char>* out_hash); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 70 | static off_t RawHashOfFile(const std::string& name, off_t length, |
| 71 | std::vector<char>* out_hash); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 72 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 73 | // Used by tests |
| 74 | static std::string OmahaHashOfBytes(const void* data, size_t length); |
| 75 | static std::string OmahaHashOfString(const std::string& str); |
| 76 | static std::string OmahaHashOfData(const std::vector<char>& data); |
| 77 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 78 | // Encodes data of given size as a base64 out string |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 79 | static bool Base64Encode(const void* data, size_t size, std::string* out); |
| 80 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 81 | // Decodes given base64-encoded in string into the out vector. Since the |
| 82 | // output can have null characters, we're returning a byte vector instead of |
Jay Srinivasan | e56c873 | 2012-10-17 12:19:27 -0700 | [diff] [blame] | 83 | // a string. This method works fine even if |raw_in| has any newlines. |
| 84 | // Any existing contents of |out| will be erased. |
| 85 | static bool Base64Decode(const std::string& raw_in, std::vector<char>* out); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 86 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 87 | private: |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 88 | // If non-empty, the final base64 encoded hash and the raw hash. Will only be |
| 89 | // set to non-empty when Finalize is called. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 | std::string hash_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 91 | std::vector<char> raw_hash_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 92 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 93 | // Init success |
| 94 | bool valid_; |
| 95 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 96 | // The hash state used by OpenSSL |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 97 | SHA256_CTX ctx_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 98 | DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator); |
| 99 | }; |
| 100 | |
| 101 | } // namespace chromeos_update_engine |
| 102 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 103 | #endif // UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_ |