Exemplo n.º 1
0
    def test_pyuecc(self):
        (pub1, pri1) = pyuecc.make_key()
        logging.debug('pub key %s' % pub1)
        logging.debug('pri key %s' % pri1)
        assert len(pub1) == 64
        assert len(pri1) == 64

        (pub2, pri2) = pyuecc.make_key()
        spri1 = pyuecc.shared_secret(pub1, pri2)
        spri2 = pyuecc.shared_secret(pub2, pri1)
        logging.debug('shared key %s' % spri1)
        assert spri1 == spri2

        cpub1 = pyuecc.compress(pub1)
        logging.debug('compressed key %s' % cpub1)
        assert len(cpub1) == 33

        _pub1 = pyuecc.decompress(cpub1)
        logging.debug('decompressed key %s' % _pub1)
        assert _pub1 == pub1

        msg = b'test'
        sig1 = pyuecc.sign(pri1, msg)
        ok = pyuecc.verify(pub1, msg, sig1)
        logging.debug('signature %s' % sig1)
        assert ok

        ok = pyuecc.verify(pub2, msg, sig1)
        assert not ok
Exemplo n.º 2
0
    def encrypt_handshake(self, msg, remote_permanent_public_b64):
        """
        encrypt handshake message structured:

        KEY - 33 bytes, the sender's ephemeral exchange public key in
              compressed format
        IV - 4 bytes, a random but unique value determined by the sender
        INNER - the AES-256-CTR encrypted inner packet ciphertext
        HMAC - 4 bytes, the calculated HMAC of all of the previous
               KEY+IV+INNER bytes

        :param bytes msg: handshake message
        :param bytes remote_permanent_public_b64: remote node pubkey
        :return: encrypted bytes data
        """

        remote_permanent_public = \
            base64.b64decode(remote_permanent_public_b64)
        compressed_pkey = pyuecc.compress(self.remote_ephemeral_public)
        shared_key = get_sha256(
            pyuecc.shared_secret(remote_permanent_public,
                                 self.remote_ephemeral_private)
        )
        iv = pack('I', self.seq)
        self.seq += 1
        aes = AES.new(shared_key, AES.MODE_CTR, counter=IVCounter(iv))
        enc_msg = aes.encrypt(msg.encode())
        hmac_key = pyuecc.shared_secret(remote_permanent_public,
                                        self.mynode.private) + iv
        sig = fold3(hmac.new(hmac_key, enc_msg, hashlib.sha256).digest())
        self.token = fold1(get_sha256(compressed_pkey[0:16]))
        return compressed_pkey + iv + enc_msg + sig