「Dm-crypt/デバイスの暗号化」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(ページの作成:「{{Lowercase title}} Category:セキュリティ Category:ファイルシステム en:Dm-crypt/Device encryption Dm-crypt に戻る。 このセクショ...」)
 
(カテゴリを変更)
 
(3人の利用者による、間の10版が非表示)
1行目: 1行目:
 
{{Lowercase title}}
 
{{Lowercase title}}
[[Category:セキュリティ]]
+
[[Category:スク暗号化]]
 
[[Category:ファイルシステム]]
 
[[Category:ファイルシステム]]
 
[[en:Dm-crypt/Device encryption]]
 
[[en:Dm-crypt/Device encryption]]
[[Dm-crypt]] に戻る。
 
 
 
このセクションではコマンドラインから ''dm-crypt'' を利用して手動でシステムを暗号化する方法を説明しています。
 
このセクションではコマンドラインから ''dm-crypt'' を利用して手動でシステムを暗号化する方法を説明しています。
   
 
== 準備 ==
 
== 準備 ==
{{pkg|cryptsetup}} を使用する前に、{{ic|dm-crypt}} [[カーネルモジュール]]がロードされていることを確認してください。
+
{{pkg|cryptsetup}} を使用する前に、{{ic|dm_crypt}} [[カーネルモジュール]]がロードされていることを確認してください。
   
 
== Cryptsetup の使用方法 ==
 
== Cryptsetup の使用方法 ==
  +
''Cryptsetup'' は暗号化デバイスを作成・管理する ''dm-crypt'' を使うためのコマンドラインツールです。後に Linux カーネルの '''d'''evice-'''m'''apper と '''crypt'''ographic モジュールを使用する別の暗号化もサポートするように拡張されました。最も著しい拡張は Linux Unified Key Setup (LUKS) の拡張で、dm-crypt をセットアップするのに必要な情報を全てディスク自体に保存してパーティションとキーの管理を抽象化することで使いやすさを増しています。device-mapper によってアクセスされるデバイスはブロックデバイスと呼ばれます。詳しくは[[ディスク暗号化#ブロックデバイスの暗号化]]を見て下さい。
''Cryptsetup'' is the command line tool to interface with ''dm-crypt'' for creating, accessing and managing encrypted devices. The tool was later expanded to support different encryption types that rely on the Linux kernel '''d'''evice-'''m'''apper and the '''crypt'''ographic modules. The most notable expansion was for the Linux Unified Key Setup (LUKS) extension, which stores all of the needed setup information for dm-crypt on the disk itself and abstracts partition and key management in an attempt to improve ease of use. Devices accessed via the device-mapper are called blockdevices. For further information see [[Disk_encryption#Block_device_encryption]].
 
   
  +
ツールは以下のように使います:
The tool is used as follows:
 
   
 
# cryptsetup <OPTIONS> <action> <action-specific-options> <device> <dmname>
 
# cryptsetup <OPTIONS> <action> <action-specific-options> <device> <dmname>
   
  +
オプションや暗号化モードにはデフォルト値が存在し、コマンドラインで何も指定しなかった場合はデフォルト値が使われます。オプションや暗号化モードのデフォルトパラメータを確認するには以下のコマンドを実行:
It has compiled-in defaults for the options and the encryption mode, which will be used if no others are specified on the command line. Have a look at
 
   
 
$ cryptsetup --help
 
$ cryptsetup --help
   
  +
オプションの完全なリストは man ページで確認できます。暗号化モードやアクションによって必要なパラメータは変わるため、以下のセクションでは違いについて説明しています。ブロックデバイス暗号化は高速ですが、速度は非常に重要な問題です。ブロックデバイスの暗号を設定後に変更することは難しいため、それぞれのパラメータについて ''dm-crypt'' のパフォーマンスをチェックすると良いでしょう:
which lists options, actions and the default parameters for the encryption modes in that order. A full list of options can be found on the man page.
 
Since different parameters are required or optional, depending on encryption mode and action, the following sections point out differences further. Blockdevice encryption is fast, but speed matters a lot too. Since changing an encryption cipher of a blockdevice after setup is difficult, it is important to check ''dm-crypt'' performance for the individual parameters in advance:
 
   
 
$ cryptsetup benchmark
 
$ cryptsetup benchmark
   
  +
上記のコマンドはインストールするときにアルゴリズムや鍵長を決める手がかりになります。特定の AES 暗号が非常に高速に処理される場合、おそらくその暗号は CPU によるハードウェア支援の恩恵に与かっています。
can give guidance on deciding for an algorithm and key-size prior to installation. If certain AES ciphers excel with a considerable higher throughput, these are probably the ones with hardware support in the CPU.
 
   
  +
{{Tip|テストしたいときは[[:カテゴリ:仮想化|仮想マシン]]の仮想ハードドライブで練習すると良いでしょう。}}
{{Tip|You may want to practise encrypting a virtual hard drive in a [[:Category:Virtualization|virtual machine]] when learning.}}
 
   
 
=== Cryptsetup のパスフレーズとキー ===
 
=== Cryptsetup のパスフレーズとキー ===
  +
暗号化されたブロックデバイスはキーによって保護されます。キーは以下のいずれかです:
An encrypted blockdevice is protected by a key. A key is either a:
 
   
  +
*パスフレーズ、[[ディスク暗号化#強固なパスフレーズの選択]]を見て下さい。
*Passphrase, see [[Disk_encryption#Choosing_a_strong_passphrase]]
 
  +
*キーファイル、[[#キーファイル]] を見て下さい。
*Keyfile, see [[#Keyfiles]]
 
   
  +
どちらのタイプのキーも最大サイズが決められています: パスフレーズは512文字まで、キーファイルは 8192kB までです。
Both key types have default maximum sizes: A passphrase can be up to 512 characters and a keyfile can have 8192kB.
 
  +
 
  +
''LUKS'' の重要な特徴として、キーは LUKS によって暗号化されたデバイスのマスターキーを解除するのに使われ、root 権限で変えることができるということです。他の暗号化モードでは設定後にキーを変更することはできません。暗号化にマスターキーを使わないためです。詳しくは[[ディスク暗号化#ブロックデバイスの暗号化]]を見て下さい。
An important distinction of ''LUKS'' to note at this point is that the key is used to unlock the master-key of a LUKS-encrypted device and can be changed with root access. Other encryption modes do not support changing the key after setup, because they do not employ a master-key for the encryption. See [[Disk_encryption#Block_device_encryption]] for details.
 
   
== Encryption options with dm-crypt ==
+
== dm-crypt の暗号化オプション ==
  +
''Cryptsetup'' は ''dm-crypt'' で使用できる様々な暗号化モードをサポートしています。最も一般的な (デフォルトの) モードは:
''Cryptsetup'' supports different encryption operating modes to use with ''dm-crypt''. The most common (and default) is
 
*{{ic|--type LUKS}}
+
*{{ic|--type LUKS}}
  +
他に利用できるモードは:
The other ones are
 
*{{ic|--type plain}} for using dm-crypt plain mode,
+
*{{ic|--type plain}} - dm-crypt plain モードを使用。
*{{ic|--type loopaes}} for a loopaes legacy mode, and
+
*{{ic|--type loopaes}} - loopaes legacy モードを使用。
*{{ic|--type tcrypt}} for a [[Truecrypt]] compatibility mode.
+
*{{ic|--type tcrypt}} - [[Truecrypt]] 互換モードを使用。
   
  +
暗号やハッシュの基本的な暗号化オプションは全てのモードで使うことができ、カーネルの暗号化バックエンド機能を利用します。使用できるオプションは以下のコマンドで確認できます:
The basic cryptographic options for encryption cipher and hashes available can be used for all modes and rely on the kernel cryptographic backend features. All that are loaded at runtime can be viewed with
 
 
$ less /proc/crypto
 
$ less /proc/crypto
  +
リストが短い場合、{{ic|cryptsetup benchmark}} を実行すればモジュールのロードが行われます。
and are available to use as options. If the list is short, execute {{ic|cryptsetup benchmark}} which will trigger loading available modules.
 
   
  +
以下では最初の2つのモードの暗号化オプションを紹介します。記事の中で例として使っているオプションを並べていますが、利用できるオプションの全てではないので注意してください。
The following introduces encryption options for the first two modes. Note that the tables list options used in the respective examples in this article and not all available ones.
 
   
 
=== LUKS モードの暗号化オプション ===
 
=== LUKS モードの暗号化オプション ===
  +
LUKS 暗号化モードで新しい dm-crypt デバイスをセットアップする ''cryptsetup'' のアクションは ''luksFormat'' です。名前にはフォーマットとありますが、実際にはデバイスをフォーマットするのではなく、LUKS デバイスヘッダーを設定して指定された暗号オプションを使ってマスター鍵を暗号化します。
The ''cryptsetup'' action to set up a new dm-crypt device in LUKS encryption mode is ''luksFormat''. Unlike the name implies, it does not format the device, but sets up the LUKS device header and encrypts the master-key with the desired cryptographic options.
 
  +
  +
LUKS はデフォルトの暗号化モードなので、デフォルトパラメータで新しい LUKS デバイスを作成する場合、以下のコマンドだけで作成できます ({{ic|-v}} は任意です):
  +
  +
# cryptsetup -v luksFormat ''device''
  +
  +
手動でデフォルトのオプションを全て指定すると以下のようになります:
   
  +
# cryptsetup -v --cipher aes-xts-plain64 --key-size 256 --hash sha256 --iter-time 2000 --use-urandom --verify-passphrase luksFormat ''device''
As LUKS is the default encryption mode:
 
# cryptsetup -v luksFormat <device>
 
is all needed to perform it with default parameters ({{ic|-v}} optional). For comparison, we can specify the default options manually too:
 
# cryptsetup -v --cipher aes-xts-plain64 --key-size 256 --hash sha1 --iter-time 1000 --use-urandom --verify-passphrase luksFormat <device>
 
   
  +
以下の表ではデフォルト値とサンプルにコメントを付けて説明しています:
These options used are compared below in the left column, with another set in the right column:
 
   
 
{| class="wikitable"
 
{| class="wikitable"
! scope="col" style="text-align:left" | Options
+
! scope="col" style="text-align:left" | オプション
! scope="col" style="text-align:left" | Cryptsetup (1.6.2) defaults
+
! scope="col" style="text-align:left" | Cryptsetup (1.7.0) のデフォルト
! scope="col" style="text-align:left" | Example
+
! scope="col" style="text-align:left" |
! scope="col" style="text-align:left" | Comment
+
! scope="col" style="text-align:left" | コメント
 
|-
 
|-
 
! scope="row" style="text-align:right" | --cipher, -c
 
! scope="row" style="text-align:right" | --cipher, -c
 
| {{ic|aes-xts-plain64}}
 
| {{ic|aes-xts-plain64}}
 
| {{ic|aes-xts-plain64}}
 
| {{ic|aes-xts-plain64}}
  +
| 例ではデフォルトと同じ暗号を使っています: AES [[ディスク暗号化#暗号と利用形態|暗号]]と [[wikipedia:Disk encryption theory#XEX-based tweaked-codebook mode with ciphertext stealing (XTS)|XTS]]。
| The example uses the same cipher as the default: the AES-[[Disk encryption#Ciphers_and_modes_of_operation|cipher]] with [[wikipedia:Disk_encryption_theory#XEX-based_tweaked-codebook_mode_with_ciphertext_stealing_.28XTS.29|XTS]].
 
 
|-
 
|-
 
! scope="row" style="text-align:right" | --key-size, -s
 
! scope="row" style="text-align:right" | --key-size, -s
 
| {{ic|256}}
 
| {{ic|256}}
 
| {{ic|512}}
 
| {{ic|512}}
  +
| デフォルトでは256ビットが使われます。ただし [[wikipedia:Disk encryption theory#XEX-based tweaked-codebook mode with ciphertext stealing (XTS)|XTS はキーを半分に割る]]ので、AES-128 ではなく AES-256 を使うには XTS のキーサイズを {{ic|512}} に設定する必要があります。
| The default uses a 256 bit key-size. [[wikipedia:XEX-TCB-CTS#Issues_with_XTS|XTS splits the supplied key]] into fraternal twins. For an effective AES-256 the XTS key-size must be {{ic|512}}.
 
 
|-
 
|-
 
! scope="row" style="text-align:right" | --hash, -h
 
! scope="row" style="text-align:right" | --hash, -h
| {{ic|sha1}}
+
| {{ic|sha256}}
 
| {{ic|sha512}}
 
| {{ic|sha512}}
  +
| [[ディスク暗号化#キーとキーファイルとパスフレーズ|PBKDF2]] で使用されるハッシュアルゴリズム。リリース 1.7.0 でデフォルト設定が {{ic|sha1}} から {{ic|sha256}} に変更されました。セキュリティ上の理由ではなく SHA1 が使用できないシステムでも動作するようにするためです [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.7/v1.7.0-ReleaseNotes]。{{ic|sha1}} でも十分セキュアであるため古いバージョンの ''cryptsetup'' と互換性を維持する目的で使用できます [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#5-security-aspects]。
| Hash algorithm used for [[Disk encryption#Keys, keyfiles and passphrases|PBKDF2]]. Due to this processing, SHA1 is considered [http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/7093 adequate] as of January 2014.
 
 
|-
 
|-
 
! scope="row" style="text-align:right" | --iter-time, -i
 
! scope="row" style="text-align:right" | --iter-time, -i
| {{ic|1000}}
+
| {{ic|2000}}
 
| {{ic|5000}}
 
| {{ic|5000}}
  +
| PBKDF2 パスフレーズを処理するのに費やす時間 (ミリ秒単位)。"''PBKDF2 の反復回数を高く保持したままユーザーが許容できる数値にするため''"、リリース 1.7.0 からデフォルト値が {{ic|1000}} から {{ic|2000}} に変更されています [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.7/v1.7.0-ReleaseNotes]。''luksFormat'' や ''luksAddKey'' などパスフレーズを設定・変更する LUKS 操作のみに関連するオプションです。パラメータとして 0 を指定するとコンパイル時のデフォルトが選択されます。
| Number of milliseconds to spend with PBKDF2 passphrase processing. Using a hash stronger than sha1 results in less iterations if iter-time is not increased.
 
 
|-
 
|-
! scope="row" style="text-align:right" | --use-random
+
! scope="row" style="text-align:right" | --use-{u,}random
 
| {{ic|--use-'''u'''random}}
 
| {{ic|--use-'''u'''random}}
 
| {{ic|--use-random}}
 
| {{ic|--use-random}}
  +
| [[乱数生成|乱数生成器]]の選択。cryptsetup のマニュアルページより: "エントロピーが少ない環境 (例: 組み込み環境)、どちらを選択しても問題になります。/dev/urandom を使うとキーが弱くなりかねません。/dev/random を使うと長い時間がかかり、カーネルから提供されるエントロピーが足りない場合、永遠に終わりません。"
| [[Random number generation|/dev/urandom]] is used as randomness source for the (long-term) volume master key. Avoid generating an insecure master key if low on entropy. The last three options only affect the encryption of the master key and not the disk operations.
 
 
|-
 
|-
 
! scope="row" style="text-align:right" | --verify-passphrase, -y
 
! scope="row" style="text-align:right" | --verify-passphrase, -y
 
| Yes
 
| Yes
 
| -
 
| -
| Default only for luksFormat and luksAddKey. No need to type for Arch Linux with LUKS mode at the moment.
+
| luksFormat luksAddKey のデフォルトのみ。Arch Linux LUKS モードを使うのに入力する必要はありません。
 
|}
 
|}
   
  +
LUKS の暗号機能について詳しく知りたい場合は [https://gitlab.com/cryptsetup/cryptsetup/wikis/Specification LUKS specification] (例: 付記) を読むと良いでしょう。
The options used in the example column result in the following:
 
  +
{{Tip|LUKS ヘッダーは、やがて別の大きな改訂を受けると予想されます。計画に興味がある場合は、開発者の [https://mbroz.fedorapeople.org/talks/DevConf2016/devconf2016-luks2.pdf devconfcz2016] (pdf) プレゼンテーションに要約されています。}}
# cryptsetup -v --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 5000 --use-random luksFormat <device>
 
 
Please note that with release 1.6.0, the defaults have changed to an AES cipher in ''XTS'' mode. It is advised against using the previous default {{ic|--cipher aes-cbc-essiv}}, because of its known [https://en.wikipedia.org/wiki/Disk_encryption_theory#Cipher-block_chaining_.28CBC.29 issues] and practical [http://www.jakoblell.com/blog/2013/12/22/practical-malleability-attack-against-cbc-encrypted-luks-partitions/ attacks] against them.
 
   
 
=== plain モードの暗号化オプション ===
 
=== plain モードの暗号化オプション ===
  +
dm-crypt の ''plain'' モードでは、デバイスのマスター鍵が存在せず、セットアップも必要ありません。代わりに、直接暗号化オプションを使用して暗号ディスクと名前付きデバイスのマッピングを作成します。パーティションやデバイス全体に対してマッピングを作成できます。後者の場合、パーティションテーブルも不要です。
In dm-crypt ''plain'' mode, there is no master-key on the device, hence, there is no need to set it up. Instead the encryption options to be employed are used directly to create the mapping between an encrypted disk and a named device. The mapping can be created against a partition or a full device. In the latter case not even a partition table is needed.
 
   
  +
cryptsetup のデフォルトパラメータを使って ''plain'' のマッピングを作成するには:
To create a ''plain'' mode mapping with cryptsetup's default parameters:
 
 
# cryptsetup <options> open --type plain <device> <dmname>
 
# cryptsetup <options> open --type plain <device> <dmname>
  +
実行するとパスワードを求められます。以下は [[Dm-crypt/システム全体の暗号化#Plain dm-crypt]] の例とデフォルトパラメータの比較表です。
Executing it will prompt for a password, which should have very high entropy.
 
Below a comparison of default parameters with the example in [[Dm-crypt/Encrypting an entire system#Plain dm-crypt]]
 
   
 
{| class="wikitable"
 
{| class="wikitable"
! Option !! Cryptsetup defaults (1.6.2) !! Example !! Comment !!
+
! オプション !! Cryptsetup (1.7.0) のデフォルト !! !! コメント !!
 
|-
 
|-
| '''--hash''' || {{ic|ripemd160}} || - || The hash is used to create the key from the passphrase; it is not used on a keyfile.
+
| '''--hash''' || {{ic|ripemd160}} || - || パスフレーズからキーを作成するのに使用するハッシュ。キーファイルでは使われない。
 
|-
 
|-
| '''--cipher'''|| {{ic|aes-cbc-essiv:sha256}}|| {{ic|twofish-xts-plain64}} || The cipher consists of three parts: cipher-chainmode-IV generator. Please see [[Disk encryption#Ciphers and modes of operation]] for an explanation of these settings, and the [https://code.google.com/p/cryptsetup/wiki/DMCrypt DMCrypt documentation] for some of the options available.
+
| '''--cipher'''|| {{ic|aes-cbc-essiv:sha256}}|| {{ic|twofish-xts-plain64}} || 暗号は3つの文字列からなります: cipher-chainmode-IV generator[[ディスク暗号化#暗号と利用形態]] [https://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt DMCrypt のドキュメント] を見てください。
 
|-
 
|-
  +
| '''--key-size'''||{{ic|256}}||{{ic|512}}||キーサイズ (ビット数)。サイズは使用する暗号や使用するチェインモードによって変わります。Xts モードは cbc モードの2倍のキーサイズを必要とします。
| '''--key-size'''||{{ic|256bits}}||{{ic|512}}||The key size (in bits). The size will depend on the cipher being used and also the chainmode in use. Xts mode requires twice the key size of cbc.
 
 
|-
 
|-
| '''--offset'''||{{ic|0}}||{{ic|0}}||The offset from the beginning of the target disk from which to start the mapping
+
| '''--offset'''||{{ic|0}}||{{ic|0}}||マッピングを開始するディスクの先頭からのオフセット。
 
|-
 
|-
| '''--key-file'''||default uses a passphrase||{{ic|/dev/sd''Z''}} (or e.g. /boot/keyfile.enc)||The device or file to be used as a key. See [[#Keyfiles]] for further details.
+
| '''--key-file'''||デフォルトではパスフレーズを使用||{{ic|/dev/sd''Z''}} (もしくは {{ic|/boot/keyfile.enc}})||キーとして使用するデバイスまたはファイル。詳しくは [[#キーファイル]] を参照。
 
|-
 
|-
| '''--keyfile-offset'''||{{ic|0}}||{{ic|0}}||Offset from the beginning of the file where the key starts (in bytes).
+
| '''--keyfile-offset'''||{{ic|0}}||{{ic|0}}||キーファイルの先頭からのオフセット (バイト数)。''cryptsetup'' 1.6.7 以上でサポートされているオプション。
 
|-
 
|-
| '''--keyfile-size'''||{{ic|8192kB}}||- (default applies)||Limits the bytes read from the key file.
+
| '''--keyfile-size'''||{{ic|8192kB}}||- (デフォルト)||キーファイルから読み込まれるバイト数を制限。''cryptsetup'' 1.6.7 以上でサポートされているオプション。
 
|}
 
|}
   
  +
{{ic|/dev/sd''X''}} デバイスで、上記の例を使用する場合:
Using the device {{ic|/dev/sd''X''}}, the above right column example results in:
 
{{bc|<nowiki># cryptsetup --cipher=twofish-xts-plain64 --offset=0 --key-file=</nowiki>/dev/sd''Z'' <nowiki>--key-size=512 open --type=plain /dev/sdX enc</nowiki>}}
+
# cryptsetup --cipher=twofish-xts-plain64 --offset=0 --key-file=/dev/sd''Z'' --key-size=512 open --type=plain /dev/sdX enc
  +
Unlike encrypting with LUKS, the above command must be executed ''in full'' whenever the mapping needs to be re-established, so it is important to remember the cipher, hash and key file details.
 
  +
LUKS による暗号化と違って、上記のコマンドはマッピングを再現するために毎回全て指定する必要があります。暗号やハッシュ、キーファイルについて覚えておく必要があります。マッピングが作成されたことは以下のコマンドで確認できます:
We can now check that the mapping has been made:
 
 
# fdisk -l
 
# fdisk -l
An entry should now exist for {{ic|/dev/mapper/enc}}.
+
{{ic|/dev/mapper/enc}} のエントリが存在しているはずです。
   
 
== cryptsetup でデバイスを暗号化 ==
 
== cryptsetup でデバイスを暗号化 ==
  +
以下のセクションでは新しい暗号化ブロックデバイスを作成するオプションと手動のアクセス方法を説明します。
This section shows how to employ the options for creating new encrypted blockdevices and accessing them manually.
 
   
 
=== LUKS モードでデバイスを暗号化 ===
 
=== LUKS モードでデバイスを暗号化 ===
 
==== LUKS パーティションのフォーマット ====
 
==== LUKS パーティションのフォーマット ====
   
  +
暗号化 LUKS パーティションとして設定するには次を実行:
In order to setup a partition as an encrypted LUKS partition execute:
 
{{hc|# cryptsetup -c <cipher> -y -s <key size> luksFormat /dev/<partition name>|
 
Enter passphrase: <password>
 
Verify passphrase: <password>}}
 
first to setup the encrypted master-key. Checking results can be done with:
 
# cryptsetup luksDump /dev/<drive>
 
   
  +
# cryptsetup luksFormat ''device''
This should be repeated for all partitions to be encrypted (except for {{ic|/boot}}). You will note that the dump not only shows the cipher header information, but also the key-slots in use for the LUKS partition.
 
   
  +
上記のコマンドを実行するとパスワードの入力が要求されます。
The following example will create an encrypted root partition using the default AES cipher in XTS mode with an effective 256-bit encryption
 
{{bc|# cryptsetup -s 512 luksFormat /dev/sdaX}}
 
   
  +
コマンドラインオプションは [[#LUKS モードの暗号化オプション]]を参照。
=====LUKS を使ってキーファイルでパーティションをフォーマット=====
 
   
  +
結果は次のコマンドで確認できます:
When creating a new LUKS encrypted partition, a keyfile may be associated with the partition on its creation using:
 
   
  +
# cryptsetup luksDump ''device''
# cryptsetup -c <desired cipher> -s <key size> luksFormat /dev/<volume to encrypt> '''/path/to/mykeyfile'''
 
   
  +
ダンプでは暗号のヘッダー情報だけでなく、LUKS パーティションで使われるキースロットもわかります。
This is accomplished by appending the bold area to the standard cryptsetup command which defines where the keyfile is located.
 
   
  +
以下の例はデフォルトの AES 暗号を使って256ビットの XTS モードで {{ic|/dev/sda1}} に暗号化したルートパーティションを作成します:
See [[#Keyfiles]] for instructions on how to generate and manage keyfiles.
 
  +
# cryptsetup -s 512 luksFormat /dev/sda1
  +
  +
=====LUKS を使ってキーファイルでパーティションをフォーマット=====
  +
  +
LUKS による暗号化パーティションを新しく作成する場合、以下のように作成するときにパーティションにキーファイルを関連付けることができます:
  +
  +
# cryptsetup luksFormat ''device'' ''/path/to/mykeyfile''
  +
  +
キーファイルを作成・管理する方法は [[#キーファイル]] を見て下さい。
   
 
====デバイスマッパーで LUKS パーティションのロックを解除・マップ====
 
====デバイスマッパーで LUKS パーティションのロックを解除・マップ====
   
  +
LUKS パーティションを作成したら、解錠することができます。
Once the LUKS partitions have been created it is time to unlock them.
 
   
  +
解錠ではデバイスマッパーによってパーティションが新しいデバイス名にマッピングされます。カーネルは {{ic|''device''}} が暗号化デバイスであり暗号化データを上書きしないように {{ic|/dev/mapper/''dm_name''}} を使って LUKS 経由で解決する必要があることを知ります。間違って上書きしないために、設定が完了したら[[#バックアップとリストア|暗号化ヘッダーをバックアップ]]する方法を確認してください。
The unlocking process will map the partitions to a new device name using the device mapper. This alerts the kernel that {{ic|/dev/<partition name>}} is actually an encrypted device and should be addressed through LUKS using the {{ic|/dev/mapper/<name>}} so as not to overwrite the encrypted data. To guard against accidental overwriting, read about the possibilities to [[Dm-crypt/Device encryption#Backup_and_restore|backup the cryptheader]] after finishing setup.
 
   
  +
暗号化された LUKS パーティションを開くには次のコマンドを実行:
In order to open an encrypted LUKS partition execute:
 
{{hc|# cryptsetup open --type luks /dev/<partition name> <device-mapper name>|
 
Enter any LUKS passphrase: <password>
 
key slot 0 unlocked.
 
Command successful.}}
 
   
  +
# cryptsetup open --type luks ''device'' ''dm_name''
Usually the device mapped name is descriptive of the function of the partition that is mapped, example:
 
   
  +
実行するとパーティションを解錠するパスワードの入力が求められます。通常、マッピングするデバイスの名前はパーティションの機能が分かりやすいものにします。例えば、以下のコマンドは luks パーティション {{ic|/dev/sda1}} を解錠して {{ic|cryptroot}} という名前のデバイスマッパーにマッピングします:
# cryptsetup open --type luks /dev/sdaX root
 
Once opened, the root partition device address would be {{ic|/dev/mapper/root}} instead of the partition (e.g. {{ic|/dev/sdaX}}).
 
   
# cryptsetup open --type luks /dev/sda3 lvmpool
+
# cryptsetup open --type luks /dev/sda1 cryptroot
For setting up LVM ontop the encryption layer the device file for the decrypted volume group would be anything like {{ic|/dev/mapper/lvmpool}} instead of {{ic|/dev/sdaX}}. LVM will then give additional names to all logical volumes created, e.g. {{ic|/dev/mapper/lvmpool-root}} and {{ic|/dev/mapper/lvmpool-swap}}.
 
   
  +
パーティションをオープンしたら、ルートパーティションのデバイスアドレスはパーティション本体 (例: {{ic|/dev/sda1}}) の代わりに {{ic|/dev/mapper/cryptroot}} が使われます。
In order to write encrypted data into the partition it must be accessed through the device mapped name. The first step of access will typically be to create a filesystem
 
  +
# mkfs -t ext4 /dev/mapper/root
 
  +
暗号化レイヤーの上に LVM をセットアップする場合、復号化したボリュームグループのデバイスファイルは {{ic|/dev/sda1}} ではなく {{ic|/dev/mapper/cryptroot}} などとなります。それから LVM は作成されている論理ボリューム全てに名前を割り当てます (例: {{ic|/dev/mapper/lvmpool-root}} や {{ic|/dev/mapper/lvmpool-swap}})。
and mount it
 
  +
# mount -t ext4 /dev/mapper/root /mnt
 
  +
暗号化データをパーティションに書き込むときは、マッピングされたデバイス名を使ってアクセスする必要があります。例えば[[ファイルシステム#デバイスのフォーマット|ファイルシステムを作成]]するときは以下のようにします:
The mounted blockdevice can then be used like any other partition. Once done, closing the device locks it again
 
  +
# umount /mnt
 
  +
# mkfs -t ext4 /dev/mapper/cryptroot
# cryptsetup close root
 
  +
  +
{{ic|/dev/mapper/cryptroot}} デバイスは他のパーティションと同じように[[マウント]]できます。
  +
  +
luks コンテナを閉じるには、パーティションをアンマウントしてください:
  +
  +
# cryptsetup close cryptroot
   
 
=== plain モードでデバイスを暗号化 ===
 
=== plain モードでデバイスを暗号化 ===
   
  +
''dm-crypt'' プレーンモード暗号化の作成とその後のアクセスはどちらも、正しい [[#plain視の暗号化オプション| parametersで ''cryptsetup'' {{ic|open}} アクションを使用するだけで済みます。]] 以下は、非ルートデバイスの2つの例で示していますが、両方をスタックすることで癖が追加されています (つまり、2番目は最初のデバイス内に作成されます) 明らかに、暗号化をスタックするとオーバーヘッドが2倍になります。 ここでの使用例は、暗号オプションの使用法の別の例を説明するためのものです。
The creation and subsequent access of a ''dm-crypt'' plain mode encryption both require not more than using the ''cryptsetup'' {{ic|open}} action with correct [[Dm-crypt/Device encryption#Encryption_options_for_plain_mode|parameters]]. The following shows that with two examples of non-root devices, but adds a quirk by stacking both (i.e. the second is created inside the first). Obviously, stacking the encryption doubles overhead. The usecase here is simply to illustrate another example of the cipher option usage.
 
   
  +
上記の表の左側の列で説明されているように、最初のマッパーは ''cryptsetup'' のプレーンモードのデフォルトで作成されます。
A first mapper is created with ''cryptsetup's'' plain-mode defaults, as described in the table's left column above
 
# cryptsetup --type plain -v open /dev/sdaX plain1
+
{{hc|# cryptsetup --type plain -v open /dev/sdaX plain1|
Enter passphrase:
+
Enter passphrase:
Command successful.
+
Command successful.
  +
}}
#
 
  +
次に、さまざまな暗号化パラメーターを使用し、(オプションの) オフセットを使用して、その中に2番目のブロックデバイスを追加し、ファイルシステムを作成してマウントします。
Now we add the second blockdevice inside it, using different encryption parameters and with an (optional) offset, create a filesystem and mount it
 
# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/mapper/plain1 plain2
+
{{hc|1=# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/mapper/plain1 plain2|2=
Enter passphrase:
+
Enter passphrase:
  +
}}
# lsblk -p
 
  +
{{hc|# lsblk -p|
 
NAME
 
NAME
 
/dev/sda
 
/dev/sda
214行目: 215行目:
 
│ └─/dev/mapper/plain2
 
│ └─/dev/mapper/plain2
 
...
 
...
  +
}}
 
# mkfs -t ext2 /dev/mapper/plain2
 
# mkfs -t ext2 /dev/mapper/plain2
 
# mount -t ext2 /dev/mapper/plain2 /mnt
 
# mount -t ext2 /dev/mapper/plain2 /mnt
222行目: 224行目:
 
First, let's try to open the filesystem directly:
 
First, let's try to open the filesystem directly:
 
# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/sdaX plain2
 
# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/sdaX plain2
# mount -t ext2 /dev/mapper/plain2 /mnt
+
{{hc|# mount -t ext2 /dev/mapper/plain2 /mnt|
mount: wrong fs type, bad option, bad superblock on /dev/mapper/plain2,
+
mount: wrong fs type, bad option, bad superblock on /dev/mapper/plain2,
missing codepage or helper program, or other error
+
missing codepage or helper program, or other error
  +
}}
Why that did not work? Because the "plain2" starting block (10) is still encrypted with the cipher from "plain1". It can only be accessed via the stacked mapper. The error is arbitrary though, trying a wrong passphrase or wrong options will yield the same. For ''dm-crypt'' plain mode, the {{ic|open}} action will not error out itself.
 
  +
Why that did not work? Because the "plain2" starting block ({{ic|10}}) is still encrypted with the cipher from "plain1". It can only be accessed via the stacked mapper. The error is arbitrary though, trying a wrong passphrase or wrong options will yield the same. For ''dm-crypt'' plain mode, the {{ic|open}} action will not error out itself.
   
 
Trying again in correct order:
 
Trying again in correct order:
 
# cryptsetup close plain2 # dysfunctional mapper from previous try
 
# cryptsetup close plain2 # dysfunctional mapper from previous try
# cryptsetup --type plain open /dev/sdaX plain1
+
{{hc|# cryptsetup --type plain open /dev/sdaX plain1|
Enter passphrase:
+
Enter passphrase:
  +
}}
# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/mapper/plain1 plain2
 
  +
{{hc|1=# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/mapper/plain1 plain2|2=
Enter passphrase:
 
  +
Enter passphrase:
# mount /dev/mapper/plain2 /mnt && cat /mnt/stacked.txt
 
  +
}}
This is stacked. one passphrase per foot to shoot.
 
  +
{{hc|# mount /dev/mapper/plain2 /mnt && cat /mnt/stacked.txt|
  +
This is stacked. one passphrase per foot to shoot.
  +
}}
 
# exit
 
# exit
 
''dm-crypt'' will handle stacked encryption with some mixed modes too. For example LUKS mode could be stacked on the "plain1" mapper. Its header would then be encrypted inside "plain1" when that is closed.
 
''dm-crypt'' will handle stacked encryption with some mixed modes too. For example LUKS mode could be stacked on the "plain1" mapper. Its header would then be encrypted inside "plain1" when that is closed.
   
 
Available for plain mode only is the option {{ic|--shared}}. With it a single device can be segmented into different non-overlapping mappers. We do that in the next example, using a ''loopaes'' compatible cipher mode for "plain2" this time:
 
Available for plain mode only is the option {{ic|--shared}}. With it a single device can be segmented into different non-overlapping mappers. We do that in the next example, using a ''loopaes'' compatible cipher mode for "plain2" this time:
# cryptsetup --type plain --offset 0 --size 1000 open /dev/sdaX plain1
+
{{hc|# cryptsetup --type plain --offset 0 --size 1000 open /dev/sdaX plain1|2=
Enter passphrase:
+
Enter passphrase:
  +
}}
# cryptsetup --type plain --offset 1000 --size 1000 --shared --cipher=aes-cbc-lmk --hash=sha256 open /dev/sdaX plain2
 
  +
{{hc|1=# cryptsetup --type plain --offset 1000 --size 1000 --shared --cipher=aes-cbc-lmk --hash=sha256 open /dev/sdaX plain2|2=
Enter passphrase:
 
  +
Enter passphrase:
# lsblk -p
 
  +
}}
NAME
 
  +
{{hc|# lsblk -p|
dev/sdaX
 
├─/dev/sdaX
+
NAME
│ ├─/dev/mapper/plain1
+
dev/sdaX
│ └─/dev/mapper/plain2
+
├─/dev/sdaX
  +
│ ├─/dev/mapper/plain1
...
 
  +
│ └─/dev/mapper/plain2
  +
...
  +
}}
 
As the devicetree shows both reside on the same level, i.e. are not stacked and "plain2" can be opened individually.
 
As the devicetree shows both reside on the same level, i.e. are not stacked and "plain2" can be opened individually.
   
 
== LUKS 固有の Cryptsetup のアクション ==
 
== LUKS 固有の Cryptsetup のアクション ==
 
=== キーの管理 ===
 
=== キーの管理 ===
  +
LUKS パーティションには異なる8つまでのキーを定義することができます。バックアップストレージのためのアクセスキーを作成することが可能です。いわゆるキーエスクローで、あるキーは日常的に使用するために使い、他のキーはエスクローに保存して日常用のパスフレーズを忘れたときやキーファイルを喪失・破損したときにパーティションにアクセスするために使います。また、別のキースロットを使って一時的なキーを発行してユーザーにパーティションへのアクセスを許可し、後でキーを無効化したりできます。
It is possible to define up to 8 different keys per LUKS partition. This enables the user to create access keys for save backup storage: In a so-called key escrow, one key is used for daily usage, another kept in escrow to gain access to the partition in case the daily passphrase is forgotten or a keyfile is lost/damaged. Also a different key-slot could be used to grant access to a partition to a user by issuing a second key and later revoking it again.
 
   
  +
暗号化パーティションを作成すると、初期キースロット 0 が作成されます (手動で別の数字を指定しなかった場合)。追加キースロットは 1 から 7 までの数字が割り振られます。どのキースロットを使用しているかは以下のコマンドで確認できます (<device> は LUKS ヘッダーが存在するボリュームに置き換えてください):
Once an encrypted partition has been created, the initial keyslot 0 is created (if no other was specified manually). Additional keyslots are numbered from 1 to 7. Which keyslots are used can be seen by issuing
 
{{hc|# cryptsetup luksDump /dev/<device> <nowiki>|</nowiki>grep BLED|
+
{{hc|# cryptsetup luksDump /dev/<device> {{!}} grep BLED|
 
Key Slot 0: ENABLED
 
Key Slot 0: ENABLED
 
Key Slot 1: ENABLED
 
Key Slot 1: ENABLED
267行目: 276行目:
 
Key Slot 7: DISABLED}}
 
Key Slot 7: DISABLED}}
   
  +
上記のコマンドや下で説明しているコマンドはヘッダーのバックアップファイルでも使えます。
Where <device> is the volume containing the LUKS header. This and all the following commands in this section work on header backup files as well.
 
   
 
==== LUKS キーの追加 ====
 
==== LUKS キーの追加 ====
   
  +
新しいキースロットの追加は cryptsetup の {{ic|luksAddKey}} アクションを使うことで出来ます。デバイスが解錠されていても安全のために、新しいキーを入力する前に、必ず既存のキーの入力が求められます ("any passphrase"):
Adding new keyslots is accomplished using cryptsetup with the {{ic|luksAddKey}} action. For safety it will always, i.e. also for already unlocked devices, ask for a valid existing key ("any passphrase") before a new one may be entered:
 
   
# cryptsetup luksAddKey /dev/<device> (/path/to/<additionalkeyfile>)
+
{{hc|# cryptsetup luksAddKey /dev/<device> (/path/to/<additionalkeyfile>)|
Enter any passphrase:
+
Enter any passphrase:
Enter new passphrase for key slot:
+
Enter new passphrase for key slot:
Verify passphrase:
+
Verify passphrase:
  +
}}
   
  +
{{ic|/path/to/<additionalkeyfile>}} を指定した場合、cryptsetup は <additionalkeyfile> の新しいキースロットを追加します。指定しなかった場合は新しいパスフレーズの入力が2回要求されます。既存の''キーファイル''を使って認証するには、{{ic|--key-file}} または {{ic|-d}} オプションの後に"既存"の <keyfile> を指定することで全てのキーファイルのキースロットが解除されます:
If {{ic|/path/to/<additionalkeyfile>}} is given, cryptsetup will add a new keyslot for <additionalkeyfile>. Otherwise a new passphrase will be prompted for twice. For using an existing ''keyfile'' to authorize the action, the {{ic|--key-file}} or {{ic|-d}} option followed by the "old" <keyfile> will try to unlock all available keyfile keyslots:
 
   
 
# cryptsetup luksAddKey /dev/<device> (/path/to/<additionalkeyfile>) -d /path/to/<keyfile>
 
# cryptsetup luksAddKey /dev/<device> (/path/to/<additionalkeyfile>) -d /path/to/<keyfile>
   
  +
複数のキーを使っていてキーを変更・無効化する場合、{{ic|--key-slot}} または {{ic|-S}} オプションでスロットを指定できます:
If it is intended to use multiple keys and change or revoke them, the {{ic|--key-slot}} or {{ic|-S}} option may be used to specify the slot:
 
# cryptsetup luksAddKey /dev/<device> -S 6
+
{{hc|# cryptsetup luksAddKey /dev/<device> -S 6|
Enter any passphrase:
+
Enter any passphrase:
Enter new passphrase for key slot:
+
Enter new passphrase for key slot:
Verify passphrase:
+
Verify passphrase:
  +
}}
# cryptsetup luksDump /dev/sda8 |grep 'Slot 6'
 
  +
{{hc|# cryptsetup luksDump /dev/sda8 {{!}} grep 'Slot 6'|
Key Slot 6: ENABLED
 
  +
Key Slot 6: ENABLED
  +
}}
   
  +
例えばキーを変更したい場合:
To show an associated action in this example, we decide to change the key right away:
 
# cryptsetup luksChangeKey /dev/<device> -S 6
+
{{hc|# cryptsetup luksChangeKey /dev/<device> -S 6|
Enter LUKS passphrase to be changed:
+
Enter LUKS passphrase to be changed:
Enter new LUKS passphrase:
+
Enter new LUKS passphrase:
  +
}}
 
before continuing to remove it.
 
   
 
==== LUKS キーの削除 ====
 
==== LUKS キーの削除 ====
   
  +
ヘッダーからキーを削除するアクションは3つ存在します:
There are three different actions to remove keys from the header:
 
*{{ic|luksRemoveKey}} is used to remove a key by specifying its passphrase/key-file.
+
*{{ic|luksRemoveKey}} はパスフレーズやキーファイルを指定してキーを削除します。
  +
*{{ic|luksKillSlot}} は (他のキーを使って) 指定したキースロットからキーを削除します。パスフレーズを忘れたり、キーファイルを無くしたり、誰もアクセスできないようにしたい場合に有用です。
*{{ic|luksKillSlot}} may be used to remove a key from a specific key slot (using another key). Obviously, this is extremely useful if you have forgotten a passphrase, lost a key-file, or have no access to it.
 
*{{ic|luksErase}} is used to quickly remove '''all''' active keys.
+
*{{ic|luksErase}} は全ての有効なキーを即座に削除します。
 
{{warning|
 
{{warning|
 
*All above actions can be used to irrevocably delete the last active key for an encrypted device!
 
*All above actions can be used to irrevocably delete the last active key for an encrypted device!
*The {{ic|luksErase}} command was added in version 1.6.4 to quickly nuke access to the device. This action '''will not''' prompt for a valid passphrase! It will not [[Dm-crypt/Drive_preparation#Wipe_LUKS_header|wipe the LUKS header]], but all keyslots at once and you will, therefore, not be able to regain access unless you have a valid backup of the LUKS header.}}
+
*The {{ic|luksErase}} command was added in version 1.6.4 to quickly nuke access to the device. This action '''will not''' prompt for a valid passphrase! It will not [[Dm-crypt/ドライブの準備#LUKS ヘッダーを消去|wipe the LUKS header]], but all keyslots at once and you will, therefore, not be able to regain access unless you have a valid backup of the LUKS header.}}
   
 
For above warning it is good to know the key we want to '''keep''' is valid. An easy check is to unlock the device with the {{ic|-v}} option, which will specify which slot it occupies:
 
For above warning it is good to know the key we want to '''keep''' is valid. An easy check is to unlock the device with the {{ic|-v}} option, which will specify which slot it occupies:
{{bc|# cryptsetup -v open /dev/<device> testcrypt
+
{{hc|# cryptsetup -v open /dev/<device> testcrypt|
 
Enter passphrase for /dev/<device>:
 
Enter passphrase for /dev/<device>:
 
Key slot 1 unlocked.
 
Key slot 1 unlocked.
314行目: 325行目:
   
 
Now we can remove the key added in the previous subsection using its passphrase:
 
Now we can remove the key added in the previous subsection using its passphrase:
# cryptsetup luksRemoveKey /dev/<device>
+
{{hc|# cryptsetup luksRemoveKey /dev/<device>|
Enter LUKS passphrase to be deleted:
+
Enter LUKS passphrase to be deleted:
  +
}}
 
If we had used the same passphrase for two keyslots, the first slot would be wiped now. Only executing it again would remove the second one.
 
If we had used the same passphrase for two keyslots, the first slot would be wiped now. Only executing it again would remove the second one.
   
 
Alternatively, we can specify the key slot:
 
Alternatively, we can specify the key slot:
# cryptsetup luksKillSlot /dev/<device> 6
+
{{hc|# cryptsetup luksKillSlot /dev/<device> 6|
Enter any remaining LUKS passphrase:
+
Enter any remaining LUKS passphrase:
  +
}}
   
 
Note that in both cases, no confirmation was required.
 
Note that in both cases, no confirmation was required.
# cryptsetup luksDump /dev/sda8 |grep 'Slot 6'
+
{{hc|# cryptsetup luksDump /dev/sda8 {{!}} grep 'Slot 6'|
Key Slot 6: DISABLED
+
Key Slot 6: DISABLED
  +
}}
 
To re-iterate the warning above: If the same passphrase had been used for key slots 1 and 6, both would be gone now.
 
To re-iterate the warning above: If the same passphrase had been used for key slots 1 and 6, both would be gone now.
   
  +
=== バックアップとリストア ===
=== Backup and restore ===
 
If the header of a LUKS encrypted partition gets destroyed, you will not be able to decrypt your data. It is just as much as a dilemma as forgetting the passphrase or damaging a key-file used to unlock the partition. A damage may occur by your own fault while re-partitioning the disk later or by third-party programs misinterpreting the partition table.
+
If the header of a LUKS encrypted partition gets destroyed, you will not be able to decrypt your data. It is just as much of a dilemma as forgetting the passphrase or damaging a key-file used to unlock the partition. Damage may occur by your own fault while re-partitioning the disk later or by third-party programs misinterpreting the partition table. Therefore, having a backup of the header and storing it on another disk might be a good idea.
 
Therefore, having a backup of the header and storing it on another disk might be a good idea.
 
   
  +
{{Note|If the LUKS-encrypted partitions' master passphrase becomes compromised, you must revoke it on ''every'' copy of the cryptheader, even those you have backed up. Otherwise, a copy of the backed-up cryptheader that uses the compromised passphrase can be used to decrypt the associated partition. See [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#6-backup-and-data-recovery LUKS FAQ] for further details.}}
'''Attention:''' Many people recommend NOT backing up the cryptheader, but even so it's a single point of failure!
 
In short, the problem is that LUKS is not aware of the duplicated cryptheader, which contains the master key used to encrypt all files on the partition. Of course this master key is encrypted with your passphrases or keyfiles.
 
But if one of those gets compromised and you want to revoke it you have to do this on all copies of the cryptheader!
 
I.e. if someone obtains a copy of the cryptheader and one of your keys he can decrypt the master key and access all your data.
 
Of course the same is true for all backups you create of partitions. So you decide if you are one of those paranoids brave enough to go without a backup for the sake of security or not. See also the [http://code.google.com/p/cryptsetup/wiki/FrequentlyAskedQuestions#6._Backup_and_Data_Recovery| LUKS FAQ] for further details on this.
 
   
 
==== cryptsetup を使ってバックアップ ====
 
==== cryptsetup を使ってバックアップ ====
344行目: 352行目:
   
 
{{Tip|You can also back up the plaintext header into ramfs and encrypt it in example with gpg before writing to persistent backup storage by executing the following commands.}}
 
{{Tip|You can also back up the plaintext header into ramfs and encrypt it in example with gpg before writing to persistent backup storage by executing the following commands.}}
{{bc|# mkdir /root/<tmp>/
+
# mkdir /root/<tmp>/
# mount ramfs /root/<tmp>/ -t ramfs
+
# mount ramfs /root/<tmp>/ -t ramfs
# cryptsetup luksHeaderBackup /dev/<device> --header-backup-file /root/<tmp>/<file>.img
+
# cryptsetup luksHeaderBackup /dev/<device> --header-backup-file /root/<tmp>/<file>.img
# gpg2 --recipient <User ID> --encrypt /root/<tmp>/<file>.img
+
# gpg2 --recipient <User ID> --encrypt /root/<tmp>/<file>.img
# cp /root/<tmp>/<file>.img.gpg /mnt/<backup>/
+
# cp /root/<tmp>/<file>.img.gpg /mnt/<backup>/
# umount /root/<tmp>}}
+
# umount /root/<tmp>
 
{{Warning|Tmpfs can swap to harddisk if low on memory so it is not recommended here.}}
 
{{Warning|Tmpfs can swap to harddisk if low on memory so it is not recommended here.}}
   
358行目: 366行目:
 
In order to evade restoring a wrong header, you can ensure it does work by using it as a remote {{ic|--header}} first:
 
In order to evade restoring a wrong header, you can ensure it does work by using it as a remote {{ic|--header}} first:
   
# cryptsetup -v --header /mnt/<backup>/<file>.img open /dev/<device> test
+
{{hc|# cryptsetup -v --header /mnt/<backup>/<file>.img open /dev/<device> test|
Key slot 0 unlocked.
+
Key slot 0 unlocked.
Command successful.
+
Command successful.
  +
}}
 
# mount /dev/mapper/test /mnt/test && ls /mnt/test
 
# mount /dev/mapper/test /mnt/test && ls /mnt/test
 
# umount /mnt/test
 
# umount /mnt/test
372行目: 381行目:
 
==== 手動バックアップとリストア ====
 
==== 手動バックアップとリストア ====
 
The header always resides at the beginning of the device and a backup can be performed without access to ''cryptsetup'' as well. First you have to find out the payload offset of the crypted partition:
 
The header always resides at the beginning of the device and a backup can be performed without access to ''cryptsetup'' as well. First you have to find out the payload offset of the crypted partition:
{{hc|# cryptsetup luksDump /dev/<device> <nowiki>|</nowiki> grep "Payload offset"|
+
{{hc|# cryptsetup luksDump /dev/<device> {{!}} grep "Payload offset"|
Payload offset: 4040}}
+
Payload offset: 4040}}
 
Second check the sector size of the drive
 
Second check the sector size of the drive
{{hc|# fdisk -l /dev/<device> <nowiki>|</nowiki>grep "Sector size"|Sector size (logical/physical): 512 bytes / 512 bytes}}
+
{{hc|# fdisk -l /dev/<device> {{!}} grep "Sector size"|Sector size (logical/physical): 512 bytes / 512 bytes}}
   
 
Now that you know the values, you can backup the header with a simple dd command:
 
Now that you know the values, you can backup the header with a simple dd command:
383行目: 392行目:
 
A restore can then be performed using the same values as when backing up:
 
A restore can then be performed using the same values as when backing up:
 
# dd if=./<file>.img of=/dev/<device> bs=512 count=4040
 
# dd if=./<file>.img of=/dev/<device> bs=512 count=4040
  +
  +
=== デバイスの再暗号化 ===
  +
  +
The {{Pkg|cryptsetup}} package features the ''cryptsetup-reencrypt'' tool. It can be used to convert an existing unencrypted filesystem to a LUKS encrypted one (option {{ic|--new}}) and permanently remove LUKS encryption ({{ic|--decrypt}}) from a device. As its name suggests it can also be used to re-encrypt an existing LUKS encrypted device, though, re-encryption is not possible for a detached LUKS header or other encryption modes (e.g. plain-mode). For re-encryption it is possible to change the [[#LUKS モードの暗号化オプション]]. ''cryptsetup-reencrypt'' actions can be performed to unmounted devices only. See {{man|8|cryptsetup-reencrypt}} for more information.
  +
  +
One application of re-encryption may be to secure the data again after a passphrase or [[#キーファイル|keyfile]] has been compromised ''and'' one cannot be certain that no copy of the LUKS header has been obtained. For example, if only a passphrase has been shoulder-surfed but no physical/logical access to the device happened, it would be enough to change the respective passphrase/key only ([[#Key management]]).
  +
  +
{{Warning|Always make sure a '''reliable backup''' is available and double-check options you specify before using the tool!}}
  +
  +
The following shows an example to encrypt an unencrypted filesystem partition and a re-encryption of an existing LUKS device.
  +
  +
==== 暗号化されていないファイルシステムの暗号化 ====
  +
  +
A LUKS encryption header is always stored at the beginning of the device. Since an existing filesystem will usually be allocated all partition sectors, the first step is to shrink it to make space for the LUKS header.
  +
  +
The [[#LUKS モードの暗号化オプション|default]] LUKS header encryption cipher requires {{ic|4096}} 512-byte sectors. We already checked space and keep it simple by shrinking the existing {{ic|ext4}} filesystem on {{ic|/dev/sdaX}} to its current possible minimum:
  +
  +
# umount /mnt
  +
{{hc|# e2fsck -f /dev/sdaX|
  +
e2fsck 1.43-WIP (18-May-2015)
  +
Pass 1: Checking inodes, blocks, and sizes
  +
...
  +
/dev/sda6: 12/166320 files (0.0% non-contiguous), 28783/665062 blocks
  +
}}
  +
{{hc|# resize2fs -M /dev/sdaX|
  +
resize2fs 1.43-WIP (18-May-2015)
  +
Resizing the filesystem on /dev/sdaX to 26347 (4k) blocks.
  +
The filesystem on /dev/sdaX is now 26347 (4k) blocks long.}}
  +
  +
Now we encrypt it, using the default cipher we do not have to specify it explicitly. Note there is no option (yet) to double-check the passphrase before encryption starts, be careful not to mistype:
  +
  +
{{hc|# cryptsetup-reencrypt /dev/sdaX --new --reduce-device-size 4096S|
  +
WARNING: this is experimental code, it can completely break your data.
  +
Enter new passphrase:
  +
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed 37,6 MiB/s}}
  +
  +
After it finished, the encryption was performed to the full partition, i.e. not only the space the filesystem was shrunk to ({{ic|sdaX}} has {{ic|2.6GiB}} and the CPU used in the example has no hardware AES instructions). As a final step we extend the filesystem of the now encrypted device again to occupy available space:
  +
  +
{{hc|# cryptsetup open /dev/sdaX recrypt|
  +
Enter passphrase for /dev/sdaX:
  +
...
  +
}}
  +
{{hc|# resize2fs /dev/mapper/recrypt|
  +
resize2fs 1.43-WIP (18-May-2015)
  +
Resizing the filesystem on /dev/mapper/recrypt to 664807 (4k) blocks.
  +
The filesystem on /dev/mapper/recrypt is now 664807 (4k) blocks long.}}
  +
# mount /dev/mapper/recrypt /mnt
  +
  +
and are done.
  +
  +
==== 既存の LUKS パーティションの再暗号化 ====
  +
  +
以下の例では既存の LUKS デバイスを最暗号化しています。
  +
  +
{{Warning|Double-check you specify encryption options for ''cryptsetup-reencrypt'' correctly and ''never'' re-encrypt without a '''reliable backup'''! As of September 2015 the tool '''does''' accept invalid options and damage the LUKS header, if not used correctly!}}
  +
  +
In order to re-encrypt a device with its existing encryption options, they do not need to be specified. A simple:
  +
  +
{{hc|# cryptsetup-reencrypt /dev/sdaX|
  +
WARNING: this is experimental code, it can completely break your data.
  +
Enter passphrase for key slot 0:
  +
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed 36,5 MiB/s}}
  +
  +
performs it.
  +
  +
A possible usecase is to re-encrypt LUKS devices which have non-current encryption options. Apart from above warning on specifying options correctly, the ability to change the LUKS header may also be limited by its size. For example, if the device was initially encrypted using a CBC mode cipher and 128 bit key-size, the LUKS header will be half the size of above mentioned {{ic|4096}} sectors:
  +
{{hc|# cryptsetup luksDump /dev/sdaX <nowiki>|</nowiki>grep -e "mode" -e "Payload" -e "MK bits"|
  +
Cipher mode: cbc-essiv:sha256
  +
Payload offset: '''2048'''
  +
MK bits: 128}}
  +
While it is possible to upgrade the encryption of such a device, it is currently only feasible in two steps. First, re-encrypting with the same encryption options, but using the {{ic|--reduce-device-size}} option to make further space for the larger LUKS header. Second, re-encypt the whole device again with the desired cipher. For this reason and the fact that a backup should be created in any case, creating a new, fresh encrypted device to restore into is always the faster option.
  +
  +
== 暗号化デバイスのリサイズ ==
  +
  +
If a storage device encrypted with dm-crypt is being cloned (with a tool like dd) to another larger device, the underlying dm-crypt device must be resized to use the whole space.
  +
  +
The destination device is /dev/sdX2 in this example, the whole available space adjacent to the partition will be used:
  +
  +
# cryptsetup luksOpen /dev/sdX2 sdX2
  +
# cryptsetup resize sdX2
  +
  +
Then the underlying filesystem must be resized.
  +
  +
=== ループバックファイルシステム ===
  +
  +
暗号化したループバックデバイスを {{ic|/mnt/secret}} にマウントしている場合、例えば [[Dm-crypt/root 以外のファイルシステムの暗号化#Loop デバイス]] に従っている場合、最初に暗号化コンテナをアンマウント:
  +
# umount /mnt/secret
  +
# cryptsetup close secret
  +
# losetup -d /dev/loop0
  +
  +
次に、追加したいデータ容量を決めてコンテナファイルを拡張します:
  +
  +
{{Warning|{{ic|>}} を2つ使っていることに注意してください。1つだけだと現在のコンテナを上書きしてしまいます。}}
  +
# dd if=/dev/urandom bs=1M count=1024 | cat - >> /bigsecret
  +
  +
ループデバイスにコンテナをマップ:
  +
# losetup /dev/loop0 /bigsecret
  +
# cryptsetup --type luks open /dev/loop0 secret
  +
  +
その後、コンテナの暗号化された部分をコンテナファイルの最大サイズまでリサイズ:
  +
# cryptsetup resize secret
  +
  +
最後に、ファイルシステムのチェックを実行して、問題なければ、リサイズを行います (例: ext2/3/4):
  +
# e2fsck -f /dev/mapper/secret
  +
# resize2fs /dev/mapper/secret
  +
  +
またコンテナをマウントすることができます:
  +
# mount /dev/mapper/secret /mnt/secret
   
 
== キーファイル ==
 
== キーファイル ==
   
  +
{{Note|このセクションでは平文のキーファイルを使う方法を説明しています。キーファイルを暗号化して二段階認証したい場合は[[Dm-crypt/特記事項#GPG や OpenSSL で暗号化されたキーファイルを使う|GPG や OpenSSL で暗号化されたキーファイルを使う]]を見て下さい。ただし、このセクションもあらかじめ読むようにしてください。}}
{{Note|This section describes using a plaintext keyfile. If you want to encrypt your keyfile giving you two factor authentication see [[Dm-crypt/Specialties#Using_GPG_or_OpenSSL_Encrypted_Keyfiles|Using GPG or OpenSSL Encrypted Keyfiles]] for details, but please still read this section.}}
 
   
  +
'''キーファイルとは?'''
'''What is a keyfile?'''
 
   
  +
キーファイルは暗号化されたボリュームを解錠するパスフレーズとして使用するデータを含んだファイルです。ファイルを喪失した場合、ボリュームの復号化は出来なくなります。
A keyfile is any file in which the data contained within it is used as the passphrase to unlock an encrypted volume.
 
Therefore if these files are lost or changed, decrypting the volume will no longer be possible.
 
   
 
{{Tip|Define a passphrase in addition to the keyfile for backup access to encrypted volumes in the event the defined keyfile is lost or changed.}}
 
{{Tip|Define a passphrase in addition to the keyfile for backup access to encrypted volumes in the event the defined keyfile is lost or changed.}}
   
  +
'''なぜキーファイルを使うのか?'''
'''Why use a keyfile?'''
 
   
  +
キーファイルには様々なタイプがあります。以下で説明しているようにそれぞれメリットとデメリットが存在します:
There are many kinds of keyfiles. Each type of keyfile used has benefits and disadvantages summarized below:
 
   
 
=== キーファイルのタイプ ===
 
=== キーファイルのタイプ ===
 
==== passphrase ====
 
==== passphrase ====
   
  +
単にパスフレーズが記述されたキーファイルです。メリットはファイルを無くしても暗号化ボリュームの所有者が簡単に中身を思い出せることです。デメリットはパスフレーズの入力と比べてセキュリティ上の優位点がないことです。
This is a keyfile containing a simple passphrase. The benefit of this type of keyfile is that if the file is lost the data it contained is known and hopefully easily remembered by the owner of the encrypted volume. However the disadvantage is that this does not add any security over entering a passphrase during the initial system start.
 
   
Example: 1234
+
: {{ic|1234}}。
  +
  +
{{Note|パスフレーズが記述されたキーファイルに改行を含めてはいけません。以下のようなコマンドでファイルを作成できます:
  +
# echo -n 'your_passphrase' > /path/to/<keyfile>
  +
# chown root:root /path/to/<keyfile>; chmod 400 /path/to/<keyfile>
  +
}}
   
 
==== randomtext ====
 
==== randomtext ====
410行目: 531行目:
 
This is a keyfile containing a block of random characters. The benefit of this type of keyfile is that it is much more resistant to dictionary attacks than a simple passphrase. An additional strength of keyfiles can be utilized in this situation which is the length of data used. Since this is not a string meant to be memorized by a person for entry, it is trivial to create files containing thousands of random characters as the key. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase.
 
This is a keyfile containing a block of random characters. The benefit of this type of keyfile is that it is much more resistant to dictionary attacks than a simple passphrase. An additional strength of keyfiles can be utilized in this situation which is the length of data used. Since this is not a string meant to be memorized by a person for entry, it is trivial to create files containing thousands of random characters as the key. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase.
   
Example: fjqweifj830149-57 819y4my1-38t1934yt8-91m 34co3;t8y;9p3y-
+
: {{ic|fjqweifj830149-57 819y4my1-38t1934yt8-91m 34co3;t8y;9p3y-}}。
   
 
==== binary ====
 
==== binary ====
416行目: 537行目:
 
This is a binary file that has been defined as a keyfile. When identifying files as candidates for a keyfile, it is recommended to choose files that are relatively static such as photos, music, video clips. The benefit of these files is that they serve a dual function which can make them harder to identify as keyfiles. Instead of having a text file with a large amount of random text, the keyfile would look like a regular image file or music clip to the casual observer. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase. Additionally, there is a theoretical loss of randomness when compared to a randomly generated text file. This is due to the fact that images, videos and music have some intrinsic relationship between neighboring bits of data that does not exist for a text file. However this is controversial and has never been exploited publicly.
 
This is a binary file that has been defined as a keyfile. When identifying files as candidates for a keyfile, it is recommended to choose files that are relatively static such as photos, music, video clips. The benefit of these files is that they serve a dual function which can make them harder to identify as keyfiles. Instead of having a text file with a large amount of random text, the keyfile would look like a regular image file or music clip to the casual observer. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase. Additionally, there is a theoretical loss of randomness when compared to a randomly generated text file. This is due to the fact that images, videos and music have some intrinsic relationship between neighboring bits of data that does not exist for a text file. However this is controversial and has never been exploited publicly.
   
  +
例: 画像, テキスト, 動画。
Example: images, text, video, ...
 
   
 
=== ランダムな文字列でキーファイルを作成 ===
 
=== ランダムな文字列でキーファイルを作成 ===
422行目: 543行目:
 
==== ファイルシステムにキーファイルを保存 ====
 
==== ファイルシステムにキーファイルを保存 ====
   
  +
キーファイルの中身とサイズは任意に決められます。
A keyfile can be of arbitrary content and size.
 
   
Here {{ic|dd}} is used to generate a keyfile of 2048 random bytes, storing it in the file {{ic|/etc/mykeyfile}}:
+
以下では {{ic|dd}} を使って2048バイトのランダムなキーファイルを生成して、{{ic|/etc/mykeyfile}} ファイルに保存します:
   
# dd bs=512 count=4 if=/dev/urandom of=/etc/mykeyfile iflag=fullblock
+
# dd bs=512 count=4 if=/dev/urandom of=/etc/mykeyfile
   
  +
キーファイルを外部デバイスに保存する場合、出力先を適切なディレクトリに変更します:
If you are planning to store the keyfile on an external device, you can also simply change the outputfile to the corresponding directory:
 
   
# dd bs=512 count=4 if=/dev/urandom of=/media/usbstick/mykeyfile iflag=fullblock
+
# dd bs=512 count=4 if=/dev/urandom of=/media/usbstick/mykeyfile
   
 
===== 保存されたキーファイルを完全に消去 =====
 
===== 保存されたキーファイルを完全に消去 =====
   
  +
物理的なストレージデバイスに一時的なキーファイルを保存する場合、削除するときは、以下のように完全に削除してください:
If you stored your temporary keyfile on a physical storage device, and want to delete it, remember to not just remove the keyfile later on, but use something like
 
   
 
# shred --remove --zero mykeyfile
 
# shred --remove --zero mykeyfile
   
  +
FAT や ext2 であれば上記で十分ですが、ジャーナリングファイルシステムやフラッシュメモリハードウェアの場合、[[ディスクの完全消去]]あるいはキーファイルのあるパーティションを削除することを強く推奨します。
to securely overwrite it. For overaged filesystems like FAT or ext2 this will suffice while in the case of journaling filesystems, flash memory hardware and other cases it is highly recommended to [[Securely wipe disk|wipe the entire device]] or at least the keyfiles partition.
 
   
==== tmpfs にキーファイルを保存 ====
+
==== ramfs にキーファイルを保存 ====
   
  +
もしくは、ramfs をマウントしてキーファイルを一時的に保存:
Alternatively, you can mount a tmpfs for storing the keyfile temporarily:
 
   
# mkdir mytmpfs
+
# mkdir /root/myramfs
# mount tmpfs mytmpfs -t tmpfs -o size=32m
+
# mount ramfs /root/myramfs/ -t ramfs
# cd mytmpfs
+
# cd /root/myramfs
   
  +
物理ディスクではなくメモリ上に配置することで、ramfs をアンマウントしたら復元できないという利点があります。キーファイルを他の安全なファイルシステムにコピーしたら、以下のコマンドで ramfs をアンマウントしてください:
The advantage is that it resides in RAM and not on a physical disk, therefore it can not be recovered after unmounting the tmpfs. On the other hand this requires you to copy the keyfile to another filesystem you consider secure before unmounting.
 
  +
# umount /root/myramfs
   
 
=== キーファイルを使用するように LUKS を設定 ===
 
=== キーファイルを使用するように LUKS を設定 ===
   
  +
キーファイルのキースロットを LUKS ヘッダに追加:
Add a keyslot for the keyfile to the LUKS header:
 
   
{{hc|# cryptsetup luksAddKey /dev/sda2 mykeyfile|
+
{{hc|# cryptsetup luksAddKey /dev/sda2 /etc/mykeyfile|
 
Enter any LUKS passphrase:
 
Enter any LUKS passphrase:
 
key slot 0 unlocked.
 
key slot 0 unlocked.
 
Command successful.}}
 
Command successful.}}
  +
  +
=== キーファイルを使ってパーティションを手動でアンロック ===
  +
  +
LUKS デバイスをオープンするときに {{ic|--key-file}} オプションを使ってください:
  +
  +
# cryptsetup open /dev/sda2 ''dm_name'' --key-file /etc/mykeyfile
   
 
=== 起動時にロックを解除 ===
 
=== 起動時にロックを解除 ===
   
  +
ファイルシステムのキーファイルを暗号化されたルートディレクトリの中に保存することで、システムの電源が切れているときに安全を確保することができ、[[Dm-crypt/システム設定#crypttab|crypttab]] によって起動時に自動的にマウントを解錠することができます。[[UUID]] を使用する例:
If the keyfile for a secondary file system is itself stored inside an encrypted root, it is safe while the system is powered off but can be sourced to automatically unlock the mount during with boot via [[Dm-crypt/System configuration#crypttab|crypttab]]. Following above first example
 
{{hc|/etc/crypttab|home /dev/sda2 /etc/mykeyfile}}
+
{{hc|/etc/crypttab|<nowiki>home UUID=<UUID identifier> /etc/mykeyfile</nowiki>}}
 
is all needed for unlocking, and
 
is all needed for unlocking, and
 
{{hc|/etc/fstab|/dev/mapper/home /home ext4 defaults 0 2}} for mounting the LUKS blockdevice with the generated keyfile.
 
{{hc|/etc/fstab|/dev/mapper/home /home ext4 defaults 0 2}} for mounting the LUKS blockdevice with the generated keyfile.
{{Tip|If you prefer to use a {{ic|--plain}} mode blockdevice, the encryption options necessary to unlock it are specified in {{ic|/etc/crypttab}}. Take care to apply the systemd workaround mentioned in [[Dm-crypt/System configuration#crypttab|crypttab]] in this case.}}
+
{{Tip|If you prefer to use a {{ic|--plain}} mode blockdevice, the encryption options necessary to unlock it are specified in {{ic|/etc/crypttab}}. Take care to apply the systemd workaround mentioned in [[Dm-crypt/システム設定#crypttab|crypttab]] in this case.}}
   
 
=== キーファイルを使って起動時に root パーティションのロックを解除 ===
 
=== キーファイルを使って起動時に root パーティションのロックを解除 ===
   
  +
[[mkinitcpio]] を設定して必要なモジュールやファイルを記述して [[Dm-crypt/システム設定#cryptkey|cryptkey]] [[カーネルパラメータ]]を設定してキーファイルの場所を指定します。
The following method uses an USB-stick to store the keyfile and configures {{ic|mkinitcpio}} to load the keyfile and unlock the root partition at boot.
 
   
  +
2つの方法が存在します:
==== mkinitcpio の設定 ====
 
   
  +
# 外部メディア (USB スティック) にキーファイルを保存
You have to add two extra modules in your {{ic|/etc/mkinitcpio.conf}}, one for the drive's file system ({{ic|vfat}} module in the example below) and one for the codepage ({{ic|nls_cp437}} module) :
 
  +
# initramfs にキーファイルを埋め込む
MODULES="nls_cp437 vfat"
 
   
  +
==== キーファイルを外部メディアに保存 ====
In this example it is assumed that you use a FAT formatted USB drive ({{ic|vfat}} module). Replace those module names if you use another file system on your USB stick (e.g. {{ic|ext2}}) or another codepage. Users running the stock Arch kernel should stick to the codepage mentioned here. If it complains of bad superblock and bad codepage at boot, then you need an extra codepage module to be loaded. For instance, you may need {{ic|nls_iso8859-1}} module for {{ic|iso8859-1}} codepage.
 
  +
  +
===== mkinitcpio の設定 =====
  +
  +
ドライブのファイルシステムを使うために {{ic|/etc/mkinitcpio.conf}} にモジュールを追加してください (以下の例では {{ic|vfat}} モジュール):
  +
MODULES=(vfat)
  +
  +
In this example it is assumed that you use a FAT formatted USB drive ({{ic|vfat}} module). Replace those module names if you use another file system on your USB stick (e.g. {{ic|ext2}}) or another codepage. If it complains of bad superblock and bad codepage at boot, then you need an extra codepage module to be loaded. For instance, you may need {{ic|nls_iso8859-1}} module for {{ic|iso8859-1}} codepage.
   
 
If you have a non-US keyboard, it might prove useful to load your keyboard layout before you are prompted to enter the password to unlock the root partition at boot. For this, you will need the {{ic|keymap}} hook before {{ic|encrypt}}.
 
If you have a non-US keyboard, it might prove useful to load your keyboard layout before you are prompted to enter the password to unlock the root partition at boot. For this, you will need the {{ic|keymap}} hook before {{ic|encrypt}}.
484行目: 619行目:
 
# mkinitcpio -p linux
 
# mkinitcpio -p linux
   
==== カーネルパラメータの設定 ====
+
===== カーネルパラメータの設定 =====
   
以下のオプションを[[カーネルパラメータ]]に追加してください:
+
以下のオプションを[[カーネルパラメータ]]に追加してください ({{ic|encrypt}} フックではなく {{ic|sd-encrypt}} フックを使用する場合は [[Dm-crypt/システム設定#sd-encrypt フックを使う]]を参照):
   
cryptdevice=/dev/<partition1>:root cryptkey=/dev/<partition2>:<fstype>:<path>
+
cryptdevice=/dev/''<partition1>'':root cryptkey=/dev/''<partition2>'':<fstype>:<path>
   
 
例:
 
例:
496行目: 631行目:
 
Choosing a plain filename for your key provides a bit of 'security through obscurity'. The keyfile can not be a hidden file, that means the filename must not start with a dot, or the {{ic|encrypt}} hook will fail to find the keyfile during the boot process.
 
Choosing a plain filename for your key provides a bit of 'security through obscurity'. The keyfile can not be a hidden file, that means the filename must not start with a dot, or the {{ic|encrypt}} hook will fail to find the keyfile during the boot process.
   
  +
{{ic|/dev/sdb1}} のようなデバイスノードの名前は再起動しても同じであるとは保証されていません。udev による[[永続的なブロックデバイスの命名]]を使ったほうが確実にデバイスにアクセスできます。外部ストレージデバイスからキーファイルを読み取るときに {{ic|encrypt}} フックが確実にキーファイルを見つけられるように、永続的なブロックデバイスの名前を絶対に使うべきです。[[永続的なブロックデバイスの命名]]を見て下さい。
The naming of device nodes like {{ic|/dev/sdb1}} is not guaranteed to stay the same across reboots. It is more reliable to access the device with udev's [[persistent block device naming]] instead. To assure that the {{ic|encrypt}} hook finds your keyfile when reading it from an external storage device, persistent block device names must be used. See the article [[persistent block device naming]].
 
  +
  +
==== キーファイルを initramfs に埋め込む ====
  +
  +
{{Warning|Use an embedded keyfile '''only''' if you have some form of authentication mechanism beforehand that protects the keyfile sufficiently. Otherwise auto-decryption will occur, defeating completely the purpose of block device encryption.}}
  +
  +
This method allows to use a specially named keyfile that will be embedded in the [[initramfs]] and picked up by the {{ic|encrypt}} [[Mkinitcpio#HOOKS|hook]] to unlock the root filesystem ({{ic|cryptdevice}}) automatically. It may be useful to apply when using the [[GRUB#暗号化された /boot|GRUB early cryptodisk]] feature, in order to avoid entering two passphrases during boot.
  +
  +
The {{ic|encrypt}} hook lets the user specify a keyfile with the {{ic|cryptkey}} kernel parameter: in the case of initramfs, the syntax is {{ic|rootfs:''path''}}, see [[Dm-crypt/システム設定#cryptkey]]. Besides, this kernel parameter defaults to use {{ic|/crypto_keyfile.bin}}, and if the initramfs contains a valid key with this name, decryption will occur automatically without the need to configure the {{ic|cryptkey}} parameter.
  +
  +
{{ic|encrypt}} の代わりに {{ic|sd-encrypt}} を使用する場合、{{ic|luks.key}} カーネルパラメータでキーファイルの場所を指定してください。[[Dm-crypt/システム設定#luks.key]] を参照。
  +
  +
[[#ランダムな文字列でキーファイルを作成|キーファイルを生成]]して適切な権限を与えて [[#LUKS キーの追加|LUKS キーとして追加]]:
  +
  +
# dd bs=512 count=4 if=/dev/urandom of=/crypto_keyfile.bin
  +
# chmod 000 /crypto_keyfile.bin
  +
# chmod 600 /boot/initramfs-linux*
  +
# cryptsetup luksAddKey /dev/sdX# /crypto_keyfile.bin
  +
  +
{{Warning|When initramfs' permissions are set to 644 (by default), then all users will be able to dump the keyfile. Make sure the permissions are still 600 if you install a new kernel.}}
  +
  +
[[Mkinitcpio#BINARIES_と_FILES|mkinitcpio の FILES]] にキーを記述:
  +
  +
{{hc|/etc/mkinitcpio.conf|2=FILES=(/crypto_keyfile.bin)}}
  +
  +
最後に [[Mkinitcpio#イメージ作成とアクティベーション|initramfs を再生成]]してください。
  +
  +
次の起動時からコンテナを復号化するパスフレーズを入力するのは一度だけですむようになります。
  +
  +
詳しくは [http://www.pavelkogan.com/2014/05/23/luks-full-disk-encryption/#bonus-login-once こちら] を参照。

2023年9月9日 (土) 18:16時点における最新版

このセクションではコマンドラインから dm-crypt を利用して手動でシステムを暗号化する方法を説明しています。

目次

準備

cryptsetup を使用する前に、dm_crypt カーネルモジュールがロードされていることを確認してください。

Cryptsetup の使用方法

Cryptsetup は暗号化デバイスを作成・管理する dm-crypt を使うためのコマンドラインツールです。後に Linux カーネルの device-mapper と cryptographic モジュールを使用する別の暗号化もサポートするように拡張されました。最も著しい拡張は Linux Unified Key Setup (LUKS) の拡張で、dm-crypt をセットアップするのに必要な情報を全てディスク自体に保存してパーティションとキーの管理を抽象化することで使いやすさを増しています。device-mapper によってアクセスされるデバイスはブロックデバイスと呼ばれます。詳しくはディスク暗号化#ブロックデバイスの暗号化を見て下さい。

ツールは以下のように使います:

# cryptsetup <OPTIONS> <action> <action-specific-options> <device> <dmname>

オプションや暗号化モードにはデフォルト値が存在し、コマンドラインで何も指定しなかった場合はデフォルト値が使われます。オプションや暗号化モードのデフォルトパラメータを確認するには以下のコマンドを実行:

$ cryptsetup --help 

オプションの完全なリストは man ページで確認できます。暗号化モードやアクションによって必要なパラメータは変わるため、以下のセクションでは違いについて説明しています。ブロックデバイス暗号化は高速ですが、速度は非常に重要な問題です。ブロックデバイスの暗号を設定後に変更することは難しいため、それぞれのパラメータについて dm-crypt のパフォーマンスをチェックすると良いでしょう:

$ cryptsetup benchmark 

上記のコマンドはインストールするときにアルゴリズムや鍵長を決める手がかりになります。特定の AES 暗号が非常に高速に処理される場合、おそらくその暗号は CPU によるハードウェア支援の恩恵に与かっています。

ヒント: テストしたいときは仮想マシンの仮想ハードドライブで練習すると良いでしょう。

Cryptsetup のパスフレーズとキー

暗号化されたブロックデバイスはキーによって保護されます。キーは以下のいずれかです:

どちらのタイプのキーも最大サイズが決められています: パスフレーズは512文字まで、キーファイルは 8192kB までです。

LUKS の重要な特徴として、キーは LUKS によって暗号化されたデバイスのマスターキーを解除するのに使われ、root 権限で変えることができるということです。他の暗号化モードでは設定後にキーを変更することはできません。暗号化にマスターキーを使わないためです。詳しくはディスク暗号化#ブロックデバイスの暗号化を見て下さい。

dm-crypt の暗号化オプション

Cryptsetupdm-crypt で使用できる様々な暗号化モードをサポートしています。最も一般的な (デフォルトの) モードは:

  • --type LUKS

他に利用できるモードは:

  • --type plain - dm-crypt plain モードを使用。
  • --type loopaes - loopaes legacy モードを使用。
  • --type tcrypt - Truecrypt 互換モードを使用。

暗号やハッシュの基本的な暗号化オプションは全てのモードで使うことができ、カーネルの暗号化バックエンド機能を利用します。使用できるオプションは以下のコマンドで確認できます:

$ less /proc/crypto 

リストが短い場合、cryptsetup benchmark を実行すればモジュールのロードが行われます。

以下では最初の2つのモードの暗号化オプションを紹介します。記事の中で例として使っているオプションを並べていますが、利用できるオプションの全てではないので注意してください。

LUKS モードの暗号化オプション

LUKS 暗号化モードで新しい dm-crypt デバイスをセットアップする cryptsetup のアクションは luksFormat です。名前にはフォーマットとありますが、実際にはデバイスをフォーマットするのではなく、LUKS デバイスヘッダーを設定して指定された暗号オプションを使ってマスター鍵を暗号化します。

LUKS はデフォルトの暗号化モードなので、デフォルトパラメータで新しい LUKS デバイスを作成する場合、以下のコマンドだけで作成できます (-v は任意です):

# cryptsetup -v luksFormat device

手動でデフォルトのオプションを全て指定すると以下のようになります:

# cryptsetup -v --cipher aes-xts-plain64 --key-size 256 --hash sha256 --iter-time 2000 --use-urandom --verify-passphrase luksFormat device

以下の表ではデフォルト値とサンプルにコメントを付けて説明しています:

オプション Cryptsetup (1.7.0) のデフォルト コメント
--cipher, -c aes-xts-plain64 aes-xts-plain64 例ではデフォルトと同じ暗号を使っています: AES 暗号XTS
--key-size, -s 256 512 デフォルトでは256ビットが使われます。ただし XTS はキーを半分に割るので、AES-128 ではなく AES-256 を使うには XTS のキーサイズを 512 に設定する必要があります。
--hash, -h sha256 sha512 PBKDF2 で使用されるハッシュアルゴリズム。リリース 1.7.0 でデフォルト設定が sha1 から sha256 に変更されました。セキュリティ上の理由ではなく SHA1 が使用できないシステムでも動作するようにするためです [1]sha1 でも十分セキュアであるため古いバージョンの cryptsetup と互換性を維持する目的で使用できます [2]
--iter-time, -i 2000 5000 PBKDF2 パスフレーズを処理するのに費やす時間 (ミリ秒単位)。"PBKDF2 の反復回数を高く保持したままユーザーが許容できる数値にするため"、リリース 1.7.0 からデフォルト値が 1000 から 2000 に変更されています [3]luksFormatluksAddKey などパスフレーズを設定・変更する LUKS 操作のみに関連するオプションです。パラメータとして 0 を指定するとコンパイル時のデフォルトが選択されます。
--use-{u,}random --use-urandom --use-random 乱数生成器の選択。cryptsetup のマニュアルページより: "エントロピーが少ない環境 (例: 組み込み環境)、どちらを選択しても問題になります。/dev/urandom を使うとキーが弱くなりかねません。/dev/random を使うと長い時間がかかり、カーネルから提供されるエントロピーが足りない場合、永遠に終わりません。"
--verify-passphrase, -y Yes - luksFormat と luksAddKey のデフォルトのみ。Arch Linux で LUKS モードを使うのに入力する必要はありません。

LUKS の暗号機能について詳しく知りたい場合は LUKS specification (例: 付記) を読むと良いでしょう。

ヒント: LUKS ヘッダーは、やがて別の大きな改訂を受けると予想されます。計画に興味がある場合は、開発者の devconfcz2016 (pdf) プレゼンテーションに要約されています。

plain モードの暗号化オプション

dm-crypt の plain モードでは、デバイスのマスター鍵が存在せず、セットアップも必要ありません。代わりに、直接暗号化オプションを使用して暗号ディスクと名前付きデバイスのマッピングを作成します。パーティションやデバイス全体に対してマッピングを作成できます。後者の場合、パーティションテーブルも不要です。

cryptsetup のデフォルトパラメータを使って plain のマッピングを作成するには:

# cryptsetup <options> open --type plain <device> <dmname>

実行するとパスワードを求められます。以下は Dm-crypt/システム全体の暗号化#Plain dm-crypt の例とデフォルトパラメータの比較表です。

オプション Cryptsetup (1.7.0) のデフォルト コメント
--hash ripemd160 - パスフレーズからキーを作成するのに使用するハッシュ。キーファイルでは使われない。
--cipher aes-cbc-essiv:sha256 twofish-xts-plain64 暗号は3つの文字列からなります: cipher-chainmode-IV generator。ディスク暗号化#暗号と利用形態DMCrypt のドキュメント を見てください。
--key-size 256 512 キーサイズ (ビット数)。サイズは使用する暗号や使用するチェインモードによって変わります。Xts モードは cbc モードの2倍のキーサイズを必要とします。
--offset 0 0 マッピングを開始するディスクの先頭からのオフセット。
--key-file デフォルトではパスフレーズを使用 /dev/sdZ (もしくは /boot/keyfile.enc) キーとして使用するデバイスまたはファイル。詳しくは #キーファイル を参照。
--keyfile-offset 0 0 キーファイルの先頭からのオフセット (バイト数)。cryptsetup 1.6.7 以上でサポートされているオプション。
--keyfile-size 8192kB - (デフォルト) キーファイルから読み込まれるバイト数を制限。cryptsetup 1.6.7 以上でサポートされているオプション。

/dev/sdX デバイスで、上記の例を使用する場合:

# cryptsetup --cipher=twofish-xts-plain64 --offset=0 --key-file=/dev/sdZ --key-size=512 open --type=plain /dev/sdX enc

LUKS による暗号化と違って、上記のコマンドはマッピングを再現するために毎回全て指定する必要があります。暗号やハッシュ、キーファイルについて覚えておく必要があります。マッピングが作成されたことは以下のコマンドで確認できます:

# fdisk -l

/dev/mapper/enc のエントリが存在しているはずです。

cryptsetup でデバイスを暗号化

以下のセクションでは新しい暗号化ブロックデバイスを作成するオプションと手動のアクセス方法を説明します。

LUKS モードでデバイスを暗号化

LUKS パーティションのフォーマット

暗号化 LUKS パーティションとして設定するには次を実行:

# cryptsetup luksFormat device

上記のコマンドを実行するとパスワードの入力が要求されます。

コマンドラインオプションは #LUKS モードの暗号化オプションを参照。

結果は次のコマンドで確認できます:

# cryptsetup luksDump device

ダンプでは暗号のヘッダー情報だけでなく、LUKS パーティションで使われるキースロットもわかります。

以下の例はデフォルトの AES 暗号を使って256ビットの XTS モードで /dev/sda1 に暗号化したルートパーティションを作成します:

# cryptsetup -s 512 luksFormat /dev/sda1
LUKS を使ってキーファイルでパーティションをフォーマット

LUKS による暗号化パーティションを新しく作成する場合、以下のように作成するときにパーティションにキーファイルを関連付けることができます:

# cryptsetup luksFormat device /path/to/mykeyfile

キーファイルを作成・管理する方法は #キーファイル を見て下さい。

デバイスマッパーで LUKS パーティションのロックを解除・マップ

LUKS パーティションを作成したら、解錠することができます。

解錠ではデバイスマッパーによってパーティションが新しいデバイス名にマッピングされます。カーネルは device が暗号化デバイスであり暗号化データを上書きしないように /dev/mapper/dm_name を使って LUKS 経由で解決する必要があることを知ります。間違って上書きしないために、設定が完了したら暗号化ヘッダーをバックアップする方法を確認してください。

暗号化された LUKS パーティションを開くには次のコマンドを実行:

# cryptsetup open --type luks device dm_name

実行するとパーティションを解錠するパスワードの入力が求められます。通常、マッピングするデバイスの名前はパーティションの機能が分かりやすいものにします。例えば、以下のコマンドは luks パーティション /dev/sda1 を解錠して cryptroot という名前のデバイスマッパーにマッピングします:

# cryptsetup open --type luks /dev/sda1 cryptroot 

パーティションをオープンしたら、ルートパーティションのデバイスアドレスはパーティション本体 (例: /dev/sda1) の代わりに /dev/mapper/cryptroot が使われます。

暗号化レイヤーの上に LVM をセットアップする場合、復号化したボリュームグループのデバイスファイルは /dev/sda1 ではなく /dev/mapper/cryptroot などとなります。それから LVM は作成されている論理ボリューム全てに名前を割り当てます (例: /dev/mapper/lvmpool-root/dev/mapper/lvmpool-swap)。

暗号化データをパーティションに書き込むときは、マッピングされたデバイス名を使ってアクセスする必要があります。例えばファイルシステムを作成するときは以下のようにします:

# mkfs -t ext4 /dev/mapper/cryptroot

/dev/mapper/cryptroot デバイスは他のパーティションと同じようにマウントできます。

luks コンテナを閉じるには、パーティションをアンマウントしてください:

# cryptsetup close cryptroot

plain モードでデバイスを暗号化

dm-crypt プレーンモード暗号化の作成とその後のアクセスはどちらも、正しい parametersで cryptsetup open アクションを使用するだけで済みます。 以下は、非ルートデバイスの2つの例で示していますが、両方をスタックすることで癖が追加されています (つまり、2番目は最初のデバイス内に作成されます) 明らかに、暗号化をスタックするとオーバーヘッドが2倍になります。 ここでの使用例は、暗号オプションの使用法の別の例を説明するためのものです。

上記の表の左側の列で説明されているように、最初のマッパーは cryptsetup のプレーンモードのデフォルトで作成されます。

# cryptsetup --type plain -v open /dev/sdaX plain1
Enter passphrase: 
Command successful.

次に、さまざまな暗号化パラメーターを使用し、(オプションの) オフセットを使用して、その中に2番目のブロックデバイスを追加し、ファイルシステムを作成してマウントします。

# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10  open /dev/mapper/plain1 plain2
Enter passphrase:
# lsblk -p
 NAME                                                     
 /dev/sda                                     
 ├─/dev/sdaX          
 │ └─/dev/mapper/plain1     
 │   └─/dev/mapper/plain2              
 ...
# mkfs -t ext2 /dev/mapper/plain2
# mount -t ext2 /dev/mapper/plain2 /mnt
# echo "This is stacked. one passphrase per foot to shoot." > /mnt/stacked.txt

We close the stack to check access works

# cryptsetup close plain2
# cryptsetup close plain1

First, let's try to open the filesystem directly:

# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/sdaX plain2
# mount -t ext2 /dev/mapper/plain2 /mnt
mount: wrong fs type, bad option, bad superblock on /dev/mapper/plain2,
      missing codepage or helper program, or other error

Why that did not work? Because the "plain2" starting block (10) is still encrypted with the cipher from "plain1". It can only be accessed via the stacked mapper. The error is arbitrary though, trying a wrong passphrase or wrong options will yield the same. For dm-crypt plain mode, the open action will not error out itself.

Trying again in correct order:

# cryptsetup close plain2    # dysfunctional mapper from previous try
# cryptsetup --type plain open /dev/sdaX plain1
Enter passphrase: 
# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/mapper/plain1 plain2
Enter passphrase:
# mount /dev/mapper/plain2 /mnt && cat /mnt/stacked.txt
This is stacked. one passphrase per foot to shoot.
# exit

dm-crypt will handle stacked encryption with some mixed modes too. For example LUKS mode could be stacked on the "plain1" mapper. Its header would then be encrypted inside "plain1" when that is closed.

Available for plain mode only is the option --shared. With it a single device can be segmented into different non-overlapping mappers. We do that in the next example, using a loopaes compatible cipher mode for "plain2" this time:

# cryptsetup --type plain --offset 0 --size 1000 open /dev/sdaX plain1
Enter passphrase:
# cryptsetup --type plain --offset 1000 --size 1000 --shared --cipher=aes-cbc-lmk --hash=sha256 open /dev/sdaX plain2
Enter passphrase:
# lsblk -p
NAME                    
dev/sdaX                    
├─/dev/sdaX               
│ ├─/dev/mapper/plain1     
│ └─/dev/mapper/plain2     
...

As the devicetree shows both reside on the same level, i.e. are not stacked and "plain2" can be opened individually.

LUKS 固有の Cryptsetup のアクション

キーの管理

LUKS パーティションには異なる8つまでのキーを定義することができます。バックアップストレージのためのアクセスキーを作成することが可能です。いわゆるキーエスクローで、あるキーは日常的に使用するために使い、他のキーはエスクローに保存して日常用のパスフレーズを忘れたときやキーファイルを喪失・破損したときにパーティションにアクセスするために使います。また、別のキースロットを使って一時的なキーを発行してユーザーにパーティションへのアクセスを許可し、後でキーを無効化したりできます。

暗号化パーティションを作成すると、初期キースロット 0 が作成されます (手動で別の数字を指定しなかった場合)。追加キースロットは 1 から 7 までの数字が割り振られます。どのキースロットを使用しているかは以下のコマンドで確認できます (<device> は LUKS ヘッダーが存在するボリュームに置き換えてください):

# cryptsetup luksDump /dev/<device> | grep BLED
Key Slot 0: ENABLED
Key Slot 1: ENABLED
Key Slot 2: ENABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED

上記のコマンドや下で説明しているコマンドはヘッダーのバックアップファイルでも使えます。

LUKS キーの追加

新しいキースロットの追加は cryptsetup の luksAddKey アクションを使うことで出来ます。デバイスが解錠されていても安全のために、新しいキーを入力する前に、必ず既存のキーの入力が求められます ("any passphrase"):

# cryptsetup luksAddKey /dev/<device> (/path/to/<additionalkeyfile>)
Enter any passphrase:
Enter new passphrase for key slot:
Verify passphrase: 

/path/to/<additionalkeyfile> を指定した場合、cryptsetup は <additionalkeyfile> の新しいキースロットを追加します。指定しなかった場合は新しいパスフレーズの入力が2回要求されます。既存のキーファイルを使って認証するには、--key-file または -d オプションの後に"既存"の <keyfile> を指定することで全てのキーファイルのキースロットが解除されます:

# cryptsetup luksAddKey /dev/<device> (/path/to/<additionalkeyfile>) -d /path/to/<keyfile>

複数のキーを使っていてキーを変更・無効化する場合、--key-slot または -S オプションでスロットを指定できます:

# cryptsetup luksAddKey /dev/<device> -S 6
Enter any passphrase: 
Enter new passphrase for key slot: 
Verify passphrase:
# cryptsetup luksDump /dev/sda8 | grep 'Slot 6'
Key Slot 6: ENABLED

例えばキーを変更したい場合:

# cryptsetup luksChangeKey /dev/<device> -S 6
Enter LUKS passphrase to be changed: 
Enter new LUKS passphrase: 

LUKS キーの削除

ヘッダーからキーを削除するアクションは3つ存在します:

  • luksRemoveKey はパスフレーズやキーファイルを指定してキーを削除します。
  • luksKillSlot は (他のキーを使って) 指定したキースロットからキーを削除します。パスフレーズを忘れたり、キーファイルを無くしたり、誰もアクセスできないようにしたい場合に有用です。
  • luksErase は全ての有効なキーを即座に削除します。
警告:
  • All above actions can be used to irrevocably delete the last active key for an encrypted device!
  • The luksErase command was added in version 1.6.4 to quickly nuke access to the device. This action will not prompt for a valid passphrase! It will not wipe the LUKS header, but all keyslots at once and you will, therefore, not be able to regain access unless you have a valid backup of the LUKS header.

For above warning it is good to know the key we want to keep is valid. An easy check is to unlock the device with the -v option, which will specify which slot it occupies:

# cryptsetup -v open /dev/<device> testcrypt
Enter passphrase for /dev/<device>: 
Key slot 1 unlocked.
Command successful.

Now we can remove the key added in the previous subsection using its passphrase:

# cryptsetup luksRemoveKey /dev/<device>
Enter LUKS passphrase to be deleted: 

If we had used the same passphrase for two keyslots, the first slot would be wiped now. Only executing it again would remove the second one.

Alternatively, we can specify the key slot:

# cryptsetup luksKillSlot /dev/<device> 6
Enter any remaining LUKS passphrase:

Note that in both cases, no confirmation was required.

# cryptsetup luksDump /dev/sda8 | grep 'Slot 6'
Key Slot 6: DISABLED

To re-iterate the warning above: If the same passphrase had been used for key slots 1 and 6, both would be gone now.

バックアップとリストア

If the header of a LUKS encrypted partition gets destroyed, you will not be able to decrypt your data. It is just as much of a dilemma as forgetting the passphrase or damaging a key-file used to unlock the partition. Damage may occur by your own fault while re-partitioning the disk later or by third-party programs misinterpreting the partition table. Therefore, having a backup of the header and storing it on another disk might be a good idea.

ノート: If the LUKS-encrypted partitions' master passphrase becomes compromised, you must revoke it on every copy of the cryptheader, even those you have backed up. Otherwise, a copy of the backed-up cryptheader that uses the compromised passphrase can be used to decrypt the associated partition. See LUKS FAQ for further details.

cryptsetup を使ってバックアップ

Cryptsetup's luksHeaderBackup action stores a binary backup of the LUKS header and keyslot area:

# cryptsetup luksHeaderBackup /dev/<device> --header-backup-file /mnt/<backup>/<file>.img

where <device> is the partition containing the LUKS volume.

ヒント: You can also back up the plaintext header into ramfs and encrypt it in example with gpg before writing to persistent backup storage by executing the following commands.
# mkdir /root/<tmp>/
# mount ramfs /root/<tmp>/ -t ramfs
# cryptsetup luksHeaderBackup /dev/<device> --header-backup-file /root/<tmp>/<file>.img
# gpg2 --recipient <User ID> --encrypt /root/<tmp>/<file>.img 
# cp /root/<tmp>/<file>.img.gpg /mnt/<backup>/
# umount /root/<tmp>
警告: Tmpfs can swap to harddisk if low on memory so it is not recommended here.

cryptsetup を使ってリストア

警告: Restoring the wrong header or restoring to an unencrypted partition will cause data loss! The action can not perform a check whether the header is actually the correct one for that particular device.

In order to evade restoring a wrong header, you can ensure it does work by using it as a remote --header first:

# cryptsetup -v --header /mnt/<backup>/<file>.img open /dev/<device> test
Key slot 0 unlocked.
Command successful.
# mount /dev/mapper/test /mnt/test && ls /mnt/test 
# umount /mnt/test 
# cryptsetup close test 

Now that the check succeeded, the restore may be performed:

# cryptsetup luksHeaderRestore /dev/<device> --header-backup-file ./mnt/<backup>/<file>.img

Now that all the keyslot areas are overwritten; only active keyslots from the backup file are available after issuing the command.

手動バックアップとリストア

The header always resides at the beginning of the device and a backup can be performed without access to cryptsetup as well. First you have to find out the payload offset of the crypted partition:

# cryptsetup luksDump /dev/<device> | grep "Payload offset"
Payload offset:	4040

Second check the sector size of the drive

# fdisk -l /dev/<device> | grep "Sector size"
Sector size (logical/physical): 512 bytes / 512 bytes

Now that you know the values, you can backup the header with a simple dd command:

# dd if=/dev/<device> of=/path/to/<file>.img bs=512 count=4040

and store it safely.

A restore can then be performed using the same values as when backing up:

# dd if=./<file>.img of=/dev/<device> bs=512 count=4040

デバイスの再暗号化

The cryptsetup package features the cryptsetup-reencrypt tool. It can be used to convert an existing unencrypted filesystem to a LUKS encrypted one (option --new) and permanently remove LUKS encryption (--decrypt) from a device. As its name suggests it can also be used to re-encrypt an existing LUKS encrypted device, though, re-encryption is not possible for a detached LUKS header or other encryption modes (e.g. plain-mode). For re-encryption it is possible to change the #LUKS モードの暗号化オプション. cryptsetup-reencrypt actions can be performed to unmounted devices only. See cryptsetup-reencrypt(8) for more information.

One application of re-encryption may be to secure the data again after a passphrase or keyfile has been compromised and one cannot be certain that no copy of the LUKS header has been obtained. For example, if only a passphrase has been shoulder-surfed but no physical/logical access to the device happened, it would be enough to change the respective passphrase/key only (#Key management).

警告: Always make sure a reliable backup is available and double-check options you specify before using the tool!

The following shows an example to encrypt an unencrypted filesystem partition and a re-encryption of an existing LUKS device.

暗号化されていないファイルシステムの暗号化

A LUKS encryption header is always stored at the beginning of the device. Since an existing filesystem will usually be allocated all partition sectors, the first step is to shrink it to make space for the LUKS header.

The default LUKS header encryption cipher requires 4096 512-byte sectors. We already checked space and keep it simple by shrinking the existing ext4 filesystem on /dev/sdaX to its current possible minimum:

# umount /mnt
# e2fsck -f /dev/sdaX
e2fsck 1.43-WIP (18-May-2015)
Pass 1: Checking inodes, blocks, and sizes
...
/dev/sda6: 12/166320 files (0.0% non-contiguous), 28783/665062 blocks
# resize2fs -M /dev/sdaX
resize2fs 1.43-WIP (18-May-2015)
Resizing the filesystem on /dev/sdaX to 26347 (4k) blocks.
The filesystem on /dev/sdaX is now 26347 (4k) blocks long.

Now we encrypt it, using the default cipher we do not have to specify it explicitly. Note there is no option (yet) to double-check the passphrase before encryption starts, be careful not to mistype:

# cryptsetup-reencrypt /dev/sdaX --new  --reduce-device-size 4096S
WARNING: this is experimental code, it can completely break your data.
Enter new passphrase: 
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed  37,6 MiB/s

After it finished, the encryption was performed to the full partition, i.e. not only the space the filesystem was shrunk to (sdaX has 2.6GiB and the CPU used in the example has no hardware AES instructions). As a final step we extend the filesystem of the now encrypted device again to occupy available space:

# cryptsetup open /dev/sdaX recrypt
Enter passphrase for /dev/sdaX: 
...
# resize2fs /dev/mapper/recrypt
resize2fs 1.43-WIP (18-May-2015)
Resizing the filesystem on /dev/mapper/recrypt to 664807 (4k) blocks.
The filesystem on /dev/mapper/recrypt is now 664807 (4k) blocks long.
# mount /dev/mapper/recrypt /mnt

and are done.

既存の LUKS パーティションの再暗号化

以下の例では既存の LUKS デバイスを最暗号化しています。

警告: Double-check you specify encryption options for cryptsetup-reencrypt correctly and never re-encrypt without a reliable backup! As of September 2015 the tool does accept invalid options and damage the LUKS header, if not used correctly!

In order to re-encrypt a device with its existing encryption options, they do not need to be specified. A simple:

# cryptsetup-reencrypt /dev/sdaX
 
WARNING: this is experimental code, it can completely break your data.
Enter passphrase for key slot 0: 
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed  36,5 MiB/s

performs it.

A possible usecase is to re-encrypt LUKS devices which have non-current encryption options. Apart from above warning on specifying options correctly, the ability to change the LUKS header may also be limited by its size. For example, if the device was initially encrypted using a CBC mode cipher and 128 bit key-size, the LUKS header will be half the size of above mentioned 4096 sectors:

# cryptsetup luksDump /dev/sdaX |grep -e "mode" -e "Payload" -e "MK bits"
Cipher mode:   	cbc-essiv:sha256
Payload offset:	2048
MK bits:       	128

While it is possible to upgrade the encryption of such a device, it is currently only feasible in two steps. First, re-encrypting with the same encryption options, but using the --reduce-device-size option to make further space for the larger LUKS header. Second, re-encypt the whole device again with the desired cipher. For this reason and the fact that a backup should be created in any case, creating a new, fresh encrypted device to restore into is always the faster option.

暗号化デバイスのリサイズ

If a storage device encrypted with dm-crypt is being cloned (with a tool like dd) to another larger device, the underlying dm-crypt device must be resized to use the whole space.

The destination device is /dev/sdX2 in this example, the whole available space adjacent to the partition will be used:

# cryptsetup luksOpen /dev/sdX2 sdX2
# cryptsetup resize sdX2

Then the underlying filesystem must be resized.

ループバックファイルシステム

暗号化したループバックデバイスを /mnt/secret にマウントしている場合、例えば Dm-crypt/root 以外のファイルシステムの暗号化#Loop デバイス に従っている場合、最初に暗号化コンテナをアンマウント:

# umount /mnt/secret
# cryptsetup close secret
# losetup -d /dev/loop0

次に、追加したいデータ容量を決めてコンテナファイルを拡張します:

警告: > を2つ使っていることに注意してください。1つだけだと現在のコンテナを上書きしてしまいます。
# dd if=/dev/urandom bs=1M count=1024 | cat - >> /bigsecret

ループデバイスにコンテナをマップ:

# losetup /dev/loop0 /bigsecret
# cryptsetup --type luks open /dev/loop0 secret

その後、コンテナの暗号化された部分をコンテナファイルの最大サイズまでリサイズ:

# cryptsetup resize secret

最後に、ファイルシステムのチェックを実行して、問題なければ、リサイズを行います (例: ext2/3/4):

# e2fsck -f /dev/mapper/secret
# resize2fs /dev/mapper/secret

またコンテナをマウントすることができます:

# mount /dev/mapper/secret /mnt/secret

キーファイル

ノート: このセクションでは平文のキーファイルを使う方法を説明しています。キーファイルを暗号化して二段階認証したい場合はGPG や OpenSSL で暗号化されたキーファイルを使うを見て下さい。ただし、このセクションもあらかじめ読むようにしてください。

キーファイルとは?

キーファイルは暗号化されたボリュームを解錠するパスフレーズとして使用するデータを含んだファイルです。ファイルを喪失した場合、ボリュームの復号化は出来なくなります。

ヒント: Define a passphrase in addition to the keyfile for backup access to encrypted volumes in the event the defined keyfile is lost or changed.

なぜキーファイルを使うのか?

キーファイルには様々なタイプがあります。以下で説明しているようにそれぞれメリットとデメリットが存在します:

キーファイルのタイプ

passphrase

単にパスフレーズが記述されたキーファイルです。メリットはファイルを無くしても暗号化ボリュームの所有者が簡単に中身を思い出せることです。デメリットはパスフレーズの入力と比べてセキュリティ上の優位点がないことです。

例: 1234

ノート: パスフレーズが記述されたキーファイルに改行を含めてはいけません。以下のようなコマンドでファイルを作成できます:
# echo -n 'your_passphrase' > /path/to/<keyfile>
# chown root:root /path/to/<keyfile>; chmod 400 /path/to/<keyfile>

randomtext

This is a keyfile containing a block of random characters. The benefit of this type of keyfile is that it is much more resistant to dictionary attacks than a simple passphrase. An additional strength of keyfiles can be utilized in this situation which is the length of data used. Since this is not a string meant to be memorized by a person for entry, it is trivial to create files containing thousands of random characters as the key. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase.

例: fjqweifj830149-57 819y4my1-38t1934yt8-91m 34co3;t8y;9p3y-

binary

This is a binary file that has been defined as a keyfile. When identifying files as candidates for a keyfile, it is recommended to choose files that are relatively static such as photos, music, video clips. The benefit of these files is that they serve a dual function which can make them harder to identify as keyfiles. Instead of having a text file with a large amount of random text, the keyfile would look like a regular image file or music clip to the casual observer. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase. Additionally, there is a theoretical loss of randomness when compared to a randomly generated text file. This is due to the fact that images, videos and music have some intrinsic relationship between neighboring bits of data that does not exist for a text file. However this is controversial and has never been exploited publicly.

例: 画像, テキスト, 動画。

ランダムな文字列でキーファイルを作成

ファイルシステムにキーファイルを保存

キーファイルの中身とサイズは任意に決められます。

以下では dd を使って2048バイトのランダムなキーファイルを生成して、/etc/mykeyfile ファイルに保存します:

# dd bs=512 count=4 if=/dev/urandom of=/etc/mykeyfile

キーファイルを外部デバイスに保存する場合、出力先を適切なディレクトリに変更します:

# dd bs=512 count=4 if=/dev/urandom of=/media/usbstick/mykeyfile
保存されたキーファイルを完全に消去

物理的なストレージデバイスに一時的なキーファイルを保存する場合、削除するときは、以下のように完全に削除してください:

# shred --remove --zero mykeyfile

FAT や ext2 であれば上記で十分ですが、ジャーナリングファイルシステムやフラッシュメモリハードウェアの場合、ディスクの完全消去あるいはキーファイルのあるパーティションを削除することを強く推奨します。

ramfs にキーファイルを保存

もしくは、ramfs をマウントしてキーファイルを一時的に保存:

# mkdir /root/myramfs
# mount ramfs /root/myramfs/ -t ramfs
# cd /root/myramfs

物理ディスクではなくメモリ上に配置することで、ramfs をアンマウントしたら復元できないという利点があります。キーファイルを他の安全なファイルシステムにコピーしたら、以下のコマンドで ramfs をアンマウントしてください:

# umount /root/myramfs

キーファイルを使用するように LUKS を設定

キーファイルのキースロットを LUKS ヘッダに追加:

# cryptsetup luksAddKey /dev/sda2 /etc/mykeyfile
Enter any LUKS passphrase:
key slot 0 unlocked.
Command successful.

キーファイルを使ってパーティションを手動でアンロック

LUKS デバイスをオープンするときに --key-file オプションを使ってください:

# cryptsetup open /dev/sda2 dm_name --key-file /etc/mykeyfile

起動時にロックを解除

ファイルシステムのキーファイルを暗号化されたルートディレクトリの中に保存することで、システムの電源が切れているときに安全を確保することができ、crypttab によって起動時に自動的にマウントを解錠することができます。UUID を使用する例:

/etc/crypttab
home    UUID=<UUID identifier>    /etc/mykeyfile

is all needed for unlocking, and

/etc/fstab
/dev/mapper/home        /home   ext4        defaults        0       2

for mounting the LUKS blockdevice with the generated keyfile.

ヒント: If you prefer to use a --plain mode blockdevice, the encryption options necessary to unlock it are specified in /etc/crypttab. Take care to apply the systemd workaround mentioned in crypttab in this case.

キーファイルを使って起動時に root パーティションのロックを解除

mkinitcpio を設定して必要なモジュールやファイルを記述して cryptkey カーネルパラメータを設定してキーファイルの場所を指定します。

2つの方法が存在します:

  1. 外部メディア (USB スティック) にキーファイルを保存
  2. initramfs にキーファイルを埋め込む

キーファイルを外部メディアに保存

mkinitcpio の設定

ドライブのファイルシステムを使うために /etc/mkinitcpio.conf にモジュールを追加してください (以下の例では vfat モジュール):

MODULES=(vfat)

In this example it is assumed that you use a FAT formatted USB drive (vfat module). Replace those module names if you use another file system on your USB stick (e.g. ext2) or another codepage. If it complains of bad superblock and bad codepage at boot, then you need an extra codepage module to be loaded. For instance, you may need nls_iso8859-1 module for iso8859-1 codepage.

If you have a non-US keyboard, it might prove useful to load your keyboard layout before you are prompted to enter the password to unlock the root partition at boot. For this, you will need the keymap hook before encrypt.

Generate a new initramfs image:

# mkinitcpio -p linux
カーネルパラメータの設定

以下のオプションをカーネルパラメータに追加してください (encrypt フックではなく sd-encrypt フックを使用する場合は Dm-crypt/システム設定#sd-encrypt フックを使うを参照):

cryptdevice=/dev/<partition1>:root cryptkey=/dev/<partition2>:<fstype>:<path>

例:

cryptdevice=/dev/sda3:root cryptkey=/dev/sdb1:vfat:/keys/secretkey

Choosing a plain filename for your key provides a bit of 'security through obscurity'. The keyfile can not be a hidden file, that means the filename must not start with a dot, or the encrypt hook will fail to find the keyfile during the boot process.

/dev/sdb1 のようなデバイスノードの名前は再起動しても同じであるとは保証されていません。udev による永続的なブロックデバイスの命名を使ったほうが確実にデバイスにアクセスできます。外部ストレージデバイスからキーファイルを読み取るときに encrypt フックが確実にキーファイルを見つけられるように、永続的なブロックデバイスの名前を絶対に使うべきです。永続的なブロックデバイスの命名を見て下さい。

キーファイルを initramfs に埋め込む

警告: Use an embedded keyfile only if you have some form of authentication mechanism beforehand that protects the keyfile sufficiently. Otherwise auto-decryption will occur, defeating completely the purpose of block device encryption.

This method allows to use a specially named keyfile that will be embedded in the initramfs and picked up by the encrypt hook to unlock the root filesystem (cryptdevice) automatically. It may be useful to apply when using the GRUB early cryptodisk feature, in order to avoid entering two passphrases during boot.

The encrypt hook lets the user specify a keyfile with the cryptkey kernel parameter: in the case of initramfs, the syntax is rootfs:path, see Dm-crypt/システム設定#cryptkey. Besides, this kernel parameter defaults to use /crypto_keyfile.bin, and if the initramfs contains a valid key with this name, decryption will occur automatically without the need to configure the cryptkey parameter.

encrypt の代わりに sd-encrypt を使用する場合、luks.key カーネルパラメータでキーファイルの場所を指定してください。Dm-crypt/システム設定#luks.key を参照。

キーファイルを生成して適切な権限を与えて LUKS キーとして追加:

# dd bs=512 count=4 if=/dev/urandom of=/crypto_keyfile.bin
# chmod 000 /crypto_keyfile.bin
# chmod 600 /boot/initramfs-linux*
# cryptsetup luksAddKey /dev/sdX# /crypto_keyfile.bin
警告: When initramfs' permissions are set to 644 (by default), then all users will be able to dump the keyfile. Make sure the permissions are still 600 if you install a new kernel.

mkinitcpio の FILES にキーを記述:

/etc/mkinitcpio.conf
FILES=(/crypto_keyfile.bin)

最後に initramfs を再生成してください。

次の起動時からコンテナを復号化するパスフレーズを入力するのは一度だけですむようになります。

詳しくは こちら を参照。