authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-10-05 23:51:53+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-10-06 00:00:33+02:00
log06c16f44e77decd8bdafd5f1cd149b475ddf92cd
treee57daa2d4e6062422d66fefec51ccd608d12045d
parentd343b75e7fa11d94e9668fb306b9e6b2ba68a0da

std/crypto: Add support for AES-GCM

Already pretty fast on platforms with AES-NI, even though GHASH reduction hasn't been optimized yet, and we don't do stitching either.

3 files changed, 165 insertions(+), 0 deletions(-)

lib/std/crypto.zig+2
......@@ -13,6 +13,8 @@ pub const aead = struct {
1313 pub const XChaCha20Poly1305 = chacha20.XChacha20Poly1305;
1414 pub const AEGIS128L = @import("crypto/aegis.zig").AEGIS128L;
1515 pub const AEGIS256 = @import("crypto/aegis.zig").AEGIS256;
16 pub const AES128GCM = @import("crypto/aes_gcm.zig").AES128GCM;
17 pub const AES256GCM = @import("crypto/aes_gcm.zig").AES256GCM;
1618};
1719
1820/// Authentication (MAC) functions.
lib/std/crypto/aes_gcm.zig created+161
......@@ -0,0 +1,161 @@
1const std = @import("std");
2const assert = std.debug.assert;
3const builtin = std.builtin;
4const crypto = std.crypto;
5const debug = std.debug;
6const Ghash = std.crypto.onetimeauth.Ghash;
7const mem = std.mem;
8const modes = crypto.core.modes;
9
10pub const AES128GCM = AESGCM(crypto.core.aes.AES128);
11pub const AES256GCM = AESGCM(crypto.core.aes.AES256);
12
13fn AESGCM(comptime AES: anytype) type {
14 debug.assert(AES.block.block_size == 16);
15
16 return struct {
17 pub const tag_length = 16;
18 pub const nonce_length = 12;
19 pub const key_length = AES.key_bits / 8;
20
21 const zeros = [_]u8{0} ** 16;
22
23 pub fn encrypt(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) void {
24 debug.assert(c.len == m.len);
25 debug.assert(m.len <= 16 * ((1 << 32) - 2));
26
27 const aes = AES.initEnc(key);
28 var h: [16]u8 = undefined;
29 aes.encrypt(&h, &zeros);
30
31 var t: [16]u8 = undefined;
32 var j: [16]u8 = undefined;
33 mem.copy(u8, j[0..nonce_length], npub[0..]);
34 mem.writeIntBig(u32, j[nonce_length..][0..4], 1);
35 aes.encrypt(&t, &j);
36
37 var mac = Ghash.init(&h);
38 mac.update(ad);
39 mac.pad();
40
41 mem.writeIntBig(u32, j[nonce_length..][0..4], 2);
42 modes.ctr(@TypeOf(aes), aes, c, m, j, builtin.Endian.Big);
43 mac.update(c[0..m.len][0..]);
44 mac.pad();
45
46 var final_block = h;
47 mem.writeIntBig(u64, final_block[0..8], ad.len * 8);
48 mem.writeIntBig(u64, final_block[8..16], m.len * 8);
49 mac.update(&final_block);
50 mac.final(tag);
51 for (t) |x, i| {
52 tag[i] ^= x;
53 }
54 }
55
56 pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) !void {
57 assert(c.len == m.len);
58
59 const aes = AES.initEnc(key);
60 var h: [16]u8 = undefined;
61 aes.encrypt(&h, &zeros);
62
63 var t: [16]u8 = undefined;
64 var j: [16]u8 = undefined;
65 mem.copy(u8, j[0..nonce_length], npub[0..]);
66 mem.writeIntBig(u32, j[nonce_length..][0..4], 1);
67 aes.encrypt(&t, &j);
68
69 var mac = Ghash.init(&h);
70 mac.update(ad);
71 mac.pad();
72
73 mac.update(c);
74 mac.pad();
75
76 var final_block = h;
77 mem.writeIntBig(u64, final_block[0..8], ad.len * 8);
78 mem.writeIntBig(u64, final_block[8..16], m.len * 8);
79 mac.update(&final_block);
80 var computed_tag: [Ghash.mac_length]u8 = undefined;
81 mac.final(&computed_tag);
82 for (t) |x, i| {
83 computed_tag[i] ^= x;
84 }
85
86 var acc: u8 = 0;
87 for (computed_tag) |_, p| {
88 acc |= (computed_tag[p] ^ tag[p]);
89 }
90 if (acc != 0) {
91 mem.set(u8, m, 0xaa);
92 return error.AuthenticationFailed;
93 }
94
95 mem.writeIntBig(u32, j[nonce_length..][0..4], 2);
96 modes.ctr(@TypeOf(aes), aes, m, c, j, builtin.Endian.Big);
97 }
98 };
99}
100
101const htest = @import("test.zig");
102const testing = std.testing;
103
104test "AES256GCM - Empty message and no associated data" {
105 const key: [AES256GCM.key_length]u8 = [_]u8{0x69} ** AES256GCM.key_length;
106 const nonce: [AES256GCM.nonce_length]u8 = [_]u8{0x42} ** AES256GCM.nonce_length;
107 const ad = "";
108 const m = "";
109 var c: [m.len]u8 = undefined;
110 var m2: [m.len]u8 = undefined;
111 var tag: [AES256GCM.tag_length]u8 = undefined;
112
113 AES256GCM.encrypt(&c, &tag, m, ad, nonce, key);
114 htest.assertEqual("6b6ff610a16fa4cd59f1fb7903154e92", &tag);
115}
116
117test "AES256GCM - Associated data only" {
118 const key: [AES256GCM.key_length]u8 = [_]u8{0x69} ** AES256GCM.key_length;
119 const nonce: [AES256GCM.nonce_length]u8 = [_]u8{0x42} ** AES256GCM.nonce_length;
120 const m = "";
121 const ad = "Test with associated data";
122 var c: [m.len]u8 = undefined;
123 var tag: [AES256GCM.tag_length]u8 = undefined;
124
125 AES256GCM.encrypt(&c, &tag, m, ad, nonce, key);
126 htest.assertEqual("262ed164c2dfb26e080a9d108dd9dd4c", &tag);
127}
128
129test "AES256GCM - Message only" {
130 const key: [AES256GCM.key_length]u8 = [_]u8{0x69} ** AES256GCM.key_length;
131 const nonce: [AES256GCM.nonce_length]u8 = [_]u8{0x42} ** AES256GCM.nonce_length;
132 const m = "Test with message only";
133 const ad = "";
134 var c: [m.len]u8 = undefined;
135 var m2: [m.len]u8 = undefined;
136 var tag: [AES256GCM.tag_length]u8 = undefined;
137
138 AES256GCM.encrypt(&c, &tag, m, ad, nonce, key);
139 try AES256GCM.decrypt(&m2, &c, tag, ad, nonce, key);
140 testing.expectEqualSlices(u8, m[0..], m2[0..]);
141
142 htest.assertEqual("5ca1642d90009fea33d01f78cf6eefaf01d539472f7c", &c);
143 htest.assertEqual("07cd7fc9103e2f9e9bf2dfaa319caff4", &tag);
144}
145
146test "AES256GCM - Message and associated data" {
147 const key: [AES256GCM.key_length]u8 = [_]u8{0x69} ** AES256GCM.key_length;
148 const nonce: [AES256GCM.nonce_length]u8 = [_]u8{0x42} ** AES256GCM.nonce_length;
149 const m = "Test with message";
150 const ad = "Test with associated data";
151 var c: [m.len]u8 = undefined;
152 var m2: [m.len]u8 = undefined;
153 var tag: [AES256GCM.tag_length]u8 = undefined;
154
155 AES256GCM.encrypt(&c, &tag, m, ad, nonce, key);
156 try AES256GCM.decrypt(&m2, &c, tag, ad, nonce, key);
157 testing.expectEqualSlices(u8, m[0..], m2[0..]);
158
159 htest.assertEqual("5ca1642d90009fea33d01f78cf6eefaf01", &c);
160 htest.assertEqual("64accec679d444e2373bd9f6796c0d2c", &tag);
161}
lib/std/crypto/benchmark.zig+2
......@@ -152,6 +152,8 @@ const aeads = [_]Crypto{
152152 Crypto{ .ty = crypto.aead.Gimli, .name = "gimli-aead" },
153153 Crypto{ .ty = crypto.aead.AEGIS128L, .name = "aegis-128l" },
154154 Crypto{ .ty = crypto.aead.AEGIS256, .name = "aegis-256" },
155 Crypto{ .ty = crypto.aead.AES128GCM, .name = "aes128-gcm" },
156 Crypto{ .ty = crypto.aead.AES256GCM, .name = "aes256-gcm" },
155157};
156158
157159pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 {