Esempio n. 1
0
    def __init__(self, prime=None, bites=GENERAL.PRIME_BITES, quick=False):
        """
        :param prime: prime number
        :type prime: Integer, Long or None
        :param int bites: number of bites for randome prime number generator
        :param bool quick: True for quick setup

        If :param args: is not set will be generare a prime number of
        :param bites: bites.
        """

        if not prime:
            if not isinstance(bites, int):
                raise TypeError("Parameter bites should be integer !")
            self._prime = self.get_prime(bites)

        else:
            if not sutil.primality_test(prime):
                raise ValueError("Parameter prime should be a prime number !")
            self._prime = prime

        sutil.debug("[i] Prime number is: {}".format(self._prime))

        if quick:
            self._primitive_root = self.quick_setup()
        else:
            self._primitive_root = self.setup()
Esempio n. 2
0
    def __init__(self, prime=None, bites=GENERAL.PRIME_BITES, quick=False):
        """
        :param prime: prime number
        :type prime: Integer, Long or None
        :param int bites: number of bites for randome prime number generator
        :param bool quick: True for quick setup

        If :param args: is not set will be generare a prime number of
        :param bites: bites.
        """

        if not prime:
            if not isinstance(bites, int):
                raise TypeError("Parameter bites should be integer !")
            self._prime = self.get_prime(bites)

        else:
            if not sutil.primality_test(prime):
                raise ValueError("Parameter prime should be a prime number !")
            self._prime = prime

        sutil.debug("[i] Prime number is: {}".format(self._prime))

        if quick:
            self._primitive_root = self.quick_setup()
        else:
            self._primitive_root = self.setup()
Esempio n. 3
0
    def _request_handler(self, connection):
        """Process information from request and send response to client"""
        if not self.handshake(connection):
            print("[x] Invalid handshake !")
            return False

        if not self.authentificate(connection):
            print("[x] Unknown user !")
            return False

        sutil.debug("[o] Authentification complete.")
        self.comunicate(connection)
Esempio n. 4
0
    def _request_handler(self, connection):
        """Process information from request and send response to client"""
        if not self.handshake(connection):
            print("[x] Invalid handshake !")
            return False

        if not self.authentificate(connection):
            print("[x] Unknown user !")
            return False

        sutil.debug("[o] Authentification complete.")
        self.comunicate(connection)
Esempio n. 5
0
    def setup(self):
        """
        Will choose a generator that will generates the *entire*
        multiplicative group modulo :param prime:
        """
        divisors = self.divisors(self._prime - 1)

        while True:
            root_test = 1
            random_number = self.process_number()

            sutil.debug("[i] Random number is: {}".format(random_number))
            sutil.debug("[i] Will check if number is primitive root !")

            for divisor in divisors:
                if sutil.primality_test(divisor):
                    root_test = pow(random_number, (self._prime - 1) / divisor,
                                    self._prime)
                else:
                    root_test = pow(random_number, divisor, self._prime)

                if root_test == 1:
                    sutil.debug("[x] Number is not generator !")
                    break

            if root_test != 1:
                sutil.debug("[o] Number is generator !")
                break

        return random_number
Esempio n. 6
0
    def setup(self):
        """
        Will choose a generator that will generates the *entire*
        multiplicative group modulo :param prime:
        """
        divisors = self.divisors(self._prime - 1)

        while True:
            root_test = 1
            random_number = self.process_number()

            sutil.debug("[i] Random number is: {}".format(random_number))
            sutil.debug("[i] Will check if number is primitive root !")

            for divisor in divisors:
                if sutil.primality_test(divisor):
                    root_test = pow(random_number, (self._prime - 1) / divisor,
                                    self._prime)
                else:
                    root_test = pow(random_number, divisor, self._prime)

                if root_test == 1:
                    sutil.debug("[x] Number is not generator !")
                    break

            if root_test != 1:
                sutil.debug("[o] Number is generator !")
                break

        return random_number
Esempio n. 7
0
 def run(self):
     """Start process information received from client"""
     while self.online:
         try:
             # Waiting for new connection to handle
             connection, client_address = self._socket.accept()
             self._sts.clear()
             self._sts["address"] = client_address
             sutil.debug("[i] New client: {}".format(client_address))
         except IOError as exc:
             error_code, _ = exc.args
             if error_code == errno.EINTR:
                 # This call did not succeed because it was interrupted.
                 # However, if you try again, it will probably work.
                 continue
             sutil.debug(exc)
         else:
             self._request_handler(connection)    # Communicate with client
             connection.close()                   # Close current connection
Esempio n. 8
0
 def run(self):
     """Start process information received from client"""
     while self.online:
         try:
             # Waiting for new connection to handle
             connection, client_address = self._socket.accept()
             self._sts.clear()
             self._sts["address"] = client_address
             sutil.debug("[i] New client: {}".format(client_address))
         except IOError as exc:
             error_code, _ = exc.args
             if error_code == errno.EINTR:
                 # This call did not succeed because it was interrupted.
                 # However, if you try again, it will probably work.
                 continue
             sutil.debug(exc)
         else:
             self._request_handler(connection)  # Communicate with client
             connection.close()  # Close current connection
Esempio n. 9
0
    def divisors(number):
        """Compute the prime divisors for recived number"""
        divisors = []

        while number > 1:

            if sutil.primality_test(number):
                sutil.debug("[i] Last divisor: {}".format(number))
                divisors.append(number)
                break

            if GENERAL.DIVISORS == "brent_rho":
                divisor = sutil.brent_rho(number)
            elif GENERAL.DIVISORS == "pollard_rho":
                divisor = sutil.pollard_rho(number)
            else:
                raise ValueError("Invalid value for GENERAL.DIVISORS !")

            divisors.append(divisor)
            sutil.debug("[i] Divisor found: {}".format(divisor))
            number = number / divisor

        return divisors
Esempio n. 10
0
    def divisors(number):
        """Compute the prime divisors for recived number"""
        divisors = []

        while number > 1:

            if sutil.primality_test(number):
                sutil.debug("[i] Last divisor: {}".format(number))
                divisors.append(number)
                break

            if GENERAL.DIVISORS == "brent_rho":
                divisor = sutil.brent_rho(number)
            elif GENERAL.DIVISORS == "pollard_rho":
                divisor = sutil.pollard_rho(number)
            else:
                raise ValueError("Invalid value for GENERAL.DIVISORS !")

            divisors.append(divisor)
            sutil.debug("[i] Divisor found: {}".format(divisor))
            number = number / divisor

        return divisors
Esempio n. 11
0
    def authentificate(self, connection):
        """Will validate client identity"""

        sutil.debug("[i] Authentification - Send exponent")
        # Compute pow(genrator, random_value) and send to client
        connection.sendall(self._auth_step_one())

        sutil.debug("[i] Authentification - Check client response")

        # Get response for step one and process it
        message = sutil.read_data(connection)
        if not self._check_step_one(message):
            return False

        # User is valid - send confirmation
        sutil.debug("[i] Authentification complete - Send confirmation")
        connection.sendall(self._auth_step_two())
        return True
Esempio n. 12
0
    def authentificate(self, connection):
        """Will validate client identity"""

        sutil.debug("[i] Authentification - Send exponent")
        # Compute pow(genrator, random_value) and send to client
        connection.sendall(self._auth_step_one())

        sutil.debug("[i] Authentification - Check client response")

        # Get response for step one and process it
        message = sutil.read_data(connection)
        if not self._check_step_one(message):
            return False

        # User is valid - send confirmation
        sutil.debug("[i] Authentification complete - Send confirmation")
        connection.sendall(self._auth_step_two())
        return True
Esempio n. 13
0
    def run(self):
        """Start client"""
        while True:
            try:
                sutil.debug("[i] Try to connect to: {}".format(self._address))
                self._socket.connect(self._address)
            except IOError as exc:
                error_code, _ = exc.args
                if error_code == errno.EINTR:
                    # This call did not succeed because it was interrupted.
                    # However, if you try again, it will probably work.
                    continue
                sutil.debug(exc)
            else:
                sutil.debug("[o] Successfully connected !")
                self._request_handler()  # Communicate with client
                # self._socket.close()          # Close current connection

            break
Esempio n. 14
0
    def run(self):
        """Start client"""
        while True:
            try:
                sutil.debug("[i] Try to connect to: {}".format(self._address))
                self._socket.connect(self._address)
            except IOError as exc:
                error_code, _ = exc.args
                if error_code == errno.EINTR:
                    # This call did not succeed because it was interrupted.
                    # However, if you try again, it will probably work.
                    continue
                sutil.debug(exc)
            else:
                sutil.debug("[o] Successfully connected !")
                self._request_handler()         # Communicate with client
                # self._socket.close()          # Close current connection

            break
Esempio n. 15
0
    def authentificate(self):
        """Will validate server identity"""
        # Compute the secret
        self._auth_step_one()

        # Get exponent from server
        sutil.debug("[i] Get Server Secret !")
        self._sts["s_secret"] = sutil.read_data(self._socket)

        # Send authentification data
        sutil.debug("[i] Send authentification step two information !")
        self._socket.sendall(self._auth_step_two())

        # Wait for server response
        sutil.debug("[i] Waiting for server response !")
        message = sutil.read_data(self._socket)

        if not self._check_auth(message):
            return False

        return True
Esempio n. 16
0
    def authentificate(self):
        """Will validate server identity"""
        # Compute the secret
        self._auth_step_one()

        # Get exponent from server
        sutil.debug("[i] Get Server Secret !")
        self._sts["s_secret"] = sutil.read_data(self._socket)

        # Send authentification data
        sutil.debug("[i] Send authentification step two information !")
        self._socket.sendall(self._auth_step_two())

        # Wait for server response
        sutil.debug("[i] Waiting for server response !")
        message = sutil.read_data(self._socket)

        if not self._check_auth(message):
            return False

        return True
Esempio n. 17
0
    def handshake(self):
        """Exchange cryptographic keys whit user"""

        try:
            # Recive information from server
            sutil.debug("[i] Waiting for Public Information")
            message = sutil.read_data(self._socket)
            message = json.loads(message)
            for key in self.data:
                if not key in message:
                    raise ValueError("Missing {} field !".format(key))
                self.data[key] = message[key]
        except ValueError as exc:
            print("[x] {}".format(exc))
            return False

        except TypeError:
            print("[x] Invalid information recived !")
            return False

        except Exception as exc:
            print("[x] {}".format(exc))
            return False

        # Send Public key
        sutil.debug("[i] Send public key to server !")
        self._socket.sendall(self._private_key.public_key)

        try:
            # Will check if recived message is a RSA Public Key
            sutil.debug("[i] Check if recived message is a RSA Public Key")
            self.data["public_key"] = crypto.PublicKey(self.data["public_key"])
        except ValueError as exc:
            print("[x] {}".format(exc))
            return False

        return True
Esempio n. 18
0
    def handshake(self):
        """Exchange cryptographic keys whit user"""

        try:
            # Recive information from server
            sutil.debug("[i] Waiting for Public Information")
            message = sutil.read_data(self._socket)
            message = json.loads(message)
            for key in self.data:
                if not key in message:
                    raise ValueError("Missing {} field !".format(key))
                self.data[key] = message[key]
        except ValueError as exc:
            print("[x] {}".format(exc))
            return False

        except TypeError:
            print("[x] Invalid information recived !")
            return False

        except Exception as exc:
            print("[x] {}".format(exc))
            return False

        # Send Public key
        sutil.debug("[i] Send public key to server !")
        self._socket.sendall(self._private_key.public_key)

        try:
            # Will check if recived message is a RSA Public Key
            sutil.debug("[i] Check if recived message is a RSA Public Key")
            self.data["public_key"] = crypto.PublicKey(self.data["public_key"])
        except ValueError as exc:
            print("[x] {}".format(exc))
            return False

        return True
Esempio n. 19
0
    def handshake(self, connection):
        """Exchange cryptographic keys whit user"""

        # Send public information - Public key | Prime number | Generator
        sutil.debug("[i] Send public information to client !")
        connection.sendall(json.dumps(self._public_data))

        try:
            # Recive information from client
            sutil.debug("[i] Waiting for client RSA Public Key")
            message = sutil.read_data(connection)
        except ValueError as exc:
            print("[x] {}".format(exc))
            return False

        try:
            # Will check if recived message is a RSA Public Key
            sutil.debug("[i] Check if recived message is a RSA Public Key")
            self._sts["public_key"] = crypto.PublicKey(message)
        except ValueError as exc:
            print("[x] {}".format(exc))
            return False

        return True
Esempio n. 20
0
    def handshake(self, connection):
        """Exchange cryptographic keys whit user"""

        # Send public information - Public key | Prime number | Generator
        sutil.debug("[i] Send public information to client !")
        connection.sendall(json.dumps(self._public_data))

        try:
            # Recive information from client
            sutil.debug("[i] Waiting for client RSA Public Key")
            message = sutil.read_data(connection)
        except ValueError as exc:
            print("[x] {}".format(exc))
            return False

        try:
            # Will check if recived message is a RSA Public Key
            sutil.debug("[i] Check if recived message is a RSA Public Key")
            self._sts["public_key"] = crypto.PublicKey(message)
        except ValueError as exc:
            print("[x] {}".format(exc))
            return False

        return True