2019-06-21 23:21:16 +01:00
|
|
|
// 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"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/memory"
|
2019-06-21 23:21:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
2019-11-22 21:00:04 +00:00
|
|
|
func DeriveRootKey(password, salt []byte, path storj.Path, argon2Threads uint8) (*storj.Key, error) {
|
2019-06-21 23:21:16 +01:00
|
|
|
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.
|
2019-11-22 21:00:04 +00:00
|
|
|
keyData := argon2.IDKey(password, pathSalt, 1, uint32(64*memory.MiB/memory.KiB), argon2Threads, 32)
|
2019-06-21 23:21:16 +01:00
|
|
|
if len(keyData) != len(storj.Key{}) {
|
|
|
|
return nil, errs.New("invalid output from argon2id")
|
|
|
|
}
|
|
|
|
|
|
|
|
var key storj.Key
|
|
|
|
copy(key[:], keyData)
|
|
|
|
return &key, nil
|
|
|
|
}
|