2014-12-05 21:45:49 +01:00
|
|
|
package restic
|
2014-09-23 22:39:12 +02:00
|
|
|
|
|
|
|
import (
|
2015-02-11 17:41:11 +01:00
|
|
|
"bytes"
|
2014-09-23 22:39:12 +02:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-02-11 17:41:11 +01:00
|
|
|
"hash"
|
2014-09-23 22:39:12 +02:00
|
|
|
"io"
|
2015-02-11 18:36:31 +01:00
|
|
|
"io/ioutil"
|
2014-09-23 22:39:12 +02:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"time"
|
|
|
|
|
2014-12-05 21:45:49 +01:00
|
|
|
"github.com/restic/restic/backend"
|
|
|
|
"github.com/restic/restic/chunker"
|
2014-09-23 22:39:12 +02:00
|
|
|
|
2014-11-15 19:04:32 +01:00
|
|
|
"golang.org/x/crypto/scrypt"
|
2014-09-23 22:39:12 +02:00
|
|
|
)
|
|
|
|
|
2014-11-23 16:48:00 +01:00
|
|
|
// max size is 8MiB, defined in chunker
|
|
|
|
const ivSize = aes.BlockSize
|
|
|
|
const hmacSize = sha256.Size
|
2014-11-23 22:58:28 +01:00
|
|
|
const maxCiphertextSize = ivSize + chunker.MaxSize + hmacSize
|
|
|
|
const CiphertextExtension = ivSize + hmacSize
|
2014-11-23 16:48:00 +01:00
|
|
|
|
2014-09-23 22:39:12 +02:00
|
|
|
var (
|
2014-11-15 17:17:24 +01: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-11-23 22:58:28 +01:00
|
|
|
// ErrBufferTooSmall is returned when the destination slice is too small
|
|
|
|
// for the ciphertext.
|
|
|
|
ErrBufferTooSmall = errors.New("destination buffer too small")
|
2014-09-23 22:39:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: figure out scrypt values on the fly depending on the current
|
|
|
|
// hardware.
|
|
|
|
const (
|
2014-11-15 17:17:24 +01:00
|
|
|
scryptN = 65536
|
|
|
|
scryptR = 8
|
|
|
|
scryptP = 1
|
|
|
|
scryptSaltsize = 64
|
|
|
|
aesKeysize = 32 // for AES256
|
|
|
|
hmacKeysize = 32 // for HMAC with SHA256
|
2014-09-23 22:39:12 +02:00
|
|
|
)
|
|
|
|
|
2014-11-15 17:17:24 +01:00
|
|
|
// Key represents an encrypted master key for a repository.
|
2014-09-23 22:39:12 +02: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-25 23:18:02 +01:00
|
|
|
|
|
|
|
id backend.ID
|
2014-09-23 22:39:12 +02:00
|
|
|
}
|
|
|
|
|
2014-11-15 17:17:24 +01:00
|
|
|
// keys is a JSON structure that holds signing and encryption keys.
|
2014-09-23 22:39:12 +02:00
|
|
|
type keys struct {
|
|
|
|
Sign []byte
|
|
|
|
Encrypt []byte
|
|
|
|
}
|
|
|
|
|
2014-11-15 17:17:24 +01:00
|
|
|
// CreateKey initializes a master key in the given backend and encrypts it with
|
|
|
|
// the password.
|
2014-12-21 17:02:49 +01:00
|
|
|
func CreateKey(s Server, password string) (*Key, error) {
|
2014-12-21 18:16:22 +01:00
|
|
|
return AddKey(s, password, nil)
|
2014-09-23 22:39:12 +02:00
|
|
|
}
|
|
|
|
|
2014-11-15 17:17:24 +01:00
|
|
|
// OpenKey tries do decrypt the key specified by id with the given password.
|
2014-12-21 17:02:49 +01:00
|
|
|
func OpenKey(s Server, id backend.ID, password string) (*Key, error) {
|
2014-09-23 22:39:12 +02:00
|
|
|
// extract data from repo
|
2014-12-21 17:02:49 +01:00
|
|
|
data, err := s.Get(backend.Key, id)
|
2014-09-23 22:39:12 +02:00
|
|
|
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
|
|
|
|
}
|
2014-11-25 23:18:02 +01:00
|
|
|
k.id = id
|
2014-09-23 22:39:12 +02:00
|
|
|
|
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
|
2014-11-15 17:17:24 +01:00
|
|
|
// SearchKey tries to decrypt all keys in the backend with the given password.
|
|
|
|
// If none could be found, ErrNoKeyFound is returned.
|
2014-12-21 17:02:49 +01:00
|
|
|
func SearchKey(s Server, password string) (*Key, error) {
|
2014-09-23 22:39:12 +02:00
|
|
|
// list all keys
|
2014-12-21 17:02:49 +01:00
|
|
|
ids, err := s.List(backend.Key)
|
2014-09-23 22:39:12 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// try all keys in repo
|
|
|
|
var key *Key
|
|
|
|
for _, id := range ids {
|
2014-12-21 17:02:49 +01:00
|
|
|
key, err = OpenKey(s, id, password)
|
2014-09-23 22:39:12 +02:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNoKeyFound
|
|
|
|
}
|
|
|
|
|
2014-11-25 23:07:00 +01:00
|
|
|
// AddKey adds a new key to an already existing repository.
|
2014-12-21 18:16:22 +01:00
|
|
|
func AddKey(s Server, password string, template *Key) (*Key, error) {
|
2014-11-25 23:07:00 +01:00
|
|
|
// fill meta data about key
|
|
|
|
newkey := &Key{
|
|
|
|
Created: time.Now(),
|
|
|
|
KDF: "scrypt",
|
|
|
|
N: scryptN,
|
|
|
|
R: scryptR,
|
|
|
|
P: scryptP,
|
|
|
|
}
|
|
|
|
|
|
|
|
hn, err := os.Hostname()
|
|
|
|
if err == nil {
|
|
|
|
newkey.Hostname = hn
|
|
|
|
}
|
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err == nil {
|
|
|
|
newkey.Username = usr.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate random salt
|
|
|
|
newkey.Salt = make([]byte, scryptSaltsize)
|
|
|
|
n, err := rand.Read(newkey.Salt)
|
|
|
|
if n != scryptSaltsize || err != nil {
|
|
|
|
panic("unable to read enough random bytes for salt")
|
|
|
|
}
|
|
|
|
|
|
|
|
// call scrypt() to derive user key
|
|
|
|
newkey.user, err = newkey.scrypt(password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-21 18:16:22 +01:00
|
|
|
if template == nil {
|
|
|
|
// generate new random master keys
|
|
|
|
newkey.master, err = newkey.newKeys()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// copy master keys from old key
|
|
|
|
newkey.master = template.master
|
|
|
|
}
|
2014-11-25 23:07:00 +01:00
|
|
|
|
|
|
|
// encrypt master keys (as json) with user key
|
|
|
|
buf, err := json.Marshal(newkey.master)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newkey.Data = GetChunkBuf("key")
|
|
|
|
n, err = newkey.EncryptUser(newkey.Data, buf)
|
|
|
|
newkey.Data = newkey.Data[:n]
|
|
|
|
|
|
|
|
// dump as json
|
|
|
|
buf, err = json.Marshal(newkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// store in repository and return
|
2014-12-21 17:02:49 +01:00
|
|
|
id, err := s.Create(backend.Key, buf)
|
2014-11-25 23:07:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-25 23:18:02 +01:00
|
|
|
newkey.id = id
|
2014-11-25 23:07:00 +01:00
|
|
|
|
|
|
|
FreeChunkBuf("key", newkey.Data)
|
|
|
|
|
2014-12-21 18:16:22 +01:00
|
|
|
return newkey, nil
|
2014-11-25 23:07:00 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 22:39:12 +02:00
|
|
|
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 17:17:24 +01:00
|
|
|
scryptKeys, err := scrypt.Key([]byte(password), k.Salt, k.N, k.R, k.P, keybytes)
|
2014-09-23 22:39:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error deriving keys from password: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-11-15 17:17:24 +01:00
|
|
|
if len(scryptKeys) != keybytes {
|
|
|
|
return nil, fmt.Errorf("invalid numbers of bytes expanded from scrypt(): %d", len(scryptKeys))
|
2014-09-23 22:39:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ks := &keys{
|
2014-11-15 17:17:24 +01:00
|
|
|
Encrypt: scryptKeys[:aesKeysize],
|
|
|
|
Sign: scryptKeys[aesKeysize:],
|
2014-09-23 22:39:12 +02: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
|
|
|
|
}
|
|
|
|
|
2014-11-23 16:48:00 +01:00
|
|
|
func (k *Key) newIV(buf []byte) error {
|
|
|
|
_, err := io.ReadFull(rand.Reader, buf[:ivSize])
|
|
|
|
buf = buf[:ivSize]
|
2014-09-23 22:39:12 +02:00
|
|
|
if err != nil {
|
2014-11-23 16:48:00 +01:00
|
|
|
return err
|
2014-09-23 22:39:12 +02:00
|
|
|
}
|
|
|
|
|
2014-11-23 16:48:00 +01:00
|
|
|
return nil
|
2014-09-23 22:39:12 +02:00
|
|
|
}
|
|
|
|
|
2014-11-23 16:48:00 +01:00
|
|
|
// Encrypt encrypts and signs data. Stored in ciphertext is IV || Ciphertext ||
|
|
|
|
// HMAC. Encrypt returns the ciphertext's length. For the hash function, SHA256
|
|
|
|
// is used, so the overhead is 16+32=48 byte.
|
|
|
|
func (k *Key) encrypt(ks *keys, ciphertext, plaintext []byte) (int, error) {
|
2014-11-23 22:58:28 +01:00
|
|
|
if cap(ciphertext) < len(plaintext)+ivSize+hmacSize {
|
|
|
|
return 0, ErrBufferTooSmall
|
2014-11-23 16:48:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := io.ReadFull(rand.Reader, ciphertext[:ivSize])
|
2014-09-23 22:39:12 +02:00
|
|
|
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-11-23 16:48:00 +01:00
|
|
|
e := cipher.NewCTR(c, ciphertext[:ivSize])
|
|
|
|
e.XORKeyStream(ciphertext[ivSize:cap(ciphertext)], plaintext)
|
|
|
|
ciphertext = ciphertext[:ivSize+len(plaintext)]
|
2014-09-23 22:39:12 +02: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))
|
|
|
|
}
|
|
|
|
|
2014-11-23 16:48:00 +01:00
|
|
|
ciphertext = hm.Sum(ciphertext)
|
|
|
|
|
|
|
|
return len(ciphertext), nil
|
2014-09-23 22:39:12 +02:00
|
|
|
}
|
|
|
|
|
2014-11-23 16:48:00 +01:00
|
|
|
// EncryptUser encrypts and signs data with the user key. Stored in ciphertext
|
|
|
|
// is IV || Ciphertext || HMAC. Returns the ciphertext length. For the hash
|
|
|
|
// function, SHA256 is used, so the overhead is 16+32=48 byte.
|
|
|
|
func (k *Key) EncryptUser(ciphertext, plaintext []byte) (int, error) {
|
|
|
|
return k.encrypt(k.user, ciphertext, plaintext)
|
2014-09-23 22:39:12 +02:00
|
|
|
}
|
|
|
|
|
2014-11-23 16:48:00 +01:00
|
|
|
// Encrypt encrypts and signs data with the master key. Stored in ciphertext is
|
|
|
|
// IV || Ciphertext || HMAC. Returns the ciphertext length. For the hash
|
|
|
|
// function, SHA256 is used, so the overhead is 16+32=48 byte.
|
|
|
|
func (k *Key) Encrypt(ciphertext, plaintext []byte) (int, error) {
|
|
|
|
return k.encrypt(k.master, ciphertext, plaintext)
|
2014-09-23 22:39:12 +02:00
|
|
|
}
|
|
|
|
|
2015-02-11 17:41:11 +01:00
|
|
|
type HashReader struct {
|
|
|
|
r io.Reader
|
|
|
|
h hash.Hash
|
|
|
|
sum []byte
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHashReader(r io.Reader, h hash.Hash) *HashReader {
|
|
|
|
return &HashReader{
|
|
|
|
h: h,
|
|
|
|
r: io.TeeReader(r, h),
|
|
|
|
sum: make([]byte, 0, h.Size()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HashReader) Read(p []byte) (n int, err error) {
|
|
|
|
if !h.closed {
|
|
|
|
n, err = h.r.Read(p)
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
h.closed = true
|
|
|
|
h.sum = h.h.Sum(h.sum)
|
|
|
|
} else if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.closed {
|
|
|
|
// output hash
|
|
|
|
r := len(p) - n
|
|
|
|
|
|
|
|
if r > 0 {
|
|
|
|
c := copy(p[n:], h.sum)
|
|
|
|
h.sum = h.sum[c:]
|
|
|
|
|
|
|
|
n += c
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(h.sum) == 0 {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryptFrom encrypts and signs data read from rd with ks. The returned
|
|
|
|
// io.Reader reads IV || Ciphertext || HMAC. For the hash function, SHA256 is
|
|
|
|
// used.
|
|
|
|
func (k *Key) encryptFrom(ks *keys, rd io.Reader) io.Reader {
|
|
|
|
// create IV
|
|
|
|
iv := make([]byte, ivSize)
|
|
|
|
|
|
|
|
_, err := io.ReadFull(rand.Reader, iv)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
ivReader := bytes.NewReader(iv)
|
|
|
|
|
|
|
|
encryptReader := cipher.StreamReader{
|
|
|
|
R: rd,
|
|
|
|
S: cipher.NewCTR(c, iv),
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewHashReader(io.MultiReader(ivReader, encryptReader),
|
|
|
|
hmac.New(sha256.New, ks.Sign))
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptFrom encrypts and signs data read from rd with the master key. The
|
|
|
|
// returned io.Reader reads IV || Ciphertext || HMAC. For the hash function,
|
|
|
|
// SHA256 is used.
|
|
|
|
func (k *Key) EncryptFrom(rd io.Reader) io.Reader {
|
|
|
|
return k.encryptFrom(k.master, rd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptFrom encrypts and signs data read from rd with the user key. The
|
|
|
|
// returned io.Reader reads IV || Ciphertext || HMAC. For the hash function,
|
|
|
|
// SHA256 is used.
|
|
|
|
func (k *Key) EncryptUserFrom(rd io.Reader) io.Reader {
|
|
|
|
return k.encryptFrom(k.user, rd)
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:39:12 +02:00
|
|
|
// 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) {
|
2014-11-24 22:11:09 +01:00
|
|
|
// check for plausible length
|
2014-11-30 16:06:37 +01:00
|
|
|
if len(ciphertext) < ivSize+hmacSize {
|
2014-11-24 22:11:09 +01:00
|
|
|
panic("trying to decryipt invalid data: ciphertext too small")
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:39:12 +02:00
|
|
|
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 16:57:42 +02:00
|
|
|
e := cipher.NewCTR(c, iv)
|
2014-09-23 22:39:12 +02:00
|
|
|
plaintext := make([]byte, len(ciphertext))
|
2014-10-04 16:57:42 +02:00
|
|
|
e.XORKeyStream(plaintext, ciphertext)
|
2014-09-23 22:39:12 +02:00
|
|
|
|
2014-10-04 16:57:42 +02:00
|
|
|
return plaintext, nil
|
2014-09-23 22:39:12 +02: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)
|
|
|
|
}
|
|
|
|
|
2014-12-29 12:39:18 +01:00
|
|
|
// DecryptUser verifes and decrypts the ciphertext with the user key. Ciphertext
|
2014-09-23 22:39:12 +02:00
|
|
|
// must be in the form IV || Ciphertext || HMAC.
|
|
|
|
func (k *Key) DecryptUser(ciphertext []byte) ([]byte, error) {
|
|
|
|
return k.decrypt(k.user, ciphertext)
|
|
|
|
}
|
|
|
|
|
2015-02-11 18:36:31 +01:00
|
|
|
// decryptFrom verifies and decrypts the ciphertext read from rd with ks and
|
|
|
|
// makes it available on the returned Reader. Ciphertext must be in the form IV
|
|
|
|
// || Ciphertext || HMAC. In order to correctly verify the ciphertext, rd is
|
|
|
|
// drained, locally buffered and made available on the returned Reader
|
|
|
|
// afterwards. If an HMAC verification failure is observed, it is returned
|
|
|
|
// immediately.
|
|
|
|
func (k *Key) decryptFrom(ks *keys, rd io.Reader) (io.Reader, error) {
|
|
|
|
ciphertext, err := ioutil.ReadAll(rd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for plausible length
|
|
|
|
if len(ciphertext) < ivSize+hmacSize {
|
|
|
|
panic("trying to decryipt invalid data: ciphertext too small")
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
r := cipher.StreamReader{
|
|
|
|
S: cipher.NewCTR(c, iv),
|
|
|
|
R: bytes.NewReader(ciphertext),
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptFrom verifies and decrypts the ciphertext read from rd and makes it
|
|
|
|
// available on the returned Reader. Ciphertext must be in the form IV ||
|
|
|
|
// Ciphertext || HMAC. In order to correctly verify the ciphertext, rd is
|
|
|
|
// drained, locally buffered and made available on the returned Reader
|
|
|
|
// afterwards. If an HMAC verification failure is observed, it is returned
|
|
|
|
// immediately.
|
|
|
|
func (k *Key) DecryptFrom(rd io.Reader) (io.Reader, error) {
|
|
|
|
return k.decryptFrom(k.master, rd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptFrom verifies and decrypts the ciphertext read from rd with the user
|
|
|
|
// key and makes it available on the returned Reader. Ciphertext must be in the
|
|
|
|
// form IV || Ciphertext || HMAC. In order to correctly verify the ciphertext,
|
|
|
|
// rd is drained, locally buffered and made available on the returned Reader
|
|
|
|
// afterwards. If an HMAC verification failure is observed, it is returned
|
|
|
|
// immediately.
|
|
|
|
func (k *Key) DecryptUserFrom(rd io.Reader) (io.Reader, error) {
|
|
|
|
return k.decryptFrom(k.user, rd)
|
|
|
|
}
|
|
|
|
|
2014-09-23 22:39:12 +02:00
|
|
|
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)
|
|
|
|
}
|
2014-11-25 23:18:02 +01:00
|
|
|
|
|
|
|
func (k Key) ID() backend.ID {
|
|
|
|
return k.id
|
|
|
|
}
|