// Copyright 2018 The Chromium OS Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef __FUZZ_MEM_HASH_TREE_H #define __FUZZ_MEM_HASH_TREE_H #include #include #include #include #include #define HIDE_EC_STDLIB #include "board/host/dcrypto.h" #include "fuzz/span.h" // MaskedLabel.first is the label path, this is shifted to the right by the // (bits_per_level * level) // MaskedLabel.second is the level of the label (0 for leaf, height for root) typedef std::pair MaskedLabel; namespace std { template <> struct hash { size_t operator()(const MaskedLabel& lbl) const { static const auto hash_first = hash(); static const auto hash_second = hash(); return hash_first(lbl.first) * hash_second(lbl.second); } }; } // namespace std class MemHashTree { public: MemHashTree(); bool GetLeaf(uint64_t label, fuzz::span leaf_hash) const; // Writes the result to |path_hashes| and returns the size in bytes of the // returned path for use in serializers that report how much buffer was used. size_t GetPath(uint64_t label, fuzz::span path_hashes) const; // Updates the hashes in the path of the specified leaf. If |path_hash| is // empty, the entry in hash_tree_ is deleted representing an empty leaf. void UpdatePath(uint64_t label, fuzz::span path_hash); void Reset(); void Reset(uint8_t bits_per_level, uint8_t height); private: uint8_t bits_per_level_; uint8_t height_; // Only contains hashes for non empty paths in the tree. std::unordered_map> hash_tree_; std::vector> empty_node_hashes_; }; #endif // __FUZZ_MEM_HASH_TREE_H