summaryrefslogtreecommitdiff
path: root/lib/pawprint.zig
blob: 8ca4ea7148e64b5e78943cf57152f4a707d0f2fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const std = @import("std");

const builtin = std.builtin;
const mem = std.mem;

const argon2 = std.crypto.pwhash.argon2;
const ed25519 = std.crypto.sign.Ed25519;
const xx32 = std.hash.XxHash32;

pub const pawprint = struct {
    pub const Context = struct {
        alloc: mem.Allocator,
        io: std.Io,
    };

    pub const identity = struct {
        pub const UnlockError = error{
            ChecksumMismatch,
        };

        const salt_len = 16;

        pub fn passphrase_kdf(
            ctx: *const Context,
            salt: [salt_len]u8,
            passphrase: []const u8,
        ) ![36]u8 {
            var derived: [36]u8 = undefined;
            try argon2.kdf(
                ctx.alloc,
                &derived,
                passphrase,
                &salt,
                argon2.Params.sensitive_2id,
                argon2.Mode.argon2id,
                ctx.io,
            );

            return derived;
        }

        pub const Locked = struct {
            bytes: [36]u8,
            salt: [salt_len]u8,

            pub fn unlock(
                self: *const Locked,
                ctx: *const Context,
                passphrase: []const u8,
            ) !Unlocked {
                var seed = try passphrase_kdf(ctx, self.salt, passphrase);
                for (0..36) |i| {
                    seed[i] ^= self.bytes[i];
                }

                const csum = xx32.hash(0, seed[0..32]);

                var csum_bytes: [4]u8 = undefined;
                mem.writeInt(u32, &csum_bytes, csum, builtin.Endian.little);

                if (!mem.eql(u8, &csum_bytes, seed[32..36])) {
                    return UnlockError.ChecksumMismatch;
                }

                return Unlocked{
                    .keypair = try ed25519.KeyPair.generateDeterministic(
                        seed[0..32].*,
                    ),
                };
            }

            pub fn drop(self: *volatile Locked) void {
                self.* = std.mem.zeroes(Locked);
            }
        };

        pub const Unlocked = struct {
            keypair: ed25519.KeyPair,

            pub fn generate(io: std.Io) Unlocked {
                return Unlocked{
                    .keypair = ed25519.KeyPair.generate(io),
                };
            }

            pub fn lock(
                self: *const Unlocked,
                ctx: *const Context,
                passphrase: []const u8,
            ) !Locked {
                var salt: [salt_len]u8 = undefined;
                try ctx.io.randomSecure(&salt);

                const derived = try passphrase_kdf(ctx, salt, passphrase);
                var locked = Locked{
                    .bytes = derived,
                    .salt = salt,
                };

                const seed = self.keypair.secret_key.seed();
                for (0..32) |i| {
                    locked.bytes[i] ^= seed[i];
                }

                const csum = xx32.hash(0, seed);

                var csum_bytes: [4]u8 = undefined;
                mem.writeInt(u32, &csum_bytes, csum, builtin.Endian.little);

                for (0..4) |i| {
                    locked.bytes[i + 32] ^= csum_bytes[i];
                }

                return locked;
            }

            pub fn drop(self: *volatile Unlocked) void {
                self.* = std.mem.zeroes(Unlocked);
            }
        };
    };
};

test "lock and unlock identity" {
    const ctx = pawprint.Context{
        .alloc = std.testing.allocator,
        .io = std.testing.io,
    };

    const ident = pawprint.identity.Unlocked.generate(ctx.io);
    const locked = try ident.lock(&ctx, "foobar123");
    const unlocked = try locked.unlock(&ctx, "foobar123");

    try std.testing.expectEqualDeep(ident, unlocked);
}

test "unlock with wrong passphrase" {
    const ctx = pawprint.Context{
        .alloc = std.testing.allocator,
        .io = std.testing.io,
    };

    const ident = pawprint.identity.Unlocked.generate(ctx.io);
    const locked = try ident.lock(&ctx, "foobar123");
    const unlocked = locked.unlock(&ctx, "barbaz456");

    try std.testing.expectEqual(
        unlocked,
        pawprint.identity.UnlockError.ChecksumMismatch,
    );
}

test "drop unlocked identity" {
    const ctx = pawprint.Context{
        .alloc = std.testing.allocator,
        .io = std.testing.io,
    };

    var ident = pawprint.identity.Unlocked.generate(ctx.io);
    ident.drop();

    try std.testing.expectEqual(
        mem.zeroes(pawprint.identity.Unlocked),
        ident,
    );
}

test "drop locked identity" {
    const ctx = pawprint.Context{
        .alloc = std.testing.allocator,
        .io = std.testing.io,
    };

    const ident = pawprint.identity.Unlocked.generate(ctx.io);
    var locked = try ident.lock(&ctx, "foobar123");
    locked.drop();

    try std.testing.expectEqual(
        mem.zeroes(pawprint.identity.Locked),
        locked,
    );
}