blob: 06d2cfb910a0f22bd2a403259917442ea1237b20 [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
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_HASH_CALCULATOR_H_
18#define UPDATE_ENGINE_COMMON_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 Vakulenko3f39d5c2015-10-13 09:27:13 -070028#include <brillo/secure_blob.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000029
Sen Jiang2703ef42017-03-16 13:36:21 -070030// This class provides a simple wrapper around OpenSSL providing a hash of data
31// 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
Sen Jiang2703ef42017-03-16 13:36:21 -070034// or more calls to raw_hash().
rspangler@google.com49fdf182009-10-10 00:57:34 +000035
36namespace chromeos_update_engine {
37
Alex Deymo39910dc2015-11-09 17:04:30 -080038class HashCalculator {
rspangler@google.com49fdf182009-10-10 00:57:34 +000039 public:
Alex Deymo39910dc2015-11-09 17:04:30 -080040 HashCalculator();
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
Sen Jiang2703ef42017-03-16 13:36:21 -070053 // OpenSSL that no more data will come in.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070054 // Returns true on success.
55 bool Finalize();
rspangler@google.com49fdf182009-10-10 00:57:34 +000056
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070057 const brillo::Blob& raw_hash() const {
Darin Petkovd7061ab2010-10-06 14:37:09 -070058 DCHECK(!raw_hash_.empty()) << "Call Finalize() first";
59 return raw_hash_;
60 }
61
Darin Petkov73058b42010-10-06 16:32:19 -070062 // Gets the current hash context. Note that the string will contain binary
63 // data (including \0 characters).
64 std::string GetContext() const;
65
66 // Sets the current hash context. |context| must the string returned by a
Alex Deymo39910dc2015-11-09 17:04:30 -080067 // previous HashCalculator::GetContext method call. Returns true on success,
68 // and false otherwise.
Darin Petkov73058b42010-10-06 16:32:19 -070069 bool SetContext(const std::string& context);
70
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080071 static bool RawHashOfBytes(const void* data,
Darin Petkovadb3cef2011-01-13 16:16:08 -080072 size_t length,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070073 brillo::Blob* out_hash);
74 static bool RawHashOfData(const brillo::Blob& data,
75 brillo::Blob* out_hash);
Darin Petkov698d0412010-10-13 10:59:44 -070076 static off_t RawHashOfFile(const std::string& name, off_t length,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070077 brillo::Blob* out_hash);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070078
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 private:
Sen Jiang2703ef42017-03-16 13:36:21 -070080 // If non-empty, the final raw hash. Will only be set to non-empty when
81 // Finalize is called.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070082 brillo::Blob raw_hash_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000083
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070084 // Init success
85 bool valid_;
86
rspangler@google.com49fdf182009-10-10 00:57:34 +000087 // The hash state used by OpenSSL
Darin Petkovd22cb292010-09-29 10:02:29 -070088 SHA256_CTX ctx_;
Alex Deymo39910dc2015-11-09 17:04:30 -080089 DISALLOW_COPY_AND_ASSIGN(HashCalculator);
rspangler@google.com49fdf182009-10-10 00:57:34 +000090};
91
92} // namespace chromeos_update_engine
93
Alex Deymo39910dc2015-11-09 17:04:30 -080094#endif // UPDATE_ENGINE_COMMON_HASH_CALCULATOR_H_