====== dm-crypt ======
> dm-crypt is a transparent disk encryption subsystem in Linux kernel.
[[wp>dm-crypt|dm-crypt]]是Linux内核提供的存储设备全盘透明加密方案。cryptsetup是其操作前端(frontends),用户主要接触的部分,以下主要讲解cryptsetup的使用。
===== Encrypt a device =====
首先运行''benchmark''查看系统性能,一般的处理器都支持''AES''加速,故建议采用''AES''相关的cipher
cryptsetup benchmark
Intel i5-3210M的结果如下:
PBKDF2-sha1 811591 iterations per second for 256-bit key
PBKDF2-sha256 1013116 iterations per second for 256-bit key
PBKDF2-sha512 666185 iterations per second for 256-bit key
PBKDF2-ripemd160 614640 iterations per second for 256-bit key
PBKDF2-whirlpool 386643 iterations per second for 256-bit key
# Algorithm | Key | Encryption | Decryption
aes-cbc 128b 497.8 MiB/s 1580.6 MiB/s
serpent-cbc 128b 69.9 MiB/s 233.1 MiB/s
twofish-cbc 128b 141.4 MiB/s 261.5 MiB/s
aes-cbc 256b 360.9 MiB/s 1305.9 MiB/s
serpent-cbc 256b 71.4 MiB/s 246.8 MiB/s
twofish-cbc 256b 158.2 MiB/s 285.0 MiB/s
aes-xts 256b 1316.7 MiB/s 1342.0 MiB/s
serpent-xts 256b 260.9 MiB/s 233.1 MiB/s
twofish-xts 256b 285.4 MiB/s 277.2 MiB/s
aes-xts 512b 1147.1 MiB/s 1142.4 MiB/s
serpent-xts 512b 244.9 MiB/s 241.2 MiB/s
twofish-xts 512b 290.8 MiB/s 286.4 MiB/s
''cryptsetup''根据以上结果设置哈希算法(Hash algorithm)和加密算法(cipher):
> Hash 选择 ''sha512'' 的原因是据说能降低专用破解设备如定制FPGA的爆破密码速度
> [[https://en.wikipedia.org/wiki/Disk_encryption_theory#XEX-based_tweaked-codebook_mode_with_ciphertext_stealing_.28XTS.29|因为xts的缘故]],key-size=256只相当于AES-128,想用AES-256请使用--key-size 512
cryptsetup -v --cipher aes-xts-plain64 --key-size 256 --hash sha512 luksFormat [device]
===== Add keys to luks device =====
添加一个密码:
cryptsetup luksAddKey /dev/
添加一个文件作为密码:
cryptsetup luksAddKey /dev/ /path/to/
查看当前luks设备的key slots:
cryptsetup luksDump /dev/
备份luks header:
cryptsetup luksHeaderBackup /dev/ --header-backup-file /mnt//.img
> 由于luksErase会擦除掉luks header,密码是保存在header里面的,在执行该命令后**所有**人都将无法访问该设备下的内容,所以考虑备份header且存放在安全的
===== Re-encrypt luks device with new encrypt algorithm =====
请备份数据以免数据丢失!!!
重新加密将**非常**消耗时间!请保证重新加密时供电、环境的绝对稳定!
我的移动硬盘是较早时间加密的,哈希算法还是在用已经宣布为不安全的''sha1'',所以有了这节内容。
如果想修改加密算法或者哈希算法,可以运行cryptsetup提供的cryptsetup-reencrypt
cryptsetup-reencrypt --cipher aes-xts-plain64 --key-size 256 --hash sha512 /dev/
===== Umount a luks device =====
cryptsetup luksClose /dev/mapper/luks-