2015-04-12 07:36:14 +00:00
|
|
|
package crypto
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
2015-03-22 20:03:01 +00:00
|
|
|
"encoding/json"
|
2015-04-12 07:36:14 +00:00
|
|
|
"errors"
|
2015-03-14 18:53:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2015-04-12 07:36:14 +00:00
|
|
|
"github.com/restic/restic/chunker"
|
2015-03-14 18:53:51 +00:00
|
|
|
"golang.org/x/crypto/poly1305"
|
|
|
|
"golang.org/x/crypto/scrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-12 07:41:47 +00:00
|
|
|
aesKeySize = 32 // for AES256
|
|
|
|
macKeySizeK = 16 // for AES-128
|
|
|
|
macKeySizeR = 16 // for Poly1305
|
|
|
|
macKeySize = macKeySizeK + macKeySizeR // for Poly1305-AES128
|
2015-03-22 18:09:30 +00:00
|
|
|
ivSize = aes.BlockSize
|
2015-04-12 07:36:14 +00:00
|
|
|
|
2015-04-12 07:41:47 +00:00
|
|
|
macSize = poly1305.TagSize // Poly1305 size is 16 byte
|
|
|
|
Extension = ivSize + macSize
|
2015-04-12 07:36:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrUnauthenticated is returned when ciphertext verification has failed.
|
|
|
|
ErrUnauthenticated = errors.New("ciphertext verification failed")
|
|
|
|
|
|
|
|
// ErrBufferTooSmall is returned when the destination slice is too small
|
|
|
|
// for the ciphertext.
|
|
|
|
ErrBufferTooSmall = errors.New("destination buffer too small")
|
2015-03-14 18:53:51 +00:00
|
|
|
)
|
|
|
|
|
2015-04-12 07:41:47 +00:00
|
|
|
// Key holds signing and encryption keys for a repository. It is stored
|
2015-04-12 07:36:14 +00:00
|
|
|
// encrypted and signed as a JSON data structure in the Data field of the Key
|
|
|
|
// structure. For the master key, the secret random polynomial used for content
|
|
|
|
// defined chunking is included.
|
2015-04-12 07:41:47 +00:00
|
|
|
type Key struct {
|
2015-04-12 08:53:46 +00:00
|
|
|
Sign SigningKey `json:"sign"`
|
|
|
|
Encrypt EncryptionKey `json:"encrypt"`
|
|
|
|
ChunkerPolynomial chunker.Pol `json:"chunker_polynomial,omitempty"`
|
2015-04-12 07:36:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-12 08:53:46 +00:00
|
|
|
type EncryptionKey [32]byte
|
|
|
|
type SigningKey struct {
|
|
|
|
K [16]byte `json:"k"` // for AES128
|
|
|
|
R [16]byte `json:"r"` // for Poly1305
|
2015-04-26 11:07:26 +00:00
|
|
|
|
|
|
|
masked bool // remember if the signing key has already been masked
|
2015-03-22 18:09:30 +00:00
|
|
|
}
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
// mask for key, (cf. http://cr.yp.to/mac/poly1305-20050329.pdf)
|
|
|
|
var poly1305KeyMask = [16]byte{
|
|
|
|
0xff,
|
|
|
|
0xff,
|
|
|
|
0xff,
|
|
|
|
0x0f, // 3: top four bits zero
|
|
|
|
0xfc, // 4: bottom two bits zero
|
|
|
|
0xff,
|
|
|
|
0xff,
|
|
|
|
0x0f, // 7: top four bits zero
|
|
|
|
0xfc, // 8: bottom two bits zero
|
|
|
|
0xff,
|
|
|
|
0xff,
|
|
|
|
0x0f, // 11: top four bits zero
|
|
|
|
0xfc, // 12: bottom two bits zero
|
|
|
|
0xff,
|
|
|
|
0xff,
|
|
|
|
0x0f, // 15: top four bits zero
|
|
|
|
}
|
|
|
|
|
|
|
|
// key is a [32]byte, in the form k||r
|
2015-04-19 12:56:23 +00:00
|
|
|
func poly1305Sign(msg []byte, nonce []byte, key *SigningKey) []byte {
|
2015-03-14 18:53:51 +00:00
|
|
|
// prepare key for low-level poly1305.Sum(): r||n
|
|
|
|
var k [32]byte
|
|
|
|
|
|
|
|
// make sure key is masked
|
2015-04-26 11:07:26 +00:00
|
|
|
if !key.masked {
|
|
|
|
maskKey(key)
|
|
|
|
}
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
// fill in nonce, encrypted with AES and key[:16]
|
2015-03-22 18:09:30 +00:00
|
|
|
cipher, err := aes.NewCipher(key.K[:])
|
2015-03-14 18:53:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
cipher.Encrypt(k[16:], nonce[:])
|
|
|
|
|
|
|
|
// copy r
|
2015-03-22 18:09:30 +00:00
|
|
|
copy(k[:16], key.R[:])
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
// save mac in out
|
|
|
|
var out [16]byte
|
|
|
|
poly1305.Sum(&out, msg, &k)
|
|
|
|
|
|
|
|
return out[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// mask poly1305 key
|
2015-04-12 08:53:46 +00:00
|
|
|
func maskKey(k *SigningKey) {
|
2015-03-14 18:53:51 +00:00
|
|
|
if k == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := 0; i < poly1305.TagSize; i++ {
|
2015-03-22 18:09:30 +00:00
|
|
|
k.R[i] = k.R[i] & poly1305KeyMask[i]
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
2015-04-26 11:07:26 +00:00
|
|
|
|
|
|
|
k.masked = true
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 18:09:30 +00:00
|
|
|
// construct mac key from slice (k||r), with masking
|
2015-04-12 08:53:46 +00:00
|
|
|
func macKeyFromSlice(mk *SigningKey, data []byte) {
|
2015-03-22 18:09:30 +00:00
|
|
|
copy(mk.K[:], data[:16])
|
|
|
|
copy(mk.R[:], data[16:32])
|
|
|
|
maskKey(mk)
|
|
|
|
}
|
|
|
|
|
2015-03-14 18:53:51 +00:00
|
|
|
// key: k||r
|
2015-04-19 12:56:23 +00:00
|
|
|
func poly1305Verify(msg []byte, nonce []byte, key *SigningKey, mac []byte) bool {
|
2015-03-14 18:53:51 +00:00
|
|
|
// prepare key for low-level poly1305.Sum(): r||n
|
|
|
|
var k [32]byte
|
|
|
|
|
|
|
|
// make sure key is masked
|
2015-04-26 11:07:26 +00:00
|
|
|
if !key.masked {
|
|
|
|
maskKey(key)
|
|
|
|
}
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
// fill in nonce, encrypted with AES and key[:16]
|
2015-03-22 18:09:30 +00:00
|
|
|
cipher, err := aes.NewCipher(key.K[:])
|
2015-03-14 18:53:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
cipher.Encrypt(k[16:], nonce[:])
|
|
|
|
|
|
|
|
// copy r
|
2015-03-22 18:09:30 +00:00
|
|
|
copy(k[:16], key.R[:])
|
2015-03-14 18:53:51 +00:00
|
|
|
|
|
|
|
// copy mac to array
|
|
|
|
var m [16]byte
|
|
|
|
copy(m[:], mac)
|
|
|
|
|
|
|
|
return poly1305.Verify(&m, msg, &k)
|
|
|
|
}
|
|
|
|
|
2015-04-12 08:53:46 +00:00
|
|
|
// NewKey returns new encryption and signing keys.
|
|
|
|
func NewKey() (k *Key) {
|
2015-04-12 07:41:47 +00:00
|
|
|
k = &Key{}
|
2015-03-22 18:18:34 +00:00
|
|
|
n, err := rand.Read(k.Encrypt[:])
|
2015-04-12 07:41:47 +00:00
|
|
|
if n != aesKeySize || err != nil {
|
2015-03-14 18:53:51 +00:00
|
|
|
panic("unable to read enough random bytes for encryption key")
|
|
|
|
}
|
|
|
|
|
2015-03-22 18:18:34 +00:00
|
|
|
n, err = rand.Read(k.Sign.K[:])
|
2015-04-12 07:41:47 +00:00
|
|
|
if n != macKeySizeK || err != nil {
|
2015-03-22 18:09:30 +00:00
|
|
|
panic("unable to read enough random bytes for mac encryption key")
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 18:18:34 +00:00
|
|
|
n, err = rand.Read(k.Sign.R[:])
|
2015-04-12 07:41:47 +00:00
|
|
|
if n != macKeySizeR || err != nil {
|
2015-03-22 18:09:30 +00:00
|
|
|
panic("unable to read enough random bytes for mac signing key")
|
|
|
|
}
|
|
|
|
// mask r
|
2015-03-22 18:18:34 +00:00
|
|
|
maskKey(&k.Sign)
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-03-22 18:18:34 +00:00
|
|
|
return k
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 18:51:52 +00:00
|
|
|
func newIV() []byte {
|
|
|
|
iv := make([]byte, ivSize)
|
|
|
|
n, err := rand.Read(iv)
|
2015-03-14 18:53:51 +00:00
|
|
|
if n != ivSize || err != nil {
|
|
|
|
panic("unable to read enough random bytes for iv")
|
|
|
|
}
|
2015-04-15 18:51:52 +00:00
|
|
|
return iv
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 20:03:01 +00:00
|
|
|
type jsonMACKey struct {
|
|
|
|
K []byte `json:"k"`
|
|
|
|
R []byte `json:"r"`
|
|
|
|
}
|
|
|
|
|
2015-04-12 08:53:46 +00:00
|
|
|
func (m *SigningKey) MarshalJSON() ([]byte, error) {
|
2015-03-22 20:03:01 +00:00
|
|
|
return json.Marshal(jsonMACKey{K: m.K[:], R: m.R[:]})
|
|
|
|
}
|
|
|
|
|
2015-04-12 08:53:46 +00:00
|
|
|
func (m *SigningKey) UnmarshalJSON(data []byte) error {
|
2015-03-22 20:03:01 +00:00
|
|
|
j := jsonMACKey{}
|
|
|
|
err := json.Unmarshal(data, &j)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copy(m.K[:], j.K)
|
|
|
|
copy(m.R[:], j.R)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-12 08:53:46 +00:00
|
|
|
func (k *EncryptionKey) MarshalJSON() ([]byte, error) {
|
2015-03-22 20:03:01 +00:00
|
|
|
return json.Marshal(k[:])
|
|
|
|
}
|
|
|
|
|
2015-04-12 08:53:46 +00:00
|
|
|
func (k *EncryptionKey) UnmarshalJSON(data []byte) error {
|
2015-04-12 07:41:47 +00:00
|
|
|
d := make([]byte, aesKeySize)
|
2015-03-22 20:03:01 +00:00
|
|
|
err := json.Unmarshal(data, &d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copy(k[:], d)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-19 12:06:55 +00:00
|
|
|
// ErrInvalidCiphertext is returned when trying to encrypt into the slice that
|
|
|
|
// holds the plaintext.
|
|
|
|
var ErrInvalidCiphertext = errors.New("invalid ciphertext, same slice used for plaintext")
|
|
|
|
|
2015-03-14 18:53:51 +00:00
|
|
|
// Encrypt encrypts and signs data. Stored in ciphertext is IV || Ciphertext ||
|
2015-04-12 18:58:41 +00:00
|
|
|
// MAC. Encrypt returns the new ciphertext slice, which is extended when
|
2015-04-19 11:34:42 +00:00
|
|
|
// necessary. ciphertext and plaintext may not point to (exactly) the same
|
|
|
|
// slice or non-intersecting slices.
|
2015-04-12 18:58:41 +00:00
|
|
|
func Encrypt(ks *Key, ciphertext, plaintext []byte) ([]byte, error) {
|
2015-04-19 11:34:42 +00:00
|
|
|
ciphertext = ciphertext[:cap(ciphertext)]
|
2015-04-19 12:06:55 +00:00
|
|
|
|
|
|
|
// test for same slice, if possible
|
|
|
|
if len(plaintext) > 0 && len(ciphertext) > 0 && &plaintext[0] == &ciphertext[0] {
|
|
|
|
return nil, ErrInvalidCiphertext
|
|
|
|
}
|
|
|
|
|
|
|
|
// extend ciphertext slice if necessary
|
2015-04-19 11:34:42 +00:00
|
|
|
if len(ciphertext) < len(plaintext)+Extension {
|
2015-04-12 18:58:41 +00:00
|
|
|
ext := len(plaintext) + Extension - cap(ciphertext)
|
|
|
|
ciphertext = append(ciphertext, make([]byte, ext)...)
|
2015-04-19 11:34:42 +00:00
|
|
|
ciphertext = ciphertext[:cap(ciphertext)]
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-12 08:53:46 +00:00
|
|
|
iv := newIV()
|
2015-03-14 18:53:51 +00:00
|
|
|
c, err := aes.NewCipher(ks.Encrypt[:])
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to create cipher: %v", err))
|
|
|
|
}
|
|
|
|
|
2015-04-12 18:58:41 +00:00
|
|
|
e := cipher.NewCTR(c, iv[:])
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-04-19 11:34:42 +00:00
|
|
|
e.XORKeyStream(ciphertext[ivSize:], plaintext)
|
2015-04-12 18:58:41 +00:00
|
|
|
copy(ciphertext, iv[:])
|
2015-04-19 11:34:42 +00:00
|
|
|
// truncate to only conver iv and actual ciphertext
|
2015-03-14 18:53:51 +00:00
|
|
|
ciphertext = ciphertext[:ivSize+len(plaintext)]
|
|
|
|
|
2015-04-19 12:56:23 +00:00
|
|
|
mac := poly1305Sign(ciphertext[ivSize:], ciphertext[:ivSize], &ks.Sign)
|
2015-04-19 11:34:42 +00:00
|
|
|
// append the mac tag
|
2015-03-14 18:53:51 +00:00
|
|
|
ciphertext = append(ciphertext, mac...)
|
|
|
|
|
2015-04-12 18:58:41 +00:00
|
|
|
return ciphertext, nil
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 18:09:30 +00:00
|
|
|
// Decrypt verifies and decrypts the ciphertext. Ciphertext must be in the form
|
2015-04-19 11:34:42 +00:00
|
|
|
// IV || Ciphertext || MAC. plaintext and ciphertext may point to (exactly) the
|
|
|
|
// same slice.
|
2015-04-12 07:41:47 +00:00
|
|
|
func Decrypt(ks *Key, plaintext, ciphertext []byte) ([]byte, error) {
|
2015-03-14 18:53:51 +00:00
|
|
|
// check for plausible length
|
|
|
|
if len(ciphertext) < ivSize+macSize {
|
|
|
|
panic("trying to decrypt invalid data: ciphertext too small")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cap(plaintext) < len(ciphertext) {
|
|
|
|
// extend plaintext
|
|
|
|
plaintext = append(plaintext, make([]byte, len(ciphertext)-cap(plaintext))...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract mac
|
|
|
|
l := len(ciphertext) - macSize
|
|
|
|
ciphertext, mac := ciphertext[:l], ciphertext[l:]
|
|
|
|
|
|
|
|
// verify mac
|
2015-04-19 12:56:23 +00:00
|
|
|
if !poly1305Verify(ciphertext[ivSize:], ciphertext[:ivSize], &ks.Sign, mac) {
|
2015-03-14 18:53:51 +00:00
|
|
|
return nil, ErrUnauthenticated
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract iv
|
|
|
|
iv, ciphertext := ciphertext[:ivSize], ciphertext[ivSize:]
|
|
|
|
|
|
|
|
// decrypt data
|
|
|
|
c, err := aes.NewCipher(ks.Encrypt[:])
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to create cipher: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt
|
|
|
|
e := cipher.NewCTR(c, iv)
|
|
|
|
plaintext = plaintext[:len(ciphertext)]
|
|
|
|
e.XORKeyStream(plaintext, ciphertext)
|
|
|
|
|
|
|
|
return plaintext, nil
|
|
|
|
}
|
|
|
|
|
2015-04-12 07:36:14 +00:00
|
|
|
// KDF derives encryption and signing keys from the password using the supplied
|
|
|
|
// parameters N, R and P and the Salt.
|
2015-04-12 07:41:47 +00:00
|
|
|
func KDF(N, R, P int, salt []byte, password string) (*Key, error) {
|
2015-04-12 07:36:14 +00:00
|
|
|
if len(salt) == 0 {
|
2015-03-14 18:53:51 +00:00
|
|
|
return nil, fmt.Errorf("scrypt() called with empty salt")
|
|
|
|
}
|
|
|
|
|
2015-04-12 07:41:47 +00:00
|
|
|
derKeys := &Key{}
|
2015-03-22 18:18:34 +00:00
|
|
|
|
2015-04-12 07:41:47 +00:00
|
|
|
keybytes := macKeySize + aesKeySize
|
2015-04-12 07:36:14 +00:00
|
|
|
scryptKeys, err := scrypt.Key([]byte(password), salt, N, R, P, keybytes)
|
2015-03-14 18:53:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error deriving keys from password: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(scryptKeys) != keybytes {
|
|
|
|
return nil, fmt.Errorf("invalid numbers of bytes expanded from scrypt(): %d", len(scryptKeys))
|
|
|
|
}
|
|
|
|
|
2015-03-22 18:09:30 +00:00
|
|
|
// first 32 byte of scrypt output is the encryption key
|
2015-04-12 07:41:47 +00:00
|
|
|
copy(derKeys.Encrypt[:], scryptKeys[:aesKeySize])
|
2015-03-14 18:53:51 +00:00
|
|
|
|
2015-03-22 18:09:30 +00:00
|
|
|
// next 32 byte of scrypt output is the mac key, in the form k||r
|
2015-04-12 07:41:47 +00:00
|
|
|
macKeyFromSlice(&derKeys.Sign, scryptKeys[aesKeySize:])
|
2015-03-22 18:18:34 +00:00
|
|
|
|
|
|
|
return derKeys, nil
|
2015-03-14 18:53:51 +00:00
|
|
|
}
|