authorgravatar for rob@neverwood.orgRob Napier <rob@neverwood.org> 2020-09-11 17:10:27-04:00
committergravatar for rob@neverwood.orgRob Napier <rob@neverwood.org> 2020-09-11 17:10:27-04:00
logc2b02d01d5c0bb684565d5b23b34022b752e9507
tree7aac025659cd92f325e9229a07b8136ff6ec86a8
parent749417a1f3060f0695bbfe72d929f06b0be42535

Add crypto.kdf.pbkdf2


2 files changed, 233 insertions(+), 0 deletions(-)

lib/std/crypto.zig+6
...@@ -35,6 +35,11 @@ pub const onetimeauth = struct {...@@ -35,6 +35,11 @@ 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 functions
39pub const kdf = struct {
40 pub const pbkdf2 = @import("crypto/pbkdf2.zig").pbkdf2;
41};
42
38/// Core functions, that should rarely be used directly by applications.43/// Core functions, that should rarely be used directly by applications.
39pub const core = struct {44pub const core = struct {
40 pub const aes = @import("crypto/aes.zig");45 pub const aes = @import("crypto/aes.zig");
...@@ -77,6 +82,7 @@ test "crypto" {...@@ -77,6 +82,7 @@ test "crypto" {
77 _ = @import("crypto/gimli.zig");82 _ = @import("crypto/gimli.zig");
78 _ = @import("crypto/hmac.zig");83 _ = @import("crypto/hmac.zig");
79 _ = @import("crypto/md5.zig");84 _ = @import("crypto/md5.zig");
85 _ = @import("crypto/pbkdf2.zig");
80 _ = @import("crypto/poly1305.zig");86 _ = @import("crypto/poly1305.zig");
81 _ = @import("crypto/sha1.zig");87 _ = @import("crypto/sha1.zig");
82 _ = @import("crypto/sha2.zig");88 _ = @import("crypto/sha2.zig");
lib/std/crypto/pbkdf2.zig created+227
...@@ -0,0 +1,227 @@
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
7const std = @import("std");
8const crypto = std.crypto;
9const debug = std.debug;
10const assert = debug.assert;
11const mem = std.mem;
12
13// RFC 2898 Section 5.2
14//
15// FromSpec:
16//
17// PBKDF2 applies a pseudorandom function (see Appendix B.1 for an
18// example) to derive keys. The length of the derived key is essentially
19// unbounded. (However, the maximum effective search space for the
20// derived key may be limited by the structure of the underlying
21// pseudorandom function. See Appendix B.1 for further discussion.)
22// PBKDF2 is recommended for new applications.
23//
24// PBKDF2 (P, S, c, dkLen)
25//
26// Options: PRF underlying pseudorandom function (hLen
27// denotes the length in octets of the
28// pseudorandom function output)
29//
30// Input: P password, an octet string
31// S salt, an octet string
32// c iteration count, a positive integer
33// dkLen intended length in octets of the derived
34// key, a positive integer, at most
35// (2^32 - 1) * hLen
36//
37// Output: DK derived key, a dkLen-octet string
38
39// Based on Apple's CommonKeyDerivation, based originally on code by Damien Bergamini.
40
41pub fn pbkdf2(derivedKey: []u8, password: []const u8, salt: []const u8, rounds: u32, comptime Hash: type) void {
42 assert(rounds >= 1);
43
44 const dkLen = derivedKey.len;
45 const hLen = Hash.digest_length;
46 const Prf = crypto.auth.hmac.Hmac(Hash);
47
48 // FromSpec:
49 //
50 // 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
51 // stop.
52 //
53 assert(dkLen > 0 and dkLen <= (1 << 32 - 1) * hLen);
54
55 // FromSpec:
56 //
57 // 2. Let l be the number of hLen-octet blocks in the derived key,
58 // rounding up, and let r be the number of octets in the last
59 // block
60 //
61 const l = (dkLen + hLen - 1) / hLen;
62 var r = dkLen % hLen;
63 r = if (r != 0) r else hLen;
64
65 // FromSpec:
66 //
67 // 3. For each block of the derived key apply the function F defined
68 // below to the password P, the salt S, the iteration count c, and
69 // the block index to compute the block:
70 //
71 // T_1 = F (P, S, c, 1) ,
72 // T_2 = F (P, S, c, 2) ,
73 // ...
74 // T_l = F (P, S, c, l) ,
75 //
76 // where the function F is defined as the exclusive-or sum of the
77 // first c iterates of the underlying pseudorandom function PRF
78 // applied to the password P and the concatenation of the salt S
79 // and the block index i:
80 //
81 // F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
82 //
83 // where
84 //
85 // U_1 = PRF (P, S || INT (i)) ,
86 // U_2 = PRF (P, U_1) ,
87 // ...
88 // U_c = PRF (P, U_{c-1}) .
89 //
90 // Here, INT (i) is a four-octet encoding of the integer i, most
91 // significant octet first.
92 //
93 // 4. Concatenate the blocks and extract the first dkLen octets to
94 // produce a derived key DK:
95 //
96 // DK = T_1 || T_2 || ... || T_l<0..r-1>
97
98 var prevBlock: [hLen]u8 = undefined;
99 var newBlock: [hLen]u8 = undefined;
100
101 var block: u32 = 0; // Spec limits to u32
102 while (block < l) : (block += 1) {
103
104 // U_1 = PRF (P, S || INT (i))
105 const blockIndex = mem.toBytes(mem.nativeToBig(u32, block + 1)); // Block index starts at 0001
106 var ctx = Prf.init(password);
107 ctx.update(salt);
108 ctx.update(blockIndex[0..]);
109 ctx.final(prevBlock[0..]);
110
111 // Choose portion of DK to write into (T_n) and initialize
112 const offset = block * hLen;
113 const blockLen = if (block != l - 1) hLen else r;
114 var dkBlock = derivedKey[offset..(offset + blockLen)];
115 mem.copy(u8, dkBlock[0..], prevBlock[0..dkBlock.len]);
116
117 var i: u32 = 1;
118 while (i < rounds) : (i += 1) {
119 // U_c = PRF (P, U_{c-1})
120 Prf.create(newBlock[0..], prevBlock[0..], password);
121 mem.copy(u8, prevBlock[0..], newBlock[0..]);
122
123 // F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
124 for (dkBlock) |_, j| {
125 dkBlock[j] ^= newBlock[j];
126 }
127 }
128 }
129}
130
131const htest = @import("test.zig");
132
133// RFC 6070 PBKDF2 HMAC-SHA1 Test Vectors
134test "RFC 6070 one iteration" {
135 const p = "password";
136 const s = "salt";
137 const c = 1;
138 const dkLen = 20;
139
140 var derivedKey: [dkLen]u8 = undefined;
141
142 pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1);
143
144 const expected = "0c60c80f961f0e71f3a9b524af6012062fe037a6";
145
146 htest.assertEqual(expected, derivedKey[0..]);
147}
148
149test "RFC 6070 two iterations" {
150 const p = "password";
151 const s = "salt";
152 const c = 2;
153 const dkLen = 20;
154
155 var derivedKey: [dkLen]u8 = undefined;
156
157 pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1);
158
159 const expected = "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957";
160
161 htest.assertEqual(expected, derivedKey[0..]);
162}
163
164test "RFC 6070 4096 iterations" {
165 const p = "password";
166 const s = "salt";
167 const c = 4096;
168 const dkLen = 20;
169
170 var derivedKey: [dkLen]u8 = undefined;
171
172 pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1);
173
174 const expected = "4b007901b765489abead49d926f721d065a429c1";
175
176 htest.assertEqual(expected, derivedKey[0..]);
177}
178
179test "RFC 6070 16,777,216 iterations" {
180 // These iteration tests are slow so we always skip them. Results have been verified.
181 if (true) {
182 return error.SkipZigTest;
183 }
184
185 const p = "password";
186 const s = "salt";
187 const c = 16777216;
188 const dkLen = 20;
189
190 var derivedKey = [_]u8{0} ** dkLen;
191
192 pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1);
193
194 const expected = "eefe3d61cd4da4e4e9945b3d6ba2158c2634e984";
195
196 htest.assertEqual(expected, derivedKey[0..]);
197}
198
199test "RFC 6070 multi-block salt and password" {
200 const p = "passwordPASSWORDpassword";
201 const s = "saltSALTsaltSALTsaltSALTsaltSALTsalt";
202 const c = 4096;
203 const dkLen = 25;
204
205 var derivedKey: [dkLen]u8 = undefined;
206
207 pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1);
208
209 const expected = "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038";
210
211 htest.assertEqual(expected, derivedKey[0..]);
212}
213
214test "RFC 6070 embedded NUL" {
215 const p = "pass\x00word";
216 const s = "sa\x00lt";
217 const c = 4096;
218 const dkLen = 16;
219
220 var derivedKey: [dkLen]u8 = undefined;
221
222 pbkdf2(&derivedKey, p, s, c, crypto.hash.Sha1);
223
224 const expected = "56fa6aa75548099dcc37d7f03425e0c3";
225
226 htest.assertEqual(expected, derivedKey[0..]);
227}