feature. See also
. The project being documented here (as the example) is the Zig library itself.
File
Code
const std = @import("std");
const assert = std.debug.assert;
const crypto = std.crypto;
const debug = std.debug;
const mem = std.mem;
const math = std.math;
const modes = @import("modes.zig");
const Polyval = @import("ghash_polyval.zig").Polyval;
const AuthenticationError = crypto.errors.AuthenticationError;
pub const Aes128GcmSiv = AesGcmSiv(crypto.core.aes.Aes128);
pub const Aes256GcmSiv = AesGcmSiv(crypto.core.aes.Aes256);
fn AesGcmSiv(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: [16]u8 = @splat(0);
fn deriveKeys(message_key: *[key_length]u8, auth_key: *[16]u8, key: [key_length]u8, nonce: [nonce_length]u8) void {
const aes = Aes.initEnc(key);
// Each encryption produces 16 bytes, but we only use first 8 bytes of each block
if (key_length == 16) {
var key_blocks: [4 * 16]u8 = undefined;
var cipher_outs: [4 * 16]u8 = undefined;
inline for (0..4) |i| {
mem.writeInt(u32, key_blocks[i * 16 ..][0..4], @intCast(i), .little);
key_blocks[i * 16 + 4 .. i * 16 + 16].* = nonce;
}
aes.encryptWide(4, &cipher_outs, &key_blocks);
@memcpy(auth_key[0..8], cipher_outs[0..8]);
@memcpy(auth_key[8..16], cipher_outs[16..24]);
@memcpy(message_key[0..8], cipher_outs[32..40]);
@memcpy(message_key[8..16], cipher_outs[48..56]);
} else {
var key_blocks: [6 * 16]u8 = undefined;
var cipher_outs: [6 * 16]u8 = undefined;
inline for (0..6) |i| {
mem.writeInt(u32, key_blocks[i * 16 ..][0..4], @intCast(i), .little);
key_blocks[i * 16 + 4 .. i * 16 + 16].* = nonce;
}
aes.encryptWide(6, &cipher_outs, &key_blocks);
@memcpy(auth_key[0..8], cipher_outs[0..8]);
@memcpy(auth_key[8..16], cipher_outs[16..24]);
@memcpy(message_key[0..8], cipher_outs[32..40]);
@memcpy(message_key[8..16], cipher_outs[48..56]);
@memcpy(message_key[16..24], cipher_outs[64..72]);
@memcpy(message_key[24..32], cipher_outs[80..88]);
}
}
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 <= (1 << 36));
debug.assert(ad.len <= (1 << 36));
var auth_key: [16]u8 = undefined;
var message_key: [key_length]u8 = undefined;
deriveKeys(&message_key, &auth_key, key, npub);
const block_count = (math.divCeil(usize, ad.len, Polyval.block_length) catch unreachable) +
(math.divCeil(usize, m.len, Polyval.block_length) catch unreachable) + 1;
var mac = Polyval.initForBlockCount(&auth_key, block_count);
mac.update(ad);
mac.pad();
mac.update(m);
mac.pad();
var length_block: [16]u8 = undefined;
mem.writeInt(u64, length_block[0..8], @as(u64, ad.len) * 8, .little);
mem.writeInt(u64, length_block[8..16], @as(u64, m.len) * 8, .little);
mac.update(&length_block);
var s: [16]u8 = undefined;
mac.final(&s);
for (npub, 0..) |b, i| {
s[i] ^= b;
}
s[15] &= 0x7f;
const tag_aes = Aes.initEnc(message_key);
tag_aes.encrypt(tag, &s);
var counter: [16]u8 = tag.*;
counter[15] |= 0x80;
// Encrypt message using CTR mode with 32-bit little-endian counter
const aes_ctx = Aes.initEnc(message_key);
modes.ctrSlice(@TypeOf(aes_ctx), aes_ctx, c, m, counter, .little, 0, 4);
}
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);
assert(c.len <= (1 << 36));
assert(ad.len <= (1 << 36));
var auth_key: [16]u8 = undefined;
var message_key: [key_length]u8 = undefined;
deriveKeys(&message_key, &auth_key, key, npub);
var counter: [16]u8 = tag;
counter[15] |= 0x80;
const aes_ctx = Aes.initEnc(message_key);
modes.ctrSlice(@TypeOf(aes_ctx), aes_ctx, m, c, counter, .little, 0, 4);
const block_count = (math.divCeil(usize, ad.len, Polyval.block_length) catch unreachable) +
(math.divCeil(usize, m.len, Polyval.block_length) catch unreachable) + 1;
var mac = Polyval.initForBlockCount(&auth_key, block_count);
mac.update(ad);
mac.pad();
mac.update(m);
mac.pad();
var length_block: [16]u8 = undefined;
mem.writeInt(u64, length_block[0..8], @as(u64, ad.len) * 8, .little);
mem.writeInt(u64, length_block[8..16], @as(u64, m.len) * 8, .little);
mac.update(&length_block);
var s: [16]u8 = undefined;
mac.final(&s);
for (npub, 0..) |b, i| {
s[i] ^= b;
}
s[15] &= 0x7f;
const tag_aes = Aes.initEnc(message_key);
var computed_tag: [tag_length]u8 = undefined;
tag_aes.encrypt(&computed_tag, &s);
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;
}
}
};
}
const htest = @import("test.zig");
const testing = std.testing;
test "Aes128GcmSiv - RFC 8452 Test Vector 1" {
const key = [_]u8{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const nonce = [_]u8{
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
const ad = "";
const m = "";
var c: [m.len]u8 = undefined;
var tag: [Aes128GcmSiv.tag_length]u8 = undefined;
Aes128GcmSiv.encrypt(&c, &tag, m, ad, nonce, key);
try htest.assertEqual("dc20e2d83f25705bb49e439eca56de25", &tag);
}
test "Aes128GcmSiv - RFC 8452 Test Vector 2" {
const key = [_]u8{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const nonce = [_]u8{
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
const plaintext = [_]u8{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const ad = "";
var c: [plaintext.len]u8 = undefined;
var tag: [Aes128GcmSiv.tag_length]u8 = undefined;
Aes128GcmSiv.encrypt(&c, &tag, &plaintext, ad, nonce, key);
try htest.assertEqual("b5d839330ac7b786", &c);
try htest.assertEqual("578782fff6013b815b287c22493a364c", &tag);
var m2: [plaintext.len]u8 = undefined;
try Aes128GcmSiv.decrypt(&m2, &c, tag, ad, nonce, key);
try testing.expectEqualSlices(u8, &plaintext, &m2);
}
test "Aes128GcmSiv - RFC 8452 Test Vector 3" {
const key = [_]u8{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const nonce = [_]u8{
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
const plaintext = [_]u8{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
const ad = "";
var c: [plaintext.len]u8 = undefined;
var tag: [Aes128GcmSiv.tag_length]u8 = undefined;
Aes128GcmSiv.encrypt(&c, &tag, &plaintext, ad, nonce, key);
try htest.assertEqual("7323ea61d05932260047d942", &c);
try htest.assertEqual("a4978db357391a0bc4fdec8b0d106639", &tag);
var m2: [plaintext.len]u8 = undefined;
try Aes128GcmSiv.decrypt(&m2, &c, tag, ad, nonce, key);
try testing.expectEqualSlices(u8, &plaintext, &m2);
}
test "Aes256GcmSiv - RFC 8452 Test Vector" {
const key = [_]u8{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const nonce = [_]u8{
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
const ad = "";
const m = "";
var c: [m.len]u8 = undefined;
var tag: [Aes256GcmSiv.tag_length]u8 = undefined;
Aes256GcmSiv.encrypt(&c, &tag, m, ad, nonce, key);
try htest.assertEqual("07f5f4169bbf55a8400cd47ea6fd400f", &tag);
}
test "Aes128GcmSiv - Decrypt with wrong tag" {
const key: [Aes128GcmSiv.key_length]u8 = @splat(0x69);
const nonce: [Aes128GcmSiv.nonce_length]u8 = @splat(0x42);
const m = "Test message";
const ad = "";
var c: [m.len]u8 = undefined;
var tag: [Aes128GcmSiv.tag_length]u8 = undefined;
Aes128GcmSiv.encrypt(&c, &tag, m, ad, nonce, key);
tag[0] ^= 0x01;
var m2: [m.len]u8 = undefined;
try testing.expectError(error.AuthenticationFailed, Aes128GcmSiv.decrypt(&m2, &c, tag, ad, nonce, key));
}