digestpp 1.0
C++11 header-only message digest library
Loading...
Searching...
No Matches
kmac_provider.hpp
Go to the documentation of this file.
1/*
2This code is written by kerukuro and released into public domain.
3*/
4
5#ifndef DIGESTPP_PROVIDERS_KMAC_HPP
6#define DIGESTPP_PROVIDERS_KMAC_HPP
7
8#include "shake_provider.hpp"
9
10namespace digestpp
11{
12
13namespace detail
14{
15
16namespace kmac_functions
17{
18 template<size_t B>
19 inline std::string bytepad(const std::string& str)
20 {
21 const size_t pad = B == 128 ? 168 : 136;
22 unsigned char buf[32];
23 size_t len = shake_functions::left_encode(pad, buf);
24 std::string res(reinterpret_cast<const char*>(buf), len);
25 len = shake_functions::left_encode(str.length() * 8, buf);
26 res.append(reinterpret_cast<const char*>(buf), len);
27 res.append(str);
28 size_t delta = pad - res.size() % pad;
29 if (delta && delta != pad)
30 res.append(delta, '\x00');
31 return res;
32 }
33
34} // namespace kmac_functions
35
36
37template<size_t B, bool XOF, size_t HS = 0>
39{
40public:
41 static const bool is_xof = XOF;
42
43 template<bool xof=XOF, size_t hss=HS, typename std::enable_if<!xof && hss == 0>::type* = nullptr>
44 kmac_provider(size_t hashsize) : hs(hashsize)
45 {
46 static_assert(B == 128 || B == 256, "KMAC only supports 128 and 256 bits");
47 validate_hash_size(hashsize, SIZE_MAX);
48 set_key("");
49 }
50
51 template<bool xof=XOF, size_t hss=HS, typename std::enable_if<!xof && hss != 0>::type* = nullptr>
52 kmac_provider() : hs(hss)
53 {
54 static_assert(B == 128 || B == 256, "KMAC only supports 128 and 256 bits");
55 static_assert(hss % 8 == 0);
56 set_key("");
57 }
58
59 template<bool xof=XOF, typename std::enable_if<xof>::type* = nullptr>
60 kmac_provider() : hs(0)
61 {
62 static_assert(B == 128 || B == 256, "KMAC only supports 128 and 256 bits");
63 set_key("");
64 }
65
67 {
68 clear();
69 }
70
71 inline void set_key(const std::string& key)
72 {
74 }
75
76 inline void set_customization(const std::string& customization)
77 {
78 shake.set_customization(customization);
79 }
80
81 inline void init()
82 {
83 squeezing = false;
84 shake.set_function_name("KMAC");
85 shake.init();
86 update(reinterpret_cast<const unsigned char*>(K.data()), K.length());
87 }
88
89 inline void update(const unsigned char* data, size_t len)
90 {
91 shake.update(data, len);
92 }
93
94 inline void squeeze(unsigned char* hash, size_t hs)
95 {
96 if (!squeezing)
97 {
98 unsigned char buf[32];
99 size_t len = shake_functions::right_encode(!XOF ? hs * 8 : 0, buf, false);
100 update(buf, len);
101 squeezing = true;
102 }
103 shake.squeeze(hash, hs);
104 }
105
106 inline void final(unsigned char* hash)
107 {
108 squeeze(hash, hs / 8);
109 }
110
111 inline void clear()
112 {
113 shake.clear();
114 zero_memory(K);
115 set_key("");
116 }
117
118 inline size_t hash_size() const
119 {
120 return hs;
121 }
122
123private:
124 std::string K;
125 size_t hs;
126 bool squeezing;
128};
129
130} // namespace detail
131
132} // namespace digestpp
133
134#endif // DIGESTPP_PROVIDERS_KMAC_HPP
Definition kmac_provider.hpp:39
void clear()
Definition kmac_provider.hpp:111
size_t hash_size() const
Definition kmac_provider.hpp:118
void update(const unsigned char *data, size_t len)
Definition kmac_provider.hpp:89
void init()
Definition kmac_provider.hpp:81
kmac_provider()
Definition kmac_provider.hpp:60
~kmac_provider()
Definition kmac_provider.hpp:66
kmac_provider(size_t hashsize)
Definition kmac_provider.hpp:44
void set_key(const std::string &key)
Definition kmac_provider.hpp:71
void set_customization(const std::string &customization)
Definition kmac_provider.hpp:76
void squeeze(unsigned char *hash, size_t hs)
Definition kmac_provider.hpp:94
kmac_provider()
Definition kmac_provider.hpp:52
Definition shake_provider.hpp:56
void update(const unsigned char *data, size_t len)
Definition shake_provider.hpp:113
void squeeze(unsigned char *hash, size_t hs)
Definition shake_provider.hpp:124
void clear()
Definition shake_provider.hpp:158
void set_function_name(const std::string &function_name)
Definition shake_provider.hpp:70
void set_customization(const std::string &customization)
Definition shake_provider.hpp:75
void init()
Definition shake_provider.hpp:80
std::string bytepad(const std::string &str)
Definition kmac_provider.hpp:19
static size_t left_encode(size_t num, unsigned char *buf)
Definition shake_provider.hpp:21
static size_t right_encode(size_t num, unsigned char *buf, bool k12)
Definition shake_provider.hpp:36
void validate_hash_size(size_t hs, std::initializer_list< size_t > set)
Definition validate_hash_size.hpp:14
void zero_memory(void *v, size_t n)
Definition functions.hpp:85
digestpp namespace
Definition ascon.hpp:14
Definition traits.hpp:17