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

randombit / botan / 5312210723

19 Jun 2023 01:02PM UTC coverage: 91.712% (+0.002%) from 91.71%
5312210723

Pull #3549

github

web-flow
Merge 3275783d5 into 5288e84e1
Pull Request #3549: SPHINCS+

78132 of 85193 relevant lines covered (91.71%)

11884653.99 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/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) {
84,056,015✔
17
   m_hash->update(input, length);
84,056,015✔
18
}
84,056,015✔
19

20
void Truncated_Hash::final_result(uint8_t out[]) {
23,050,529✔
21
   BOTAN_ASSERT_NOMSG(m_hash->output_length() * 8 >= m_output_bits);
23,050,529✔
22

23
   m_hash->final(m_buffer);
23,050,529✔
24

25
   // truncate output to a full number of bytes
26
   const auto bytes = output_length();
23,050,529✔
27
   std::copy_n(m_buffer.begin(), bytes, out);
46,101,058✔
28
   zeroise(m_buffer);
23,050,529✔
29

30
   // mask the unwanted bits in the final byte
31
   const uint8_t bits_in_last_byte = ((m_output_bits - 1) % 8) + 1;
23,050,529✔
32
   const uint8_t bitmask = ~((1 << (8 - bits_in_last_byte)) - 1);
23,050,529✔
33

34
   out[bytes - 1] &= bitmask;
23,050,529✔
35
}
23,050,529✔
36

37
size_t Truncated_Hash::output_length() const {
46,650,252✔
38
   return (m_output_bits + 7) / 8;
23,050,529✔
39
}
40

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

45
std::unique_ptr<HashFunction> Truncated_Hash::new_object() const {
227✔
46
   return std::make_unique<Truncated_Hash>(m_hash->new_object(), m_output_bits);
227✔
47
}
48

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

53
void Truncated_Hash::clear() {
274✔
54
   m_hash->clear();
274✔
55
}
274✔
56

57
Truncated_Hash::Truncated_Hash(std::unique_ptr<HashFunction> hash, size_t bits) :
359✔
58
      m_hash(std::move(hash)), m_output_bits(bits), m_buffer(m_hash->output_length()) {
359✔
59
   BOTAN_ASSERT_NONNULL(m_hash);
359✔
60

61
   if(m_output_bits == 0) {
359✔
62
      throw Invalid_Argument("Truncating a hash to 0 does not make sense");
1✔
63
   }
64

65
   if(m_hash->output_length() * 8 < m_output_bits) {
358✔
66
      throw Invalid_Argument("Underlying hash function does not produce enough bytes for truncation");
1✔
67
   }
68
}
361✔
69

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