Esempio n. 1
0
    def update(self, data, ed):
        """Processes the given ciphertext/plaintext

        Inputs:
            data: raw string of any length
            ed:   'e' for encryption, 'd' for decryption
        Output:
            hashed data as raw string

        This is not really an update function:
        Everytime the function is called, the hash from the input data is calculated.
        No finalizing needed.
        """
        assert ed == 'e'
        blocksize = self.blocksize

        m = (len(data) + blocksize - 1) / blocksize  #m = amount of datablocks
        i = 0
        for i in range(1, m):
            self.IV = self.codebook.encrypt(
                util.xorstring(data[(i - 1) * blocksize:(i) * blocksize],
                               self.IV))

        if len(data[(i) * blocksize:]) == blocksize:
            X = util.xorstring(util.xorstring(data[(i) * blocksize:], self.IV),
                               self.Lu)
        else:
            tmp = data[(i) *
                       blocksize:] + '\x80' + '\x00' * (blocksize - len(data[
                           (i) * blocksize:]) - 1)
            X = util.xorstring(util.xorstring(tmp, self.IV), self.Lu2)

        T = self.codebook.encrypt(X)
        return T
Esempio n. 2
0
 def __xts_step(self, ed, tocrypt, T):
     T_string = util.number2string_N(T, 16)[::-1]
     # C = E_K1(P xor T) xor T
     if ed == 'd':
         return util.xorstring(
             T_string,
             self.codebook1.decrypt(util.xorstring(T_string, tocrypt)))
     else:
         return util.xorstring(
             T_string,
             self.codebook1.encrypt(util.xorstring(T_string, tocrypt)))
Esempio n. 3
0
    def update(self, data, ed):
        """Processes the given ciphertext/plaintext

        Inputs:
            data: raw string of any length
            ed:   'e' for encryption, 'd' for decryption
        Output:
            processed raw string block(s), if any

        When the supplied data is not a multiple of the blocksize
          of the cipher, then the remaining input data will be cached.
        The next time the update function is called with some data,
          the new data will be concatenated to the cache and then
          cache+data will be processed and full blocks will be outputted.
        """
        if ed == 'e':
            encrypted_blocks = ''
            self.cache += data
            if len(self.cache) < self.blocksize:
                return ''
            for i in xrange(0,
                            len(self.cache) - self.blocksize + 1,
                            self.blocksize):
                self.IV = self.codebook.encrypt(
                    util.xorstring(self.cache[i:i + self.blocksize], self.IV))
                encrypted_blocks += self.IV
            self.cache = self.cache[i + self.blocksize:]
            return encrypted_blocks
        else:
            decrypted_blocks = ''
            self.cache += data
            if len(self.cache) < self.blocksize:
                return ''
            for i in xrange(0,
                            len(self.cache) - self.blocksize + 1,
                            self.blocksize):
                plaintext = util.xorstring(
                    self.IV,
                    self.codebook.decrypt(self.cache[i:i + self.blocksize]))
                self.IV = self.cache[i:i + self.blocksize]
                decrypted_blocks += plaintext
            self.cache = self.cache[i + self.blocksize:]
            return decrypted_blocks