2023-09-27 21:59:09 +00:00
|
|
|
package nip44
|
|
|
|
|
|
|
|
import (
|
2023-09-30 12:40:52 +00:00
|
|
|
"bytes"
|
2023-09-27 21:59:09 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
2024-04-24 20:35:27 +00:00
|
|
|
"encoding/hex"
|
2023-09-27 21:59:09 +00:00
|
|
|
"errors"
|
2023-12-22 16:39:34 +00:00
|
|
|
"fmt"
|
2023-09-27 21:59:09 +00:00
|
|
|
"io"
|
|
|
|
"math"
|
2024-04-24 20:35:27 +00:00
|
|
|
"math/big"
|
2023-09-27 21:59:09 +00:00
|
|
|
|
2023-09-30 17:40:06 +00:00
|
|
|
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
2023-09-27 21:59:09 +00:00
|
|
|
"golang.org/x/crypto/chacha20"
|
|
|
|
"golang.org/x/crypto/hkdf"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-12-22 13:22:49 +00:00
|
|
|
MinPlaintextSize = 0x0001 // 1b msg => padded to 32b
|
|
|
|
MaxPlaintextSize = 0xffff // 65535 (64kb-1) => padded to 64kb
|
2023-09-27 21:59:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type EncryptOptions struct {
|
|
|
|
Salt []byte
|
|
|
|
Version int
|
|
|
|
}
|
|
|
|
|
2023-09-30 17:40:06 +00:00
|
|
|
func Encrypt(conversationKey []byte, plaintext string, options *EncryptOptions) (string, error) {
|
2023-09-27 21:59:09 +00:00
|
|
|
var (
|
2023-09-30 12:00:53 +00:00
|
|
|
version int = 2
|
2023-09-27 21:59:09 +00:00
|
|
|
salt []byte
|
|
|
|
enc []byte
|
|
|
|
nonce []byte
|
|
|
|
auth []byte
|
|
|
|
padded []byte
|
|
|
|
ciphertext []byte
|
|
|
|
hmac_ []byte
|
|
|
|
concat []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if options.Version != 0 {
|
|
|
|
version = options.Version
|
|
|
|
}
|
|
|
|
if options.Salt != nil {
|
|
|
|
salt = options.Salt
|
|
|
|
} else {
|
|
|
|
if salt, err = randomBytes(32); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 12:00:53 +00:00
|
|
|
if version != 2 {
|
2023-12-22 16:39:34 +00:00
|
|
|
return "", errors.New(fmt.Sprintf("unknown version %d", version))
|
2023-09-27 21:59:09 +00:00
|
|
|
}
|
|
|
|
if len(salt) != 32 {
|
|
|
|
return "", errors.New("salt must be 32 bytes")
|
|
|
|
}
|
2023-09-30 17:40:06 +00:00
|
|
|
if enc, nonce, auth, err = messageKeys(conversationKey, salt); err != nil {
|
2023-09-27 21:59:09 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if padded, err = pad(plaintext); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-09-30 14:04:43 +00:00
|
|
|
if ciphertext, err = chacha20_(enc, nonce, []byte(padded)); err != nil {
|
2023-09-27 21:59:09 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2023-12-22 17:06:19 +00:00
|
|
|
if hmac_, err = sha256Hmac(auth, ciphertext, salt); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-09-30 12:00:53 +00:00
|
|
|
concat = append(concat, []byte{byte(version)}...)
|
2023-09-27 21:59:09 +00:00
|
|
|
concat = append(concat, salt...)
|
|
|
|
concat = append(concat, ciphertext...)
|
|
|
|
concat = append(concat, hmac_...)
|
2023-09-30 12:00:53 +00:00
|
|
|
return base64.StdEncoding.EncodeToString(concat), nil
|
2023-09-27 21:59:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 17:40:06 +00:00
|
|
|
func Decrypt(conversationKey []byte, ciphertext string) (string, error) {
|
2023-09-30 12:40:52 +00:00
|
|
|
var (
|
|
|
|
version int = 2
|
|
|
|
decoded []byte
|
2023-12-22 16:39:34 +00:00
|
|
|
cLen int
|
2023-09-30 12:40:52 +00:00
|
|
|
dLen int
|
|
|
|
salt []byte
|
|
|
|
ciphertext_ []byte
|
2023-12-22 17:06:19 +00:00
|
|
|
hmac []byte
|
2023-09-30 12:40:52 +00:00
|
|
|
hmac_ []byte
|
|
|
|
enc []byte
|
|
|
|
nonce []byte
|
|
|
|
auth []byte
|
|
|
|
padded []byte
|
|
|
|
unpaddedLen uint16
|
|
|
|
unpadded []byte
|
|
|
|
err error
|
|
|
|
)
|
2023-12-22 16:39:34 +00:00
|
|
|
cLen = len(ciphertext)
|
|
|
|
if cLen < 132 || cLen > 87472 {
|
|
|
|
return "", errors.New(fmt.Sprintf("invalid payload length: %d", cLen))
|
|
|
|
}
|
2023-09-30 12:40:52 +00:00
|
|
|
if ciphertext[0:1] == "#" {
|
|
|
|
return "", errors.New("unknown version")
|
|
|
|
}
|
|
|
|
if decoded, err = base64.StdEncoding.DecodeString(ciphertext); err != nil {
|
2023-09-30 13:18:27 +00:00
|
|
|
return "", errors.New("invalid base64")
|
2023-09-30 12:40:52 +00:00
|
|
|
}
|
|
|
|
if version = int(decoded[0]); version != 2 {
|
2023-12-22 16:39:34 +00:00
|
|
|
return "", errors.New(fmt.Sprintf("unknown version %d", version))
|
2023-09-30 12:40:52 +00:00
|
|
|
}
|
|
|
|
dLen = len(decoded)
|
2023-12-22 16:39:34 +00:00
|
|
|
if dLen < 99 || dLen > 65603 {
|
|
|
|
return "", errors.New(fmt.Sprintf("invalid data length: %d", dLen))
|
|
|
|
}
|
2023-09-30 12:40:52 +00:00
|
|
|
salt, ciphertext_, hmac_ = decoded[1:33], decoded[33:dLen-32], decoded[dLen-32:]
|
2023-09-30 17:40:06 +00:00
|
|
|
if enc, nonce, auth, err = messageKeys(conversationKey, salt); err != nil {
|
2023-09-30 12:40:52 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2023-12-22 17:06:19 +00:00
|
|
|
if hmac, err = sha256Hmac(auth, ciphertext_, salt); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !bytes.Equal(hmac_, hmac) {
|
2023-09-30 12:40:52 +00:00
|
|
|
return "", errors.New("invalid hmac")
|
|
|
|
}
|
2023-09-30 14:04:43 +00:00
|
|
|
if padded, err = chacha20_(enc, nonce, ciphertext_); err != nil {
|
2023-09-30 12:40:52 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
unpaddedLen = binary.BigEndian.Uint16(padded[0:2])
|
2023-12-22 16:51:24 +00:00
|
|
|
if unpaddedLen < uint16(MinPlaintextSize) || unpaddedLen > uint16(MaxPlaintextSize) || len(padded) != 2+calcPadding(int(unpaddedLen)) {
|
2023-12-22 16:39:34 +00:00
|
|
|
return "", errors.New("invalid padding")
|
|
|
|
}
|
2023-09-30 12:40:52 +00:00
|
|
|
unpadded = padded[2 : unpaddedLen+2]
|
2023-12-22 16:51:24 +00:00
|
|
|
if len(unpadded) == 0 || len(unpadded) != int(unpaddedLen) {
|
2023-09-30 12:40:52 +00:00
|
|
|
return "", errors.New("invalid padding")
|
|
|
|
}
|
|
|
|
return string(unpadded), nil
|
|
|
|
}
|
|
|
|
|
2024-04-24 20:35:27 +00:00
|
|
|
func GenerateConversationKey(sendPrivkey []byte, recvPubkey []byte) ([]byte, error) {
|
|
|
|
var (
|
|
|
|
N = secp256k1.S256().N
|
|
|
|
sk *secp256k1.PrivateKey
|
|
|
|
pk *secp256k1.PublicKey
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
// make sure that private key is on curve before using unsafe secp256k1.PrivKeyFromBytes
|
|
|
|
// see https://pkg.go.dev/github.com/decred/dcrd/dcrec/secp256k1/v4#PrivKeyFromBytes
|
|
|
|
skX := new(big.Int).SetBytes(sendPrivkey)
|
|
|
|
if skX.Cmp(big.NewInt(0)) == 0 || skX.Cmp(N) >= 0 {
|
|
|
|
return []byte{}, fmt.Errorf("invalid private key: x coordinate %s is not on the secp256k1 curve", hex.EncodeToString(sendPrivkey))
|
|
|
|
}
|
|
|
|
sk = secp256k1.PrivKeyFromBytes(sendPrivkey)
|
|
|
|
if pk, err = secp256k1.ParsePubKey(recvPubkey); err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
shared := secp256k1.GenerateSharedSecret(sk, pk)
|
|
|
|
return hkdf.Extract(sha256.New, shared, []byte("nip44-v2")), nil
|
2023-09-30 17:40:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 14:04:43 +00:00
|
|
|
func chacha20_(key []byte, nonce []byte, message []byte) ([]byte, error) {
|
2023-09-27 21:59:09 +00:00
|
|
|
var (
|
|
|
|
cipher *chacha20.Cipher
|
|
|
|
dst = make([]byte, len(message))
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if cipher, err = chacha20.NewUnauthenticatedCipher(key, nonce); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cipher.XORKeyStream(dst, message)
|
|
|
|
return dst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomBytes(n int) ([]byte, error) {
|
|
|
|
buf := make([]byte, n)
|
|
|
|
if _, err := rand.Read(buf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2023-12-22 17:06:19 +00:00
|
|
|
func sha256Hmac(key []byte, ciphertext []byte, aad []byte) ([]byte, error) {
|
|
|
|
if len(aad) != 32 {
|
|
|
|
return nil, errors.New("aad data must be 32 bytes")
|
|
|
|
}
|
2023-09-27 21:59:09 +00:00
|
|
|
h := hmac.New(sha256.New, key)
|
2023-12-22 17:06:19 +00:00
|
|
|
h.Write(aad)
|
2023-09-27 21:59:09 +00:00
|
|
|
h.Write(ciphertext)
|
2023-12-22 17:06:19 +00:00
|
|
|
return h.Sum(nil), nil
|
2023-09-27 21:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func messageKeys(conversationKey []byte, salt []byte) ([]byte, []byte, []byte, error) {
|
|
|
|
var (
|
|
|
|
r io.Reader
|
|
|
|
enc []byte = make([]byte, 32)
|
|
|
|
nonce []byte = make([]byte, 12)
|
|
|
|
auth []byte = make([]byte, 32)
|
|
|
|
err error
|
|
|
|
)
|
2023-12-22 17:06:19 +00:00
|
|
|
if len(conversationKey) != 32 {
|
|
|
|
return nil, nil, nil, errors.New("conversation key must be 32 bytes")
|
|
|
|
}
|
|
|
|
if len(salt) != 32 {
|
|
|
|
return nil, nil, nil, errors.New("salt must be 32 bytes")
|
|
|
|
}
|
2023-12-22 07:58:40 +00:00
|
|
|
r = hkdf.Expand(sha256.New, conversationKey, salt)
|
2023-09-27 21:59:09 +00:00
|
|
|
if _, err = io.ReadFull(r, enc); err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
if _, err = io.ReadFull(r, nonce); err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
if _, err = io.ReadFull(r, auth); err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
return enc, nonce, auth, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pad(s string) ([]byte, error) {
|
|
|
|
var (
|
|
|
|
sb []byte
|
|
|
|
sbLen int
|
|
|
|
padding int
|
|
|
|
result []byte
|
|
|
|
)
|
|
|
|
sb = []byte(s)
|
|
|
|
sbLen = len(sb)
|
2023-12-22 13:22:49 +00:00
|
|
|
if sbLen < 1 || sbLen > MaxPlaintextSize {
|
2023-09-27 21:59:09 +00:00
|
|
|
return nil, errors.New("plaintext should be between 1b and 64kB")
|
|
|
|
}
|
|
|
|
padding = calcPadding(sbLen)
|
|
|
|
result = make([]byte, 2)
|
|
|
|
binary.BigEndian.PutUint16(result, uint16(sbLen))
|
|
|
|
result = append(result, sb...)
|
|
|
|
result = append(result, make([]byte, padding-sbLen)...)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func calcPadding(sLen int) int {
|
|
|
|
var (
|
|
|
|
nextPower int
|
|
|
|
chunk int
|
|
|
|
)
|
|
|
|
if sLen <= 32 {
|
|
|
|
return 32
|
|
|
|
}
|
|
|
|
nextPower = 1 << int(math.Floor(math.Log2(float64(sLen-1)))+1)
|
|
|
|
chunk = int(math.Max(32, float64(nextPower/8)))
|
|
|
|
return chunk * int(math.Floor(float64((sLen-1)/chunk))+1)
|
|
|
|
}
|