Compare commits

...

2 commits

5 changed files with 102 additions and 48 deletions

View file

@ -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
};

View file

@ -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;

View file

@ -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;
}

42
src/destination.zig Normal file
View file

@ -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
//

View file

@ -1,3 +1,15 @@
///////////////// Module
//
const Self = @This();
header: PacketHeader,
address1: [ADDRESS_SIZE]u8,
address2: [ADDRESS_SIZE]u8,
context: PacketContext,
data: []u8,
///////////////// Constants
//
@ -7,39 +19,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 +78,7 @@ const PacketContext = enum(u8) {
///////////////// Structs
//
const PacketHeader = packed struct {
pub const PacketHeader = packed struct {
ifac: IfacFlag,
header: HeaderType,
context: ContextFlag,
@ -76,14 +88,6 @@ const PacketHeader = packed struct {
hops: u8,
};
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));
}