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

randombit / botan / 5122901828

30 May 2023 02:42PM UTC coverage: 92.22% (+0.01%) from 92.209%
5122901828

Pull #3549

github

web-flow
Merge 26dd3cf4b into 057bcbc35
Pull Request #3549: SPHINCS+

76784 of 83262 relevant lines covered (92.22%)

12270748.88 hits per line

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

92.31
/src/lib/hash/trunc_hash/trunc_hash.cpp
1
/**
2
 * Wrapper for truncated hashes
3
 * (C) 2023 Jack Lloyd
4
 *     2023 René Meusel - Rohde & Schwarz Cybersecurity
5
 *
6
 * Botan is released under the Simplified BSD License (see license.txt)
7
 */
8

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

11
#include <botan/exceptn.h>
12
#include <botan/internal/fmt.h>
13

14
namespace Botan {
15

16
void Truncated_Hash::add_data(const uint8_t input[], size_t length) { m_hash->update(input, length); }
77,180,517✔
17

18
void Truncated_Hash::final_result(uint8_t out[]) {
20,982,096✔
19
   BOTAN_ASSERT_NOMSG(m_hash->output_length() * 8 >= m_output_bits);
20,982,096✔
20

21
   m_hash->final(m_buffer);
20,982,096✔
22

23
   // truncate output to a full number of bytes
24
   const auto bytes = output_length();
20,982,096✔
25
   std::copy_n(m_buffer.begin(), bytes, out);
41,964,192✔
26
   zeroise(m_buffer);
20,982,096✔
27

28
   // mask the unwanted bits in the final byte
29
   const uint8_t bits_in_last_byte = ((m_output_bits - 1) % 8) + 1;
20,982,096✔
30
   const uint8_t bitmask = ~((1 << (8 - bits_in_last_byte)) - 1);
20,982,096✔
31

32
   out[bytes - 1] &= bitmask;
20,982,096✔
33
}
20,982,096✔
34

35
size_t Truncated_Hash::output_length() const { return (m_output_bits + 7) / 8; }
20,982,096✔
36

37
std::string Truncated_Hash::name() const { return fmt("Truncated({},{})", m_hash->name(), m_output_bits); }
21✔
38

39
std::unique_ptr<HashFunction> Truncated_Hash::new_object() const {
227✔
40
   return std::make_unique<Truncated_Hash>(m_hash->new_object(), m_output_bits);
227✔
41
}
42

43
std::unique_ptr<HashFunction> Truncated_Hash::copy_state() const {
×
44
   return std::make_unique<Truncated_Hash>(m_hash->copy_state(), m_output_bits);
×
45
}
46

47
void Truncated_Hash::clear() { m_hash->clear(); }
269✔
48

49
Truncated_Hash::Truncated_Hash(std::unique_ptr<HashFunction> hash, size_t bits) :
331✔
50
      m_hash(std::move(hash)), m_output_bits(bits), m_buffer(m_hash->output_length()) {
331✔
51
   BOTAN_ASSERT_NONNULL(m_hash);
331✔
52

53
   if(m_output_bits == 0) {
331✔
54
      throw Invalid_Argument("Truncating a hash to 0 does not make sense");
1✔
55
   }
56

57
   if(m_hash->output_length() * 8 < m_output_bits) {
330✔
58
      throw Invalid_Argument("Underlying hash function does not produce enough bytes for truncation");
1✔
59
   }
60
}
333✔
61

62
}  // 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