aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/hash/streebog/streebog.cpp
blob: c58320bca062878c7739379b4bc6c7d1e6f2071d (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Streebog
* (C) 2017 Ribose Inc.
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/streebog.h>
#include <botan/exceptn.h>

namespace Botan {

extern const uint64_t STREEBOG_Ax[8][256];
extern const uint64_t STREEBOG_C[12][8];

std::unique_ptr<HashFunction> Streebog::copy_state() const
   {
   return std::unique_ptr<HashFunction>(new Streebog(*this));
   }

namespace {

static inline void addm(const uint8_t* m, uint64_t* h)
   {
   uint64_t carry = false;
   for(int i = 0; i < 8; i++)
      {
      const uint64_t m64 = load_le<uint64_t>(m, i);
      const uint64_t hi = load_le<uint64_t>(reinterpret_cast<uint8_t*>(h), i);
      const uint64_t t = hi + m64;

      const uint64_t overflow = (t < hi) | (t < m64);
      store_le(t + carry, reinterpret_cast<uint8_t*>(&h[i]));
      carry = overflow;
      }
   }

inline void lps(uint64_t* block)
   {
   uint8_t r[64];
   std::memcpy(r, block, 64);

   for(int i = 0; i < 8; ++i)
      {
      block[i] =  load_le<uint64_t>(reinterpret_cast<const uint8_t*>(&STREEBOG_Ax[0][r[i]]), 0) ^
                  load_le<uint64_t>(reinterpret_cast<const uint8_t*>(&STREEBOG_Ax[1][r[i + 8]]), 0) ^
                  load_le<uint64_t>(reinterpret_cast<const uint8_t*>(&STREEBOG_Ax[2][r[i + 16]]), 0) ^
                  load_le<uint64_t>(reinterpret_cast<const uint8_t*>(&STREEBOG_Ax[3][r[i + 24]]), 0) ^
                  load_le<uint64_t>(reinterpret_cast<const uint8_t*>(&STREEBOG_Ax[4][r[i + 32]]), 0) ^
                  load_le<uint64_t>(reinterpret_cast<const uint8_t*>(&STREEBOG_Ax[5][r[i + 40]]), 0) ^
                  load_le<uint64_t>(reinterpret_cast<const uint8_t*>(&STREEBOG_Ax[6][r[i + 48]]), 0) ^
                  load_le<uint64_t>(reinterpret_cast<const uint8_t*>(&STREEBOG_Ax[7][r[i + 56]]), 0);
      }
   }

inline void e(uint64_t* K, const uint8_t* m)
   {
   uint64_t tmp[8];
   uint64_t C[8];

   std::memcpy(tmp, K, 64);
   xor_buf(K, reinterpret_cast<const uint64_t*>(m), 8);
   for(int i = 0; i < 12; ++i)
      {
      lps(K);
      load_le(C, reinterpret_cast<const uint8_t*>(&STREEBOG_C[i][0]), 8);
      xor_buf(tmp, C, 8);
      lps(tmp);
      xor_buf(K, tmp, 8);
      }
   }

inline void g(uint64_t* h, const uint8_t* m, uint64_t N)
   {
   uint64_t hN[8];

   // force N to little-endian
   store_le(N, reinterpret_cast<uint8_t*>(&N));

   std::memcpy(hN, h, 64);
   hN[0] ^= N;
   lps(hN);
   e(hN, m);
   xor_buf(h, hN, 8);
   xor_buf(h, reinterpret_cast<const uint64_t*>(m), 8);
   }

} //namespace

Streebog::Streebog(size_t output_bits) :
   m_output_bits(output_bits),
   m_count(0),
   m_position(0),
   m_buffer(64),
   m_h(8),
   m_S(8)
   {
   if(output_bits != 256 && output_bits != 512)
      throw Invalid_Argument("Streebog: Invalid output length " +
                             std::to_string(output_bits));

   clear();
   }

std::string Streebog::name() const
   {
   return "Streebog-" + std::to_string(m_output_bits);
   }

/*
* Clear memory of sensitive data
*/
void Streebog::clear()
   {
   m_count = 0;
   m_position = 0;
   zeroise(m_buffer);
   if(m_output_bits == 256)
      { std::fill(m_h.begin(), m_h.end(), 0x0101010101010101ULL); }
   else
      { std::fill(m_h.begin(), m_h.end(), 0x0ULL); }

   zeroise(m_S);
   }

/*
* Update the hash
*/
void Streebog::add_data(const uint8_t input[], size_t length)
   {
   while(m_position + length >= 64)
      {
      buffer_insert(m_buffer, m_position, input, 64 - m_position);
      compress(m_buffer.data());
      m_count += 512;
      input += (64 - m_position);
      length -= (64 - m_position);
      m_position = 0;
      }

   buffer_insert(m_buffer, m_position, input, length);
   m_position += length;
   }

/*
* Finalize a hash
*/
void Streebog::final_result(uint8_t output[])
   {
   m_buffer[m_position++] = 0x01;
   std::fill(m_buffer.begin() + m_position, m_buffer.end(), 0x00);

   compress(m_buffer.data());
   m_count += (m_position - 1) * 8;

   zeroise(m_buffer);
   store_le(m_count, m_buffer.data());
   compress(m_buffer.data(), true);

   compress(reinterpret_cast<const uint8_t*>(m_S.data()), true);
   std::memcpy(output, &m_h[8 - output_length() / 8], output_length());
   clear();
   }

void Streebog::compress(const uint8_t input[], bool last_block)
   {
   g(m_h.data(), input, last_block ? 0ULL : m_count);
   if(!last_block)
      { addm(input, m_S.data()); }
   }

}