Skip to content

Commit efababa

Browse files
committed
Botan-3 Migration
Complete Botan-3 migration targeting: Migrate from marked as deprecated functions -1363 padding needs to be done manually now -No serialization directly on the hasher -no declaration of functions with a serialization.
1 parent 6858f63 commit efababa

File tree

17 files changed

+79
-66
lines changed

17 files changed

+79
-66
lines changed

src/account/AccountService.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ AccountService::handle_session_fetch(const SessionLookup& msg, const Link& link,
4646
return response;
4747
}
4848

49-
auto key = Botan::BigInt::encode(*session);
49+
std::vector<uint8_t> key((*session).bytes());
50+
(*session).serialize_to(std::span<uint8_t>(key.data(), key.size()));
5051

5152
response.status = Status::OK;
5253
response.account_id = msg.account_id();
@@ -120,4 +121,4 @@ AccountService::handle_disconnect_by_id(const DisconnectID& msg, const Link& lin
120121
return std::nullopt;
121122
}
122123

123-
} // ember
124+
} // ember

src/gateway/AccountClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ void AccountClient::handle_locate_response(std::expected<const SessionResponse*,
8787
return;
8888
}
8989

90-
auto key = Botan::BigInt::decode(msg->key()->data(), msg->key()->size());
90+
auto key = Botan::BigInt::from_bytes(std::span<const std::uint8_t>(msg->key()->data(), msg->key()->size()));
9191
cb(msg->status(), std::move(key));
9292
}
9393

94-
} // gateway, ember
94+
} // gateway, ember

src/gateway/PacketCrypto.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class PacketCrypto final {
4444
);
4545

4646
key_.resize(key.bytes(), boost::container::default_init);
47-
key.binary_encode(key_.data(), key_.size());
47+
key.serialize_to(std::span<uint8_t>( key_.data() + (key_.size() - key.bytes()), key.bytes() ));
4848
}
4949

5050
inline void encrypt(auto& data) {
@@ -77,4 +77,4 @@ class PacketCrypto final {
7777
}
7878
};
7979

80-
} // gateway, ember
80+
} // gateway, ember

src/gateway/states/Authentication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void prove_session(ClientContext& ctx, const Botan::BigInt& key) {
164164
key.bytes(), boost::container::default_init
165165
);
166166

167-
key.binary_encode(k_bytes.data(), k_bytes.size());
167+
key.serialize_to(std::span<uint8_t>(k_bytes.data() + (k_bytes.size() - key.bytes()), key.bytes()));
168168

169169
const std::uint32_t protocol_id = 0; // best guess, this is hardcoded to zero in the client
170170
auto& auth_ctx = std::get<Context>(ctx.state_ctx);

src/libs/shared/shared/database/daos/mysql/PatchDAO.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class MySQLPatchDAO final : public PatchDAO {
6464

6565
const auto md5 = res->getString("md5");
6666
Botan::BigInt md5_int(md5.asStdString());
67-
Botan::BigInt::encode_1363(meta.file_meta.md5.data(), meta.file_meta.md5.size(), md5_int);
67+
std::fill(meta.file_meta.md5.begin(), meta.file_meta.md5.end(), 0); // 1363 style padding
68+
md5_int.serialize_to({ meta.file_meta.md5.data() + meta.file_meta.md5.size() - md5_int.bytes(), md5_int.bytes() });
6869
patches.emplace_back(std::move(meta));
6970
}
7071

@@ -87,8 +88,8 @@ class MySQLPatchDAO final : public PatchDAO {
8788
stmt->setBoolean(3, meta.mpq);
8889
stmt->setString(4, meta.file_meta.name);
8990
stmt->setUInt64(5, meta.file_meta.size);
90-
auto md5 = Botan::BigInt::decode(reinterpret_cast<const std::uint8_t*>(meta.file_meta.md5.data()),
91-
meta.file_meta.md5.size());
91+
auto md5 = Botan::BigInt::from_bytes(std::span<const uint8_t>(reinterpret_cast<const std::uint8_t*>(
92+
meta.file_meta.md5.data()), meta.file_meta.md5.size()));
9293

9394
stmt->setString(6, md5.to_hex_string());
9495
stmt->setUInt(7, meta.locale_id);

src/libs/srp6/src/Client.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Client::Client(std::string identifier, std::string password, Generator gen, std:
2323
: Client(std::move(identifier),
2424
std::move(password),
2525
gen,
26-
BigInt::decode((AutoSeeded_RNG()).random_vec(key_size)) % gen.prime(),
26+
BigInt::from_bytes(std::span<const uint8_t>{ (AutoSeeded_RNG().random_vec(key_size)).data(), key_size }) % gen.prime(),
2727
srp6a) { }
2828

2929
Client::Client(std::string identifier, std::string password, Generator gen, BigInt a, bool srp6a)
@@ -64,7 +64,7 @@ SessionKey Client::session_key(const BigInt& B, std::span<const std::uint8_t> sa
6464
return SessionKey(detail::interleaved_hash(detail::encode_flip_1363(S, N.bytes())));
6565
} else {
6666
KeyType key(S.bytes(), boost::container::default_init);
67-
S.binary_encode(key.data(), key.size());
67+
S.serialize_to(std::span<uint8_t>(key.data(), key.size()));
6868
return SessionKey(key);
6969
}
7070
}
@@ -74,4 +74,4 @@ BigInt Client::generate_proof(const SessionKey& key, const Botan::BigInt& B,
7474
return generate_client_proof(identifier_, key, gen_.prime(), gen_.generator(), A_, B, salt);
7575
}
7676

77-
} // srp6, ember
77+
} // srp6, ember

src/libs/srp6/src/Server.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014 - 2024 Ember
2+
* Copyright (c) 2014 - 2025 Ember
33
*
44
* This Source Code Form is subject to the terms of the Mozilla Public
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -48,7 +48,7 @@ SessionKey Server::session_key(const BigInt& A, Compliance mode, bool interleave
4848
return SessionKey(detail::interleaved_hash(detail::encode_flip_1363(S, N_.bytes())));
4949
} else {
5050
KeyType key(N_.bytes(), boost::container::default_init);
51-
S.binary_encode(key.data(), key.size());
51+
S.serialize_to(std::span<uint8_t>(key.data(), key.size()));
5252
return SessionKey(key);
5353
}
5454
}
@@ -58,4 +58,4 @@ BigInt Server::generate_proof(const SessionKey& key, const Botan::BigInt& A,
5858
return generate_server_proof(A, client_proof, key, N_.bytes());
5959
}
6060

61-
} // srp6, ember
61+
} // srp6, ember

src/libs/srp6/src/Util.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ namespace detail {
2121

2222
Botan::BigInt decode_flip(std::span<std::uint8_t> val) {
2323
std::ranges::reverse(val);
24-
return Botan::BigInt::decode(val.data(), val.size());
24+
return Botan::BigInt::from_bytes(std::span<const uint8_t>(val.data(), val.size()));
2525
}
2626

2727
SmallVec encode_flip(const Botan::BigInt& val) {
2828
SmallVec res(val.bytes(), boost::container::default_init);
29-
val.binary_encode(res.data(), res.size());
29+
val.serialize_to(std::span<uint8_t>(res.data(), res.size()));
3030
std::ranges::reverse(res);
3131
return res;
3232
}
3333

3434
SmallVec encode_flip_1363(const Botan::BigInt& val, std::size_t padding) {
3535
SmallVec res(padding, boost::container::default_init);
36-
Botan::BigInt::encode_1363(res.data(), res.size(), val);
36+
std::fill(res.begin(), res.end(), 0); // 1363 style padding
37+
val.serialize_to({ res.data() + res.size() - val.bytes(), val.bytes() });
3738
std::ranges::reverse(res);
3839
return res;
3940
}
@@ -69,15 +70,15 @@ Botan::BigInt scrambler(const Botan::BigInt& A, const Botan::BigInt& B, std::siz
6970
auto hasher = Botan::HashFunction::create_or_throw("SHA-1");
7071
BOOST_ASSERT_MSG(SHA1_LEN == hasher->output_length(), "Bad hash length");
7172
std::array<std::uint8_t, SHA1_LEN> hash_out;
72-
SmallVec vec(padding, boost::container::default_init);
73+
SmallVec vec(padding, 0); // 1363 style padding
7374

7475
if(mode == Compliance::RFC5054) {
75-
Botan::BigInt::encode_1363(vec.data(), vec.size(), A);
76+
A.serialize_to(std::span<uint8_t>(vec.data(), vec.size()));
7677
hasher->update(vec.data(), vec.size());
77-
Botan::BigInt::encode_1363(vec.data(), vec.size(), B);
78+
B.serialize_to(std::span<uint8_t>(vec.data(), vec.size()));
7879
hasher->update(vec.data(), vec.size());
7980
hasher->final(hash_out.data());
80-
return Botan::BigInt::decode(hash_out.data(), hash_out.size());
81+
return Botan::BigInt::from_bytes(std::span<const uint8_t>(hash_out.data(), hash_out.size()));
8182
} else {
8283
const auto& a_enc = encode_flip_1363(A, padding);
8384
const auto& b_enc = encode_flip_1363(B, padding);
@@ -93,10 +94,14 @@ Botan::BigInt compute_k(const Botan::BigInt& g, const Botan::BigInt& N) {
9394
std::array<std::uint8_t, SHA1_LEN> hash;
9495
auto hasher = Botan::HashFunction::create_or_throw("SHA-1");
9596
BOOST_ASSERT_MSG(SHA1_LEN == hasher->output_length(), "Bad hash length");
96-
hasher->update(Botan::BigInt::encode(N));
97-
hasher->update(Botan::BigInt::encode_1363(g, N.bytes()));
97+
std::vector<uint8_t> n_buf(N.bytes(), 0); // 1363 style padding
98+
N.serialize_to({ n_buf.data() + n_buf.size() - N.bytes(), N.bytes() });
99+
std::vector<uint8_t> g_buf(N.bytes(), 0); // 1363 style padding
100+
g.serialize_to({ g_buf.data() + g_buf.size() - g.bytes(), g.bytes() });
101+
hasher->update(n_buf.data(), n_buf.size());
102+
hasher->update(g_buf.data(), g_buf.size());
98103
hasher->final(hash.data());
99-
return Botan::BigInt::decode(hash.data(), hash.size());
104+
return Botan::BigInt::from_bytes(std::span<const uint8_t>(hash.data(), hash.size()));
100105
}
101106

102107
Botan::BigInt compute_x(std::string_view identifier, std::string_view password,
@@ -123,7 +128,7 @@ Botan::BigInt compute_x(std::string_view identifier, std::string_view password,
123128
hasher->final(hash.data());
124129

125130
if(mode == Compliance::RFC5054) {
126-
return Botan::BigInt::decode(hash.data(), hash.size());
131+
return Botan::BigInt::from_bytes(std::span<const uint8_t>(hash.data(), hash.size()));
127132
} else {
128133
return detail::decode_flip(hash);
129134
}
@@ -194,4 +199,4 @@ Botan::BigInt generate_verifier(std::string_view identifier, std::string_view pa
194199
return detail::generate(identifier, password, generator, salt, mode);
195200
}
196201

197-
} // srp6, ember
202+
} // srp6, ember

src/login/AccountClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ void AccountClient::handle_locate_response(std::expected<const SessionResponse*,
9090
return;
9191
}
9292

93-
auto key = Botan::BigInt::decode(msg->key()->data(), msg->key()->size());
93+
auto key = Botan::BigInt::from_bytes(std::span<const std::uint8_t>(msg->key()->data(), msg->key()->size()));
9494
cb(msg->status(), std::move(key));
9595
}
9696

97-
} // ember
97+
} // ember

src/login/Authenticator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ReconnectAuthenticator::ReconnectAuthenticator(utf8_string username,
5353
: username_(std::move(username)) {
5454
std::ranges::copy(salt, salt_.data());
5555
sess_key_.t.resize(session_key.bytes());
56-
session_key.binary_encode(sess_key_.t.data(), sess_key_.t.size());
56+
session_key.serialize_to(std::span<uint8_t>(sess_key_.t.data() + (sess_key_.t.size() - session_key.bytes()), session_key.bytes()));
5757
}
5858

5959
bool ReconnectAuthenticator::proof_check(std::span<const std::uint8_t> salt,
@@ -69,4 +69,4 @@ bool ReconnectAuthenticator::proof_check(std::span<const std::uint8_t> salt,
6969
return std::ranges::equal(res, proof);
7070
}
7171

72-
} // ember
72+
} // ember

0 commit comments

Comments
 (0)