Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
crypto_hash.h
1 /*
2  * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <hicn/transport/errors/runtime_exception.h>
19 #include <hicn/transport/utils/membuf.h>
20 
21 #include <iomanip>
22 
23 extern "C" {
24 #include <openssl/evp.h>
25 }
26 
27 namespace transport {
28 namespace auth {
29 
30 typedef const EVP_MD *(*CryptoHashEVP)(void);
31 
32 enum class CryptoHashType : uint8_t {
33  UNKNOWN,
34  SHA256,
35  SHA512,
36  BLAKE2B512,
37  BLAKE2S256,
38 };
39 
40 class CryptoHash {
41  public:
42  // Constructors
43  CryptoHash();
44  CryptoHash(const CryptoHash &other);
45  CryptoHash(CryptoHash &&other);
46  CryptoHash(CryptoHashType hash_type);
47  CryptoHash(const uint8_t *hash, std::size_t size, CryptoHashType hash_type);
48  CryptoHash(const std::vector<uint8_t> &hash, CryptoHashType hash_type);
49 
50  // Destructor
51  ~CryptoHash() = default;
52 
53  // Operators
54  CryptoHash &operator=(const CryptoHash &other);
55  bool operator==(const CryptoHash &other) const;
56 
57  // Compute the hash of given buffer
58  void computeDigest(const uint8_t *buffer, std::size_t len);
59  void computeDigest(const std::vector<uint8_t> &buffer);
60 
61  // Compute the hash of given membuf
62  void computeDigest(const utils::MemBuf *buffer);
63 
64  // Return the computed hash
65  std::vector<uint8_t> getDigest() const;
66 
67  // Return the computed hash as a string
68  std::string getStringDigest() const;
69 
70  // Return hash type
71  CryptoHashType getType() const;
72 
73  // Return hash size
74  std::size_t getSize() const;
75 
76  // Change hash type
77  void setType(CryptoHashType hash_type);
78 
79  // Print hash to stdout
80  void display();
81 
82  // Reset hash
83  void reset();
84 
85  // Return OpenSSL EVP function associated to a given hash type
86  static CryptoHashEVP getEVP(CryptoHashType hash_type);
87 
88  // Return hash size
89  static std::size_t getSize(CryptoHashType hash_type);
90 
91  // Compare two raw buffers
92  static bool compareDigest(const uint8_t *h1, const uint8_t *h2,
93  CryptoHashType hash_type);
94 
95  private:
96  CryptoHashType digest_type_;
97  std::vector<uint8_t> digest_;
98  std::size_t digest_size_;
99 };
100 
101 } // namespace auth
102 } // namespace transport
transport::auth::CryptoHash
Definition: crypto_hash.h:40
transport
Definition: forwarder_config.h:32
utils::MemBuf
Definition: membuf.h:45