digestpp 1.0
C++11 header-only message digest library
Loading...
Searching...
No Matches
k12m14_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_K12M14_HPP
6#define DIGESTPP_PROVIDERS_K12M14_HPP
7
10#include "shake_provider.hpp"
11#include <array>
12
13namespace digestpp
14{
15
16namespace detail
17{
18
19enum class kangaroo_type
20{
21 k12,
22 m14
23};
24
25template<size_t B, kangaroo_type type>
27{
28public:
29 static const bool is_xof = true;
30
32 {
33 static_assert(B == 128 || B == 256, "K12M14 only supports 128 and 256 bits");
34 }
35
37 {
38 clear();
39 }
40
41 inline void set_customization(const std::string& customization)
42 {
43 S = customization;
44 }
45
46 inline void init()
47 {
48 main.init();
49 pos = 0;
50 total = 0;
51 chunk = 0;
52 squeezing = false;
53 }
54
55 inline void update(const unsigned char* data, size_t len)
56 {
57 detail::absorb_bytes(data, len, m.size(), m.size() + 1, m.data(), pos, total,
58 [this](const unsigned char* data, size_t len) { transform(data, len, len * 8192); });
59 }
60
61 inline void squeeze(unsigned char* hash, size_t hs)
62 {
63 if (!squeezing)
64 {
65 unsigned char buf[B / 4];
66 size_t len = shake_functions::right_encode(S.length(), buf, true);
67 if (!S.empty())
68 update(reinterpret_cast<const unsigned char*>(S.data()), S.length());
69 update(buf, len);
70 if (pos)
71 {
72 if (!chunk)
73 {
74 main.update(m.data(), pos);
75 main.set_suffix(0x07);
76 }
77 else
78 {
79 child.update(m.data(), pos);
80 child.squeeze(buf, B / 4);
81 main.update(buf, B / 4);
82 main.set_suffix(0x06);
83 len = shake_functions::right_encode(chunk, buf, true);
84 main.update(buf, len);
85 main.update(reinterpret_cast<const unsigned char*>("\xFF\xFF"), 2);
86 }
87 }
88 else
89 main.set_suffix(0x07);
90 squeezing = true;
91 }
92 main.squeeze(hash, hs);
93 }
94
95 inline void transform(const unsigned char* data, uint64_t num_blks, size_t reallen)
96 {
97 (void)reallen;
98
99 for (uint64_t blk = 0; blk < num_blks; blk++)
100 {
101 if (!chunk)
102 {
103 main.update(data, 8192);
104 main.update(reinterpret_cast<const unsigned char*>("\x03\x00\x00\x00\x00\x00\x00\x00"), 8);
105 }
106 else
107 {
108 child.update(data, 8192);
109 unsigned char buf[B / 4];
110 child.squeeze(buf, B / 4);
111 main.update(buf, B / 4);
112 }
113
114 child.init();
115 child.set_suffix(0x0b);
116 ++chunk;
117 data += 8192;
118 }
119 }
120
121
122 inline void clear()
123 {
124 main.clear();
125 child.clear();
126 zero_memory(m);
127 zero_memory(S);
128 S.clear();
129 }
130
131private:
132 constexpr static size_t R = type == kangaroo_type::k12 ? 12 : 14;
135 std::array<unsigned char, 8192> m;
136 std::string S;
137 size_t pos;
138 size_t total;
139 size_t chunk;
140 bool squeezing;
141};
142
143} // namespace detail
144
145} // namespace digestpp
146
147#endif // DIGESTPP_PROVIDERS_K12M14_HPP
Definition k12m14_provider.hpp:27
void init()
Definition k12m14_provider.hpp:46
void set_customization(const std::string &customization)
Definition k12m14_provider.hpp:41
void squeeze(unsigned char *hash, size_t hs)
Definition k12m14_provider.hpp:61
void update(const unsigned char *data, size_t len)
Definition k12m14_provider.hpp:55
void transform(const unsigned char *data, uint64_t num_blks, size_t reallen)
Definition k12m14_provider.hpp:95
~k12m14_provider()
Definition k12m14_provider.hpp:36
void clear()
Definition k12m14_provider.hpp:122
k12m14_provider()
Definition k12m14_provider.hpp:31
Definition shake_provider.hpp:56
Main class template implementing the public API for hashing.
Definition hasher.hpp:38
static size_t right_encode(size_t num, unsigned char *buf, bool k12)
Definition shake_provider.hpp:36
kangaroo_type
Definition k12m14_provider.hpp:20
void zero_memory(void *v, size_t n)
Definition functions.hpp:85
void absorb_bytes(const unsigned char *data, size_t len, size_t bs, size_t bschk, unsigned char *m, size_t &pos, T &total, TF transform)
Definition absorb_data.hpp:16
digestpp namespace
Definition ascon.hpp:14
Definition traits.hpp:17