Example #1
0
    def _generate_random_integer_interval(self, minimum_value, maximum_value):
        # sets the default minimum number of bits, even if the
        # range is too small
        minimum_number_bits = 32

        # calculates the range of the random numbers
        # to generate
        range = maximum_value - minimum_value

        # converts the range into bits
        range_bits = math.log(range, 2)

        # converts the range into bytes
        range_bytes = colony.ceil_integer(range_bits / 8.0)

        # converts the range into bits, but verifies that there
        # is at least a minimum number of bits
        range_bits = max(range_bytes * 8, minimum_number_bits * 2)

        # generates the random number of bits to be used
        number_bits = random.randint(minimum_number_bits, range_bits)

        # generates the random integer with the number of bits generated
        # and applying modulo of the range
        random_base_value = self._generate_random_integer(number_bits) % range

        # creates the random value adding the minimum value to the
        # random base value
        random_value = random_base_value + minimum_value

        # returns the random value
        return random_value
Example #2
0
    def _encrypt_string(self, message, key, modulus):
        # retrieves the modulus size in bytes
        modulus_size_bytes = colony.ceil_integer(math.log(modulus, 256))

        # converts the message to integer, then uses this integer
        # value to encrypt it using the rsa algorithm and converts
        # the resulting encrypted integer back to a string
        message_integer = self._string_to_integer(message)
        message_integer_encrypted = self._encrypt_integer(message_integer, key, modulus)
        message_encrypted = self._integer_to_string(message_integer_encrypted, modulus_size_bytes)

        # returns the resulting message encrypted using the rsa
        # algorithm for cryptographic purposes
        return message_encrypted
Example #3
0
    def _randomized_primality_testing(self, number, k_value):
        # the property of the jacobi witness function
        q_value = 0.5

        # calculates t to ha
        t_value = colony.ceil_integer(k_value / math.log(1 / q_value, 2))

        # iterates over the range of t value plus one
        for _index in colony.legacy.xrange(t_value + 1):
            # generates a random number in the interval
            random_number = self._generate_random_integer_interval(1, number - 1)

            # in case the random number is a jacobi witness
            # then the number is not prime
            if self._jacobi_witness(random_number, number):
                # returns false (invalid)
                return False

        # returns true (valid)
        return True
Example #4
0
    def _encrypt(self, keys, message):
        # unpacks the keys tuple, retrieving the public key,
        # private key and extras map and then retrieves the
        # modulus from the public key
        public_key, _private_key, _extras = keys
        modulus = public_key["n"]

        # calculates the size of the modulus in terms of bytes
        # this will be used as the size of the message (including
        # the padding)
        modulus_size_bytes = colony.ceil_integer(math.log(modulus, 256))

        # calculates the current size of the message and uses this
        # value to calculate the size of the pad
        message_length = len(message)
        pad_length = modulus_size_bytes - message_length - 3

        # creates the pad with the required size using just lower cased
        # characters to avoid the zero value and then constructs the final
        # padded message that includes the created padding
        pad = "".join(random.choice(string.ascii_lowercase) for _value in colony.legacy.xrange(pad_length))
        message_pad = b"\x00\x02" + colony.legacy.bytes(pad) + b"\x00" + message
        return message_pad
Example #5
0
    def _generate_random_integer(self, number_bits):
        """
        Generates a random integer of approximately the
        size of the provided number bits bits rounded up
        to whole bytes.

        @type number_bits: int
        @param number_bits: The number of bits of the generated
        random integer.
        @rtype: int
        @return: The generated random integer.
        """

        # calculates the number of bytes to represent the number
        number_bytes = colony.ceil_integer(number_bits / 8.0)

        # generates a random data string with the specified
        # number of bytes in length
        random_data = os.urandom(number_bytes)

        # converts the random data int an integer
        # vale and returns it to the caller method
        random_integer = self._string_to_integer(random_data)
        return random_integer
Example #6
0
    def _sign(self, keys, hash_algorithm_name, digest_value):
        # retrieves the hash algorithm tuple
        hash_algorithm_tuple = HASH_OBJECT_IDENTIFIERS_TUPLES_MAP[hash_algorithm_name]

        # creates the ber structure
        ber_structure = self.ber_plugin.create_structure({})

        # creates the algorithm value
        algorithm_value = {
            TYPE_VALUE : OBJECT_IDENTIFIER_TYPE,
            VALUE_VALUE : hash_algorithm_tuple
        }

        # creates the argument value
        arguments_value = {
            TYPE_VALUE : NULL_TYPE,
            VALUE_VALUE : None
        }

        # creates the digest algorithm contents (list)
        digest_algorithm_contents = [
            algorithm_value,
            arguments_value
        ]

        # creates the digest algorithm
        digest_algorithm = {
            TYPE_VALUE : {
                TYPE_CONSTRUCTED_VALUE : 1,
                TYPE_NUMBER_VALUE : SEQUENCE_TYPE,
                TYPE_CLASS_VALUE : 0
            },
            VALUE_VALUE : digest_algorithm_contents
        }

        # creates the digest value value
        digest_value_value = {
            TYPE_VALUE : OCTET_STRING_TYPE,
            VALUE_VALUE : digest_value
        }

        # creates the signature value contents (list)
        signature_value_contents = [
            digest_algorithm,
            digest_value_value
        ]

        # creates the signature value
        signature_value = {
            TYPE_VALUE : {
                TYPE_CONSTRUCTED_VALUE : 1,
                TYPE_NUMBER_VALUE : SEQUENCE_TYPE,
                TYPE_CLASS_VALUE : 0
            },
            VALUE_VALUE : signature_value_contents
        }

        # packs the signature value
        signature_value_packed = ber_structure.pack(signature_value)

        # creates the signature buffer
        signature_buffer = colony.StringBuffer()

        # unpacks the keys tuple, retrieving the
        # public key, private key and extras map
        public_key, _private_key, _extras = keys

        # retrieves the modulus
        modulus = public_key["n"]

        # retrieves the signature value packed length
        signature_value_packed_length = len(signature_value_packed)

        # retrieves the modulus size in bytes
        modulus_size_bytes = colony.ceil_integer(math.log(modulus, 256))

        # calculates the padding size (from the modulus size bytes and the signature value packed length)
        padding_size = modulus_size_bytes - (signature_value_packed_length + 3)

        # writes the beginning of the padding value to the signature buffer
        signature_buffer.write(b"\x00")
        signature_buffer.write(b"\x01")

        # creates the padding string value
        padding = b"\xff" * padding_size

        # writes the padding to the signature buffer
        signature_buffer.write(padding)

        # writes the end of padding value to the signature buffer
        signature_buffer.write(b"\x00")

        # writes the signature value packed in the string buffer
        signature_buffer.write(signature_value_packed)

        # retrieves the signature verified from the string buffer
        signature_verified = signature_buffer.get_value()

        # returns the signature verified
        return signature_verified