xref: /OK3568_Linux_fs/kernel/Documentation/admin-guide/device-mapper/dm-crypt.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun========
2*4882a593Smuzhiyundm-crypt
3*4882a593Smuzhiyun========
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunDevice-Mapper's "crypt" target provides transparent encryption of block devices
6*4882a593Smuzhiyunusing the kernel crypto API.
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunFor a more detailed description of supported parameters see:
9*4882a593Smuzhiyunhttps://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunParameters::
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun	      <cipher> <key> <iv_offset> <device path> \
14*4882a593Smuzhiyun	      <offset> [<#opt_params> <opt_params>]
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun<cipher>
17*4882a593Smuzhiyun    Encryption cipher, encryption mode and Initial Vector (IV) generator.
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun    The cipher specifications format is::
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun       cipher[:keycount]-chainmode-ivmode[:ivopts]
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun    Examples::
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun       aes-cbc-essiv:sha256
26*4882a593Smuzhiyun       aes-xts-plain64
27*4882a593Smuzhiyun       serpent-xts-plain64
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun    Cipher format also supports direct specification with kernel crypt API
30*4882a593Smuzhiyun    format (selected by capi: prefix). The IV specification is the same
31*4882a593Smuzhiyun    as for the first format type.
32*4882a593Smuzhiyun    This format is mainly used for specification of authenticated modes.
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun    The crypto API cipher specifications format is::
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun        capi:cipher_api_spec-ivmode[:ivopts]
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun    Examples::
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun        capi:cbc(aes)-essiv:sha256
41*4882a593Smuzhiyun        capi:xts(aes)-plain64
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun    Examples of authenticated modes::
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun        capi:gcm(aes)-random
46*4882a593Smuzhiyun        capi:authenc(hmac(sha256),xts(aes))-random
47*4882a593Smuzhiyun        capi:rfc7539(chacha20,poly1305)-random
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun    The /proc/crypto contains a list of curently loaded crypto modes.
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun<key>
52*4882a593Smuzhiyun    Key used for encryption. It is encoded either as a hexadecimal number
53*4882a593Smuzhiyun    or it can be passed as <key_string> prefixed with single colon
54*4882a593Smuzhiyun    character (':') for keys residing in kernel keyring service.
55*4882a593Smuzhiyun    You can only use key sizes that are valid for the selected cipher
56*4882a593Smuzhiyun    in combination with the selected iv mode.
57*4882a593Smuzhiyun    Note that for some iv modes the key string can contain additional
58*4882a593Smuzhiyun    keys (for example IV seed) so the key contains more parts concatenated
59*4882a593Smuzhiyun    into a single string.
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun<key_string>
62*4882a593Smuzhiyun    The kernel keyring key is identified by string in following format:
63*4882a593Smuzhiyun    <key_size>:<key_type>:<key_description>.
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun<key_size>
66*4882a593Smuzhiyun    The encryption key size in bytes. The kernel key payload size must match
67*4882a593Smuzhiyun    the value passed in <key_size>.
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun<key_type>
70*4882a593Smuzhiyun    Either 'logon', 'user' or 'encrypted' kernel key type.
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun<key_description>
73*4882a593Smuzhiyun    The kernel keyring key description crypt target should look for
74*4882a593Smuzhiyun    when loading key of <key_type>.
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun<keycount>
77*4882a593Smuzhiyun    Multi-key compatibility mode. You can define <keycount> keys and
78*4882a593Smuzhiyun    then sectors are encrypted according to their offsets (sector 0 uses key0;
79*4882a593Smuzhiyun    sector 1 uses key1 etc.).  <keycount> must be a power of two.
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun<iv_offset>
82*4882a593Smuzhiyun    The IV offset is a sector count that is added to the sector number
83*4882a593Smuzhiyun    before creating the IV.
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun<device path>
86*4882a593Smuzhiyun    This is the device that is going to be used as backend and contains the
87*4882a593Smuzhiyun    encrypted data.  You can specify it as a path like /dev/xxx or a device
88*4882a593Smuzhiyun    number <major>:<minor>.
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun<offset>
91*4882a593Smuzhiyun    Starting sector within the device where the encrypted data begins.
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun<#opt_params>
94*4882a593Smuzhiyun    Number of optional parameters. If there are no optional parameters,
95*4882a593Smuzhiyun    the optional paramaters section can be skipped or #opt_params can be zero.
96*4882a593Smuzhiyun    Otherwise #opt_params is the number of following arguments.
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun    Example of optional parameters section:
99*4882a593Smuzhiyun        3 allow_discards same_cpu_crypt submit_from_crypt_cpus
100*4882a593Smuzhiyun
101*4882a593Smuzhiyunallow_discards
102*4882a593Smuzhiyun    Block discard requests (a.k.a. TRIM) are passed through the crypt device.
103*4882a593Smuzhiyun    The default is to ignore discard requests.
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun    WARNING: Assess the specific security risks carefully before enabling this
106*4882a593Smuzhiyun    option.  For example, allowing discards on encrypted devices may lead to
107*4882a593Smuzhiyun    the leak of information about the ciphertext device (filesystem type,
108*4882a593Smuzhiyun    used space etc.) if the discarded blocks can be located easily on the
109*4882a593Smuzhiyun    device later.
110*4882a593Smuzhiyun
111*4882a593Smuzhiyunsame_cpu_crypt
112*4882a593Smuzhiyun    Perform encryption using the same cpu that IO was submitted on.
113*4882a593Smuzhiyun    The default is to use an unbound workqueue so that encryption work
114*4882a593Smuzhiyun    is automatically balanced between available CPUs.
115*4882a593Smuzhiyun
116*4882a593Smuzhiyunsubmit_from_crypt_cpus
117*4882a593Smuzhiyun    Disable offloading writes to a separate thread after encryption.
118*4882a593Smuzhiyun    There are some situations where offloading write bios from the
119*4882a593Smuzhiyun    encryption threads to a single thread degrades performance
120*4882a593Smuzhiyun    significantly.  The default is to offload write bios to the same
121*4882a593Smuzhiyun    thread because it benefits CFQ to have writes submitted using the
122*4882a593Smuzhiyun    same context.
123*4882a593Smuzhiyun
124*4882a593Smuzhiyunno_read_workqueue
125*4882a593Smuzhiyun    Bypass dm-crypt internal workqueue and process read requests synchronously.
126*4882a593Smuzhiyun
127*4882a593Smuzhiyunno_write_workqueue
128*4882a593Smuzhiyun    Bypass dm-crypt internal workqueue and process write requests synchronously.
129*4882a593Smuzhiyun    This option is automatically enabled for host-managed zoned block devices
130*4882a593Smuzhiyun    (e.g. host-managed SMR hard-disks).
131*4882a593Smuzhiyun
132*4882a593Smuzhiyunintegrity:<bytes>:<type>
133*4882a593Smuzhiyun    The device requires additional <bytes> metadata per-sector stored
134*4882a593Smuzhiyun    in per-bio integrity structure. This metadata must by provided
135*4882a593Smuzhiyun    by underlying dm-integrity target.
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun    The <type> can be "none" if metadata is used only for persistent IV.
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun    For Authenticated Encryption with Additional Data (AEAD)
140*4882a593Smuzhiyun    the <type> is "aead". An AEAD mode additionally calculates and verifies
141*4882a593Smuzhiyun    integrity for the encrypted device. The additional space is then
142*4882a593Smuzhiyun    used for storing authentication tag (and persistent IV if needed).
143*4882a593Smuzhiyun
144*4882a593Smuzhiyunsector_size:<bytes>
145*4882a593Smuzhiyun    Use <bytes> as the encryption unit instead of 512 bytes sectors.
146*4882a593Smuzhiyun    This option can be in range 512 - 4096 bytes and must be power of two.
147*4882a593Smuzhiyun    Virtual device will announce this size as a minimal IO and logical sector.
148*4882a593Smuzhiyun
149*4882a593Smuzhiyuniv_large_sectors
150*4882a593Smuzhiyun   IV generators will use sector number counted in <sector_size> units
151*4882a593Smuzhiyun   instead of default 512 bytes sectors.
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun   For example, if <sector_size> is 4096 bytes, plain64 IV for the second
154*4882a593Smuzhiyun   sector will be 8 (without flag) and 1 if iv_large_sectors is present.
155*4882a593Smuzhiyun   The <iv_offset> must be multiple of <sector_size> (in 512 bytes units)
156*4882a593Smuzhiyun   if this flag is specified.
157*4882a593Smuzhiyun
158*4882a593SmuzhiyunExample scripts
159*4882a593Smuzhiyun===============
160*4882a593SmuzhiyunLUKS (Linux Unified Key Setup) is now the preferred way to set up disk
161*4882a593Smuzhiyunencryption with dm-crypt using the 'cryptsetup' utility, see
162*4882a593Smuzhiyunhttps://gitlab.com/cryptsetup/cryptsetup
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun::
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun	#!/bin/sh
167*4882a593Smuzhiyun	# Create a crypt device using dmsetup
168*4882a593Smuzhiyun	dmsetup create crypt1 --table "0 `blockdev --getsz $1` crypt aes-cbc-essiv:sha256 babebabebabebabebabebabebabebabe 0 $1 0"
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun::
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun	#!/bin/sh
173*4882a593Smuzhiyun	# Create a crypt device using dmsetup when encryption key is stored in keyring service
174*4882a593Smuzhiyun	dmsetup create crypt2 --table "0 `blockdev --getsize $1` crypt aes-cbc-essiv:sha256 :32:logon:my_prefix:my_key 0 $1 0"
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun::
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun	#!/bin/sh
179*4882a593Smuzhiyun	# Create a crypt device using cryptsetup and LUKS header with default cipher
180*4882a593Smuzhiyun	cryptsetup luksFormat $1
181*4882a593Smuzhiyun	cryptsetup luksOpen $1 crypt1
182