| 1 |
/* |
| 2 |
* crypto_test.go - tests for the crypto package |
| 3 |
* |
| 4 |
* Copyright 2017 Google Inc. |
| 5 |
* Author: Joe Richey ([email protected]) |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 |
* use this file except in compliance with the License. You may obtain a copy of |
| 9 |
* the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 |
* License for the specific language governing permissions and limitations under |
| 17 |
* the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package crypto |
| 21 |
|
| 22 |
import ( |
| 23 |
"bytes" |
| 24 |
"compress/zlib" |
| 25 |
"crypto/aes" |
| 26 |
"crypto/sha256" |
| 27 |
"encoding/hex" |
| 28 |
"fmt" |
| 29 |
"io" |
| 30 |
"os" |
| 31 |
"testing" |
| 32 |
|
| 33 |
"github.com/google/fscrypt/metadata" |
| 34 |
) |
| 35 |
|
| 36 |
// Reader that always returns the same byte |
| 37 |
type ConstReader byte |
| 38 |
|
| 39 |
func (r ConstReader) Read(b []byte) (n int, err error) { |
| 40 |
for i := range b { |
| 41 |
b[i] = byte(r) |
| 42 |
} |
| 43 |
return len(b), nil |
| 44 |
} |
| 45 |
|
| 46 |
// Makes a key of the same repeating byte |
| 47 |
func makeKey(b byte, n int) (*Key, error) { |
| 48 |
return NewFixedLengthKeyFromReader(ConstReader(b), n) |
| 49 |
} |
| 50 |
|
| 51 |
var ( |
| 52 |
fakeSalt = bytes.Repeat([]byte{'a'}, metadata.SaltLen) |
| 53 |
fakePassword = []byte("password") |
| 54 |
|
| 55 |
fakeValidPolicyKey, _ = makeKey(42, metadata.PolicyKeyLen) |
| 56 |
fakeWrappingKey, _ = makeKey(17, metadata.InternalKeyLen) |
| 57 |
) |
| 58 |
|
| 59 |
// As the passphrase hashing function clears the passphrase, we need to make |
| 60 |
// a new passphrase key for each test |
| 61 |
func fakePassphraseKey() (*Key, error) { |
| 62 |
return NewFixedLengthKeyFromReader(bytes.NewReader(fakePassword), len(fakePassword)) |
| 63 |
} |
| 64 |
|
| 65 |
// Values for test cases pulled from argon2 command line tool. |
| 66 |
// To generate run: |
| 67 |
// |
| 68 |
// echo "password" | argon2 "aaaaaaaaaaaaaaaa" -id -t <t> -m <m> -p <p> -l 32 |
| 69 |
// |
| 70 |
// where costs.Time = <t>, costs.Memory = 2^<m>, and costs.Parallelism = <p>. |
| 71 |
type hashTestCase struct { |
| 72 |
costs *metadata.HashingCosts |
| 73 |
hexHash string |
| 74 |
} |
| 75 |
|
| 76 |
var hashTestCases = []hashTestCase{ |
| 77 |
{ |
| 78 |
costs: &metadata.HashingCosts{Time: 1, Memory: 1 << 10, Parallelism: 1}, |
| 79 |
hexHash: "a66f5398e33761bf161fdf1273e99b148f07d88d12d85b7673fddd723f95ec34", |
| 80 |
}, |
| 81 |
// Make sure we maintain our backwards compatible behavior, where |
| 82 |
// Parallelism is truncated to 8-bits unless TruncationFixed is true. |
| 83 |
{ |
| 84 |
costs: &metadata.HashingCosts{Time: 1, Memory: 1 << 10, Parallelism: 257}, |
| 85 |
hexHash: "a66f5398e33761bf161fdf1273e99b148f07d88d12d85b7673fddd723f95ec34", |
| 86 |
}, |
| 87 |
{ |
| 88 |
costs: &metadata.HashingCosts{Time: 10, Memory: 1 << 10, Parallelism: 1}, |
| 89 |
hexHash: "5fa2cb89db1f7413ba1776258b7c8ee8c377d122078d28fe1fd645c353787f50", |
| 90 |
}, |
| 91 |
{ |
| 92 |
costs: &metadata.HashingCosts{Time: 1, Memory: 1 << 15, Parallelism: 1}, |
| 93 |
hexHash: "f474a213ed14d16ead619568000939b938ddfbd2ac4a82d253afa81b5ebaef84", |
| 94 |
}, |
| 95 |
{ |
| 96 |
costs: &metadata.HashingCosts{Time: 1, Memory: 1 << 10, Parallelism: 10}, |
| 97 |
hexHash: "b7c3d7a0be222680b5ea3af3fb1a0b7b02b92cbd7007821dc8b84800c86c7783", |
| 98 |
}, |
| 99 |
{ |
| 100 |
costs: &metadata.HashingCosts{Time: 1, Memory: 1 << 11, Parallelism: 255}, |
| 101 |
hexHash: "d51af3775bbdd0cba31d96fd6d921d9de27f521ceffe667618cd7624f6643071", |
| 102 |
}, |
| 103 |
// Adding TruncationFixed shouldn't matter if Parallelism < 256. |
| 104 |
{ |
| 105 |
costs: &metadata.HashingCosts{Time: 1, Memory: 1 << 11, Parallelism: 255, TruncationFixed: true}, |
| 106 |
hexHash: "d51af3775bbdd0cba31d96fd6d921d9de27f521ceffe667618cd7624f6643071", |
| 107 |
}, |
| 108 |
} |
| 109 |
|
| 110 |
// Checks that len(array) == expected |
| 111 |
func lengthCheck(name string, array []byte, expected int) error { |
| 112 |
if len(array) != expected { |
| 113 |
return fmt.Errorf("length of %s should be %d", name, expected) |
| 114 |
} |
| 115 |
return nil |
| 116 |
} |
| 117 |
|
| 118 |
// Tests the two ways of making keys |
| 119 |
func TestMakeKeys(t *testing.T) { |
| 120 |
data := []byte("1234\n6789") |
| •121 |
|
| 122 |
key1, err := NewKeyFromReader(bytes.NewReader(data)) |
| 123 |
switch err { |
| 124 |
case nil: |
| 125 |
defer key1.Wipe() |
| 126 |
case ErrMlockUlimit: |
| 127 |
// Don't fail just because "ulimit -l" is too low. |
| 128 |
t.Skip(err) |
| 129 |
default: |
| 130 |
t.Fatal(err) |
| 131 |
} |
| 132 |
if !bytes.Equal(data, key1.data) { |
| 133 |
t.Error("Key from reader contained incorrect data") |
| 134 |
} |
| 135 |
|
| 136 |
key2, err := NewFixedLengthKeyFromReader(bytes.NewReader(data), 6) |
| 137 |
if err != nil { |
| 138 |
t.Fatal(err) |
| 139 |
} |
| 140 |
defer key2.Wipe() |
| 141 |
if !bytes.Equal([]byte("1234\n6"), key2.data) { |
| 142 |
t.Error("Fixed length key from reader contained incorrect data") |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// Tests that wipe succeeds |
| •147 |
func TestWipe(t *testing.T) { |
| 148 |
key, err := makeKey(1, 1000) |
| 149 |
if err == ErrMlockUlimit { |
| 150 |
// Don't fail just because "ulimit -l" is too low. |
| 151 |
t.Skip(err) |
| 152 |
} |
| 153 |
if err != nil { |
| 154 |
t.Fatal(err) |
| 155 |
} |
| 156 |
if err := key.Wipe(); err != nil { |
| 157 |
t.Error(err) |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// Making keys with negative length should fail |
| 162 |
func TestInvalidLength(t *testing.T) { |
| 163 |
key, err := NewFixedLengthKeyFromReader(ConstReader(1), -1) |
| 164 |
if err == nil { |
| 165 |
key.Wipe() |
| 166 |
t.Error("Negative lengths should cause failure") |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
// Test making keys of zero length |
| 171 |
func TestZeroLength(t *testing.T) { |
| 172 |
key1, err := NewFixedLengthKeyFromReader(os.Stdin, 0) |
| 173 |
if err != nil { |
| 174 |
t.Fatal(err) |
| 175 |
} |
| 176 |
defer key1.Wipe() |
| 177 |
if key1.data != nil { |
| 178 |
t.Error("Fixed length key from reader contained data") |
| 179 |
} |
| 180 |
|
| •181 |
key2, err := NewKeyFromReader(bytes.NewReader(nil)) |
| 182 |
if err != nil { |
| 183 |
if err == ErrMlockUlimit { |
| 184 |
// Don't fail just because "ulimit -l" is too low. |
| 185 |
t.Skip(err) |
| 186 |
} |
| 187 |
t.Fatal(err) |
| 188 |
} |
| 189 |
defer key2.Wipe() |
| 190 |
if key2.data != nil { |
| 191 |
t.Error("Key from empty reader contained data") |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// Test that enabling then disabling memory locking succeeds even if a key is |
| 196 |
// active when the variable changes. |
| 197 |
func TestEnableDisableMemoryLocking(t *testing.T) { |
| 198 |
// Mlock on for creation, off for wiping |
| 199 |
key, err := NewRandomKey(metadata.InternalKeyLen) |
| 200 |
UseMlock = false |
| 201 |
defer func() { |
| 202 |
UseMlock = true |
| 203 |
}() |
| •204 |
|
| 205 |
if err != nil { |
| 206 |
if err == ErrMlockUlimit { |
| 207 |
// Don't fail just because "ulimit -l" is too low. |
| 208 |
t.Skip(err) |
| 209 |
} |
| 210 |
t.Fatal(err) |
| 211 |
} |
| 212 |
if err := key.Wipe(); err != nil { |
| 213 |
t.Error(err) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
// Test that disabling then enabling memory locking succeeds even if a key is |
| 218 |
// active when the variable changes. |
| 219 |
func TestDisableEnableMemoryLocking(t *testing.T) { |
| 220 |
// Mlock off for creation, on for wiping |
| 221 |
UseMlock = false |
| 222 |
key2, err := NewRandomKey(metadata.InternalKeyLen) |
| 223 |
UseMlock = true |
| 224 |
|
| 225 |
if err != nil { |
| 226 |
t.Fatal(err) |
| 227 |
} |
| 228 |
if err := key2.Wipe(); err != nil { |
| 229 |
t.Error(err) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
// Test making keys long enough that the keys will have to resize |
| 234 |
func TestKeyResize(t *testing.T) { |
| 235 |
// Key will have to resize once |
| 236 |
r := io.LimitReader(ConstReader(1), int64(os.Getpagesize())+1) |
| •237 |
key, err := NewKeyFromReader(r) |
| 238 |
if err != nil { |
| 239 |
if err == ErrMlockUlimit { |
| 240 |
// Don't fail just because "ulimit -l" is too low. |
| 241 |
t.Skip(err) |
| 242 |
} |
| 243 |
t.Fatal(err) |
| 244 |
} |
| 245 |
defer key.Wipe() |
| 246 |
for i, b := range key.data { |
| 247 |
if b != 1 { |
| 248 |
t.Fatalf("Byte %d contained invalid data %q", i, b) |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
// Test making keys so long that many resizes are necessary |
| 254 |
func TestKeyLargeResize(t *testing.T) { |
| 255 |
// Key will have to resize 7 times |
| 256 |
r := io.LimitReader(ConstReader(1), int64(os.Getpagesize())*65) |
| 257 |
|
| 258 |
// Turn off Mlocking as the key will exceed the limit on some systems. |
| 259 |
UseMlock = false |
| 260 |
key, err := NewKeyFromReader(r) |
| 261 |
UseMlock = true |
| 262 |
|
| 263 |
if err != nil { |
| 264 |
t.Fatal(err) |
| 265 |
} |
| 266 |
defer key.Wipe() |
| 267 |
for i, b := range key.data { |
| 268 |
if b != 1 { |
| 269 |
t.Fatalf("Byte %d contained invalid data %q", i, b) |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
// Check that we can create random keys. All this test does to test the |
| 275 |
// "randomness" is generate a page of random bytes and attempts compression. |
| 276 |
// If the data can be compressed it is probably not very random. This isn't |
| 277 |
// intended to be a sufficient test for randomness (which is impossible), but a |
| 278 |
// way to catch simple regressions (key is all zeros or contains a repeating |
| 279 |
// pattern). |
| 280 |
func TestRandomKeyGen(t *testing.T) { |
| •281 |
key, err := NewRandomKey(os.Getpagesize()) |
| 282 |
if err != nil { |
| 283 |
if err == ErrMlockUlimit { |
| 284 |
// Don't fail just because "ulimit -l" is too low. |
| 285 |
t.Skip(err) |
| 286 |
} |
| 287 |
t.Fatal(err) |
| 288 |
} |
| 289 |
defer key.Wipe() |
| 290 |
|
| 291 |
if didCompress(key.data) { |
| 292 |
t.Errorf("Random key (%d bytes) should not be compressible", key.Len()) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
func TestBigKeyGen(t *testing.T) { |
| 297 |
key, err := NewRandomKey(4096 * 4096) |
| 298 |
switch err { |
| 299 |
case nil: |
| 300 |
key.Wipe() |
| 301 |
return |
| 302 |
case ErrMlockUlimit: |
| 303 |
// Don't fail just because "ulimit -l" is too low. |
| 304 |
return |
| 305 |
default: |
| 306 |
t.Fatal(err) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
// didCompress checks if the given data can be compressed. Specifically, it |
| 311 |
// returns true if running zlib on the provided input produces a shorter output. |
| 312 |
func didCompress(input []byte) bool { |
| 313 |
var output bytes.Buffer |
| 314 |
|
| 315 |
w := zlib.NewWriter(&output) |
| 316 |
_, err := w.Write(input) |
| 317 |
w.Close() |
| 318 |
|
| 319 |
return err == nil && len(input) > output.Len() |
| 320 |
} |
| 321 |
|
| 322 |
// Checks that the input arrays are all distinct |
| 323 |
func buffersDistinct(buffers ...[]byte) bool { |
| 324 |
for i := 0; i < len(buffers); i++ { |
| 325 |
for j := i + 1; j < len(buffers); j++ { |
| 326 |
if bytes.Equal(buffers[i], buffers[j]) { |
| 327 |
// Different entry, but equal arrays |
| 328 |
return false |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
return true |
| 333 |
} |
| 334 |
|
| 335 |
// Checks that our cryptographic operations all produce distinct data |
| 336 |
func TestKeysAndOutputsDistinct(t *testing.T) { |
| 337 |
data, err := Wrap(fakeWrappingKey, fakeValidPolicyKey) |
| 338 |
if err != nil { |
| 339 |
t.Fatal(err) |
| 340 |
} |
| 341 |
|
| 342 |
encKey, authKey := stretchKey(fakeWrappingKey) |
| 343 |
defer encKey.Wipe() |
| 344 |
defer authKey.Wipe() |
| 345 |
|
| 346 |
if !buffersDistinct(fakeWrappingKey.data, fakeValidPolicyKey.data, |
| 347 |
encKey.data, authKey.data, data.IV, data.EncryptedKey, data.Hmac) { |
| 348 |
t.Error("Key wrapping produced duplicate data") |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
// Check that Wrap() works with fixed keys |
| 353 |
func TestWrapSucceeds(t *testing.T) { |
| 354 |
data, err := Wrap(fakeWrappingKey, fakeValidPolicyKey) |
| 355 |
if err != nil { |
| 356 |
t.Fatal(err) |
| 357 |
} |
| 358 |
|
| 359 |
if err = lengthCheck("IV", data.IV, aes.BlockSize); err != nil { |
| 360 |
t.Error(err) |
| 361 |
} |
| 362 |
if err = lengthCheck("Encrypted Key", data.EncryptedKey, metadata.PolicyKeyLen); err != nil { |
| 363 |
t.Error(err) |
| 364 |
} |
| 365 |
if err = lengthCheck("HMAC", data.Hmac, sha256.Size); err != nil { |
| 366 |
t.Error(err) |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
// Checks that applying Wrap then Unwrap gives the original data |
| 371 |
func testWrapUnwrapEqual(wrappingKey *Key, secretKey *Key) error { |
| 372 |
data, err := Wrap(wrappingKey, secretKey) |
| 373 |
if err != nil { |
| 374 |
return err |
| 375 |
} |
| 376 |
|
| 377 |
secret, err := Unwrap(wrappingKey, data) |
| 378 |
if err != nil { |
| 379 |
return err |
| 380 |
} |
| 381 |
defer secret.Wipe() |
| 382 |
|
| 383 |
if !bytes.Equal(secretKey.data, secret.data) { |
| 384 |
return fmt.Errorf("Got %x after wrap/unwrap with w=%x and s=%x", |
| 385 |
secret.data, wrappingKey.data, secretKey.data) |
| 386 |
} |
| 387 |
return nil |
| 388 |
} |
| 389 |
|
| 390 |
// Check that Unwrap(Wrap(x)) == x with fixed keys |
| 391 |
func TestWrapUnwrapEqual(t *testing.T) { |
| 392 |
if err := testWrapUnwrapEqual(fakeWrappingKey, fakeValidPolicyKey); err != nil { |
| 393 |
t.Error(err) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
// Check that Unwrap(Wrap(x)) == x with random keys |
| 398 |
func TestRandomWrapUnwrapEqual(t *testing.T) { |
| 399 |
for i := 0; i < 10; i++ { |
| 400 |
wk, err := NewRandomKey(metadata.InternalKeyLen) |
| 401 |
if err != nil { |
| 402 |
t.Fatal(err) |
| 403 |
} |
| 404 |
sk, err := NewRandomKey(metadata.InternalKeyLen) |
| 405 |
if err != nil { |
| 406 |
t.Fatal(err) |
| 407 |
} |
| 408 |
if err = testWrapUnwrapEqual(wk, sk); err != nil { |
| 409 |
t.Error(err) |
| 410 |
} |
| 411 |
wk.Wipe() |
| 412 |
sk.Wipe() |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
// Check that Unwrap(Wrap(x)) == x with differing lengths of secret key |
| 417 |
func TestDifferentLengthSecretKey(t *testing.T) { |
| 418 |
wk, err := makeKey(1, metadata.InternalKeyLen) |
| 419 |
if err != nil { |
| 420 |
t.Fatal(err) |
| 421 |
} |
| 422 |
defer wk.Wipe() |
| 423 |
for i := 0; i < 100; i++ { |
| 424 |
sk, err := makeKey(2, i) |
| 425 |
if err != nil { |
| 426 |
t.Fatal(err) |
| 427 |
} |
| 428 |
if err = testWrapUnwrapEqual(wk, sk); err != nil { |
| 429 |
t.Error(err) |
| 430 |
} |
| 431 |
sk.Wipe() |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
// Wrong length of wrapping key should fail |
| 436 |
func TestWrongWrappingKeyLength(t *testing.T) { |
| 437 |
_, err := Wrap(fakeValidPolicyKey, fakeWrappingKey) |
| 438 |
if err == nil { |
| 439 |
t.Fatal("using a policy key for wrapping should fail") |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
// Wrong length of unwrapping key should fail |
| 444 |
func TestWrongUnwrappingKeyLength(t *testing.T) { |
| 445 |
data, err := Wrap(fakeWrappingKey, fakeWrappingKey) |
| 446 |
if err != nil { |
| 447 |
t.Fatal(err) |
| 448 |
} |
| 449 |
if k, err := Unwrap(fakeValidPolicyKey, data); err == nil { |
| 450 |
k.Wipe() |
| 451 |
t.Fatal("using a policy key for unwrapping should fail") |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
// Wrapping twice with the same keys should give different components |
| 456 |
func TestWrapTwiceDistinct(t *testing.T) { |
| 457 |
data1, err := Wrap(fakeWrappingKey, fakeValidPolicyKey) |
| 458 |
if err != nil { |
| 459 |
t.Fatal(err) |
| 460 |
} |
| 461 |
data2, err := Wrap(fakeWrappingKey, fakeValidPolicyKey) |
| 462 |
if err != nil { |
| 463 |
t.Fatal(err) |
| 464 |
} |
| 465 |
if !buffersDistinct(data1.IV, data1.EncryptedKey, data1.Hmac, |
| 466 |
data2.IV, data2.EncryptedKey, data2.Hmac) { |
| 467 |
t.Error("Wrapping same keys twice should give distinct results") |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
// Attempts to Unwrap data with key after altering tweak, should fail |
| 472 |
func testFailWithTweak(key *Key, data *metadata.WrappedKeyData, tweak []byte) error { |
| 473 |
tweak[0]++ |
| 474 |
key, err := Unwrap(key, data) |
| 475 |
if err == nil { |
| 476 |
key.Wipe() |
| 477 |
} |
| 478 |
tweak[0]-- |
| 479 |
return err |
| 480 |
} |
| 481 |
|
| 482 |
// Wrapping then unwrapping with different components altered |
| 483 |
func TestUnwrapWrongKey(t *testing.T) { |
| 484 |
data, err := Wrap(fakeWrappingKey, fakeValidPolicyKey) |
| 485 |
if err != nil { |
| 486 |
t.Fatal(err) |
| 487 |
} |
| 488 |
if testFailWithTweak(fakeWrappingKey, data, fakeWrappingKey.data) == nil { |
| 489 |
t.Error("using a different wrapping key should make unwrap fail") |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
func TestUnwrapWrongData(t *testing.T) { |
| 494 |
data, err := Wrap(fakeWrappingKey, fakeValidPolicyKey) |
| 495 |
if err != nil { |
| 496 |
t.Fatal(err) |
| 497 |
} |
| 498 |
if testFailWithTweak(fakeWrappingKey, data, data.EncryptedKey) == nil { |
| 499 |
t.Error("changing encryption key should make unwrap fail") |
| 500 |
} |
| 501 |
if testFailWithTweak(fakeWrappingKey, data, data.IV) == nil { |
| 502 |
t.Error("changing IV should make unwrap fail") |
| 503 |
} |
| 504 |
if testFailWithTweak(fakeWrappingKey, data, data.Hmac) == nil { |
| 505 |
t.Error("changing HMAC should make unwrap fail") |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
func TestComputeKeyDescriptorV1(t *testing.T) { |
| 510 |
descriptor, err := ComputeKeyDescriptor(fakeValidPolicyKey, 1) |
| 511 |
if err != nil { |
| 512 |
t.Fatal(err) |
| 513 |
} |
| 514 |
if descriptor != "8290608a029c5aae" { |
| 515 |
t.Errorf("wrong v1 descriptor: %s", descriptor) |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
func TestComputeKeyDescriptorV2(t *testing.T) { |
| 520 |
descriptor, err := ComputeKeyDescriptor(fakeValidPolicyKey, 2) |
| 521 |
if err != nil { |
| 522 |
t.Fatal(err) |
| 523 |
} |
| 524 |
if descriptor != "2139f52bf8386ee99845818ac7e91c4a" { |
| 525 |
t.Errorf("wrong v2 descriptor: %s", descriptor) |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
func TestComputeKeyDescriptorBadVersion(t *testing.T) { |
| 530 |
_, err := ComputeKeyDescriptor(fakeValidPolicyKey, 0) |
| 531 |
if err == nil { |
| 532 |
t.Error("computing key descriptor with bad version should fail") |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
// Run our test cases for passphrase hashing |
| 537 |
func TestPassphraseHashing(t *testing.T) { |
| 538 |
pk, err := fakePassphraseKey() |
| 539 |
if err != nil { |
| 540 |
t.Fatal(err) |
| 541 |
} |
| 542 |
defer pk.Wipe() |
| 543 |
|
| 544 |
for i, testCase := range hashTestCases { |
| 545 |
if err := testCase.costs.CheckValidity(); err != nil { |
| 546 |
t.Errorf("Hash test %d: for costs=%+v hashing failed: %v", i, testCase.costs, err) |
| 547 |
continue |
| 548 |
} |
| 549 |
hash, err := PassphraseHash(pk, fakeSalt, testCase.costs) |
| 550 |
if err != nil { |
| 551 |
t.Errorf("Hash test %d: for costs=%+v hashing failed: %v", i, testCase.costs, err) |
| 552 |
continue |
| 553 |
} |
| 554 |
defer hash.Wipe() |
| 555 |
|
| 556 |
actual := hex.EncodeToString(hash.data) |
| 557 |
if actual != testCase.hexHash { |
| 558 |
t.Errorf("Hash test %d: for costs=%+v expected hash of %q got %q", |
| 559 |
i, testCase.costs, testCase.hexHash, actual) |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
var badCosts = []*metadata.HashingCosts{ |
| 565 |
// Bad Time costs |
| 566 |
{Time: 0, Memory: 1 << 11, Parallelism: 1}, |
| 567 |
{Time: 1 << 33, Memory: 1 << 11, Parallelism: 1}, |
| 568 |
// Bad Memory costs |
| 569 |
{Time: 1, Memory: 5, Parallelism: 1}, |
| 570 |
{Time: 1, Memory: 1 << 33, Parallelism: 1}, |
| 571 |
// Bad Parallelism costs |
| 572 |
{Time: 1, Memory: 1 << 11, Parallelism: 0, TruncationFixed: false}, |
| 573 |
{Time: 1, Memory: 1 << 11, Parallelism: 0, TruncationFixed: true}, |
| 574 |
{Time: 1, Memory: 1 << 11, Parallelism: 256, TruncationFixed: false}, |
| 575 |
{Time: 1, Memory: 1 << 11, Parallelism: 256, TruncationFixed: true}, |
| 576 |
{Time: 1, Memory: 1 << 11, Parallelism: 257, TruncationFixed: true}, |
| 577 |
} |
| 578 |
|
| 579 |
func TestBadParameters(t *testing.T) { |
| 580 |
for i, costs := range badCosts { |
| 581 |
if costs.CheckValidity() == nil { |
| 582 |
t.Errorf("Hash test %d: expected error for costs=%+v", i, costs) |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
func BenchmarkWrap(b *testing.B) { |
| 588 |
for n := 0; n < b.N; n++ { |
| 589 |
Wrap(fakeWrappingKey, fakeValidPolicyKey) |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
func BenchmarkUnwrap(b *testing.B) { |
| 594 |
b.StopTimer() |
| 595 |
|
| 596 |
data, _ := Wrap(fakeWrappingKey, fakeValidPolicyKey) |
| 597 |
|
| 598 |
b.StartTimer() |
| 599 |
for n := 0; n < b.N; n++ { |
| 600 |
key, err := Unwrap(fakeWrappingKey, data) |
| 601 |
if err != nil { |
| 602 |
b.Fatal(err) |
| 603 |
} |
| 604 |
key.Wipe() |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
func BenchmarkUnwrapNolock(b *testing.B) { |
| 609 |
b.StopTimer() |
| 610 |
|
| 611 |
UseMlock = false |
| 612 |
defer func() { |
| 613 |
UseMlock = true |
| 614 |
}() |
| 615 |
data, _ := Wrap(fakeWrappingKey, fakeValidPolicyKey) |
| 616 |
|
| 617 |
b.StartTimer() |
| 618 |
for n := 0; n < b.N; n++ { |
| 619 |
key, err := Unwrap(fakeWrappingKey, data) |
| 620 |
if err != nil { |
| 621 |
b.Fatal(err) |
| 622 |
} |
| 623 |
key.Wipe() |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
func BenchmarkRandomWrapUnwrap(b *testing.B) { |
| 628 |
for n := 0; n < b.N; n++ { |
| 629 |
wk, _ := NewRandomKey(metadata.InternalKeyLen) |
| 630 |
sk, _ := NewRandomKey(metadata.InternalKeyLen) |
| 631 |
|
| 632 |
testWrapUnwrapEqual(wk, sk) |
| 633 |
// Must manually call wipe here, or test will use too much memory. |
| 634 |
wk.Wipe() |
| 635 |
sk.Wipe() |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
func benchmarkPassphraseHashing(b *testing.B, costs *metadata.HashingCosts) { |
| 640 |
b.StopTimer() |
| 641 |
|
| 642 |
pk, err := fakePassphraseKey() |
| 643 |
if err != nil { |
| 644 |
b.Fatal(err) |
| 645 |
} |
| 646 |
defer pk.Wipe() |
| 647 |
|
| 648 |
b.StartTimer() |
| 649 |
for n := 0; n < b.N; n++ { |
| 650 |
hash, err := PassphraseHash(pk, fakeSalt, costs) |
| 651 |
hash.Wipe() |
| 652 |
if err != nil { |
| 653 |
b.Fatal(err) |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
func BenchmarkPassphraseHashing_1MB_1Thread(b *testing.B) { |
| 659 |
benchmarkPassphraseHashing(b, |
| 660 |
&metadata.HashingCosts{Time: 1, Memory: 1 << 10, Parallelism: 1}) |
| 661 |
} |
| 662 |
|
| 663 |
func BenchmarkPassphraseHashing_1GB_1Thread(b *testing.B) { |
| 664 |
benchmarkPassphraseHashing(b, |
| 665 |
&metadata.HashingCosts{Time: 1, Memory: 1 << 20, Parallelism: 1}) |
| 666 |
} |
| 667 |
|
| 668 |
func BenchmarkPassphraseHashing_128MB_1Thread(b *testing.B) { |
| 669 |
benchmarkPassphraseHashing(b, |
| 670 |
&metadata.HashingCosts{Time: 1, Memory: 1 << 17, Parallelism: 1}) |
| 671 |
} |
| 672 |
|
| 673 |
func BenchmarkPassphraseHashing_128MB_8Thread(b *testing.B) { |
| 674 |
benchmarkPassphraseHashing(b, |
| 675 |
&metadata.HashingCosts{Time: 1, Memory: 1 << 17, Parallelism: 8}) |
| 676 |
} |
| 677 |
|
| 678 |
func BenchmarkPassphraseHashing_128MB_8Pass(b *testing.B) { |
| 679 |
benchmarkPassphraseHashing(b, |
| 680 |
&metadata.HashingCosts{Time: 8, Memory: 1 << 17, Parallelism: 1}) |
| 681 |
} |