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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

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

98.08
/src/tests/test_keccak_helpers.cpp
1
/*
2
* (C) 2023 Jack Lloyd
3
*     2023 René Meusel - Rohde & Schwarz Cybersecurity
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_KECCAK_PERM)
11

12
   #include <botan/hex.h>
13
   #include <botan/internal/keccak_helpers.h>
14
   #include <botan/internal/mem_utils.h>
15

16
   #if defined(BOTAN_HAS_SHAKE_XOF)
17
      #include <botan/xof.h>
18
   #endif
19

20
namespace Botan_Tests {
21

22
namespace {
23

24
decltype(auto) encode_size(size_t x) {
7✔
25
   return Botan::keccak_int_encoding_size(x);
7✔
26
}
27

28
decltype(auto) left_encode(Test::Result& result, size_t x) {
10✔
29
   const auto expected_length = Botan::keccak_int_encoding_size(x);
10✔
30
   std::vector<uint8_t> out(expected_length);
10✔
31
   result.test_eq("left_encode return value", Botan::keccak_int_left_encode(out, x).size(), expected_length);
10✔
32
   return out;
10✔
33
}
×
34

35
decltype(auto) right_encode(Test::Result& result, size_t x) {
10✔
36
   const auto expected_length = Botan::keccak_int_encoding_size(x);
10✔
37
   std::vector<uint8_t> out(expected_length);
10✔
38
   result.test_eq("right_encode return value", Botan::keccak_int_right_encode(out, x).size(), expected_length);
10✔
39
   return out;
10✔
40
}
×
41

42
decltype(auto) hex(std::string_view str) {
24✔
43
   return Botan::hex_decode(str);
20✔
44
}
45

46
   #if defined(BOTAN_HAS_SHAKE_XOF)
47

48
decltype(auto) shake32(std::vector<uint8_t> data) {
2✔
49
   const auto xof = Botan::XOF::create_or_throw("SHAKE-256");
2✔
50
   xof->update(data);
2✔
51
   return xof->output_stdvec(32);
2✔
52
}
2✔
53

54
   #endif
55

56
std::vector<Test::Result> keccak_helpers() {
1✔
57
   return {
1✔
58
      CHECK("keccak_int_encoding_size()",
59
            [](Test::Result& result) {
1✔
60
               result.test_eq("keccak_int_encoding_size(0)", encode_size(0), 2);
1✔
61
               result.test_eq("keccak_int_encoding_size(255)", encode_size(0xFF), 2);
1✔
62
               result.test_eq("keccak_int_encoding_size(256)", encode_size(0xFF + 1), 3);
1✔
63
               result.test_eq("keccak_int_encoding_size(65.535)", encode_size(0xFFFF), 3);
1✔
64
               result.test_eq("keccak_int_encoding_size(65.536)", encode_size(0xFFFF + 1), 4);
1✔
65
               result.test_eq("keccak_int_encoding_size(16.777.215)", encode_size(0xFFFFFF), 4);
1✔
66
               result.test_eq("keccak_int_encoding_size(16.777.216)", encode_size(0xFFFFFF + 1), 5);
1✔
67
            }),
1✔
68

69
         CHECK("keccak_int_left_encode()",
70
               [](Test::Result& result) {
1✔
71
                  result.test_is_eq("left_encode(0)", left_encode(result, 0), hex("0100"));
3✔
72
                  result.test_is_eq("left_encode(1)", left_encode(result, 1), hex("0101"));
3✔
73
                  result.test_is_eq("left_encode(255)", left_encode(result, 255), hex("01FF"));
3✔
74
                  result.test_is_eq("left_encode(256)", left_encode(result, 0xFF + 1), hex("020100"));
3✔
75
                  result.test_is_eq("left_encode(65.535)", left_encode(result, 0xFFFF), hex("02FFFF"));
3✔
76
                  result.test_is_eq("left_encode(65.536)", left_encode(result, 0xFFFF + 1), hex("03010000"));
3✔
77
                  result.test_is_eq("left_encode(16.777.215)", left_encode(result, 0xFFFFFF), hex("03FFFFFF"));
3✔
78
                  result.test_is_eq("left_encode(16.777.215)", left_encode(result, 0xFFFFFF), hex("03FFFFFF"));
3✔
79
                  result.test_is_eq("left_encode(16.777.216)", left_encode(result, 0xFFFFFF + 1), hex("0401000000"));
3✔
80
                  result.test_is_eq("left_encode(287.454.020)", left_encode(result, 0x11223344), hex("0411223344"));
3✔
81
               }),
1✔
82

83
         CHECK("keccak_int_right_encode()",
84
               [](Test::Result& result) {
1✔
85
                  result.test_is_eq("right_encode(0)", right_encode(result, 0), hex("0001"));
3✔
86
                  result.test_is_eq("right_encode(1)", right_encode(result, 1), hex("0101"));
3✔
87
                  result.test_is_eq("right_encode(255)", right_encode(result, 255), hex("FF01"));
3✔
88
                  result.test_is_eq("right_encode(256)", right_encode(result, 0xFF + 1), hex("010002"));
4✔
89
                  result.test_is_eq("right_encode(65.535)", right_encode(result, 0xFFFF), hex("FFFF02"));
3✔
90
                  result.test_is_eq("right_encode(65.536)", right_encode(result, 0xFFFF + 1), hex("01000003"));
3✔
91
                  result.test_is_eq("right_encode(16.777.215)", right_encode(result, 0xFFFFFF), hex("FFFFFF03"));
3✔
92
                  result.test_is_eq("right_encode(16.777.215)", right_encode(result, 0xFFFFFF), hex("FFFFFF03"));
3✔
93
                  result.test_is_eq("right_encode(16.777.216)", right_encode(result, 0xFFFFFF + 1), hex("0100000004"));
3✔
94
                  result.test_is_eq("right_encode(287.454.020)", right_encode(result, 0x11223344), hex("1122334404"));
3✔
95
               }),
1✔
96

97
         CHECK(
98
            "keccak_absorb_padded_strings_encoding() with one byte string (std::vector<>)",
99
            [](Test::Result& result) {
1✔
100
               std::vector<uint8_t> out;
1✔
101
               const auto padmod = 136 /* SHAKE-256 byte rate */;
1✔
102

103
               const std::vector<uint8_t> n{'K', 'M', 'A', 'C'};
1✔
104
               const auto bytes_generated = Botan::keccak_absorb_padded_strings_encoding(out, padmod, n);
1✔
105
               result.test_eq("padded bytes", bytes_generated, padmod);
1✔
106

107
               result.test_is_eq(
1✔
108
                  out,
109
                  hex(
2✔
110
                     "0188"     /* left_encode(perm.byte_rate()) */
111
                     "0120"     /* left_encode(n.size() * 8) */
112
                     "4B4D4143" /* "KMAC" */
113
                     "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
114
            }),
2✔
115

116
         CHECK(
117
            "keccak_absorb_padded_strings_encoding() with two byte strings (std::vector<>)",
118
            [](Test::Result& result) {
1✔
119
               std::vector<uint8_t> out;
1✔
120
               const auto padmod = 136 /* SHAKE-256 byte rate */;
1✔
121

122
               const std::vector<uint8_t> n{'K', 'M', 'A', 'C'};
1✔
123
               const std::string str =
1✔
124
                  "This is a long salt, that is longer than 128 bytes in order to fill up the first round of the Keccak permutation. That should do it.";
1✔
125
               const auto bytes_generated =
1✔
126
                  Botan::keccak_absorb_padded_strings_encoding(out, padmod, n, Botan::as_span_of_bytes(str));
1✔
127
               result.test_eq("padded bytes", bytes_generated, padmod * 2);
1✔
128

129
               result.test_is_eq(
1✔
130
                  out,
131
                  hex(
2✔
132
                     "0188"     /* left_encode(perm.byte_rate()) */
133
                     "0120"     /* left_encode(n.size() * 8) */
134
                     "4B4D4143" /* "KMAC" */
135
                     "020420"   /* left_encode(s.size() * 8) */
136
                     "546869732069732061206c6f6e672073616c742c2074686174206973206c6f6e676572207468616e2031323820627974657320696e206f7264657220746f2066696c6c2075702074686520666972737420726f756e64206f6620746865204b656363616b207065726d75746174696f6e2e20546861742073686f756c6420646f2069742e"
137
                     "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
138
            }),
3✔
139

140
   #if defined(BOTAN_HAS_SHAKE_XOF)
141

142
         CHECK(
143
            "keccak_absorb_padded_strings_encoding() with one byte string",
144
            [](Test::Result& result) {
1✔
145
               std::vector<uint8_t> out(32);
1✔
146
               const auto xof = Botan::XOF::create_or_throw("SHAKE-256");
1✔
147
               const auto padmod = xof->block_size();
1✔
148

149
               const std::vector<uint8_t> n{'K', 'M', 'A', 'C'};
1✔
150
               const auto bytes_generated = Botan::keccak_absorb_padded_strings_encoding(*xof, padmod, n);
1✔
151
               result.test_eq("padded bytes", bytes_generated, padmod);
1✔
152

153
               result.test_is_eq(
1✔
154
                  xof->output_stdvec(32),
2✔
155
                  shake32(hex(
3✔
156
                     "0188"     /* left_encode(perm.byte_rate()) */
157
                     "0120"     /* left_encode(n.size() * 8) */
158
                     "4B4D4143" /* "KMAC" */
159
                     "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")));
160
            }),
3✔
161

162
         CHECK("keccak_absorb_padded_strings_encoding() with two byte strings", [](Test::Result& result) {
1✔
163
            std::vector<uint8_t> out(32);
2✔
164
            const auto xof = Botan::XOF::create_or_throw("SHAKE-256");
1✔
165
            const auto padmod = xof->block_size();
1✔
166

167
            const std::vector<uint8_t> n{'K', 'M', 'A', 'C'};
1✔
168
            const std::string str =
1✔
169
               "This is a long salt, that is longer than 128 bytes in order to fill up the first round of the Keccak permutation. That should do it.";
1✔
170
            const auto bytes_generated =
1✔
171
               Botan::keccak_absorb_padded_strings_encoding(*xof, padmod, n, Botan::as_span_of_bytes(str));
1✔
172
            result.test_eq("padded bytes", bytes_generated, padmod * 2);
1✔
173

174
            result.test_is_eq(
1✔
175
               xof->output_stdvec(32),
3✔
176
               shake32(hex(
3✔
177
                  "0188"     /* left_encode(perm.byte_rate()) */
178
                  "0120"     /* left_encode(n.size() * 8) */
179
                  "4B4D4143" /* "KMAC" */
180
                  "020420"   /* left_encode(s.size() * 8) */
181
                  "546869732069732061206c6f6e672073616c742c2074686174206973206c6f6e676572207468616e2031323820627974657320696e206f7264657220746f2066696c6c2075702074686520666972737420726f756e64206f6620746865204b656363616b207065726d75746174696f6e2e20546861742073686f756c6420646f2069742e"
182
                  "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")));
183
         }),
4✔
184

185
   #endif
186
   };
8✔
187
}
1✔
188

189
}  // namespace
190

191
BOTAN_REGISTER_TEST_FN("utils", "keccak_helpers", keccak_helpers);
192

193
}  // namespace Botan_Tests
194

195
#endif
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