xref: /aosp_15_r20/system/update_engine/common/hash_calculator.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2009 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_COMMON_HASH_CALCULATOR_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_COMMON_HASH_CALCULATOR_H_
19*5a923131SAndroid Build Coastguard Worker 
20*5a923131SAndroid Build Coastguard Worker #include <openssl/sha.h>
21*5a923131SAndroid Build Coastguard Worker #include <unistd.h>
22*5a923131SAndroid Build Coastguard Worker 
23*5a923131SAndroid Build Coastguard Worker #include <string>
24*5a923131SAndroid Build Coastguard Worker #include <vector>
25*5a923131SAndroid Build Coastguard Worker 
26*5a923131SAndroid Build Coastguard Worker #include <base/logging.h>
27*5a923131SAndroid Build Coastguard Worker #include <android-base/macros.h>
28*5a923131SAndroid Build Coastguard Worker #include <brillo/secure_blob.h>
29*5a923131SAndroid Build Coastguard Worker 
30*5a923131SAndroid Build Coastguard Worker // This class provides a simple wrapper around OpenSSL providing a hash of data
31*5a923131SAndroid Build Coastguard Worker // passed in.
32*5a923131SAndroid Build Coastguard Worker // The methods of this class must be called in a very specific order: First the
33*5a923131SAndroid Build Coastguard Worker // ctor (of course), then 0 or more calls to Update(), then Finalize(), then 0
34*5a923131SAndroid Build Coastguard Worker // or more calls to raw_hash().
35*5a923131SAndroid Build Coastguard Worker 
36*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
37*5a923131SAndroid Build Coastguard Worker 
38*5a923131SAndroid Build Coastguard Worker class HashCalculator {
39*5a923131SAndroid Build Coastguard Worker  public:
40*5a923131SAndroid Build Coastguard Worker   HashCalculator();
41*5a923131SAndroid Build Coastguard Worker 
42*5a923131SAndroid Build Coastguard Worker   // Update is called with all of the data that should be hashed in order.
43*5a923131SAndroid Build Coastguard Worker   // Update will read |length| bytes of |data|.
44*5a923131SAndroid Build Coastguard Worker   // Returns true on success.
45*5a923131SAndroid Build Coastguard Worker   bool Update(const void* data, size_t length);
46*5a923131SAndroid Build Coastguard Worker 
47*5a923131SAndroid Build Coastguard Worker   // Updates the hash with up to |length| bytes of data from |file|. If |length|
48*5a923131SAndroid Build Coastguard Worker   // is negative, reads in and updates with the whole file. Returns the number
49*5a923131SAndroid Build Coastguard Worker   // of bytes that the hash was updated with, or -1 on error.
50*5a923131SAndroid Build Coastguard Worker   off_t UpdateFile(const std::string& name, off_t length);
51*5a923131SAndroid Build Coastguard Worker 
52*5a923131SAndroid Build Coastguard Worker   // Call Finalize() when all data has been passed in. This method tells
53*5a923131SAndroid Build Coastguard Worker   // OpenSSL that no more data will come in.
54*5a923131SAndroid Build Coastguard Worker   // Returns true on success.
55*5a923131SAndroid Build Coastguard Worker   bool Finalize();
56*5a923131SAndroid Build Coastguard Worker 
raw_hash()57*5a923131SAndroid Build Coastguard Worker   const brillo::Blob& raw_hash() const {
58*5a923131SAndroid Build Coastguard Worker     DCHECK(!raw_hash_.empty()) << "Call Finalize() first";
59*5a923131SAndroid Build Coastguard Worker     return raw_hash_;
60*5a923131SAndroid Build Coastguard Worker   }
61*5a923131SAndroid Build Coastguard Worker 
62*5a923131SAndroid Build Coastguard Worker   // Gets the current hash context. Note that the string will contain binary
63*5a923131SAndroid Build Coastguard Worker   // data (including \0 characters).
64*5a923131SAndroid Build Coastguard Worker   std::string GetContext() const;
65*5a923131SAndroid Build Coastguard Worker 
66*5a923131SAndroid Build Coastguard Worker   // Sets the current hash context. |context| must the string returned by a
67*5a923131SAndroid Build Coastguard Worker   // previous HashCalculator::GetContext method call. Returns true on success,
68*5a923131SAndroid Build Coastguard Worker   // and false otherwise.
69*5a923131SAndroid Build Coastguard Worker   bool SetContext(const std::string& context);
70*5a923131SAndroid Build Coastguard Worker 
71*5a923131SAndroid Build Coastguard Worker   static bool RawHashOfBytes(const void* data,
72*5a923131SAndroid Build Coastguard Worker                              size_t length,
73*5a923131SAndroid Build Coastguard Worker                              brillo::Blob* out_hash);
74*5a923131SAndroid Build Coastguard Worker   static bool RawHashOfData(const brillo::Blob& data, brillo::Blob* out_hash);
75*5a923131SAndroid Build Coastguard Worker   static off_t RawHashOfFile(const std::string& name,
76*5a923131SAndroid Build Coastguard Worker                              off_t length,
77*5a923131SAndroid Build Coastguard Worker                              brillo::Blob* out_hash);
78*5a923131SAndroid Build Coastguard Worker   static bool RawHashOfFile(const std::string& name, brillo::Blob* out_hash);
79*5a923131SAndroid Build Coastguard Worker   static std::string SHA256Digest(std::string_view blob);
80*5a923131SAndroid Build Coastguard Worker 
81*5a923131SAndroid Build Coastguard Worker   static std::string SHA256Digest(std::vector<unsigned char> blob);
82*5a923131SAndroid Build Coastguard Worker   static std::string SHA256Digest(std::vector<char> blob);
83*5a923131SAndroid Build Coastguard Worker 
84*5a923131SAndroid Build Coastguard Worker  private:
85*5a923131SAndroid Build Coastguard Worker   // If non-empty, the final raw hash. Will only be set to non-empty when
86*5a923131SAndroid Build Coastguard Worker   // Finalize is called.
87*5a923131SAndroid Build Coastguard Worker   brillo::Blob raw_hash_;
88*5a923131SAndroid Build Coastguard Worker 
89*5a923131SAndroid Build Coastguard Worker   // Init success
90*5a923131SAndroid Build Coastguard Worker   bool valid_;
91*5a923131SAndroid Build Coastguard Worker 
92*5a923131SAndroid Build Coastguard Worker   // The hash state used by OpenSSL
93*5a923131SAndroid Build Coastguard Worker   SHA256_CTX ctx_{};
94*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(HashCalculator);
95*5a923131SAndroid Build Coastguard Worker };
96*5a923131SAndroid Build Coastguard Worker 
97*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
98*5a923131SAndroid Build Coastguard Worker 
99*5a923131SAndroid Build Coastguard Worker #endif  // UPDATE_ENGINE_COMMON_HASH_CALCULATOR_H_
100