一.基本名称,容易混淆

1.dm-crypt是linux的2.6内核开始集成的一种磁盘加密功能。十几年来,连sche调度算法都被改了N次,但dm-crypt一直稳定在内核中,稳定性还是很好的。

2.cryptsetup是linux一般自带的一个用户态工具,用来操作dm-crypt。

[root@localhost mnt]# whereis cryptsetup
cryptsetup: /usr/sbin/cryptsetup /usr/share/man/man8/cryptsetup..gz

3.luks是dm-crypt 最常用的一种模式,因为是linux中最常用的,所以有人经常混淆这三个东西。

二.dm-crypt支持的类型

open <device> <name> --type <device_type>

              Opens (creates a mapping with) <name> backed by device <device>.

              Device type can be plain, luks (default), loopaes or tcrypt.

              For backward compatibility there are open command aliases:

              create (argument-order <name> <device>): open --type plain
plainOpen: open --type plain
luksOpen: open --type luks
loopaesOpen: open --type loopaes
tcryptOpen: open --type tcrypt-------这个在cryptsetup 的1.6版本之后才开始支持。

四种模型,建议使用luks。

PLAIN DM-CRYPT OR LUKS?
Unless you understand the cryptographic background well, use LUKS. With plain dm-crypt there are a number of possible user errors
that massively decrease security. While LUKS cannot fix them all, it can lessen the impact for many of them.

由于cryptsetup 也在不停发展,在1.6的版本之后才开始支持tcrypt,所以如果要测试tcrypt 模式,建议使用高版本的内核,这样自带的cryptsetup 一般都支持。

# cryptsetup --version
cryptsetup 1.6.

三.测试记录:

由于不同的模式有不同的参数,本文记录的是luks模式。

You can only call luksFormat on a LUKS device that is not mapped.

              <options> can be [--hash, --cipher, --verify-passphrase, --key-size, --key-slot, --key-file (takes precedence over optional
second argument), --keyfile-offset, --keyfile-size, --use-random | --use-urandom, --uuid, --master-key-file, --iter-time,
--header, --force-password]. WARNING: Doing a luksFormat on an existing LUKS container will make all data the old container permanently irretrievable,
unless you have a header backup.

怎么设置参数呢?如果没指定的话,cryptsetup 会使用默认参数。

先fdisk分配一个分区,本文例子中,分区是sdb2。

对比两次不同的算法搭配不同的模式的性能比较:

[root@localhost mnt]# cryptsetup benchmark
# Tests are approximate using memory only (no storage IO).
PBKDF2-sha1 iterations per second
PBKDF2-sha256 iterations per second
PBKDF2-sha512 iterations per second
PBKDF2-ripemd160 iterations per second
PBKDF2-whirlpool iterations per second
# Algorithm | Key | Encryption | Decryption
aes-cbc 128b 646.6 MiB/s 2649.2 MiB/s
serpent-cbc 128b 79.2 MiB/s 535.7 MiB/s
twofish-cbc 128b 170.5 MiB/s 352.5 MiB/s
aes-cbc 256b 478.4 MiB/s 2049.6 MiB/s
serpent-cbc 256b 90.6 MiB/s 553.2 MiB/s
twofish-cbc 256b 183.2 MiB/s 353.4 MiB/s
aes-xts 256b 2238.0 MiB/s 2244.4 MiB/s
serpent-xts 256b 551.2 MiB/s 533.5 MiB/s
twofish-xts 256b 342.4 MiB/s 349.7 MiB/s
aes-xts 512b 1771.8 MiB/s 1767.3 MiB/s
serpent-xts 512b 552.3 MiB/s 535.4 MiB/s
twofish-xts 512b 343.8 MiB/s 349.3 MiB/s
[root@localhost mnt]# cryptsetup benchmark
# Tests are approximate using memory only (no storage IO).
PBKDF2-sha1 iterations per second
PBKDF2-sha256 iterations per second
PBKDF2-sha512 iterations per second
PBKDF2-ripemd160 iterations per second
PBKDF2-whirlpool iterations per second
# Algorithm | Key | Encryption | Decryption
aes-cbc 128b 646.2 MiB/s 2622.6 MiB/s
serpent-cbc 128b 90.5 MiB/s 551.6 MiB/s
twofish-cbc 128b 183.8 MiB/s 353.0 MiB/s
aes-cbc 256b 479.0 MiB/s 2048.1 MiB/s
serpent-cbc 256b 90.4 MiB/s 554.1 MiB/s
twofish-cbc 256b 183.6 MiB/s 353.5 MiB/s
aes-xts 256b 2237.1 MiB/s 2238.7 MiB/s
serpent-xts 256b 554.0 MiB/s 534.2 MiB/s
twofish-xts 256b 339.8 MiB/s 348.9 MiB/s
aes-xts 512b 1773.2 MiB/s 1708.6 MiB/s
serpent-xts 512b 553.3 MiB/s 535.7 MiB/s
twofish-xts 512b 343.6 MiB/s 348.2 MiB/s

可以看到,aes搭配xts性能较高,所以我们创建加密分区的时候,使用这种模式。

cryptsetup --cipher aes-xts-plain64 --key-size 512 --hash sha512  luksFormat /dev/sdb2

这个表示加密方式是aes-xts-plain64,密钥长度为512,散列算法为sha512 。

[root@localhost mnt]# cryptsetup --cipher aes-xts-plain64 --key-size  --hash sha512  luksFormat /dev/sdb2

WARNING!
========
This will overwrite data on /dev/sdb2 irrevocably. Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:

密码必须大于8位,且不能是简单密码,否则会无法通过。

用 LUKS 方式创建(格式化)加密盘之后,开始可以打开该盘用于操作了。

cryptsetup open  /dev/sdb2 caq_Encrypted_Fs
Enter passphrase for /dev/sdb2:

输入之前的密码,open成功,由于默认的open格式是luks,所以不需要输入对应的格式。否则,需要按照下面的格式来。

 Opens (creates a mapping with) <name> backed by device <device>.

              Device type can be plain, luks (default), loopaes or tcrypt.

              For backward compatibility there are open command aliases:

              create (argument-order <name> <device>): open --type plain
plainOpen: open --type plain
luksOpen: open --type luks
loopaesOpen: open --type loopaes
tcryptOpen: open --type tcrypt

可以看到,在/dev/mapper下,有一个创建的虚拟设备了。

[root@localhost mnt]# ls /dev/mapper/caq_Encrypted_Fs
/dev/mapper/caq_Encrypted_Fs
[root@localhost mnt]# cryptsetup status /dev/mapper/caq_Encrypted_Fs
/dev/mapper/caq_Encrypted_Fs is active.
type: LUKS1
cipher: aes-xts-plain64
keysize: bits
device: /dev/sdb2
offset: sectors
size: sectors
mode: read/write

之后,我们所有的操作都是基于caq_Encrypted_Fs。比如挂载,读写等。

创建一个目录,用来挂载。

mkdir -p /mnt/caqEncryptedFilesystem/

然后对我们的盘进行建立文件系统的操作:

root@localhost mnt]# mkfs.xfs -b size= -d sunit=,swidth= /dev/mapper/caq_Encrypted_Fs
meta-data=/dev/mapper/caq_Encrypted_Fs isize= agcount=, agsize= blks
= sectsz= attr=, projid32bit=
= crc= finobt=, sparse=
data = bsize= blocks=, imaxpct=
= sunit= swidth= blks
naming =version bsize= ascii-ci= ftype=
log =internal log bsize= blocks=, version=
= sectsz= sunit= blks, lazy-count=
realtime =none extsz= blocks=, rtextents=
[root@localhost mnt]# mount /dev/mapper/caq_Encrypted_Fs /mnt/caqEncryptedFilesystem/

挂载之后,可以对/mnt/caqEncryptedFilesystem/ 进行正常的文件读写了。

[root@localhost mnt]# cd /mnt/caqEncryptedFilesystem/
[root@localhost caqEncryptedFilesystem]# ls
[root@localhost caqEncryptedFilesystem]#
[root@localhost caqEncryptedFilesystem]#
[root@localhost caqEncryptedFilesystem]# dd if=/dev/zero of=caq.txt bs=4K count=
+ records in
+ records out
bytes ( kB) copied, 0.000195657 s, MB/s
[root@localhost caqEncryptedFilesystem]# ls *
caq.txt
[root@localhost caqEncryptedFilesystem]# echo "do it yourself" >>caq.txt2
[root@localhost caqEncryptedFilesystem]# ls
caq.txt caq.txt2

加密,顾名思义,保存在硬盘上的数据,应该就不是明文了,但是我们cat 看到的文件和写的文件是一样的,是因为经过了解密。如果硬盘丢失了,就不会有各种艳照门出现了。

描述到此,也没看出分区加密和tpm什么关系啊,下面,开始描述两者结合产生的效果。

我们知道,tpm能够加密文件,如果我们的加密分区的密码是以文件的形式存在,两者就很好地配合了。来看下面的操作:

先产生一个随机文件:

dd if=/dev/urandom of=/tmp/passwd_caq bs=1k count=64

我们看dump默认的输出,是密码数组,所谓密码数组,是指可以用一组独立的密码来解密这个加密的分区。

# cryptsetup luksDump /dev/sdb2
LUKS header information for /dev/sdb2 Version:
Cipher name: aes
Cipher mode: xts-plain64
Hash spec: sha512
Payload offset:
MK bits:
MK digest: e6 5c e8 ab 1d 8a ac cc f6 e5 7d bb d0 e2 6c 5f ed
MK salt: a8 1d b6 c8 cb de f4 2a 0d 5b cb
b9 aa e7 d2 eb aa aa 8f bf f2
MK iterations:
UUID: da24c447-4e62-463b-b533-89bd7fdf3238 Key Slot : ENABLED
Iterations:
Salt: 8f ab 9a e1 df 0b 5e
0c c7 fc e8 d6 e7 5d d1 0e f1
Key material offset:
AF stripes:
Key Slot : DISABLED
Key Slot : DISABLED
Key Slot : DISABLED
Key Slot : DISABLED
Key Slot : DISABLED
Key Slot : DISABLED
Key Slot : DISABLED

目前使用了第一个密码,后面的是空的,我们用来存密码文件。

# cryptsetup luksAddKey /dev/sdb2 /tmp/passwd_caq
Enter any passphrase:

现在,/tmp/passwd_caq 就成为了这个加密分区的密码文件了。重新dump一下,发现用掉了一个组,还剩下6个。

# cryptsetup luksDump /dev/sdb2
LUKS header information for /dev/sdb2 Version:
Cipher name: aes
Cipher mode: xts-plain64
Hash spec: sha512
Payload offset:
MK bits:
MK digest: e6 5c e8 ab 1d 8a ac cc f6 e5 7d bb d0 e2 6c 5f ed
MK salt: a8 1d b6 c8 cb de f4 2a 0d 5b cb
b9 aa e7 d2 eb aa aa 8f bf f2
MK iterations:
UUID: da24c447-4e62-463b-b533-89bd7fdf3238 Key Slot : ENABLED
Iterations:
Salt: 8f ab 9a e1 df 0b 5e
0c c7 fc e8 d6 e7 5d d1 0e f1
Key material offset:
AF stripes:
Key Slot : ENABLED
Iterations:
Salt: c0 0c 4d c2 d1 b6 4a a6 6d 8e
d3 5f d0 d4 a3 4d 4d ad
Key material offset:
AF stripes:
Key Slot : DISABLED
Key Slot : DISABLED
Key Slot : DISABLED
Key Slot : DISABLED
Key Slot : DISABLED
Key Slot : DISABLED

然后我们umount这个挂载的路径,看新增加的密码文件能否解密我们的分区。

[root@localhost mnt]# umount /mnt/caqEncryptedFilesystem/
[root@localhost mnt]# cryptsetup close /dev/mapper/caq_Encrypted_Fs
[root@localhost mnt]# cryptsetup --key-file /tmp/passwd_caq luksOpen /dev/sdb2 caq_Encrypted_Fs

从第三行可以看出,再也不用输入密码了,直接从文件中读密码。

下面的步骤,就简单了,使用之前的一篇博客,《linux tpm 测试完整记录,亲测有效》来再次加密这个/tmp/passwd_caq,就可以统一在tpm中管理我们的分区加密密码了。

linux 磁盘加密和tpm搭配使用1的更多相关文章

  1. linux初学者-磁盘加密篇

    linux初学者-磁盘加密篇 因为保密需要,一般系统中会在文件和磁盘中进行加密,但是文件的加密比较容易破解,不安全.所以在特殊需要下,会对磁盘进行加密,磁盘加密后在磁盘损坏的同时,其中的数据也会损坏, ...

  2. 存储系统管理(二)——Linux系统的swap分区、磁盘加密、磁盘阵列

    磁盘驱动器上的空间 , 用作当前未使用部分内存的溢出.这样 , 系统就能在主内存中留出空间用于储存当前正在处理的数据 , 并在系统面临主内存空间不足的风险时提供应急溢出. swap分区的建立: fdi ...

  3. openstack havana块存储Cinder磁盘加密方法研究

    http://blog.csdn.net/cloudresearch/article/details/19092219 在openstack havana的release note中有如下介绍“Att ...

  4. Linux 磁盘原理与管理 (分区 挂载)

    目前硬盘与主机系统的联系主要有几种传输介面规格: IDE介面:         IDE介面插槽所使用的排线较宽,每条排线上面可以接两个IDE装置,由於可以接两个装置,那为了判别两个装置的主/从架构, ...

  5. linux磁盘分区模式

    linux磁盘分区模式 模式一:MBR 1)主分区不超过四个 2)单个分区容量最大2TB 模式二:GPT 1)主分区个数"几乎"没有限制(原因:在GPT的分区表中最多可以支持128 ...

  6. MySQL 调优基础(四) Linux 磁盘IO

    1. IO处理过程 磁盘IO经常会成为系统的一个瓶颈,特别是对于运行数据库的系统而言.数据从磁盘读取到内存,在到CPU缓存和寄存器,然后进行处理,最后写回磁盘,中间要经过很多的过程,下图是一个以wri ...

  7. <实训|第七天>横扫Linux磁盘分区、软件安装障碍附制作软件仓库

    期待已久的linux运维.oracle"培训班"终于开班了,我从已经开始长期四个半月的linux运维.oracle培训,每天白天我会好好学习,晚上回来我会努力更新教程,包括今天学到 ...

  8. 调整Linux磁盘分区的大小的方法

      昨天数据入库时,一直报错,说磁盘满了,,df -h    一看,发现/目录下只有50G空间,已使用49G:我的程序和dbss都安装在/目录下,ftp到的数据放在/data下的一个子目录下,分解完的 ...

  9. Linux 磁盘管理

    Linux磁盘管理好坏管理直接关系到整个系统的性能问题. Linux磁盘管理常用三个命令为df.du和fdisk. df:列出文件系统的整体磁盘使用量 du:检查磁盘空间使用量 fdisk:用于磁盘分 ...

随机推荐

  1. Netty之心跳检测技术(四)

    Netty之心跳检测技术(四) 一.简介 "心跳"听起来感觉很牛X的样子,其实只是一种检测端到端连接状态的技术.举个简单的"栗子",现有A.B两端已经互相连接, ...

  2. [Java] 在 jar 文件中读取 resources 目录下的文件

    注意两点: 1. 将资源目录添加到 build path,确保该目录下的文件被拷贝到 jar 文件中. 2. jar 内部的东西,可以当作 stream 来读取,但不应该当作 file 来读取. 例子 ...

  3. 更新Appium中的WebDriverAgent

    到WebDriverAgent下载最新版本的WebDriverAgent 进入下载后的WebDriverAgent文件 执行 ./Scripts/bootstrap.sh 直接用Xcode打开WebD ...

  4. 利用appium-1.5.3.dmg安装Appium. doctors时,提示 Could not detect Mac OS X Version from sw_vers output: '10.12'

    发生这种错误的原因是因为:appium不支持mac 10.12版本. 解决方法: https://stackoverflow.com/questions/40129794/how-to-fix-err ...

  5. hive问题: For direct MetaStore DB connections, we don't support retries at the client level

    已经配置好hive,mysql作为元数据的数据库.在hive中执行drop语句出错: drop table mytest; 错误提示如下: FAILED: SemanticException Unab ...

  6. Zabbix实战-简易教程--告警屏蔽(Maintenances)

    Zabbix Maintenance 一.Zabbix Maintenance(zabbix告警屏蔽) 很多时候,我们需要在某段时间更新维护线上环境,这个时候你是已知触发器会引发报警,所以此时你可以提 ...

  7. windbg指定SOS版本,执行扩展命令报错

    调试dump文件,加载相匹配版本的sos/clr时,绝大多数都是可以正常使用的. 然而凡事都有例外,今天在做类似工作时,遇到了错误: CLRDLL: Consider using ".cor ...

  8. Django的生命周期图解

    下边的图就是一次完整的django生命周期,从客户端输入url,经过wsgi模块处理,得到符合HTTP协议的字符串,走中间件,假如中间件return None,继续往下走到urls......; 假如 ...

  9. JavaScript call()和apply()

    ECMAScript规范给所有函数都定义了call()与apply()两个方法,call()与apply()的第一个参数都是需要调用的函数对象,在函数体内这个参数就是this的值,剩余的参数是需要传递 ...

  10. 【批处理学习笔记】第十一课:常用DOS命令(1)

    [ 文件夹管理 ]cd 显示当前目录名或改变当前目录.md 创建目录.rd 删除一个目录.dir 显示目录中的文件和子目录列表.tree 以图形显示驱动器或路径的文件夹结构.path 为可执行文件显示 ...