• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 6183012530

14 Sep 2023 08:38AM UTC coverage: 91.745% (+0.02%) from 91.721%
6183012530

Pull #3693

github

web-flow
Merge 7293deb2e into 43dcc8a1d
Pull Request #3693: Refactor: AlignmentBuffer<> helper for block-oriented Hashes

79357 of 86497 relevant lines covered (91.75%)

8560302.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.62
/src/lib/hash/streebog/streebog.cpp
1
/*
2
* Streebog
3
* (C) 2017 Ribose Inc.
4
* (C) 2018 Jack Lloyd
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include <botan/internal/streebog.h>
10

11
#include <botan/exceptn.h>
12
#include <botan/internal/fmt.h>
13
#include <botan/internal/loadstor.h>
14
#include <botan/internal/stl_util.h>
15

16
namespace Botan {
17

18
extern const uint64_t STREEBOG_Ax[8][256];
19
extern const uint64_t STREEBOG_C[12][8];
20

21
std::unique_ptr<HashFunction> Streebog::copy_state() const {
253✔
22
   return std::make_unique<Streebog>(*this);
253✔
23
}
24

25
Streebog::Streebog(size_t output_bits) : m_output_bits(output_bits), m_count(0), m_buffer(64), m_h(8), m_S(8) {
801✔
26
   if(output_bits != 256 && output_bits != 512) {
801✔
27
      throw Invalid_Argument(fmt("Streebog: Invalid output length {}", output_bits));
×
28
   }
29

30
   clear();
801✔
31
}
801✔
32

33
std::string Streebog::name() const {
801✔
34
   return fmt("Streebog-{}", m_output_bits);
801✔
35
}
36

37
/*
38
* Clear memory of sensitive data
39
*/
40
void Streebog::clear() {
3,166✔
41
   m_count = 0;
3,166✔
42
   m_buffer.clear();
3,166✔
43
   zeroise(m_S);
3,166✔
44

45
   const uint64_t fill = (m_output_bits == 512) ? 0 : 0x0101010101010101;
3,166✔
46
   std::fill(m_h.begin(), m_h.end(), fill);
3,166✔
47
}
3,166✔
48

49
/*
50
* Update the hash
51
*/
52
void Streebog::add_data(std::span<const uint8_t> input) {
4,536✔
53
   BufferSlicer in(input);
4,536✔
54

55
   while(!in.empty()) {
14,118✔
56
      if(const auto one_block = m_buffer.handle_unaligned_data(in)) {
5,046✔
57
         compress(one_block->data());
278✔
58
         m_count += 512;
278✔
59
      }
60

61
      if(m_buffer.in_alignment()) {
5,046✔
62
         while(const auto aligned_block = m_buffer.next_aligned_block_to_process(in)) {
2,074✔
63
            compress(aligned_block->data());
980✔
64
            m_count += 512;
980✔
65
         }
980✔
66
      }
67
   }
68
}
4,536✔
69

70
/*
71
* Finalize a hash
72
*/
73
void Streebog::final_result(std::span<uint8_t> output) {
2,100✔
74
   const auto pos = m_buffer.elements_in_buffer();
2,100✔
75

76
   const uint8_t padding = 0x01;
2,100✔
77
   m_buffer.append({&padding, 1});
2,100✔
78
   m_buffer.fill_up_with_zeros();
2,100✔
79

80
   compress(m_buffer.consume().data());
2,100✔
81
   m_count += pos * 8;
2,100✔
82

83
   m_buffer.fill_up_with_zeros();
2,100✔
84
   store_le(m_count, m_buffer.directly_modify_first(sizeof(m_count)).data());
2,100✔
85
   compress(m_buffer.consume().data(), true);
2,100✔
86

87
   compress_64(m_S.data(), true);
2,100✔
88
   // FIXME
89
   std::memcpy(output.data(), &m_h[8 - output_length() / 8], output_length());
2,100✔
90
   clear();
2,100✔
91
}
2,100✔
92

93
namespace {
94

95
inline uint64_t force_le(uint64_t x) {
96
#if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
97
   return x;
98
#elif defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
99
   return reverse_bytes(x);
100
#else
101
   store_le(x, reinterpret_cast<uint8_t*>(&x));
102
   return x;
103
#endif
104
}
105

106
inline void lps(uint64_t block[8]) {
188,950✔
107
   uint8_t r[64];
188,950✔
108
   // FIXME
109
   std::memcpy(r, block, 64);
188,950✔
110

111
   for(int i = 0; i < 8; ++i) {
1,700,550✔
112
      block[i] = force_le(STREEBOG_Ax[0][r[i + 0 * 8]]) ^ force_le(STREEBOG_Ax[1][r[i + 1 * 8]]) ^
1,511,600✔
113
                 force_le(STREEBOG_Ax[2][r[i + 2 * 8]]) ^ force_le(STREEBOG_Ax[3][r[i + 3 * 8]]) ^
1,511,600✔
114
                 force_le(STREEBOG_Ax[4][r[i + 4 * 8]]) ^ force_le(STREEBOG_Ax[5][r[i + 5 * 8]]) ^
1,511,600✔
115
                 force_le(STREEBOG_Ax[6][r[i + 6 * 8]]) ^ force_le(STREEBOG_Ax[7][r[i + 7 * 8]]);
1,511,600✔
116
   }
117
}
188,950✔
118

119
}  //namespace
120

121
void Streebog::compress(const uint8_t input[], bool last_block) {
5,458✔
122
   uint64_t M[8];
5,458✔
123
   std::memcpy(M, input, 64);
5,458✔
124

125
   compress_64(M, last_block);
5,458✔
126
}
×
127

128
void Streebog::compress_64(const uint64_t M[], bool last_block) {
7,558✔
129
   const uint64_t N = last_block ? 0 : force_le(m_count);
7,558✔
130

131
   uint64_t hN[8];
7,558✔
132
   uint64_t A[8];
7,558✔
133

134
   copy_mem(hN, m_h.data(), 8);
7,558✔
135
   hN[0] ^= N;
7,558✔
136
   lps(hN);
7,558✔
137

138
   copy_mem(A, hN, 8);
7,558✔
139

140
   for(size_t i = 0; i != 8; ++i) {
68,022✔
141
      hN[i] ^= M[i];
60,464✔
142
   }
143

144
   for(size_t i = 0; i < 12; ++i) {
98,254✔
145
      for(size_t j = 0; j != 8; ++j) {
816,264✔
146
         A[j] ^= force_le(STREEBOG_C[i][j]);
725,568✔
147
      }
148
      lps(A);
90,696✔
149

150
      lps(hN);
90,696✔
151
      for(size_t j = 0; j != 8; ++j) {
816,264✔
152
         hN[j] ^= A[j];
725,568✔
153
      }
154
   }
155

156
   for(size_t i = 0; i != 8; ++i) {
68,022✔
157
      m_h[i] ^= hN[i] ^ M[i];
60,464✔
158
   }
159

160
   if(!last_block) {
7,558✔
161
      uint64_t carry = 0;
162
      for(int i = 0; i < 8; i++) {
30,222✔
163
         const uint64_t m = force_le(M[i]);
26,864✔
164
         const uint64_t hi = force_le(m_S[i]);
26,864✔
165
         const uint64_t t = hi + m + carry;
26,864✔
166

167
         m_S[i] = force_le(t);
26,864✔
168
         if(t != m) {
26,864✔
169
            carry = (t < m);
9,952✔
170
         }
171
      }
172
   }
173
}
7,558✔
174

175
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc