soarecostin / file-vault

A Laravel package for encrypting and decrypting files of any size
MIT License
185 stars 62 forks source link

[Question] How to make private vaults for per authenticated user? #6

Open rhnkyr opened 4 years ago

rhnkyr commented 4 years ago

Hi @soarecostin,

I would like to know which approach is the best for my question?

Thanks in advance.

Cheers.

leonekmi commented 4 years ago

You may use the key($key) function in the facade to set one encryption key per user. You can ask the user password and use pbkdf2 to generate a safe hash to use as key : https://www.php.net/manual/en/function.hash-pbkdf2.php

laugre commented 4 years ago

@leonekmi but with this technique if the user change his password you have to re-encrypt all the files, no ?

underdpt commented 3 years ago

I would love also to know if with this technique it's required to re-encrypt all files when the user changes its password.

leonekmi commented 3 years ago

Sure, but you can probably create a background task after the user changes his password. Assuming you ask the old one for stealth checks, you can decrypt the data and encrypt it again with a new key (aka the new password).

laugre commented 3 years ago

Right but if the user own big amount of files, that could be a heavy task. Why not use a hash of a user data that won't change, as user uuid for example ;)

leonekmi commented 3 years ago

It kinda defeats the purpose of having an encrypted vault if the uuid or any other "non changing informations" is stored into your database "as is", why encrypt it then? It's only useful then if you don't trust the external provider for your data (S3 for example), but it would be misleading to say to your users that data is encrypted, because if you have the keys available with a simple request as an admin, the data is not really encrypted in a way that would actually be protecting user data.

underdpt commented 3 years ago

What about generating a random key to encrypt the data, and then use pbkdf2 to store that key? That way if the user changes the password, we only have to re-encrypt the stored key again with pdbkdf2, not all the data. Would that be a good aproach (from a security point of view)?

laugre commented 3 years ago

Hi, I come back on that question because it's my dev time to shield my app. After skimming the crypto and security forums about per user encryption, I realised that my first proposition (with user id) was far from reality ^^ @underdpt your is a good choice I think, the one well described there https://security.stackexchange.com/questions/157422/store-encrypted-user-data-in-database The user and only can decrypt his encrypted data.

The only thing blocking for me now is if the user loses his password, he loses the key too, no way... I have to let the user access a forget password process in my app. A solution could be to also pbkdf2 store the initial random key with another user passphrase or question/answer asked at user registration like the password. My app could ask the passphrase to allow user to reset his password if he forgot it and get the encryption key. The encryption key would then be re-encrypted with the new password. Would that be a lack of security ? Any idea ?