Botan  2.4.0
Crypto and TLS for C++11
bearssl_hash.cpp
Go to the documentation of this file.
1 /*
2 * BearSSL Hash Functions
3 * (C) 1999-2007,2015 Jack Lloyd
4 * (C) 2017 Patrick Wildt
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/hash.h>
10 #include <botan/internal/bearssl.h>
11 #include <unordered_map>
12 
13 extern "C" {
14  #include <bearssl_hash.h>
15 }
16 
17 namespace Botan {
18 
19 namespace {
20 
21 class BearSSL_HashFunction final : public HashFunction
22  {
23  public:
24  void clear() override
25  {
26  m_ctx.vtable->init(&m_ctx.vtable);
27  }
28 
29  std::string provider() const override { return "bearssl"; }
30  std::string name() const override { return m_name; }
31 
32  HashFunction* clone() const override
33  {
34  return new BearSSL_HashFunction(m_ctx.vtable, m_name);
35  }
36 
37  std::unique_ptr<HashFunction> copy_state() const override
38  {
39  std::unique_ptr<BearSSL_HashFunction> copy(new BearSSL_HashFunction(m_ctx.vtable, m_name));
40  memcpy(&copy->m_ctx, &m_ctx, sizeof(m_ctx));
41  return std::move(copy);
42  }
43 
44  size_t output_length() const override
45  {
46  return (m_ctx.vtable->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
47  }
48 
49  size_t hash_block_size() const override
50  {
51  return 1 << ((m_ctx.vtable->desc >> BR_HASHDESC_LBLEN_OFF) & BR_HASHDESC_LBLEN_MASK);
52  }
53 
54  BearSSL_HashFunction(const br_hash_class *hash, const std::string name)
55  {
56  m_name = name;
57  hash->init(&m_ctx.vtable);
58  }
59 
60  ~BearSSL_HashFunction()
61  {
62  }
63 
64  private:
65  void add_data(const uint8_t input[], size_t length) override
66  {
67  m_ctx.vtable->update(&m_ctx.vtable, input, length);
68  }
69 
70  void final_result(uint8_t output[]) override
71  {
72  m_ctx.vtable->out(&m_ctx.vtable, output);
73  m_ctx.vtable->init(&m_ctx.vtable);
74  }
75 
76  std::string m_name;
77  br_hash_compat_context m_ctx;
78  };
79 
80 }
81 
82 std::unique_ptr<HashFunction>
83 make_bearssl_hash(const std::string& name)
84  {
85 #define MAKE_BEARSSL_HASH(vtable) \
86  std::unique_ptr<HashFunction>(new BearSSL_HashFunction(vtable, name))
87 
88 #if defined(BOTAN_HAS_SHA2_32)
89  if(name == "SHA-224")
90  return MAKE_BEARSSL_HASH(&br_sha224_vtable);
91  if(name == "SHA-256")
92  return MAKE_BEARSSL_HASH(&br_sha256_vtable);
93 #endif
94 
95 #if defined(BOTAN_HAS_SHA2_64)
96  if(name == "SHA-384")
97  return MAKE_BEARSSL_HASH(&br_sha384_vtable);
98  if(name == "SHA-512")
99  return MAKE_BEARSSL_HASH(&br_sha512_vtable);
100 #endif
101 
102 #if defined(BOTAN_HAS_SHA1)
103  if(name == "SHA-160" || name == "SHA-1" || name == "SHA1")
104  return MAKE_BEARSSL_HASH(&br_sha1_vtable);
105 #endif
106 
107 #if defined(BOTAN_HAS_MD5)
108  if(name == "MD5")
109  return MAKE_BEARSSL_HASH(&br_md5_vtable);
110 #endif
111 
112 #if defined(BOTAN_HAS_PARALLEL_HASH)
113  if(name == "Parallel(MD5,SHA-160)")
114  return MAKE_BEARSSL_HASH(&br_md5sha1_vtable);
115 #endif
116 
117  return nullptr;
118  }
119 
120 }
std::unique_ptr< HashFunction > make_bearssl_hash(const std::string &name)
Definition: alg_id.cpp:13
#define MAKE_BEARSSL_HASH(vtable)
MechanismType hash