2018-10-09 17:00:01 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-10-09 17:00:01 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package madmin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-10-14 17:06:04 -04:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2018-10-09 17:00:01 -04:00
|
|
|
"crypto/rand"
|
2019-10-14 17:06:04 -04:00
|
|
|
"errors"
|
2018-10-09 17:00:01 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2019-10-14 17:06:04 -04:00
|
|
|
"github.com/secure-io/sio-go"
|
2018-10-09 17:00:01 -04:00
|
|
|
"golang.org/x/crypto/argon2"
|
2019-10-14 17:06:04 -04:00
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
|
|
|
"golang.org/x/sys/cpu"
|
2018-10-09 17:00:01 -04:00
|
|
|
)
|
|
|
|
|
2019-10-14 17:06:04 -04:00
|
|
|
// EncryptData encrypts the data with an unique key
|
|
|
|
// derived from password using the Argon2id PBKDF.
|
|
|
|
//
|
|
|
|
// The returned ciphertext data consists of:
|
|
|
|
// salt | AEAD ID | nonce | encrypted data
|
|
|
|
// 32 1 8 ~ len(data)
|
2018-10-09 17:00:01 -04:00
|
|
|
func EncryptData(password string, data []byte) ([]byte, error) {
|
|
|
|
salt := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:06:04 -04:00
|
|
|
// Derive an unique 256 bit key from the password and the random salt.
|
|
|
|
key := argon2.IDKey([]byte(password), salt, 1, 64*1024, 4, 32)
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-10-14 17:06:04 -04:00
|
|
|
// Create a new AEAD instance - either AES-GCM if the CPU provides
|
|
|
|
// an AES hardware implementation or ChaCha20-Poly1305 otherwise.
|
|
|
|
id, cipher, err := newAEAD(key)
|
2018-10-09 17:00:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-14 17:06:04 -04:00
|
|
|
stream := sio.NewStream(cipher, sio.BufSize)
|
|
|
|
|
|
|
|
nonce := make([]byte, stream.NonceSize())
|
|
|
|
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ciphertext = salt || AEAD ID | nonce | encrypted data
|
|
|
|
cLen := int64(len(salt)+1+len(nonce)+len(data)) + stream.Overhead(int64(len(data)))
|
|
|
|
ciphertext := bytes.NewBuffer(make([]byte, 0, cLen)) // pre-alloc correct length
|
|
|
|
|
|
|
|
// Prefix the ciphertext with salt, AEAD ID and nonce
|
|
|
|
ciphertext.Write(salt)
|
|
|
|
ciphertext.WriteByte(id)
|
|
|
|
ciphertext.Write(nonce)
|
|
|
|
|
|
|
|
w := stream.EncryptWriter(ciphertext, nonce, nil)
|
|
|
|
if _, err = w.Write(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = w.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ciphertext.Bytes(), nil
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2019-10-14 17:06:04 -04:00
|
|
|
// DecryptData decrypts the data with the key derived
|
|
|
|
// from the salt (part of data) and the password using
|
|
|
|
// the PBKDF used in EncryptData. DecryptData returns
|
|
|
|
// the decrypted plaintext on success.
|
|
|
|
//
|
|
|
|
// The data must be a valid ciphertext produced by
|
|
|
|
// EncryptData. Otherwise, the decryption will fail.
|
2018-10-09 17:00:01 -04:00
|
|
|
func DecryptData(password string, data io.Reader) ([]byte, error) {
|
2019-10-14 17:06:04 -04:00
|
|
|
var (
|
|
|
|
salt [32]byte
|
|
|
|
id [1]byte
|
|
|
|
nonce [8]byte // This depends on the AEAD but both used ciphers have the same nonce length.
|
|
|
|
)
|
|
|
|
|
|
|
|
if _, err := io.ReadFull(data, salt[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := io.ReadFull(data, id[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := io.ReadFull(data, nonce[:]); err != nil {
|
2018-10-09 17:00:01 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:06:04 -04:00
|
|
|
key := argon2.IDKey([]byte(password), salt[:], 1, 64*1024, 4, 32)
|
|
|
|
cipher, err := parseAEAD(id[0], key)
|
2018-10-09 17:00:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-14 17:06:04 -04:00
|
|
|
stream := sio.NewStream(cipher, sio.BufSize)
|
|
|
|
return ioutil.ReadAll(stream.DecryptReader(data, nonce[:], nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
aesGcm = 0x00
|
|
|
|
c20p1305 = 0x01
|
|
|
|
)
|
|
|
|
|
|
|
|
// newAEAD creates a new AEAD instance from the given key.
|
|
|
|
// If the CPU provides an AES hardware implementation it
|
|
|
|
// returns an AES-GCM instance. Otherwise it returns a
|
|
|
|
// ChaCha20-Poly1305 instance.
|
|
|
|
//
|
|
|
|
// newAEAD also returns a byte as algorithm ID indicating
|
|
|
|
// which AEAD scheme it has selected.
|
|
|
|
func newAEAD(key []byte) (byte, cipher.AEAD, error) {
|
|
|
|
if (cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ) || cpu.S390X.HasAES || cpu.PPC64.IsPOWER8 || cpu.ARM64.HasAES {
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
c, err := cipher.NewGCM(block)
|
|
|
|
return aesGcm, c, err
|
|
|
|
}
|
|
|
|
c, err := chacha20poly1305.New(key)
|
|
|
|
return c20p1305, c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseAEAD creates a new AEAD instance from the id and key.
|
|
|
|
// The id must be either 0 (AES-GCM) or 1 (ChaCha20-Poly1305).
|
|
|
|
func parseAEAD(id byte, key []byte) (cipher.AEAD, error) {
|
|
|
|
switch id {
|
|
|
|
case aesGcm:
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cipher.NewGCM(block)
|
|
|
|
case c20p1305:
|
|
|
|
return chacha20poly1305.New(key)
|
|
|
|
default:
|
|
|
|
return nil, errors.New("madmin: invalid AEAD algorithm ID")
|
|
|
|
}
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|