storj/pkg/encryption/password.go
JT Olio 031ba86de5
argon2: choose a steady parallelism value (#3630)
* argon2: choose a steady parallelism value

Change-Id: I6006da7d7980cda88f5f08ee759612df23a8132d

* whoops, not cruft

Change-Id: Ied9039f9a9be1d0f6ff3c7d5c4839a83fc7b4b1f

* fix broken test file

Change-Id: I07288cd6cef32ba387f2f003febff5c297e50997

* fix linting error

Change-Id: Icdbda8b709cc100a86f3859303c40edb8dff1e0f
2019-11-22 14:00:04 -07:00

48 lines
1.2 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package encryption
import (
"crypto/hmac"
"crypto/sha256"
"github.com/zeebo/errs"
"golang.org/x/crypto/argon2"
"storj.io/storj/pkg/storj"
"storj.io/storj/private/memory"
)
func sha256hmac(key, data []byte) ([]byte, error) {
h := hmac.New(sha256.New, key)
if _, err := h.Write(data); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// DeriveRootKey derives a root key for some path using the salt for the bucket and
// a password from the user. See the password key derivation design doc.
func DeriveRootKey(password, salt []byte, path storj.Path, argon2Threads uint8) (*storj.Key, error) {
mixedSalt, err := sha256hmac(password, salt)
if err != nil {
return nil, err
}
pathSalt, err := sha256hmac(mixedSalt, []byte(path))
if err != nil {
return nil, err
}
// use a time of 1, 64MB of ram, and all of the cores.
keyData := argon2.IDKey(password, pathSalt, 1, uint32(64*memory.MiB/memory.KiB), argon2Threads, 32)
if len(keyData) != len(storj.Key{}) {
return nil, errs.New("invalid output from argon2id")
}
var key storj.Key
copy(key[:], keyData)
return &key, nil
}