def _decrypt(self, stream): """ Build the master key from header settings and key-hash list. Start reading from `stream` after the header and decrypt all the data. Remove padding as needed and feed into hashed block reader, set as in-buffer. """ super(KDB4File, self)._decrypt(stream) ciphername = self.header.ciphers.get(self.header.CipherID, self.header.CipherID) if ciphername == 'AES': data = aes_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) elif ciphername == 'Twofish': data = twofish_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) else: raise IOError('Unsupported decryption type: %s' % codecs.encode(ciphername, 'hex')) length = len(self.header.StreamStartBytes) if self.header.StreamStartBytes == data[:length]: # skip startbytes and wrap data in a hashed block io self.in_buffer = HashedBlockIO(initial_bytes=data[length:]) # set successful decryption flag self.opened = True else: raise IOError('Master key invalid.')
def test_twofish_cbc_decrypt(self): self.assertEqual(twofish_cbc_encrypt(b'datamustbe16byte', sha256(b'b'), b'ivmustbe16bytesl'), b'\xd4^&`\xe1j\xfd{\xeb\x04{\x90f\xed\xebi') self.assertEqual(twofish_cbc_encrypt(b'datamustbe16byte', sha256(b'c'), b'ivmustbe16bytesl'), b'\xb3\x86\xbe\x0ficZW\x92P\xeb\x17\xa8\xa8\xac\xe8') self.assertEqual(twofish_cbc_decrypt(b'K\x07q\xad\xd0\x8d\xb9\x0f\x15\xef\x87\x089\xc3\x83\x9c', sha256(b'd'), b'ivmustbe16bytesl'), b'datamustbe16byte')
def _decrypt(self, stream): super(KDB3File, self)._decrypt(stream) if self.header.encryption_flags[self.header.Flags-1] == 'AES': data = aes_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) elif self.header.encryption_flags[self.header.Flags-1] == 'Twofish': data = twofish_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) else: raise IOError('Unsupported encryption type: %s'%self.header.encryption_flags.get(self.header['Flags']-1, self.header['Flags']-1)) if self.header.ContentHash == sha256(data): # put data in bytes io self.in_buffer = io.BytesIO(data) # set successful decryption flag self.opened = True else: raise IOError('Master key invalid.')