aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/prov/bearssl/bearssl_hash.cpp
blob: 5649356244dd558f71968692eb0496c1bc880ce3 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* BearSSL Hash Functions
* (C) 1999-2007,2015 Jack Lloyd
* (C) 2017 Patrick Wildt
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/hash.h>
#include <botan/internal/bearssl.h>
#include <unordered_map>

extern "C" {
  #include <bearssl_hash.h>
}

namespace Botan {

namespace {

class BearSSL_HashFunction final : public HashFunction
   {
   public:
      void clear() override
         {
         m_ctx.vtable->init(&m_ctx.vtable);
         }

      std::string provider() const override { return "bearssl"; }
      std::string name() const override { return m_name; }

      HashFunction* clone() const override
         {
         return new BearSSL_HashFunction(m_ctx.vtable, m_name);
         }

      std::unique_ptr<HashFunction> copy_state() const override
         {
         std::unique_ptr<BearSSL_HashFunction> copy(new BearSSL_HashFunction(m_ctx.vtable, m_name));
         memcpy(&copy->m_ctx, &m_ctx, sizeof(m_ctx));
         return std::move(copy);
         }

      size_t output_length() const override
         {
         return (m_ctx.vtable->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
         }

      size_t hash_block_size() const override
         {
         return 1 << ((m_ctx.vtable->desc >> BR_HASHDESC_LBLEN_OFF) & BR_HASHDESC_LBLEN_MASK);
         }

      BearSSL_HashFunction(const br_hash_class *hash, const std::string name)
         {
         m_name = name;
         hash->init(&m_ctx.vtable);
         }

      ~BearSSL_HashFunction()
         {
         }

   private:
      void add_data(const uint8_t input[], size_t length) override
         {
         m_ctx.vtable->update(&m_ctx.vtable, input, length);
         }

      void final_result(uint8_t output[]) override
         {
         m_ctx.vtable->out(&m_ctx.vtable, output);
         m_ctx.vtable->init(&m_ctx.vtable);
         }

      std::string m_name;
      br_hash_compat_context m_ctx;
   };

}

std::unique_ptr<HashFunction>
make_bearssl_hash(const std::string& name)
   {
#define MAKE_BEARSSL_HASH(vtable) \
   std::unique_ptr<HashFunction>(new BearSSL_HashFunction(vtable, name))

#if defined(BOTAN_HAS_SHA2_32)
   if(name == "SHA-224")
      return MAKE_BEARSSL_HASH(&br_sha224_vtable);
   if(name == "SHA-256")
      return MAKE_BEARSSL_HASH(&br_sha256_vtable);
#endif

#if defined(BOTAN_HAS_SHA2_64)
   if(name == "SHA-384")
      return MAKE_BEARSSL_HASH(&br_sha384_vtable);
   if(name == "SHA-512")
      return MAKE_BEARSSL_HASH(&br_sha512_vtable);
#endif

#if defined(BOTAN_HAS_SHA1)
   if(name == "SHA-160" || name == "SHA-1" || name == "SHA1")
      return MAKE_BEARSSL_HASH(&br_sha1_vtable);
#endif

#if defined(BOTAN_HAS_MD5)
   if(name == "MD5")
      return MAKE_BEARSSL_HASH(&br_md5_vtable);
#endif

#if defined(BOTAN_HAS_PARALLEL_HASH)
   if(name == "Parallel(MD5,SHA-160)")
      return MAKE_BEARSSL_HASH(&br_md5sha1_vtable);
#endif

   return nullptr;
   }

}