Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

blake2b.cpp
Go to the documentation of this file.
1 /*
2 * Blake2b
3 * (C) 2016 cynecx
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/blake2b.h>
9 #include <botan/exceptn.h>
10 #include <botan/mem_ops.h>
11 #include <botan/loadstor.h>
12 #include <botan/rotate.h>
13 #include <algorithm>
14
15 namespace Botan {
16
17 namespace {
18
19 const uint64_t blake2b_IV[BLAKE2B_IVU64COUNT] = {
20 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
21 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
22 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
23 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
24 };
25
26 const uint64_t blake2b_sigma[12][16] = {
27 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
28 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
29 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
30 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
31 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
32 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
33 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
34 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
35 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
36 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
37 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
38 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
39 };
40 }
41
42 Blake2b::Blake2b(size_t output_bits) :
43 m_output_bits(output_bits),
44 m_buffer(BLAKE2B_BLOCKBYTES),
45 m_buflen(0),
46 m_H(BLAKE2B_IVU64COUNT)
47 {
48 if(output_bits == 0 || output_bits % 8 != 0
49 || output_bits / 8 > BLAKE2B_OUTBYTES)
50 {
51 throw Invalid_Argument("Bad output bits size for Blake2b");
52 }
53
54 state_init();
55 }
56
57 void Blake2b::state_init()
58 {
59 std::copy(std::begin(blake2b_IV), std::end(blake2b_IV), m_H.begin());
60 m_H[0] ^= 0x01010000 ^ static_cast<uint8_t>(output_length());
61 m_T[0] = m_T[1] = 0;
62 m_F[0] = m_F[1] = 0;
63 }
64
65 void Blake2b::compress(bool lastblock)
66 {
67 uint64_t m[16];
68 uint64_t v[16];
69 uint64_t* const H = m_H.data();
70 const uint8_t* const block = m_buffer.data();
71
72 if(lastblock)
73 {
74 m_F[0] = ~0ULL;
75 }
76
77 for(int i = 0; i < 16; i++)
78 {
79 m[i] = load_le<uint64_t>(block, i);
80 }
81
82 for(int i = 0; i < 8; i++)
83 {
84 v[i] = H[i];
85 v[i + 8] = blake2b_IV[i];
86 }
87
88 v[12] ^= m_T[0];
89 v[13] ^= m_T[1];
90 v[14] ^= m_F[0];
91 v[15] ^= m_F[1];
92
93 #define G(r, i, a, b, c, d) \
94 do { \
95 a = a + b + m[blake2b_sigma[r][2 * i + 0]]; \
96 d = rotate_right<uint64_t>(d ^ a, 32); \
97 c = c + d; \
98 b = rotate_right<uint64_t>(b ^ c, 24); \
99 a = a + b + m[blake2b_sigma[r][2 * i + 1]]; \
100 d = rotate_right<uint64_t>(d ^ a, 16); \
101 c = c + d; \
102 b = rotate_right<uint64_t>(b ^ c, 63); \
103 } while(0)
104
105 #define ROUND(r) \
106 do { \
107 G(r, 0, v[0], v[4], v[8], v[12]); \
108 G(r, 1, v[1], v[5], v[9], v[13]); \
109 G(r, 2, v[2], v[6], v[10], v[14]); \
110 G(r, 3, v[3], v[7], v[11], v[15]); \
111 G(r, 4, v[0], v[5], v[10], v[15]); \
112 G(r, 5, v[1], v[6], v[11], v[12]); \
113 G(r, 6, v[2], v[7], v[8], v[13]); \
114 G(r, 7, v[3], v[4], v[9], v[14]); \
115 } while(0)
116
117 ROUND(0);
118 ROUND(1);
119 ROUND(2);
120 ROUND(3);
121 ROUND(4);
122 ROUND(5);
123 ROUND(6);
124 ROUND(7);
125 ROUND(8);
126 ROUND(9);
127 ROUND(10);
128 ROUND(11);
129
130 for(int i = 0; i < 8; i++)
131 {
132 H[i] ^= v[i] ^ v[i + 8];
133 }
134
135 #undef G
136 #undef ROUND
137 }
138
139 void Blake2b::increment_counter(const uint64_t inc)
140 {
141 m_T[0] += inc;
142 if(m_T[0] < inc)
143 {
144 m_T[1]++;
145 }
146 }
147
148 void Blake2b::add_data(const uint8_t input[], size_t length)
149 {
150 if(!input || length == 0)
151 {
152 return;
153 }
154
155 uint8_t* const buffer = m_buffer.data();
156
157 while(length > 0)
158 {
159 size_t fill = BLAKE2B_BLOCKBYTES - m_buflen;
160
161 if(length <= fill)
162 {
163 std::memcpy(buffer + m_buflen, input, length);
164 m_buflen += length;
165 return;
166 }
167
168 std::memcpy(buffer + m_buflen, input, fill);
169 increment_counter(BLAKE2B_BLOCKBYTES);
170 compress();
171
172 m_buflen = 0;
173 input += fill;
174 length -= fill;
175 }
176 }
177
178 void Blake2b::final_result(uint8_t output[])
179 {
180 if(!output)
181 {
182 return;
183 }
184
185 uint8_t* const buffer = m_buffer.data();
186 const uint64_t* const H = static_cast<const uint64_t*>(m_H.data());
187 uint16_t outlen = static_cast<uint16_t>(output_length());
188
189 std::memset(buffer + m_buflen, 0, BLAKE2B_BLOCKBYTES - m_buflen);
190 increment_counter(m_buflen);
191 compress(true);
192
193 for (uint16_t i = 0; i < outlen; i++)
194 {
195 output[i] = (H[i >> 3] >> (8 * (i & 7))) & 0xFF;
196 }
197
198 clear();
199 }
200
201 std::string Blake2b::name() const
202 {
203 return "Blake2b(" + std::to_string(m_output_bits) + ")";
204 }
205
206 HashFunction* Blake2b::clone() const
207 {
208 return new Blake2b(m_output_bits);
209 }
210
211 void Blake2b::clear()
212 {
213 zeroise(m_H);
214 zeroise(m_buffer);
215 m_buflen = 0;
216 state_init();
217 }
218
219 }
Botan::Blake2b::clone
HashFunction * clone() const override
Definition: blake2b.cpp:206
Botan::Invalid_Argument
Definition: exceptn.h:34
Botan::Blake2b::Blake2b
Blake2b(size_t output_bits=512)
Definition: blake2b.cpp:42
Botan::BLAKE2B_OUTBYTES
Definition: blake2b.h:19
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
Botan::BLAKE2B_BLOCKBYTES
Definition: blake2b.h:18
Botan::load_le< uint64_t >
uint64_t load_le< uint64_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:243
Botan::Blake2b::clear
void clear() override
Definition: blake2b.cpp:211
Botan
Definition: alg_id.cpp:13
Botan::Blake2b::name
std::string name() const override
Definition: blake2b.cpp:201
Botan::HashFunction
Definition: hash.h:19
ROUND
#define ROUND(r)
Botan::BLAKE2B_IVU64COUNT
Definition: blake2b.h:20
Botan::Blake2b::output_length
size_t output_length() const override
Definition: blake2b.h:35
Botan::zeroise
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:211