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 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 8 | #include <string> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 9 | #include <vector> |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 10 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 11 | #include <openssl/sha.h> |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 12 | #include <base/basictypes.h> |
| 13 | #include <base/logging.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 14 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 15 | // 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] | 16 | // wrapper around OpenSSL providing such a formatted hash of data passed in. |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 17 | // The methods of this class must be called in a very specific order: First the |
| 18 | // ctor (of course), then 0 or more calls to Update(), then Finalize(), then 0 |
| 19 | // or more calls to hash(). |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
| 23 | class OmahaHashCalculator { |
| 24 | public: |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 25 | OmahaHashCalculator(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 26 | |
| 27 | // 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] | 28 | // Update will read |length| bytes of |data|. |
| 29 | // Returns true on success. |
| 30 | bool Update(const char* data, size_t length); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 31 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 32 | // Updates the hash with up to |length| bytes of data from |file|. If |length| |
| 33 | // is negative, reads in and updates with the whole file. Returns the number |
| 34 | // of bytes that the hash was updated with, or -1 on error. |
| 35 | off_t UpdateFile(const std::string& name, off_t length); |
| 36 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 37 | // Call Finalize() when all data has been passed in. This method tells |
| 38 | // OpenSSl that no more data will come in and base64 encodes the resulting |
| 39 | // hash. |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 40 | // Returns true on success. |
| 41 | bool Finalize(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 42 | |
| 43 | // Gets the hash. Finalize() must have been called. |
| 44 | const std::string& hash() const { |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 45 | DCHECK(!hash_.empty()) << "Call Finalize() first"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 46 | return hash_; |
| 47 | } |
| 48 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 49 | const std::vector<char>& raw_hash() const { |
| 50 | DCHECK(!raw_hash_.empty()) << "Call Finalize() first"; |
| 51 | return raw_hash_; |
| 52 | } |
| 53 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 54 | // Gets the current hash context. Note that the string will contain binary |
| 55 | // data (including \0 characters). |
| 56 | std::string GetContext() const; |
| 57 | |
| 58 | // Sets the current hash context. |context| must the string returned by a |
| 59 | // previous OmahaHashCalculator::GetContext method call. Returns true on |
| 60 | // success, and false otherwise. |
| 61 | bool SetContext(const std::string& context); |
| 62 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 63 | static bool RawHashOfData(const std::vector<char>& data, |
| 64 | std::vector<char>* out_hash); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 65 | static off_t RawHashOfFile(const std::string& name, off_t length, |
| 66 | std::vector<char>* out_hash); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 67 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 68 | // Used by tests |
| 69 | static std::string OmahaHashOfBytes(const void* data, size_t length); |
| 70 | static std::string OmahaHashOfString(const std::string& str); |
| 71 | static std::string OmahaHashOfData(const std::vector<char>& data); |
| 72 | |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame^] | 73 | static bool Base64Encode(const void* data, size_t size, std::string* out); |
| 74 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | private: |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 76 | // If non-empty, the final base64 encoded hash and the raw hash. Will only be |
| 77 | // set to non-empty when Finalize is called. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | std::string hash_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 79 | std::vector<char> raw_hash_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 80 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 81 | // Init success |
| 82 | bool valid_; |
| 83 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | // The hash state used by OpenSSL |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 85 | SHA256_CTX ctx_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 86 | DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator); |
| 87 | }; |
| 88 | |
| 89 | } // namespace chromeos_update_engine |
| 90 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 91 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__ |