Files
kopia/cli/command_benchmark_crypto.go
Jarek Kowalski ddd267accc crypto: deprecated crypto algorithms and replaced with better alternatives
New ciphers are using authenticated encryption with associated data
(AEAD) and per-content key derived using HMAC-SHA256:

* AES256-GCM-HMAC-SHA256
* CHACHA20-POLY1305-HMAC-SHA256

They support content IDs of arbitrary length and are quite fast:

On my 2019 MBP:

- BLAKE2B-256 + AES256-GCM-HMAC-SHA256 - 648.7 MiB / second
- BLAKE2B-256 + CHACHA20-POLY1305-HMAC-SHA256 - 597.1 MiB / second
- HMAC-SHA256 + AES256-GCM-HMAC-SHA256 351 MiB / second
- HMAC-SHA256 + CHACHA20-POLY1305-HMAC-SHA256 316.2 MiB / second

Previous ciphers had several subtle issues:

* SALSA20 encryption, used weak nonce (64 bit prefix of content ID),
  which means that for any two contents, whose IDs that have the same
  64-bit prefix, their plaintext can be decoded from the ciphertext
  alone.

* AES-{128,192,256}-CTR were not authenticated, so we were
  required to hash plaintext after decryption to validate. This is not
  recommended due to possibility of subtle timing attacks if an attacker
  controls the ciphertext.

* SALSA20-HMAC was only validating checksum and not that the ciphertext
  was for the correct content ID.

New repositories cannot be created using deprecated ciphers, but they
will still be supported for existing repositories, until at least 0.6.0.

The users are encouraged to migrate to one of new ciphers when 0.5.0 is
out.
2020-02-29 20:50:50 -08:00

87 lines
2.6 KiB
Go

package cli
import (
"sort"
"time"
"github.com/kopia/kopia/internal/units"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/encryption"
"github.com/kopia/kopia/repo/hashing"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
benchmarkCryptoCommand = benchmarkCommands.Command("crypto", "Run hash and encryption benchmarks")
benchmarkCryptoBlockSize = benchmarkCryptoCommand.Flag("block-size", "Size of a block to encrypt").Default("1MB").Bytes()
benchmarkCryptoEncryption = benchmarkCryptoCommand.Flag("encryption", "Test encrypted formats").Default("true").Bool()
benchmarkCryptoRepeat = benchmarkCryptoCommand.Flag("repeat", "Number of repetitions").Default("100").Int()
benchmarkCryptoDeprecatedAlgorithms = benchmarkCryptoCommand.Flag("deprecated", "Include deprecated algorithms").Bool()
)
func runBenchmarkCryptoAction(ctx *kingpin.ParseContext) error {
type benchResult struct {
hash string
encryption string
throughput float64
}
var results []benchResult
data := make([]byte, *benchmarkCryptoBlockSize)
for _, ha := range hashing.SupportedAlgorithms() {
for _, ea := range encryption.SupportedAlgorithms(*benchmarkCryptoDeprecatedAlgorithms) {
isEncrypted := ea != encryption.NoneAlgorithm
if *benchmarkCryptoEncryption != isEncrypted {
continue
}
h, e, err := content.CreateHashAndEncryptor(&content.FormattingOptions{
Encryption: ea,
Hash: ha,
MasterKey: make([]byte, 32),
HMACSecret: make([]byte, 32),
})
if err != nil {
continue
}
printStderr("Benchmarking hash '%v' and encryption '%v'... (%v x %v bytes)\n", ha, ea, *benchmarkCryptoRepeat, len(data))
t0 := time.Now()
hashCount := *benchmarkCryptoRepeat
for i := 0; i < hashCount; i++ {
contentID := h(data)
if _, encerr := e.Encrypt(data, contentID); encerr != nil {
printStderr("encryption failed: %v\n", encerr)
break
}
}
hashTime := time.Since(t0)
bytesPerSecond := float64(len(data)) * float64(hashCount) / hashTime.Seconds()
results = append(results, benchResult{hash: ha, encryption: ea, throughput: bytesPerSecond})
}
}
sort.Slice(results, func(i, j int) bool {
return results[i].throughput > results[j].throughput
})
printStdout(" %-20v %-20v %v\n", "Hash", "Encryption", "Throughput")
printStdout("-----------------------------------------------------------------\n")
for ndx, r := range results {
printStdout("%3d. %-20v %-20v %v / second\n", ndx, r.hash, r.encryption, units.BytesStringBase2(int64(r.throughput)))
}
return nil
}
func init() {
benchmarkCryptoCommand.Action(runBenchmarkCryptoAction)
}