authorgravatar for 35231115+Rocknest@users.noreply.github.comRocknest <35231115+Rocknest@users.noreply.github.com> 2020-09-13 22:09:45+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-09-13 22:09:45+03:00
loga6d947191e528c02b2f7193dde7c1e51653bc848
treecd1a6e99c0d15e49f640a230202772badf6273f2
parentdcd229be922eb316ac2dcc885e26c6a3503b4895
parent85366771ea21a0dcd93e58b35738489d773590fc
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2 from rnapier/pbkdf2

Pbkdf2

3 files changed, 26 insertions(+), 13 deletions(-)

lib/std/crypto.zig+2-5
...@@ -35,10 +35,7 @@ pub const onetimeauth = struct {...@@ -35,10 +35,7 @@ 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 functions38pub const kdf = @import("crypto/kdf.zig");
39pub const kdf = struct {
40 pub const pbkdf2 = @import("crypto/pbkdf2.zig").pbkdf2;
41};
4239
43/// Core functions, that should rarely be used directly by applications.40/// Core functions, that should rarely be used directly by applications.
44pub const core = struct {41pub const core = struct {
...@@ -82,7 +79,7 @@ test "crypto" {...@@ -82,7 +79,7 @@ test "crypto" {
82 _ = @import("crypto/gimli.zig");79 _ = @import("crypto/gimli.zig");
83 _ = @import("crypto/hmac.zig");80 _ = @import("crypto/hmac.zig");
84 _ = @import("crypto/md5.zig");81 _ = @import("crypto/md5.zig");
85 _ = @import("crypto/pbkdf2.zig");82 _ = @import("crypto/kdf.zig");
86 _ = @import("crypto/poly1305.zig");83 _ = @import("crypto/poly1305.zig");
87 _ = @import("crypto/sha1.zig");84 _ = @import("crypto/sha1.zig");
88 _ = @import("crypto/sha2.zig");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
13pub const pbkdf2 = @import("pbkdf2.zig").pbkdf2;
14
15test "kdf" {
16 _ = @import("pbkdf2.zig");
17}
lib/std/crypto/pbkdf2.zig+7-8
...@@ -10,13 +10,10 @@ const debug = std.debug;...@@ -10,13 +10,10 @@ 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 generated13// Exports
14//! password into a strong key, suitable for cryptographic uses. It does this by salting and14comptime {
15//! stretching the password. Salting injects non-secret random data, so that identical passwords15 _ = crypto.kdf.pbkdf2;
16//! will be converted into unique keys. Stretching applies a deliberately slow hashing function to16}
17//! frustrate brute-force guessing.
18//!
19//! PBKDF2 is defined in RFC 2898, and is a recommendation of NIST SP 800-132.
2017
21// RFC 2898 Section 5.218// RFC 2898 Section 5.2
22//19//
...@@ -48,6 +45,8 @@ const mem = std.mem;...@@ -48,6 +45,8 @@ const mem = std.mem;
4845
49/// Apply PBKDF2 to generate a key from a password.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/// derivedKey: Slice of appropriate size for generated key. Generally 16 or 32 bytes in length.50/// derivedKey: Slice of appropriate size for generated key. Generally 16 or 32 bytes in length.
52/// May be uninitialized. All bytes will be written.51/// May be uninitialized. All bytes will be written.
53/// Maximum size is (2^32 - 1) * Hash.digest_length52/// 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,7 +130,7 @@ pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds:
131 ctx.final(prevBlock[0..]);130 ctx.final(prevBlock[0..]);
132131
133 // Choose portion of DK to write into (T_n) and initialize132 // 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 const blockLen = if (block != l - 1) hLen else r;134 const blockLen = if (block != l - 1) hLen else r;
136 var dkBlock = derivedKey[offset..(offset + blockLen)];135 var dkBlock = derivedKey[offset..(offset + blockLen)];
137 mem.copy(u8, dkBlock, prevBlock[0..dkBlock.len]);136 mem.copy(u8, dkBlock, prevBlock[0..dkBlock.len]);