Skip to content

Commit

Permalink
encryption: Add schacha20blake3
Browse files Browse the repository at this point in the history
  • Loading branch information
dev committed Jan 24, 2024
1 parent 952b420 commit c71f20d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
48 changes: 39 additions & 9 deletions encryption_aead/encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit c71f20d

Please sign in to comment.