Skip to content

Commit 0698936

Browse files
committed
Update deprecated mem_ops.h func calls to use std::span-based versions and make others todo
1 parent cf74a5d commit 0698936

File tree

9 files changed

+52
-60
lines changed

9 files changed

+52
-60
lines changed

src/lib/mac/cmac/cmac.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ void CMAC::add_data(std::span<const uint8_t> input) {
2525
copy_mem(m_buffer.data() + m_position, input.data(), initial_fill);
2626

2727
if(m_position + input.size() > bs) {
28-
xor_buf(m_state, m_buffer, bs);
28+
xor_buf(m_state, std::span{m_buffer}.first(bs));
2929
m_cipher->encrypt(m_state);
3030

3131
BufferSlicer in(input);
3232
in.skip(bs - m_position);
3333
while(in.remaining() > bs) {
34-
xor_buf(m_state, in.take(bs), bs);
34+
xor_buf(m_state, in.take(bs));
3535
m_cipher->encrypt(m_state);
3636
}
3737

@@ -47,13 +47,13 @@ void CMAC::add_data(std::span<const uint8_t> input) {
4747
* Finalize an CMAC Calculation
4848
*/
4949
void CMAC::final_result(std::span<uint8_t> mac) {
50-
xor_buf(m_state, m_buffer, m_position);
50+
xor_buf(std::span{m_state}.first(m_position), std::span{m_buffer}.first(m_position));
5151

5252
if(m_position == output_length()) {
53-
xor_buf(m_state, m_B, output_length());
53+
xor_buf(m_state, m_B);
5454
} else {
5555
m_state[m_position] ^= 0x80;
56-
xor_buf(m_state, m_P, output_length());
56+
xor_buf(m_state, m_P);
5757
}
5858

5959
m_cipher->encrypt(m_state);

src/lib/mac/x919_mac/x919_mac.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void ANSI_X919_MAC::add_data(std::span<const uint8_t> input) {
2121
BufferSlicer in(input);
2222

2323
const auto to_be_xored = in.take(std::min(8 - m_position, in.remaining()));
24-
xor_buf(&m_state[m_position], to_be_xored.data(), to_be_xored.size());
24+
xor_buf(std::span{m_state}.subspan(m_position, to_be_xored.size()), to_be_xored);
2525
m_position += to_be_xored.size();
2626

2727
if(m_position < 8) {
@@ -30,12 +30,12 @@ void ANSI_X919_MAC::add_data(std::span<const uint8_t> input) {
3030

3131
m_des1->encrypt(m_state);
3232
while(in.remaining() >= 8) {
33-
xor_buf(m_state, in.take(8).data(), 8);
33+
xor_buf(std::span{m_state}.first<8>(), in.take<8>());
3434
m_des1->encrypt(m_state);
3535
}
3636

3737
const auto remaining = in.take(in.remaining());
38-
xor_buf(m_state, remaining.data(), remaining.size());
38+
xor_buf(std::span{m_state}.first(remaining.size()), remaining);
3939
m_position = remaining.size();
4040
}
4141

src/lib/misc/roughtime/roughtime.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <botan/pubkey.h>
1414
#include <botan/rng.h>
1515
#include <botan/internal/socket_udp.h>
16+
#include <botan/internal/stl_util.h>
1617

1718
#include <cmath>
1819
#include <map>
@@ -47,7 +48,8 @@ template <typename T>
4748
T copy(const uint8_t* t)
4849
requires(is_array<T>::value)
4950
{
50-
return typecast_copy<T>(t); //arrays are endianess independent, so we do a memcpy
51+
//arrays are endianess independent, so we do a memcpy
52+
return typecast_copy<T>(std::span<const uint8_t, sizeof(T)>(t, sizeof(T)));
5153
}
5254

5355
template <typename T>
@@ -146,10 +148,7 @@ void hashNode(std::array<uint8_t, 64>& hash, const std::array<uint8_t, 64>& node
146148

147149
template <size_t N, typename T>
148150
std::array<uint8_t, N> vector_to_array(std::vector<uint8_t, T> vec) {
149-
if(vec.size() != N) {
150-
throw std::logic_error("Invalid vector size");
151-
}
152-
return typecast_copy<std::array<uint8_t, N>>(vec.data());
151+
return typecast_copy<std::array<uint8_t, N>>(vec);
153152
}
154153
} // namespace
155154

@@ -159,7 +158,7 @@ Nonce::Nonce(const std::vector<uint8_t>& nonce) : m_nonce{} {
159158
if(nonce.size() != 64) {
160159
throw Invalid_Argument("Roughtime nonce must be 64 bytes long");
161160
}
162-
m_nonce = typecast_copy<std::array<uint8_t, 64>>(nonce.data());
161+
m_nonce = typecast_copy<std::array<uint8_t, 64>>(nonce);
163162
}
164163

165164
Nonce::Nonce(RandomNumberGenerator& rng) : m_nonce(rng.random_array<64>()) {}
@@ -201,10 +200,10 @@ Response Response::from_bits(const std::vector<uint8_t>& response, const Nonce&
201200

202201
auto hash = hashLeaf(nonce.get_nonce());
203202
auto index = indx;
204-
size_t level = 0;
205-
while(level < levels) {
206-
hashNode(hash, typecast_copy<std::array<uint8_t, 64>>(path.data() + level * 64), index & 1);
207-
++level;
203+
204+
BufferSlicer slicer(path);
205+
for(size_t level = 0; level < levels; ++level) {
206+
hashNode(hash, typecast_copy<std::array<uint8_t, 64>>(slicer.take<64>()), index & 1);
208207
index >>= 1;
209208
}
210209

src/lib/modes/aead/eax/eax.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ void EAX_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
133133
update(buffer, offset);
134134

135135
secure_vector<uint8_t> data_mac = m_cmac->final();
136-
xor_buf(data_mac, m_nonce_mac, data_mac.size());
136+
xor_buf(data_mac, m_nonce_mac);
137137

138138
if(m_ad_mac.empty()) {
139139
m_ad_mac = eax_prf(1, block_size(), *m_cmac, nullptr, 0);
140140
}
141141

142-
xor_buf(data_mac, m_ad_mac, data_mac.size());
142+
xor_buf(data_mac, m_ad_mac);
143143

144144
buffer += std::make_pair(data_mac.data(), tag_size());
145145

src/lib/modes/xts/xts.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,19 @@ void XTS_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
155155
buffer.resize(full_blocks + offset);
156156
update(buffer, offset);
157157

158-
xor_buf(last, tweak(), BS);
158+
xor_buf(std::span(last).first(BS), std::span(tweak(), BS));
159159
cipher().encrypt(last);
160-
xor_buf(last, tweak(), BS);
160+
xor_buf(std::span(last).first(BS), std::span(tweak(), BS));
161161

162162
for(size_t i = 0; i != final_bytes - BS; ++i) {
163163
last[i] ^= last[i + BS];
164164
last[i + BS] ^= last[i];
165165
last[i] ^= last[i + BS];
166166
}
167167

168-
xor_buf(last, tweak() + BS, BS);
168+
xor_buf(std::span(last).first(BS), std::span(tweak() + BS, BS));
169169
cipher().encrypt(last);
170-
xor_buf(last, tweak() + BS, BS);
170+
xor_buf(std::span(last).first(BS), std::span(tweak() + BS, BS));
171171

172172
buffer += last;
173173
}
@@ -190,9 +190,9 @@ size_t XTS_Decryption::process_msg(uint8_t buf[], size_t sz) {
190190
const size_t to_proc = std::min(blocks, blocks_in_tweak);
191191
const size_t proc_bytes = to_proc * BS;
192192

193-
xor_buf(buf, tweak(), proc_bytes);
193+
xor_buf(std::span(buf, proc_bytes), std::span(tweak(), proc_bytes));
194194
cipher().decrypt_n(buf, buf, to_proc);
195-
xor_buf(buf, tweak(), proc_bytes);
195+
xor_buf(std::span(buf, proc_bytes), std::span(tweak(), proc_bytes));
196196

197197
buf += proc_bytes;
198198
blocks -= to_proc;
@@ -224,19 +224,19 @@ void XTS_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
224224
buffer.resize(full_blocks + offset);
225225
update(buffer, offset);
226226

227-
xor_buf(last, tweak() + BS, BS);
227+
xor_buf(std::span(last).first(BS), std::span(tweak() + BS, BS));
228228
cipher().decrypt(last);
229-
xor_buf(last, tweak() + BS, BS);
229+
xor_buf(std::span(last).first(BS), std::span(tweak() + BS, BS));
230230

231231
for(size_t i = 0; i != final_bytes - BS; ++i) {
232232
last[i] ^= last[i + BS];
233233
last[i + BS] ^= last[i];
234234
last[i] ^= last[i + BS];
235235
}
236236

237-
xor_buf(last, tweak(), BS);
237+
xor_buf(std::span(last).first(BS), std::span(tweak(), BS));
238238
cipher().decrypt(last);
239-
xor_buf(last, tweak(), BS);
239+
xor_buf(std::span(last).first(BS), std::span(tweak(), BS));
240240

241241
buffer += last;
242242
}

src/lib/pubkey/dlies/dlies.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ std::vector<uint8_t> DLIES_Encryptor::enc(const uint8_t in[], size_t length, Ran
7070
m_cipher->start(m_iv.bits_of());
7171
m_cipher->finish(ciphertext);
7272
} else {
73-
xor_buf(ciphertext, secret_keys, cipher_key_len);
73+
xor_buf(std::span{ciphertext}.first(cipher_key_len), std::span{secret_keys}.first(cipher_key_len));
7474
}
7575

7676
// calculate MAC
@@ -183,7 +183,7 @@ secure_vector<uint8_t> DLIES_Decryptor::do_decrypt(uint8_t& valid_mask, const ui
183183
return secure_vector<uint8_t>();
184184
}
185185
} else {
186-
xor_buf(ciphertext, secret_keys.data(), cipher_key_len);
186+
xor_buf(std::span{ciphertext}.first(cipher_key_len), std::span{secret_keys}.first(cipher_key_len));
187187
}
188188

189189
return ciphertext;

src/lib/tls/tls12/tls_record.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <botan/internal/ct_utils.h>
1717
#include <botan/internal/fmt.h>
1818
#include <botan/internal/loadstor.h>
19+
#include <botan/internal/stl_util.h>
1920
#include <botan/internal/tls_seq_numbers.h>
2021
#include <botan/internal/tls_session_key.h>
2122
#include <sstream>
@@ -108,16 +109,13 @@ std::vector<uint8_t> Connection_Cipher_State::aead_nonce(uint64_t seq, RandomNum
108109
}
109110
case Nonce_Format::AEAD_XOR_12: {
110111
std::vector<uint8_t> nonce(12);
111-
store_be(seq, nonce.data() + 4);
112-
xor_buf(nonce, m_nonce.data(), m_nonce.size());
112+
store_be(seq, std::span{nonce}.last<8>());
113+
xor_buf(nonce, m_nonce);
113114
return nonce;
114115
}
115116
case Nonce_Format::AEAD_IMPLICIT_4: {
116117
BOTAN_ASSERT_NOMSG(m_nonce.size() == 4);
117-
std::vector<uint8_t> nonce(12);
118-
copy_mem(&nonce[0], m_nonce.data(), 4); // NOLINT(*container-data-pointer)
119-
store_be(seq, &nonce[nonce_bytes_from_handshake()]);
120-
return nonce;
118+
return concat(m_nonce, store_be(seq));
121119
}
122120
}
123121

@@ -143,19 +141,16 @@ std::vector<uint8_t> Connection_Cipher_State::aead_nonce(const uint8_t record[],
143141
}
144142
case Nonce_Format::AEAD_XOR_12: {
145143
std::vector<uint8_t> nonce(12);
146-
store_be(seq, nonce.data() + 4);
147-
xor_buf(nonce, m_nonce.data(), m_nonce.size());
144+
store_be(seq, std::span{nonce}.last<8>());
145+
xor_buf(nonce, m_nonce);
148146
return nonce;
149147
}
150148
case Nonce_Format::AEAD_IMPLICIT_4: {
151149
BOTAN_ASSERT_NOMSG(m_nonce.size() == 4);
152150
if(record_len < nonce_bytes_from_record()) {
153151
throw Decoding_Error("Invalid AEAD packet too short to be valid");
154152
}
155-
std::vector<uint8_t> nonce(12);
156-
copy_mem(&nonce[0], m_nonce.data(), 4); // NOLINT(*container-data-pointer)
157-
copy_mem(&nonce[nonce_bytes_from_handshake()], record, nonce_bytes_from_record());
158-
return nonce;
153+
return concat(m_nonce, std::span{record, record_len}.first(nonce_bytes_from_record()));
159154
}
160155
}
161156

src/lib/utils/mem_ops.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ inline constexpr ToT typecast_copy(const FromR& src) {
214214
return dst;
215215
}
216216

217-
// TODO: deprecate and replace
217+
BOTAN_DEPRECATED("This function is deprecated, use range-based version instead")
218218
template <typename T>
219219
inline constexpr void typecast_copy(uint8_t out[], T in[], size_t N)
220220
requires std::is_trivially_copyable_v<T>
@@ -223,7 +223,7 @@ inline constexpr void typecast_copy(uint8_t out[], T in[], size_t N)
223223
typecast_copy(std::span<uint8_t>(out, sizeof(T) * N), std::span<const T>(in, N));
224224
}
225225

226-
// TODO: deprecate and replace
226+
BOTAN_DEPRECATED("This function is deprecated, use range-based version instead")
227227
template <typename T>
228228
inline constexpr void typecast_copy(T out[], const uint8_t in[], size_t N)
229229
requires std::is_trivial_v<T>
@@ -232,22 +232,22 @@ inline constexpr void typecast_copy(T out[], const uint8_t in[], size_t N)
232232
typecast_copy(std::span<T>(out, N), std::span<const uint8_t>(in, N * sizeof(T)));
233233
}
234234

235-
// TODO: deprecate and replace
235+
BOTAN_DEPRECATED("This function is deprecated, use range-based version instead")
236236
template <typename T>
237237
inline constexpr void typecast_copy(uint8_t out[], const T& in) {
238238
// asserts that *out points to the correct amount of memory
239239
typecast_copy(std::span<uint8_t, sizeof(T)>(out, sizeof(T)), in);
240240
}
241241

242-
// TODO: deprecate and replace
242+
BOTAN_DEPRECATED("This function is deprecated, use range-based version instead")
243243
template <typename T>
244244
requires std::is_trivial_v<std::decay_t<T>>
245245
inline constexpr void typecast_copy(T& out, const uint8_t in[]) {
246246
// asserts that *in points to the correct amount of memory
247247
typecast_copy(out, std::span<const uint8_t, sizeof(T)>(in, sizeof(T)));
248248
}
249249

250-
// TODO: deprecate and replace
250+
BOTAN_DEPRECATED("This function is deprecated, use range-based version instead")
251251
template <typename To>
252252
requires std::is_trivial_v<To>
253253
inline constexpr To typecast_copy(const uint8_t src[]) noexcept {
@@ -416,22 +416,22 @@ inline void xor_buf(uint8_t out[], const uint8_t in[], const uint8_t in2[], size
416416
xor_buf(std::span{out, length}, std::span{in, length}, std::span{in2, length});
417417
}
418418

419-
// TODO: deprecate and replace, use .subspan()
419+
BOTAN_DEPRECATED("This function is deprecated, use range-based version instead")
420420
inline void xor_buf(std::span<uint8_t> out, std::span<const uint8_t> in, size_t n) {
421421
BOTAN_ARG_CHECK(out.size() >= n, "output span is too small");
422422
BOTAN_ARG_CHECK(in.size() >= n, "input span is too small");
423423
xor_buf(out.first(n), in.first(n));
424424
}
425425

426-
// TODO: deprecate and replace, use .subspan()
426+
BOTAN_DEPRECATED("This function is deprecated, use range-based version instead")
427427
template <typename Alloc>
428428
void xor_buf(std::vector<uint8_t, Alloc>& out, const uint8_t* in, size_t n) {
429429
BOTAN_ARG_CHECK(out.size() >= n, "output vector is too small");
430430
// simply assumes that *in points to "n" allocated bytes at least
431431
xor_buf(std::span{out}.first(n), std::span{in, n});
432432
}
433433

434-
// TODO: deprecate and replace
434+
BOTAN_DEPRECATED("This function is deprecated, use range-based version instead")
435435
template <typename Alloc, typename Alloc2>
436436
void xor_buf(std::vector<uint8_t, Alloc>& out, const uint8_t* in, const std::vector<uint8_t, Alloc2>& in2, size_t n) {
437437
BOTAN_ARG_CHECK(out.size() >= n, "output vector is too small");

src/tests/test_roughtime.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ class Roughtime_Request_Tests final : public Text_Based_Test {
3434

3535
const auto request = Botan::Roughtime::encode_request(nonce);
3636
result.test_eq(
37-
"encode", type == "Valid", request == Botan::typecast_copy<std::array<uint8_t, 1024>>(request_v.data()));
38-
37+
"encode", type == "Valid", request == Botan::typecast_copy<std::array<uint8_t, 1024>>(request_v));
3938
return result;
4039
}
4140
};
@@ -114,10 +113,10 @@ class Roughtime final : public Test {
114113
auto rand64 = Botan::unlock(rng.random_vec(64));
115114
Botan::Roughtime::Nonce nonce_v(rand64);
116115
result.confirm("nonce from vector",
117-
nonce_v.get_nonce() == Botan::typecast_copy<std::array<uint8_t, 64>>(rand64.data()));
118-
Botan::Roughtime::Nonce nonce_a(Botan::typecast_copy<std::array<uint8_t, 64>>(rand64.data()));
116+
nonce_v.get_nonce() == Botan::typecast_copy<std::array<uint8_t, 64>>(rand64));
117+
Botan::Roughtime::Nonce nonce_a(Botan::typecast_copy<std::array<uint8_t, 64>>(rand64));
119118
result.confirm("nonce from array",
120-
nonce_v.get_nonce() == Botan::typecast_copy<std::array<uint8_t, 64>>(rand64.data()));
119+
nonce_v.get_nonce() == Botan::typecast_copy<std::array<uint8_t, 64>>(rand64));
121120
rand64.push_back(10);
122121
result.test_throws("vector oversize", [&rand64]() { Botan::Roughtime::Nonce nonce_v2(rand64); }); //size 65
123122
rand64.pop_back();
@@ -135,9 +134,8 @@ class Roughtime final : public Test {
135134

136135
auto rand64 = Botan::unlock(rng.random_vec(64));
137136
Botan::Roughtime::Nonce nonce_v(rand64);
138-
result.confirm(
139-
"empty chain nonce is blind",
140-
c1.next_nonce(nonce_v).get_nonce() == Botan::typecast_copy<std::array<uint8_t, 64>>(rand64.data()));
137+
result.confirm("empty chain nonce is blind",
138+
c1.next_nonce(nonce_v).get_nonce() == Botan::typecast_copy<std::array<uint8_t, 64>>(rand64));
141139

142140
const std::string chain_str =
143141
"ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= eu9yhsJfVfguVSqGZdE8WKIxaBBM0ZG3Vmuc+IyZmG2YVmrIktUByDdwIFw6F4rZqmSFsBO85ljoVPz5bVPCOw== BQAAAEAAAABAAAAApAAAADwBAABTSUcAUEFUSFNSRVBDRVJUSU5EWBnGOEajOwPA6G7oL47seBP4C7eEpr57H43C2/fK/kMA0UGZVUdf4KNX8oxOK6JIcsbVk8qhghTwA70qtwpYmQkDAAAABAAAAAwAAABSQURJTUlEUFJPT1RAQg8AJrA8tEqPBQAqisiuAxgy2Pj7UJAiWbCdzGz1xcCnja3T+AqhC8fwpeIwW4GPy/vEb/awXW2DgSLKJfzWIAz+2lsR7t4UjNPvAgAAAEAAAABTSUcAREVMRes9Ch4X0HIw5KdOTB8xK4VDFSJBD/G9t7Et/CU7UW61OiTBXYYQTG2JekWZmGa0OHX1JPGG+APkpbsNw0BKUgYDAAAAIAAAACgAAABQVUJLTUlOVE1BWFR/9BWjpsWTQ1f6iUJea3EfZ1MkX3ftJiV3ABqNLpncFwAAAAAAAAAA//////////8AAAAA\n"

0 commit comments

Comments
 (0)