| ... | @@ -10,14 +10,6 @@ const debug = std.debug; | ... | @@ -10,14 +10,6 @@ const debug = std.debug; |
| 10 | const assert = debug.assert; | 10 | const assert = debug.assert; |
| 11 | const mem = std.mem; | 11 | const mem = std.mem; |
| 12 | | 12 | |
| 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.2 | 13 | // 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; |
| 48 | | 40 | |
| 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_length | 47 | /// 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. |
| 65 | pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: u32, comptime Prf: type) void { | 61 | pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: u32, comptime Prf: type) void { |
| 66 | assert(rounds >= 1); | 62 | assert(rounds >= 1); |
| 67 | | 63 | |
| ... | @@ -161,7 +157,7 @@ test "RFC 6070 one iteration" { | ... | @@ -161,7 +157,7 @@ test "RFC 6070 one iteration" { |
| 161 | | 157 | |
| 162 | var derivedKey: [dkLen]u8 = undefined; | 158 | var derivedKey: [dkLen]u8 = undefined; |
| 163 | | 159 | |
| 164 | pbkdf2(&derivedKey, p, s, c, crypto.auth.hmac.HmacSha1); | 160 | std.crypto.kdf.pbkdf2(&derivedKey, p, s, c, crypto.auth.hmac.HmacSha1); |
| 165 | | 161 | |
| 166 | const expected = "0c60c80f961f0e71f3a9b524af6012062fe037a6"; | 162 | const expected = "0c60c80f961f0e71f3a9b524af6012062fe037a6"; |
| 167 | | 163 | |