| ... | @@ -0,0 +1,161 @@ |
| 1 | const std = @import("std"); |
| 2 | const assert = std.debug.assert; |
| 3 | const builtin = std.builtin; |
| 4 | const crypto = std.crypto; |
| 5 | const debug = std.debug; |
| 6 | const Ghash = std.crypto.onetimeauth.Ghash; |
| 7 | const mem = std.mem; |
| 8 | const modes = crypto.core.modes; |
| 9 | |
| 10 | pub const AES128GCM = AESGCM(crypto.core.aes.AES128); |
| 11 | pub const AES256GCM = AESGCM(crypto.core.aes.AES256); |
| 12 | |
| 13 | fn 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 | |
| 101 | const htest = @import("test.zig"); |
| 102 | const testing = std.testing; |
| 103 | |
| 104 | test "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 | |
| 117 | test "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 | |
| 129 | test "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 | |
| 146 | test "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 | } |