ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

Home Page:https://ziglang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Argon2 outputs uninitialized memory with keysize > 64

guidovranken opened this issue · comments

Zig Version

zig-linux-x86_64-0.11.0-dev.1969+d525ecb52

Steps to Reproduce and Observed Behavior

const std = @import("std");
const argon2 = std.crypto.pwhash.argon2;

pub fn main() !void {
    var pw: [32]u8 = [32]u8{
        0x01, 0x09, 0xb4, 0xd9, 0xce, 0xb9, 0x0b, 0xbf, 0xd8, 0xae, 0x53, 0xfc, 0x48, 0xcb, 0x37, 0xf0, 
        0x47, 0xf3, 0x5c, 0x08, 0xfb, 0x0f, 0x65, 0xa7, 0x84, 0x0b, 0xf9, 0x32, 0x0f, 0xc1, 0xbf, 0x0e};
    var salt: [32]u8 = [32]u8{
        0xbc, 0x22, 0x5f, 0xa8, 0xb1, 0x0d, 0x77, 0x5d, 0x1c, 0x66, 0xb9, 0x99, 0x1b, 0x1e, 0xc0, 0xc4, 
        0xd8, 0x0e, 0x3a, 0x3b, 0x75, 0x29, 0xc9, 0x6b, 0x1d, 0x4b, 0x12, 0xb7, 0xf5, 0x59, 0xbf, 0x89};
    const mode = argon2.Mode.argon2id;
    const params = argon2.Params{ .t = 2, .m = 20801, .p = 1 };
    const allocator = std.heap.page_allocator;
    var out: [65]u8 = undefined;
    argon2.kdf(
            allocator,
            out[0..65],
            pw[0..32],
            salt[0..32],
            params,
            mode) catch {
        return;
    };
    const stdout = std.io.getStdOut().writer();
    for (out) |elem| {
        std.fmt.format(stdout, "0x{x}, ", .{elem}) catch { };
    }
    std.fmt.format(stdout, "\n", .{}) catch { };
}

Prints:

0x82, 0xa1, 0x10, 0xa4, 0x28, 0xc8, 0xbb, 0x83, 0xa9, 0xdd, 0xac, 0x94, 0x25, 0x5b, 0x8a, 0x9a, 0x54, 0xcc, 0xb2, 0xa, 0xfb, 0x95, 0x8a, 0x6f, 0xda, 0x38, 0x97, 0x9b, 0x83, 0x89, 0x30, 0xa5, 0x4, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,

Expected Behavior

Should print:

0x82, 0xa1, 0x10, 0xa4, 0x28, 0xc8, 0xbb, 0x83, 0xa9, 0xdd, 0xac, 0x94, 0x25, 0x5b, 0x8a, 0x9a, 0x54, 0xcc, 0xb2, 0x0a, 0xfb, 0x95, 0x8a, 0x6f, 0xda, 0x38, 0x97, 0x9b, 0x83, 0x89, 0x30, 0xa5, 0xdf, 0xec, 0xde, 0xa8, 0x23, 0xb4, 0x19, 0x16, 0x80, 0x98, 0x3f, 0x8f, 0xca, 0x9d, 0x2f, 0xc2, 0x8f, 0x4d, 0xa8, 0x6d, 0x8e, 0x73, 0xec, 0xe5, 0x7c, 0xac, 0x31, 0xc3, 0xf4, 0x08, 0x26, 0xc4, 0x4a