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