blob: a32ac184f1b021cfd385aa2efb4cc001396d20d7 [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
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_
6#define UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
Ben Chan05735a12014-09-03 07:48:22 -07008#include <openssl/sha.h>
Han Shen2643cb72012-06-26 14:45:33 -07009#include <unistd.h>
Ben Chan05735a12014-09-03 07:48:22 -070010
11#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000012#include <vector>
Darin Petkov36a58222010-10-07 22:00:09 -070013
Darin Petkov36a58222010-10-07 22:00:09 -070014#include <base/logging.h>
Ben Chan05735a12014-09-03 07:48:22 -070015#include <base/macros.h>
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080016#include <chromeos/secure_blob.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000017
Darin Petkov73058b42010-10-06 16:32:19 -070018// Omaha uses base64 encoded SHA-256 as the hash. This class provides a simple
rspangler@google.com49fdf182009-10-10 00:57:34 +000019// wrapper around OpenSSL providing such a formatted hash of data passed in.
Darin Petkov73058b42010-10-06 16:32:19 -070020// The methods of this class must be called in a very specific order: First the
21// ctor (of course), then 0 or more calls to Update(), then Finalize(), then 0
22// or more calls to hash().
rspangler@google.com49fdf182009-10-10 00:57:34 +000023
24namespace chromeos_update_engine {
25
26class OmahaHashCalculator {
27 public:
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070028 OmahaHashCalculator();
rspangler@google.com49fdf182009-10-10 00:57:34 +000029
30 // Update is called with all of the data that should be hashed in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070031 // Update will read |length| bytes of |data|.
32 // Returns true on success.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080033 bool Update(const void* data, size_t length);
rspangler@google.com49fdf182009-10-10 00:57:34 +000034
Darin Petkov36a58222010-10-07 22:00:09 -070035 // Updates the hash with up to |length| bytes of data from |file|. If |length|
36 // is negative, reads in and updates with the whole file. Returns the number
37 // of bytes that the hash was updated with, or -1 on error.
38 off_t UpdateFile(const std::string& name, off_t length);
39
rspangler@google.com49fdf182009-10-10 00:57:34 +000040 // Call Finalize() when all data has been passed in. This method tells
41 // OpenSSl that no more data will come in and base64 encodes the resulting
42 // hash.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070043 // Returns true on success.
44 bool Finalize();
rspangler@google.com49fdf182009-10-10 00:57:34 +000045
46 // Gets the hash. Finalize() must have been called.
47 const std::string& hash() const {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070048 DCHECK(!hash_.empty()) << "Call Finalize() first";
rspangler@google.com49fdf182009-10-10 00:57:34 +000049 return hash_;
50 }
51
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080052 const chromeos::Blob& raw_hash() const {
Darin Petkovd7061ab2010-10-06 14:37:09 -070053 DCHECK(!raw_hash_.empty()) << "Call Finalize() first";
54 return raw_hash_;
55 }
56
Darin Petkov73058b42010-10-06 16:32:19 -070057 // Gets the current hash context. Note that the string will contain binary
58 // data (including \0 characters).
59 std::string GetContext() const;
60
61 // Sets the current hash context. |context| must the string returned by a
62 // previous OmahaHashCalculator::GetContext method call. Returns true on
63 // success, and false otherwise.
64 bool SetContext(const std::string& context);
65
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080066 static bool RawHashOfBytes(const void* data,
Darin Petkovadb3cef2011-01-13 16:16:08 -080067 size_t length,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080068 chromeos::Blob* out_hash);
69 static bool RawHashOfData(const chromeos::Blob& data,
70 chromeos::Blob* out_hash);
Darin Petkov698d0412010-10-13 10:59:44 -070071 static off_t RawHashOfFile(const std::string& name, off_t length,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080072 chromeos::Blob* out_hash);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070073
rspangler@google.com49fdf182009-10-10 00:57:34 +000074 // Used by tests
75 static std::string OmahaHashOfBytes(const void* data, size_t length);
76 static std::string OmahaHashOfString(const std::string& str);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080077 static std::string OmahaHashOfData(const chromeos::Blob& data);
rspangler@google.com49fdf182009-10-10 00:57:34 +000078
79 private:
Darin Petkovd7061ab2010-10-06 14:37:09 -070080 // If non-empty, the final base64 encoded hash and the raw hash. Will only be
81 // set to non-empty when Finalize is called.
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 std::string hash_;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080083 chromeos::Blob raw_hash_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000084
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070085 // Init success
86 bool valid_;
87
rspangler@google.com49fdf182009-10-10 00:57:34 +000088 // The hash state used by OpenSSL
Darin Petkovd22cb292010-09-29 10:02:29 -070089 SHA256_CTX ctx_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000090 DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator);
91};
92
93} // namespace chromeos_update_engine
94
Gilad Arnoldcf175a02014-07-10 16:48:47 -070095#endif // UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_