2023-06-28 10:53:46 +00:00
|
|
|
package encryption
|
|
|
|
|
|
|
|
import (
|
2023-10-12 15:45:23 +00:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2023-06-28 10:53:46 +00:00
|
|
|
"time"
|
2023-10-12 15:45:23 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto/ecies"
|
2023-06-28 10:53:46 +00:00
|
|
|
)
|
|
|
|
|
2023-10-12 15:45:23 +00:00
|
|
|
const keyBumpValue = uint64(10)
|
2023-06-28 10:53:46 +00:00
|
|
|
|
2023-10-12 15:45:23 +00:00
|
|
|
// GetCurrentTime64 returns the current unix time in milliseconds
|
|
|
|
func GetCurrentTime() uint64 {
|
|
|
|
return (uint64)(time.Now().UnixNano() / int64(time.Millisecond))
|
2023-06-28 10:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// bumpKeyID takes a timestampID and returns its value incremented by the keyBumpValue
|
2023-10-12 15:45:23 +00:00
|
|
|
func bumpKeyID(timestampID uint64) uint64 {
|
2023-06-28 10:53:46 +00:00
|
|
|
return timestampID + keyBumpValue
|
|
|
|
}
|
2023-10-12 15:45:23 +00:00
|
|
|
|
|
|
|
func generateHashRatchetKeyID(groupID []byte, timestamp uint64, keyBytes []byte) []byte {
|
|
|
|
var keyMaterial []byte
|
|
|
|
|
|
|
|
keyMaterial = append(keyMaterial, groupID...)
|
|
|
|
|
|
|
|
timestampBytes := make([]byte, 8) // 8 bytes for a uint64
|
|
|
|
binary.LittleEndian.PutUint64(timestampBytes, timestamp)
|
|
|
|
keyMaterial = append(keyMaterial, timestampBytes...)
|
|
|
|
|
|
|
|
keyMaterial = append(keyMaterial, keyBytes...)
|
|
|
|
|
|
|
|
return crypto.Keccak256(keyMaterial)
|
|
|
|
}
|
|
|
|
|
|
|
|
func publicKeyMostRelevantBytes(key *ecdsa.PublicKey) uint32 {
|
|
|
|
|
|
|
|
keyBytes := crypto.FromECDSAPub(key)
|
|
|
|
|
|
|
|
return binary.LittleEndian.Uint32(keyBytes[1:5])
|
|
|
|
}
|
|
|
|
|
|
|
|
func encrypt(plaintext []byte, key []byte, reader io.Reader) ([]byte, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSharedKey(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) ([]byte, error) {
|
|
|
|
|
|
|
|
const encryptedPayloadKeyLength = 16
|
|
|
|
|
|
|
|
return ecies.ImportECDSA(privateKey).GenerateShared(
|
|
|
|
ecies.ImportECDSAPublic(publicKey),
|
|
|
|
encryptedPayloadKeyLength,
|
|
|
|
encryptedPayloadKeyLength,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
func buildGroupRekeyMessage(privateKey *ecdsa.PrivateKey, groupID []byte, timestamp uint64, keyMaterial []byte, keys []*ecdsa.PublicKey) (*RekeyGroup, error) {
|
2023-10-12 15:45:23 +00:00
|
|
|
|
|
|
|
message := &RekeyGroup{
|
|
|
|
Timestamp: timestamp,
|
|
|
|
}
|
|
|
|
|
|
|
|
message.Keys = make(map[uint32][]byte)
|
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
for _, k := range keys {
|
2023-10-12 15:45:23 +00:00
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
sharedKey, err := generateSharedKey(privateKey, k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-10-12 15:45:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
encryptedKey, err := encrypt(keyMaterial, sharedKey, rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-12 15:45:23 +00:00
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
kBytes := publicKeyMostRelevantBytes(k)
|
2023-10-12 15:45:23 +00:00
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
if message.Keys[kBytes] == nil {
|
|
|
|
message.Keys[kBytes] = encryptedKey
|
|
|
|
} else {
|
|
|
|
message.Keys[kBytes] = append(message.Keys[kBytes], encryptedKey...)
|
2023-10-12 15:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
return message, nil
|
2023-10-12 15:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const nonceLength = 12
|
|
|
|
|
|
|
|
func decrypt(cyphertext []byte, key []byte) ([]byte, error) {
|
|
|
|
if len(cyphertext) < nonceLength {
|
|
|
|
return nil, errors.New("invalid cyphertext length")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
const keySize = 60
|
|
|
|
|
|
|
|
func decryptGroupRekeyMessage(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, message *RekeyGroup) ([]byte, error) {
|
|
|
|
kBytes := publicKeyMostRelevantBytes(&privateKey.PublicKey)
|
|
|
|
if message.Keys == nil || message.Keys[kBytes] == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sharedKey, err := generateSharedKey(privateKey, publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := message.Keys[kBytes]
|
|
|
|
|
|
|
|
nKeys := len(keys) / keySize
|
|
|
|
|
|
|
|
var decryptedKey []byte
|
|
|
|
for i := 0; i < nKeys; i++ {
|
|
|
|
|
|
|
|
encryptedKey := keys[i*keySize : i*keySize+keySize]
|
|
|
|
decryptedKey, err = decrypt(encryptedKey, sharedKey)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return decryptedKey, nil
|
|
|
|
}
|