| ... | @@ -36,6 +36,14 @@ const maxInt = std.math.maxInt; | ... | @@ -36,6 +36,14 @@ const maxInt = std.math.maxInt; |
| 36 | | 36 | |
| 37 | // Based on Apple's CommonKeyDerivation, based originally on code by Damien Bergamini. | 37 | // Based on Apple's CommonKeyDerivation, based originally on code by Damien Bergamini. |
| 38 | | 38 | |
| | 39 | pub const Pbkdf2Error = error{ |
| | 40 | /// At least one round is required |
| | 41 | TooFewRounds, |
| | 42 | |
| | 43 | /// Maximum length of the derived key is `maxInt(u32) * Prf.mac_length` |
| | 44 | DerivedKeyTooLong, |
| | 45 | }; |
| | 46 | |
| 39 | /// Apply PBKDF2 to generate a key from a password. | 47 | /// Apply PBKDF2 to generate a key from a password. |
| 40 | /// | 48 | /// |
| 41 | /// PBKDF2 is defined in RFC 2898, and is a recommendation of NIST SP 800-132. | 49 | /// PBKDF2 is defined in RFC 2898, and is a recommendation of NIST SP 800-132. |
| ... | @@ -54,7 +62,7 @@ const maxInt = std.math.maxInt; | ... | @@ -54,7 +62,7 @@ const maxInt = std.math.maxInt; |
| 54 | /// the derivedKey. It is common to tune this parameter to achieve approximately 100ms. | 62 | /// the derivedKey. It is common to tune this parameter to achieve approximately 100ms. |
| 55 | /// | 63 | /// |
| 56 | /// Prf: Pseudo-random function to use. A common choice is `std.crypto.auth.hmac.HmacSha256`. | 64 | /// Prf: Pseudo-random function to use. A common choice is `std.crypto.auth.hmac.HmacSha256`. |
| 57 | pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: u32, comptime Prf: type) !void { | 65 | pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: u32, comptime Prf: type) Pbkdf2Error!void { |
| 58 | if (rounds < 1) return error.TooFewRounds; | 66 | if (rounds < 1) return error.TooFewRounds; |
| 59 | | 67 | |
| 60 | const dkLen = derivedKey.len; | 68 | const dkLen = derivedKey.len; |