From b67ae9ec59e46beff4832852e0950ca9cc3057b2 Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Fri, 26 Jun 2026 16:22:01 +0200 Subject: [PATCH 1/2] Fixed a bug in the build file, fixed functions signature for aes and ed25519, made visibility changes in packet and made the enums needed for destination --- build.zig | 2 +- src/crypto/aes.zig | 8 ++++---- src/crypto/ed25519.zig | 14 +++++++++++--- src/destination.zig | 42 ++++++++++++++++++++++++++++++++++++++++++ src/packet.zig | 18 +++++++++--------- 5 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 src/destination.zig diff --git a/build.zig b/build.zig index ffa5b3d..cac537c 100644 --- a/build.zig +++ b/build.zig @@ -23,7 +23,7 @@ pub fn build(b: *std.Build) void { // Configuration .target = b.standardTargetOptions(.{}), .optimize = b.standardOptimizeOption(.{}), - .linkage = if (opt_shared) .static else .dynamic, + .linkage = if (opt_shared) .dynamic else .static, .test_step = opt_test }; diff --git a/src/crypto/aes.zig b/src/crypto/aes.zig index ec77f0c..853f39f 100644 --- a/src/crypto/aes.zig +++ b/src/crypto/aes.zig @@ -4,18 +4,18 @@ pub const CBC256_Implementation = struct { pub const KEY_SIZE = 32; // NOTE: data should be padded to make its length be a multiple of BLOCK_SIZE - encypt: *const fn (key: *const [KEY_SIZE]u8, iv: *const [BLOCK_SIZE]u8, data: *const []u8, ) void, - decrypt: *const fn (key: *const [KEY_SIZE]u8, iv: *const [BLOCK_SIZE]u8, data: *const []u8, ) void, + encypt: *const fn (key: *const [KEY_SIZE]u8, iv: *const [BLOCK_SIZE]u8, data: *const []u8) void, + decrypt: *const fn (key: *const [KEY_SIZE]u8, iv: *const [BLOCK_SIZE]u8, data: *const []u8) void, }; // TODO pub const CBC265_DefaultImplementation = struct { - pub fn encrypt( key: *const [32]u8, iv: *const [16]u8, data: *const []u8 ) void { + pub fn encrypt(key: *const [32]u8, iv: *const [16]u8, data: *const []u8) !void { _ = key; _ = iv; _ = data; } - pub fn decrypt( key: *const [32]u8, iv: *const [16]u8, data: *const []u8 ) void { + pub fn decrypt(key: *const [32]u8, iv: *const [16]u8, data: *const []u8) !void { _ = key; _ = iv; _ = data; diff --git a/src/crypto/ed25519.zig b/src/crypto/ed25519.zig index 5ab0ca1..2da4bc1 100644 --- a/src/crypto/ed25519.zig +++ b/src/crypto/ed25519.zig @@ -1,19 +1,27 @@ pub const Implementation = struct { + pub const KEY_SIZE = 32; pub const SIGNATURE_SIZE = 64; + + generateKey: *const fn (privkey_buffer: *[KEY_SIZE]u8) void, sign: *const fn (key: *const [KEY_SIZE]u8, data: *const []const u8, signature_out: *const [SIGNATURE_SIZE]u8) void, verify: *const fn (key: *const [KEY_SIZE]u8, data: *const []const u8, signature: *const [SIGNATURE_SIZE]u8) void, }; // TODO pub const defaultImplementation = struct { - fn sign(key: *const [32]u8, data: *const []const u8, signature_out: *const [64]u8) void { + + pub fn generateKeys(privkey_buffer: *[32]u8) !void { + _ = privkey_buffer; + } + + pub fn sign(key: *const [32]u8, data: *const []const u8, signature_out: *const [64]u8) !void { _ = key; _ = data; _ = signature_out; - } - fn verify(signature: *const [64]u8, data: *const []const u8) void { + + pub fn verify(signature: *const [64]u8, data: *const []const u8) !void { _ = signature; _ = data; } diff --git a/src/destination.zig b/src/destination.zig new file mode 100644 index 0000000..77ea87a --- /dev/null +++ b/src/destination.zig @@ -0,0 +1,42 @@ +///////////////// Constants +// + +///////////////// Enums +// + +pub const Type = enum(u2) { + single = 0x00, + group = 0x01, + plain = 0x02, + link = 0x03, +}; + +pub const ProofStrategy = enum(u2) { + prove_none = 0x21, + prove_app = 0x22, + prove_all = 0x23, +}; + +pub const RequestPolicy = enum(u2) { + allow_none = 0x00, + allow_all = 0x01, + allow_list = 0x02, +}; + +pub const Direction = enum(u2) { + in = 0x11, + out = 0x12, +}; + +///////////////// Functions +// + +//////// Helper functions +// + +//////// Public functions +// + +///////////////// Tests +// + diff --git a/src/packet.zig b/src/packet.zig index 7589890..2d1290c 100644 --- a/src/packet.zig +++ b/src/packet.zig @@ -7,39 +7,39 @@ const ADDRESS_SIZE = 16; ///////////////// Enums // -const IfacFlag = enum(u1) { +pub const IfacFlag = enum(u1) { open = 0, // Packet for publically accessible interface authenticated = 1, // Interface authentication is included in packet }; -const HeaderType = enum(u1) { +pub const HeaderType = enum(u1) { type1 = 0, // One address field type2 = 1, // Two address fields }; -const ContextFlag = enum(u1) { +pub const ContextFlag = enum(u1) { // Meaning depends on packet context unset = 0, set = 1, }; -const PropagationType = enum(u1) { +pub const PropagationType = enum(u1) { broadcast = 0, transport = 1 }; -const DestinationType = enum(u2) { +pub const DestinationType = enum(u2) { single = 0b0, group = 0b01, plain = 0b10, link = 0b11, }; -const PacketType = enum(u2) { +pub const PacketType = enum(u2) { data = 0b0, announce = 0b01, link_request = 0b10, proof = 0b11, }; -const PacketContext = enum(u8) { +pub const PacketContext = enum(u8) { none = 0x00, // Generic data packet resource = 0x01, // Packet is part of a resource resource_adv = 0x02, // Packet is a resource advertisement @@ -66,7 +66,7 @@ const PacketContext = enum(u8) { ///////////////// Structs // -const PacketHeader = packed struct { +pub const PacketHeader = packed struct { ifac: IfacFlag, header: HeaderType, context: ContextFlag, @@ -76,7 +76,7 @@ const PacketHeader = packed struct { hops: u8, }; -const Packet = struct { +pub const Packet = struct { header: PacketHeader, address1: [ADDRESS_SIZE]u8, address2: [ADDRESS_SIZE]u8, From cb72a9cdc9c9160ee755dd6f70162fb78a7b2a14 Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Fri, 26 Jun 2026 18:24:42 +0200 Subject: [PATCH 2/2] Refactored packet.zig --- src/packet.zig | 68 ++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/src/packet.zig b/src/packet.zig index 2d1290c..a38294e 100644 --- a/src/packet.zig +++ b/src/packet.zig @@ -1,3 +1,15 @@ +///////////////// Module +// + +const Self = @This(); + +header: PacketHeader, +address1: [ADDRESS_SIZE]u8, +address2: [ADDRESS_SIZE]u8, +context: PacketContext, +data: []u8, + + ///////////////// Constants // @@ -76,14 +88,6 @@ pub const PacketHeader = packed struct { hops: u8, }; -pub const Packet = struct { - header: PacketHeader, - address1: [ADDRESS_SIZE]u8, - address2: [ADDRESS_SIZE]u8, - context: PacketContext, - data: []u8, -}; - ///////////////// Functions // @@ -115,7 +119,7 @@ fn getPacketSize(data_size: usize, has_two_addresses: bool) usize { //////// Public functions // -pub fn serializePacket(packet: Packet, output_buffer: []u8) !usize { +pub fn serialize(packet: *const Self, output_buffer: []u8) !usize { const has_two_addresses = packet.header.header == HeaderType.type2; const target_size = getPacketSize(packet.data.len, has_two_addresses); @@ -151,17 +155,17 @@ pub fn serializePacket(packet: Packet, output_buffer: []u8) !usize { } // Reads the message buffer and builds the corresponding packet struct inside dest_packet -pub fn deserializePacket(message_buffer: []u8, dest_packet: *Packet) !void { +pub fn deserialize(packet: *Self, message_buffer: []u8) !void { var offset: usize = 0; - offset = try copyToPacket(&dest_packet.header, message_buffer, offset, @sizeOf(PacketHeader)); - offset = try copyToPacket(&dest_packet.address1, message_buffer, offset, ADDRESS_SIZE); - const has_two_addresses = dest_packet.header.header == HeaderType.type2; + offset = try copyToPacket(&packet.header, message_buffer, offset, @sizeOf(PacketHeader)); + offset = try copyToPacket(&packet.address1, message_buffer, offset, ADDRESS_SIZE); + const has_two_addresses = packet.header.header == HeaderType.type2; if (has_two_addresses) { - offset = try copyToPacket(&dest_packet.address2, message_buffer, offset, ADDRESS_SIZE); + offset = try copyToPacket(&packet.address2, message_buffer, offset, ADDRESS_SIZE); } - offset = try copyToPacket(&dest_packet.context, message_buffer, offset, @sizeOf(PacketContext)); - offset = try copyToPacket(dest_packet.data, message_buffer, offset, message_buffer.len - offset); + offset = try copyToPacket(&packet.context, message_buffer, offset, @sizeOf(PacketContext)); + offset = try copyToPacket(packet.data, message_buffer, offset, message_buffer.len - offset); } ///////////////// Tests @@ -177,10 +181,10 @@ const asBytes = std.mem.asBytes; // // Serializes the given packet and makes sure the output is correct -fn testPacketSerialization(packet: Packet) !void { - const buf_size = comptime @sizeOf(Packet) + MAX_DATA_SIZE; +fn testPacketSerialization(packet: *const Self) !void { + const buf_size = comptime @sizeOf(Self) + MAX_DATA_SIZE; var buf: [buf_size]u8 = undefined; - const res: usize = try serializePacket(packet, &buf); + const res: usize = try packet.serialize(&buf); const has_second_address = packet.header.header == HeaderType.type2; const expected_res: usize = getPacketSize(packet.data.len, has_second_address); @@ -216,7 +220,7 @@ fn headersEql(h1: *const PacketHeader, h2: *const PacketHeader) bool { and h1.hops == h2.hops; } -fn packetsEql(p1: *const Packet, p2: *const Packet) bool { +fn packetsEql(p1: *const Self, p2: *const Self) bool { return headersEql(&p1.header, &p2.header) and memeql(u8, &p1.address1, &p2.address1) and memeql(u8, &p1.address2, &p2.address2) @@ -246,7 +250,7 @@ test "Basic serialization: header type 1, max data size" { const data_size = MAX_DATA_SIZE; var data: [data_size]u8 = undefined; - const packet: Packet = .{ + const packet: Self = .{ .header = header, .address1 = undefined, .address2 = undefined, @@ -254,7 +258,7 @@ test "Basic serialization: header type 1, max data size" { .data = &data, }; - try testPacketSerialization(packet); + try packet.testPacketSerialization(); } test "Basic serialization: header type 2, max data size" { @@ -271,7 +275,7 @@ test "Basic serialization: header type 2, max data size" { const data_size = MAX_DATA_SIZE; var data: [data_size]u8 = undefined; - const packet: Packet = .{ + const packet: Self = .{ .header = header, .address1 = undefined, .address2 = undefined, @@ -279,7 +283,7 @@ test "Basic serialization: header type 2, max data size" { .data = &data, }; - try testPacketSerialization(packet); + try packet.testPacketSerialization(); } test "Basic serialization: header type 1, medium data size" { @@ -296,7 +300,7 @@ test "Basic serialization: header type 1, medium data size" { const data_size = MAX_DATA_SIZE / 2 + 3; var data: [data_size]u8 = undefined; - const packet: Packet = .{ + const packet: Self = .{ .header = header, .address1 = undefined, .address2 = undefined, @@ -304,7 +308,7 @@ test "Basic serialization: header type 1, medium data size" { .data = &data, }; - try testPacketSerialization(packet); + try packet.testPacketSerialization(); } test "Basic serialization: header type 2, medium data size" { @@ -321,7 +325,7 @@ test "Basic serialization: header type 2, medium data size" { const data_size = MAX_DATA_SIZE / 2 + 3; var data: [data_size]u8 = undefined; - const packet: Packet = .{ + const packet: Self = .{ .header = header, .address1 = undefined, .address2 = undefined, @@ -329,7 +333,7 @@ test "Basic serialization: header type 2, medium data size" { .data = &data, }; - try testPacketSerialization(packet); + try packet.testPacketSerialization(); } test "Serialize / Deserialize Packet: Header type2, Medium data size" { @@ -346,7 +350,7 @@ test "Serialize / Deserialize Packet: Header type2, Medium data size" { const data_size = MAX_DATA_SIZE / 2 + 3; var data: [data_size]u8 = undefined; - const packet: Packet = .{ + const packet: Self = .{ .header = header, .address1 = undefined, .address2 = undefined, @@ -356,11 +360,11 @@ test "Serialize / Deserialize Packet: Header type2, Medium data size" { const buf_size = comptime @sizeOf(PacketHeader) + 2*ADDRESS_SIZE + @sizeOf(PacketContext) + data_size; var buf: [buf_size]u8 = undefined; - var res_packet: Packet = undefined; + var res_packet: Self = undefined; var res_data: [data_size]u8 = undefined; res_packet.data = &res_data; - _ = try serializePacket(packet, &buf); - try deserializePacket(&buf, &res_packet); + _ = try packet.serialize(&buf); + try res_packet.deserialize(&buf); try expect(packetsEql(&res_packet, &packet)); }