Ejemplo n.º 1
0
    def read(self, length):
        """
        Compute the next piece of XOF output.

        .. note::
            You cannot use :meth:`update` anymore after the first call to
            :meth:`read`.

        Args:
            length (integer): the amount of bytes this method must return

        :return: the next piece of XOF output (of the given length)
        :rtype: byte string
        """

        self._is_squeezing = True
        bfr = create_string_buffer(length)
        result = _raw_keccak_lib.keccak_squeeze(self._state.get(),
                                                bfr,
                                                c_size_t(length),
                                                c_ubyte(self._padding))
        if result:
            raise ValueError("Error %d while extracting from SHAKE128"
                             % result)

        return get_raw_buffer(bfr)
Ejemplo n.º 2
0
 def _create_keccak(self):
     state = VoidPointer()
     result = _raw_keccak_lib.keccak_init(state.address_of(),
                                          c_size_t(32),  # 32 bytes of capacity (256 bits)
                                          c_ubyte(12))   # Reduced number of rounds
     if result:
         raise ValueError("Error %d while instantiating KangarooTwelve"
                          % result)
     return SmartPointer(state.get(), _raw_keccak_lib.keccak_destroy)
Ejemplo n.º 3
0
    def _squeeze(self, hash_obj, length, padding):
        bfr = create_string_buffer(length)
        result = _raw_keccak_lib.keccak_squeeze(hash_obj.get(),
                                                bfr,
                                                c_size_t(length),
                                                c_ubyte(padding))
        if result:
            raise ValueError("Error %d while extracting from KangarooTwelve"
                             % result)

        return get_raw_buffer(bfr)
Ejemplo n.º 4
0
    def __init__(self, data=None):
        state = VoidPointer()
        result = _raw_keccak_lib.keccak_init(state.address_of(), c_size_t(64),
                                             c_ubyte(24))
        if result:
            raise ValueError("Error %d while instantiating SHAKE256" % result)
        self._state = SmartPointer(state.get(), _raw_keccak_lib.keccak_destroy)
        self._is_squeezing = False
        self._padding = 0x1F

        if data:
            self.update(data)
Ejemplo n.º 5
0
    def __init__(self, data, update_after_digest):
        self._update_after_digest = update_after_digest
        self._digest_done = False
        self._padding = 0x06

        state = VoidPointer()
        result = _raw_keccak_lib.keccak_init(state.address_of(),
                                             c_size_t(self.digest_size * 2),
                                             c_ubyte(24))
        if result:
            raise ValueError("Error %d while instantiating SHA-3/512" % result)
        self._state = SmartPointer(state.get(), _raw_keccak_lib.keccak_destroy)
        if data:
            self.update(data)
Ejemplo n.º 6
0
    def digest(self):
        """Return the **binary** (non-printable) digest of the message that has been hashed so far.

        :return: The hash digest, computed over the data processed so far.
                 Binary form.
        :rtype: byte string
        """

        self._digest_done = True
        bfr = create_string_buffer(self.digest_size)
        result = _raw_keccak_lib.keccak_digest(self._state.get(),
                                               bfr,
                                               c_size_t(self.digest_size),
                                               c_ubyte(self._padding))
        if result:
            raise ValueError("Error %d while squeezing keccak" % result)

        return get_raw_buffer(bfr)
Ejemplo n.º 7
0
    def __init__(self, data, custom, capacity, function):
        state = VoidPointer()

        if custom or function:
            prefix_unpad = _encode_str(function) + _encode_str(custom)
            prefix = _bytepad(prefix_unpad, (1600 - capacity) // 8)
            self._padding = 0x04
        else:
            prefix = None
            self._padding = 0x1F  # for SHAKE

        result = _raw_keccak_lib.keccak_init(state.address_of(),
                                             c_size_t(capacity // 8),
                                             c_ubyte(24))
        if result:
            raise ValueError("Error %d while instantiating cSHAKE" % result)
        self._state = SmartPointer(state.get(), _raw_keccak_lib.keccak_destroy)
        self._is_squeezing = False

        if prefix:
            self.update(prefix)

        if data:
            self.update(data)