Compute the IV for a truncated version of SHA512 per FIPS 180 Section 5.3.6
fn truncatedSha512Iv(digest_len: comptime_int) Iv64
fn truncatedSha512Iv(digest_len: comptime_int) Iv64 {
const assert = std.debug.assert;
comptime assert(digest_len > 1);
comptime assert(digest_len <= 512);
comptime assert(digest_len != 384); // NIST specially defines this (see `iv384`)
comptime var gen_params = iv512;
inline for (&gen_params) |*iv| {
iv.* ^= 0xa5a5a5a5a5a5a5a5;
}
const GenHash = Sha2x64(gen_params, 512);
var params: [@sizeOf(Iv64)]u8 = undefined;
const algo_str = std.fmt.comptimePrint("SHA-512/{d}", .{digest_len});
GenHash.hash(algo_str, ¶ms, .{});
return Iv64{
std.mem.readInt(u64, params[0..8], .big),
std.mem.readInt(u64, params[8..16], .big),
std.mem.readInt(u64, params[16..24], .big),
std.mem.readInt(u64, params[24..32], .big),
std.mem.readInt(u64, params[32..40], .big),
std.mem.readInt(u64, params[40..48], .big),
std.mem.readInt(u64, params[48..56], .big),
std.mem.readInt(u64, params[56..64], .big),
};
}