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

randombit / botan / 13235046472

10 Feb 2025 06:40AM UTC coverage: 91.652% (-0.004%) from 91.656%
13235046472

Pull #4647

github

web-flow
Merge 1d27f28ee into 7deaa69bb
Pull Request #4647: Avoid using mem_ops.h or assert.h in public headers

94827 of 103464 relevant lines covered (91.65%)

11135060.49 hits per line

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

93.75
/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/assert.h>
12
#include <botan/exceptn.h>
13
#include <botan/internal/fmt.h>
14
#include <algorithm>
15

16
namespace Botan {
17

18
void Truncated_Hash::add_data(std::span<const uint8_t> input) {
231,460,796✔
19
   m_hash->update(input);
231,460,796✔
20
}
231,460,796✔
21

22
void Truncated_Hash::final_result(std::span<uint8_t> out) {
72,186,263✔
23
   BOTAN_ASSERT_NOMSG(m_hash->output_length() * 8 >= m_output_bits);
72,186,263✔
24

25
   m_hash->final(m_buffer);
72,186,263✔
26

27
   // truncate output to a full number of bytes
28
   const auto bytes = output_length();
72,186,263✔
29
   std::copy_n(m_buffer.begin(), bytes, out.data());
72,186,263✔
30
   zeroise(m_buffer);
72,186,263✔
31

32
   // mask the unwanted bits in the final byte
33
   const uint8_t bits_in_last_byte = ((m_output_bits - 1) % 8) + 1;
72,186,263✔
34
   const uint8_t bitmask = ~((1 << (8 - bits_in_last_byte)) - 1);
72,186,263✔
35

36
   out.back() &= bitmask;
72,186,263✔
37
}
72,186,263✔
38

39
size_t Truncated_Hash::output_length() const {
145,045,903✔
40
   return (m_output_bits + 7) / 8;
72,186,263✔
41
}
42

43
std::string Truncated_Hash::name() const {
21✔
44
   return fmt("Truncated({},{})", m_hash->name(), m_output_bits);
21✔
45
}
46

47
std::unique_ptr<HashFunction> Truncated_Hash::new_object() const {
763✔
48
   return std::make_unique<Truncated_Hash>(m_hash->new_object(), m_output_bits);
763✔
49
}
50

51
std::unique_ptr<HashFunction> Truncated_Hash::copy_state() const {
×
52
   return std::make_unique<Truncated_Hash>(m_hash->copy_state(), m_output_bits);
×
53
}
54

55
void Truncated_Hash::clear() {
276✔
56
   m_hash->clear();
276✔
57
}
276✔
58

59
Truncated_Hash::Truncated_Hash(std::unique_ptr<HashFunction> hash, size_t bits) :
13,425✔
60
      m_hash(std::move(hash)), m_output_bits(bits), m_buffer(m_hash->output_length()) {
13,425✔
61
   BOTAN_ASSERT_NONNULL(m_hash);
13,425✔
62

63
   if(m_output_bits == 0) {
13,425✔
64
      throw Invalid_Argument("Truncating a hash to 0 does not make sense");
1✔
65
   }
66

67
   if(m_hash->output_length() * 8 < m_output_bits) {
13,424✔
68
      throw Invalid_Argument("Underlying hash function does not produce enough bytes for truncation");
1✔
69
   }
70
}
13,427✔
71

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

© 2026 Coveralls, Inc