fix crypto: secure zeroization of handshake state, ephemeral keys,

derived keys
This commit is contained in:
2026-02-11 14:20:16 -03:00
parent 4770fad51c
commit e45bffbd8b
7 changed files with 195 additions and 241 deletions

View File

@@ -22,6 +22,14 @@ const Role = noise.Role;
/// Maximum message size for Noise protocol
pub const MAX_MESSAGE_SIZE = noise.MAX_MESSAGE_SIZE;
/// Volatile secure zeroing (prevents compiler from eliding the write)
fn secureZeroBytes(bytes: []u8) void {
const volatile_ptr: [*]volatile u8 = @ptrCast(bytes.ptr);
for (0..bytes.len) |i| {
volatile_ptr[i] = 0;
}
}
/// Authentication tag size
pub const TAG_SIZE = noise.TAG_SIZE;
@@ -119,6 +127,9 @@ pub const SecureConnection = struct {
self.transport = hs.split() catch return Error.HandshakeFailed;
self.remote_static_key = hs.rs;
self.handshake_complete = true;
// Zeroize handshake state (ephemeral keys, symmetric state)
secureZeroBytes(std.mem.asBytes(&hs));
}
/// Perform handshake as responder (server accepting connection)
@@ -145,6 +156,9 @@ pub const SecureConnection = struct {
self.transport = hs.split() catch return Error.HandshakeFailed;
self.remote_static_key = hs.rs;
self.handshake_complete = true;
// Zeroize handshake state (ephemeral keys, symmetric state)
secureZeroBytes(std.mem.asBytes(&hs));
}
/// Send encrypted data (blocking - will sleep on WouldBlock)
@@ -333,19 +347,20 @@ pub const SecureConnection = struct {
// Private helpers for raw socket I/O during handshake
// =========================================================================
/// Absolute wall-clock timeout for each handshake send/receive operation (nanoseconds)
const HANDSHAKE_TIMEOUT_NS: i128 = 5 * std.time.ns_per_s;
/// Send raw data (unencrypted, for handshake)
fn sendRaw(self: *Self, data: []const u8) Error!void {
var sent: usize = 0;
var retries: u32 = 0;
const max_retries: u32 = 500; // 5 seconds with 10ms sleep
const deadline = std.time.nanoTimestamp() + HANDSHAKE_TIMEOUT_NS;
while (sent < data.len) {
const bytes = self.socket.send(data[sent..]) catch |err| {
switch (err) {
CoreError.ConnectionTimeout => {
// WouldBlock - retry
retries += 1;
if (retries > max_retries) return Error.HandshakeTimeout;
// WouldBlock - check absolute deadline
if (std.time.nanoTimestamp() >= deadline) return Error.HandshakeTimeout;
std.Thread.sleep(10 * std.time.ns_per_ms);
continue;
},
@@ -353,7 +368,6 @@ pub const SecureConnection = struct {
}
};
sent += bytes;
retries = 0;
}
}
@@ -361,16 +375,14 @@ pub const SecureConnection = struct {
/// Returns actual bytes received
fn receiveRaw(self: *Self, buffer: []u8, min_bytes: usize) Error!usize {
var received: usize = 0;
var retries: u32 = 0;
const max_retries: u32 = 500; // 5 seconds with 10ms sleep
const deadline = std.time.nanoTimestamp() + HANDSHAKE_TIMEOUT_NS;
while (received < min_bytes) {
const bytes = self.socket.receive(buffer[received..]) catch |err| {
switch (err) {
CoreError.ConnectionTimeout => {
// WouldBlock - retry
retries += 1;
if (retries > max_retries) return Error.HandshakeTimeout;
// WouldBlock - check absolute deadline
if (std.time.nanoTimestamp() >= deadline) return Error.HandshakeTimeout;
std.Thread.sleep(10 * std.time.ns_per_ms);
continue;
},
@@ -379,7 +391,6 @@ pub const SecureConnection = struct {
}
};
received += bytes;
retries = 0;
}
return received;