Commit 244d7cd8 authored by jan.koester's avatar jan.koester
Browse files

mem optimize

parent b7da4e00
Loading
Loading
Loading
Loading
+164 −253

File changed.

Preview size limit exceeded, changes collapsed.

+63 −9
Changes for src/socket.h: 63 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -729,6 +729,44 @@ namespace netplus {
		void pumpIncoming();

	private:
		// Current encryption level for frame processing. Declared here
		// (rather than down with the other crypto state) because it's a
		// parameter type for protectPacket()/unprotectPacket() below, and a
		// nested type must be declared before it's named in a member
		// function's parameter list.
		enum class EncryptionLevel { Initial, Handshake, Application };

		// Key material + resolved ciphers for one direction (send or recv)
		// at one encryption level. Filled once, in deriveInitialKeys()/
		// deriveHandshakeKeys()/deriveApplicationKeys(), using _is_server —
		// every other call site asks for `.send` (packets we originate) or
		// `.recv` (packets we decrypt from the peer) and never branches on
		// _is_server again. Reused for the Initial level too even though
		// Initial is always AES-128 (RFC 9001 §5.2): key/hp then only fill
		// the first 16 bytes and leave the rest zeroed — the same zero-pad
		// convention Handshake/Application already use for the
		// AES-128-negotiated case, not a new one.
		struct LevelKeyMaterial {
			uint8_t key[32] = {0};
			uint8_t iv[12]  = {0};
			uint8_t hp[32]  = {0};
			std::unique_ptr<aes> aes_gcm;  // payload AEAD cipher, this direction
			std::unique_ptr<aes> aes_hp;   // header-protection ECB cipher, this direction

			void zero() {
				std::memset(key, 0, sizeof(key));
				std::memset(iv,  0, sizeof(iv));
				std::memset(hp,  0, sizeof(hp));
				aes_gcm.reset();
				aes_hp.reset();
			}
		};
		struct LevelKeys {
			LevelKeyMaterial send;
			LevelKeyMaterial recv;
			void zero() { send.zero(); recv.zero(); }
		};

		// Packet processing
		void processIncomingPacket(const uint8_t* data, size_t len);
		void processLongHeaderPacket(const uint8_t* data, size_t len);
@@ -785,21 +823,28 @@ namespace netplus {
		void deriveHandshakeKeys(const std::vector<uint8_t>& shared_secret);
		void deriveApplicationKeys();

		// `mat` is the already-role-resolved key material for this call —
		// the send-direction struct (e.g. _app_keys.send) to protect/apply
		// HP, or the recv-direction struct (e.g. _app_keys.recv) to
		// unprotect — so these functions never branch on _is_server or
		// re-derive "which cached cipher" themselves; the caller's choice
		// of which LevelKeyMaterial to pass in *is* that decision. `level`
		// is needed only for the defensive fallback that builds an ephemeral
		// cipher when mat.aes_gcm/aes_hp is unexpectedly null (Initial is
		// always AES-128; Handshake/Application depend on _selected_cipher).
		std::vector<uint8_t> protectPacket(const std::vector<uint8_t>& header,
		                                   const std::vector<uint8_t>& payload,
		                                   uint64_t packet_number,
		                                   const uint8_t* key, const uint8_t* iv,
		                                   bool force_aes128 = false);
		                                   EncryptionLevel level, LevelKeyMaterial& mat);
		bool unprotectPacket(const uint8_t* data, size_t len,
		                     std::vector<uint8_t>& header,
		                     std::vector<uint8_t>& payload,
		                     uint64_t& packet_number,
		                     const uint8_t* key, const uint8_t* iv,
		                     const uint8_t* hp_key, bool force_aes128 = false);
		                     EncryptionLevel level, LevelKeyMaterial& mat);

		// Header protection
		void applyHeaderProtection(std::vector<uint8_t>& packet, const uint8_t* hp_key, bool force_aes128 = false);
		void removeHeaderProtection(std::vector<uint8_t>& packet, const uint8_t* hp_key, bool force_aes128 = false);
		// Header protection (send direction only — decrypt-side header
		// protection removal is handled inline inside unprotectPacket()).
		void applyHeaderProtection(std::vector<uint8_t>& packet, EncryptionLevel level, LevelKeyMaterial& mat);

		// Variable-length integer encoding/decoding (RFC 9000 Section 16).
		static size_t encodeVarInt(uint64_t value, uint8_t* out);
@@ -924,6 +969,15 @@ namespace netplus {
		uint8_t _app_iv_server[12] = {0};
		uint8_t _app_hp_server[32] = {0};

		// Consolidated key material (send/recv per level) — see
		// LevelKeyMaterial's comment above. Populated alongside the flat
		// arrays above during the migration to this scheme; the flat arrays
		// and the _aes_* unique_ptrs further below are removed once every
		// call site has moved over to these.
		LevelKeys _initial_keys;
		LevelKeys _hs_keys;
		LevelKeys _app_keys;

		// TLS handshake traffic secrets (for computing Finished)
		std::vector<uint8_t> _c_hs_traffic_secret;
		std::vector<uint8_t> _s_hs_traffic_secret;
@@ -957,8 +1011,8 @@ namespace netplus {
		uint64_t _crypto_offset_handshake = 0;
		uint64_t _crypto_offset_app = 0;

		// Current encryption level for frame processing
		enum class EncryptionLevel { Initial, Handshake, Application };
		// Current encryption level for frame processing (EncryptionLevel
		// itself is declared near the top of the private section, above).
		EncryptionLevel _current_enc_level = EncryptionLevel::Initial;

		// Streams
+1 −0
Changes for test/Testing/Temporary/CTestCostData.txt: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
---