Skip to content

24 Byte Crypto #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions decoder/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn build(b: *std.Build) !void {
.target = target,
.name = "main",
.optimize = .ReleaseSafe,
.link_libc = true,
});

const unit_tests = b.addTest(.{
Expand Down
6 changes: 3 additions & 3 deletions decoder/lib/crypto.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const std = @import("std");

pub fn decrypt(data: []u8, key: [16]u8) void {
pub fn decrypt(data: []u8, key: [24]u8) void {
var extended_key: [32]u8 = undefined;
@memcpy(extended_key[0..16], key[0..16]);
@memcpy(extended_key[16..32], key[0..16]);
@memcpy(extended_key[0..24], key[0..24]);
@memcpy(extended_key[24..32], key[0..8]);
std.crypto.stream.salsa.Salsa20.xor(data, data, 0, extended_key, std.mem.zeroes([8]u8));
}
61 changes: 33 additions & 28 deletions decoder/lib/subscription.zig

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions decoder/subscribe.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn execute(body: []u8) !void {
const header: *const SubscribeHeader = @ptrCast(body.ptr);
const channel_index = header.channel - 1;

if (root.subscriptions[channel_index]) |*subscription| subscription.deinit();
root.subscriptions[channel_index] = lib.Subscription.init(header.start, header.end, body[@sizeOf(SubscribeHeader)..]);

try flash.saveSubscriptions(@truncate(channel_index));
Expand Down
4 changes: 2 additions & 2 deletions design/ectf25_design/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def hash(data: bytes):
return blake3(data).digest(length=16)
return blake3(data).digest(length=24)


class Encoder:
Expand Down Expand Up @@ -74,7 +74,7 @@ def encode(self, channel: int, frame: bytes, timestamp: int) -> bytes:
else:
curr = hash(curr + RIGHT_SALT)

encrypted_frame = Salsa20.new(key=curr+curr, nonce=bytes([0 for _ in range(8)])).encrypt(frame)
encrypted_frame = Salsa20.new(key=curr+curr[:8], nonce=bytes([0 for _ in range(8)])).encrypt(frame)

message = struct.pack("<IQ", channel, timestamp) + encrypted_frame

Expand Down
2 changes: 1 addition & 1 deletion design/ectf25_design/gen_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def gen_secrets(channels: list[int]) -> bytes:
signer = eddsa.new(keypair, "rfc8032")

secrets = {
"seeds": {str(channel): os.urandom(16).hex() for channel in channels},
"seeds": {str(channel): os.urandom(24).hex() for channel in channels},
"subscription_salt": os.urandom(32).hex(),
"public_key": keypair.public_key().export_key(format="raw").hex(),
"private_key": keypair.export_key(format="PEM"),
Expand Down
4 changes: 1 addition & 3 deletions design/ectf25_design/gen_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def hash(data: bytes):
return blake3(data).digest(length=16)
return blake3(data).digest(length=24)


@dataclass(frozen=True)
Expand Down Expand Up @@ -62,11 +62,9 @@ def gen_subscription(
curr = hash(curr + LEFT_SALT)
else:
curr = hash(curr + RIGHT_SALT)
print(root, curr.hex())
hashes.append(curr)

subscription_salt = bytes.fromhex(secrets["subscription_salt"])
print(f"{subscription_salt.hex()}{device_id:08x}")
key = blake3(f"{subscription_salt.hex()}{device_id:08x}".encode()).digest()

# Pack the subscription. This will be sent to the decoder with ectf25.tv.subscribe
Expand Down
Binary file modified docs/design.pdf
Binary file not shown.
6 changes: 3 additions & 3 deletions docs/design.typ
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ This aspect of the design secures the system in the following ways:
[Start Timestamp], [8 bytes], bytesize(8),
[End Timestamp], [8 bytes], bytesize(8),
[Packed Subtree Root Hashes],
${16n "bytes" | n in NN, 1 <= n <= 126 }$,
box(box(bytesize(16)) + " " + box($...n "times"$)),
${24n "bytes" | n in NN^+, n <= 126 }$,
box(box(bytesize(24)) + " " + box($...n "times"$)),
),
),
caption: [Structure of a subscribe packet],
Expand Down Expand Up @@ -420,7 +420,7 @@ def get_nodes(a: int, b: int) -> list[Node]:
while a <= b:
power = min((b - a + 1).bit_length() - 1, ctz(a))
ranges.append(Node(offset=a, power=power))
a += 1 << power
a += 2 ** power

return ranges
```
Expand Down
57 changes: 29 additions & 28 deletions docs/pages/page-6.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions docs/pages/page-9.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.