示例#1
0
    async def accept(self) -> bool:
        self.update_state('EXCHANGING_KEYS')

        if not self.call:
            self.call_failed()
            raise RuntimeError('call is not set')

        if isinstance(self.call, types.PhoneCallDiscarded):
            print('Call is already discarded')
            self.call_discarded()
            return False

        await self.get_dhc()
        self.b = randint(2, self.dhc.p - 1)
        self.g_b = pow(self.dhc.g, self.b, self.dhc.p)
        self.g_a_hash = self.call.g_a_hash

        try:
            self.call = (await self.client.send(
                functions.phone.AcceptCall(peer=types.InputPhoneCall(
                    id=self.call_id, access_hash=self.call_access_hash),
                                           g_b=i2b(self.g_b),
                                           protocol=self.get_protocol())
            )).phone_call
        except Exception as e:
            print(e)

            await self.discard_call()

            self.stop()
            self.call_discarded()
            return False

        return True
示例#2
0
    async def call_accepted(self) -> None:
        self.update_state('EXCHANGING_KEYS')

        await self.get_dhc()
        self.g_b = b2i(self.call.g_b)
        self.check_g(self.g_b, self.dhc.p)
        self.auth_key = pow(self.g_b, self.a, self.dhc.p)
        self.key_fingerprint = calc_fingerprint(self.auth_key_bytes)

        self.call = (await self.client.send(functions.phone.ConfirmCall(
            key_fingerprint=self.key_fingerprint,
            # peer=self.call_peer,
            peer=types.InputPhoneCall(id=self.call_id, access_hash=self.call_access_hash),
            g_a=i2b(self.g_a),
            protocol=self.get_protocol(),
        ))).phone_call

        await self._initiate_encrypted_call()
示例#3
0
    async def request(self):
        self.update_state('REQUESTING')

        self.peer = await self.client.resolve_peer(self.user_id)

        await self.get_dhc()
        self.a = randint(2, self.dhc.p - 1)
        self.g_a = pow(self.dhc.g, self.a, self.dhc.p)
        self.g_a_hash = hashlib.sha256(i2b(self.g_a)).digest()

        self.call = (await self.client.send(functions.phone.RequestCall(
            user_id=self.peer,
            random_id=randint(0, 0x7fffffff - 1),
            g_a_hash=self.g_a_hash,
            protocol=self.get_protocol(),
        ))).phone_call

        self.update_state('WAITING')
示例#4
0
def main():

    message = b'hi mom'
    pubkey, privkey = generate_keypair_bytes(bits=1024, e=3)

    # create our forged signature
    h = Hb(message)
    padded = pkcs1v1_5_pad_evil(message)
    padded += b'\x10'
    padded += b'\x00' * 55  # this seems like a good number
    fake_sig = i2b(root3(b2i(padded)))

    if verify_bytes(pubkey, message, fake_sig,
                    unpadf=pkcs1v1_5_unpad_insecure):
        print("Successfully forged signature!")
        print()
        print("Padded data:")
        print(padded)
        print()
        print("Signature:")
        print(b2hs(fake_sig))
    else:
        print("Failed to forge signature :(")
示例#5
0
 def auth_key_bytes(self) -> bytes:
     return i2b(self.auth_key) if self.auth_key is not None else b''
示例#6
0
def generate_keypair_bytes(bits, e=E):

    pubkey, privkey = generate_keypair_num(bits, e)
    return (i2b(pubkey[0]), i2b(pubkey[1])), (i2b(privkey[0]), i2b(privkey[1]))
示例#7
0
def _crypt_bytes(k, msg):

    k = (b2i(k[0]), b2i(k[1]))
    msg = b2i(msg)
    return i2b(_crypt(k, msg))
示例#8
0
from fractions import gcd
from collections import namedtuple
from Crypto.PublicKey.pubkey import getStrongPrime

from mymath import invmod
from helpers import i2b, b2i
from hashlib import sha1

E = 65535

Hb = lambda x: sha1(x).digest()  # hash bytes
Hn = lambda x: b2i(sha1(i2b(x)).digest())  # hash num


def sign_bytes(privkey, message):

    return _crypt_bytes(privkey, Hb(message))


def sign_num(privkey, message):

    return _crypt(privkey, Hn(message))


def verify_bytes(pubkey, message, signature):

    return _crypt_bytes(pubkey, signature) == Hb(message)


def verify_num(pubkey, message, signature):