You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.8 KiB

  1. package xsalsa20symmetric
  2. import (
  3. "errors"
  4. "fmt"
  5. "golang.org/x/crypto/nacl/secretbox"
  6. "github.com/tendermint/tendermint/crypto"
  7. cmn "github.com/tendermint/tendermint/libs/common"
  8. )
  9. // TODO, make this into a struct that implements crypto.Symmetric.
  10. const nonceLen = 24
  11. const secretLen = 32
  12. // secret must be 32 bytes long. Use something like Sha256(Bcrypt(passphrase))
  13. // The ciphertext is (secretbox.Overhead + 24) bytes longer than the plaintext.
  14. func EncryptSymmetric(plaintext []byte, secret []byte) (ciphertext []byte) {
  15. if len(secret) != secretLen {
  16. cmn.PanicSanity(fmt.Sprintf("Secret must be 32 bytes long, got len %v", len(secret)))
  17. }
  18. nonce := crypto.CRandBytes(nonceLen)
  19. nonceArr := [nonceLen]byte{}
  20. copy(nonceArr[:], nonce)
  21. secretArr := [secretLen]byte{}
  22. copy(secretArr[:], secret)
  23. ciphertext = make([]byte, nonceLen+secretbox.Overhead+len(plaintext))
  24. copy(ciphertext, nonce)
  25. secretbox.Seal(ciphertext[nonceLen:nonceLen], plaintext, &nonceArr, &secretArr)
  26. return ciphertext
  27. }
  28. // secret must be 32 bytes long. Use something like Sha256(Bcrypt(passphrase))
  29. // The ciphertext is (secretbox.Overhead + 24) bytes longer than the plaintext.
  30. func DecryptSymmetric(ciphertext []byte, secret []byte) (plaintext []byte, err error) {
  31. if len(secret) != secretLen {
  32. cmn.PanicSanity(fmt.Sprintf("Secret must be 32 bytes long, got len %v", len(secret)))
  33. }
  34. if len(ciphertext) <= secretbox.Overhead+nonceLen {
  35. return nil, errors.New("Ciphertext is too short")
  36. }
  37. nonce := ciphertext[:nonceLen]
  38. nonceArr := [nonceLen]byte{}
  39. copy(nonceArr[:], nonce)
  40. secretArr := [secretLen]byte{}
  41. copy(secretArr[:], secret)
  42. plaintext = make([]byte, len(ciphertext)-nonceLen-secretbox.Overhead)
  43. _, ok := secretbox.Open(plaintext[:0], ciphertext[nonceLen:], &nonceArr, &secretArr)
  44. if !ok {
  45. return nil, errors.New("Ciphertext decryption failed")
  46. }
  47. return plaintext, nil
  48. }