Coverage Report

Created: 2025-12-14 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/src/lib/mac/poly1305/poly1305.cpp
Line
Count
Source
1
/*
2
* Derived from poly1305-donna-64.h by Andrew Moon <liquidsun@gmail.com>
3
* in https://github.com/floodyberry/poly1305-donna
4
*
5
* (C) 2014 Andrew Moon
6
* (C) 2014 Jack Lloyd
7
*
8
* Botan is released under the Simplified BSD License (see license.txt)
9
*/
10
11
#include <botan/internal/poly1305.h>
12
13
#include <botan/internal/ct_utils.h>
14
#include <botan/internal/donna128.h>
15
#include <botan/internal/loadstor.h>
16
#include <botan/internal/stl_util.h>
17
18
namespace Botan {
19
20
namespace {
21
22
149
void poly1305_init(secure_vector<uint64_t>& X, const uint8_t key[32]) {
23
   /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
24
149
   const uint64_t t0 = load_le<uint64_t>(key, 0);
25
149
   const uint64_t t1 = load_le<uint64_t>(key, 1);
26
27
149
   X[0] = (t0) & 0xffc0fffffff;
28
149
   X[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffff;
29
149
   X[2] = ((t1 >> 24)) & 0x00ffffffc0f;
30
31
   /* h = 0 */
32
149
   X[3] = 0;
33
149
   X[4] = 0;
34
149
   X[5] = 0;
35
36
   /* save pad for later */
37
149
   X[6] = load_le<uint64_t>(key, 2);
38
149
   X[7] = load_le<uint64_t>(key, 3);
39
149
}
40
41
473
void poly1305_blocks(secure_vector<uint64_t>& X, const uint8_t* m, size_t blocks, bool is_final = false) {
42
#if !defined(BOTAN_TARGET_HAS_NATIVE_UINT128)
43
   typedef donna128 uint128_t;
44
#endif
45
46
473
   const uint64_t hibit = is_final ? 0 : (static_cast<uint64_t>(1) << 40); /* 1 << 128 */
47
48
473
   const uint64_t r0 = X[0];
49
473
   const uint64_t r1 = X[1];
50
473
   const uint64_t r2 = X[2];
51
52
473
   const uint64_t M44 = 0xFFFFFFFFFFF;
53
473
   const uint64_t M42 = 0x3FFFFFFFFFF;
54
55
473
   uint64_t h0 = X[3 + 0];
56
473
   uint64_t h1 = X[3 + 1];
57
473
   uint64_t h2 = X[3 + 2];
58
59
473
   const uint64_t s1 = r1 * 20;
60
473
   const uint64_t s2 = r2 * 20;
61
62
3.49k
   for(size_t i = 0; i != blocks; ++i) {
63
3.02k
      const uint64_t t0 = load_le<uint64_t>(m, 0);
64
3.02k
      const uint64_t t1 = load_le<uint64_t>(m, 1);
65
66
3.02k
      h0 += ((t0)&M44);
67
3.02k
      h1 += (((t0 >> 44) | (t1 << 20)) & M44);
68
3.02k
      h2 += (((t1 >> 24)) & M42) | hibit;
69
70
3.02k
      const uint128_t d0 = uint128_t(h0) * r0 + uint128_t(h1) * s2 + uint128_t(h2) * s1;
71
3.02k
      const uint64_t c0 = carry_shift(d0, 44);
72
73
3.02k
      const uint128_t d1 = uint128_t(h0) * r1 + uint128_t(h1) * r0 + uint128_t(h2) * s2 + c0;
74
3.02k
      const uint64_t c1 = carry_shift(d1, 44);
75
76
3.02k
      const uint128_t d2 = uint128_t(h0) * r2 + uint128_t(h1) * r1 + uint128_t(h2) * r0 + c1;
77
3.02k
      const uint64_t c2 = carry_shift(d2, 42);
78
79
3.02k
      h0 = d0 & M44;
80
3.02k
      h1 = d1 & M44;
81
3.02k
      h2 = d2 & M42;
82
83
3.02k
      h0 += c2 * 5;
84
3.02k
      h1 += h0 >> 44;
85
3.02k
      h0 = h0 & M44;
86
87
3.02k
      m += 16;
88
3.02k
   }
89
90
473
   X[3 + 0] = h0;
91
473
   X[3 + 1] = h1;
92
473
   X[3 + 2] = h2;
93
473
}
94
95
149
void poly1305_finish(secure_vector<uint64_t>& X, uint8_t mac[16]) {
96
149
   const uint64_t M44 = 0xFFFFFFFFFFF;
97
149
   const uint64_t M42 = 0x3FFFFFFFFFF;
98
99
   /* fully carry h */
100
149
   uint64_t h0 = X[3 + 0];
101
149
   uint64_t h1 = X[3 + 1];
102
149
   uint64_t h2 = X[3 + 2];
103
104
149
   uint64_t c = (h1 >> 44);
105
149
   h1 &= M44;
106
149
   h2 += c;
107
149
   c = (h2 >> 42);
108
149
   h2 &= M42;
109
149
   h0 += c * 5;
110
149
   c = (h0 >> 44);
111
149
   h0 &= M44;
112
149
   h1 += c;
113
149
   c = (h1 >> 44);
114
149
   h1 &= M44;
115
149
   h2 += c;
116
149
   c = (h2 >> 42);
117
149
   h2 &= M42;
118
149
   h0 += c * 5;
119
149
   c = (h0 >> 44);
120
149
   h0 &= M44;
121
149
   h1 += c;
122
123
   /* compute h + -p */
124
149
   uint64_t g0 = h0 + 5;
125
149
   c = (g0 >> 44);
126
149
   g0 &= M44;
127
149
   uint64_t g1 = h1 + c;
128
149
   c = (g1 >> 44);
129
149
   g1 &= M44;
130
149
   const uint64_t g2 = h2 + c - (static_cast<uint64_t>(1) << 42);
131
132
   /* select h if h < p, or h + -p if h >= p */
133
149
   const auto c_mask = CT::Mask<uint64_t>::expand(c);
134
149
   h0 = c_mask.select(g0, h0);
135
149
   h1 = c_mask.select(g1, h1);
136
149
   h2 = c_mask.select(g2, h2);
137
138
   /* h = (h + pad) */
139
149
   const uint64_t t0 = X[6];
140
149
   const uint64_t t1 = X[7];
141
142
149
   h0 += ((t0)&M44);
143
149
   c = (h0 >> 44);
144
149
   h0 &= M44;
145
149
   h1 += (((t0 >> 44) | (t1 << 20)) & M44) + c;
146
149
   c = (h1 >> 44);
147
149
   h1 &= M44;
148
149
   h2 += (((t1 >> 24)) & M42) + c;
149
149
   h2 &= M42;
150
151
   /* mac = h % (2^128) */
152
149
   h0 = ((h0) | (h1 << 44));
153
149
   h1 = ((h1 >> 20) | (h2 << 24));
154
155
149
   store_le(mac, h0, h1);
156
157
   /* zero out the state */
158
149
   clear_mem(X.data(), X.size());
159
149
}
160
161
}  // namespace
162
163
0
void Poly1305::clear() {
164
0
   zap(m_poly);
165
0
   m_buffer.clear();
166
0
}
167
168
974
bool Poly1305::has_keying_material() const {
169
974
   return m_poly.size() == 8;
170
974
}
171
172
149
void Poly1305::key_schedule(std::span<const uint8_t> key) {
173
149
   m_buffer.clear();
174
149
   m_poly.resize(8);
175
176
149
   poly1305_init(m_poly, key.data());
177
149
}
178
179
825
void Poly1305::add_data(std::span<const uint8_t> input) {
180
825
   assert_key_material_set();
181
182
825
   BufferSlicer in(input);
183
184
1.67k
   while(!in.empty()) {
185
852
      if(const auto one_block = m_buffer.handle_unaligned_data(in)) {
186
379
         poly1305_blocks(m_poly, one_block->data(), 1);
187
379
      }
188
189
852
      if(m_buffer.in_alignment()) {
190
473
         const auto [aligned_data, full_blocks] = m_buffer.aligned_data_to_process(in);
191
473
         if(full_blocks > 0) {
192
94
            poly1305_blocks(m_poly, aligned_data.data(), full_blocks);
193
94
         }
194
473
      }
195
852
   }
196
825
}
197
198
149
void Poly1305::final_result(std::span<uint8_t> out) {
199
149
   assert_key_material_set();
200
201
149
   if(!m_buffer.in_alignment()) {
202
0
      const uint8_t final_byte = 0x01;
203
0
      m_buffer.append({&final_byte, 1});
204
0
      m_buffer.fill_up_with_zeros();
205
0
      poly1305_blocks(m_poly, m_buffer.consume().data(), 1, true);
206
0
   }
207
208
149
   poly1305_finish(m_poly, out.data());
209
210
149
   m_poly.clear();
211
149
   m_buffer.clear();
212
149
}
213
214
}  // namespace Botan