2020-07-07 09:00:04 +00:00
|
|
|
package common
|
2020-07-03 08:02:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/ecdsa"
|
2022-11-17 21:19:17 +00:00
|
|
|
"crypto/rand"
|
2020-07-07 09:00:04 +00:00
|
|
|
"errors"
|
2020-07-03 08:02:28 +00:00
|
|
|
"io"
|
2022-11-17 21:19:17 +00:00
|
|
|
"math/big"
|
2020-07-22 07:41:40 +00:00
|
|
|
|
|
|
|
"golang.org/x/crypto/sha3"
|
|
|
|
|
2021-02-11 17:07:11 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2020-11-18 09:16:51 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2020-07-03 08:02:28 +00:00
|
|
|
)
|
|
|
|
|
2021-02-17 23:14:48 +00:00
|
|
|
const (
|
|
|
|
nonceLength = 12
|
2021-02-11 17:07:11 +00:00
|
|
|
defaultECHDSharedKeyLength = 16
|
2021-02-17 23:14:48 +00:00
|
|
|
defaultECHDMACLength = 16
|
2021-02-11 17:07:11 +00:00
|
|
|
)
|
2020-07-07 09:00:04 +00:00
|
|
|
|
2022-11-17 21:19:17 +00:00
|
|
|
var (
|
|
|
|
ErrInvalidCiphertextLength = errors.New("invalid cyphertext length")
|
|
|
|
|
|
|
|
letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
numberRunes = []rune("0123456789")
|
|
|
|
alphanumericRunes = append(numberRunes, letterRunes...)
|
|
|
|
)
|
2020-07-07 09:00:04 +00:00
|
|
|
|
|
|
|
func HashPublicKey(pk *ecdsa.PublicKey) []byte {
|
|
|
|
return Shake256(crypto.CompressPubkey(pk))
|
2020-07-03 08:02:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
func Decrypt(cyphertext []byte, key []byte) ([]byte, error) {
|
2020-07-03 08:02:28 +00:00
|
|
|
if len(cyphertext) < nonceLength {
|
|
|
|
return nil, ErrInvalidCiphertextLength
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce := cyphertext[:nonceLength]
|
|
|
|
return gcm.Open(nil, nonce, cyphertext[nonceLength:], nil)
|
|
|
|
}
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
func Encrypt(plaintext []byte, key []byte, reader io.Reader) ([]byte, error) {
|
2020-07-03 08:02:28 +00:00
|
|
|
c, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
|
|
if _, err = io.ReadFull(reader, nonce); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gcm.Seal(nonce, nonce, plaintext, nil), nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
func Shake256(buf []byte) []byte {
|
2020-07-03 08:02:28 +00:00
|
|
|
h := make([]byte, 64)
|
|
|
|
sha3.ShakeSum256(h, buf)
|
|
|
|
return h
|
|
|
|
}
|
2020-07-07 09:00:04 +00:00
|
|
|
|
|
|
|
// IsPubKeyEqual checks that two public keys are equal
|
|
|
|
func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {
|
|
|
|
// the curve is always the same, just compare the points
|
|
|
|
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
|
|
|
|
}
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
func PubkeyToHex(key *ecdsa.PublicKey) string {
|
|
|
|
return types.EncodeHex(crypto.FromECDSAPub(key))
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func PubkeyToHexBytes(key *ecdsa.PublicKey) types.HexBytes {
|
|
|
|
return crypto.FromECDSAPub(key)
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
func HexToPubkey(pk string) (*ecdsa.PublicKey, error) {
|
|
|
|
bytes, err := types.DecodeHex(pk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return crypto.UnmarshalPubkey(bytes)
|
|
|
|
}
|
2021-02-11 17:07:11 +00:00
|
|
|
|
|
|
|
func MakeECDHSharedKey(yourPrivateKey *ecdsa.PrivateKey, theirPubKey *ecdsa.PublicKey) ([]byte, error) {
|
|
|
|
return ecies.ImportECDSA(yourPrivateKey).GenerateShared(
|
|
|
|
ecies.ImportECDSAPublic(theirPubKey),
|
|
|
|
defaultECHDSharedKeyLength,
|
|
|
|
defaultECHDMACLength,
|
|
|
|
)
|
|
|
|
}
|
2022-11-17 21:19:17 +00:00
|
|
|
|
|
|
|
func randomString(choice []rune, n int) (string, error) {
|
|
|
|
max := big.NewInt(int64(len(choice)))
|
|
|
|
rr := rand.Reader
|
|
|
|
|
|
|
|
b := make([]rune, n)
|
|
|
|
for i := range b {
|
|
|
|
pos, err := rand.Int(rr, max)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
b[i] = choice[pos.Int64()]
|
|
|
|
}
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RandomAlphabeticalString(n int) (string, error) {
|
|
|
|
return randomString(letterRunes, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RandomAlphanumericString(n int) (string, error) {
|
|
|
|
return randomString(alphanumericRunes, n)
|
|
|
|
}
|