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.

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