lee
lee

Reputation: 772

Transparent filesystem in pyfuse with encryption and compression

I'm trying to make a simple transparent filesystem in fuse. I'm using this guide as a basis. This code works perfectly. I'm trying to modify it so that it compresses and then encrypts as well.

So far I have:

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

AES_KEY = b"thisisaverysecretkey1234"
AES_IV = b"thisisaverysecre"

def encrypt_data(self, data):
    cipher = AES.new(AES_KEY, AES.MODE_CBC, AES_IV)
    return cipher.encrypt(pad(data, AES.block_size))

def decrypt_data(self, data):
    cipher = AES.new(AES_KEY, AES.MODE_CBC, AES_IV)
    return unpad(cipher.decrypt(data), AES.block_size)

And I modified the read and write methods like so:

    def read(self, path, size, offset, fh):
        full_path = self._get_file_path(path)
        os.lseek(fh, offset, os.SEEK_SET)
        encrypted_data = os.read(fh, size)

        # Decrypt the data before returning it
        return decrypt_data(encrypted_data)

    def write(self, path, buf, offset, fh):
        full_path = self._get_file_path(path)
        os.lseek(fh, offset, os.SEEK_SET)

        # Encrypt data before writing it
        encrypted_data = self._encrypt_data(buf)

        return os.write(encrypted_data)

However, I keep getting the error:

fuse: wrote too many bytes

I can't seem to write encrypted files properly (I haven't even compressed/decompressed before applying encryption).

This is just a side project to test out my own compression/encryption scheme, and I'm using aes and zlib as placeholders.

Upvotes: 0

Views: 47

Answers (1)

Oren Kishon
Oren Kishon

Reputation: 767

The meaning of the message is that the number of bytes actually written is larger than the number of bytes the caller intended to write. And it makes sense because the buffer you actually write (encrypted_data) is different (in content and size, I guess) than the buffer given to fuse (buf).

You need to return the size of buf and not the return value of os.write.

This requires you as well, in the output of getattr to modify the size field of the file to the size as if the whole file is decoded.

Another issue is reading the file starting from some offset. You need to adjust the offset to be in "encrypted blocks" count.

Upvotes: 0

Related Questions