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
|
const std = @import("std");
const assert = std.debug.assert;
const crypto = std.crypto;
const debug = std.debug;
const Ghash = std.crypto.onetimeauth.Ghash;
const math = std.math;
const mem = std.mem;
const modes = crypto.core.modes;
const AuthenticationError = crypto.errors.AuthenticationError;
pub const Aes128Gcm = AesGcm(crypto.core.aes.Aes128);
pub const Aes256Gcm = AesGcm(crypto.core.aes.Aes256);
fn AesGcm(comptime Aes: anytype) type {
debug.assert(Aes.block.block_length == 16);
return struct {
pub const tag_length = 16;
pub const nonce_length = 12;
pub const key_length = Aes.key_bits / 8;
const zeros = [_]u8{0} ** 16;
/// `c`: The ciphertext buffer to write the encrypted data to.
/// `tag`: The authentication tag buffer to write the computed tag to.
/// `m`: The plaintext message to encrypt.
/// `ad`: The associated data to authenticate.
/// `npub`: The nonce to use for encryption.
/// `key`: The encryption key.
pub fn encrypt(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) void {
debug.assert(c.len == m.len);
debug.assert(m.len <= 16 * ((1 << 32) - 2));
const aes = Aes.initEnc(key);
var h: [16]u8 = undefined;
aes.encrypt(&h, &zeros);
var t: [16]u8 = undefined;
var j: [16]u8 = undefined;
j[0..nonce_length].* = npub;
mem.writeInt(u32, j[nonce_length..][0..4], 1, .big);
aes.encrypt(&t, &j);
const block_count = (math.divCeil(usize, ad.len, Ghash.block_length) catch unreachable) + (math.divCeil(usize, c.len, Ghash.block_length) catch unreachable) + 1;
var mac = Ghash.initForBlockCount(&h, block_count);
mac.update(ad);
mac.pad();
mem.writeInt(u32, j[nonce_length..][0..4], 2, .big);
modes.ctr(@TypeOf(aes), aes, c, m, j, .big);
mac.update(c[0..m.len][0..]);
mac.pad();
var final_block = h;
mem.writeInt(u64, final_block[0..8], @as(u64, ad.len) * 8, .big);
mem.writeInt(u64, final_block[8..16], @as(u64, m.len) * 8, .big);
mac.update(&final_block);
mac.final(tag);
for (t, 0..) |x, i| {
tag[i] ^= x;
}
}
/// `m`: Message
/// `c`: Ciphertext
/// `tag`: Authentication tag
/// `ad`: Associated data
/// `npub`: Public nonce
/// `k`: Private key
/// Asserts `c.len == m.len`.
///
/// Contents of `m` are undefined if an error is returned.
pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) AuthenticationError!void {
assert(c.len == m.len);
const aes = Aes.initEnc(key);
var h: [16]u8 = undefined;
aes.encrypt(&h, &zeros);
var t: [16]u8 = undefined;
var j: [16]u8 = undefined;
j[0..nonce_length].* = npub;
mem.writeInt(u32, j[nonce_length..][0..4], 1, .big);
aes.encrypt(&t, &j);
const block_count = (math.divCeil(usize, ad.len, Ghash.block_length) catch unreachable) + (math.divCeil(usize, c.len, Ghash.block_length) catch unreachable) + 1;
var mac = Ghash.initForBlockCount(&h, block_count);
mac.update(ad);
mac.pad();
mac.update(c);
mac.pad();
var final_block = h;
mem.writeInt(u64, final_block[0..8], @as(u64, ad.len) * 8, .big);
mem.writeInt(u64, final_block[8..16], @as(u64, m.len) * 8, .big);
mac.update(&final_block);
var computed_tag: [Ghash.mac_length]u8 = undefined;
mac.final(&computed_tag);
for (t, 0..) |x, i| {
computed_tag[i] ^= x;
}
const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
if (!verify) {
crypto.secureZero(u8, &computed_tag);
@memset(m, undefined);
return error.AuthenticationFailed;
}
mem.writeInt(u32, j[nonce_length..][0..4], 2, .big);
modes.ctr(@TypeOf(aes), aes, m, c, j, .big);
}
};
}
const htest = @import("test.zig");
const testing = std.testing;
test "Aes256Gcm - Empty message and no associated data" {
const key: [Aes256Gcm.key_length]u8 = [_]u8{0x69} ** Aes256Gcm.key_length;
const nonce: [Aes256Gcm.nonce_length]u8 = [_]u8{0x42} ** Aes256Gcm.nonce_length;
const ad = "";
const m = "";
var c: [m.len]u8 = undefined;
var tag: [Aes256Gcm.tag_length]u8 = undefined;
Aes256Gcm.encrypt(&c, &tag, m, ad, nonce, key);
try htest.assertEqual("6b6ff610a16fa4cd59f1fb7903154e92", &tag);
}
test "Aes256Gcm - Associated data only" {
const key: [Aes256Gcm.key_length]u8 = [_]u8{0x69} ** Aes256Gcm.key_length;
const nonce: [Aes256Gcm.nonce_length]u8 = [_]u8{0x42} ** Aes256Gcm.nonce_length;
const m = "";
const ad = "Test with associated data";
var c: [m.len]u8 = undefined;
var tag: [Aes256Gcm.tag_length]u8 = undefined;
Aes256Gcm.encrypt(&c, &tag, m, ad, nonce, key);
try htest.assertEqual("262ed164c2dfb26e080a9d108dd9dd4c", &tag);
}
test "Aes256Gcm - Message only" {
const key: [Aes256Gcm.key_length]u8 = [_]u8{0x69} ** Aes256Gcm.key_length;
const nonce: [Aes256Gcm.nonce_length]u8 = [_]u8{0x42} ** Aes256Gcm.nonce_length;
const m = "Test with message only";
const ad = "";
var c: [m.len]u8 = undefined;
var m2: [m.len]u8 = undefined;
var tag: [Aes256Gcm.tag_length]u8 = undefined;
Aes256Gcm.encrypt(&c, &tag, m, ad, nonce, key);
try Aes256Gcm.decrypt(&m2, &c, tag, ad, nonce, key);
try testing.expectEqualSlices(u8, m[0..], m2[0..]);
try htest.assertEqual("5ca1642d90009fea33d01f78cf6eefaf01d539472f7c", &c);
try htest.assertEqual("07cd7fc9103e2f9e9bf2dfaa319caff4", &tag);
}
test "Aes256Gcm - Message and associated data" {
const key: [Aes256Gcm.key_length]u8 = [_]u8{0x69} ** Aes256Gcm.key_length;
const nonce: [Aes256Gcm.nonce_length]u8 = [_]u8{0x42} ** Aes256Gcm.nonce_length;
const m = "Test with message";
const ad = "Test with associated data";
var c: [m.len]u8 = undefined;
var m2: [m.len]u8 = undefined;
var tag: [Aes256Gcm.tag_length]u8 = undefined;
Aes256Gcm.encrypt(&c, &tag, m, ad, nonce, key);
try Aes256Gcm.decrypt(&m2, &c, tag, ad, nonce, key);
try testing.expectEqualSlices(u8, m[0..], m2[0..]);
try htest.assertEqual("5ca1642d90009fea33d01f78cf6eefaf01", &c);
try htest.assertEqual("64accec679d444e2373bd9f6796c0d2c", &tag);
}
|