| ... | ... | @@ -10,6 +10,14 @@ const debug = std.debug; |
| 10 | 10 | const assert = debug.assert; |
| 11 | 11 | const mem = std.mem; |
| 12 | 12 | |
| 13 | //! PBKDF2 (Password-Based Key Derivation Function 2) is a specific Key Derivation Function, |
| 14 | //! intended to turn a weak, human generated password into a strong key, suitable for cryptographic |
| 15 | //! uses. It does this by salting and stretching the password. Salting injects non-secret random |
| 16 | //! data, so that identical passwords will be converted into unique keys. Stretching applies a |
| 17 | //! deliberately slow hashing function to frustrate brute-force guessing. |
| 18 | //! |
| 19 | //! PBKDF2 is defined in RFC 2898, and is a recommendation of NIST SP 800-132. |
| 20 | |
| 13 | 21 | // RFC 2898 Section 5.2 |
| 14 | 22 | // |
| 15 | 23 | // FromSpec: |
| ... | ... | @@ -38,19 +46,33 @@ const mem = std.mem; |
| 38 | 46 | |
| 39 | 47 | // Based on Apple's CommonKeyDerivation, based originally on code by Damien Bergamini. |
| 40 | 48 | |
| 41 | | pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: u32, comptime Hash: type) void { |
| 49 | /// Given a password, salt, iteration count (rounds), and a pseudo-random function, generates a |
| 50 | /// derived key in the provided buffer slice. |
| 51 | /// |
| 52 | /// derivedKey: Slice of appropriate size for generated key. Generally 16 or 32 bytes in length. |
| 53 | /// May be uninitialized. All bytes will be written. |
| 54 | /// Maximum size is (2^32 - 1) * Hash.digest_length |
| 55 | /// It is a programming error to pass buffer longer than the maximum size. |
| 56 | /// |
| 57 | /// password: Arbitrary sequence of bytes of any length, including empty. |
| 58 | /// |
| 59 | /// salt: Arbitrary sequence of bytes of any length, including empty. A common length is 8 bytes. |
| 60 | /// |
| 61 | /// rounds: Iteration count. Must be greater than 0. Common values range from 1,000 to 100,000. |
| 62 | /// |
| 63 | /// Prf: Pseudo-random function to use. The most common choice is std.crypto.auth.hmac.HmacSha256. |
| 64 | pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: u32, comptime Prf: type) void { |
| 42 | 65 | assert(rounds >= 1); |
| 43 | 66 | |
| 44 | | const dkLen = derivedKey.len; |
| 45 | | const hLen = Hash.digest_length; |
| 46 | | const Prf = crypto.auth.hmac.Hmac(Hash); |
| 67 | const dkLen: u64 = derivedKey.len; |
| 68 | const hLen: u32 = Prf.mac_length; // Force type to ensure multiplications can't overflow |
| 47 | 69 | |
| 48 | 70 | // FromSpec: |
| 49 | 71 | // |
| 50 | 72 | // 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and |
| 51 | 73 | // stop. |
| 52 | 74 | // |
| 53 | | assert(dkLen > 0 and dkLen <= (1 << 32 - 1) * hLen); |
| 75 | assert(dkLen > 0 and dkLen <= @as(u64, 1 << 32 - 1) * hLen); |
| 54 | 76 | |
| 55 | 77 | // FromSpec: |
| 56 | 78 | // |
| ... | ... | @@ -108,7 +130,7 @@ pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: |
| 108 | 130 | ctx.final(prevBlock[0..]); |
| 109 | 131 | |
| 110 | 132 | // Choose portion of DK to write into (T_n) and initialize |
| 111 | | const offset: usize = block * hLen; |
| 133 | const offset: u64 = @as(u64, block) * hLen; |
| 112 | 134 | const blockLen = if (block != l - 1) hLen else r; |
| 113 | 135 | var dkBlock = derivedKey[offset..(offset + blockLen)]; |
| 114 | 136 | mem.copy(u8, dkBlock, prevBlock[0..dkBlock.len]); |
| ... | ... | @@ -138,7 +160,7 @@ test "RFC 6070 one iteration" { |
| 138 | 160 | |
| 139 | 161 | var derivedKey: [dkLen]u8 = undefined; |
| 140 | 162 | |
| 141 | | pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1); |
| 163 | pbkdf2(&derivedKey, p, s, c, crypto.auth.hmac.HmacSha1); |
| 142 | 164 | |
| 143 | 165 | const expected = "0c60c80f961f0e71f3a9b524af6012062fe037a6"; |
| 144 | 166 | |
| ... | ... | @@ -153,7 +175,7 @@ test "RFC 6070 two iterations" { |
| 153 | 175 | |
| 154 | 176 | var derivedKey: [dkLen]u8 = undefined; |
| 155 | 177 | |
| 156 | | pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1); |
| 178 | pbkdf2(&derivedKey, p, s, c, crypto.auth.hmac.HmacSha1); |
| 157 | 179 | |
| 158 | 180 | const expected = "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957"; |
| 159 | 181 | |
| ... | ... | @@ -168,7 +190,7 @@ test "RFC 6070 4096 iterations" { |
| 168 | 190 | |
| 169 | 191 | var derivedKey: [dkLen]u8 = undefined; |
| 170 | 192 | |
| 171 | | pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1); |
| 193 | pbkdf2(&derivedKey, p, s, c, crypto.auth.hmac.HmacSha1); |
| 172 | 194 | |
| 173 | 195 | const expected = "4b007901b765489abead49d926f721d065a429c1"; |
| 174 | 196 | |
| ... | ... | @@ -188,7 +210,7 @@ test "RFC 6070 16,777,216 iterations" { |
| 188 | 210 | |
| 189 | 211 | var derivedKey = [_]u8{0} ** dkLen; |
| 190 | 212 | |
| 191 | | pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1); |
| 213 | pbkdf2(&derivedKey, p, s, c, crypto.auth.hmac.HmacSha1); |
| 192 | 214 | |
| 193 | 215 | const expected = "eefe3d61cd4da4e4e9945b3d6ba2158c2634e984"; |
| 194 | 216 | |
| ... | ... | @@ -203,7 +225,7 @@ test "RFC 6070 multi-block salt and password" { |
| 203 | 225 | |
| 204 | 226 | var derivedKey: [dkLen]u8 = undefined; |
| 205 | 227 | |
| 206 | | pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1); |
| 228 | pbkdf2(&derivedKey, p, s, c, crypto.auth.hmac.HmacSha1); |
| 207 | 229 | |
| 208 | 230 | const expected = "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038"; |
| 209 | 231 | |
| ... | ... | @@ -218,7 +240,7 @@ test "RFC 6070 embedded NUL" { |
| 218 | 240 | |
| 219 | 241 | var derivedKey: [dkLen]u8 = undefined; |
| 220 | 242 | |
| 221 | | pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1); |
| 243 | pbkdf2(&derivedKey, p, s, c, crypto.auth.hmac.HmacSha1); |
| 222 | 244 | |
| 223 | 245 | const expected = "56fa6aa75548099dcc37d7f03425e0c3"; |
| 224 | 246 | |
| ... | ... | @@ -226,11 +248,10 @@ test "RFC 6070 embedded NUL" { |
| 226 | 248 | } |
| 227 | 249 | |
| 228 | 250 | test "Very large dkLen" { |
| 229 | | // These iteration tests are slow so we always skip them. Results have been verified. |
| 251 | // This test allocates 8GB of memory and is expected to take several hours to run. |
| 230 | 252 | if (true) { |
| 231 | 253 | return error.SkipZigTest; |
| 232 | 254 | } |
| 233 | | |
| 234 | 255 | const p = "password"; |
| 235 | 256 | const s = "salt"; |
| 236 | 257 | const c = 1; |
| ... | ... | @@ -241,7 +262,7 @@ test "Very large dkLen" { |
| 241 | 262 | std.testing.allocator.free(derivedKey); |
| 242 | 263 | } |
| 243 | 264 | |
| 244 | | pbkdf2(derivedKey, p, s, c, crypto.hash.Sha1); |
| 265 | pbkdf2(derivedKey, p, s, c, crypto.auth.hmac.HmacSha1); |
| 245 | 266 | |
| 246 | 267 | const expected = "0c60c80f961f0e71f3a9b524af6012062fe037a6"; |
| 247 | 268 | |