labunix's blog

labunixのラボUnix

cryptsetupでパスワードを追加する

■cryptsetupでパスワードを追加する。

 debian Wheezyにdm_cryptを導入
 http://d.hatena.ne.jp/labunix/20130516

 cryptsetupの暗号化方式について
 http://d.hatena.ne.jp/labunix/20130526


■スロットの数=8ユーザまでのパスワードを設定して使用可能。

$ sudo cryptsetup luksOpen luks.img luks
$ test -f /dev/dm-0 && sudo mount /dev/dm-0 /media/luksfs
$ ls -l /media/luksfs/
合計 14
drwxrwx---+ 3 labunix labunix  1024  529 09:11 labunix
drwx------  2 root    root    12288  516 20:11 lost+found

■スロットにパスワードの追加

$ sudo cryptsetup luksAddKey luks.img 
Enter any passphrase: 
Enter new passphrase for key slot: 
Verify passphrase: 

$ sudo cryptsetup luksDump luks.img 
LUKS header information for /dev/loop1

Version:       	1
Cipher name:   	aes
Cipher mode:   	cbc-essiv:sha256
Hash spec:     	sha1
Payload offset:	4096
MK bits:       	256
MK digest:     	54 98 43 1d a2 11 b0 0a 5b 58 7e e0 0c 3f b0 8f 1c 80 dc 60 
MK salt:       	07 34 a1 20 57 80 17 d7 24 ea ce 04 93 5d 1f 7b 
               	4c 7e 78 3e e4 d8 a4 fd 5d 22 aa 0a 2f 91 bb f6 
MK iterations: 	8875
UUID:          	417053cc-72f7-45c0-9203-2d4922d199e9

Key Slot 0: ENABLED
	Iterations:         	35996
	Salt:               	e9 75 cb e4 2a 23 40 a9 a1 4a 21 06 65 dd 16 e8 
	                      	da fd 8d 69 0c 49 a6 f8 16 4b d9 af 7e ad c8 8d 
	Key material offset:	8
	AF stripes:            	4000
Key Slot 1: ENABLED
	Iterations:         	35640
	Salt:               	44 79 88 ca 54 94 c1 c5 0b 83 25 1e 82 dc 88 19 
	                      	52 b9 e6 87 e4 52 d6 79 c9 60 f9 63 1a b9 28 cd 
	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

■luksオプションはtab補完が使える。

$ sudo cryptsetup luks
luksAddKey         luksFormat         luksOpen           luksUUID
luksClose          luksHeaderBackup   luksRemoveKey      
luksDelKey         luksHeaderRestore  luksResume         
luksDump           luksKillSlot       luksSuspend        

■パスワードの追加と削除
 Key Slot番号を指定(07までの計8)

$ sudo cryptsetup luksAddKey luks.img
Enter any passphrase: 
Enter new passphrase for key slot: 
Verify passphrase: 

$ sudo cryptsetup luksDump luks.img | grep "Slot 1"
Key Slot 1: ENABLED

udo cryptsetup luksRemoveKey -S 1 luks.img
Enter LUKS passphrase to be deleted: 

$ sudo cryptsetup luksDump luks.img | grep "Slot"
Key Slot 0: ENABLED
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

■「Key slot 0」と同じパスワードを「Key slot 1」に設定して
 0のパスワードを削除

$ sudo cryptsetup luksAddKey luks.img
Enter any passphrase: 
Enter new passphrase for key slot: 
Verify passphrase: 

$ sudo cryptsetup luksDump luks.img | grep "Slot"
Key Slot 0: ENABLED
Key Slot 1: ENABLED
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED

$ sudo cryptsetup luksRemoveKey -S 0 luks.img
Enter LUKS passphrase to be deleted: 

$ sudo cryptsetup luksDump luks.img | grep "Slot"
Key Slot 0: DISABLED
Key Slot 1: ENABLED
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED

■新しいパスワードの追加は番号の低いスロットから順に使われる。
 0が空いているのに、1が有効だからといって、2にセットされない。

$ sudo cryptsetup luksAddKey luks.img
$ sudo cryptsetup luksDump luks.img | grep "Slot.*ENABLED"
Key Slot 0: ENABLED
Key Slot 1: ENABLED

■0を空けたまま2に追加するにはslot番号を指定する。

$ sudo cryptsetup luksAddKey -S 2 luks.img
Enter any passphrase: 
Enter new passphrase for key slot: 
Verify passphrase: 

$ sudo cryptsetup luksDump luks.img | grep "Slot.*ENABLED"
Key Slot 1: ENABLED
Key Slot 2: ENABLED

■若い順にスロットが使われるが、
 若い番号を欠番とすることも出来るので、
 「Key Slot n」がnユーザのパスワードという割り振りで
 8ユーザまでの管理が出来ると言える。