blob: dc9fe4ef02099cf16a9b3282b0a886d37d4237c7 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// 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.comc98a7ed2009-12-04 18:54:03 +00005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +00009#include <vector>
Darin Petkov36a58222010-10-07 22:00:09 -070010
rspangler@google.com49fdf182009-10-10 00:57:34 +000011#include <openssl/sha.h>
Darin Petkov36a58222010-10-07 22:00:09 -070012#include <base/basictypes.h>
13#include <base/logging.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000014
Darin Petkov73058b42010-10-06 16:32:19 -070015// Omaha uses base64 encoded SHA-256 as the hash. This class provides a simple
rspangler@google.com49fdf182009-10-10 00:57:34 +000016// wrapper around OpenSSL providing such a formatted hash of data passed in.
Darin Petkov73058b42010-10-06 16:32:19 -070017// 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.com49fdf182009-10-10 00:57:34 +000020
21namespace chromeos_update_engine {
22
23class OmahaHashCalculator {
24 public:
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070025 OmahaHashCalculator();
rspangler@google.com49fdf182009-10-10 00:57:34 +000026
27 // Update is called with all of the data that should be hashed in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070028 // Update will read |length| bytes of |data|.
29 // Returns true on success.
30 bool Update(const char* data, size_t length);
rspangler@google.com49fdf182009-10-10 00:57:34 +000031
Darin Petkov36a58222010-10-07 22:00:09 -070032 // 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.com49fdf182009-10-10 00:57:34 +000037 // 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 Reyes932bc4c2010-08-23 18:14:09 -070040 // Returns true on success.
41 bool Finalize();
rspangler@google.com49fdf182009-10-10 00:57:34 +000042
43 // Gets the hash. Finalize() must have been called.
44 const std::string& hash() const {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070045 DCHECK(!hash_.empty()) << "Call Finalize() first";
rspangler@google.com49fdf182009-10-10 00:57:34 +000046 return hash_;
47 }
48
Darin Petkovd7061ab2010-10-06 14:37:09 -070049 const std::vector<char>& raw_hash() const {
50 DCHECK(!raw_hash_.empty()) << "Call Finalize() first";
51 return raw_hash_;
52 }
53
Darin Petkov73058b42010-10-06 16:32:19 -070054 // 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 Reyes932bc4c2010-08-23 18:14:09 -070063 static bool RawHashOfData(const std::vector<char>& data,
64 std::vector<char>* out_hash);
Darin Petkov698d0412010-10-13 10:59:44 -070065 static off_t RawHashOfFile(const std::string& name, off_t length,
66 std::vector<char>* out_hash);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070067
rspangler@google.com49fdf182009-10-10 00:57:34 +000068 // 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
73 private:
Darin Petkovd7061ab2010-10-06 14:37:09 -070074 // If non-empty, the final base64 encoded hash and the raw hash. Will only be
75 // set to non-empty when Finalize is called.
rspangler@google.com49fdf182009-10-10 00:57:34 +000076 std::string hash_;
Darin Petkovd7061ab2010-10-06 14:37:09 -070077 std::vector<char> raw_hash_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000078
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070079 // Init success
80 bool valid_;
81
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 // The hash state used by OpenSSL
Darin Petkovd22cb292010-09-29 10:02:29 -070083 SHA256_CTX ctx_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000084 DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator);
85};
86
87} // namespace chromeos_update_engine
88
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070089#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H__