2014-09-23 20:39:12 +00:00
|
|
|
package khepri
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fd0/khepri/backend"
|
|
|
|
|
|
|
|
"code.google.com/p/go.crypto/scrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-11-15 16:17:24 +00:00
|
|
|
// ErrUnauthenticated is returned when ciphertext verification has failed.
|
|
|
|
ErrUnauthenticated = errors.New("ciphertext verification failed")
|
|
|
|
// ErrNoKeyFound is returned when no key for the repository could be decrypted.
|
|
|
|
ErrNoKeyFound = errors.New("no key could be found")
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: figure out scrypt values on the fly depending on the current
|
|
|
|
// hardware.
|
|
|
|
const (
|
2014-11-15 16:17:24 +00:00
|
|
|
scryptN = 65536
|
|
|
|
scryptR = 8
|
|
|
|
scryptP = 1
|
|
|
|
scryptSaltsize = 64
|
|
|
|
aesKeysize = 32 // for AES256
|
|
|
|
hmacKeysize = 32 // for HMAC with SHA256
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
// Key represents an encrypted master key for a repository.
|
2014-09-23 20:39:12 +00:00
|
|
|
type Key struct {
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
Comment string `json:"comment,omitempty"`
|
|
|
|
|
|
|
|
KDF string `json:"kdf"`
|
|
|
|
N int `json:"N"`
|
|
|
|
R int `json:"r"`
|
|
|
|
P int `json:"p"`
|
|
|
|
Salt []byte `json:"salt"`
|
|
|
|
Data []byte `json:"data"`
|
|
|
|
|
|
|
|
user *keys
|
|
|
|
master *keys
|
|
|
|
}
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
// keys is a JSON structure that holds signing and encryption keys.
|
2014-09-23 20:39:12 +00:00
|
|
|
type keys struct {
|
|
|
|
Sign []byte
|
|
|
|
Encrypt []byte
|
|
|
|
}
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
// CreateKey initializes a master key in the given backend and encrypts it with
|
|
|
|
// the password.
|
2014-09-23 20:39:12 +00:00
|
|
|
func CreateKey(be backend.Server, password string) (*Key, error) {
|
|
|
|
// fill meta data about key
|
|
|
|
k := &Key{
|
|
|
|
Created: time.Now(),
|
|
|
|
KDF: "scrypt",
|
2014-11-15 16:17:24 +00:00
|
|
|
N: scryptN,
|
|
|
|
R: scryptR,
|
|
|
|
P: scryptP,
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hn, err := os.Hostname()
|
|
|
|
if err == nil {
|
|
|
|
k.Hostname = hn
|
|
|
|
}
|
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err == nil {
|
|
|
|
k.Username = usr.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate random salt
|
2014-11-15 16:17:24 +00:00
|
|
|
k.Salt = make([]byte, scryptSaltsize)
|
2014-09-23 20:39:12 +00:00
|
|
|
n, err := rand.Read(k.Salt)
|
2014-11-15 16:17:24 +00:00
|
|
|
if n != scryptSaltsize || err != nil {
|
2014-09-23 20:39:12 +00:00
|
|
|
panic("unable to read enough random bytes for salt")
|
|
|
|
}
|
|
|
|
|
|
|
|
// call scrypt() to derive user key
|
|
|
|
k.user, err = k.scrypt(password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate new random master keys
|
|
|
|
k.master, err = k.newKeys()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// encrypt master keys (as json) with user key
|
|
|
|
buf, err := json.Marshal(k.master)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
k.Data, err = k.EncryptUser(buf)
|
|
|
|
|
|
|
|
// dump as json
|
|
|
|
buf, err = json.Marshal(k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// store in repository and return
|
|
|
|
_, err = be.Create(backend.Key, buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
// OpenKey tries do decrypt the key specified by id with the given password.
|
2014-09-23 20:39:12 +00:00
|
|
|
func OpenKey(be backend.Server, id backend.ID, password string) (*Key, error) {
|
|
|
|
// extract data from repo
|
|
|
|
data, err := be.Get(backend.Key, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore json
|
|
|
|
k := &Key{}
|
|
|
|
err = json.Unmarshal(data, k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check KDF
|
|
|
|
if k.KDF != "scrypt" {
|
|
|
|
return nil, errors.New("only supported KDF is scrypt()")
|
|
|
|
}
|
|
|
|
|
|
|
|
// derive user key
|
|
|
|
k.user, err = k.scrypt(password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt master keys
|
|
|
|
buf, err := k.DecryptUser(k.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore json
|
|
|
|
k.master = &keys{}
|
|
|
|
err = json.Unmarshal(buf, k.master)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
// SearchKey tries to decrypt all keys in the backend with the given password.
|
|
|
|
// If none could be found, ErrNoKeyFound is returned.
|
2014-09-23 20:39:12 +00:00
|
|
|
func SearchKey(be backend.Server, password string) (*Key, error) {
|
|
|
|
// list all keys
|
|
|
|
ids, err := be.List(backend.Key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// try all keys in repo
|
|
|
|
var key *Key
|
|
|
|
for _, id := range ids {
|
|
|
|
key, err = OpenKey(be, id, password)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNoKeyFound
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Key) scrypt(password string) (*keys, error) {
|
|
|
|
if len(k.Salt) == 0 {
|
|
|
|
return nil, fmt.Errorf("scrypt() called with empty salt")
|
|
|
|
}
|
|
|
|
|
|
|
|
keybytes := hmacKeysize + aesKeysize
|
2014-11-15 16:17:24 +00:00
|
|
|
scryptKeys, err := scrypt.Key([]byte(password), k.Salt, k.N, k.R, k.P, keybytes)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error deriving keys from password: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
if len(scryptKeys) != keybytes {
|
|
|
|
return nil, fmt.Errorf("invalid numbers of bytes expanded from scrypt(): %d", len(scryptKeys))
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ks := &keys{
|
2014-11-15 16:17:24 +00:00
|
|
|
Encrypt: scryptKeys[:aesKeysize],
|
|
|
|
Sign: scryptKeys[aesKeysize:],
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
return ks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Key) newKeys() (*keys, error) {
|
|
|
|
ks := &keys{
|
|
|
|
Encrypt: make([]byte, aesKeysize),
|
|
|
|
Sign: make([]byte, hmacKeysize),
|
|
|
|
}
|
|
|
|
n, err := rand.Read(ks.Encrypt)
|
|
|
|
if n != aesKeysize || err != nil {
|
|
|
|
panic("unable to read enough random bytes for encryption key")
|
|
|
|
}
|
|
|
|
n, err = rand.Read(ks.Sign)
|
|
|
|
if n != hmacKeysize || err != nil {
|
|
|
|
panic("unable to read enough random bytes for signing key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Key) newIV() ([]byte, error) {
|
|
|
|
buf := make([]byte, aes.BlockSize)
|
|
|
|
_, err := io.ReadFull(rand.Reader, buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt encrypts and signs data. Returned is IV || Ciphertext || HMAC. For
|
|
|
|
// the hash function, SHA256 is used, so the overhead is 16+32=48 byte.
|
|
|
|
func (k *Key) encrypt(ks *keys, plaintext []byte) ([]byte, error) {
|
|
|
|
iv, err := k.newIV()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to generate new random iv: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := aes.NewCipher(ks.Encrypt)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to create cipher: %v", err))
|
|
|
|
}
|
|
|
|
|
2014-10-04 14:57:42 +00:00
|
|
|
e := cipher.NewCTR(c, iv)
|
|
|
|
l := len(iv)
|
|
|
|
ciphertext := make([]byte, l+len(plaintext))
|
|
|
|
copy(ciphertext[:l], iv)
|
|
|
|
e.XORKeyStream(ciphertext[l:], plaintext)
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
hm := hmac.New(sha256.New, ks.Sign)
|
|
|
|
|
|
|
|
n, err := hm.Write(ciphertext)
|
|
|
|
if err != nil || n != len(ciphertext) {
|
|
|
|
panic(fmt.Sprintf("unable to calculate hmac of ciphertext: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return hm.Sum(ciphertext), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptUser encrypts and signs data with the user key. Returned is IV ||
|
|
|
|
// Ciphertext || HMAC. For the hash function, SHA256 is used, so the overhead
|
|
|
|
// is 16+32=48 byte.
|
|
|
|
func (k *Key) EncryptUser(plaintext []byte) ([]byte, error) {
|
|
|
|
return k.encrypt(k.user, plaintext)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt encrypts and signs data with the master key. Returned is IV ||
|
|
|
|
// Ciphertext || HMAC. For the hash function, SHA256 is used, so the overhead
|
|
|
|
// is 16+32=48 byte.
|
|
|
|
func (k *Key) Encrypt(plaintext []byte) ([]byte, error) {
|
|
|
|
return k.encrypt(k.master, plaintext)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt verifes and decrypts the ciphertext. Ciphertext must be in the form
|
|
|
|
// IV || Ciphertext || HMAC.
|
|
|
|
func (k *Key) decrypt(ks *keys, ciphertext []byte) ([]byte, error) {
|
|
|
|
hm := hmac.New(sha256.New, ks.Sign)
|
|
|
|
|
|
|
|
// extract hmac
|
|
|
|
l := len(ciphertext) - hm.Size()
|
|
|
|
ciphertext, mac := ciphertext[:l], ciphertext[l:]
|
|
|
|
|
|
|
|
// calculate new hmac
|
|
|
|
n, err := hm.Write(ciphertext)
|
|
|
|
if err != nil || n != len(ciphertext) {
|
|
|
|
panic(fmt.Sprintf("unable to calculate hmac of ciphertext, err %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify hmac
|
|
|
|
mac2 := hm.Sum(nil)
|
|
|
|
|
|
|
|
if !hmac.Equal(mac, mac2) {
|
|
|
|
return nil, ErrUnauthenticated
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract iv
|
|
|
|
iv, ciphertext := ciphertext[:aes.BlockSize], ciphertext[aes.BlockSize:]
|
|
|
|
|
|
|
|
// decrypt data
|
|
|
|
c, err := aes.NewCipher(ks.Encrypt)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to create cipher: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt
|
2014-10-04 14:57:42 +00:00
|
|
|
e := cipher.NewCTR(c, iv)
|
2014-09-23 20:39:12 +00:00
|
|
|
plaintext := make([]byte, len(ciphertext))
|
2014-10-04 14:57:42 +00:00
|
|
|
e.XORKeyStream(plaintext, ciphertext)
|
2014-09-23 20:39:12 +00:00
|
|
|
|
2014-10-04 14:57:42 +00:00
|
|
|
return plaintext, nil
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt verifes and decrypts the ciphertext with the master key. Ciphertext
|
|
|
|
// must be in the form IV || Ciphertext || HMAC.
|
|
|
|
func (k *Key) Decrypt(ciphertext []byte) ([]byte, error) {
|
|
|
|
return k.decrypt(k.master, ciphertext)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptUser verifes and decrypts the ciphertext with the master key. Ciphertext
|
|
|
|
// must be in the form IV || Ciphertext || HMAC.
|
|
|
|
func (k *Key) DecryptUser(ciphertext []byte) ([]byte, error) {
|
|
|
|
return k.decrypt(k.user, ciphertext)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each calls backend.Each() with the given parameters, Decrypt() on the
|
|
|
|
// ciphertext and, on successful decryption, f with the plaintext.
|
|
|
|
func (k *Key) Each(be backend.Server, t backend.Type, f func(backend.ID, []byte, error)) error {
|
|
|
|
return backend.Each(be, t, func(id backend.ID, data []byte, e error) {
|
|
|
|
if e != nil {
|
|
|
|
f(id, nil, e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := k.Decrypt(data)
|
|
|
|
if err != nil {
|
|
|
|
f(id, nil, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f(id, buf, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Key) String() string {
|
|
|
|
if k == nil {
|
|
|
|
return "<Key nil>"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("<Key of %s@%s, created on %s>", k.Username, k.Hostname, k.Created)
|
|
|
|
}
|