blob: 44340b0436eaa87725275cc68e1b3833d8351dd3 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com49fdf182009-10-10 00:57:34 +000016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_
18#define UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
Ben Chan05735a12014-09-03 07:48:22 -070020#include <openssl/sha.h>
Han Shen2643cb72012-06-26 14:45:33 -070021#include <unistd.h>
Ben Chan05735a12014-09-03 07:48:22 -070022
23#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000024#include <vector>
Darin Petkov36a58222010-10-07 22:00:09 -070025
Darin Petkov36a58222010-10-07 22:00:09 -070026#include <base/logging.h>
Ben Chan05735a12014-09-03 07:48:22 -070027#include <base/macros.h>
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080028#include <chromeos/secure_blob.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000029
Darin Petkov73058b42010-10-06 16:32:19 -070030// Omaha uses base64 encoded SHA-256 as the hash. This class provides a simple
rspangler@google.com49fdf182009-10-10 00:57:34 +000031// wrapper around OpenSSL providing such a formatted hash of data passed in.
Darin Petkov73058b42010-10-06 16:32:19 -070032// 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.com49fdf182009-10-10 00:57:34 +000035
36namespace chromeos_update_engine {
37
38class OmahaHashCalculator {
39 public:
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070040 OmahaHashCalculator();
rspangler@google.com49fdf182009-10-10 00:57:34 +000041
42 // Update is called with all of the data that should be hashed in order.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070043 // Update will read |length| bytes of |data|.
44 // Returns true on success.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080045 bool Update(const void* data, size_t length);
rspangler@google.com49fdf182009-10-10 00:57:34 +000046
Darin Petkov36a58222010-10-07 22:00:09 -070047 // 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.com49fdf182009-10-10 00:57:34 +000052 // 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 Reyes932bc4c2010-08-23 18:14:09 -070055 // Returns true on success.
56 bool Finalize();
rspangler@google.com49fdf182009-10-10 00:57:34 +000057
58 // Gets the hash. Finalize() must have been called.
59 const std::string& hash() const {
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070060 DCHECK(!hash_.empty()) << "Call Finalize() first";
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 return hash_;
62 }
63
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080064 const chromeos::Blob& raw_hash() const {
Darin Petkovd7061ab2010-10-06 14:37:09 -070065 DCHECK(!raw_hash_.empty()) << "Call Finalize() first";
66 return raw_hash_;
67 }
68
Darin Petkov73058b42010-10-06 16:32:19 -070069 // 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 Vakulenkof68bbbc2015-02-09 12:53:18 -080078 static bool RawHashOfBytes(const void* data,
Darin Petkovadb3cef2011-01-13 16:16:08 -080079 size_t length,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080080 chromeos::Blob* out_hash);
81 static bool RawHashOfData(const chromeos::Blob& data,
82 chromeos::Blob* out_hash);
Darin Petkov698d0412010-10-13 10:59:44 -070083 static off_t RawHashOfFile(const std::string& name, off_t length,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080084 chromeos::Blob* out_hash);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070085
rspangler@google.com49fdf182009-10-10 00:57:34 +000086 // Used by tests
87 static std::string OmahaHashOfBytes(const void* data, size_t length);
88 static std::string OmahaHashOfString(const std::string& str);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080089 static std::string OmahaHashOfData(const chromeos::Blob& data);
rspangler@google.com49fdf182009-10-10 00:57:34 +000090
91 private:
Darin Petkovd7061ab2010-10-06 14:37:09 -070092 // 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.com49fdf182009-10-10 00:57:34 +000094 std::string hash_;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080095 chromeos::Blob raw_hash_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000096
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070097 // Init success
98 bool valid_;
99
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100 // The hash state used by OpenSSL
Darin Petkovd22cb292010-09-29 10:02:29 -0700101 SHA256_CTX ctx_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102 DISALLOW_COPY_AND_ASSIGN(OmahaHashCalculator);
103};
104
105} // namespace chromeos_update_engine
106
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700107#endif // UPDATE_ENGINE_OMAHA_HASH_CALCULATOR_H_