From c71f20d53807024c72e41b3f560b3af4b2ac568e Mon Sep 17 00:00:00 2001 From: dev <> Date: Wed, 24 Jan 2024 12:13:52 +0000 Subject: [PATCH] encryption: Add schacha20blake3 --- encryption_aead/encryption_test.go | 48 ++++++++++++++++++++++++------ go.mod | 2 +- go.sum | 4 +-- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/encryption_aead/encryption_test.go b/encryption_aead/encryption_test.go index 6e15a66..f416fb7 100644 --- a/encryption_aead/encryption_test.go +++ b/encryption_aead/encryption_test.go @@ -6,10 +6,11 @@ import ( "fmt" "testing" - "github.com/bloom42/stdx/crypto/experimental_do_not_use/xchacha20sha256" + "github.com/bloom42/stdx/crypto/schacha20blake3" "github.com/bloom42/stdx/crypto/xchacha12blake3" "github.com/bloom42/stdx/crypto/xchacha20" "github.com/bloom42/stdx/crypto/xchacha20blake3" + "github.com/bloom42/stdx/crypto/xchacha20sha256" "github.com/skerkour/go-benchmarks/utils" "golang.org/x/crypto/chacha20poly1305" ) @@ -41,19 +42,22 @@ func BenchmarkEncryptAEAD(b *testing.B) { chaCha20Key := utils.RandBytes(b, chacha20poly1305.KeySize) chaCha20Nonce := utils.RandBytes(b, chacha20poly1305.NonceSize) + sChaCha20Nonce := utils.RandBytes(b, schacha20blake3.NonceSize) + aes256GcmKey := utils.RandBytes(b, 32) aes256GcmNonce := utils.RandBytes(b, 12) - // aes128GcmKey := utils.RandBytes(b, 16) - // aes128GcmNonce := utils.RandBytes(b, 12) + aes128GcmKey := utils.RandBytes(b, 16) + aes128GcmNonce := utils.RandBytes(b, 12) for _, size := range BENCHMARKS { benchmarkEncrypt(b, size, "XChaCha20_BLAKE3", newXChaCha20Blake3Cipher(b, xChaCha20Key), xChaCha20Nonce, additionalData) - benchmarkEncrypt(b, size, "XChaCha12_BLAKE3", newXChaCha12Blake3Cipher(b, xChaCha20Key), xChaCha20Nonce, additionalData) benchmarkEncrypt(b, size, "XChaCha20_Poly1305", newXChaCha20Poly1305Cipher(b, xChaCha20Key), xChaCha20Nonce, additionalData) benchmarkEncrypt(b, size, "ChaCha20_Poly1305", newChaCha20Poly1305Cipher(b, chaCha20Key), chaCha20Nonce, additionalData) benchmarkEncrypt(b, size, "AES_256_GCM", newAesGcmCipher(b, aes256GcmKey), aes256GcmNonce, additionalData) - // benchmarkEncrypt(b, size, "AES_128_GCM", newAesGcmCipher(b, aes128GcmKey), aes128GcmNonce, additionalData) + benchmarkEncrypt(b, size, "AES_128_GCM", newAesGcmCipher(b, aes128GcmKey), aes128GcmNonce, additionalData) + benchmarkEncrypt(b, size, "SChaCha20_BLAKE3", newSChaCha20Blake3Cipher(b, xChaCha20Key), sChaCha20Nonce, additionalData) + benchmarkEncrypt(b, size, "XChaCha12_BLAKE3", newXChaCha12Blake3Cipher(b, xChaCha20Key), xChaCha20Nonce, additionalData) benchmarkEncrypt(b, size, "XChaCha20_SHA256", newXChaCha20Sha256Cipher(b, xChaCha20Key), xChaCha20Nonce, additionalData) } } @@ -70,16 +74,19 @@ func BenchmarkDecryptAEAD(b *testing.B) { aes256GcmKey := utils.RandBytes(b, 32) aes256GcmNonce := utils.RandBytes(b, 12) - // aes128GcmKey := utils.RandBytes(b, 16) - // aes128GcmNonce := utils.RandBytes(b, 12) + aes128GcmKey := utils.RandBytes(b, 16) + aes128GcmNonce := utils.RandBytes(b, 12) + + sChaCha20Nonce := utils.RandBytes(b, schacha20blake3.NonceSize) for _, size := range BENCHMARKS { benchmarkDecrypt(b, size, "XChaCha20_BLAKE3", newXChaCha20Blake3Cipher(b, xChaCha20Key), xChaCha20Nonce, additionalData) - benchmarkDecrypt(b, size, "XChaCha12_BLAKE3", newXChaCha12Blake3Cipher(b, xChaCha20Key), xChaCha20Nonce, additionalData) benchmarkDecrypt(b, size, "XChaCha20_Poly1305", newXChaCha20Poly1305Cipher(b, xChaCha20Key), xChaCha20Nonce, additionalData) benchmarkDecrypt(b, size, "ChaCha20_Poly1305", newChaCha20Poly1305Cipher(b, chaCha20Key), chaCha20Nonce, additionalData) benchmarkDecrypt(b, size, "AES_256_GCM", newAesGcmCipher(b, aes256GcmKey), aes256GcmNonce, additionalData) - // benchmarkDecrypt(b, size, "AES_128_GCM", newAesGcmCipher(b, aes128GcmKey), aes128GcmNonce, additionalData) + benchmarkDecrypt(b, size, "AES_128_GCM", newAesGcmCipher(b, aes128GcmKey), aes128GcmNonce, additionalData) + benchmarkDecrypt(b, size, "SChaCha20_BLAKE3", newSChaCha20Blake3Cipher(b, xChaCha20Key), sChaCha20Nonce, additionalData) + benchmarkDecrypt(b, size, "XChaCha12_BLAKE3", newXChaCha12Blake3Cipher(b, xChaCha20Key), xChaCha20Nonce, additionalData) benchmarkDecrypt(b, size, "XChaCha20_SHA256", newXChaCha20Sha256Cipher(b, xChaCha20Key), xChaCha20Nonce, additionalData) } } @@ -135,6 +142,29 @@ func (cipher xChaCha20Blake3Cipher) Decrypt(dst, nonce, ciphertext, additionalDa _, _ = cipher.cipher.Open(dst, nonce, ciphertext, additionalData) } +type sChaCha20Blake3Cipher struct { + cipher cipher.AEAD +} + +func newSChaCha20Blake3Cipher(b *testing.B, key []byte) sChaCha20Blake3Cipher { + cipher, err := schacha20blake3.New(key) + if err != nil { + b.Error(err) + } + + return sChaCha20Blake3Cipher{ + cipher: cipher, + } +} + +func (cipher sChaCha20Blake3Cipher) Encrypt(dst, nonce, plaintext, additionalData []byte) []byte { + return cipher.cipher.Seal(dst, nonce, plaintext, additionalData) +} + +func (cipher sChaCha20Blake3Cipher) Decrypt(dst, nonce, ciphertext, additionalData []byte) { + _, _ = cipher.cipher.Open(dst, nonce, ciphertext, additionalData) +} + type xChaCha20Poly1305Cipher struct { cipher cipher.AEAD } diff --git a/go.mod b/go.mod index 2d9aceb..8df576e 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 require ( github.com/DataDog/zstd v1.5.5 github.com/akamensky/base58 v0.0.0-20210829145138-ce8bf8802e8f - github.com/bloom42/stdx v0.0.0-20240121092629-09ed451d9a75 + github.com/bloom42/stdx v0.0.0-20240124120249-bb22673ca845 github.com/cespare/xxhash/v2 v2.2.0 github.com/golang/snappy v0.0.4 github.com/jotfs/fastcdc-go v0.2.0 diff --git a/go.sum b/go.sum index 36a95c6..d6ea256 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ= github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/akamensky/base58 v0.0.0-20210829145138-ce8bf8802e8f h1:z8MkSJCUyTmW5YQlxsMLBlwA7GmjxC7L4ooicxqnhz8= github.com/akamensky/base58 v0.0.0-20210829145138-ce8bf8802e8f/go.mod h1:UdUwYgAXBiL+kLfcqxoQJYkHA/vl937/PbFhZM34aZs= -github.com/bloom42/stdx v0.0.0-20240121092629-09ed451d9a75 h1:06wdAsXTKv/vD2RiTsylrnmZnYNv0bTW49PLEN/Ywtw= -github.com/bloom42/stdx v0.0.0-20240121092629-09ed451d9a75/go.mod h1:zZWdLGQq1BX7GoDuZZPyyE0WUTy5MmNtPB6+8TB6Mf0= +github.com/bloom42/stdx v0.0.0-20240124120249-bb22673ca845 h1:BrWxBn1TvA47JGwouMnTLQ8PO79UBEDCUbtXlw9QuAw= +github.com/bloom42/stdx v0.0.0-20240124120249-bb22673ca845/go.mod h1:zZWdLGQq1BX7GoDuZZPyyE0WUTy5MmNtPB6+8TB6Mf0= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=