Skip to content

Commit 6a7733a

Browse files
cpuFiloSottile
authored andcommitted
crypto/pbkdf2: init package
This commit imports the x/crypto/pbkdf2 package as described in the linked proposal. The code is unchanged with the exception of a few small updates to reflect feedback from the proposal comment period: * the Key function is made generic over a hash.Hash * the h function is moved to be the first argument * keyLen is renamed to keyLength * an error return is added * the unit tests were moved to the pbkdf2_test package Updates #69488 Change-Id: If72f854daeb65a5c7fbe45ebd341e63a33340624 Reviewed-on: https://go-review.googlesource.com/c/go/+/628135 Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent a86ea80 commit 6a7733a

File tree

6 files changed

+267
-0
lines changed

6 files changed

+267
-0
lines changed

api/next/69488.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg crypto/pbkdf2, func Key[$0 hash.Hash](func() $0, string, []uint8, int, int) ([]uint8, error) #69488

doc/next/6-stdlib/2-pbkdf2.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A new pbkdf2 [Key] derivation function was added, based on the pre-existing
2+
`golang.org/x/crypto/pbkdf2` package. <!-- go.dev/issue/69488 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- This is a new package; covered in 6-stdlib/2-pbkdf2.md. -->

src/crypto/pbkdf2/pbkdf2.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2012 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
/*
6+
Package pbkdf2 implements the key derivation function PBKDF2 as defined in RFC
7+
2898 / PKCS #5 v2.0.
8+
9+
A key derivation function is useful when encrypting data based on a password
10+
or any other not-fully-random data. It uses a pseudorandom function to derive
11+
a secure encryption key based on the password.
12+
13+
While v2.0 of the standard defines only one pseudorandom function to use,
14+
HMAC-SHA1, the drafted v2.1 specification allows use of all five FIPS Approved
15+
Hash Functions SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512 for HMAC. To
16+
choose, you can pass the `New` functions from the different SHA packages to
17+
pbkdf2.Key.
18+
*/
19+
package pbkdf2
20+
21+
import (
22+
"crypto/hmac"
23+
"hash"
24+
)
25+
26+
// Key derives a key from the password, salt and iteration count, returning a
27+
// []byte of length keyLength that can be used as cryptographic key. The key is
28+
// derived based on the method described as PBKDF2 with the HMAC variant using
29+
// the supplied hash function.
30+
//
31+
// For example, to use a HMAC-SHA-1 based PBKDF2 key derivation function, you
32+
// can get a derived key for e.g. AES-256 (which needs a 32-byte key) by
33+
// doing:
34+
//
35+
// dk := pbkdf2.Key(sha1.New, []byte("some password"), salt, 4096, 32)
36+
//
37+
// Remember to get a good random salt. At least 8 bytes is recommended by the
38+
// RFC.
39+
//
40+
// Using a higher iteration count will increase the cost of an exhaustive
41+
// search but will also make derivation proportionally slower.
42+
func Key[Hash hash.Hash](h func() Hash, password string, salt []byte, iter, keyLength int) ([]byte, error) {
43+
prf := hmac.New(func() hash.Hash { return h() }, []byte(password))
44+
hashLen := prf.Size()
45+
numBlocks := (keyLength + hashLen - 1) / hashLen
46+
47+
var buf [4]byte
48+
dk := make([]byte, 0, numBlocks*hashLen)
49+
U := make([]byte, hashLen)
50+
for block := 1; block <= numBlocks; block++ {
51+
// N.B.: || means concatenation, ^ means XOR
52+
// for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
53+
// U_1 = PRF(password, salt || uint(i))
54+
prf.Reset()
55+
prf.Write(salt)
56+
buf[0] = byte(block >> 24)
57+
buf[1] = byte(block >> 16)
58+
buf[2] = byte(block >> 8)
59+
buf[3] = byte(block)
60+
prf.Write(buf[:4])
61+
dk = prf.Sum(dk)
62+
T := dk[len(dk)-hashLen:]
63+
copy(U, T)
64+
65+
// U_n = PRF(password, U_(n-1))
66+
for n := 2; n <= iter; n++ {
67+
prf.Reset()
68+
prf.Write(U)
69+
U = U[:0]
70+
U = prf.Sum(U)
71+
for x := range U {
72+
T[x] ^= U[x]
73+
}
74+
}
75+
}
76+
return dk[:keyLength], nil
77+
}

src/crypto/pbkdf2/pbkdf2_test.go

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Copyright 2012 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package pbkdf2_test
6+
7+
import (
8+
"bytes"
9+
"crypto/pbkdf2"
10+
"crypto/sha1"
11+
"crypto/sha256"
12+
"hash"
13+
"testing"
14+
)
15+
16+
type testVector struct {
17+
password string
18+
salt string
19+
iter int
20+
output []byte
21+
}
22+
23+
// Test vectors from RFC 6070, http://tools.ietf.org/html/rfc6070
24+
var sha1TestVectors = []testVector{
25+
{
26+
"password",
27+
"salt",
28+
1,
29+
[]byte{
30+
0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
31+
0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
32+
0x2f, 0xe0, 0x37, 0xa6,
33+
},
34+
},
35+
{
36+
"password",
37+
"salt",
38+
2,
39+
[]byte{
40+
0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
41+
0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
42+
0xd8, 0xde, 0x89, 0x57,
43+
},
44+
},
45+
{
46+
"password",
47+
"salt",
48+
4096,
49+
[]byte{
50+
0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
51+
0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
52+
0x65, 0xa4, 0x29, 0xc1,
53+
},
54+
},
55+
// // This one takes too long
56+
// {
57+
// "password",
58+
// "salt",
59+
// 16777216,
60+
// []byte{
61+
// 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
62+
// 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
63+
// 0x26, 0x34, 0xe9, 0x84,
64+
// },
65+
// },
66+
{
67+
"passwordPASSWORDpassword",
68+
"saltSALTsaltSALTsaltSALTsaltSALTsalt",
69+
4096,
70+
[]byte{
71+
0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
72+
0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
73+
0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
74+
0x38,
75+
},
76+
},
77+
{
78+
"pass\000word",
79+
"sa\000lt",
80+
4096,
81+
[]byte{
82+
0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
83+
0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3,
84+
},
85+
},
86+
}
87+
88+
// Test vectors from
89+
// http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors
90+
var sha256TestVectors = []testVector{
91+
{
92+
"password",
93+
"salt",
94+
1,
95+
[]byte{
96+
0x12, 0x0f, 0xb6, 0xcf, 0xfc, 0xf8, 0xb3, 0x2c,
97+
0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4, 0xf8, 0x37,
98+
0xa8, 0x65, 0x48, 0xc9,
99+
},
100+
},
101+
{
102+
"password",
103+
"salt",
104+
2,
105+
[]byte{
106+
0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3,
107+
0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0,
108+
0x2a, 0x30, 0x3f, 0x8e,
109+
},
110+
},
111+
{
112+
"password",
113+
"salt",
114+
4096,
115+
[]byte{
116+
0xc5, 0xe4, 0x78, 0xd5, 0x92, 0x88, 0xc8, 0x41,
117+
0xaa, 0x53, 0x0d, 0xb6, 0x84, 0x5c, 0x4c, 0x8d,
118+
0x96, 0x28, 0x93, 0xa0,
119+
},
120+
},
121+
{
122+
"passwordPASSWORDpassword",
123+
"saltSALTsaltSALTsaltSALTsaltSALTsalt",
124+
4096,
125+
[]byte{
126+
0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f,
127+
0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf,
128+
0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18,
129+
0x1c,
130+
},
131+
},
132+
{
133+
"pass\000word",
134+
"sa\000lt",
135+
4096,
136+
[]byte{
137+
0x89, 0xb6, 0x9d, 0x05, 0x16, 0xf8, 0x29, 0x89,
138+
0x3c, 0x69, 0x62, 0x26, 0x65, 0x0a, 0x86, 0x87,
139+
},
140+
},
141+
}
142+
143+
func testHash(t *testing.T, h func() hash.Hash, hashName string, vectors []testVector) {
144+
for i, v := range vectors {
145+
o, err := pbkdf2.Key(h, v.password, []byte(v.salt), v.iter, len(v.output))
146+
if err != nil {
147+
t.Error(err)
148+
}
149+
if !bytes.Equal(o, v.output) {
150+
t.Errorf("%s %d: expected %x, got %x", hashName, i, v.output, o)
151+
}
152+
}
153+
}
154+
155+
func TestWithHMACSHA1(t *testing.T) {
156+
testHash(t, sha1.New, "SHA1", sha1TestVectors)
157+
}
158+
159+
func TestWithHMACSHA256(t *testing.T) {
160+
testHash(t, sha256.New, "SHA256", sha256TestVectors)
161+
}
162+
163+
var sink uint8
164+
165+
func benchmark(b *testing.B, h func() hash.Hash) {
166+
var err error
167+
password := make([]byte, h().Size())
168+
salt := make([]byte, 8)
169+
for i := 0; i < b.N; i++ {
170+
password, err = pbkdf2.Key(h, string(password), salt, 4096, len(password))
171+
if err != nil {
172+
b.Error(err)
173+
}
174+
}
175+
sink += password[0]
176+
}
177+
178+
func BenchmarkHMACSHA1(b *testing.B) {
179+
benchmark(b, sha1.New)
180+
}
181+
182+
func BenchmarkHMACSHA256(b *testing.B) {
183+
benchmark(b, sha256.New)
184+
}

src/go/build/deps_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ var depsRules = `
516516
crypto/boring, crypto/internal/fips140/edwards25519/field
517517
< crypto/ecdh;
518518
519+
crypto/hmac < crypto/pbkdf2;
520+
519521
# Unfortunately, stuck with reflect via encoding/binary.
520522
encoding/binary, crypto/boring < golang.org/x/crypto/sha3;
521523

0 commit comments

Comments
 (0)