Skip to content

Commit

Permalink
refactor libdave naming
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 5, 2024
1 parent 46e048f commit 1dc8e50
Show file tree
Hide file tree
Showing 23 changed files with 153 additions and 156 deletions.
2 changes: 1 addition & 1 deletion src/dpp/dave/cipher_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace dpp::dave {

std::unique_ptr<cipher_interface> create_cipher(const EncryptionKey& encryptionKey)
std::unique_ptr<cipher_interface> create_cipher(const encryption_key& encryptionKey)
{
auto cipher = std::make_unique<openssl_aead_cipher>(encryptionKey);
return cipher->is_valid() ? std::move(cipher) : nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/dpp/dave/cipher_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class cipher_interface { // NOLINT
virtual bool decrypt(byte_view plaintextBufferOut, const_byte_view ciphertextBuffer, const_byte_view tagBuffer, const_byte_view nonceBuffer, const_byte_view additionalData) = 0;
};

std::unique_ptr<cipher_interface> create_cipher(const EncryptionKey& encryptionKey);
std::unique_ptr<cipher_interface> create_cipher(const encryption_key& encryptionKey);

} // namespace dpp::dave

2 changes: 1 addition & 1 deletion src/dpp/dave/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class clock_interface {
virtual time_point now() const = 0;
};

class Clock : public clock_interface {
class clock : public clock_interface {
public:
time_point now() const override { return base_clock::now(); }
};
Expand Down
4 changes: 2 additions & 2 deletions src/dpp/dave/codec_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

namespace dpp::dave::codec_utils {

UnencryptedFrameHeaderSize BytesCoveringH264PPS(const uint8_t* payload,
const uint64_t sizeRemaining)
unencrypted_frame_header_size BytesCoveringH264PPS(const uint8_t* payload,
const uint64_t sizeRemaining)
{
// the payload starts with three exponential golomb encoded values
// (first_mb_in_slice, sps_id, pps_id)
Expand Down
30 changes: 13 additions & 17 deletions src/dpp/dave/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ namespace mlspp::bytes_ns {

namespace dpp::dave {

using UnencryptedFrameHeaderSize = uint16_t;
using TruncatedSyncNonce = uint32_t;
using MagicMarker = uint16_t;
using EncryptionKey = ::mlspp::bytes_ns::bytes;
using TransitionId = uint16_t;
using SupplementalBytesSize = uint8_t;
using unencrypted_frame_header_size = uint16_t;
using truncated_sync_nonce = uint32_t;
using magic_marker = uint16_t;
using encryption_key = ::mlspp::bytes_ns::bytes;
using transition_id = uint16_t;
using supplemental_bytes_size = uint8_t;

enum media_type : uint8_t { media_audio, media_video };
enum Codec : uint8_t { Unknown, Opus, VP8, VP9, H264, H265, AV1 };
Expand All @@ -59,25 +59,22 @@ struct ignored_t {};
// Map of ID-key pairs.
// In ProcessCommit, this lists IDs whose keys have been added, changed, or removed;
// an empty value value means a key was removed.
using RosterMap = std::map<uint64_t, std::vector<uint8_t>>;
using roster_map = std::map<uint64_t, std::vector<uint8_t>>;

// Return type for functions producing RosterMap or hard or soft failures
using RosterVariant = std::variant<failed_t, ignored_t, RosterMap>;
using roster_variant = std::variant<failed_t, ignored_t, roster_map>;

constexpr MagicMarker kMarkerBytes = 0xFAFA;
constexpr magic_marker kMarkerBytes = 0xFAFA;

// Layout constants
constexpr size_t kAesGcm128KeyBytes = 16;
constexpr size_t kAesGcm128NonceBytes = 12;
constexpr size_t kAesGcm128TruncatedSyncNonceBytes = 4;
constexpr size_t kAesGcm128TruncatedSyncNonceOffset =
kAesGcm128NonceBytes - kAesGcm128TruncatedSyncNonceBytes;
constexpr size_t kAesGcm128TruncatedSyncNonceOffset = kAesGcm128NonceBytes - kAesGcm128TruncatedSyncNonceBytes;
constexpr size_t kAesGcm128TruncatedTagBytes = 8;
constexpr size_t kRatchetGenerationBytes = 1;
constexpr size_t kRatchetGenerationShiftBits =
8 * (kAesGcm128TruncatedSyncNonceBytes - kRatchetGenerationBytes);
constexpr size_t kSupplementalBytes =
kAesGcm128TruncatedTagBytes + sizeof(SupplementalBytesSize) + sizeof(MagicMarker);
constexpr size_t kRatchetGenerationShiftBits = 8 * (kAesGcm128TruncatedSyncNonceBytes - kRatchetGenerationBytes);
constexpr size_t kSupplementalBytes = kAesGcm128TruncatedTagBytes + sizeof(supplemental_bytes_size) + sizeof(magic_marker);
constexpr size_t kTransformPaddingBytes = 64;

// Timing constants
Expand All @@ -94,8 +91,7 @@ constexpr auto kMaxFramesPerSecond = 50 + 2 * 60; // 50 audio frames + 2 * 60fps
constexpr std::array<uint8_t, 3> kOpusSilencePacket = {0xF8, 0xFF, 0xFE};

// Utility routine for variant return types
template <class T, class V>
inline std::optional<T> GetOptional(V&& variant)
template <class T, class V> inline std::optional<T> get_optional(V&& variant)
{
if (auto map = std::get_if<T>(&variant)) {
if constexpr (std::is_rvalue_reference_v<decltype(variant)>) {
Expand Down
20 changes: 10 additions & 10 deletions src/dpp/dave/cryptor_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ KeyGeneration compute_wrapped_generation(KeyGeneration oldest, KeyGeneration gen
return factor * kGenerationWrap + generation;
}

BigNonce compute_wrapped_big_nonce(KeyGeneration generation, TruncatedSyncNonce nonce)
big_nonce compute_wrapped_big_nonce(KeyGeneration generation, truncated_sync_nonce nonce)
{
// Remove the generation bits from the nonce
auto maskedNonce = nonce & ((1 << kRatchetGenerationShiftBits) - 1);
// Add the wrapped generation bits back in
return static_cast<BigNonce>(generation) << kRatchetGenerationShiftBits | maskedNonce;
return static_cast<big_nonce>(generation) << kRatchetGenerationShiftBits | maskedNonce;
}

aead_cipher_manager::aead_cipher_manager(const clock_interface& clock, std::unique_ptr<IKeyRatchet> keyRatchet)
Expand All @@ -60,7 +60,7 @@ aead_cipher_manager::aead_cipher_manager(const clock_interface& clock, std::uniq
{
}

bool aead_cipher_manager::CanProcessNonce(KeyGeneration generation, TruncatedSyncNonce nonce) const
bool aead_cipher_manager::can_process_nonce(KeyGeneration generation, truncated_sync_nonce nonce) const
{
if (!newestProcessedNonce_) {
return true;
Expand All @@ -71,9 +71,9 @@ bool aead_cipher_manager::CanProcessNonce(KeyGeneration generation, TruncatedSyn
std::find(missingNonces_.rbegin(), missingNonces_.rend(), bigNonce) != missingNonces_.rend();
}

cipher_interface* aead_cipher_manager::GetCryptor(KeyGeneration generation)
cipher_interface* aead_cipher_manager::get_cipher(KeyGeneration generation)
{
CleanupExpiredCryptors();
cleanup_expired_ciphers();

if (generation < oldestGeneration_) {
DISCORD_LOG(LS_INFO) << "Received frame with old generation: " << generation
Expand Down Expand Up @@ -102,15 +102,15 @@ cipher_interface* aead_cipher_manager::GetCryptor(KeyGeneration generation)
auto it = cryptors_.find(generation);
if (it == cryptors_.end()) {
// We don't have a cryptor for this generation, create one
std::tie(it, std::ignore) = cryptors_.emplace(generation, MakeExpiringCryptor(generation));
std::tie(it, std::ignore) = cryptors_.emplace(generation, make_expiring_cipher(generation));
}

// Return a non-owning pointer to the cryptor
auto& [cryptor, expiry] = it->second;
return cryptor.get();
}

void aead_cipher_manager::ReportCryptorSuccess(KeyGeneration generation, TruncatedSyncNonce nonce)
void aead_cipher_manager::report_cipher_success(KeyGeneration generation, truncated_sync_nonce nonce)
{
auto bigNonce = compute_wrapped_big_nonce(generation, nonce);

Expand Down Expand Up @@ -157,12 +157,12 @@ void aead_cipher_manager::ReportCryptorSuccess(KeyGeneration generation, Truncat
}
}

KeyGeneration aead_cipher_manager::ComputeWrappedGeneration(KeyGeneration generation)
KeyGeneration aead_cipher_manager::compute_wrapped_generation(KeyGeneration generation)
{
return ::dpp::dave::compute_wrapped_generation(oldestGeneration_, generation);
}

aead_cipher_manager::ExpiringCryptor aead_cipher_manager::MakeExpiringCryptor(KeyGeneration generation)
aead_cipher_manager::expiring_cipher aead_cipher_manager::make_expiring_cipher(KeyGeneration generation)
{
// Get the new key from the ratchet
auto encryptionKey = keyRatchet_->GetKey(generation);
Expand All @@ -182,7 +182,7 @@ aead_cipher_manager::ExpiringCryptor aead_cipher_manager::MakeExpiringCryptor(Ke
return {create_cipher(encryptionKey), expiryTime};
}

void aead_cipher_manager::CleanupExpiredCryptors()
void aead_cipher_manager::cleanup_expired_ciphers()
{
for (auto it = cryptors_.begin(); it != cryptors_.end();) {
auto& [generation, cryptor] = *it;
Expand Down
28 changes: 14 additions & 14 deletions src/dpp/dave/cryptor_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,44 @@ namespace dpp::dave {

KeyGeneration compute_wrapped_generation(KeyGeneration oldest, KeyGeneration generation);

using BigNonce = uint64_t;
BigNonce compute_wrapped_big_nonce(KeyGeneration generation, TruncatedSyncNonce nonce);
using big_nonce = uint64_t;
big_nonce compute_wrapped_big_nonce(KeyGeneration generation, truncated_sync_nonce nonce);

class aead_cipher_manager {
public:
using time_point = typename clock_interface::time_point;

aead_cipher_manager(const clock_interface& clock, std::unique_ptr<IKeyRatchet> keyRatchet);

void UpdateExpiry(time_point expiry) { ratchetExpiry_ = expiry; }
bool IsExpired() const { return clock_.now() > ratchetExpiry_; }
void update_expiry(time_point expiry) { ratchetExpiry_ = expiry; }
bool is_expired() const { return clock_.now() > ratchetExpiry_; }

bool CanProcessNonce(KeyGeneration generation, TruncatedSyncNonce nonce) const;
KeyGeneration ComputeWrappedGeneration(KeyGeneration generation);
bool can_process_nonce(KeyGeneration generation, truncated_sync_nonce nonce) const;
KeyGeneration compute_wrapped_generation(KeyGeneration generation);

cipher_interface* GetCryptor(KeyGeneration generation);
void ReportCryptorSuccess(KeyGeneration generation, TruncatedSyncNonce nonce);
cipher_interface* get_cipher(KeyGeneration generation);
void report_cipher_success(KeyGeneration generation, truncated_sync_nonce nonce);

private:
struct ExpiringCryptor {
struct expiring_cipher {
std::unique_ptr<cipher_interface> cryptor;
time_point expiry;
};

ExpiringCryptor MakeExpiringCryptor(KeyGeneration generation);
void CleanupExpiredCryptors();
expiring_cipher make_expiring_cipher(KeyGeneration generation);
void cleanup_expired_ciphers();

const clock_interface& clock_;
std::unique_ptr<IKeyRatchet> keyRatchet_;
std::unordered_map<KeyGeneration, ExpiringCryptor> cryptors_;
std::unordered_map<KeyGeneration, expiring_cipher> cryptors_;

time_point ratchetCreation_;
time_point ratchetExpiry_;
KeyGeneration oldestGeneration_{0};
KeyGeneration newestGeneration_{0};

std::optional<BigNonce> newestProcessedNonce_;
std::deque<BigNonce> missingNonces_;
std::optional<big_nonce> newestProcessedNonce_;
std::deque<big_nonce> missingNonces_;
};

} // namespace dpp::dave
Expand Down
Loading

0 comments on commit 1dc8e50

Please sign in to comment.