Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
keys.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
6
7#include <algorithm>
8#include <cstring>
9
11{
12
14{
15 switch (level)
16 {
18 return "Initial";
20 return "Handshake";
22 return "0-RTT";
24 return "Application";
25 default:
26 return "Unknown";
27 }
28}
29
30auto quic_keys::is_valid() const noexcept -> bool
31{
32 // Check if key is non-zero
33 return std::any_of(key.begin(), key.end(), [](uint8_t b) { return b != 0; });
34}
35
36void quic_keys::clear() noexcept
37{
38 // Use volatile to prevent compiler optimization
39 volatile uint8_t* p;
40
41 p = secret.data();
42 std::memset(const_cast<uint8_t*>(p), 0, secret.size());
43
44 p = key.data();
45 std::memset(const_cast<uint8_t*>(p), 0, key.size());
46
47 p = iv.data();
48 std::memset(const_cast<uint8_t*>(p), 0, iv.size());
49
50 p = hp_key.data();
51 std::memset(const_cast<uint8_t*>(p), 0, hp_key.size());
52}
53
54auto quic_keys::operator==(const quic_keys& other) const noexcept -> bool
55{
56 return secret == other.secret && key == other.key && iv == other.iv &&
57 hp_key == other.hp_key;
58}
59
60auto quic_keys::operator!=(const quic_keys& other) const noexcept -> bool
61{
62 return !(*this == other);
63}
64
65auto key_pair::is_valid() const noexcept -> bool
66{
67 return read.is_valid() && write.is_valid();
68}
69
70void key_pair::clear() noexcept
71{
72 read.clear();
73 write.clear();
74}
75
76} // namespace kcenon::network::protocols::quic
auto encryption_level_to_string(encryption_level level) -> std::string
Convert encryption level to string for debugging.
Definition keys.cpp:13
encryption_level
QUIC encryption levels (RFC 9001 Section 4)
Definition keys.h:54
@ application
1-RTT application data encryption
@ initial
Initial encryption (derived from DCID)
quic_keys read
Keys for decrypting received packets.
Definition keys.h:135
auto is_valid() const noexcept -> bool
Check if both read and write keys are valid.
Definition keys.cpp:65
quic_keys write
Keys for encrypting outgoing packets.
Definition keys.h:136
void clear() noexcept
Clear all key material securely.
Definition keys.cpp:70
QUIC encryption keys for a single encryption level (RFC 9001 Section 5)
Definition keys.h:92
auto is_valid() const noexcept -> bool
Check if keys are initialized (non-zero)
Definition keys.cpp:30
std::array< uint8_t, hp_key_size > hp_key
Header protection key.
Definition keys.h:103
void clear() noexcept
Clear all key material securely.
Definition keys.cpp:36
std::array< uint8_t, aes_128_key_size > key
AEAD encryption key (AES-128-GCM by default)
Definition keys.h:97
auto operator!=(const quic_keys &other) const noexcept -> bool
Inequality comparison.
Definition keys.cpp:60
auto operator==(const quic_keys &other) const noexcept -> bool
Equality comparison.
Definition keys.cpp:54
std::array< uint8_t, secret_size > secret
Traffic secret (used for key updates)
Definition keys.h:94
std::array< uint8_t, aead_iv_size > iv
AEAD initialization vector.
Definition keys.h:100