Browse Source

Remove unused xsalsa20symmetric package. (#7691)

This package has no uses within Tendermint core.
PR https://github.com/cosmos/cosmos-sdk/pull/11027 replaces the use in
the Cosmos SDK.

A follow-up to #7689 and #7690.
pull/7699/head
M. J. Fromberger 3 years ago
committed by GitHub
parent
commit
fcfe157f6b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 0 additions and 94 deletions
  1. +0
    -54
      crypto/xsalsa20symmetric/symmetric.go
  2. +0
    -40
      crypto/xsalsa20symmetric/symmetric_test.go

+ 0
- 54
crypto/xsalsa20symmetric/symmetric.go View File

@ -1,54 +0,0 @@
package xsalsa20symmetric
import (
"errors"
"fmt"
"golang.org/x/crypto/nacl/secretbox"
"github.com/tendermint/tendermint/crypto"
)
// TODO, make this into a struct that implements crypto.Symmetric.
const nonceLen = 24
const secretLen = 32
// secret must be 32 bytes long. Use something like Sha256(Bcrypt(passphrase))
// The ciphertext is (secretbox.Overhead + 24) bytes longer than the plaintext.
func EncryptSymmetric(plaintext []byte, secret []byte) (ciphertext []byte) {
if len(secret) != secretLen {
panic(fmt.Sprintf("Secret must be 32 bytes long, got len %v", len(secret)))
}
nonce := crypto.CRandBytes(nonceLen)
nonceArr := [nonceLen]byte{}
copy(nonceArr[:], nonce)
secretArr := [secretLen]byte{}
copy(secretArr[:], secret)
ciphertext = make([]byte, nonceLen+secretbox.Overhead+len(plaintext))
copy(ciphertext, nonce)
secretbox.Seal(ciphertext[nonceLen:nonceLen], plaintext, &nonceArr, &secretArr)
return ciphertext
}
// secret must be 32 bytes long. Use something like Sha256(Bcrypt(passphrase))
// The ciphertext is (secretbox.Overhead + 24) bytes longer than the plaintext.
func DecryptSymmetric(ciphertext []byte, secret []byte) (plaintext []byte, err error) {
if len(secret) != secretLen {
panic(fmt.Sprintf("Secret must be 32 bytes long, got len %v", len(secret)))
}
if len(ciphertext) <= secretbox.Overhead+nonceLen {
return nil, errors.New("ciphertext is too short")
}
nonce := ciphertext[:nonceLen]
nonceArr := [nonceLen]byte{}
copy(nonceArr[:], nonce)
secretArr := [secretLen]byte{}
copy(secretArr[:], secret)
plaintext = make([]byte, len(ciphertext)-nonceLen-secretbox.Overhead)
_, ok := secretbox.Open(plaintext[:0], ciphertext[nonceLen:], &nonceArr, &secretArr)
if !ok {
return nil, errors.New("ciphertext decryption failed")
}
return plaintext, nil
}

+ 0
- 40
crypto/xsalsa20symmetric/symmetric_test.go View File

@ -1,40 +0,0 @@
package xsalsa20symmetric
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/bcrypt"
"github.com/tendermint/tendermint/crypto"
)
func TestSimple(t *testing.T) {
plaintext := []byte("sometext")
secret := []byte("somesecretoflengththirtytwo===32")
ciphertext := EncryptSymmetric(plaintext, secret)
plaintext2, err := DecryptSymmetric(ciphertext, secret)
require.NoError(t, err, "%+v", err)
assert.Equal(t, plaintext, plaintext2)
}
func TestSimpleWithKDF(t *testing.T) {
plaintext := []byte("sometext")
secretPass := []byte("somesecret")
secret, err := bcrypt.GenerateFromPassword(secretPass, 12)
if err != nil {
t.Error(err)
}
secret = crypto.Sha256(secret)
ciphertext := EncryptSymmetric(plaintext, secret)
plaintext2, err := DecryptSymmetric(ciphertext, secret)
require.NoError(t, err, "%+v", err)
assert.Equal(t, plaintext, plaintext2)
}

Loading…
Cancel
Save