Пример #1
0
    def _encrypt(self):
        """
        Rebuild the master key from header settings and key-hash list. Encrypt
        the stream start bytes and the out-buffer formatted as hashed block
        stream with padding added as needed.
        """
        # rebuild master key from (possibly) updated header
        self._make_master_key()

        # make hashed block stream
        block_buffer = HashedBlockIO()
        block_buffer.write(self.out_buffer.read())
        # data is buffered in hashed block io, start a new one
        self.out_buffer = io.BytesIO()
        # write start bytes (for successful decrypt check)
        self.out_buffer.write(self.header.StreamStartBytes)
        # append blocked data to out-buffer
        block_buffer.write_block_stream(self.out_buffer)
        block_buffer.close()
        self.out_buffer.seek(0)

        # encrypt the whole thing with header settings and master key
        ciphername = self.header.ciphers.get(self.header.CipherID,
                                             self.header.CipherID)
        if ciphername == 'AES':
            data = pad(self.out_buffer.read())
            self.out_buffer = aes_cbc_encrypt(data, self.master_key,
                                              self.header.EncryptionIV)
        elif ciphername == 'Twofish':
            data = pad(self.out_buffer.read())
            self.out_buffer = twofish_cbc_encrypt(data, self.master_key,
                                                  self.header.EncryptionIV)
        else:
            raise IOError('Unsupported encryption type: %s' %
                          codecs.encode(ciphername, 'hex'))
Пример #2
0
    def _encrypt(self):
        """
        Rebuild the master key from header settings and key-hash list. Encrypt
        the stream start bytes and the out-buffer formatted as hashed block
        stream with padding added as needed.
        """
        # rebuild master key from (possibly) updated header
        self._make_master_key()

        # make hashed block stream
        block_buffer = HashedBlockIO()
        block_buffer.write(self.out_buffer.read())
        # data is buffered in hashed block io, start a new one
        self.out_buffer = io.BytesIO()
        # write start bytes (for successful decrypt check)
        self.out_buffer.write(self.header.StreamStartBytes)
        # append blocked data to out-buffer
        block_buffer.write_block_stream(self.out_buffer)
        block_buffer.close()
        self.out_buffer.seek(0)

        # encrypt the whole thing with header settings and master key
        data = pad(self.out_buffer.read())
        self.out_buffer = aes_cbc_encrypt(data, self.master_key,
                                          self.header.EncryptionIV)
Пример #3
0
 def _encrypt(self):
     """
     Rebuild the master key from header settings and key-hash list. Encrypt
     the stream start bytes and the out-buffer formatted as hashed block
     stream with padding added as needed.
     """
     # rebuild master key from (possibly) updated header
     self._make_master_key()
     
     # make hashed block stream
     block_buffer = HashedBlockIO()
     block_buffer.write(self.out_buffer.read())
     # data is buffered in hashed block io, start a new one
     self.out_buffer = io.BytesIO()
     # write start bytes (for successful decrypt check)
     self.out_buffer.write(self.header.StreamStartBytes)
     # append blocked data to out-buffer
     block_buffer.write_block_stream(self.out_buffer)
     block_buffer.close()
     self.out_buffer.seek(0)
     
     # encrypt the whole thing with header settings and master key
     data = pad(self.out_buffer.read())
     self.out_buffer = aes_cbc_encrypt(data, self.master_key, 
         self.header.EncryptionIV)
Пример #4
0
 def test_pad(self):
     self.assertEquals(pad(b''),
         b'\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10')
     self.assertEquals(pad(b'\xff'),
         b'\xff\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f')
     self.assertEquals(pad(b'\xff\xff'),
         b'\xff\xff\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e')
     self.assertEquals(pad(b'\xff\xff\xff'),
         b'\xff\xff\xff\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d')
     self.assertEquals(pad(b'\xff\xff\xff\xff'),
         b'\xff\xff\xff\xff\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c')
     self.assertEquals(pad(b'\xff\xff\xff\xff\xff'),
         b'\xff\xff\xff\xff\xff\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b')
     self.assertEquals(len(pad(b'\xff')), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff'*0)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff'*1)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff'*2)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff'*15)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff'*16)), 2*AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff'*17)), 2*AES_BLOCK_SIZE)
Пример #5
0
 def test_pad(self):
     self.assertEquals(pad(b''), b'\x10' * 16)
     self.assertEquals(pad(b'\xff'), b'\xff' + b'\x0f' * 15)
     self.assertEquals(pad(b'\xff' * 2), b'\xff' * 2 + b'\x0e' * 14)
     self.assertEquals(pad(b'\xff' * 3), b'\xff' * 3 + b'\x0d' * 13)
     self.assertEquals(pad(b'\xff' * 4), b'\xff' * 4 + b'\x0c' * 12)
     self.assertEquals(pad(b'\xff' * 5), b'\xff' * 5 + b'\x0b' * 11)
     self.assertEquals(len(pad(b'\xff')), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 0)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 1)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 2)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 15)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 16)), 2 * AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 17)), 2 * AES_BLOCK_SIZE)
Пример #6
0
 def encrypt(self, message):
     message = libkeepass_crypto.pad(message)
     encrypted_message = libkeepass_crypto.aes_cbc_encrypt(
         message, self.key, self.iv)
     return base64.b64encode(encrypted_message)
Пример #7
0
 def test_pad(self):
     self.assertEquals(pad(b''), b'\x10' * 16)
     self.assertEquals(pad(b'\xff'), b'\xff' + b'\x0f' * 15)
     self.assertEquals(pad(b'\xff' * 2), b'\xff' * 2 + b'\x0e' * 14)
     self.assertEquals(pad(b'\xff' * 3), b'\xff' * 3 + b'\x0d' * 13)
     self.assertEquals(pad(b'\xff' * 4), b'\xff' * 4 + b'\x0c' * 12)
     self.assertEquals(pad(b'\xff' * 5), b'\xff' * 5 + b'\x0b' * 11)
     self.assertEquals(len(pad(b'\xff')), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 0)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 1)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 2)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 15)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 16)), 2 * AES_BLOCK_SIZE)
     self.assertEquals(len(pad(b'\xff' * 17)), 2 * AES_BLOCK_SIZE)
Пример #8
0
 def encrypt(self, message):
     message = libkeepass_crypto.pad(message)
     encrypted_message = libkeepass_crypto.aes_cbc_encrypt(message, self.key, self.iv)
     return base64.b64encode(encrypted_message)
Пример #9
0
 def test_pad(self):
     self.assertEquals(pad(''),
         '\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10')
     self.assertEquals(pad('\xff'),
         '\xff\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f')
     self.assertEquals(pad('\xff\xff'),
         '\xff\xff\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e')
     self.assertEquals(pad('\xff\xff\xff'),
         '\xff\xff\xff\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d\x0d')
     self.assertEquals(pad('\xff\xff\xff\xff'),
         '\xff\xff\xff\xff\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c')
     self.assertEquals(pad('\xff\xff\xff\xff\xff'),
         '\xff\xff\xff\xff\xff\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b')
     self.assertEquals(len(pad('\xff')), AES_BLOCK_SIZE)
     self.assertEquals(len(pad('\xff'*0)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad('\xff'*1)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad('\xff'*2)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad('\xff'*15)), AES_BLOCK_SIZE)
     self.assertEquals(len(pad('\xff'*16)), 2*AES_BLOCK_SIZE)
     self.assertEquals(len(pad('\xff'*17)), 2*AES_BLOCK_SIZE)