Skip to content

Commit

Permalink
PT-2347 Use KDF instead of hash
Browse files Browse the repository at this point in the history
  • Loading branch information
artemgavrilov committed Dec 17, 2024
1 parent 754c406 commit 78f2030
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/go/pt-secure-collect/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,56 @@ import (

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/hkdf"
)

var (
hkdfInfo = []byte("Percona Toolkit")
salt = [256]byte{
0x33, 0xc5, 0xc5, 0x5f, 0x3e, 0x81, 0xf6, 0x8d, 0x51, 0xd8, 0x18, 0xb9, 0xb7, 0x09, 0x70, 0x51,
0xc3, 0x60, 0x66, 0xef, 0xd4, 0x97, 0x2e, 0xdf, 0x11, 0x59, 0x34, 0x94, 0x47, 0xab, 0xd4, 0x44,
0xac, 0x2e, 0x89, 0x81, 0x85, 0xd5, 0x83, 0xbd, 0x2d, 0xb5, 0x43, 0xdd, 0xd7, 0x6c, 0x1b, 0xa7,
0x6d, 0x8f, 0xb7, 0x84, 0x33, 0x1a, 0x5e, 0x31, 0x76, 0x4b, 0x5d, 0x3b, 0x98, 0x38, 0xe3, 0x93,
0x28, 0xcc, 0x91, 0x30, 0x37, 0xb6, 0x06, 0xd2, 0xab, 0xdc, 0x9a, 0x14, 0xab, 0xb7, 0xd2, 0x90,
0xd6, 0x1f, 0x1c, 0xc1, 0x74, 0x2c, 0xc2, 0x08, 0x27, 0x68, 0x7a, 0xdd, 0xef, 0x6e, 0xb9, 0x59,
0xb0, 0x2f, 0x76, 0x36, 0xba, 0xd8, 0x55, 0x22, 0xba, 0x95, 0xd6, 0xfa, 0xa1, 0x69, 0xab, 0x95,
0x94, 0x1f, 0x61, 0x7f, 0x56, 0x34, 0xaa, 0x17, 0x78, 0xf8, 0xbb, 0xa0, 0x4e, 0x61, 0xad, 0xee,
0x51, 0x1c, 0x42, 0xa7, 0x07, 0x44, 0xd5, 0xa6, 0x16, 0x72, 0x1b, 0x05, 0x4e, 0xd4, 0xbf, 0x32,
0x7d, 0xec, 0x8d, 0x4b, 0x19, 0xd2, 0x32, 0x50, 0x3d, 0x1f, 0x2a, 0x51, 0xc9, 0x62, 0x22, 0x75,
0xb5, 0xde, 0x0d, 0x58, 0x2b, 0x3a, 0xf8, 0x0e, 0xfc, 0x43, 0x07, 0xc3, 0x60, 0x65, 0x83, 0x3d,
0xa9, 0x84, 0x02, 0x3a, 0x13, 0x85, 0x3e, 0x8d, 0x87, 0x04, 0xee, 0x58, 0x8d, 0x9a, 0xbc, 0xc0,
0xec, 0xdd, 0x92, 0xf3, 0x96, 0x03, 0x86, 0xbe, 0x51, 0xb8, 0x96, 0xd5, 0x38, 0xed, 0x03, 0x7b,
0x18, 0x77, 0x26, 0xaf, 0x15, 0x94, 0x60, 0x25, 0x1b, 0x3f, 0x57, 0xa7, 0xe4, 0xe6, 0x63, 0xf8,
0xe2, 0xca, 0x15, 0x3d, 0xa3, 0xee, 0xcb, 0xef, 0xb3, 0x23, 0x41, 0x54, 0xd4, 0xaa, 0x93, 0xb4,
0x48, 0xd3, 0x97, 0x7c, 0x39, 0x5e, 0x1d, 0x05, 0x93, 0x34, 0x70, 0xdf, 0xd6, 0xf0, 0x30, 0xc7,
}
)

func encryptorCmd(opts *cliOptions) (err error) {
password := sha256.Sum256([]byte(*opts.EncryptPassword))
hkdf := hkdf.New(sha256.New, []byte(*opts.EncryptPassword), salt[:], hkdfInfo)
key := make([]byte, 32)
if _, err := io.ReadFull(hkdf, key); err != nil {
return errors.Wrap(err, "Cannot derive key from password")
}

switch opts.Command {
case "decrypt":
if *opts.DecryptOutFile == "" && strings.HasSuffix(*opts.DecryptInFile, ".aes") {
*opts.DecryptOutFile = strings.TrimSuffix(filepath.Base(*opts.DecryptInFile), ".aes")
}
log.Infof("Decrypting file %q into %q", *opts.DecryptInFile, *opts.DecryptOutFile)
err = decrypt(*opts.DecryptInFile, *opts.DecryptOutFile, password)
err = decrypt(*opts.DecryptInFile, *opts.DecryptOutFile, key)
case "encrypt":
if *opts.EncryptOutFile == "" {
*opts.EncryptOutFile = filepath.Base(*opts.EncryptInFile) + ".aes"
}
log.Infof("Encrypting file %q into %q", *opts.EncryptInFile, *opts.EncryptOutFile)
err = encrypt(*opts.EncryptInFile, *opts.EncryptOutFile, password)
err = encrypt(*opts.EncryptInFile, *opts.EncryptOutFile, key)
}
return
}

func encrypt(infile, outfile string, pass [32]byte) error {
key := pass[:]
func encrypt(infile, outfile string, key []byte) error {
inFile, err := os.Open(infile)
if err != nil {
return errors.Wrapf(err, "Cannot open input file %q", infile)
Expand Down Expand Up @@ -64,8 +90,7 @@ func encrypt(infile, outfile string, pass [32]byte) error {
return nil
}

func decrypt(infile, outfile string, pass [32]byte) error {
key := pass[:]
func decrypt(infile, outfile string, key []byte) error {
inFile, err := os.Open(infile)
if err != nil {
return errors.Wrapf(err, "Cannot open %q for reading", infile)
Expand Down

0 comments on commit 78f2030

Please sign in to comment.