2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-10-09 17:00:01 -04:00
|
|
|
|
|
|
|
package madmin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-04-22 11:45:30 -04:00
|
|
|
"crypto/sha256"
|
2019-10-14 17:06:04 -04:00
|
|
|
"errors"
|
2018-10-09 17:00:01 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2021-04-27 12:49:26 -04:00
|
|
|
"github.com/minio/argon2"
|
2021-04-22 11:45:30 -04:00
|
|
|
"github.com/minio/minio/pkg/fips"
|
2019-10-14 17:06:04 -04:00
|
|
|
"github.com/secure-io/sio-go"
|
2019-10-29 18:36:47 -04:00
|
|
|
"github.com/secure-io/sio-go/sioutil"
|
2021-04-22 11:45:30 -04:00
|
|
|
"golang.org/x/crypto/pbkdf2"
|
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) {
|
2019-10-29 18:36:47 -04:00
|
|
|
salt := sioutil.MustRandom(32)
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-10-29 18:36:47 -04:00
|
|
|
var (
|
|
|
|
id byte
|
|
|
|
err error
|
|
|
|
stream *sio.Stream
|
|
|
|
)
|
2021-04-22 11:45:30 -04:00
|
|
|
if fips.Enabled() {
|
|
|
|
key := pbkdf2.Key([]byte(password), salt, pbkdf2Cost, 32, sha256.New)
|
2019-10-29 18:36:47 -04:00
|
|
|
stream, err = sio.AES_256_GCM.Stream(key)
|
2021-04-22 11:45:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id = pbkdf2AESGCM
|
2019-10-29 18:36:47 -04:00
|
|
|
} else {
|
2021-04-22 11:45:30 -04:00
|
|
|
key := argon2.IDKey([]byte(password), salt, argon2idTime, argon2idMemory, argon2idThreads, 32)
|
|
|
|
if sioutil.NativeAES() {
|
|
|
|
stream, err = sio.AES_256_GCM.Stream(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id = argon2idAESGCM
|
|
|
|
} else {
|
|
|
|
stream, err = sio.ChaCha20Poly1305.Stream(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id = argon2idChaCHa20Poly1305
|
|
|
|
}
|
2019-10-14 17:06:04 -04:00
|
|
|
}
|
2021-04-22 11:45:30 -04:00
|
|
|
|
2019-10-29 18:36:47 -04:00
|
|
|
nonce := sioutil.MustRandom(stream.NonceSize())
|
2019-10-14 17:06:04 -04:00
|
|
|
|
|
|
|
// 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-11-01 18:53:16 -04:00
|
|
|
// ErrMaliciousData indicates that the stream cannot be
|
|
|
|
// decrypted by provided credentials.
|
|
|
|
var ErrMaliciousData = sio.NotAuthentic
|
|
|
|
|
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-29 18:36:47 -04:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
stream *sio.Stream
|
|
|
|
)
|
2021-04-22 11:45:30 -04:00
|
|
|
switch {
|
|
|
|
case id[0] == argon2idAESGCM:
|
|
|
|
key := argon2.IDKey([]byte(password), salt[:], argon2idTime, argon2idMemory, argon2idThreads, 32)
|
2019-10-29 18:36:47 -04:00
|
|
|
stream, err = sio.AES_256_GCM.Stream(key)
|
2021-04-22 11:45:30 -04:00
|
|
|
case id[0] == argon2idChaCHa20Poly1305:
|
|
|
|
key := argon2.IDKey([]byte(password), salt[:], argon2idTime, argon2idMemory, argon2idThreads, 32)
|
2019-10-29 18:36:47 -04:00
|
|
|
stream, err = sio.ChaCha20Poly1305.Stream(key)
|
2021-04-22 11:45:30 -04:00
|
|
|
case id[0] == pbkdf2AESGCM:
|
|
|
|
key := pbkdf2.Key([]byte(password), salt[:], pbkdf2Cost, 32, sha256.New)
|
|
|
|
stream, err = sio.AES_256_GCM.Stream(key)
|
2019-10-29 18:36:47 -04:00
|
|
|
default:
|
2021-04-22 11:45:30 -04:00
|
|
|
err = errors.New("madmin: invalid encryption algorithm ID")
|
2019-10-29 18:36:47 -04:00
|
|
|
}
|
2018-10-09 17:00:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-01 18:53:16 -04:00
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
plaintext, err := ioutil.ReadAll(stream.DecryptReader(data, nonce[:], nil))
|
2019-11-01 18:53:16 -04:00
|
|
|
if err != nil {
|
2021-04-22 11:45:30 -04:00
|
|
|
return nil, err
|
2019-11-01 18:53:16 -04:00
|
|
|
}
|
2021-04-22 11:45:30 -04:00
|
|
|
return plaintext, err
|
2019-10-14 17:06:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-04-22 11:45:30 -04:00
|
|
|
argon2idAESGCM = 0x00
|
|
|
|
argon2idChaCHa20Poly1305 = 0x01
|
|
|
|
pbkdf2AESGCM = 0x02
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
argon2idTime = 1
|
|
|
|
argon2idMemory = 64 * 1024
|
|
|
|
argon2idThreads = 4
|
|
|
|
pbkdf2Cost = 8192
|
2019-10-14 17:06:04 -04:00
|
|
|
)
|