ab ovo
This commit is contained in:
206
src/noise/cipher.zig
Normal file
206
src/noise/cipher.zig
Normal file
@@ -0,0 +1,206 @@
|
||||
//! CipherState for Noise Protocol
|
||||
//!
|
||||
//! Manages encryption/decryption using ChaCha20-Poly1305.
|
||||
//! Handles key and nonce management as per Noise spec.
|
||||
|
||||
const std = @import("std");
|
||||
const ChaCha20Poly1305 = std.crypto.aead.chacha_poly.ChaCha20Poly1305;
|
||||
|
||||
const noise = @import("noise.zig");
|
||||
const Error = noise.Error;
|
||||
|
||||
/// CipherState manages symmetric encryption for Noise protocol.
|
||||
/// Uses ChaCha20-Poly1305 AEAD with a 64-bit nonce counter.
|
||||
pub const CipherState = struct {
|
||||
/// Symmetric key (32 bytes), null if not initialized
|
||||
k: ?[32]u8,
|
||||
/// Nonce counter (incremented after each encryption)
|
||||
n: u64,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
/// Initialize an empty CipherState (no key set)
|
||||
pub fn init() Self {
|
||||
return Self{
|
||||
.k = null,
|
||||
.n = 0,
|
||||
};
|
||||
}
|
||||
|
||||
/// Initialize CipherState with a key
|
||||
pub fn initWithKey(key: [32]u8) Self {
|
||||
return Self{
|
||||
.k = key,
|
||||
.n = 0,
|
||||
};
|
||||
}
|
||||
|
||||
/// Check if CipherState has a key
|
||||
pub fn hasKey(self: *const Self) bool {
|
||||
return self.k != null;
|
||||
}
|
||||
|
||||
/// Set the key
|
||||
pub fn setKey(self: *Self, key: [32]u8) void {
|
||||
self.k = key;
|
||||
self.n = 0;
|
||||
}
|
||||
|
||||
/// Encrypt plaintext with associated data.
|
||||
/// Returns ciphertext (plaintext.len + 16 bytes for tag).
|
||||
pub fn encryptWithAd(
|
||||
self: *Self,
|
||||
ad: []const u8,
|
||||
plaintext: []const u8,
|
||||
out: []u8,
|
||||
) Error![]u8 {
|
||||
if (self.k == null) {
|
||||
// If no key, return plaintext unchanged (as per Noise spec)
|
||||
if (out.len < plaintext.len) return Error.BufferTooSmall;
|
||||
@memcpy(out[0..plaintext.len], plaintext);
|
||||
return out[0..plaintext.len];
|
||||
}
|
||||
|
||||
if (out.len < plaintext.len + 16) return Error.BufferTooSmall;
|
||||
if (self.n == std.math.maxInt(u64)) return Error.NonceOverflow;
|
||||
|
||||
// Build 12-byte nonce: 4 zero bytes + 8-byte little-endian counter
|
||||
var nonce: [12]u8 = undefined;
|
||||
@memset(nonce[0..4], 0);
|
||||
std.mem.writeInt(u64, nonce[4..12], self.n, .little);
|
||||
|
||||
// Encrypt
|
||||
var tag: [16]u8 = undefined;
|
||||
ChaCha20Poly1305.encrypt(
|
||||
out[0..plaintext.len],
|
||||
&tag,
|
||||
plaintext,
|
||||
ad,
|
||||
nonce,
|
||||
self.k.?,
|
||||
);
|
||||
|
||||
// Append tag
|
||||
@memcpy(out[plaintext.len..][0..16], &tag);
|
||||
|
||||
// Increment nonce
|
||||
self.n += 1;
|
||||
|
||||
return out[0 .. plaintext.len + 16];
|
||||
}
|
||||
|
||||
/// Decrypt ciphertext with associated data.
|
||||
/// Ciphertext includes 16-byte authentication tag at the end.
|
||||
/// Returns plaintext (ciphertext.len - 16 bytes).
|
||||
pub fn decryptWithAd(
|
||||
self: *Self,
|
||||
ad: []const u8,
|
||||
ciphertext: []const u8,
|
||||
out: []u8,
|
||||
) Error![]u8 {
|
||||
if (self.k == null) {
|
||||
// If no key, return ciphertext unchanged (as per Noise spec)
|
||||
if (out.len < ciphertext.len) return Error.BufferTooSmall;
|
||||
@memcpy(out[0..ciphertext.len], ciphertext);
|
||||
return out[0..ciphertext.len];
|
||||
}
|
||||
|
||||
if (ciphertext.len < 16) return Error.DecryptionFailed;
|
||||
const plaintext_len = ciphertext.len - 16;
|
||||
if (out.len < plaintext_len) return Error.BufferTooSmall;
|
||||
if (self.n == std.math.maxInt(u64)) return Error.NonceOverflow;
|
||||
|
||||
// Build 12-byte nonce
|
||||
var nonce: [12]u8 = undefined;
|
||||
@memset(nonce[0..4], 0);
|
||||
std.mem.writeInt(u64, nonce[4..12], self.n, .little);
|
||||
|
||||
// Extract tag
|
||||
var tag: [16]u8 = undefined;
|
||||
@memcpy(&tag, ciphertext[plaintext_len..][0..16]);
|
||||
|
||||
// Decrypt
|
||||
ChaCha20Poly1305.decrypt(
|
||||
out[0..plaintext_len],
|
||||
ciphertext[0..plaintext_len],
|
||||
tag,
|
||||
ad,
|
||||
nonce,
|
||||
self.k.?,
|
||||
) catch return Error.DecryptionFailed;
|
||||
|
||||
// Increment nonce
|
||||
self.n += 1;
|
||||
|
||||
return out[0..plaintext_len];
|
||||
}
|
||||
|
||||
/// Rekey the cipher state (as per Noise spec section 4.2)
|
||||
pub fn rekey(self: *Self) void {
|
||||
if (self.k == null) return;
|
||||
|
||||
// Encrypt 32 zero bytes with maxint nonce to get new key
|
||||
var zeros: [32]u8 = [_]u8{0} ** 32;
|
||||
var new_key: [48]u8 = undefined; // 32 + 16 tag
|
||||
var nonce: [12]u8 = undefined;
|
||||
@memset(&nonce, 0xFF); // max nonce for rekey
|
||||
|
||||
var tag: [16]u8 = undefined;
|
||||
ChaCha20Poly1305.encrypt(
|
||||
new_key[0..32],
|
||||
&tag,
|
||||
&zeros,
|
||||
&.{},
|
||||
nonce,
|
||||
self.k.?,
|
||||
);
|
||||
|
||||
self.k = new_key[0..32].*;
|
||||
}
|
||||
};
|
||||
|
||||
test "CipherState encrypt/decrypt" {
|
||||
var cs = CipherState.initWithKey([_]u8{0x42} ** 32);
|
||||
|
||||
const plaintext = "Hello, Noise!";
|
||||
const ad = "additional data";
|
||||
|
||||
var ciphertext: [64]u8 = undefined;
|
||||
const encrypted = try cs.encryptWithAd(ad, plaintext, &ciphertext);
|
||||
|
||||
// Reset nonce for decryption (normally would use different CipherState)
|
||||
cs.n = 0;
|
||||
|
||||
var decrypted: [64]u8 = undefined;
|
||||
const result = try cs.decryptWithAd(ad, encrypted, &decrypted);
|
||||
|
||||
try std.testing.expectEqualStrings(plaintext, result);
|
||||
}
|
||||
|
||||
test "CipherState no key passthrough" {
|
||||
var cs = CipherState.init();
|
||||
|
||||
const plaintext = "Hello, Noise!";
|
||||
var output: [64]u8 = undefined;
|
||||
|
||||
const result = try cs.encryptWithAd(&.{}, plaintext, &output);
|
||||
try std.testing.expectEqualStrings(plaintext, result);
|
||||
}
|
||||
|
||||
test "CipherState wrong tag fails" {
|
||||
var cs = CipherState.initWithKey([_]u8{0x42} ** 32);
|
||||
|
||||
const plaintext = "Hello, Noise!";
|
||||
var ciphertext: [64]u8 = undefined;
|
||||
const encrypted = try cs.encryptWithAd(&.{}, plaintext, &ciphertext);
|
||||
|
||||
// Corrupt the tag
|
||||
var corrupted: [64]u8 = undefined;
|
||||
@memcpy(corrupted[0..encrypted.len], encrypted);
|
||||
corrupted[encrypted.len - 1] ^= 0xFF;
|
||||
|
||||
cs.n = 0;
|
||||
var decrypted: [64]u8 = undefined;
|
||||
const result = cs.decryptWithAd(&.{}, corrupted[0..encrypted.len], &decrypted);
|
||||
try std.testing.expectError(Error.DecryptionFailed, result);
|
||||
}
|
||||
113
src/noise/dh.zig
Normal file
113
src/noise/dh.zig
Normal file
@@ -0,0 +1,113 @@
|
||||
//! Diffie-Hellman functions for Noise Protocol
|
||||
//!
|
||||
//! Uses X25519 (Curve25519) for key exchange.
|
||||
//! Designed by djb with transparent "nothing up my sleeve" parameters.
|
||||
|
||||
const std = @import("std");
|
||||
const X25519 = std.crypto.dh.X25519;
|
||||
|
||||
const noise = @import("noise.zig");
|
||||
const Error = noise.Error;
|
||||
|
||||
/// Size of DH public and private keys
|
||||
pub const DH_LEN: usize = 32;
|
||||
|
||||
/// A Diffie-Hellman key pair
|
||||
pub const KeyPair = struct {
|
||||
/// Private key (secret)
|
||||
private: [DH_LEN]u8,
|
||||
/// Public key
|
||||
public: [DH_LEN]u8,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
/// Generate a new random key pair
|
||||
pub fn generate() Self {
|
||||
const seed = X25519.KeyPair.generate();
|
||||
return Self{
|
||||
.private = seed.secret_key,
|
||||
.public = seed.public_key,
|
||||
};
|
||||
}
|
||||
|
||||
/// Create key pair from private key bytes
|
||||
/// Returns error if the private key is invalid (e.g., all zeros which produces weak output)
|
||||
pub fn fromPrivate(private: [DH_LEN]u8) Error!Self {
|
||||
const public = X25519.recoverPublicKey(private) catch return Error.InvalidPublicKey;
|
||||
return Self{
|
||||
.private = private,
|
||||
.public = public,
|
||||
};
|
||||
}
|
||||
|
||||
/// Create an empty key pair (for ephemeral before generation)
|
||||
pub fn empty() Self {
|
||||
return Self{
|
||||
.private = [_]u8{0} ** DH_LEN,
|
||||
.public = [_]u8{0} ** DH_LEN,
|
||||
};
|
||||
}
|
||||
|
||||
/// Check if key pair is empty/uninitialized
|
||||
pub fn isEmpty(self: *const Self) bool {
|
||||
const zeros = [_]u8{0} ** DH_LEN;
|
||||
return std.mem.eql(u8, &self.public, &zeros);
|
||||
}
|
||||
};
|
||||
|
||||
/// Perform Diffie-Hellman key exchange
|
||||
/// Returns shared secret (32 bytes)
|
||||
pub fn dh(keypair: KeyPair, public_key: [DH_LEN]u8) Error![DH_LEN]u8 {
|
||||
return X25519.scalarmult(keypair.private, public_key) catch return Error.InvalidPublicKey;
|
||||
}
|
||||
|
||||
/// Validate a public key
|
||||
/// Returns true if the key is valid for X25519
|
||||
pub fn isValidPublicKey(public_key: [DH_LEN]u8) bool {
|
||||
// Check for all-zero key (invalid)
|
||||
const zeros = [_]u8{0} ** DH_LEN;
|
||||
if (std.mem.eql(u8, &public_key, &zeros)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// X25519 accepts most 32-byte values, but we do a trial multiply
|
||||
// to verify it's usable
|
||||
const test_scalar = [_]u8{9} ** DH_LEN; // Just a test value
|
||||
_ = X25519.scalarmult(test_scalar, public_key) catch return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
test "KeyPair generate" {
|
||||
const kp = KeyPair.generate();
|
||||
try std.testing.expect(!kp.isEmpty());
|
||||
try std.testing.expect(kp.public[0] != 0 or kp.public[1] != 0);
|
||||
}
|
||||
|
||||
test "KeyPair fromPrivate" {
|
||||
const kp1 = KeyPair.generate();
|
||||
const kp2 = try KeyPair.fromPrivate(kp1.private);
|
||||
try std.testing.expectEqualSlices(u8, &kp1.public, &kp2.public);
|
||||
}
|
||||
|
||||
test "DH exchange" {
|
||||
const alice = KeyPair.generate();
|
||||
const bob = KeyPair.generate();
|
||||
|
||||
const alice_shared = try dh(alice, bob.public);
|
||||
const bob_shared = try dh(bob, alice.public);
|
||||
|
||||
try std.testing.expectEqualSlices(u8, &alice_shared, &bob_shared);
|
||||
}
|
||||
|
||||
test "empty KeyPair" {
|
||||
const kp = KeyPair.empty();
|
||||
try std.testing.expect(kp.isEmpty());
|
||||
}
|
||||
|
||||
test "isValidPublicKey" {
|
||||
const kp = KeyPair.generate();
|
||||
try std.testing.expect(isValidPublicKey(kp.public));
|
||||
|
||||
const zeros = [_]u8{0} ** DH_LEN;
|
||||
try std.testing.expect(!isValidPublicKey(zeros));
|
||||
}
|
||||
344
src/noise/handshake.zig
Normal file
344
src/noise/handshake.zig
Normal file
@@ -0,0 +1,344 @@
|
||||
//! HandshakeState for Noise Protocol
|
||||
//!
|
||||
//! Implements the XX handshake pattern:
|
||||
//! -> e
|
||||
//! <- e, ee, s, es
|
||||
//! -> s, se
|
||||
//!
|
||||
//! XX provides mutual authentication where both parties learn
|
||||
//! each other's static public key during the handshake.
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
const noise = @import("noise.zig");
|
||||
const cipher = @import("cipher.zig");
|
||||
const hash_mod = @import("hash.zig");
|
||||
const dh_mod = @import("dh.zig");
|
||||
const symmetric = @import("symmetric.zig");
|
||||
const transport_mod = @import("transport.zig");
|
||||
|
||||
const Error = noise.Error;
|
||||
const Role = noise.Role;
|
||||
const KeyPair = dh_mod.KeyPair;
|
||||
const SymmetricState = symmetric.SymmetricState;
|
||||
const Transport = transport_mod.Transport;
|
||||
const DH_LEN = dh_mod.DH_LEN;
|
||||
const HASH_LEN = hash_mod.HASH_LEN;
|
||||
|
||||
/// HandshakeState manages the Noise handshake process.
|
||||
pub const HandshakeState = struct {
|
||||
/// Symmetric state for key derivation and encryption
|
||||
symmetric: SymmetricState,
|
||||
/// Our static key pair (long-term identity)
|
||||
s: KeyPair,
|
||||
/// Our ephemeral key pair (generated per handshake)
|
||||
e: KeyPair,
|
||||
/// Remote party's static public key
|
||||
rs: [DH_LEN]u8,
|
||||
/// Remote party's ephemeral public key
|
||||
re: [DH_LEN]u8,
|
||||
/// Our role in the handshake
|
||||
role: Role,
|
||||
/// Current message pattern index
|
||||
message_index: u8,
|
||||
/// Whether handshake is complete
|
||||
complete: bool,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
/// Initialize HandshakeState for XX pattern.
|
||||
///
|
||||
/// - role: initiator or responder
|
||||
/// - static_keypair: our long-term identity key pair
|
||||
/// - prologue: optional data to mix into handshake hash (can be null)
|
||||
pub fn initXX(
|
||||
role: Role,
|
||||
static_keypair: KeyPair,
|
||||
prologue: ?[]const u8,
|
||||
) Self {
|
||||
var self = Self{
|
||||
.symmetric = SymmetricState.init(noise.PROTOCOL_NAME),
|
||||
.s = static_keypair,
|
||||
.e = KeyPair.empty(),
|
||||
.rs = [_]u8{0} ** DH_LEN,
|
||||
.re = [_]u8{0} ** DH_LEN,
|
||||
.role = role,
|
||||
.message_index = 0,
|
||||
.complete = false,
|
||||
};
|
||||
|
||||
// Mix prologue into handshake hash if provided
|
||||
if (prologue) |p| {
|
||||
self.symmetric.mixHash(p);
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/// Write the next handshake message.
|
||||
/// Returns the number of bytes written.
|
||||
pub fn writeMessage(self: *Self, payload: []const u8, out: []u8) Error!usize {
|
||||
if (self.complete) return Error.HandshakeAlreadyComplete;
|
||||
|
||||
var offset: usize = 0;
|
||||
|
||||
switch (self.role) {
|
||||
.initiator => {
|
||||
switch (self.message_index) {
|
||||
0 => {
|
||||
// -> e
|
||||
offset = try self.writeE(out);
|
||||
// Encrypt and send payload (empty in typical XX)
|
||||
const encrypted = try self.symmetric.encryptAndHash(payload, out[offset..]);
|
||||
offset += encrypted.len;
|
||||
},
|
||||
1 => {
|
||||
// -> s, se
|
||||
offset = try self.writeS(out);
|
||||
try self.mixDH(.se);
|
||||
// Encrypt payload
|
||||
const encrypted = try self.symmetric.encryptAndHash(payload, out[offset..]);
|
||||
offset += encrypted.len;
|
||||
self.complete = true;
|
||||
},
|
||||
else => return Error.InvalidState,
|
||||
}
|
||||
},
|
||||
.responder => {
|
||||
switch (self.message_index) {
|
||||
1 => {
|
||||
// <- e, ee, s, es
|
||||
offset = try self.writeE(out);
|
||||
try self.mixDH(.ee);
|
||||
offset += try self.writeS(out[offset..]);
|
||||
try self.mixDH(.es);
|
||||
// Encrypt payload
|
||||
const encrypted = try self.symmetric.encryptAndHash(payload, out[offset..]);
|
||||
offset += encrypted.len;
|
||||
},
|
||||
else => return Error.InvalidState,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
self.message_index += 1;
|
||||
return offset;
|
||||
}
|
||||
|
||||
/// Read the next handshake message.
|
||||
/// Returns the decrypted payload length.
|
||||
pub fn readMessage(self: *Self, message: []const u8, payload_out: []u8) Error!usize {
|
||||
if (self.complete) return Error.HandshakeAlreadyComplete;
|
||||
|
||||
var offset: usize = 0;
|
||||
|
||||
switch (self.role) {
|
||||
.initiator => {
|
||||
switch (self.message_index) {
|
||||
1 => {
|
||||
// <- e, ee, s, es
|
||||
offset = try self.readE(message);
|
||||
try self.mixDH(.ee);
|
||||
offset += try self.readS(message[offset..]);
|
||||
try self.mixDH(.es);
|
||||
// Decrypt payload
|
||||
const decrypted = try self.symmetric.decryptAndHash(
|
||||
message[offset..],
|
||||
payload_out,
|
||||
);
|
||||
return decrypted.len;
|
||||
},
|
||||
else => return Error.InvalidState,
|
||||
}
|
||||
},
|
||||
.responder => {
|
||||
switch (self.message_index) {
|
||||
0 => {
|
||||
// -> e
|
||||
offset = try self.readE(message);
|
||||
// Decrypt payload (empty in typical XX)
|
||||
const decrypted = try self.symmetric.decryptAndHash(
|
||||
message[offset..],
|
||||
payload_out,
|
||||
);
|
||||
self.message_index += 1;
|
||||
return decrypted.len;
|
||||
},
|
||||
2 => {
|
||||
// -> s, se
|
||||
offset = try self.readS(message);
|
||||
try self.mixDH(.se);
|
||||
// Decrypt payload
|
||||
const decrypted = try self.symmetric.decryptAndHash(
|
||||
message[offset..],
|
||||
payload_out,
|
||||
);
|
||||
self.complete = true;
|
||||
return decrypted.len;
|
||||
},
|
||||
else => return Error.InvalidState,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
self.message_index += 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// Split the handshake into transport ciphers.
|
||||
/// Must be called after handshake is complete.
|
||||
pub fn split(self: *Self) Error!Transport {
|
||||
if (!self.complete) return Error.HandshakeNotComplete;
|
||||
|
||||
const ciphers = self.symmetric.split();
|
||||
|
||||
return switch (self.role) {
|
||||
.initiator => Transport.init(ciphers[0], ciphers[1], self.symmetric.getHandshakeHash()),
|
||||
.responder => Transport.init(ciphers[1], ciphers[0], self.symmetric.getHandshakeHash()),
|
||||
};
|
||||
}
|
||||
|
||||
/// Get the remote party's static public key.
|
||||
/// Only valid after the handshake is complete.
|
||||
pub fn getRemoteStatic(self: *const Self) Error![DH_LEN]u8 {
|
||||
if (!self.complete) return Error.HandshakeNotComplete;
|
||||
return self.rs;
|
||||
}
|
||||
|
||||
/// Get the handshake hash for channel binding.
|
||||
pub fn getHandshakeHash(self: *const Self) [HASH_LEN]u8 {
|
||||
return self.symmetric.getHandshakeHash();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Private helper functions
|
||||
// =========================================================================
|
||||
|
||||
/// Write ephemeral public key
|
||||
fn writeE(self: *Self, out: []u8) Error!usize {
|
||||
if (out.len < DH_LEN) return Error.BufferTooSmall;
|
||||
|
||||
// Generate ephemeral key pair if not already done
|
||||
if (self.e.isEmpty()) {
|
||||
self.e = KeyPair.generate();
|
||||
}
|
||||
|
||||
@memcpy(out[0..DH_LEN], &self.e.public);
|
||||
self.symmetric.mixHash(out[0..DH_LEN]);
|
||||
return DH_LEN;
|
||||
}
|
||||
|
||||
/// Write encrypted static public key
|
||||
fn writeS(self: *Self, out: []u8) Error!usize {
|
||||
if (out.len < DH_LEN + 16) return Error.BufferTooSmall;
|
||||
|
||||
const encrypted = try self.symmetric.encryptAndHash(&self.s.public, out);
|
||||
return encrypted.len;
|
||||
}
|
||||
|
||||
/// Read ephemeral public key
|
||||
fn readE(self: *Self, data: []const u8) Error!usize {
|
||||
if (data.len < DH_LEN) return Error.InvalidState;
|
||||
|
||||
@memcpy(&self.re, data[0..DH_LEN]);
|
||||
|
||||
if (!dh_mod.isValidPublicKey(self.re)) {
|
||||
return Error.InvalidPublicKey;
|
||||
}
|
||||
|
||||
self.symmetric.mixHash(&self.re);
|
||||
return DH_LEN;
|
||||
}
|
||||
|
||||
/// Read encrypted static public key
|
||||
fn readS(self: *Self, data: []const u8) Error!usize {
|
||||
if (data.len < DH_LEN + 16) return Error.InvalidState;
|
||||
|
||||
var decrypted: [DH_LEN]u8 = undefined;
|
||||
_ = try self.symmetric.decryptAndHash(data[0 .. DH_LEN + 16], &decrypted);
|
||||
@memcpy(&self.rs, &decrypted);
|
||||
|
||||
if (!dh_mod.isValidPublicKey(self.rs)) {
|
||||
return Error.InvalidPublicKey;
|
||||
}
|
||||
|
||||
return DH_LEN + 16;
|
||||
}
|
||||
|
||||
/// DH operation types
|
||||
const DHOp = enum { ee, es, se, ss };
|
||||
|
||||
/// Perform DH and mix result into symmetric state
|
||||
fn mixDH(self: *Self, op: DHOp) Error!void {
|
||||
const shared = switch (op) {
|
||||
.ee => try dh_mod.dh(self.e, self.re),
|
||||
.es => switch (self.role) {
|
||||
.initiator => try dh_mod.dh(self.e, self.rs),
|
||||
.responder => try dh_mod.dh(self.s, self.re),
|
||||
},
|
||||
.se => switch (self.role) {
|
||||
.initiator => try dh_mod.dh(self.s, self.re),
|
||||
.responder => try dh_mod.dh(self.e, self.rs),
|
||||
},
|
||||
.ss => try dh_mod.dh(self.s, self.rs),
|
||||
};
|
||||
self.symmetric.mixKey(&shared);
|
||||
}
|
||||
};
|
||||
|
||||
test "XX handshake" {
|
||||
const alice_static = KeyPair.generate();
|
||||
const bob_static = KeyPair.generate();
|
||||
|
||||
var alice = HandshakeState.initXX(.initiator, alice_static, null);
|
||||
var bob = HandshakeState.initXX(.responder, bob_static, null);
|
||||
|
||||
var msg1: [64]u8 = undefined;
|
||||
var msg2: [128]u8 = undefined;
|
||||
var msg3: [128]u8 = undefined;
|
||||
var payload: [64]u8 = undefined;
|
||||
|
||||
// -> e
|
||||
const msg1_len = try alice.writeMessage(&.{}, &msg1);
|
||||
try std.testing.expect(msg1_len >= 32);
|
||||
|
||||
// Bob reads msg1
|
||||
_ = try bob.readMessage(msg1[0..msg1_len], &payload);
|
||||
|
||||
// <- e, ee, s, es
|
||||
const msg2_len = try bob.writeMessage(&.{}, &msg2);
|
||||
try std.testing.expect(msg2_len >= 80);
|
||||
|
||||
// Alice reads msg2
|
||||
_ = try alice.readMessage(msg2[0..msg2_len], &payload);
|
||||
|
||||
// -> s, se
|
||||
const msg3_len = try alice.writeMessage(&.{}, &msg3);
|
||||
try std.testing.expect(msg3_len >= 48);
|
||||
|
||||
// Bob reads msg3
|
||||
_ = try bob.readMessage(msg3[0..msg3_len], &payload);
|
||||
|
||||
// Both should be complete
|
||||
try std.testing.expect(alice.complete);
|
||||
try std.testing.expect(bob.complete);
|
||||
|
||||
// Get transports
|
||||
var alice_transport = try alice.split();
|
||||
var bob_transport = try bob.split();
|
||||
|
||||
// Verify remote static keys
|
||||
const alice_remote = try alice.getRemoteStatic();
|
||||
const bob_remote = try bob.getRemoteStatic();
|
||||
try std.testing.expectEqualSlices(u8, &bob_static.public, &alice_remote);
|
||||
try std.testing.expectEqualSlices(u8, &alice_static.public, &bob_remote);
|
||||
|
||||
// Test transport encryption
|
||||
const test_msg = "Hello, secure world!";
|
||||
var encrypted: [128]u8 = undefined;
|
||||
var decrypted: [128]u8 = undefined;
|
||||
|
||||
const enc_len = try alice_transport.send(test_msg, &encrypted);
|
||||
const dec_result = try bob_transport.receive(encrypted[0..enc_len], &decrypted);
|
||||
try std.testing.expectEqualStrings(test_msg, dec_result);
|
||||
}
|
||||
207
src/noise/hash.zig
Normal file
207
src/noise/hash.zig
Normal file
@@ -0,0 +1,207 @@
|
||||
//! Hash functions for Noise Protocol
|
||||
//!
|
||||
//! Uses BLAKE2b-256 for all hashing operations.
|
||||
//! BLAKE2b is designed by djb and team, providing high security
|
||||
//! with no government involvement.
|
||||
|
||||
const std = @import("std");
|
||||
const Blake2b256 = std.crypto.hash.blake2.Blake2b256;
|
||||
|
||||
/// Hash output size (32 bytes for BLAKE2b-256)
|
||||
pub const HASH_LEN: usize = 32;
|
||||
|
||||
/// Block size for BLAKE2b
|
||||
pub const BLOCK_LEN: usize = 128;
|
||||
|
||||
/// Hash function - compute BLAKE2b-256 hash
|
||||
pub fn hash(data: []const u8) [HASH_LEN]u8 {
|
||||
var out: [HASH_LEN]u8 = undefined;
|
||||
Blake2b256.hash(data, &out, .{});
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Hash two inputs concatenated
|
||||
pub fn hashTwo(a: []const u8, b: []const u8) [HASH_LEN]u8 {
|
||||
var hasher = Blake2b256.init(.{});
|
||||
hasher.update(a);
|
||||
hasher.update(b);
|
||||
var out: [HASH_LEN]u8 = undefined;
|
||||
hasher.final(&out);
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Hash three inputs concatenated
|
||||
pub fn hashThree(a: []const u8, b: []const u8, c: []const u8) [HASH_LEN]u8 {
|
||||
var hasher = Blake2b256.init(.{});
|
||||
hasher.update(a);
|
||||
hasher.update(b);
|
||||
hasher.update(c);
|
||||
var out: [HASH_LEN]u8 = undefined;
|
||||
hasher.final(&out);
|
||||
return out;
|
||||
}
|
||||
|
||||
/// HMAC-BLAKE2b implementation
|
||||
/// Used for HKDF in the Noise protocol
|
||||
pub const Hmac = struct {
|
||||
inner: Blake2b256,
|
||||
outer_key: [BLOCK_LEN]u8,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn init(key: []const u8) Self {
|
||||
var self: Self = undefined;
|
||||
|
||||
// If key is longer than block size, hash it
|
||||
var key_block: [BLOCK_LEN]u8 = [_]u8{0} ** BLOCK_LEN;
|
||||
if (key.len > BLOCK_LEN) {
|
||||
const hashed = hash(key);
|
||||
@memcpy(key_block[0..HASH_LEN], &hashed);
|
||||
} else {
|
||||
@memcpy(key_block[0..key.len], key);
|
||||
}
|
||||
|
||||
// Compute outer key (key XOR 0x5c)
|
||||
for (&self.outer_key, key_block) |*o, k| {
|
||||
o.* = k ^ 0x5c;
|
||||
}
|
||||
|
||||
// Compute inner key (key XOR 0x36) and start inner hash
|
||||
var inner_key: [BLOCK_LEN]u8 = undefined;
|
||||
for (&inner_key, key_block) |*i, k| {
|
||||
i.* = k ^ 0x36;
|
||||
}
|
||||
|
||||
self.inner = Blake2b256.init(.{});
|
||||
self.inner.update(&inner_key);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
pub fn update(self: *Self, data: []const u8) void {
|
||||
self.inner.update(data);
|
||||
}
|
||||
|
||||
pub fn final(self: *Self) [HASH_LEN]u8 {
|
||||
// Finalize inner hash
|
||||
var inner_hash: [HASH_LEN]u8 = undefined;
|
||||
self.inner.final(&inner_hash);
|
||||
|
||||
// Compute outer hash: H(outer_key || inner_hash)
|
||||
var outer = Blake2b256.init(.{});
|
||||
outer.update(&self.outer_key);
|
||||
outer.update(&inner_hash);
|
||||
var out: [HASH_LEN]u8 = undefined;
|
||||
outer.final(&out);
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Convenience function for one-shot HMAC
|
||||
pub fn compute(key: []const u8, data: []const u8) [HASH_LEN]u8 {
|
||||
var hmac = Hmac.init(key);
|
||||
hmac.update(data);
|
||||
return hmac.final();
|
||||
}
|
||||
};
|
||||
|
||||
/// HKDF (HMAC-based Key Derivation Function)
|
||||
/// Extracts and expands key material as per RFC 5869
|
||||
pub const Hkdf = struct {
|
||||
/// Extract: HMAC(salt, input_key_material)
|
||||
pub fn extract(salt: []const u8, ikm: []const u8) [HASH_LEN]u8 {
|
||||
return Hmac.compute(salt, ikm);
|
||||
}
|
||||
|
||||
/// Expand: Derive output_len bytes from PRK
|
||||
pub fn expand(prk: []const u8, info: []const u8, output: []u8) void {
|
||||
var counter: u8 = 1;
|
||||
var prev: [HASH_LEN]u8 = undefined;
|
||||
var offset: usize = 0;
|
||||
|
||||
while (offset < output.len) {
|
||||
var hmac = Hmac.init(prk);
|
||||
if (counter > 1) {
|
||||
hmac.update(&prev);
|
||||
}
|
||||
hmac.update(info);
|
||||
hmac.update(&[_]u8{counter});
|
||||
prev = hmac.final();
|
||||
|
||||
const to_copy = @min(HASH_LEN, output.len - offset);
|
||||
@memcpy(output[offset..][0..to_copy], prev[0..to_copy]);
|
||||
offset += to_copy;
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Combined extract and expand
|
||||
pub fn deriveKeys(
|
||||
salt: []const u8,
|
||||
ikm: []const u8,
|
||||
info: []const u8,
|
||||
output: []u8,
|
||||
) void {
|
||||
const prk = extract(salt, ikm);
|
||||
expand(&prk, info, output);
|
||||
}
|
||||
};
|
||||
|
||||
/// Noise-specific HKDF that outputs 2 or 3 keys
|
||||
pub fn hkdf2(chaining_key: [HASH_LEN]u8, input: []const u8) struct { [HASH_LEN]u8, [HASH_LEN]u8 } {
|
||||
const temp_key = Hmac.compute(&chaining_key, input);
|
||||
const output1 = Hmac.compute(&temp_key, &[_]u8{0x01});
|
||||
|
||||
var hmac2 = Hmac.init(&temp_key);
|
||||
hmac2.update(&output1);
|
||||
hmac2.update(&[_]u8{0x02});
|
||||
const output2 = hmac2.final();
|
||||
|
||||
return .{ output1, output2 };
|
||||
}
|
||||
|
||||
pub fn hkdf3(chaining_key: [HASH_LEN]u8, input: []const u8) struct { [HASH_LEN]u8, [HASH_LEN]u8, [HASH_LEN]u8 } {
|
||||
const temp_key = Hmac.compute(&chaining_key, input);
|
||||
const output1 = Hmac.compute(&temp_key, &[_]u8{0x01});
|
||||
|
||||
var hmac2 = Hmac.init(&temp_key);
|
||||
hmac2.update(&output1);
|
||||
hmac2.update(&[_]u8{0x02});
|
||||
const output2 = hmac2.final();
|
||||
|
||||
var hmac3 = Hmac.init(&temp_key);
|
||||
hmac3.update(&output2);
|
||||
hmac3.update(&[_]u8{0x03});
|
||||
const output3 = hmac3.final();
|
||||
|
||||
return .{ output1, output2, output3 };
|
||||
}
|
||||
|
||||
test "hash basic" {
|
||||
const result = hash("hello");
|
||||
try std.testing.expect(result.len == HASH_LEN);
|
||||
}
|
||||
|
||||
test "HMAC" {
|
||||
const key = "key";
|
||||
const data = "The quick brown fox jumps over the lazy dog";
|
||||
const result = Hmac.compute(key, data);
|
||||
try std.testing.expect(result.len == HASH_LEN);
|
||||
}
|
||||
|
||||
test "HKDF" {
|
||||
const salt = [_]u8{0} ** 32;
|
||||
const ikm = "input key material";
|
||||
var output: [64]u8 = undefined;
|
||||
Hkdf.deriveKeys(&salt, ikm, "", &output);
|
||||
try std.testing.expect(output[0] != 0 or output[1] != 0); // Not all zeros
|
||||
}
|
||||
|
||||
test "hkdf2" {
|
||||
const ck = [_]u8{0x42} ** 32;
|
||||
const input = "test input";
|
||||
const result = hkdf2(ck, input);
|
||||
try std.testing.expect(result[0].len == HASH_LEN);
|
||||
try std.testing.expect(result[1].len == HASH_LEN);
|
||||
// Output1 and output2 should be different
|
||||
try std.testing.expect(!std.mem.eql(u8, &result[0], &result[1]));
|
||||
}
|
||||
139
src/noise/noise.zig
Normal file
139
src/noise/noise.zig
Normal file
@@ -0,0 +1,139 @@
|
||||
//! Noise Protocol Framework Implementation
|
||||
//!
|
||||
//! Implements Noise_XX_25519_ChaChaPoly_BLAKE2b:
|
||||
//! - Handshake: XX (mutual authentication)
|
||||
//! - DH: X25519 (Curve25519)
|
||||
//! - Cipher: ChaCha20-Poly1305
|
||||
//! - Hash: BLAKE2b
|
||||
//!
|
||||
//! This implementation follows the Noise Protocol specification:
|
||||
//! https://noiseprotocol.org/noise.html
|
||||
//!
|
||||
//! All cryptographic primitives are from djb (Daniel J. Bernstein),
|
||||
//! providing high trust and no known backdoors.
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
pub const cipher = @import("cipher.zig");
|
||||
pub const hash = @import("hash.zig");
|
||||
pub const dh = @import("dh.zig");
|
||||
pub const symmetric = @import("symmetric.zig");
|
||||
pub const handshake = @import("handshake.zig");
|
||||
pub const transport = @import("transport.zig");
|
||||
|
||||
// Re-export main types for convenience
|
||||
pub const CipherState = cipher.CipherState;
|
||||
pub const SymmetricState = symmetric.SymmetricState;
|
||||
pub const HandshakeState = handshake.HandshakeState;
|
||||
pub const Transport = transport.Transport;
|
||||
pub const KeyPair = dh.KeyPair;
|
||||
|
||||
/// Protocol name for Noise_XX_25519_ChaChaPoly_BLAKE2b
|
||||
pub const PROTOCOL_NAME = "Noise_XX_25519_ChaChaPoly_BLAKE2b";
|
||||
|
||||
/// Maximum message size (64KB as per Noise spec)
|
||||
pub const MAX_MESSAGE_SIZE = 65535;
|
||||
|
||||
/// Size of authentication tag
|
||||
pub const TAG_SIZE = 16;
|
||||
|
||||
/// Size of DH public key
|
||||
pub const DH_SIZE = 32;
|
||||
|
||||
/// Size of hash output
|
||||
pub const HASH_SIZE = 32;
|
||||
|
||||
/// Rekey threshold - rekey after this many messages to maintain forward secrecy.
|
||||
/// Set conservatively at 2^32 messages (about 4 billion).
|
||||
/// This is well below the 2^64 nonce limit but provides regular key rotation.
|
||||
pub const REKEY_AFTER_MESSAGES: u64 = 0x100000000; // 2^32
|
||||
|
||||
/// Warning threshold - warn when approaching rekey (90% of threshold)
|
||||
pub const REKEY_WARNING_THRESHOLD: u64 = REKEY_AFTER_MESSAGES - (REKEY_AFTER_MESSAGES / 10);
|
||||
|
||||
/// Noise protocol errors
|
||||
pub const Error = error{
|
||||
/// Message exceeds maximum allowed size
|
||||
MessageTooLarge,
|
||||
/// Decryption failed (authentication tag mismatch)
|
||||
DecryptionFailed,
|
||||
/// Handshake not complete
|
||||
HandshakeNotComplete,
|
||||
/// Handshake already complete
|
||||
HandshakeAlreadyComplete,
|
||||
/// Invalid handshake state
|
||||
InvalidState,
|
||||
/// Nonce overflow (extremely unlikely, would require 2^64 messages)
|
||||
NonceOverflow,
|
||||
/// Invalid public key
|
||||
InvalidPublicKey,
|
||||
/// Buffer too small
|
||||
BufferTooSmall,
|
||||
};
|
||||
|
||||
/// Role in the handshake
|
||||
pub const Role = enum {
|
||||
initiator,
|
||||
responder,
|
||||
};
|
||||
|
||||
/// Perform a complete XX handshake as initiator
|
||||
pub fn initiatorHandshake(
|
||||
static_keypair: KeyPair,
|
||||
writer: anytype,
|
||||
reader: anytype,
|
||||
) Error!Transport {
|
||||
var hs = HandshakeState.initXX(Role.initiator, static_keypair, null);
|
||||
|
||||
// -> e
|
||||
var msg1: [48]u8 = undefined; // 32 (e) + 16 (tag for empty payload)
|
||||
const msg1_len = try hs.writeMessage(&.{}, &msg1);
|
||||
try writer.writeAll(msg1[0..msg1_len]);
|
||||
|
||||
// <- e, ee, s, es
|
||||
var msg2_buf: [96]u8 = undefined;
|
||||
const msg2_len = try reader.readAtLeast(&msg2_buf, 48);
|
||||
_ = try hs.readMessage(msg2_buf[0..msg2_len], &.{});
|
||||
|
||||
// -> s, se
|
||||
var msg3: [64]u8 = undefined;
|
||||
const msg3_len = try hs.writeMessage(&.{}, &msg3);
|
||||
try writer.writeAll(msg3[0..msg3_len]);
|
||||
|
||||
return hs.split();
|
||||
}
|
||||
|
||||
/// Perform a complete XX handshake as responder
|
||||
pub fn responderHandshake(
|
||||
static_keypair: KeyPair,
|
||||
writer: anytype,
|
||||
reader: anytype,
|
||||
) Error!Transport {
|
||||
var hs = HandshakeState.initXX(Role.responder, static_keypair, null);
|
||||
|
||||
// <- e
|
||||
var msg1_buf: [48]u8 = undefined;
|
||||
const msg1_len = try reader.readAtLeast(&msg1_buf, 32);
|
||||
_ = try hs.readMessage(msg1_buf[0..msg1_len], &.{});
|
||||
|
||||
// -> e, ee, s, es
|
||||
var msg2: [96]u8 = undefined;
|
||||
const msg2_len = try hs.writeMessage(&.{}, &msg2);
|
||||
try writer.writeAll(msg2[0..msg2_len]);
|
||||
|
||||
// <- s, se
|
||||
var msg3_buf: [64]u8 = undefined;
|
||||
const msg3_len = try reader.readAtLeast(&msg3_buf, 48);
|
||||
_ = try hs.readMessage(msg3_buf[0..msg3_len], &.{});
|
||||
|
||||
return hs.split();
|
||||
}
|
||||
|
||||
test "noise module" {
|
||||
_ = cipher;
|
||||
_ = hash;
|
||||
_ = dh;
|
||||
_ = symmetric;
|
||||
_ = handshake;
|
||||
_ = transport;
|
||||
}
|
||||
153
src/noise/symmetric.zig
Normal file
153
src/noise/symmetric.zig
Normal file
@@ -0,0 +1,153 @@
|
||||
//! SymmetricState for Noise Protocol
|
||||
//!
|
||||
//! Manages the chaining key, handshake hash, and cipher state
|
||||
//! during the handshake process.
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
const noise = @import("noise.zig");
|
||||
const cipher = @import("cipher.zig");
|
||||
const hash_mod = @import("hash.zig");
|
||||
|
||||
const Error = noise.Error;
|
||||
const CipherState = cipher.CipherState;
|
||||
const HASH_LEN = hash_mod.HASH_LEN;
|
||||
|
||||
/// SymmetricState manages the symmetric cryptography during handshake.
|
||||
/// It maintains:
|
||||
/// - ck: chaining key (for key derivation)
|
||||
/// - h: handshake hash (for authentication)
|
||||
/// - CipherState: for encrypting handshake payloads
|
||||
pub const SymmetricState = struct {
|
||||
/// Chaining key - used for key derivation
|
||||
ck: [HASH_LEN]u8,
|
||||
/// Handshake hash - authenticates the handshake transcript
|
||||
h: [HASH_LEN]u8,
|
||||
/// Cipher state for encrypting handshake payloads
|
||||
cs: CipherState,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
/// Initialize SymmetricState with a protocol name.
|
||||
/// If protocol_name <= HASH_LEN, h = protocol_name padded with zeros.
|
||||
/// Otherwise, h = HASH(protocol_name).
|
||||
pub fn init(protocol_name: []const u8) Self {
|
||||
var h: [HASH_LEN]u8 = undefined;
|
||||
|
||||
if (protocol_name.len <= HASH_LEN) {
|
||||
@memset(&h, 0);
|
||||
@memcpy(h[0..protocol_name.len], protocol_name);
|
||||
} else {
|
||||
h = hash_mod.hash(protocol_name);
|
||||
}
|
||||
|
||||
return Self{
|
||||
.ck = h,
|
||||
.h = h,
|
||||
.cs = CipherState.init(),
|
||||
};
|
||||
}
|
||||
|
||||
/// Mix key material into the chaining key and optionally derive cipher key.
|
||||
/// Called after each DH operation.
|
||||
pub fn mixKey(self: *Self, input_key_material: []const u8) void {
|
||||
const result = hash_mod.hkdf2(self.ck, input_key_material);
|
||||
self.ck = result[0];
|
||||
self.cs.setKey(result[1]);
|
||||
}
|
||||
|
||||
/// Mix data into the handshake hash.
|
||||
/// Called for each piece of data that should be authenticated.
|
||||
pub fn mixHash(self: *Self, data: []const u8) void {
|
||||
self.h = hash_mod.hashTwo(&self.h, data);
|
||||
}
|
||||
|
||||
/// Mix key material into both ck and h.
|
||||
/// Used for "psk" patterns (not used in XX, but included for completeness).
|
||||
pub fn mixKeyAndHash(self: *Self, input_key_material: []const u8) void {
|
||||
const result = hash_mod.hkdf3(self.ck, input_key_material);
|
||||
self.ck = result[0];
|
||||
self.mixHash(&result[1]);
|
||||
self.cs.setKey(result[2]);
|
||||
}
|
||||
|
||||
/// Get the current handshake hash (for channel binding).
|
||||
pub fn getHandshakeHash(self: *const Self) [HASH_LEN]u8 {
|
||||
return self.h;
|
||||
}
|
||||
|
||||
/// Encrypt plaintext and mix the ciphertext into the handshake hash.
|
||||
/// Used for encrypting static keys and payloads in the handshake.
|
||||
pub fn encryptAndHash(self: *Self, plaintext: []const u8, out: []u8) Error![]u8 {
|
||||
const ciphertext = try self.cs.encryptWithAd(&self.h, plaintext, out);
|
||||
self.mixHash(ciphertext);
|
||||
return ciphertext;
|
||||
}
|
||||
|
||||
/// Decrypt ciphertext, verify authentication, and mix into handshake hash.
|
||||
pub fn decryptAndHash(self: *Self, ciphertext: []const u8, out: []u8) Error![]u8 {
|
||||
const plaintext = try self.cs.decryptWithAd(&self.h, ciphertext, out);
|
||||
self.mixHash(ciphertext);
|
||||
return plaintext;
|
||||
}
|
||||
|
||||
/// Split the SymmetricState into two CipherStates for transport.
|
||||
/// Called when the handshake is complete.
|
||||
/// Returns (send_cipher, receive_cipher).
|
||||
pub fn split(self: *Self) struct { CipherState, CipherState } {
|
||||
const result = hash_mod.hkdf2(self.ck, &.{});
|
||||
return .{
|
||||
CipherState.initWithKey(result[0]),
|
||||
CipherState.initWithKey(result[1]),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
test "SymmetricState init short name" {
|
||||
const ss = SymmetricState.init("Test");
|
||||
try std.testing.expectEqualStrings("Test", ss.h[0..4]);
|
||||
try std.testing.expect(ss.h[4] == 0); // Padded with zeros
|
||||
}
|
||||
|
||||
test "SymmetricState init long name" {
|
||||
const long_name = "This is a very long protocol name that exceeds 32 bytes in length";
|
||||
const ss = SymmetricState.init(long_name);
|
||||
// Should be hashed, so h != padded name
|
||||
try std.testing.expect(ss.h[0] != long_name[0] or ss.h[1] != long_name[1]);
|
||||
}
|
||||
|
||||
test "SymmetricState mixHash" {
|
||||
var ss = SymmetricState.init("Test");
|
||||
const h1 = ss.h;
|
||||
ss.mixHash("some data");
|
||||
try std.testing.expect(!std.mem.eql(u8, &h1, &ss.h));
|
||||
}
|
||||
|
||||
test "SymmetricState mixKey" {
|
||||
var ss = SymmetricState.init("Test");
|
||||
try std.testing.expect(!ss.cs.hasKey());
|
||||
ss.mixKey("key material");
|
||||
try std.testing.expect(ss.cs.hasKey());
|
||||
}
|
||||
|
||||
test "SymmetricState split" {
|
||||
var ss = SymmetricState.init("Test");
|
||||
ss.mixKey("key material");
|
||||
|
||||
const ciphers = ss.split();
|
||||
try std.testing.expect(ciphers[0].hasKey());
|
||||
try std.testing.expect(ciphers[1].hasKey());
|
||||
// Keys should be different
|
||||
try std.testing.expect(!std.mem.eql(u8, &ciphers[0].k.?, &ciphers[1].k.?));
|
||||
}
|
||||
|
||||
test "SymmetricState encryptAndHash" {
|
||||
var ss = SymmetricState.init("Test");
|
||||
ss.mixKey("key material");
|
||||
|
||||
const plaintext = "secret message";
|
||||
var ciphertext: [64]u8 = undefined;
|
||||
const encrypted = try ss.encryptAndHash(plaintext, &ciphertext);
|
||||
|
||||
try std.testing.expect(encrypted.len == plaintext.len + 16); // +16 for tag
|
||||
}
|
||||
550
src/noise/transport.zig
Normal file
550
src/noise/transport.zig
Normal file
@@ -0,0 +1,550 @@
|
||||
//! Transport layer for Noise Protocol
|
||||
//!
|
||||
//! After the handshake completes, the Transport provides
|
||||
//! bidirectional encrypted communication using the derived
|
||||
//! session keys.
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
const noise = @import("noise.zig");
|
||||
const cipher = @import("cipher.zig");
|
||||
const hash_mod = @import("hash.zig");
|
||||
|
||||
const Error = noise.Error;
|
||||
const CipherState = cipher.CipherState;
|
||||
const HASH_LEN = hash_mod.HASH_LEN;
|
||||
|
||||
/// Transport provides encrypted bidirectional communication
|
||||
/// after the Noise handshake is complete.
|
||||
pub const Transport = struct {
|
||||
/// CipherState for sending messages
|
||||
send_cipher: CipherState,
|
||||
/// CipherState for receiving messages
|
||||
recv_cipher: CipherState,
|
||||
/// Handshake hash for channel binding
|
||||
handshake_hash: [HASH_LEN]u8,
|
||||
/// Whether auto-rekey is enabled
|
||||
auto_rekey_enabled: bool,
|
||||
/// Count of send rekeys performed
|
||||
send_rekey_count: u32,
|
||||
/// Count of receive rekeys performed
|
||||
recv_rekey_count: u32,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
/// Initialize Transport with send/receive ciphers.
|
||||
/// Called by HandshakeState.split()
|
||||
pub fn init(
|
||||
send_cipher: CipherState,
|
||||
recv_cipher: CipherState,
|
||||
handshake_hash: [HASH_LEN]u8,
|
||||
) Self {
|
||||
return Self{
|
||||
.send_cipher = send_cipher,
|
||||
.recv_cipher = recv_cipher,
|
||||
.handshake_hash = handshake_hash,
|
||||
.auto_rekey_enabled = true, // Enable by default
|
||||
.send_rekey_count = 0,
|
||||
.recv_rekey_count = 0,
|
||||
};
|
||||
}
|
||||
|
||||
/// Initialize Transport with auto-rekey disabled (for testing)
|
||||
pub fn initNoAutoRekey(
|
||||
send_cipher: CipherState,
|
||||
recv_cipher: CipherState,
|
||||
handshake_hash: [HASH_LEN]u8,
|
||||
) Self {
|
||||
return Self{
|
||||
.send_cipher = send_cipher,
|
||||
.recv_cipher = recv_cipher,
|
||||
.handshake_hash = handshake_hash,
|
||||
.auto_rekey_enabled = false,
|
||||
.send_rekey_count = 0,
|
||||
.recv_rekey_count = 0,
|
||||
};
|
||||
}
|
||||
|
||||
/// Encrypt and send a message.
|
||||
/// Returns the encrypted message (plaintext.len + 16 bytes for tag).
|
||||
/// Automatically rekeys when nonce threshold is reached.
|
||||
pub fn send(self: *Self, plaintext: []const u8, out: []u8) Error!usize {
|
||||
if (plaintext.len > noise.MAX_MESSAGE_SIZE) {
|
||||
return Error.MessageTooLarge;
|
||||
}
|
||||
|
||||
const encrypted = try self.send_cipher.encryptWithAd(&.{}, plaintext, out);
|
||||
|
||||
// Check for auto-rekey after successful encryption
|
||||
if (self.auto_rekey_enabled) {
|
||||
self.checkAndRekeySend();
|
||||
}
|
||||
|
||||
return encrypted.len;
|
||||
}
|
||||
|
||||
/// Decrypt a received message.
|
||||
/// Returns the decrypted plaintext.
|
||||
/// Automatically rekeys when nonce threshold is reached.
|
||||
pub fn receive(self: *Self, ciphertext: []const u8, out: []u8) Error![]u8 {
|
||||
if (ciphertext.len > noise.MAX_MESSAGE_SIZE + 16) {
|
||||
return Error.MessageTooLarge;
|
||||
}
|
||||
|
||||
const result = try self.recv_cipher.decryptWithAd(&.{}, ciphertext, out);
|
||||
|
||||
// Check for auto-rekey after successful decryption
|
||||
if (self.auto_rekey_enabled) {
|
||||
self.checkAndRekeyRecv();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Check if send nonce has reached threshold and rekey if needed
|
||||
fn checkAndRekeySend(self: *Self) void {
|
||||
const nonce = self.send_cipher.n;
|
||||
if (nonce > 0 and nonce % noise.REKEY_AFTER_MESSAGES == 0) {
|
||||
self.send_cipher.rekey();
|
||||
self.send_rekey_count += 1;
|
||||
std.log.info("Noise: Auto-rekeyed send cipher (rekey #{}, after {} messages)", .{
|
||||
self.send_rekey_count,
|
||||
nonce,
|
||||
});
|
||||
} else if (nonce == noise.REKEY_WARNING_THRESHOLD) {
|
||||
std.log.warn("Noise: Approaching send rekey threshold ({}/{})", .{
|
||||
nonce,
|
||||
noise.REKEY_AFTER_MESSAGES,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if receive nonce has reached threshold and rekey if needed
|
||||
fn checkAndRekeyRecv(self: *Self) void {
|
||||
const nonce = self.recv_cipher.n;
|
||||
if (nonce > 0 and nonce % noise.REKEY_AFTER_MESSAGES == 0) {
|
||||
self.recv_cipher.rekey();
|
||||
self.recv_rekey_count += 1;
|
||||
std.log.info("Noise: Auto-rekeyed receive cipher (rekey #{}, after {} messages)", .{
|
||||
self.recv_rekey_count,
|
||||
nonce,
|
||||
});
|
||||
} else if (nonce == noise.REKEY_WARNING_THRESHOLD) {
|
||||
std.log.warn("Noise: Approaching receive rekey threshold ({}/{})", .{
|
||||
nonce,
|
||||
noise.REKEY_AFTER_MESSAGES,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Enable or disable auto-rekey
|
||||
pub fn setAutoRekey(self: *Self, enabled: bool) void {
|
||||
self.auto_rekey_enabled = enabled;
|
||||
}
|
||||
|
||||
/// Check if approaching rekey threshold (useful for monitoring)
|
||||
pub fn isApproachingRekey(self: *const Self) bool {
|
||||
return self.send_cipher.n >= noise.REKEY_WARNING_THRESHOLD or
|
||||
self.recv_cipher.n >= noise.REKEY_WARNING_THRESHOLD;
|
||||
}
|
||||
|
||||
/// Get total number of rekeys performed
|
||||
pub fn getTotalRekeyCount(self: *const Self) u32 {
|
||||
return self.send_rekey_count + self.recv_rekey_count;
|
||||
}
|
||||
|
||||
/// Force set nonce values (for testing only!)
|
||||
/// WARNING: Only use this for testing auto-rekey behavior
|
||||
pub fn setNoncesForTesting(self: *Self, send_nonce: u64, recv_nonce: u64) void {
|
||||
self.send_cipher.n = send_nonce;
|
||||
self.recv_cipher.n = recv_nonce;
|
||||
}
|
||||
|
||||
/// Get the handshake hash for channel binding.
|
||||
/// This can be used to cryptographically bind application-level
|
||||
/// authentication to the Noise session.
|
||||
pub fn getHandshakeHash(self: *const Self) [HASH_LEN]u8 {
|
||||
return self.handshake_hash;
|
||||
}
|
||||
|
||||
/// Rekey the send cipher for forward secrecy.
|
||||
/// Call periodically for long-lived connections.
|
||||
pub fn rekeyOutbound(self: *Self) void {
|
||||
self.send_cipher.rekey();
|
||||
}
|
||||
|
||||
/// Rekey the receive cipher for forward secrecy.
|
||||
/// Must be coordinated with the remote party.
|
||||
pub fn rekeyInbound(self: *Self) void {
|
||||
self.recv_cipher.rekey();
|
||||
}
|
||||
|
||||
/// Rekey both ciphers.
|
||||
pub fn rekeyBoth(self: *Self) void {
|
||||
self.rekeyOutbound();
|
||||
self.rekeyInbound();
|
||||
}
|
||||
|
||||
/// Get the current send nonce (for debugging/monitoring).
|
||||
pub fn getSendNonce(self: *const Self) u64 {
|
||||
return self.send_cipher.n;
|
||||
}
|
||||
|
||||
/// Get the current receive nonce (for debugging/monitoring).
|
||||
pub fn getRecvNonce(self: *const Self) u64 {
|
||||
return self.recv_cipher.n;
|
||||
}
|
||||
};
|
||||
|
||||
/// Framed transport with length prefixing.
|
||||
/// Wraps Transport to handle message framing over streams.
|
||||
pub const FramedTransport = struct {
|
||||
inner: Transport,
|
||||
|
||||
const Self = @This();
|
||||
const LENGTH_SIZE = 2; // 16-bit length prefix
|
||||
|
||||
pub fn init(inner: Transport) Self {
|
||||
return Self{ .inner = inner };
|
||||
}
|
||||
|
||||
/// Send a framed message.
|
||||
/// Format: [length:2][encrypted_payload:length]
|
||||
pub fn sendFramed(
|
||||
self: *Self,
|
||||
plaintext: []const u8,
|
||||
out: []u8,
|
||||
) Error!usize {
|
||||
if (plaintext.len > noise.MAX_MESSAGE_SIZE) {
|
||||
return Error.MessageTooLarge;
|
||||
}
|
||||
if (out.len < LENGTH_SIZE + plaintext.len + 16) {
|
||||
return Error.BufferTooSmall;
|
||||
}
|
||||
|
||||
// Encrypt payload
|
||||
const encrypted_len = try self.inner.send(plaintext, out[LENGTH_SIZE..]);
|
||||
|
||||
// Write length prefix (big-endian)
|
||||
std.mem.writeInt(u16, out[0..LENGTH_SIZE], @intCast(encrypted_len), .big);
|
||||
|
||||
return LENGTH_SIZE + encrypted_len;
|
||||
}
|
||||
|
||||
/// Read framed message length from a buffer.
|
||||
/// Returns the total message length (including the 2-byte prefix).
|
||||
pub fn peekFrameLength(data: []const u8) ?usize {
|
||||
if (data.len < LENGTH_SIZE) return null;
|
||||
const payload_len = std.mem.readInt(u16, data[0..LENGTH_SIZE], .big);
|
||||
return LENGTH_SIZE + payload_len;
|
||||
}
|
||||
|
||||
/// Receive and decrypt a framed message.
|
||||
/// Input should include the length prefix.
|
||||
pub fn receiveFramed(
|
||||
self: *Self,
|
||||
framed_data: []const u8,
|
||||
out: []u8,
|
||||
) Error![]u8 {
|
||||
if (framed_data.len < LENGTH_SIZE) {
|
||||
return Error.BufferTooSmall;
|
||||
}
|
||||
|
||||
const payload_len = std.mem.readInt(u16, framed_data[0..LENGTH_SIZE], .big);
|
||||
if (framed_data.len < LENGTH_SIZE + payload_len) {
|
||||
return Error.BufferTooSmall;
|
||||
}
|
||||
|
||||
return try self.inner.receive(framed_data[LENGTH_SIZE..][0..payload_len], out);
|
||||
}
|
||||
|
||||
/// Get the underlying transport for channel binding etc.
|
||||
pub fn getTransport(self: *Self) *Transport {
|
||||
return &self.inner;
|
||||
}
|
||||
};
|
||||
|
||||
test "Transport send/receive" {
|
||||
// Create two transports with matching keys (simulating successful handshake)
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const key2 = [_]u8{0x43} ** 32;
|
||||
const hash = [_]u8{0x00} ** 32;
|
||||
|
||||
var alice = Transport.init(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key2),
|
||||
hash,
|
||||
);
|
||||
var bob = Transport.init(
|
||||
cipher.CipherState.initWithKey(key2),
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
hash,
|
||||
);
|
||||
|
||||
// Alice sends to Bob
|
||||
const msg = "Hello, Bob!";
|
||||
var encrypted: [64]u8 = undefined;
|
||||
var decrypted: [64]u8 = undefined;
|
||||
|
||||
const enc_len = try alice.send(msg, &encrypted);
|
||||
const dec_result = try bob.receive(encrypted[0..enc_len], &decrypted);
|
||||
try std.testing.expectEqualStrings(msg, dec_result);
|
||||
|
||||
// Bob sends to Alice
|
||||
const reply = "Hello, Alice!";
|
||||
const enc_len2 = try bob.send(reply, &encrypted);
|
||||
const dec_result2 = try alice.receive(encrypted[0..enc_len2], &decrypted);
|
||||
try std.testing.expectEqualStrings(reply, dec_result2);
|
||||
}
|
||||
|
||||
test "FramedTransport" {
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const key2 = [_]u8{0x43} ** 32;
|
||||
const hash = [_]u8{0x00} ** 32;
|
||||
|
||||
var alice = FramedTransport.init(Transport.init(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key2),
|
||||
hash,
|
||||
));
|
||||
var bob = FramedTransport.init(Transport.init(
|
||||
cipher.CipherState.initWithKey(key2),
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
hash,
|
||||
));
|
||||
|
||||
const msg = "Framed message test";
|
||||
var buffer: [128]u8 = undefined;
|
||||
var decrypted: [64]u8 = undefined;
|
||||
|
||||
// Send framed
|
||||
const frame_len = try alice.sendFramed(msg, &buffer);
|
||||
|
||||
// Peek length
|
||||
const expected_len = FramedTransport.peekFrameLength(&buffer);
|
||||
try std.testing.expect(expected_len != null);
|
||||
try std.testing.expect(expected_len.? == frame_len);
|
||||
|
||||
// Receive framed
|
||||
const result = try bob.receiveFramed(buffer[0..frame_len], &decrypted);
|
||||
try std.testing.expectEqualStrings(msg, result);
|
||||
}
|
||||
|
||||
test "Transport rekey" {
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const key2 = [_]u8{0x43} ** 32;
|
||||
const hash = [_]u8{0x00} ** 32;
|
||||
|
||||
var alice = Transport.init(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key2),
|
||||
hash,
|
||||
);
|
||||
var bob = Transport.init(
|
||||
cipher.CipherState.initWithKey(key2),
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
hash,
|
||||
);
|
||||
|
||||
// Send and receive a message before rekey to sync nonces
|
||||
var encrypted: [64]u8 = undefined;
|
||||
var decrypted: [64]u8 = undefined;
|
||||
const pre_len = try alice.send("before rekey", &encrypted);
|
||||
const pre_result = try bob.receive(encrypted[0..pre_len], &decrypted);
|
||||
try std.testing.expectEqualStrings("before rekey", pre_result);
|
||||
|
||||
// Rekey both (must be coordinated - both sides rekey at the same point)
|
||||
alice.rekeyOutbound();
|
||||
bob.rekeyInbound();
|
||||
|
||||
// Send a message after rekey
|
||||
const enc_len = try alice.send("after rekey", &encrypted);
|
||||
const result = try bob.receive(encrypted[0..enc_len], &decrypted);
|
||||
try std.testing.expectEqualStrings("after rekey", result);
|
||||
}
|
||||
|
||||
test "Transport auto-rekey at threshold" {
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const key2 = [_]u8{0x43} ** 32;
|
||||
const hash = [_]u8{0x00} ** 32;
|
||||
|
||||
var alice = Transport.init(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key2),
|
||||
hash,
|
||||
);
|
||||
var bob = Transport.init(
|
||||
cipher.CipherState.initWithKey(key2),
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
hash,
|
||||
);
|
||||
|
||||
// Verify auto-rekey is enabled by default
|
||||
try std.testing.expect(alice.auto_rekey_enabled);
|
||||
try std.testing.expect(bob.auto_rekey_enabled);
|
||||
|
||||
// Set nonces to just before the threshold
|
||||
const threshold = noise.REKEY_AFTER_MESSAGES;
|
||||
alice.setNoncesForTesting(threshold - 1, 0);
|
||||
bob.setNoncesForTesting(0, threshold - 1);
|
||||
|
||||
try std.testing.expectEqual(@as(u32, 0), alice.send_rekey_count);
|
||||
try std.testing.expectEqual(@as(u32, 0), bob.recv_rekey_count);
|
||||
|
||||
// Send a message - this should trigger auto-rekey on alice's send cipher
|
||||
var encrypted: [64]u8 = undefined;
|
||||
var decrypted: [64]u8 = undefined;
|
||||
const enc_len = try alice.send("trigger rekey", &encrypted);
|
||||
|
||||
// Alice should have rekeyed after send
|
||||
try std.testing.expectEqual(@as(u32, 1), alice.send_rekey_count);
|
||||
|
||||
// Bob receives - this should trigger auto-rekey on bob's recv cipher
|
||||
const result = try bob.receive(encrypted[0..enc_len], &decrypted);
|
||||
try std.testing.expectEqualStrings("trigger rekey", result);
|
||||
|
||||
// Bob should have rekeyed after receive
|
||||
try std.testing.expectEqual(@as(u32, 1), bob.recv_rekey_count);
|
||||
|
||||
// Now both should have rekeyed, verify they can still communicate
|
||||
const enc_len2 = try alice.send("after auto-rekey", &encrypted);
|
||||
const result2 = try bob.receive(encrypted[0..enc_len2], &decrypted);
|
||||
try std.testing.expectEqualStrings("after auto-rekey", result2);
|
||||
|
||||
// And in the other direction
|
||||
const enc_len3 = try bob.send("bob to alice", &encrypted);
|
||||
const result3 = try alice.receive(encrypted[0..enc_len3], &decrypted);
|
||||
try std.testing.expectEqualStrings("bob to alice", result3);
|
||||
}
|
||||
|
||||
test "Transport auto-rekey disabled" {
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const key2 = [_]u8{0x43} ** 32;
|
||||
const hash = [_]u8{0x00} ** 32;
|
||||
|
||||
var alice = Transport.initNoAutoRekey(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key2),
|
||||
hash,
|
||||
);
|
||||
|
||||
// Verify auto-rekey is disabled
|
||||
try std.testing.expect(!alice.auto_rekey_enabled);
|
||||
|
||||
// Set nonce to threshold
|
||||
const threshold = noise.REKEY_AFTER_MESSAGES;
|
||||
alice.setNoncesForTesting(threshold - 1, 0);
|
||||
|
||||
// Send a message
|
||||
var encrypted: [64]u8 = undefined;
|
||||
_ = try alice.send("no rekey", &encrypted);
|
||||
|
||||
// Should NOT have rekeyed
|
||||
try std.testing.expectEqual(@as(u32, 0), alice.send_rekey_count);
|
||||
}
|
||||
|
||||
test "Transport approaching rekey threshold" {
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const hash = [_]u8{0x00} ** 32;
|
||||
|
||||
var transport = Transport.init(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
hash,
|
||||
);
|
||||
|
||||
// Initially not approaching threshold
|
||||
try std.testing.expect(!transport.isApproachingRekey());
|
||||
|
||||
// Set to warning threshold
|
||||
transport.setNoncesForTesting(noise.REKEY_WARNING_THRESHOLD, 0);
|
||||
try std.testing.expect(transport.isApproachingRekey());
|
||||
|
||||
// Set recv to warning threshold instead
|
||||
transport.setNoncesForTesting(0, noise.REKEY_WARNING_THRESHOLD);
|
||||
try std.testing.expect(transport.isApproachingRekey());
|
||||
}
|
||||
|
||||
test "Transport getHandshakeHash" {
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const hash = [_]u8{0xAB} ** 32;
|
||||
|
||||
const transport = Transport.init(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
hash,
|
||||
);
|
||||
|
||||
const returned_hash = transport.getHandshakeHash();
|
||||
try std.testing.expectEqualSlices(u8, &hash, &returned_hash);
|
||||
}
|
||||
|
||||
test "Transport getTotalRekeyCount" {
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const hash = [_]u8{0x00} ** 32;
|
||||
|
||||
var transport = Transport.init(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
hash,
|
||||
);
|
||||
|
||||
try std.testing.expectEqual(@as(u32, 0), transport.getTotalRekeyCount());
|
||||
|
||||
// Manually rekey
|
||||
transport.rekeyOutbound();
|
||||
transport.send_rekey_count += 1;
|
||||
try std.testing.expectEqual(@as(u32, 1), transport.getTotalRekeyCount());
|
||||
}
|
||||
|
||||
test "Transport getSendNonce and getRecvNonce" {
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const hash = [_]u8{0x00} ** 32;
|
||||
|
||||
var transport = Transport.init(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
hash,
|
||||
);
|
||||
|
||||
try std.testing.expectEqual(@as(u64, 0), transport.getSendNonce());
|
||||
try std.testing.expectEqual(@as(u64, 0), transport.getRecvNonce());
|
||||
|
||||
transport.setNoncesForTesting(100, 200);
|
||||
try std.testing.expectEqual(@as(u64, 100), transport.getSendNonce());
|
||||
try std.testing.expectEqual(@as(u64, 200), transport.getRecvNonce());
|
||||
}
|
||||
|
||||
test "Transport setAutoRekey" {
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const hash = [_]u8{0x00} ** 32;
|
||||
|
||||
var transport = Transport.init(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
hash,
|
||||
);
|
||||
|
||||
// Default is enabled
|
||||
try std.testing.expect(transport.auto_rekey_enabled);
|
||||
|
||||
transport.setAutoRekey(false);
|
||||
try std.testing.expect(!transport.auto_rekey_enabled);
|
||||
|
||||
transport.setAutoRekey(true);
|
||||
try std.testing.expect(transport.auto_rekey_enabled);
|
||||
}
|
||||
|
||||
test "FramedTransport init" {
|
||||
const key1 = [_]u8{0x42} ** 32;
|
||||
const hash = [_]u8{0x00} ** 32;
|
||||
|
||||
const inner = Transport.init(
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
cipher.CipherState.initWithKey(key1),
|
||||
hash,
|
||||
);
|
||||
|
||||
const framed = FramedTransport.init(inner);
|
||||
try std.testing.expectEqualSlices(u8, &hash, &framed.inner.handshake_hash);
|
||||
}
|
||||
Reference in New Issue
Block a user