summaryrefslogtreecommitdiff
path: root/fuzz/mem_hash_tree.h
blob: 34f4a40f9a32d373d67cea09d3904b6b7fba61a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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 <unistd.h>

#include <array>
#include <cstdint>
#include <unordered_map>
#include <vector>

#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<uint64_t, uint8_t> MaskedLabel;

namespace std {
template <>
struct hash<MaskedLabel> {
  size_t operator()(const MaskedLabel& lbl) const {
    static const auto hash_first = hash<uint64_t>();
    static const auto hash_second = hash<uint8_t>();
    return hash_first(lbl.first) * hash_second(lbl.second);
  }
};
}  // namespace std

class MemHashTree {
 public:
  MemHashTree();

  bool GetLeaf(uint64_t label, fuzz::span<uint8_t> 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<uint8_t> 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<const uint8_t> 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<MaskedLabel, std::array<uint8_t, 32>> hash_tree_;
  std::vector<std::array<uint8_t, 32>> empty_node_hashes_;
};

#endif  // __FUZZ_MEM_HASH_TREE_H