一.基本名称,容易混淆

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. python_第2课

    前言 回顾一下python+selenium基础,并整理相关知识点,分享给有需要,在前进道路上的朋友. 由于不是在python中敲的代码,有可能有缩进等相关错误,请自行检查 数据类型 #python中 ...

  2. php 运算符and or && || 的详解

    想弄清这个问题,首先要了解这些运算符的优先级:了解后,我们才知道,逻辑运算和赋值运算的执行顺序: //and or 的优先级小于 = //&& || 的优先级大于 = //or-前面语 ...

  3. 模拟winform里的控件的事件和委托机制

    参考:.NET 中的委托 委托的两大用处 1.委托可以实现方法作为参数进行传递,如 /// <summary> /// the English speaker. /// </summ ...

  4. c#之依赖注入

    C# 依赖注入 http://www.cnblogs.com/leoo2sk/archive/2009/06/17/1504693.html 1 IGame游戏公司的故事 1.1 讨论会 话说有一个叫 ...

  5. ionic2 安装(一)

    1.安装java JDK 2.安装nodejs 3.安装最新版ionic 指令:npm install ionic@latest 4.安装cordova 指令:npm install -g cordo ...

  6. Java编程思想读书笔记(二)【一切都是对象】

    begin 2018年1月9日17:06:47 第二章 一切都是对象 Java语言假设我们只进行面向对象的程序设计. 2.1 用引用操纵对象 每种编程语言都有自己的操纵内存元素的方式 操纵内存元素的方 ...

  7. 【译】巧用CSS变量实现自动前缀

    转:https://www.h5jun.com/post/autoprefixing-with-css-variables-lea-verou.html 最近,当我在制作 markapp.io 这个小 ...

  8. 升级node与npm

    升级node 原文地址:https://lierabbit.cn/articles/3 一.安装 n 工具,这个工具的名称很短,方便输入 npm install -g n 二.升级node至长期版或者 ...

  9. [笔记]Linux命令行大全

    date 显示当前时间和日期 cal 显示当前月份的日历 df 查看磁盘剩余空间的数量 free 显示空闲内存的数量 pwd 打印当前工作目录 cd 切换目录 ls 列出文件夹内容 绝对路径:开始于根 ...

  10. C# 知识回顾 - Lambda

    序 它是第十一个希腊字母,一个拥有失意.无奈.孤独.低调等含义的流行符号,也指示一款称为"半条命"的游戏. 不过,这次我所讲的是 C# 中的 Lambda. 目录 Lambda 简 ...