Compare commits

..

No commits in common. "cb72a9cdc9c9160ee755dd6f70162fb78a7b2a14" and "693d7cfe5138174ada6f83b11e97b8c73bf95397" have entirely different histories.

5 changed files with 48 additions and 102 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) .dynamic else .static,
.linkage = if (opt_shared) .static else .dynamic,
.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,27 +1,19 @@
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 {
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 {
fn sign(key: *const [32]u8, data: *const []const u8, signature_out: *const [64]u8) void {
_ = key;
_ = data;
_ = signature_out;
}
pub fn verify(signature: *const [64]u8, data: *const []const u8) !void {
}
fn verify(signature: *const [64]u8, data: *const []const u8) void {
_ = signature;
_ = data;
}

View file

@ -1,42 +0,0 @@
///////////////// 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,15 +1,3 @@
///////////////// Module
//
const Self = @This();
header: PacketHeader,
address1: [ADDRESS_SIZE]u8,
address2: [ADDRESS_SIZE]u8,
context: PacketContext,
data: []u8,
///////////////// Constants
//
@ -19,39 +7,39 @@ const ADDRESS_SIZE = 16;
///////////////// Enums
//
pub const IfacFlag = enum(u1) {
const IfacFlag = enum(u1) {
open = 0, // Packet for publically accessible interface
authenticated = 1, // Interface authentication is included in packet
};
pub const HeaderType = enum(u1) {
const HeaderType = enum(u1) {
type1 = 0, // One address field
type2 = 1, // Two address fields
};
pub const ContextFlag = enum(u1) {
const ContextFlag = enum(u1) {
// Meaning depends on packet context
unset = 0,
set = 1,
};
pub const PropagationType = enum(u1) {
const PropagationType = enum(u1) {
broadcast = 0,
transport = 1
};
pub const DestinationType = enum(u2) {
const DestinationType = enum(u2) {
single = 0b0,
group = 0b01,
plain = 0b10,
link = 0b11,
};
pub const PacketType = enum(u2) {
const PacketType = enum(u2) {
data = 0b0,
announce = 0b01,
link_request = 0b10,
proof = 0b11,
};
pub const PacketContext = enum(u8) {
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
@ -78,7 +66,7 @@ pub const PacketContext = enum(u8) {
///////////////// Structs
//
pub const PacketHeader = packed struct {
const PacketHeader = packed struct {
ifac: IfacFlag,
header: HeaderType,
context: ContextFlag,
@ -88,6 +76,14 @@ pub const PacketHeader = packed struct {
hops: u8,
};
const Packet = struct {
header: PacketHeader,
address1: [ADDRESS_SIZE]u8,
address2: [ADDRESS_SIZE]u8,
context: PacketContext,
data: []u8,
};
///////////////// Functions
//
@ -119,7 +115,7 @@ fn getPacketSize(data_size: usize, has_two_addresses: bool) usize {
//////// Public functions
//
pub fn serialize(packet: *const Self, output_buffer: []u8) !usize {
pub fn serializePacket(packet: Packet, output_buffer: []u8) !usize {
const has_two_addresses = packet.header.header == HeaderType.type2;
const target_size = getPacketSize(packet.data.len, has_two_addresses);
@ -155,17 +151,17 @@ pub fn serialize(packet: *const Self, output_buffer: []u8) !usize {
}
// Reads the message buffer and builds the corresponding packet struct inside dest_packet
pub fn deserialize(packet: *Self, message_buffer: []u8) !void {
pub fn deserializePacket(message_buffer: []u8, dest_packet: *Packet) !void {
var offset: usize = 0;
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;
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;
if (has_two_addresses) {
offset = try copyToPacket(&packet.address2, message_buffer, offset, ADDRESS_SIZE);
offset = try copyToPacket(&dest_packet.address2, message_buffer, offset, ADDRESS_SIZE);
}
offset = try copyToPacket(&packet.context, message_buffer, offset, @sizeOf(PacketContext));
offset = try copyToPacket(packet.data, message_buffer, offset, message_buffer.len - offset);
offset = try copyToPacket(&dest_packet.context, message_buffer, offset, @sizeOf(PacketContext));
offset = try copyToPacket(dest_packet.data, message_buffer, offset, message_buffer.len - offset);
}
///////////////// Tests
@ -181,10 +177,10 @@ const asBytes = std.mem.asBytes;
//
// Serializes the given packet and makes sure the output is correct
fn testPacketSerialization(packet: *const Self) !void {
const buf_size = comptime @sizeOf(Self) + MAX_DATA_SIZE;
fn testPacketSerialization(packet: Packet) !void {
const buf_size = comptime @sizeOf(Packet) + MAX_DATA_SIZE;
var buf: [buf_size]u8 = undefined;
const res: usize = try packet.serialize(&buf);
const res: usize = try serializePacket(packet, &buf);
const has_second_address = packet.header.header == HeaderType.type2;
const expected_res: usize = getPacketSize(packet.data.len, has_second_address);
@ -220,7 +216,7 @@ fn headersEql(h1: *const PacketHeader, h2: *const PacketHeader) bool {
and h1.hops == h2.hops;
}
fn packetsEql(p1: *const Self, p2: *const Self) bool {
fn packetsEql(p1: *const Packet, p2: *const Packet) bool {
return headersEql(&p1.header, &p2.header)
and memeql(u8, &p1.address1, &p2.address1)
and memeql(u8, &p1.address2, &p2.address2)
@ -250,7 +246,7 @@ test "Basic serialization: header type 1, max data size" {
const data_size = MAX_DATA_SIZE;
var data: [data_size]u8 = undefined;
const packet: Self = .{
const packet: Packet = .{
.header = header,
.address1 = undefined,
.address2 = undefined,
@ -258,7 +254,7 @@ test "Basic serialization: header type 1, max data size" {
.data = &data,
};
try packet.testPacketSerialization();
try testPacketSerialization(packet);
}
test "Basic serialization: header type 2, max data size" {
@ -275,7 +271,7 @@ test "Basic serialization: header type 2, max data size" {
const data_size = MAX_DATA_SIZE;
var data: [data_size]u8 = undefined;
const packet: Self = .{
const packet: Packet = .{
.header = header,
.address1 = undefined,
.address2 = undefined,
@ -283,7 +279,7 @@ test "Basic serialization: header type 2, max data size" {
.data = &data,
};
try packet.testPacketSerialization();
try testPacketSerialization(packet);
}
test "Basic serialization: header type 1, medium data size" {
@ -300,7 +296,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: Self = .{
const packet: Packet = .{
.header = header,
.address1 = undefined,
.address2 = undefined,
@ -308,7 +304,7 @@ test "Basic serialization: header type 1, medium data size" {
.data = &data,
};
try packet.testPacketSerialization();
try testPacketSerialization(packet);
}
test "Basic serialization: header type 2, medium data size" {
@ -325,7 +321,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: Self = .{
const packet: Packet = .{
.header = header,
.address1 = undefined,
.address2 = undefined,
@ -333,7 +329,7 @@ test "Basic serialization: header type 2, medium data size" {
.data = &data,
};
try packet.testPacketSerialization();
try testPacketSerialization(packet);
}
test "Serialize / Deserialize Packet: Header type2, Medium data size" {
@ -350,7 +346,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: Self = .{
const packet: Packet = .{
.header = header,
.address1 = undefined,
.address2 = undefined,
@ -360,11 +356,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: Self = undefined;
var res_packet: Packet = undefined;
var res_data: [data_size]u8 = undefined;
res_packet.data = &res_data;
_ = try packet.serialize(&buf);
try res_packet.deserialize(&buf);
_ = try serializePacket(packet, &buf);
try deserializePacket(&buf, &res_packet);
try expect(packetsEql(&res_packet, &packet));
}