package crypto
import (
"crypto/aes"
"github.com/go-faster/errors"
"github.com/gotd/ige"
"github.com/gotd/td/bin"
)
func (c Cipher ) DecryptFromBuffer (k AuthKey , buf *bin .Buffer ) (*EncryptedMessageData , error ) {
msg := &EncryptedMessage {}
if err := msg .DecodeWithoutCopy (buf ); err != nil {
return nil , err
}
return c .Decrypt (k , msg )
}
func (c Cipher ) Decrypt (k AuthKey , encrypted *EncryptedMessage ) (*EncryptedMessageData , error ) {
plaintext , err := c .decryptMessage (k , encrypted )
if err != nil {
return nil , err
}
side := c .encryptSide .DecryptSide ()
msgKey := MessageKey (k .Value , plaintext , side )
if msgKey != encrypted .MsgKey {
return nil , errors .New ("msg_key is invalid" )
}
msg := &EncryptedMessageData {}
if err := msg .DecodeWithoutCopy (&bin .Buffer {Buf : plaintext }); err != nil {
return nil , err
}
{
const maxPadding = 1024
n := int (msg .MessageDataLen )
paddingLen := len (msg .MessageDataWithPadding ) - n
switch {
case n < 0 :
return nil , errors .Errorf ("message length is invalid: %d less than zero" , n )
case n %4 != 0 :
return nil , errors .Errorf ("message length is invalid: %d is not divisible by 4" , n )
case paddingLen > maxPadding :
return nil , errors .Errorf ("padding %d of message is too big" , paddingLen )
}
}
return msg , nil
}
func (c Cipher ) decryptMessage (k AuthKey , encrypted *EncryptedMessage ) ([]byte , error ) {
if k .ID != encrypted .AuthKeyID {
return nil , errors .New ("unknown auth key id" )
}
if len (encrypted .EncryptedData )%16 != 0 {
return nil , errors .New ("invalid encrypted data padding" )
}
key , iv := Keys (k .Value , encrypted .MsgKey , c .encryptSide .DecryptSide ())
cipher , err := aes .NewCipher (key [:])
if err != nil {
return nil , err
}
plaintext := make ([]byte , len (encrypted .EncryptedData ))
ige .DecryptBlocks (cipher , iv [:], plaintext , encrypted .EncryptedData )
return plaintext , nil
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .