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 = crypto.core.modes;
const Cmac = @import("cmac.zig").Cmac;
const AuthenticationError = crypto.errors.AuthenticationError;
pub const Aes128Siv = AesSiv(crypto.core.aes.Aes128);
pub const Aes256Siv = AesSiv(crypto.core.aes.Aes256);
fn AesSiv(comptime Aes: anytype) type {
debug.assert(Aes.block.block_length == 16);
return struct {
pub const tag_length = 16;
pub const key_length = Aes.key_bits / 8 * 2;
const CmacImpl = Cmac(Aes);
fn s2v(iv: *[16]u8, key: [Aes.key_bits / 8]u8, strings: []const []const u8) void {
assert(strings.len > 0);
assert(strings.len <= 127);
var d: [16]u8 = undefined;
if (strings.len == 1 and strings[0].len == 0) {
CmacImpl.create(&d, &[_]u8{}, &key);
iv.* = d;
return;
}
const zero_block: [16]u8 = @splat(0);
CmacImpl.create(&d, &zero_block, &key);
var i: usize = 0;
while (i < strings.len - 1) : (i += 1) {
d = dbl(d);
var tmp: [16]u8 = undefined;
CmacImpl.create(&tmp, strings[i], &key);
for (&d, tmp) |*b, t| {
b.* ^= t;
}
}
const sn = strings[strings.len - 1];
if (sn.len >= 16) {
// and give the entire Sn to CMAC incrementally.
var cmac = CmacImpl.init(&key);
const prefix = sn.len - 16;
cmac.update(sn[0..prefix]);
var tail: [16]u8 = undefined;
for (&tail, sn[prefix..][0..16], d) |*out, s, db| {
out.* = s ^ db;
}
cmac.update(&tail);
cmac.final(iv);
} else {
d = dbl(d);
var padded: [16]u8 = @splat(0);
@memcpy(padded[0..sn.len], sn);
padded[sn.len] = 0x80;
for (&d, padded) |*b, p| {
b.* ^= p;
}
CmacImpl.create(iv, &d, &key);
}
}
fn dbl(d: [16]u8) [16]u8 {
const val = mem.readInt(u128, &d, .big);
const doubled = (val << 1) ^ (0x87 & -%(@as(u128, val >> 127)));
var result: [16]u8 = undefined;
mem.writeInt(u128, &result, doubled, .big);
return result;
}
pub fn encrypt(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: ?[]const u8, nonce: ?[]const u8, key: [key_length]u8) void {
debug.assert(c.len == m.len);
const k1 = key[0 .. Aes.key_bits / 8];
const k2 = key[Aes.key_bits / 8 ..];
var strings_buf: [128][]const u8 = undefined;
var strings_len: usize = 0;
if (ad) |a| {
strings_buf[strings_len] = a;
strings_len += 1;
}
if (nonce) |n| {
strings_buf[strings_len] = n;
strings_len += 1;
}
strings_buf[strings_len] = m;
strings_len += 1;
s2v(tag, k1.*, strings_buf[0..strings_len]);
var ctr_iv = tag.*;
ctr_iv[8] &= 0x7f;
ctr_iv[12] &= 0x7f;
const aes_ctx = Aes.initEnc(k2.*);
modes.ctr(@TypeOf(aes_ctx), aes_ctx, c, m, ctr_iv, .big);
}
pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: ?[]const u8, nonce: ?[]const u8, key: [key_length]u8) AuthenticationError!void {
assert(c.len == m.len);
const k1 = key[0 .. Aes.key_bits / 8];
const k2 = key[Aes.key_bits / 8 ..];
var ctr_iv = tag;
ctr_iv[8] &= 0x7f;
ctr_iv[12] &= 0x7f;
const aes_ctx = Aes.initEnc(k2.*);
modes.ctr(@TypeOf(aes_ctx), aes_ctx, m, c, ctr_iv, .big);
var strings_buf: [128][]const u8 = undefined;
var strings_len: usize = 0;
if (ad) |a| {
strings_buf[strings_len] = a;
strings_len += 1;
}
if (nonce) |n| {
strings_buf[strings_len] = n;
strings_len += 1;
}
strings_buf[strings_len] = m;
strings_len += 1;
var computed_tag: [tag_length]u8 = undefined;
s2v(&computed_tag, k1.*, strings_buf[0..strings_len]);
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;
}
}
pub fn encryptWithAdVector(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: []const []const u8, key: [key_length]u8) void {
debug.assert(c.len == m.len);
debug.assert(ad.len <= 126);
// Split key into K1 (for S2V) and K2 (for CTR)
const k1 = key[0 .. Aes.key_bits / 8];
const k2 = key[Aes.key_bits / 8 ..];
var strings_buf: [128][]const u8 = undefined;
var strings_len: usize = 0;
for (ad) |a| {
strings_buf[strings_len] = a;
strings_len += 1;
}
strings_buf[strings_len] = m;
strings_len += 1;
s2v(tag, k1.*, strings_buf[0..strings_len]);
var ctr_iv = tag.*;
ctr_iv[8] &= 0x7f;
ctr_iv[12] &= 0x7f;
const aes_ctx = Aes.initEnc(k2.*);
modes.ctr(@TypeOf(aes_ctx), aes_ctx, c, m, ctr_iv, .big);
}
pub fn decryptWithAdVector(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const []const u8, key: [key_length]u8) AuthenticationError!void {
assert(c.len == m.len);
assert(ad.len <= 126);
// Split key into K1 (for S2V) and K2 (for CTR)
const k1 = key[0 .. Aes.key_bits / 8];
const k2 = key[Aes.key_bits / 8 ..];
var ctr_iv = tag;
ctr_iv[8] &= 0x7f;
ctr_iv[12] &= 0x7f;
const aes_ctx = Aes.initEnc(k2.*);
modes.ctr(@TypeOf(aes_ctx), aes_ctx, m, c, ctr_iv, .big);
var strings_buf: [128][]const u8 = undefined;
var strings_len: usize = 0;
for (ad) |a| {
strings_buf[strings_len] = a;
strings_len += 1;
}
strings_buf[strings_len] = m;
strings_len += 1;
var computed_tag: [tag_length]u8 = undefined;
s2v(&computed_tag, k1.*, strings_buf[0..strings_len]);
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 "AES-SIV double operation" {
const AesSivTest = AesSiv(crypto.core.aes.Aes128);
const input = [_]u8{ 0x0e, 0x04, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e };
const expected = [_]u8{ 0x1c, 0x08, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c };
const result = AesSivTest.dbl(input);
try testing.expectEqualSlices(u8, &expected, &result);
}
test "AES-SIV double operation with MSB set" {
const AesSivTest = AesSiv(crypto.core.aes.Aes128);
const input = [_]u8{ 0xe0, 0x40, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0 };
const expected = [_]u8{ 0xc0, 0x80, 0x20, 0x40, 0x60, 0x80, 0xa0, 0xc0, 0xe1, 0x01, 0x21, 0x41, 0x61, 0x81, 0xa1, 0x47 };
const result = AesSivTest.dbl(input);
try testing.expectEqualSlices(u8, &expected, &result);
}
test "Aes128Siv - RFC 5297 Test Vector A.1" {
const key = [_]u8{
0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
};
const ad = [_]u8{
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
};
const plaintext = [_]u8{
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
};
var ciphertext: [plaintext.len]u8 = undefined;
var tag: [16]u8 = undefined;
const ad_components = [_][]const u8{&ad};
Aes128Siv.encryptWithAdVector(&ciphertext, &tag, &plaintext, &ad_components, key);
try htest.assertEqual("85632d07c6e8f37f950acd320a2ecc93", &tag);
try htest.assertEqual("40c02b9690c4dc04daef7f6afe5c", &ciphertext);
var decrypted: [plaintext.len]u8 = undefined;
try Aes128Siv.decryptWithAdVector(&decrypted, &ciphertext, tag, &ad_components, key);
try testing.expectEqualSlices(u8, &plaintext, &decrypted);
}
test "Aes128Siv - RFC 5297 Test Vector A.2" {
const key: [32]u8 = .{
0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78,
0x77, 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x70,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
};
const ad1 = [_]u8{
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0xde, 0xad, 0xda, 0xda, 0xde, 0xad, 0xda, 0xda,
0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00,
};
const ad2 = [_]u8{
0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
0x90, 0xa0,
};
const nonce: [16]u8 = .{
0x09, 0xf9, 0x11, 0x02, 0x9d, 0x74, 0xe3, 0x5b,
0xd8, 0x41, 0x56, 0xc5, 0x63, 0x56, 0x88, 0xc0,
};
const plaintext = [_]u8{
0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
0x73, 0x6f, 0x6d, 0x65, 0x20, 0x70, 0x6c, 0x61,
0x69, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x20, 0x74,
0x6f, 0x20, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70,
0x74, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20,
0x53, 0x49, 0x56, 0x2d, 0x41, 0x45, 0x53,
};
var ciphertext: [plaintext.len]u8 = undefined;
var tag: [16]u8 = undefined;
Aes128Siv.encryptWithAdVector(&ciphertext, &tag, &plaintext, &.{ &ad1, &ad2, &nonce }, key);
try htest.assertEqual("7bdb6e3b432667eb06f4d14bff2fbd0f", &tag);
try htest.assertEqual("cb900f2fddbe404326601965c889bf17dba77ceb094fa663b7a3f748ba8af829ea64ad544a272e9c485b62a3fd5c0d", &ciphertext);
}
test "Aes128Siv - empty plaintext" {
const key: [32]u8 = @splat(0x42);
const plaintext = "";
const ad = "additional data";
var ciphertext: [plaintext.len]u8 = undefined;
var tag: [16]u8 = undefined;
Aes128Siv.encrypt(&ciphertext, &tag, plaintext, ad, null, key);
var decrypted: [plaintext.len]u8 = undefined;
try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, ad, null, key);
}
test "Aes128Siv - with nonce" {
const key: [32]u8 = @splat(0x69);
const nonce: [16]u8 = @splat(0x42);
const plaintext = "Hello, AES-SIV!";
const ad = "metadata";
var ciphertext: [plaintext.len]u8 = undefined;
var tag: [16]u8 = undefined;
Aes128Siv.encrypt(&ciphertext, &tag, plaintext, ad, &nonce, key);
var decrypted: [plaintext.len]u8 = undefined;
try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, ad, &nonce, key);
try testing.expectEqualSlices(u8, plaintext, &decrypted);
}
test "Aes256Siv - basic functionality" {
const key: [64]u8 = @splat(0x96);
const plaintext = "Test message for AES-256-SIV";
const ad1 = "header";
const ad2 = "more data";
var ciphertext: [plaintext.len]u8 = undefined;
var tag: [16]u8 = undefined;
const ad_components = [_][]const u8{ ad1, ad2 };
Aes256Siv.encryptWithAdVector(&ciphertext, &tag, plaintext, &ad_components, key);
var decrypted: [plaintext.len]u8 = undefined;
try Aes256Siv.decryptWithAdVector(&decrypted, &ciphertext, tag, &ad_components, key);
try testing.expectEqualSlices(u8, plaintext, &decrypted);
}
test "Aes128Siv - demonstrating optional parameters" {
const key: [32]u8 = @splat(0x77);
{
const plaintext = "Deterministic encryption";
var ciphertext: [plaintext.len]u8 = undefined;
var tag: [16]u8 = undefined;
Aes128Siv.encrypt(&ciphertext, &tag, plaintext, null, null, key);
var decrypted: [plaintext.len]u8 = undefined;
try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, null, null, key);
try testing.expectEqualSlices(u8, plaintext, &decrypted);
}
{
const plaintext = "With associated data";
const ad = "some context";
var ciphertext: [plaintext.len]u8 = undefined;
var tag: [16]u8 = undefined;
Aes128Siv.encrypt(&ciphertext, &tag, plaintext, ad, null, key);
var decrypted: [plaintext.len]u8 = undefined;
try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, ad, null, key);
try testing.expectEqualSlices(u8, plaintext, &decrypted);
}
{
const plaintext = "Nonce-based encryption";
const nonce: [12]u8 = @splat(0x01);
var ciphertext: [plaintext.len]u8 = undefined;
var tag: [16]u8 = undefined;
Aes128Siv.encrypt(&ciphertext, &tag, plaintext, null, &nonce, key);
var decrypted: [plaintext.len]u8 = undefined;
try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, null, &nonce, key);
try testing.expectEqualSlices(u8, plaintext, &decrypted);
}
{
const plaintext = "Full featured";
const ad = "context";
const nonce: [16]u8 = @splat(0x02);
var ciphertext: [plaintext.len]u8 = undefined;
var tag: [16]u8 = undefined;
Aes128Siv.encrypt(&ciphertext, &tag, plaintext, ad, &nonce, key);
var decrypted: [plaintext.len]u8 = undefined;
try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, ad, &nonce, key);
try testing.expectEqualSlices(u8, plaintext, &decrypted);
}
}
test "Aes128Siv - authentication failure" {
const key: [32]u8 = @splat(0x13);
const plaintext = "Secret message";
const ad = "";
var ciphertext: [plaintext.len]u8 = undefined;
var tag: [16]u8 = undefined;
Aes128Siv.encrypt(&ciphertext, &tag, plaintext, ad, null, key);
tag[0] ^= 0x01;
var decrypted: [plaintext.len]u8 = undefined;
try testing.expectError(error.AuthenticationFailed, Aes128Siv.decrypt(&decrypted, &ciphertext, tag, ad, null, key));
}