Nutshell

Thoughts come and go, words stay eternal

23 Sep 2022

[encryption] Keep Your Data Safe

Abstract

  1. System’s built-in full disk encryption should be always the first choice, windows - BitLocker, macos - FileVault, linux - LUKS.
  2. VeraCrypt should be the first choice for drive encryption software, when no built-in full disk encryption is supported, or utilizing the system’s built-in drive encryption has several restrictions.
  3. 7-zip should be your first choice for file encryption software.
  4. Cryptomator is worth to use when the requirement is a transparent encryption about cloud storage, like GDrive/OneDrive etc.
  5. Encfs is a light way to satisify with the requirements about transparent encrypted data under a directory, it can use as a transparent encryption for cloud storage.
  6. ECryptfs should not be the first choice in all the encryption scenarios, it’s not user friendly.
  7. git-crypt is a transparent encryption tool that compitatible with git protocol, it use PGP to encrypte data.
  8. The most crucial thing is to keep your recovery key and password secure.

Encrypted Software

Software Platform Mount Type Algorithm Open Source PBA
BitLocker windows Y drive aes N Y
FileVault macos Y drive aes N Y
LUKS linux Y drive aes(v1),argon2(v2) Y Y
VeraCrypt all Y drive, volume aes Y Y
Encfs all Y filesystem aes Y N
Cryptomator all Y filesystem aes Y N
7-zip all N directory,file aes Y N
git-crypt all N directory,file aes-256-ctr Y N

more details: archlinux - Data Encryption Comparison Table

BitLocker - Drive Encryption

1. Introduction

BitLocker uses AES-XTS (default) to encrypte drive. When BitLocker uses with TPM, it’s not possible to decrypte the encrypted disk in other computer with password, but you can use Recovery Key to solve this.

2. Usage

Follow the step show on the GUI to encrypte the disk. make sure the recovery key has been safely backup.

3. Reference
  1. microsoft - bitlocker-device-encryption-overview-windows-10
  2. wiki - BitLocker

FileVault - Drive Encryption

1. Introduction

FileVault

Mac computers offer FileVault, a built-in encryption capability, to secure all data at rest. FileVault uses the AES-XTS data encryption algorithm to protect full volumes on internal and removable storage devices.

2. Usage

Follow the GUI.

3. Reference
  1. Intro to FileVault
  2. Volume encryption with FileVault in macOS
  3. Data Protection classes

LUKS - Drive Encryption

1. Introduction

LUKS can be use to encrypt full disk, inlcude boot partition, it supported resize whe used with LVM or RAID.

LUKS1 uses PBKDF2 (default), so the max avalible length of password is 64 bytes, base on rfc2898 ?

LUKS2 uses Argon2 (default).

LUKS done en- and decryption by hijacking the data path between filesystem and block device, which was done by dm-crypt.

Data Path:

  • Without LUKS: data -> filesystem -> block device
  • With LUKS: data -> filesystem -> device mapper (dm-crypt) -> block device
2. Usage

When install linux system, it’s easy to follow the GUI guard to configurate disk encryption.

archlinux - LUKS Encrypting_an_entire_system

3. Reference
  1. wiki - LUKS
  2. archlinux - LUKS Encrypting_an_entire_system
  3. archlinux - Resizing_LVM-on-LUKS
  4. archlinux - LUKS Encrypt_an_existing_unencrypted_file_system
  5. archlinux - Dm-crypt

VeraCrypt - Drive Encryption

1. Introduction

VeraCrypt is a free open source disk encryption software for Windows, Mac OSX and Linux. Brought to you by IDRIX (https://www.idrix.fr) and based on TrueCrypt 7.1a.

2. Usage

Follow the step show on the GUI.

3. Reference
  1. veracrypt - official website
  2. veracrypt -

EncFS

1. Introduction

EncFS - mounts or creates an encrypted virtual filesystem, it uses the FUSE kernel driver and library as a backend.

EncFS hijacking the data path between virtual filesystem and filesystem, the en- and decryption was done between virtual filesystem and filesystem.

Both file’s content and name were encrypted.

Then encrypted data that store in system filesystem has the same directory structure with the decrypted data show in the EncFS provided virtual filesystem, but the file name was encrypted.

Data Path:

  • Without EncFS: data -> filesystem -> block device
  • With EncFS: data -> virtual filesystem -> filesystem -> block device

Example: read operation in EncFS

  1. use read file from EncFS mount point, with file’s name
  2. EncFS program encrypte the file’s name, and use the encrypted file path to lookup file in system filesystem
  3. if the file exist and everything ok (system checking), EncFS read data from that file which data was encrypted
  4. EncFS decrypte the data
  5. EncFS response user with the decrypted data

Notice:

  1. EncFS can be a way of transparent encryption of cloud storage, like GDrive/OneDrve etc.
  2. when the encryption mode is block mode, the encrypted file name will be large than original file name, which will trigger the max path length limitation of filesystem.
2. Usage

Install

apt-get install encfs

Configuration

# crate encrypted mount point
encfs ~/.encfs ~/encfs

# /etc/fstab
encfs#/path/.encfs  /path/encfs  fuse  noauto,user,nonempty  0  0

# auto unmount filesystems after user logout
# if any user still login, auto unmout will be failed
# ${HOME}/.bash_logout
`mount | grep -q "/path/encfs"`  && /usr/bin/fusermount -u /path/encfs

Manually:

# mount
mount /path/encfs

# unmount
fusermount -u /path/encfs
3. Reference
  1. EncFS - install
  2. EncFS - sourcecode
  3. EncFS - cloud-safe option

ECryptfs

1. Introduction

ECryptfs cryptographic filesystem (utilities). ECryptfs is a POSIX-compliant enterprise-class stacked cryptographic filesystem for Linux.

It’s not as user friendly as EncFS

2. Usage

archlinux - ECryptfs

3. Reference
  1. ECryptfs - install

Cryptomator - cloud storage transparent encryption

1. Introduction

Cryptomator offers multi-platform transparent client-side encryption of your files in the cloud.

Works with Dropbox, Google Drive, OneDrive, MEGA, pCloud, ownCloud, Nextcloud and any other cloud storage service which synchronizes with a local directory.

The way to create virtual system:

  1. FUSE: linux and macos
  2. WebDAV: a standardized protocol, some system has built-in support, others need additional program to mount WebDAV Server as filesystem
  3. Dokany: windows only
2. Usage

Install and Follow the UI and cryptomator - docs to completed.

3. Reference
  1. cryptomator - docs
  2. cryptomator - sourcecode

7-zip

1. Introduction

Credit: _mohemiv twitter post

when created encrypted zip file, the length of password should not longer than 64 bytes, or it will be decreased into 20 bytes.

The pseudorandom functions that zip used is PBKDF2 (HMAC-SHA-1)

rfc2898

An example pseudorandom function for PBKDF2 (Section 5.2) is HMAC- SHA-1.

In particular, when the key is longer than 512 bits, HMAC-SHA-1 will first hash it to 160 bits.

Both the long and short password can be used to decrypted the zip file that encrypted with the long password using AES.

root@localhost:~$ echo -ne "Nev1r-G0nna-G2ve-Y8u-Up-N5v1r-G1nna-Let-Y4u-D1wn-N8v4r-G5nna-D0sert-You" | shasum
706b4838613041714e62486364773847726d5370  -

root@localhost:~$ echo -ne "Nev1r-G0nna-G2ve-Y8u-Up-N5v1r-G1nna-Let-Y4u-D1wn-N8v4r-G5nna-D0sert-You" | shasum | cut -f1 -d' ' | xxd -r -p
pkH8a0AqNbHcdw8GrmSp

7z default using ZipCrypto encrypted zip file, use -mem=AES256 to use AES

7-zip

Use ZipCrypto, if you want to get archive compatible with most of the ZIP archivers. AES-256 provides stronger encryption, but now AES-256 is supported only by 7-Zip, WinZip and some other ZIP archivers.

Compatibility: ZipCrypto > AES (now, most common zip tools support AES) Strength: AES > ZipCrypto

2. Usage
# create zip file with AES encryption algorithm
7z a file.zip /path/files -mem=AES256 -p
3. Reference
  1. rfc289
  2. 7zip - doc method

git-crypt

1. Introduction

git-crypt enables transparent encryption and decryption of files in a git repository. git-crypt relies on git filters, only the files that matched the rule will be encrypted. when the requirement is full repo encryption, git-remote-gcrypt is the best choice.

git-crypt hijacking the data path between local and remote repo, the en- and decryption was done after commited and checked out.

2. Usage

Install

### 1. pre-requirements
##### 1.1 install
apt-get install git-crypt

Configuration

##### 1.2 generate gpg key
gpg --full-generate-key

##### 1.2.1 backup/import gpg key

archive .gnupg directory
backup directory `${HOME}/.gnupg`

export/import gpg public/private key
$ gpg --export *your key-ID* > /path/backup/public.gpg
$ gpg --export-secret-keys *your key-ID* > /path/backup/private.gpg

$ gpg --import /path/backup/public.gpg
$ gpg --import /path/backup/private.gpg


### 2. configurate repo
##### 2.1 init repo
git-crypt init

##### 2.2 add pgp key (search pgp key from pgp-agent with uid)
git-crypt add-gpg-user --trusted user@example.com

##### 2.3 ".gitattributes" configurate the rules of encryption
encrypt/* filter=git-crypt diff=git-crypt
.gitattributes !filter !diff

##### 2.4 git ops under this repo like the normal repo
git add/commit/push ...

##### 2.5 show encrypted files
git-crypt status -e

##### 2.6 decrypte repo
git-crypt unlock

Notice:

  1. encrypted data on remote repo is encrypted, which show as binary format
  2. encrypted data on local repo is decrypted, like normal repo
  3. pgp tool need to use /dev/pts/0, make sure the file’s owner match with the operation user. (change use after login will cause permission wrong).

Info:

  1. .gitattributes and .gitignore has same format
  2. .git/git-crypt/keys/default store the gpg key
3. Reference
  1. git-crypt - doc
  2. git-crypt - github
  3. git-remote-gcrypt - github

Conclusion

Design - Transparent Encryption

The key point of Transparent is hijacking some points in the path.

1. Introduction

All transparent encryption was done by hijacking the data syncing path between two endpoints.

The way to hijacking the data path:

  1. user-mode: create new virtual filesystem, and hijacking virtual filesystem and filesystem
  2. kernel-mode: device mapper (dm-crypt) or ebpf
  3. hijacking the syscall from application use LD_PRELOAD

If the hijacking change the UI, program need to create a new UI to make sure user has a way to access the decrypted data, like create a virtual filesystem using FUSE/WebDAV/Dokany.


Linux-storage-stack-diagram_v4.10.png

2. Reference
  1. BPF for storage: an exokernel-inspired approach
  2. ecapture
  3. Linux-storage-stack-diagram_v4.10.png