| author | |
| committer | |
| log | a6d947191e528c02b2f7193dde7c1e51653bc848 |
| tree | cd1a6e99c0d15e49f640a230202772badf6273f2 |
| parent | dcd229be922eb316ac2dcc885e26c6a3503b4895 |
| parent | 85366771ea21a0dcd93e58b35738489d773590fc |
| signature |
Pbkdf23 files changed, 26 insertions(+), 13 deletions(-)
lib/std/crypto.zig+2-5| ... | ... | @@ -35,10 +35,7 @@ pub const onetimeauth = struct { |
| 35 | 35 | pub const Poly1305 = @import("crypto/poly1305.zig").Poly1305; |
| 36 | 36 | }; |
| 37 | 37 | |
| 38 | /// Key derivation functions | |
| 39 | pub const kdf = struct { | |
| 40 | pub const pbkdf2 = @import("crypto/pbkdf2.zig").pbkdf2; | |
| 41 | }; | |
| 38 | pub const kdf = @import("crypto/kdf.zig"); | |
| 42 | 39 | |
| 43 | 40 | /// Core functions, that should rarely be used directly by applications. |
| 44 | 41 | pub const core = struct { |
| ... | ... | @@ -82,7 +79,7 @@ test "crypto" { |
| 82 | 79 | _ = @import("crypto/gimli.zig"); |
| 83 | 80 | _ = @import("crypto/hmac.zig"); |
| 84 | 81 | _ = @import("crypto/md5.zig"); |
| 85 | _ = @import("crypto/pbkdf2.zig"); | |
| 82 | _ = @import("crypto/kdf.zig"); | |
| 86 | 83 | _ = @import("crypto/poly1305.zig"); |
| 87 | 84 | _ = @import("crypto/sha1.zig"); |
| 88 | 85 | _ = @import("crypto/sha2.zig"); |
lib/std/crypto/kdf.zig created+17| ... | ... | @@ -0,0 +1,17 @@ |
| 1 | // SPDX-License-Identifier: MIT | |
| 2 | // Copyright (c) 2015-2020 Zig Contributors | |
| 3 | // This file is part of [zig](https://ziglang.org/), which is MIT licensed. | |
| 4 | // The MIT license requires this copyright notice to be included in all copies | |
| 5 | // and substantial portions of the software. | |
| 6 | ||
| 7 | //! A Key Derivation Function (KDF) is intended to turn a weak, human generated password into a | |
| 8 | //! strong key, suitable for cryptographic uses. It does this by salting and stretching the | |
| 9 | //! password. Salting injects non-secret random data, so that identical passwords will be converted | |
| 10 | //! into unique keys. Stretching applies a deliberately slow hashing function to frustrate | |
| 11 | //! brute-force guessing. | |
| 12 | ||
| 13 | pub const pbkdf2 = @import("pbkdf2.zig").pbkdf2; | |
| 14 | ||
| 15 | test "kdf" { | |
| 16 | _ = @import("pbkdf2.zig"); | |
| 17 | } |
lib/std/crypto/pbkdf2.zig+7-8| ... | ... | @@ -10,13 +10,10 @@ 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 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. | |
| 13 | // Exports | |
| 14 | comptime { | |
| 15 | _ = crypto.kdf.pbkdf2; | |
| 16 | } | |
| 20 | 17 | |
| 21 | 18 | // RFC 2898 Section 5.2 |
| 22 | 19 | // |
| ... | ... | @@ -48,6 +45,8 @@ const mem = std.mem; |
| 48 | 45 | |
| 49 | 46 | /// Apply PBKDF2 to generate a key from a password. |
| 50 | 47 | /// |
| 48 | /// PBKDF2 is defined in RFC 2898, and is a recommendation of NIST SP 800-132. | |
| 49 | /// | |
| 51 | 50 | /// derivedKey: Slice of appropriate size for generated key. Generally 16 or 32 bytes in length. |
| 52 | 51 | /// May be uninitialized. All bytes will be written. |
| 53 | 52 | /// Maximum size is (2^32 - 1) * Hash.digest_length |
| ... | ... | @@ -131,7 +130,7 @@ pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: |
| 131 | 130 | ctx.final(prevBlock[0..]); |
| 132 | 131 | |
| 133 | 132 | // Choose portion of DK to write into (T_n) and initialize |
| 134 | const offset: u64 = @as(u64, block) * hLen; | |
| 133 | const offset: usize = @as(usize, block) * hLen; | |
| 135 | 134 | const blockLen = if (block != l - 1) hLen else r; |
| 136 | 135 | var dkBlock = derivedKey[offset..(offset + blockLen)]; |
| 137 | 136 | mem.copy(u8, dkBlock, prevBlock[0..dkBlock.len]); |