authorgravatar for rob@neverwood.orgRob Napier <rob@neverwood.org> 2020-09-13 10:50:46-04:00
committergravatar for rob@neverwood.orgRob Napier <rob@neverwood.org> 2020-09-13 10:50:46-04:00
log257c5b534839d75092909f604bb2663e883a290f
treef566bbd1f9e67552f6d29964a67e48d01fce6f03
parent0f85b85acb0bd322ff7408c25dc09a84ff6621dc

Explicitly reference std.crypto.kdf in test case


2 files changed, 10 insertions(+), 10 deletions(-)

lib/std/crypto.zig+5-1
...@@ -35,7 +35,11 @@ pub const onetimeauth = struct {...@@ -35,7 +35,11 @@ pub const onetimeauth = struct {
35 pub const Poly1305 = @import("crypto/poly1305.zig").Poly1305;35 pub const Poly1305 = @import("crypto/poly1305.zig").Poly1305;
36};36};
3737
38/// Key derivation functions38/// A Key Derivation Function (KDF) is intended to turn a weak, human generated password into a
39/// strong key, suitable for cryptographic uses. It does this by salting and stretching the
40/// password. Salting injects non-secret random data, so that identical passwords will be converted
41/// into unique keys. Stretching applies a deliberately slow hashing function to frustrate
42/// brute-force guessing.
39pub const kdf = struct {43pub const kdf = struct {
40 pub const pbkdf2 = @import("crypto/pbkdf2.zig").pbkdf2;44 pub const pbkdf2 = @import("crypto/pbkdf2.zig").pbkdf2;
41};45};
lib/std/crypto/pbkdf2.zig+5-9
...@@ -10,14 +10,6 @@ const debug = std.debug;...@@ -10,14 +10,6 @@ const debug = std.debug;
10const assert = debug.assert;10const assert = debug.assert;
11const mem = std.mem;11const mem = std.mem;
1212
13//! PBKDF2 (Password-Based Key Derivation Function 2) is intended to turn a weak, human generated
14//! password into a strong key, suitable for cryptographic uses. It does this by salting and
15//! stretching the password. Salting injects non-secret random data, so that identical passwords
16//! will be converted into unique keys. Stretching applies a deliberately slow hashing function to
17//! frustrate brute-force guessing.
18//!
19//! PBKDF2 is defined in RFC 2898, and is a recommendation of NIST SP 800-132.
20
21// RFC 2898 Section 5.213// RFC 2898 Section 5.2
22//14//
23// FromSpec:15// FromSpec:
...@@ -48,6 +40,8 @@ const mem = std.mem;...@@ -48,6 +40,8 @@ const mem = std.mem;
4840
49/// Apply PBKDF2 to generate a key from a password.41/// Apply PBKDF2 to generate a key from a password.
50///42///
43/// PBKDF2 is defined in RFC 2898, and is a recommendation of NIST SP 800-132.
44///
51/// derivedKey: Slice of appropriate size for generated key. Generally 16 or 32 bytes in length.45/// derivedKey: Slice of appropriate size for generated key. Generally 16 or 32 bytes in length.
52/// May be uninitialized. All bytes will be written.46/// May be uninitialized. All bytes will be written.
53/// Maximum size is (2^32 - 1) * Hash.digest_length47/// Maximum size is (2^32 - 1) * Hash.digest_length
...@@ -62,6 +56,8 @@ const mem = std.mem;...@@ -62,6 +56,8 @@ const mem = std.mem;
62/// the derivedKey. It is common to tune this parameter to achieve approximately 100ms.56/// the derivedKey. It is common to tune this parameter to achieve approximately 100ms.
63///57///
64/// Prf: Pseudo-random function to use. A common choice is std.crypto.auth.hmac.HmacSha256.58/// Prf: Pseudo-random function to use. A common choice is std.crypto.auth.hmac.HmacSha256.
59///
60/// PBKDF2 is defined in RFC 2898, and is a recommendation of NIST SP 800-132.
65pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: u32, comptime Prf: type) void {61pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: u32, comptime Prf: type) void {
66 assert(rounds >= 1);62 assert(rounds >= 1);
6763
...@@ -161,7 +157,7 @@ test "RFC 6070 one iteration" {...@@ -161,7 +157,7 @@ test "RFC 6070 one iteration" {
161157
162 var derivedKey: [dkLen]u8 = undefined;158 var derivedKey: [dkLen]u8 = undefined;
163159
164 pbkdf2(&derivedKey, p, s, c, crypto.auth.hmac.HmacSha1);160 std.crypto.kdf.pbkdf2(&derivedKey, p, s, c, crypto.auth.hmac.HmacSha1);
165161
166 const expected = "0c60c80f961f0e71f3a9b524af6012062fe037a6";162 const expected = "0c60c80f961f0e71f3a9b524af6012062fe037a6";
167163