7a04f298d2
- update to latest telegram layer - remove some references to fields in tg.Entities that don't exist in the schema - originally added here: https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e - referenced here - https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3 - https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
package crypto
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/sha1" // #nosec
|
|
"io"
|
|
|
|
"github.com/go-faster/errors"
|
|
"github.com/gotd/ige"
|
|
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
|
|
)
|
|
|
|
// DecryptExchangeAnswer decrypts messages created during key exchange.
|
|
func DecryptExchangeAnswer(data, key, iv []byte) (dst []byte, err error) {
|
|
// Decrypting inner data.
|
|
cipher, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "create aes cipher")
|
|
}
|
|
|
|
dataWithHash := make([]byte, len(data))
|
|
// Checking length. Invalid length will lead to panic in CryptBlocks.
|
|
if len(dataWithHash)%cipher.BlockSize() != 0 {
|
|
return nil, errors.Errorf("invalid len of data_with_hash (%d %% 16 != 0)", len(dataWithHash))
|
|
}
|
|
ige.DecryptBlocks(cipher, iv, dataWithHash, data)
|
|
|
|
dst = GuessDataWithHash(dataWithHash)
|
|
if data == nil {
|
|
// Most common cause of this error is invalid crypto implementation,
|
|
// i.e. invalid keys are used to decrypt payload which lead to
|
|
// decrypt failure, so data does not match sha1 with any padding.
|
|
return nil, errors.New("guess data from data_with_hash")
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// EncryptExchangeAnswer encrypts messages created during key exchange.
|
|
func EncryptExchangeAnswer(rand io.Reader, answer, key, iv []byte) (dst []byte, err error) {
|
|
cipher, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "create aes cipher")
|
|
}
|
|
|
|
answerWithHash, err := DataWithHash(answer, rand)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "get answer with hash")
|
|
}
|
|
|
|
dst = make([]byte, len(answerWithHash))
|
|
ige.EncryptBlocks(cipher, iv, dst, answerWithHash)
|
|
return
|
|
}
|
|
|
|
// NonceHash1 computes nonce_hash_1.
|
|
// See https://core.telegram.org/mtproto/auth_key#dh-key-exchange-complete.
|
|
func NonceHash1(newNonce bin.Int256, key Key) (r bin.Int128) {
|
|
var buf []byte
|
|
buf = append(buf, newNonce[:]...)
|
|
buf = append(buf, 1)
|
|
buf = append(buf, sha(key[:])[0:8]...)
|
|
buf = sha(buf)[4:20]
|
|
copy(r[:], buf)
|
|
return
|
|
}
|
|
|
|
func sha(v []byte) []byte {
|
|
h := sha1.Sum(v) // #nosec
|
|
return h[:]
|
|
}
|