Skip to content

Commit

Permalink
cose changes
Browse files Browse the repository at this point in the history
  • Loading branch information
r4gus committed Jun 8, 2024
1 parent 9871dda commit bfa5d61
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/cose.zig
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ pub const Key = union(KeyTag) {
d: ?[32]u8 = null,
},

pub fn getAlg(self: *const @This()) Algorithm {
switch (self.*) {
.P256 => |k| return k.alg,
}
}

pub fn getPrivKey(self: *const @This()) []const u8 {
switch (self.*) {
.P256 => |k| {
return if (k.d) |d| d[0..] else null;
},
}
}

pub fn copySecure(self: *const @This()) @This() {
switch (self.*) {
.P256 => |k| {
Expand Down
34 changes: 34 additions & 0 deletions src/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,36 @@ pub fn ArrayBackedSlice(
buffer: T = undefined,
len: usize = 0,

pub const ByteWriter = struct {
raw: []u8,
i: *usize,

pub fn writeAll(self: *const @This(), in: []const u8) !void {
if (self.raw.len - self.i.* < in.len) {
return error.OutOfMemory;
}
@memcpy(self.raw[self.i.* .. self.i.* + in.len], in);
self.i.* += in.len;
}

pub fn writeByte(self: *const @This(), in: u8) !void {
if (self.i.* >= self.raw.len) {
return error.OutOfMemory;
}
self.raw[self.i.*] = in;
self.i.* += 1;
}
};

pub fn byteWriter(self: *@This()) !ByteWriter {
if (U != u8) return error.WrongType;

return .{
.raw = &self.buffer,
.i = &self.len,
};
}

pub fn fromSlice(s: ?[]const U) !?@This() {
if (s == null) return null;
if (s.?.len > size) return error.BufferTooSmall;
Expand All @@ -46,6 +76,10 @@ pub fn ArrayBackedSlice(
return self.buffer[0..self.len];
}

pub fn getMut(self: *@This()) []U {
return self.buffer[0..self.len];
}

pub fn set(self: *@This(), v: []const u8) !void {
if (v.len > self.buffer.len) return error.BufferTooSmall;
@memcpy(self.buffer[0..v.len], v);
Expand Down

0 comments on commit bfa5d61

Please sign in to comment.