jedisct1/zig-hctr2
HCTR2 length-preserving encryption algorithm for Zig
A Zig implementation of HCTR2 and HCTR3 length-preserving encryption systems.
std.crypto
Originally designed for disk encryption and filename encryption, HCTR2/HCTR3 are also suitable for:
Requires Zig 0.14.0 or later.
Add to your build.zig.zon
:
.dependencies = .{
.hctr2 = .{
.url = "https://github.com/yourusername/zig-hctr2/archive/refs/tags/v0.1.0.tar.gz",
.hash = "...", // Use `zig fetch --save` to get the correct hash
},
},
Then in your build.zig
:
const hctr2 = b.dependency("hctr2", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("hctr2", hctr2.module("hctr2"));
const std = @import("std");
const hctr2 = @import("hctr2");
// Create an HCTR2 instance with AES-256
const cipher = hctr2.Hctr2_256.init(key);
// Encrypt with an optional tweak
cipher.encrypt(&ciphertext, plaintext, tweak);
// Decrypt
cipher.decrypt(&plaintext, ciphertext, tweak);
const std = @import("std");
const hctr2 = @import("hctr2");
// HCTR3 with improved security
const cipher = hctr2.Hctr3_256.init(key);
// Use structured data as tweak
const tweak = "[email protected]|2024-01-01";
cipher.encrypt(&ciphertext, plaintext, tweak);
Hctr2_128
- HCTR2 with AES-128Hctr2_256
- HCTR2 with AES-256Hctr3_128
- HCTR3 with AES-128 and SHA-256Hctr3_256
- HCTR3 with AES-256 and SHA-256Hctr2(comptime Aes)
- Generic HCTR2 with custom AES variantHctr3(comptime Aes, comptime Hash)
- Generic HCTR3 with custom primitivesAll cipher types provide:
init(key: [key_size]u8) Self
- Initialize with encryption keyencrypt(ciphertext: []u8, plaintext: []const u8, tweak: []const u8) void
- Encrypt datadecrypt(plaintext: []u8, ciphertext: []const u8, tweak: []const u8) void
- Decrypt data# Build the library
zig build
# Run tests
zig build test
# Build and run examples
zig build-exe example_hctr3.zig
HCTR2:
HCTR3: