| 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/internal/boring" |
| 10 |
"crypto/internal/fips140" |
| 11 |
"crypto/pbkdf2" |
| 12 |
"crypto/sha1" |
| 13 |
"crypto/sha256" |
| 14 |
"hash" |
| 15 |
"testing" |
| 16 |
) |
| 17 |
|
| 18 |
type testVector struct { |
| 19 |
password string |
| 20 |
salt string |
| 21 |
iter int |
| 22 |
output []byte |
| 23 |
} |
| 24 |
|
| 25 |
// Test vectors from RFC 6070, http://tools.ietf.org/html/rfc6070 |
| 26 |
var sha1TestVectors = []testVector{ |
| 27 |
{ |
| 28 |
"password", |
| 29 |
"salt", |
| 30 |
1, |
| 31 |
[]byte{ |
| 32 |
0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, |
| 33 |
0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, |
| 34 |
0x2f, 0xe0, 0x37, 0xa6, |
| 35 |
}, |
| 36 |
}, |
| 37 |
{ |
| 38 |
"password", |
| 39 |
"salt", |
| 40 |
2, |
| 41 |
[]byte{ |
| 42 |
0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, |
| 43 |
0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, |
| 44 |
0xd8, 0xde, 0x89, 0x57, |
| 45 |
}, |
| 46 |
}, |
| 47 |
{ |
| 48 |
"password", |
| 49 |
"salt", |
| 50 |
4096, |
| 51 |
[]byte{ |
| 52 |
0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, |
| 53 |
0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, |
| 54 |
0x65, 0xa4, 0x29, 0xc1, |
| 55 |
}, |
| 56 |
}, |
| 57 |
// // This one takes too long |
| 58 |
// { |
| 59 |
// "password", |
| 60 |
// "salt", |
| 61 |
// 16777216, |
| 62 |
// []byte{ |
| 63 |
// 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4, |
| 64 |
// 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c, |
| 65 |
// 0x26, 0x34, 0xe9, 0x84, |
| 66 |
// }, |
| 67 |
// }, |
| 68 |
{ |
| 69 |
"passwordPASSWORDpassword", |
| 70 |
"saltSALTsaltSALTsaltSALTsaltSALTsalt", |
| 71 |
4096, |
| 72 |
[]byte{ |
| 73 |
0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b, |
| 74 |
0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a, |
| 75 |
0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70, |
| 76 |
0x38, |
| 77 |
}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
"pass\000word", |
| 81 |
"sa\000lt", |
| 82 |
4096, |
| 83 |
[]byte{ |
| 84 |
0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, |
| 85 |
0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3, |
| 86 |
}, |
| 87 |
}, |
| 88 |
} |
| 89 |
|
| 90 |
// Test vectors from |
| 91 |
// http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors |
| 92 |
var sha256TestVectors = []testVector{ |
| 93 |
{ |
| 94 |
"password", |
| 95 |
"salt", |
| 96 |
1, |
| 97 |
[]byte{ |
| 98 |
0x12, 0x0f, 0xb6, 0xcf, 0xfc, 0xf8, 0xb3, 0x2c, |
| 99 |
0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4, 0xf8, 0x37, |
| 100 |
0xa8, 0x65, 0x48, 0xc9, |
| 101 |
}, |
| 102 |
}, |
| 103 |
{ |
| 104 |
"password", |
| 105 |
"salt", |
| 106 |
2, |
| 107 |
[]byte{ |
| 108 |
0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3, |
| 109 |
0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0, |
| 110 |
0x2a, 0x30, 0x3f, 0x8e, |
| 111 |
}, |
| 112 |
}, |
| 113 |
{ |
| 114 |
"password", |
| 115 |
"salt", |
| 116 |
4096, |
| 117 |
[]byte{ |
| 118 |
0xc5, 0xe4, 0x78, 0xd5, 0x92, 0x88, 0xc8, 0x41, |
| 119 |
0xaa, 0x53, 0x0d, 0xb6, 0x84, 0x5c, 0x4c, 0x8d, |
| 120 |
0x96, 0x28, 0x93, 0xa0, |
| 121 |
}, |
| 122 |
}, |
| 123 |
{ |
| 124 |
"passwordPASSWORDpassword", |
| 125 |
"saltSALTsaltSALTsaltSALTsaltSALTsalt", |
| 126 |
4096, |
| 127 |
[]byte{ |
| 128 |
0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f, |
| 129 |
0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf, |
| 130 |
0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18, |
| 131 |
0x1c, |
| 132 |
}, |
| 133 |
}, |
| 134 |
{ |
| 135 |
"pass\000word", |
| 136 |
"sa\000lt", |
| 137 |
4096, |
| 138 |
[]byte{ |
| 139 |
0x89, 0xb6, 0x9d, 0x05, 0x16, 0xf8, 0x29, 0x89, |
| 140 |
0x3c, 0x69, 0x62, 0x26, 0x65, 0x0a, 0x86, 0x87, |
| 141 |
}, |
| 142 |
}, |
| 143 |
} |
| 144 |
|
| 145 |
func testHash(t *testing.T, h func() hash.Hash, hashName string, vectors []testVector) { |
| 146 |
for i, v := range vectors { |
| 147 |
o, err := pbkdf2.Key(h, v.password, []byte(v.salt), v.iter, len(v.output)) |
| 148 |
if err != nil { |
| 149 |
t.Error(err) |
| 150 |
} |
| 151 |
if !bytes.Equal(o, v.output) { |
| 152 |
t.Errorf("%s %d: expected %x, got %x", hashName, i, v.output, o) |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func TestWithHMACSHA1(t *testing.T) { |
| 158 |
testHash(t, sha1.New, "SHA1", sha1TestVectors) |
| 159 |
} |
| 160 |
|
| 161 |
func TestWithHMACSHA256(t *testing.T) { |
| 162 |
testHash(t, sha256.New, "SHA256", sha256TestVectors) |
| 163 |
} |
| 164 |
|
| 165 |
var sink uint8 |
| 166 |
|
| 167 |
func benchmark(b *testing.B, h func() hash.Hash) { |
| 168 |
var err error |
| 169 |
password := make([]byte, h().Size()) |
| 170 |
salt := make([]byte, 8) |
| 171 |
for i := 0; i < b.N; i++ { |
| 172 |
password, err = pbkdf2.Key(h, string(password), salt, 4096, len(password)) |
| 173 |
if err != nil { |
| 174 |
b.Error(err) |
| 175 |
} |
| 176 |
} |
| 177 |
sink += password[0] |
| 178 |
} |
| 179 |
|
| 180 |
func BenchmarkHMACSHA1(b *testing.B) { |
| 181 |
benchmark(b, sha1.New) |
| 182 |
} |
| 183 |
|
| 184 |
func BenchmarkHMACSHA256(b *testing.B) { |
| 185 |
benchmark(b, sha256.New) |
| 186 |
} |
| 187 |
|
| 188 |
func TestPBKDF2ServiceIndicator(t *testing.T) { |
| 189 |
if boring.Enabled { |
| 190 |
t.Skip("in BoringCrypto mode PBKDF2 is not from the Go FIPS module") |
| 191 |
} |
| 192 |
|
| 193 |
goodSalt := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10} |
| 194 |
|
| 195 |
fips140.ResetServiceIndicator() |
| 196 |
_, err := pbkdf2.Key(sha256.New, "password", goodSalt, 1, 32) |
| 197 |
if err != nil { |
| 198 |
t.Error(err) |
| 199 |
} |
| 200 |
if !fips140.ServiceIndicator() { |
| 201 |
t.Error("FIPS service indicator should be set") |
| 202 |
} |
| 203 |
|
| 204 |
// Salt too short |
| 205 |
fips140.ResetServiceIndicator() |
| 206 |
_, err = pbkdf2.Key(sha256.New, "password", goodSalt[:8], 1, 32) |
| 207 |
if err != nil { |
| 208 |
t.Error(err) |
| 209 |
} |
| 210 |
if fips140.ServiceIndicator() { |
| 211 |
t.Error("FIPS service indicator should not be set") |
| 212 |
} |
| 213 |
|
| 214 |
// Key length too short |
| 215 |
fips140.ResetServiceIndicator() |
| 216 |
_, err = pbkdf2.Key(sha256.New, "password", goodSalt, 1, 10) |
| 217 |
if err != nil { |
| 218 |
t.Error(err) |
| 219 |
} |
| 220 |
if fips140.ServiceIndicator() { |
| 221 |
t.Error("FIPS service indicator should not be set") |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
func TestMaxKeyLength(t *testing.T) { |
| 226 |
// This error cannot be triggered on platforms where int is 31 bits (i.e. |
| 227 |
// 32-bit platforms), since the max value for keyLength is 1<<31-1 and |
| 228 |
// 1<<31-1 * hLen will always be less than 1<<32-1 * hLen. |
| 229 |
keySize := int64(1<<63 - 1) |
| 230 |
if int64(int(keySize)) != keySize { |
| 231 |
t.Skip("cannot be replicated on platforms where int is 31 bits") |
| 232 |
} |
| 233 |
_, err := pbkdf2.Key(sha256.New, "password", []byte("salt"), 1, int(keySize)) |
| 234 |
if err == nil { |
| 235 |
t.Fatal("expected pbkdf2.Key to fail with extremely large keyLength") |
| 236 |
} |
| 237 |
keySize = int64(1<<32-1) * (sha256.Size + 1) |
| 238 |
_, err = pbkdf2.Key(sha256.New, "password", []byte("salt"), 1, int(keySize)) |
| 239 |
if err == nil { |
| 240 |
t.Fatal("expected pbkdf2.Key to fail with extremely large keyLength") |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func TestZeroKeyLength(t *testing.T) { |
| 245 |
_, err := pbkdf2.Key(sha256.New, "password", []byte("salt"), 1, 0) |
| 246 |
if err == nil { |
| 247 |
t.Fatal("expected pbkdf2.Key to fail with zero keyLength") |
| 248 |
} |
| 249 |
_, err = pbkdf2.Key(sha256.New, "password", []byte("salt"), 1, -1) |
| 250 |
if err == nil { |
| 251 |
t.Fatal("expected pbkdf2.Key to fail with negative keyLength") |
| 252 |
} |
| 253 |
} |