LUKS(Linux Unified Key Setup)を使ってみる

LUKS(Linux Unified Key Setup)というLinuxの暗号化ファイルシステムがあるのですが、
これを使ってみたときのメモです。


今回はCentOS6.3(64bit版)で試しています。
また、「cryptsetup-luks」と「cryptsetup-luks-libs」のRPMを予めインストールしておきます。

デバイスの初期化


今回用意した環境です。/dev/sdbとして1GB用意しました。
今回はここを暗号化したファイルシステムにしてみます。

# fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a6af7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1045     7875584   8e  Linux LVM

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_root: 7021 MB, 7021264896 bytes
255 heads, 63 sectors/track, 853 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_swap: 1040 MB, 1040187392 bytes
255 heads, 63 sectors/track, 126 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


「/dev/sdb」をcryptsetupコマンドで初期化してみます。パスワードを聞かれるので入力します。

# cryptsetup luksFormat /dev/sdb

WARNING!
========
This will overwrite data on /dev/sdb irrevocably.

Are you sure? (Type uppercase yes): YES
Enter LUKS passphrase: 
Verify passphrase: 


オプションで暗号化アルゴリズムや鍵長を指定することも出来ます。(今回は指定していません)

暗号化ファイルシステムを使ってみる


初期化が終わりましたので、マウントしてみます。今回は「luks」という名前を付けました。
パスワードを入力すると、「/dev/mapper」以下に「luks」という名前のディレクトリが出現します。

# cryptsetup luksOpen /dev/sdb luks
Enter passphrase for /dev/sdb: 

# ls /dev/mapper/
VolGroup-lv_root  VolGroup-lv_swap  control  luks


後はフォーマットしてやれば普通に使えます。今回はext4でフォーマットしました。

# mkfs.ext4 /dev/mapper/luks 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65408 inodes, 261632 blocks
13081 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

# mount -t ext4 /dev/mapper/luks /mnt

# df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       6748936    722056   5684048  12% /
tmpfs                   251284         0    251284   0% /dev/shm
/dev/sda1               495844     31951    438293   7% /boot
/dev/mapper/luks       1030072     17668    960080   2% /mnt

# cat /proc/mounts
rootfs / rootfs rw 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0
devtmpfs /dev devtmpfs rw,seclabel,nosuid,relatime,size=242064k,nr_inodes=60516,mode=755 0 0
devpts /dev/pts devpts rw,seclabel,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /dev/shm tmpfs rw,seclabel,nosuid,nodev,relatime 0 0
/dev/mapper/VolGroup-lv_root / ext4 rw,seclabel,relatime,barrier=1,data=ordered 0 0
none /selinux selinuxfs rw,relatime 0 0
devtmpfs /dev devtmpfs rw,seclabel,nosuid,relatime,size=242064k,nr_inodes=60516,mode=755 0 0
/proc/bus/usb /proc/bus/usb usbfs rw,relatime 0 0
/dev/sda1 /boot ext4 rw,seclabel,relatime,barrier=1,data=ordered 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0
/dev/mapper/luks /mnt ext4 rw,seclabel,relatime,barrier=1,data=ordered 0 0


使用後、切り離すには以下のようにします。

# umount /mnt
# cryptsetup luksClose luks

パスワードの追加、削除


暗号化デバイスには複数のパスワードを追加することが出来ます。
今回は既にパスワードを一つ設定しているため、パスワード登録時に入力を求められます。

# cryptsetup luksAddKey /dev/sdb
Enter any passphrase: 
Enter new passphrase for key slot: 
Verify passphrase: 


パスワードを削除するには以下のようにします。
削除するパスワードはキースロット番号で指定します。

# cryptsetup luksKillSlot /dev/sdb 1
Enter any remaining LUKS passphrase: 

情報の確認


「luksDump」サブコマンドを使うと、各種情報を確認することが出来ます。

# cryptsetup luksDump /dev/sdb
LUKS header information for /dev/sdb

Version:        1
Cipher name:    aes
Cipher mode:    cbc-essiv:sha256
Hash spec:      sha1
Payload offset: 4096
MK bits:        256
MK digest:      ba 06 16 ac e3 ff 6c 0a 0e 45 8c 96 76 ed 2b c2 d4 7c 21 63 
MK salt:        b4 5e ed 3e ab 52 10 8c ad 2a 3d c4 2d e5 e7 01 
                eb 01 5a c9 b6 24 6c 76 ba ec 49 21 0b 1d 82 da 
MK iterations:  52750
UUID:           d2505312-69db-4249-a887-1469b8d33c1a

Key Slot 0: ENABLED
        Iterations:             211318
        Salt:                   0a 8f 5b 31 53 9d 45 f6 19 07 4f ff 07 2a 85 4e 
                                be 6c c8 6c 40 b3 ba 60 fb 8e af 5c 18 70 a3 4a 
        Key material offset:    8
        AF stripes:             4000
Key Slot 1: DISABLED
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED


また、「isLuks」というサブコマンドを使うと、指定したデバイスファイルがLUKSなパーティションかどうかを確認できます。
LUKSの場合は何も表示されないようです。

# cryptsetup isLuks /dev/sda
Device /dev/sda is not a valid LUKS device.

# cryptsetup isLuks /dev/sdb

暗号化ファイルシステムのヘッダー情報をバックアップ/リストア


ヘッダーのバックアップ/リストアも可能なようなので試してみました。
まずは現状を確認します。パスワードを2つ設定しています。

# cryptsetup luksDump /dev/sdb
LUKS header information for /dev/sdb

Version:        1
Cipher name:    aes
Cipher mode:    cbc-essiv:sha256
Hash spec:      sha1
Payload offset: 4096
MK bits:        256
MK digest:      ba 06 16 ac e3 ff 6c 0a 0e 45 8c 96 76 ed 2b c2 d4 7c 21 63 
MK salt:        b4 5e ed 3e ab 52 10 8c ad 2a 3d c4 2d e5 e7 01 
                eb 01 5a c9 b6 24 6c 76 ba ec 49 21 0b 1d 82 da 
MK iterations:  52750
UUID:           d2505312-69db-4249-a887-1469b8d33c1a

Key Slot 0: ENABLED
        Iterations:             211318
        Salt:                   0a 8f 5b 31 53 9d 45 f6 19 07 4f ff 07 2a 85 4e 
                                be 6c c8 6c 40 b3 ba 60 fb 8e af 5c 18 70 a3 4a 
        Key material offset:    8
        AF stripes:             4000
Key Slot 1: ENABLED
        Iterations:             201439
        Salt:                   79 ca 2d 03 aa 51 d9 da 04 e7 44 99 98 f1 09 85 
                                32 2f 16 e5 86 40 0b 77 ee 72 30 69 2e 38 41 70 
        Key material offset:    264
        AF stripes:             4000
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED


この状態でヘッダファイルをバックアップします。

# cryptsetup luksHeaderBackup /dev/sdb --header-backup-file /root/luks_header.img
# file /root/luks_header.img 
/root/luks_header.img: LUKS encrypted file, ver 1 [aes, cbc-essiv:sha256, sha1] UUID: d2505312-69db-4249-a887-1469b8d


キースロット1のパスワードを削除します。

# cryptsetup luksKillSlot /dev/sdb 1
Enter any remaining LUKS passphrase: 

# cryptsetup luksDump /dev/sdb
LUKS header information for /dev/sdb

Version:        1
Cipher name:    aes
Cipher mode:    cbc-essiv:sha256
Hash spec:      sha1
Payload offset: 4096
MK bits:        256
MK digest:      ba 06 16 ac e3 ff 6c 0a 0e 45 8c 96 76 ed 2b c2 d4 7c 21 63 
MK salt:        b4 5e ed 3e ab 52 10 8c ad 2a 3d c4 2d e5 e7 01 
                eb 01 5a c9 b6 24 6c 76 ba ec 49 21 0b 1d 82 da 
MK iterations:  52750
UUID:           d2505312-69db-4249-a887-1469b8d33c1a

Key Slot 0: ENABLED
        Iterations:             211318
        Salt:                   0a 8f 5b 31 53 9d 45 f6 19 07 4f ff 07 2a 85 4e 
                                be 6c c8 6c 40 b3 ba 60 fb 8e af 5c 18 70 a3 4a 
        Key material offset:    8
        AF stripes:             4000
Key Slot 1: DISABLED
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED


先ほど取得したヘッダファイルのバックアップをリストアしてみます。元に戻せたようです。

# cryptsetup luksHeaderRestore /dev/sdb --header-backup-file /root/luks_header.img

WARNING!
========
Device /dev/sdb already contains LUKS header. Replacing header will destroy existing keyslots.

Are you sure? (Type uppercase yes): YES

# cryptsetup luksDump /dev/sdb
LUKS header information for /dev/sdb

Version:        1
Cipher name:    aes
Cipher mode:    cbc-essiv:sha256
Hash spec:      sha1
Payload offset: 4096
MK bits:        256
MK digest:      ba 06 16 ac e3 ff 6c 0a 0e 45 8c 96 76 ed 2b c2 d4 7c 21 63 
MK salt:        b4 5e ed 3e ab 52 10 8c ad 2a 3d c4 2d e5 e7 01 
                eb 01 5a c9 b6 24 6c 76 ba ec 49 21 0b 1d 82 da 
MK iterations:  52750
UUID:           d2505312-69db-4249-a887-1469b8d33c1a

Key Slot 0: ENABLED
        Iterations:             211318
        Salt:                   0a 8f 5b 31 53 9d 45 f6 19 07 4f ff 07 2a 85 4e 
                                be 6c c8 6c 40 b3 ba 60 fb 8e af 5c 18 70 a3 4a 
        Key material offset:    8
        AF stripes:             4000
Key Slot 1: ENABLED
        Iterations:             201439
        Salt:                   79 ca 2d 03 aa 51 d9 da 04 e7 44 99 98 f1 09 85 
                                32 2f 16 e5 86 40 0b 77 ee 72 30 69 2e 38 41 70 
        Key material offset:    264
        AF stripes:             4000
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED


今日はこんなところで。