Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HKDF rejects maximum key size #14050

Closed
guidovranken opened this issue Dec 23, 2022 · 2 comments · Fixed by #14051
Closed

HKDF rejects maximum key size #14050

guidovranken opened this issue Dec 23, 2022 · 2 comments · Fixed by #14051
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@guidovranken
Copy link

Zig Version

0.10.0

Steps to Reproduce and Observed Behavior

const std = @import("std");
const hkdf = std.crypto.kdf.hkdf;

pub fn main() !void {
    var salt: [2]u8 = [2]u8{0xFF, 0xFF};
    var password: [2]u8 = [2]u8{0xFF, 0xFF};
    var info: [2]u8 = [2]u8{0xFF, 0xFF};
    var out: [16320]u8 = undefined;

    const prk = hkdf.HkdfSha512.extract(&salt, &password);
    hkdf.HkdfSha512.expand(&out, &info, prk);
}
thread 1503604 panic: reached unreachable code
/snap/zig/5939/lib/std/debug.zig:278:14: 0x215f90 in assert (hello)
    if (!ok) unreachable; // assertion failure
             ^
/snap/zig/5939/lib/std/crypto/hkdf.zig:25:19: 0x2138e8 in expand (hello)
            assert(out.len < Hmac.mac_length * 255); // output size is too large for the Hkdf construction
                  ^
./hello.zig:11:27: 0x2137b9 in main (hello)
    hkdf.HkdfSha512.expand(&out, &info, prk);
                          ^
/snap/zig/5939/lib/std/start.zig:606:37: 0x2132c7 in posixCallMainAndExit (hello)
            const result = root.main() catch |err| {
                                    ^
/snap/zig/5939/lib/std/start.zig:368:5: 0x212d51 in _start (hello)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
Aborted (core dumped)

Expected Behavior

No crash because HKDF permits key sizes up to and including 255 * hash size. The hash size of SHA512 is 64 bytes. So a key size of 255 * 64 = 16320 bytes should be permitted. See https://www.rfc-editor.org/rfc/rfc5869 section 2.3:

      L        length of output keying material in octets
               (<= 255*HashLen)
@guidovranken guidovranken added the bug Observed behavior contradicts documented or intended behavior label Dec 23, 2022
@guidovranken
Copy link
Author

Change:

assert(out.len < Hmac.mac_length * 255); // output size is too large for the Hkdf construction

to:

            assert(out.len <= Hmac.mac_length * 255); // output size is too large for the Hkdf construction

@jedisct1

@jedisct1
Copy link
Contributor

Good catch, thanks!

Changing the assert() would not be enough, since the counter could overflow, but #14051 should address this.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants