Difference between revisions of "Dm-crypt"

From ArchWiki
Jump to: navigation, search
(Caveats)
(Why Use Encryption?: minor formatting changes)
(13 intermediate revisions by the same user not shown)
Line 10: Line 10:
  
 
In the simplest terms encryption is a method for establishing privacy.  
 
In the simplest terms encryption is a method for establishing privacy.  
 
  
 
There are presently two approaches to partition level encryption '''data encryption''' and '''system encryption'''.
 
There are presently two approaches to partition level encryption '''data encryption''' and '''system encryption'''.
Line 16: Line 15:
  
 
'''Data encryption''', defined as encrypting a users data, provides for many benefits including:  
 
'''Data encryption''', defined as encrypting a users data, provides for many benefits including:  
 
  
 
::*Preventing unauthorized physical access to private data.
 
::*Preventing unauthorized physical access to private data.
Line 23: Line 21:
  
 
However data encryption alone has some significant drawbacks. In modern computing systems, there are many background processes that may store information about encrypted data or parts of the encrypted data itself in non-encrypted areas of the hard drive. Thus reducing the effectiveness of any data encryption system in place.
 
However data encryption alone has some significant drawbacks. In modern computing systems, there are many background processes that may store information about encrypted data or parts of the encrypted data itself in non-encrypted areas of the hard drive. Thus reducing the effectiveness of any data encryption system in place.
 
  
  
Line 37: Line 34:
  
 
::*Attempts to bypass the operating system by inserting a boot CD/USB
 
::*Attempts to bypass the operating system by inserting a boot CD/USB
::*Copying, modifying, or removing the harddisk drives when the computer is off
+
::*Copying, modifying, or removing the hard disk drives when the computer is off
  
  
Despite the use of system encryption, there are still points of physical insecurity. These issues revolve around the /boot partition which must remain unencrypted in order for the machine to properly boot. However system encryption is presently the best way to minimize the loss of data privacy by physical attempts at invasion.
+
Despite the use of system encryption, there are still points of physical insecurity. These issues revolve around the {{ic|/boot}} partition which must remain unencrypted in order for the machine to properly boot. However, system encryption is presently the best way to minimize the loss of data privacy by physical attempts at invasion.
  
  
{{Warning | Any encryption method employed is only as good as its associated key management. Partition level encryption does not protect you from all forms of security compromise. There are ways to break into computers while they are powered on that are unaffected by disk level encryption. Read the [[System Encryption with LUKS for dm-crypt#Caveats | caveats]] section below!}}
+
{{Warning|Any encryption method employed is only as good as its associated key management. Partition level encryption does not protect you from all forms of security compromise. There are ways to break into computers while they are powered on that are unaffected by disk level encryption. Read the [[System Encryption with LUKS for dm-crypt#Caveats | caveats]] section below!}}
  
 
=== What Methods are Available for System Encryption? ===
 
=== What Methods are Available for System Encryption? ===
  
There are multiple current methods that can be employed for system encryption including:
+
There are multiple current methods that can be employed for system encryption, including:
 
+
 
+
'''loop-AES''' ([http://loop-aes.sourceforge.net/ loop-AES])
+
 
+
::loop-AES is a descendant of cryptoloop and is a secure and fast solution to system encryption.
+
::However loop-AES is considered less user friendly than other options as it requires non-standard kernel support.
+
  
  
'''standard device-mapper encryption''' ([http://www.saout.de/misc/dm-crypt/ dm-crypt])
+
;loop-AES ([http://loop-aes.sourceforge.net/ loop-AES]):loop-AES is a descendant of cryptoloop and is a secure and fast solution to system encryption.
 +
:However loop-AES is considered less user-friendly than other options as it requires non-standard kernel support.
  
::This is the standard device mapper which can be used for those who like to have control over all aspects partition management.
 
  
 +
;standard device-mapper encryption ([http://www.saout.de/misc/dm-crypt/ dm-crypt]):This is the standard device mapper which can be used for those who like to have control over all aspects partition management.
  
'''LUKS for dm-crypt''' ([http://code.google.com/p/cryptsetup/ LUKS home page])
 
  
::LUKS 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.
+
;LUKS for dm-crypt ([http://code.google.com/p/cryptsetup/ LUKS]):LUKS 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.
  
::Briefly some key features that LUKS provides include:
+
:Briefly some key features that LUKS provides include:
  
:::*Support for either passphrase or keyfiles as encryption keys
+
::*Support for either passphrase or keyfiles as encryption keys
:::*Per partition key creation and revocation
+
::*Per partition key creation and revocation
:::*Multiple passphrases or keyfiles for a particular partition
+
::*Multiple passphrases or keyfiles for a particular partition
  
 
=== Caveats ===
 
=== Caveats ===
Line 76: Line 67:
 
For any type of encryption the security of your privacy is dependent on two things:
 
For any type of encryption the security of your privacy is dependent on two things:
  
::*The complexity/availability of your key
+
::*The complexity/availability of your key (see [[Wikipedia:Kerckhoffs's principle]])
 
::*The usage of a proven encryption algorithm
 
::*The usage of a proven encryption algorithm
  
  
'''Key Complexity and Availability'''
+
====Key Complexity and Availability====
  
 
The user provided key used for encryption, whether a passphrase or a keyfile, must be complex enough that is it not easy to guess. Having a strong encryption algorithm does nothing to provide privacy if the key used for encryption is too simple. The tenets of strong keys are based on length and randomness. There are many sources available with instructions on how to create strong encryption keys.  
 
The user provided key used for encryption, whether a passphrase or a keyfile, must be complex enough that is it not easy to guess. Having a strong encryption algorithm does nothing to provide privacy if the key used for encryption is too simple. The tenets of strong keys are based on length and randomness. There are many sources available with instructions on how to create strong encryption keys.  
Line 87: Line 78:
  
  
'''Encryption Algorithm'''
+
====Encryption Algorithm====
  
 
There are many peer-reviewed encryption algorithms in existence. The encryption algorithms and block ciphers used in any of the mentioned methods for applying encryption in this wiki page are considered strong algorithms that have been subjected to cryptographic review by the cryptography community.
 
There are many peer-reviewed encryption algorithms in existence. The encryption algorithms and block ciphers used in any of the mentioned methods for applying encryption in this wiki page are considered strong algorithms that have been subjected to cryptographic review by the cryptography community.
  
  
'''discard/TRIM support for solid state disks'''
+
====discard/TRIM support for solid state disks====
  
 
Solid state disk users should be aware that by default, Linux's full-disk encryption mechanisms will ''not'' forward TRIM commands from the filesystem to the underlying disk. The device-mapper maintainers have made it clear that TRIM support will never be enabled by default on dm-crypt devices because of the potential security implications; if TRIM support were enabled, an attacker may be able to tell which blocks have been used, how many blocks have been used, and other information that is exposed directly to the device when a TRIM is issued.
 
Solid state disk users should be aware that by default, Linux's full-disk encryption mechanisms will ''not'' forward TRIM commands from the filesystem to the underlying disk. The device-mapper maintainers have made it clear that TRIM support will never be enabled by default on dm-crypt devices because of the potential security implications; if TRIM support were enabled, an attacker may be able to tell which blocks have been used, how many blocks have been used, and other information that is exposed directly to the device when a TRIM is issued.
Line 98: Line 89:
 
It may be possible to determine the filesystem utilized by your encrypted device through the data that is leaked by TRIM. Furthermore, any information that may be derived by a profile of block usage may be exposed by enabling TRIM support on an encrypted device.
 
It may be possible to determine the filesystem utilized by your encrypted device through the data that is leaked by TRIM. Furthermore, any information that may be derived by a profile of block usage may be exposed by enabling TRIM support on an encrypted device.
  
As of kernel 3.1, support for dm-crypt TRIM pass-through can be toggled upon device creation or mount with dmsetup. Support for this option also exists in cryptsetup version 1.4.0 and up. This configuration has not been thoroughly tested. It is strongly advised that users who are not willing to lose data in this process wait until official userspace support is realized. A testing package of cryptsetup already supports this. You need to install cryptsetup from testing (at least cryptsetup-1.4.0-1) and append "{{ic|:allow-discards}}" to the cryptdevice option. The option should look like this then:
+
As of kernel 3.1, support for dm-crypt TRIM pass-through can be toggled upon device creation or mount with dmsetup. Support for this option also exists in cryptsetup version 1.4.0 and up. This configuration has not been thoroughly tested. It is strongly advised that users who are not willing to lose data in this process wait until official userspace support is realized. A testing package of cryptsetup already supports this. You need to install {{Pkg|cryptsetup}} from [testing] (at least cryptsetup-1.4.0-1) and append "{{ic|:allow-discards}}" to the cryptdevice option. The option should look like this then:
 
  cryptdevice=/dev/mapper/root:root:allow-discards
 
  cryptdevice=/dev/mapper/root:root:allow-discards
  
For more information, including specific commands and detail on dm-crypt TRIM pass-through, see these mailing list threads:
+
For more information, including specific commands and details on dm-crypt TRIM pass-through, see these mailing list threads:
 
  * http://article.gmane.org/gmane.linux.kernel.device-mapper.devel/14134
 
  * http://article.gmane.org/gmane.linux.kernel.device-mapper.devel/14134
 
  * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/5166
 
  * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/5166
Line 111: Line 102:
  
  
{{Note|System Encryption assume encryption of all mounted partitions this includes all partitions except for /boot - meaning that the root file system, swap partition, and all other partitions must be encrypted. If the swap, tmp, or root filesystems are unencrypted only Data Encryption level of security has been accomplished.}}
+
{{Note|System Encryption assume encryption of all mounted partitions this includes all partitions except for {{Filename|/boot}} - meaning that the root file system, swap partition, and all other partitions must be encrypted. If the swap, {{Filename|/tmp}}, or root filesystems are unencrypted, only Data Encryption level of security has been accomplished.}}
  
  
Line 122: Line 113:
  
  
'''Encryption of data partitions on the same physical disk as the system.'''
+
=====Encryption of data partitions on the same physical disk as the system=====
  
  
The most common form of data encryption is encrypting the /home partition.
+
The most common form of data encryption is encrypting the {{Filename|/home}} partition.
  
  
In cases where the encrypted data are located on the same physical disk as the system accessing the drive the privacy of data has already been decrease by orders of magnitude when compared to system encryption. The reason for this that the host operating systems employ background methods to assist the user in the access and management of their data. The problem lies in where these processes store this data which is most commonly in the unencrypted system partition.  
+
In cases where the encrypted data are located on the same physical disk as the system accessing the drive the privacy of data has already been decreased by orders of magnitude when compared to system encryption. The reason for this is that the host operating systems employ background methods to assist the user in the access and management of their data. The problem lies in where these processes store this data which is most commonly in the unencrypted system partition.  
  
For example, mlocate will scan all currently mounted file systems regularly and write the list of filenames to /var/lib/mlocate/mlocate.db, which is located in the non-encrypted root or /var partition. Thus an attacker will have a list of all filenames for that computer, even the ones on the encrypted /home partition, readily available to assist them in accessing the encrypted data present the disk.
+
For example, {{Pkg|mlocate}} will scan all currently mounted file systems regularly and write the list of filenames to {{Filename|/var/lib/mlocate/mlocate.db}}, which is located in the non-encrypted root or {{Filename|/var}} partition. Thus an attacker will have a list of all filenames for that computer, even the ones on the encrypted {{Filename|/home}} partition, readily available to assist them in accessing the encrypted data present on the disk.
  
Some have compared this to reducing the level of security from partition based encryption to filesystem level encryption like [[System_Encryption_with_eCryptfs|System Encryption with eCryptfs]].
+
Some have compared this to reducing the level of security from partition-based encryption to filesystem level encryption like [[System_Encryption_with_eCryptfs|System Encryption with eCryptfs]].
  
  
'''Encryption of data partitions on separate physical disks from the system.'''
+
=====Encryption of data partitions on separate physical disks from the system=====
  
 
Popular forms of data encryption on physically separate partitions include the encryption of removable media such as:
 
Popular forms of data encryption on physically separate partitions include the encryption of removable media such as:
  
 
::*USB Flash Drives
 
::*USB Flash Drives
::*External Harddisk Drives or Separate Internal Harddisk Drives
+
::*External Hard Disk Drives or Separate Internal Hard Disk Drives
::*CD/DVD/Blue-Ray Optical Media
+
::*CD/DVD/Blu-Ray Optical Media
 
::*Magnetic Storage Media
 
::*Magnetic Storage Media
  
  
The most important part of this form of data encryption is to remember that the encryption protects the privacy of the data that is located within the encrypted media only when it is not mounted. Data encryption does not protect the privacy of data once it is made accessible to a system. For example, attaching an encrypted USB Flash Drive, and subsequently decrypting a file for use temporarily on a non-secured system could result in remnants of that file existing on the host system in an unencrypted form.
+
The most important part of this form of data encryption is to remember that the encryption protects the privacy of the data that is located within the encrypted media only when it is not mounted. Data encryption does not protect the privacy of data once it is made accessible to a system. For example, attaching an encrypted USB flash drive, and subsequently decrypting a file for use temporarily on a non-secured system could result in remnants of that file existing on the host system in an unencrypted form.
  
 
== Initial Setup ==
 
== Initial Setup ==
Line 161: Line 152:
 
{{Warning | Encrypting a partition will erase everything currently on that partition. Please make appropriate data backups prior to starting.}}
 
{{Warning | Encrypting a partition will erase everything currently on that partition. Please make appropriate data backups prior to starting.}}
  
=== Secure Erasure of the Harddisk Drive ===
+
=== Secure Erasure of the Hard Disk Drive ===
  
Secure erasure of the harddisk drive involves overwriting the entire drive with random data.
+
Secure erasure of the hard disk drive involves overwriting the entire drive with random data.
  
{{Note|Overwriting a harddisk drive multiple times with random data serves no purpose. Data existing prior to overwriting cannot be recovered after it has been overwritten. [http://www.springerlink.com/content/408263ql11460147/ Overwriting Hard Drive Data: The Great Wiping Controversy]}}
+
{{Note|Overwriting a hard disk drive ''multiple'' times with random data serves no purpose. Data existing prior to overwriting cannot be recovered after it has been overwritten. [http://www.springerlink.com/content/408263ql11460147/ Overwriting Hard Drive Data: The Great Wiping Controversy]}}
  
  
'''Why perform secure of erasure of a drive?'''
+
====Why perform secure of erasure of a drive?====
  
There are two types of harddisk drives, new and used, both kinds should be securely overwritten. The reasoning is slightly different for each but the goal is to help ensure the privacy of data located within the encrypted partitions.
+
There are two types of hard disk drives, new and used, both kinds should be securely overwritten. The reasoning is slightly different for each but the goal is to help ensure the privacy of data located within the encrypted partitions.
  
::'''New Harddisk Drives'''
+
::'''New Hard Disk Drives'''
  
::In harddrives that have been directly purchased from a manufacturer there is no preexisting private data to protect. The problem is that there is no consistency in what is presently on the drive. Ideally the drive should be completely filled with random bits. However some drives have been overwritten completely with zeros. Therefore once the drive is used to write encrypted data it is relatively simple to identify where the encrypted data ends and the zeroed data begins compared to a drive that was written with random data before usage as an encrypted drive. Since an encrypted partition is supposed to be indistinguishable from random data, the lack of random data on a zeroed drive makes an encrypted drive an easier target for cryptanalysis.
+
::In hard drives that have been directly purchased from a manufacturer there is no preexisting private data to protect. The problem is that there is no consistency in what is presently on the drive. Ideally the drive should be completely filled with random bits. However some drives have been overwritten completely with zeros. Therefore once the drive is used to write encrypted data, it is relatively simple to identify where the encrypted data ends and the zeroed data begins compared to a drive that was written with random data before usage as an encrypted drive. Since an encrypted partition is supposed to be indistinguishable from random data, the lack of random data on a zeroed drive makes an encrypted drive an easier target for cryptanalysis.
  
::'''Used Harddisk Drives'''
+
::'''Used Hard Disk Drives'''
  
::Repartitioning or reformatting a used harddrive removes the file system structure for identifying where the original data was located while leaving the actual data intact on the drive itself. It is relatively straight forward using data tools like [http://foremost.sourceforge.net/ Foremost] to access the remnant data. Therefore harddrives should be securely overwritten with random data prior to encryption to prevent unintentional data recovery.
+
::Repartitioning or reformatting a used hard drive removes the file system structure for identifying where the original data was located while leaving the actual data intact on the drive itself. It is relatively straight forward using data tools like [http://foremost.sourceforge.net/ Foremost] to access the remnant data. Therefore hard drives should be securely overwritten with random data prior to encryption to prevent unintentional data recovery.
  
'''Overwriting a harddisk drive with random data.'''
 
  
There are two sources of random data commonly used for securely overwritting harddisk partitions.
+
====Overwriting a hard disk drive with random data====
  
::*urandom
+
There are two sources of random data commonly used for securely overwritting hard disk partitions.
 +
 
 +
::*{{Filename|/dev/urandom}}
 
::*badblocks
 
::*badblocks
  
 
+
=====Using urandom=====
'''Using urandom'''
+
  
 
  #dd if=/dev/urandom of=/dev/<drive> bs=1M
 
  #dd if=/dev/urandom of=/dev/<drive> bs=1M
  
Where <code>/dev/<drive></code> is the drive to be encrypted.
+
Where {{Filename|/dev/<drive>}} is the drive to be encrypted.
  
{{Note| Using ''/dev/urandom'' will take a long time to completely overwrite a drive with "random" data. In the strictest sense ''/dev/urandom'' is less random than ''/dev/random'' however ''/dev/random'' uses the kernel entropy pool and will halt overwriting until more input entropy once this pool has been exhausted. This makes the use of ''/dev/random'' for overwriting harddisks impractical.}}
+
{{Note| Using {{Filename|/dev/urandom}} will take a long time to completely overwrite a drive with "random" data. In the strictest sense, {{Filename|/dev/urandom}} is less random than {{Filename|/dev/random}}; however, {{Filename|/dev/random}} uses the kernel entropy pool and will halt overwriting until more input entropy once this pool has been exhausted. This makes the use of {{Filename|/dev/random}} for overwriting hard disks impractical.}}
  
{{Note| Users may also find that ''/dev/urandom'' takes an excessively long time on large drives of several hundred gigabytes or more (more than twenty-four hours). [[Frandom]] offers a faster alternative.}}
+
{{Note| Users may also find that {{Filename|/dev/urandom}} takes an excessively long time on large drives of several hundred gigabytes or more (more than twenty-four hours). [[Frandom]] offers a faster alternative.}}
  
  
'''Using badblocks'''
+
=====Using badblocks=====
  
 
  #badblocks -c 10240 -wsvt random /dev/<drive>
 
  #badblocks -c 10240 -wsvt random /dev/<drive>
  
Where <code>/dev/<drive></code> is the drive to be encrypted.
+
Where {{Filename|/dev/<drive>}} is the drive to be encrypted.
 
+
{{Note|The ''badblocks'' overwrites the drive at a much faster rate by generating data that is not truly random.}}
+
  
 +
{{Note|The {{Codeline|badblocks}} command overwrites the drive at a much faster rate by generating data that is not truly random.}}
  
{{Tip|In deciding which method to use for secure erasure of a harddisk drive remember that this will not need to be performed more than once for as long as the drive is used as an encrypted drive.}}
+
{{Tip|In deciding which method to use for secure erasure of a hard disk drive, remember that this will not need to be performed more than once for as long as the drive is used as an encrypted drive.}}
  
 
=== Partitioning ===
 
=== Partitioning ===
  
After the drive has been securely overwritten it is time to create partitions and begin setting up an encrypted system.
+
After the drive has been securely overwritten, it is time to create partitions and begin setting up an encrypted system.
  
 
There are multiple ways to create disk partitions:
 
There are multiple ways to create disk partitions:
  
 
::*Standard partitions
 
::*Standard partitions
::*LVM
+
::*[[LVM]]
::*RAID
+
::*[[RAID]]
  
 
LUKS is compatible in systems that require both LVM and/or RAID as well as with with standard primary, extended, and logical partitions.
 
LUKS is compatible in systems that require both LVM and/or RAID as well as with with standard primary, extended, and logical partitions.
  
  
'''Standard Partitions'''
+
====Standard Partitions====
  
These are the partitions that most people are familiar with. They come in 3 flavors, primary partitions, extended partitions, and logical partitions.
+
These are the partitions that most people are familiar with. They come in 3 flavors: primary partitions, extended partitions, and logical partitions.
  
::*Primary Partitions: These are the main partitions recognized by a BIOS. There can be up to 4 of these stored in the MBR.
 
  
::*Extended Partitions: These are primary partitions that also define another partition within themselves they were created to supersede the original 4 partition limit of primary partitions.
+
;Primary Partitions: These are the normal partitions recognized by the system BIOS. There can be up to 4 of these stored in the MBR.
  
::*Logical Partitions: These are the partitions that are defined within extended partitions.
+
;Extended Partitions: These are primary partitions that also define another partition within themselves. Extended partitions were created to work around the original 4 partition limit of primary partitions.
  
 +
;Logical Partitions: These are the partitions that are defined within extended partitions.
  
'''LVM: Linux Volume Manager'''
+
====LVM: Logical Volume Manager====
  
The LVM allows for creation of volume groups for systems that require complex combinations of multiple harddisk drives and partitions that are not possible with standard partitions. The LVM is covered in detail in the [[LVM | Arch LVM Wiki Page]] which is suggested reading prior to continuing with the instructions on setting up LUKS with LVM located below.
+
The LVM allows for creation of volume groups for systems that require complex combinations of multiple hard disk drives and partitions that are not possible with standard partitions. LVM is covered in detail in the [[LVM|Arch Linux LVM Wiki Article]] which is recommended reading prior to continuing with the instructions on setting up LUKS with LVM located below.
  
  
'''How does LVM fit into the overall system?'''
+
====How does LVM fit into the overall system?====
  
There is a growing preference towards logical volume management of LUKS encrypted physical media (LVM on LUKs). It is possible there may exist usage scenarios where encrypting logical volumes rather than physical disks is required (LUKs on LVM). However the deployment of LVM on LUKS is considered much more generalizable. One reason for this is that using LUKS as the lowest level of infrastructure most closely approximates the deployment of physical disks with built-in hardware encryption. In which case, logical volume management would be layered on top of the hardware encryption - usage of LUKS would be superfluous.
+
There is a growing preference towards logical volume management of LUKS encrypted physical media (LVM on LUKS). It is possible there may exist usage scenarios where encrypting logical volumes rather than physical disks is required (LUKS on LVM). However, the deployment of LVM on LUKS is considered much more generalizable. One reason for this is that using LUKS as the lowest level of infrastructure most closely approximates the deployment of physical disks with built-in hardware encryption. In which case, logical volume management would be layered on top of the hardware encryption -- usage of LUKS would be superfluous.
  
  
Line 256: Line 246:
 
::A root file system
 
::A root file system
  
:::*'''/'''
+
:::*{{Filename|'''/'''}}
:::*Will be encrypted and store all system and user files (/usr, /bin, /var, /home, etc.)
+
:::*Will be encrypted and store all system and user files ({{Filename|/usr}}, {{Filename|/bin}}, {{Filename|/var}}, {{Filename|/home}}, etc.)
  
 
::An initial boot partition
 
::An initial boot partition
  
:::*'''/boot'''
+
:::*{{Filename|'''/boot'''}}
:::*Will ''not'' be encrypted; the bootloader needs to access the /boot directory where it will load the initrd/encryption modules needed to load the rest of the system (which ''is'' encrypted). For this reason /boot needs to reside on its own, unencrypted, partition.
+
:::*Will ''not'' be encrypted; the bootloader needs to access the {{Filename|/boot}} directory where it will load the initramfs/encryption modules needed to load the rest of the system which ''is'' encrypted (see [[Mkinitcpio]] for details). For this reason, {{Filename|/boot}} needs to reside on its own, unencrypted partition.
  
  
Line 268: Line 258:
  
  
'''Single Disk Systems'''
+
=====Single Disk Systems=====
  
Depending on the system demands, there may be additional partitions desired. These partitions can be individually created at this level by defining separate primary or extended/logical partitions. However if LVM is to be employed, the space unoccupied by /boot and swap should be defined as single large partition which will be divided up later at the logical volume manager level.
+
Depending on the system demands, there may be additional partitions desired. These partitions can be individually created at this level by defining separate primary or extended/logical partitions. However, if LVM is to be used, the space unoccupied by {{Filename|/boot}} and swap should be defined as single large partition which will be divided up later at the LVM level.
  
  
'''Multiple Disk Systems'''
+
=====Multiple Disk Systems=====
  
In systems that will have multiple harddisk drives, the same options exist as a single disk system. After the creation of the /boot and swap partitions, the remaining free space on physical disks can divided up into their respective partitions at this level, or large partitions can define all free space per physical disk with intent to partition them within the LVM.
+
In systems that will have multiple hard disk drives, the same options exist as a single disk system. After the creation of the {{Filename|/boot}} and swap partitions, the remaining free space on physical disks can divided up into their respective partitions at this level, or large partitions can define all free space per physical disk with intent to partition them within the LVM.
  
 
== Configuring LUKS ==
 
== Configuring LUKS ==
  
Creating LUKS partitions with a passphrase is supported by the /arch/setup program.  
+
Creating LUKS partitions with a passphrase is supported by the {{ic|/arch/setup}} program.  
  
 
This section of the Wiki will cover how to manually utilize LUKS from the command line to encrypt a system.  
 
This section of the Wiki will cover how to manually utilize LUKS from the command line to encrypt a system.  
  
The steps for accomplishing this through the graphical installer are very similar and can be located in dialogue for manual configuration of the hard drive.
+
The steps for accomplishing this through the graphical installer are very similar and can be located in the dialogue for manual configuration of the hard drive.
  
  
Line 306: Line 296:
 
Cryptsetup is used to interface with LUKS for formatting, mounting and unmounting encrypted partitions.
 
Cryptsetup is used to interface with LUKS for formatting, mounting and unmounting encrypted partitions.
  
A full list of options cryptsetup accepts can be found in the [[http://www.linuxcommand.org/man_pages/cryptsetup8.html Cryptsetup Manpage]]
+
A full list of options {{ic|cryptsetup}} accepts can be found in the [[http://www.linuxcommand.org/man_pages/cryptsetup8.html Cryptsetup Manpage]]
  
 
The options used here are:
 
The options used here are:
  
::-c defines the cipher type
+
::*{{ic|-c}} defines the cipher type
::-y prompts for password confirmation on password creation
+
::*{{ic|-y}} prompts for password confirmation on password creation
::-s defines the key size
+
::*{{ic|-s}} defines the key size
  
 
::luksFormat addresses the LUKS extensions built into cryptsetup.
 
::luksFormat addresses the LUKS extensions built into cryptsetup.
  
  
In the following examples for creating LUKS partitions we will use AES with XTS, at present this is most generally used preferred cipher.
+
In the following examples for creating LUKS partitions, we will use the AES cipher in XTS mode, at present this is most generally used preferred cipher.
Other ciphers can be used with cryptsetup and details about them can be found [[http://en.wikipedia.org/wiki/Block_cipher Wikipedia: BlockCipher]]
+
Other ciphers can be used with cryptsetup, and details about them can be found here: [[Wikipedia:Block_cipher]]
  
  
 
'''Formatting LUKS Partitions'''
 
'''Formatting LUKS Partitions'''
  
First of all make sure the device mapper kernel module is loaded by executing the following:
+
First of all make sure the device mapper kernel module is loaded by executing the following: {{ic|# modprobe dm_mod}}
 
+
# modprobe dm_mod
+
  
 
In order to format a desired partition as an encrypted LUKS partition execute:
 
In order to format a desired 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>}}
  
# cryptsetup -c <cipher> -y -s <key size> luksFormat /dev/<partition name>
 
Enter passphrase: <password>
 
Verify passphrase: <password>
 
  
 +
This should be repeated for all partitions except for {{ic|/boot}} and possibly swap.
  
This should be repeated for all partitions except for /boot and possibly swap.
 
  
 +
The example below will create an encrypted root partition using the AES cipher in XTS mode (generally referred to as ''XTS-AES'').
 +
{{bc|cryptsetup -c aes-xts-plain -y -s 512 luksFormat /dev/sda2}}
  
Example:
 
  
::*cryptsetup -c aes-xts-plain -y -s 512 luksFormat /dev/sda2
+
{{Note|If hibernation usage is planned, swap must be encrypted in this fashion; otherwise, if hibernation is not a planned feature for the system, encrypting the swap file will be performed in a alternative manner.}}
:::Will create and encrypted root partition using the AES cipher
+
  
  
{{Note| If hibernation usage is planned, swap must be encrypted in this fashion. Otherwise if hibernation is not a planned feature for the system, encrypting the swap file will be performed in a alternative manner.}}
+
{{Warning|Irrespective of the chosen partitioning method, the {{ic|/boot}} partition must remain separate and unencrypted in order to load the kernel and boot the system.}}
 
+
 
+
{{Warning |Irrespective of the chosen partitioning method, the /boot partition must remain separate and unencrypted in order to load the kernel and boot the system.}}
+
  
  
Line 353: Line 338:
 
Once the LUKS partitions have been created it is time to unlock them.
 
Once the LUKS partitions have been created it is time to unlock them.
  
The unlocking process will map the partitions to a new device name using the device mapper. This alerts the kernel that /dev/<partition name> is actually an encrypted device and should be addressed through LUKS using the /dev/mapper/<name> so as not to overwrite the encrypted data.  
+
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.  
  
  
 
In order to open an encrypted LUKS partition execute:
 
In order to open an encrypted LUKS partition execute:
 
+
{{hc|# cryptsetup luksOpen /dev/<partition name> <device-mapper name>|
# cryptsetup luksOpen /dev/<partition name> <device-mapper name>
+
Enter any LUKS passphrase: <password>
Enter any LUKS passphrase: <password>
+
key slot 0 unlocked.
key slot 0 unlocked.
+
Command successful.}}
Command successful.
+
  
  
Line 368: Line 352:
  
 
::*cryptsetup luksOpen /dev/sda2 '''swap'''
 
::*cryptsetup luksOpen /dev/sda2 '''swap'''
::::Once opened the swap partition device address would be '''/dev/mapper/swap''' instead of /dev/sda2
+
::::Once opened, the swap partition device address would be '''{{ic|/dev/mapper/swap}}''' instead of {{ic|/dev/sda2}}.
  
  
 
::*cryptsetup luksOpen /dev/sda3 '''root'''
 
::*cryptsetup luksOpen /dev/sda3 '''root'''
::::Once opened the root partition device address would be '''/dev/mapper/root''' instead of /dev/sda3
+
::::Once opened, the root partition device address would be '''{{ic|/dev/mapper/root}}''' instead of {{ic|/dev/sda3}}.
  
  
{{Note| Since /boot is not encrypted it does not need a device mapped name and will be addressed as /dev/sda1.}}
+
{{Note|Since {{ic|/boot}} is not encrypted, it does not need a device mapped name and will be addressed as {{ic|/dev/sda1}}.}}
  
  
{{Warning| In order to write encrypted data into the partition it must be accessed through the device mapped name.}}
+
{{Warning|In order to write encrypted data into the partition it must be accessed through the device mapped name.}}
  
 
==== Using LUKS to Format Partitions with a Keyfile ====
 
==== Using LUKS to Format Partitions with a Keyfile ====
Line 389: Line 373:
  
  
{{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.}}
  
  
Line 398: Line 382:
  
 
:'''keyfile.passphrase:'''
 
:'''keyfile.passphrase:'''
::this is my passphrase I would have typed during boot but I've placed in a file instead
+
::this is my passphrase I would have typed during boot but I have placed it in a file instead
  
  
Line 422: Line 406:
  
  
Here dd is used to generate a keyfile of 2048bits of random characters.
+
Here {{ic|dd}} is used to generate a keyfile of 2048 bits of random characters.
  
  
Line 428: Line 412:
  
  
The usage of dd is similar to initially wiping the volume with random data prior to encryption. While badblocks may also be used, most key files are on the order of a few kilobytes and there is no noticable speed difference between dd, or badblocks.
+
The usage of {{ic|dd}} is similar to initially wiping the volume with random data prior to encryption. While badblocks may also be used, most key files are on the order of a few kilobytes and there is no noticable speed difference between dd, or badblocks.
  
  
Line 435: Line 419:
  
  
When creating a new LUKS encrypted partition a keyfile may be associated with the partition on its creation using:
+
When creating a new LUKS encrypted partition, a keyfile may be associated with the partition on its creation using:
  
  
Line 452: Line 436:
 
Additional keys will occupy slots 1 - 7.
 
Additional keys will occupy slots 1 - 7.
  
The addition of new keys to an encrypted partition is accomplished using cryptsetup with luksAddKey extension.
+
The addition of new keys to an encrypted partition is accomplished using cryptsetup with the {{ic|luksAddKey}} extension.
  
  
Line 458: Line 442:
  
  
Where /dev/<encrypted volume> is the volume that is to have the new key associated with it.
+
Where {{ic|/dev/<encrypted volume>}} is the volume that is to have the new key associated with it.
  
If the bolded area is present cryptsetup will look for the keyfile defined at that location to associate with the encrypted volume specified.
+
If the bolded area is present, cryptsetup will look for the keyfile defined at that location to associate with the encrypted volume specified.
  
 
=== Storing the Key File ===
 
=== Storing the Key File ===
Line 468: Line 452:
  
 
===== Preparation for permanent device names =====
 
===== Preparation for permanent device names =====
For reading the file from an USB stick it's important to access it through a permanent device name.
+
For reading the file from an USB stick it is important to access it through a permanent device name.
The numbering of the normal device names e.g. {{Codeline|/dev/sdb1}} is somewhat arbitrary and depends on how many storage devices are attached and in what order etc.
+
The numbering of the normal device names e.g. {{ic|/dev/sdb1}} is somewhat arbitrary and depends on how many storage devices are attached and in what order, etc.
So in order to assure that the ''encrypt'' HOOK in the initcpio finds your keyfile you have to use a permanent device name.  
+
So in order to assure that the {{ic|encrypt}} HOOK in the initcpio finds your keyfile, you must use a permanent device name.  
  
 
===== Quick method =====
 
===== Quick method =====
A quick method (as opposed to setting up a udev rule) for doing so involves referencing your removable device by its label (or UUID). To find your label or UUID, plug in your USB drive and run  
+
A quick method (as opposed to setting up a [[udev]] rule) for doing so involves referencing your removable device by its label (or UUID). To find your label or UUID, plug in your USB drive and run the following:
# ls -l /dev/disk/by-label/
+
 
lrwxrwxrwx 1 root root 10 12. Feb 10:11 Keys -> ../../sdb1
+
{{hc|# ls -l /dev/disk/by-label/|
 +
lrwxrwxrwx 1 root root 10 12. Feb 10:11 Keys -> ../../sdb1}}
 +
 
 
or
 
or
# ls -l /dev/disk/by-uuid/
 
lrwxrwxrwx 1 root root 10 12. Feb 10:11 4803-8A7B -> ../../sdb1
 
  
In this case I labeled the vfat partition on my USB drive as "Keys" so my device is always symlinked in /dev/disk/by-label/Keys, or If I had wanted to use the UUID I would find /dev/disk/by-uuid/4803-8A7B. This allows me to have a consistent naming of my USB devices regardless of the order they are plugged into the system. These device names can be used in the "cryptkey" kernel option or any where else. Filesystem UUIDs are stored in the filesystem itself, meaning that the UUID will be the same if you plug it into any other computer, and that a dd backup of it will always have the same UUID since dd does a bitwise copy.
+
{{hc|# ls -l /dev/disk/by-uuid/|
 +
lrwxrwxrwx 1 root root 10 12. Feb 10:11 4803-8A7B -> ../../sdb1}}
  
'''Note:''' If you plan to store the keyfile between [[#Storing_the_key_between_MBR_and_1st_partition|MBR and the 1st partition]] you '''cannot use this method''', since it only allows access to the partitions ({{Codeline|sdb1}},{{Codeline|sdb2}},...) but not to the usb device ({{Codeline|sdb}}) itself.
+
In this case, I labeled the vfat partition on my USB drive as "Keys" so my device is always symlinked in {{ic|/dev/disk/by-label/Keys}}, or if I had wanted to use the UUID I would find {{ic|/dev/disk/by-uuid/4803-8A7B}}. This allows me to have a consistent naming of my USB devices regardless of the order they are plugged into the system. These device names can be used in the "cryptkey" kernel option or any where else. Filesystem UUIDs are stored in the filesystem itself, meaning that the UUID will be the same if you plug it into any other computer, and that a dd backup of it will always have the same UUID since dd does a bitwise copy.
Create a UDEV rule instead as described in the following section.
+
  
==== Using UDEV ====
+
{{Note|If you plan to store the keyfile between [[#Storing_the_key_between_MBR_and_1st_partition|MBR and the 1st partition]] you '''cannot use this method''', since it only allows access to the partitions ({{ic|sdb1}}, {{ic|sdb2}}, ...) but not to the USB device ({{ic|sdb}}) itself.
Optionally you may choose to set up your stick with an udev rule. There's some documentation in the Arch wiki about that already, if you want more in-depth, structural info, read [http://reactivated.net/writing_udev_rules.html this guide]. Here's quickly how it goes.
+
Create a udev rule instead as described in the following section.}}
  
Get the serial number from your USB stick:
+
==== Using udev ====
 +
Optionally you may choose to set up your flash drive with a [[udev]] rule. There is some documentation in the Arch wiki about that already; if you want more in-depth, structural info, read [http://reactivated.net/writing_udev_rules.html this guide]. Here is quickly how it goes.
 +
 
 +
Get the serial number from your USB flash drive:
 
  lsusb -v | grep -A 5 Vendor
 
  lsusb -v | grep -A 5 Vendor
Create a udev-rule for it:
+
 
  echo 'KERNEL=="sd*", ATTRS{serial}=="$SERIAL", SYMLINK+="$SYMLINK%n"' > /etc/udev/rules.d/8-usbstick.rules
+
Create a udev rule for it by adding the following to a file in {{ic|/etc/udev/rules.d/}}, such as {{ic|8-usbstick.rules}}:
Replace $SYMLINK and $SERIAL with their respective values. %n will expand to the partition (just like sda is subdivided into sda1, sda2, ...). You do not need to go with the 'serial' attribute, if you have a custom rule of your own, you can put it in as well (e.g. using the vendor name).  
+
  KERNEL=="sd*", ATTRS{serial}=="$SERIAL", SYMLINK+="$SYMLINK%n"
 +
 
 +
Replace {{ic|$SYMLINK}} and {{ic|$SERIAL}} with their respective values. {{ic|%n}} will expand to the partition (just like sda is subdivided into sda1, sda2, ...). You do not need to go with the 'serial' attribute. If you have a custom rule of your own, you can put it in as well (e.g. using the vendor name).
  
 
Rescan your sysfs:
 
Rescan your sysfs:
 
  udevadm trigger
 
  udevadm trigger
Now check the contents of dev:
+
Now check the contents of {{ic|/dev}}:
 
  ls /dev
 
  ls /dev
It should show your device with your desired name.  
+
It should show your device with your desired name.
  
 
==== Generating the keyfile ====
 
==== Generating the keyfile ====
Line 507: Line 496:
 
The advantage is that it resides in RAM and not on a physical disk, so after unmounting your keyfile is securly gone.
 
The advantage is that it resides in RAM and not on a physical disk, so after unmounting your keyfile is securly gone.
 
So copy your keyfile to some place you consider as secure before unmounting.
 
So copy your keyfile to some place you consider as secure before unmounting.
If you are planning to store the keyfile as a plain file on your USB device, you can also simply execute the following command in the corresponding directory, e.g. {{Codeline|/media/sdb1}}
+
If you are planning to store the keyfile as a plain file on your USB device, you can also simply execute the following command in the corresponding directory, e.g. {{ic|/media/sdb1}}
  
The keyfile can be of arbitrary content and size. We'll generate a random temporary keyfile of 2048 bytes:
+
The keyfile can be of arbitrary content and size. We will generate a random temporary keyfile of 2048 bytes:
 
  # dd if=/dev/urandom of=secretkey bs=512 count=4
 
  # dd if=/dev/urandom of=secretkey bs=512 count=4
  
Line 525: Line 514:
 
==== Storing the keyfile ====
 
==== Storing the keyfile ====
 
To store the key file, you have two options. The first is less risky than the other, but perhaps a bit more secure (if you consider security by obscurity as more secure).
 
To store the key file, you have two options. The first is less risky than the other, but perhaps a bit more secure (if you consider security by obscurity as more secure).
In any case you have to do some further configuration, if not already done above
+
In any case you have to do some further configuration, if not already done above.
  
 
==== Configuration of initcpio ====
 
==== Configuration of initcpio ====
You have to add two extra modules in your /etc/mkinitcpio.conf, one for the stick's file system and one for the codepage. Further if you created a udev-rule you should tell mkinitcpio about it:
+
You have to add two extra modules in your {{ic|/etc/mkinitcpio.conf}}, one for the drive's file system and one for the codepage. Further if you created a udev rule, you should tell {{ic|mkinitcpio}} about it:
 
  MODULES="ata_generic ata_piix nls_cp437 vfat"
 
  MODULES="ata_generic ata_piix nls_cp437 vfat"
 
  FILES="/etc/udev/rules.d/8-usbstick.rules"
 
  FILES="/etc/udev/rules.d/8-usbstick.rules"
In this example it's assumed, that you use a FAT formated stick. Replace those module names if you use another file system on your USB stick (e.g. ext2) or another codepage. Users running the stock Arch kernel should stick to the codepage mentioned here.
+
In this example it is assumed that you use a FAT formatted USB drive. Replace those module names if you use another file system on your USB stick (e.g. ext2) or another codepage. Users running the stock Arch kernel should stick to the codepage mentioned here.
  
In addition insert the ''usb'' hook somewhere before the ''encrypt'' hook.
+
Additionally, insert the {{ic|usb}} hook somewhere before the {{ic|encrypt}} hook.
 
  HOOKS="... '''usb''' encrypt ... filesystems ..."
 
  HOOKS="... '''usb''' encrypt ... filesystems ..."
  
Line 542: Line 531:
 
Be sure to choose a plain name for your key - a bit of 'security through obscurity' is always nice ;-). Avoid using dots (hidden files) and similar characters - the encrypt hook will fail to find the keyfile during the boot process.
 
Be sure to choose a plain name for your key - a bit of 'security through obscurity' is always nice ;-). Avoid using dots (hidden files) and similar characters - the encrypt hook will fail to find the keyfile during the boot process.
  
You have to add a kernel parameter in your menu.lst (grub), it should look something like this:
+
You have to add a kernel parameter in your {{ic|/boot/grub/menu.lst}} (GRUB), it should look something like this:
 
  kernel /vmlinuz26 root=/dev/hda3 ro vga=791 cryptkey=/dev/usbstick:vfat:/secretkey
 
  kernel /vmlinuz26 root=/dev/hda3 ro vga=791 cryptkey=/dev/usbstick:vfat:/secretkey
This assumes {{Codeline|/dev/usbstick}} is the FAT partition of your choice. Replace it by {{Codeline|/dev/disk/by-...}} or whatever your device is.
+
This assumes {{ic|/dev/usbstick}} is the FAT partition of your choice. Replace it with {{ic|/dev/disk/by-...}} or whatever your device is.
  
 
That's all, reboot and have fun!
 
That's all, reboot and have fun!
  
 
==== Storing the key between MBR and 1st partition ====
 
==== Storing the key between MBR and 1st partition ====
We'll write the key directly between MBR and first partition.
+
We will write the key directly between MBR and first partition.
  
'''WARNING:''' you should only follow this step if you know what you are doing - <b>it can cause data loss and damage your partitions or MBR on the stick!</b>
+
{{Warning|You should only follow this step if you know what you are doing -- '''it can cause data loss and damage your partitions or MBR on the stick!'''}}
  
 
If you have a bootloader installed on your drive you have to adjust the values. E.g. Grub needs the first 16 sectors (actually, it depends on the type of the file system, so do not rely on this too much), you would have to replace seek=4 with seek=16; otherwise you would overwrite parts of your Grub installation. When in doubt, take a look at the first 64 sectors of your drive and decide on your own where to place your key.  
 
If you have a bootloader installed on your drive you have to adjust the values. E.g. Grub needs the first 16 sectors (actually, it depends on the type of the file system, so do not rely on this too much), you would have to replace seek=4 with seek=16; otherwise you would overwrite parts of your Grub installation. When in doubt, take a look at the first 64 sectors of your drive and decide on your own where to place your key.  
  
<i>Optional</i>
+
''Optional''
 
If you do not know if you've got enough free space before the first partition you can do
 
If you do not know if you've got enough free space before the first partition you can do
 
  dd if=/dev/usbstick of=64sectors bs=512 count=64  # gives you copy of your first 64 sectors
 
  dd if=/dev/usbstick of=64sectors bs=512 count=64  # gives you copy of your first 64 sectors
Line 567: Line 556:
 
You should not simply use rm as the keyfile would only be unlinked from your filesystem and be left physically intact.
 
You should not simply use rm as the keyfile would only be unlinked from your filesystem and be left physically intact.
  
Now you have to add a kernel parameter in your menu.lst (Grub), it should look something like this:
+
Now you have to add a kernel parameter in your {{ic|/boot/grub/menu.lst}} file (GRUB); it should look something like this:
 
  kernel /vmlinuz26 root=/dev/hda3 ro vga=791 cryptkey=/dev/usbstick:2048:2048
 
  kernel /vmlinuz26 root=/dev/hda3 ro vga=791 cryptkey=/dev/usbstick:2048:2048
Format for the cryptkey option:
+
Format for the {{ic|cryptkey}} option:
 
  cryptkey=BLOCKDEVICE:OFFSET:SIZE
 
  cryptkey=BLOCKDEVICE:OFFSET:SIZE
 
OFFSET and SIZE match in this example, but this is coincidence - they can differ (and often will). An other possible example could be
 
OFFSET and SIZE match in this example, but this is coincidence - they can differ (and often will). An other possible example could be
Line 590: Line 579:
  
  
This command adds the following swap partition details to /mnt/etc/crypttab:
+
This command adds the following swap partition details to {{ic|/mnt/etc/crypttab}}:
  
::*'echo' and '>> /mnt/etc/crypttab' adds the command to the file /mnt/etc/crypttab directly
+
::*'echo' and '>> /mnt/etc/crypttab' adds the command to the file {{ic|/mnt/etc/crypttab}} directly
 
::*SWAP identifies the partition as a swap partition
 
::*SWAP identifies the partition as a swap partition
::*-c defines a cipher
+
::*{{ic|-c}} defines a cipher
::*-h defines a hash algorithm
+
::*{{ic|-h}} defines a hash algorithm
::*-s defines the key size
+
::*{{ic|-s}} defines the key size
  
  
Example (/dev/sda2 is the physical swap partition and /dev/mapper/swapmapper is the device-mapper):
+
Example ({{ic|/dev/sda2}} is the physical swap partition and {{ic|/dev/mapper/swapmapper}} is the device-mapper):
  
 
::*echo swapmapper /dev/sda2 SWAP "-c aes-xts-plain -h whirlpool -s 512" >> /mnt/etc/crypttab
 
::*echo swapmapper /dev/sda2 SWAP "-c aes-xts-plain -h whirlpool -s 512" >> /mnt/etc/crypttab
:::Maps /dev/sda2 to /dev/mapper/swapmapper as a swap partition which is encrypted by AES with Whirlpool as the hash algorithm.
+
:::Maps {{ic|/dev/sda2}} to {{ic|/dev/mapper/swapmapper}} as a swap partition which is encrypted by AES with Whirlpool as the hash algorithm.
  
  
There are many hash algorithms that can be employed, for further details [[http://en.wikipedia.org/wiki/Cryptographic_hash_function Cryptographic Hash Functions]].  
+
There are many hash algorithms that can be employed. For further details, read [[Wikipedia:Cryptographic_hash_function]].  
  
  
{{Tip | Many people prefer Whirlpool as it is patent free. On modern hardware there is minimal performance difference between algorithms.}}
+
{{Tip|Many people prefer Whirlpool as it is patent free. On modern hardware there is minimal performance difference between algorithms.}}
  
  
Line 616: Line 605:
  
 
==== Using UUIDs with encrypted swap partitions ====
 
==== Using UUIDs with encrypted swap partitions ====
If there are multiple hard drives installed in the system, their naming order (sda, sdb,...) can occasionally be scrambled upon boot. When /etc/rc.sysinit parses the /etc/crypttab file and sees the SWAP line, it will create a new dm-crypt mapping backed by that device partition and initialize encrypted swap space on top of it. This operation destroys all data on the partition, which can be catastrophic if the disk names have been switched upon reboot. To prevent such mistakes on multi-drive systems, always reference disk partitions using UUIDs rather than device names. /etc/rc.sysinit will refuse to overwrite any known file system, partition table, RAID/LVM volume or valid LUKS partition.
+
If there are multiple hard drives installed in the system, their naming order (sda, sdb,...) can occasionally be scrambled upon boot. When {{ic|/etc/rc.sysinit}} parses the {{ic|/etc/crypttab}} file and sees the SWAP line, it will create a new dm-crypt mapping backed by that device partition and initialize encrypted swap space on top of it. This operation destroys all data on the partition, which can be catastrophic if the disk names have been switched upon reboot. To prevent such mistakes on multi-drive systems, always reference disk partitions using UUIDs rather than device names. {{ic|/etc/rc.sysinit}} will refuse to overwrite any known file system, partition table, RAID/LVM volume or valid LUKS partition.
  
 
Create a regular swap partition with a new UUID:
 
Create a regular swap partition with a new UUID:
 
  # mkswap /dev/sdaX  # where X is your swap partition number  
 
  # mkswap /dev/sdaX  # where X is your swap partition number  
Note the new random UUID and edit /etc/crypttab
+
Note the new random UUID and edit {{ic|/etc/crypttab}}
 
  swap  /dev/disk/by-uuid/<your UUID>  SWAP  --offset 8 -c aes-xts-plain -h whirlpool -s 512
 
  swap  /dev/disk/by-uuid/<your UUID>  SWAP  --offset 8 -c aes-xts-plain -h whirlpool -s 512
The --offset parameter will prevent the UUID (stored within the first eight 512 byte blocks) from being overwritten by cryptsetup each time new encrypted swap space is initialized.  
+
The {{ic|--offset}} parameter will prevent the UUID (stored within the first eight 512 byte blocks) from being overwritten by cryptsetup each time new encrypted swap space is initialized.  
  
Edit your /etc/rc.sysinit, find the following lines
+
Edit your {{ic|/etc/rc.sysinit}}, find the following lines
 
<pre>
 
<pre>
 
/sbin/blkid -p "$2" &>/dev/null
 
/sbin/blkid -p "$2" &>/dev/null
Line 643: Line 632:
 
</pre>
 
</pre>
  
You can now use /dev/mapper/swap as swap device in /etc/fstab.
+
You can now use {{ic|/dev/mapper/swap}} as swap device in {{ic|/etc/fstab}}.
  
 
An additional advantage of this method is that the swap partition can be shared among several operating systems, in encrypted and unencrypted forms.
 
An additional advantage of this method is that the swap partition can be shared among several operating systems, in encrypted and unencrypted forms.
  
 
==== Encrypted swap with suspend-to-disk support ====
 
==== Encrypted swap with suspend-to-disk support ====
{{Warning | Don't use this setup with a key file, please read about the issue reported [[Talk:System_Encryption_with_LUKS_for_dm-crypt#Suspend_to_disk_instructions_are_insecure|here]]}}
+
{{Warning|Do not use this setup with a key file, please read about the issue reported [[Talk:System_Encryption_with_LUKS_for_dm-crypt#Suspend_to_disk_instructions_are_insecure|here]]}}
  
To be able to resume after suspending the computer to disk (hibernate), it is required to keep the swap filesystem intact. Therefore, it is required to have a presistent LUKS swap partition, which can be stored on the disk or input manually at startup. Because the resume takes place before the crypttab can be used, it is required to create a hook in mkinitcpio.conf to open the swap LUKS device before resuming. The following setup has the disadvantage of having to insert a key manually for the swap partition.
+
To be able to resume after suspending the computer to disk (hibernate), it is required to keep the swap filesystem intact. Therefore, it is required to have a presistent LUKS swap partition, which can be stored on the disk or input manually at startup. Because the resume takes place before the crypttab can be used, it is required to create a hook in {{ic|/etc/mkinitcpio.conf}} to open the swap LUKS device before resuming. The following setup has the disadvantage of having to insert a key manually for the swap partition.
  
 
If you want to use a partition which is currently used by the system, you have to disable it, first:
 
If you want to use a partition which is currently used by the system, you have to disable it, first:
 
  # swapoff /dev/<device>
 
  # swapoff /dev/<device>
 
To create the swap partition, follow steps similar to those described in [[#Mapping_partitions | mapping partitions]] above.<br><br>
 
To create the swap partition, follow steps similar to those described in [[#Mapping_partitions | mapping partitions]] above.<br><br>
* Format the partition you want to use as swap with '''cryptsetup'''. For performance reasons, you might want to use different ciphers with different key sizes. A benchmark can be found [http://www.saout.de/tikiwiki/tiki-index.php?page=UserPageChonhulio here]{{Linkrot|2011|09|05}}.
+
* Format the partition you want to use as swap with the {{ic|cryptsetup}} command. For performance reasons, you might want to use different ciphers with different key sizes. A benchmark can be found [http://www.saout.de/tikiwiki/tiki-index.php?page=UserPageChonhulio here]{{Linkrot|2011|09|05}}.
  
 
  # cryptsetup -c aes-xts-plain -s 512 -h sha512 -v luksFormat /dev/<device>
 
  # cryptsetup -c aes-xts-plain -s 512 -h sha512 -v luksFormat /dev/<device>
Line 663: Line 652:
 
  # cryptsetup luksDump /dev/<device>
 
  # cryptsetup luksDump /dev/<device>
  
* Open the partition in ''/dev/mapper'':
+
* Open the partition in {{ic|/dev/mapper}}:
  
 
  # cryptsetup luksOpen /dev/<device> swapDevice
 
  # cryptsetup luksOpen /dev/<device> swapDevice
Line 671: Line 660:
 
  # mkswap /dev/mapper/swapDevice
 
  # mkswap /dev/mapper/swapDevice
  
Now you should have a LUKS swap partition which asks for the passphrase before mounting. Make sure you remove any line in ''/etc/crypttab'' which uses this device. Now you have to create a hook to open the swap at boot time.
+
Now you should have a LUKS swap partition which asks for the passphrase before mounting. Make sure you remove any line in {{ic|/etc/crypttab}} which uses this device. Now you have to create a hook to open the swap at boot time.
  
 
* Create a hook file containing the open command:
 
* Create a hook file containing the open command:
Line 701: Line 690:
 
}}
 
}}
  
* Add the hook ''openswap'' in the HOOKS array in ''/etc/mkinitcpio.conf'', before ''filesystem'', but '''after''' ''encrypt'' which is mandatory as well.  Don't forget to add the ''resume'' hook between ''openswap'' and ''filesystem'' as well.<br><br>
+
* Add the hook {{ic|openswap}} in the HOOKS array in {{ic|/etc/mkinitcpio.conf}}, before {{ic|filesystem}}, but '''after''' {{ic|encrypt}} which is mandatory as well.  Do not forget to add the {{ic|resume}} hook between {{ic|openswap}} and {{ic|filesystem}} as well.<br><br>
  
 
* Regenerate the boot image:
 
* Regenerate the boot image:
Line 707: Line 696:
 
  # mkinitcpio -p linux
 
  # mkinitcpio -p linux
  
* Add the mapped partition to ''fstab'':
+
* Add the mapped partition to {{ic|/etc/fstab}} by adding the following line:
{{File|name=/etc/fstab|content=
+
[...]
+
 
  /dev/mapper/swapDevice swap swap defaults 0 0
 
  /dev/mapper/swapDevice swap swap defaults 0 0
[...]
 
}}
 
  
* Set-up your system to resume from ''/dev/mapper/swapDevice''. For example, if you use GRUB with kernel hibernation support, add "resume=/dev/mapper/swapDevice" to the kernel line in ''/boot/grub/menu.lst''. A line with encrypted root and swap partitions can look like this:
+
 
 +
* Set-up your system to resume from {{ic|/dev/mapper/swapDevice}}. For example, if you use [[GRUB]] with kernel hibernation support, add {{ic|resume<nowiki>=</nowiki>/dev/mapper/swapDevice}} to the kernel line in {{ic|/boot/grub/menu.lst}}. A line with encrypted root and swap partitions can look like this:
  
 
  kernel /vmlinuz26 cryptdevice=/dev/sda2:rootDevice root=/dev/mapper/rootDevice resume=/dev/mapper/swapDevice ro
 
  kernel /vmlinuz26 cryptdevice=/dev/sda2:rootDevice root=/dev/mapper/rootDevice resume=/dev/mapper/swapDevice ro
  
At boot time, the ''openswap'' hook will open the swap partition so the kernel resume may use it. If you use special hooks for resuming from hibernation, make sure they stand '''after''' ''openswap'' in the HOOKS array. Please note that because of initrd opening swap there is no entry for swapDevice in /etc/crypttab needed in this case.
+
At boot time, the ''openswap'' hook will open the swap partition so the kernel resume may use it. If you use special hooks for resuming from hibernation, make sure they stand '''after''' ''openswap'' in the HOOKS array. Please note that because of initrd opening swap there is no entry for swapDevice in {{ic|/etc/crypttab}} needed in this case.
  
 
== Installing the system ==
 
== Installing the system ==
Line 750: Line 736:
  
 
=== Install Bootloader ===
 
=== Install Bootloader ===
'''GRUB:''' You have to make some small changes to the entries generated by the installer by replacing {{Codeline|/dev/mapper/root}} with {{Codeline|/dev/sda3}}. The corrected config looks like this:
+
'''[[GRUB]]:''' You have to make some small changes to the entries generated by the installer by replacing {{ic|/dev/mapper/root}} with {{ic|/dev/sda3}}. The corrected config looks like this:
 
  # (0) Arch Linux
 
  # (0) Arch Linux
 
  title Arch Linux
 
  title Arch Linux
Line 756: Line 742:
 
  kernel /vmlinuz26 root=/dev/sda3 ro
 
  kernel /vmlinuz26 root=/dev/sda3 ro
 
  initrd /kernel26.img
 
  initrd /kernel26.img
 +
 
For kernel >= 2.6.30:
 
For kernel >= 2.6.30:
 
  # (0) Arch Linux
 
  # (0) Arch Linux
Line 763: Line 750:
 
  initrd /kernel26.img
 
  initrd /kernel26.img
  
'''LILO:''' On Lilo, edit the Arch Linux section on /etc/lilo.conf and include a line for append option, over the initrd, with the "root=/dev/sda3" param. The append section make the same kernel line on grub. Also, you can ommit the root option, over the image option. The section look like this:
+
'''LILO:''' On Lilo, edit the Arch Linux section on {{ic|/etc/lilo.conf}} and include a line for the {{ic|append}} option, over the initrd, with the {{ic|root<nowiki>=</nowiki>/dev/sda3}} parameter. The {{ic|append}} section make the same kernel line on GRUB. Also, you can omit the {{ic|root}} option, over the {{ic|image}} option. The section look like this:
 
  # Arch Linux lilo section
 
  # Arch Linux lilo section
 
  image = /vmlinuz26
 
  image = /vmlinuz26
Line 772: Line 759:
 
   read-only
 
   read-only
  
{{Note | if you want to use a USB stick with a keyfile you have to append the ''cryptkey'' option. See the corresponding section below.}}
+
{{Note|If you want to use a USB flash drive with a keyfile, you have to append the {{ic|cryptkey}} option. See the corresponding section below.}}
  
 
=== Exit Install ===
 
=== Exit Install ===
Line 792: Line 779:
  
 
== Remote unlocking of the root (or other) partition ==
 
== Remote unlocking of the root (or other) partition ==
If you want to be able to reboot a fully LUKS encrypted system remotely or start it with a Wake-on-LAN service you will need a way to enter a passphrase for the root partition/volume at startup. This can be achived by running the "net" hook along with a ssh server in initrd. Install the [http://aur.archlinux.org/packages.php?ID=31135 dropbear_initrd_encrypt] package from AUR and follow the post installation instructions. Replace the "encrypt" hook with "encryptssh" in /etc/mkinitcpio.conf. Put the "net" hook early in the HOOKS array if your dhcp server takes a long time to assign an IP address.
+
If you want to be able to reboot a fully LUKS encrypted system remotely or start it with a Wake-on-LAN service you will need a way to enter a passphrase for the root partition/volume at startup. This can be achived by running the {{ic|net}} hook along with an [[SSH]] server in initrd. Install the {{AUR|dropbear_initrd_encrypt}} package from the [[Arch User Repository|AUR]] and follow the post-installation instructions. Replace the {{ic|encrypt}} hook with {{ic|encryptssh}} in {{ic|/etc/mkinitcpio.conf}}. Put the {{ic|net}} hook early in the HOOKS array if your DHCP server takes a long time to lease IP addresses.
  
If you simply would like a nice solution to mount other encrypted partitions (like /home) remotely you may want to look at the [https://bbs.archlinux.org/viewtopic.php?pid=880484 following forum thread].
+
If you simply would like a nice solution to mount other encrypted partitions (like {{ic|/home}}) remotely, you may want to look at the [https://bbs.archlinux.org/viewtopic.php?pid=880484 this forum thread].
  
 
== Backup the cryptheader ==
 
== Backup the cryptheader ==
Line 959: Line 946:
 
  kernel /boot/vmlinuz26 root=/dev/md0 ro md=0,/dev/sda1,/dev/sdb1 md=1,/dev/sda5,/dev/sdb5,/dev/sdc5
 
  kernel /boot/vmlinuz26 root=/dev/md0 ro md=0,/dev/sda1,/dev/sdb1 md=1,/dev/sda5,/dev/sdb5,/dev/sdc5
  
=== LVM&dm-crypt manually (short version) ===
+
=== LVM and dm-crypt manually (short version) ===
 +
 
 
==== Notes ====
 
==== Notes ====
If you're enough smart enough for this, you'll be smart enough to ignore/replace LVM-specific things if you do not want to use LVM.
+
If you are enough smart enough for this, you will be smart enough to ignore/replace LVM-specific things, if you do not want to use LVM.
 +
 
 +
{{Note|This brief uses reiserfs for some of the partitions, so change this accordingly if you want to use a more "normal" file system, like ext4.}}
 +
 
  
 
==== Partitioning scheme ====
 
==== Partitioning scheme ====
  /dev/sda1 -> /boot
+
  {{ic|/dev/sda1}} -> {{ic|/boot}}
  /dev/sda2 -> LVM
+
  {{ic|/dev/sda2}} -> LVM
==== The commands =====
+
 
 +
 
 +
==== The commands ====
 
  cryptsetup -d /dev/random -c aes-xts-plain -s 512 create lvm /dev/sda2
 
  cryptsetup -d /dev/random -c aes-xts-plain -s 512 create lvm /dev/sda2
 
  dd if=/dev/urandom of=/dev/mapper/lvm
 
  dd if=/dev/urandom of=/dev/mapper/lvm
Line 986: Line 979:
 
  mkdir -p -m 700 /mnt/etc/luks-keys
 
  mkdir -p -m 700 /mnt/etc/luks-keys
 
  dd if=/dev/random of=/mnt/etc/luks-keys/home bs=1 count=256
 
  dd if=/dev/random of=/mnt/etc/luks-keys/home bs=1 count=256
 +
 +
 
==== Install Arch Linux ====
 
==== Install Arch Linux ====
/arch/setup
+
Run {{ic|/arch/setup}}
 +
 
 +
 
 
==== Configuration ====
 
==== Configuration ====
 +
 
===== /etc/rc.conf =====
 
===== /etc/rc.conf =====
Change ''USELVM="no"'' to ''USELVM="yes"''.
+
Change {{ic|USELVM<nowiki>=</nowiki>"no"}} to {{ic|USELVM<nowiki>=</nowiki>"yes"}}.
 +
 
 +
 
 
===== /etc/mkinitcpio.conf =====
 
===== /etc/mkinitcpio.conf =====
Put ''lvm2'' and ''encrypt'' (in that order) before ''filesystems'' in the HOOKS variable. Again, note that you are setting encryption on '''top''' of LVM.)
+
Put {{ic|lvm2}} and {{ic|encrypt}} (in that order) before {{ic|filesystems}} in the HOOKS array. Again, note that you are setting encryption on '''top''' of LVM.)
 +
 
 +
 
 
===== /boot/grub/menu.lst =====
 
===== /boot/grub/menu.lst =====
Change ''root=/dev/hda3'' to ''root=/dev/lvm/root''.<br>
+
Change {{ic|root<nowiki>=</nowiki>/dev/hda3}} to {{ic|root<nowiki>=</nowiki>/dev/lvm/root}}.
For kernel >= 2.6.30, you should change ''root=/dev/hda3'' to:
+
 
  ''cryptdevice=/dev/lvm/root:root root=/dev/mapper/root''
+
For kernel >= 2.6.30, you should change {{ic|root<nowiki>=</nowiki>/dev/hda3}} to the following:
 +
  cryptdevice=/dev/lvm/root:root root=/dev/mapper/root
 +
 
  
 
===== /etc/fstab =====
 
===== /etc/fstab =====
Line 1,003: Line 1,007:
 
  /dev/mapper/tmp        /tmp    tmpfs          defaults        0      0
 
  /dev/mapper/tmp        /tmp    tmpfs          defaults        0      0
 
  /dev/mapper/swap        none    swap            sw              0      0
 
  /dev/mapper/swap        none    swap            sw              0      0
 +
  
 
===== /etc/crypttab =====
 
===== /etc/crypttab =====
Line 1,008: Line 1,013:
 
  tmp /dev/lvm/tmp /dev/urandom -c aes-xts-plain -s 512
 
  tmp /dev/lvm/tmp /dev/urandom -c aes-xts-plain -s 512
  
==== After reboot ====
+
 
 +
==== After rebooting ====
 +
 
 
===== The commands =====
 
===== The commands =====
 
  cryptsetup luksFormat -c aes-xts-plain -s 512 /dev/lvm/home /etc/luks-keys/home
 
  cryptsetup luksFormat -c aes-xts-plain -s 512 /dev/lvm/home /etc/luks-keys/home
Line 1,014: Line 1,021:
 
  mkreiserfs /dev/mapper/home
 
  mkreiserfs /dev/mapper/home
 
  mount /dev/mapper/home /home
 
  mount /dev/mapper/home /home
 +
 +
 
===== /etc/crypttab =====
 
===== /etc/crypttab =====
 
  home /dev/lvm/home  /etc/luks-keys/home
 
  home /dev/lvm/home  /etc/luks-keys/home
 +
 +
 
===== /etc/fstab =====
 
===== /etc/fstab =====
 
  /dev/mapper/home        /home  reiserfs        defaults        0      0
 
  /dev/mapper/home        /home  reiserfs        defaults        0      0
  
=== / on lvm on luks ===
+
=== / on LVM on LUKS ===
Make sure your kernel commandline looks like this:
+
Make sure your kernel command line looks like this:
 
  root=/dev/mapper/<volume-group>-<logical-volume> cryptdevice=/dev/<luks-part>:<volume-group>
 
  root=/dev/mapper/<volume-group>-<logical-volume> cryptdevice=/dev/<luks-part>:<volume-group>
for example:
+
For example:
 
  root=/dev/mapper/vg-arch cryptdevice=/dev/sda4:vg
 
  root=/dev/mapper/vg-arch cryptdevice=/dev/sda4:vg
  

Revision as of 21:56, 1 November 2011

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Summary help replacing me
This tutorial will show you how to set up system encryption with LUKS for dm-crypt.

Contents

Introduction

Why Use Encryption?

In the simplest terms encryption is a method for establishing privacy.

There are presently two approaches to partition level encryption data encryption and system encryption.


Data encryption, defined as encrypting a users data, provides for many benefits including:

  • Preventing unauthorized physical access to private data.
  • Some confidence in data disposal when discarding obsolete systems.


However data encryption alone has some significant drawbacks. In modern computing systems, there are many background processes that may store information about encrypted data or parts of the encrypted data itself in non-encrypted areas of the hard drive. Thus reducing the effectiveness of any data encryption system in place.


System encryption, defined as the encryption of the operating system and user data, helps to address some of the inadequacies of data encryption. The benefits of system encryption over data encryption alone include:


  • Preventing unauthorized physical access to operating system files
  • Preventing unauthorized physical access to private data that may cached by the system.


In the context of overall system security, system encryption should be viewed as an adjunct to the existing security mechanisms of the operating system that focuses on physical attempts to breach system security which includes:


  • Attempts to bypass the operating system by inserting a boot CD/USB
  • Copying, modifying, or removing the hard disk drives when the computer is off


Despite the use of system encryption, there are still points of physical insecurity. These issues revolve around the /boot partition which must remain unencrypted in order for the machine to properly boot. However, system encryption is presently the best way to minimize the loss of data privacy by physical attempts at invasion.


Warning: Any encryption method employed is only as good as its associated key management. Partition level encryption does not protect you from all forms of security compromise. There are ways to break into computers while they are powered on that are unaffected by disk level encryption. Read the caveats section below!

What Methods are Available for System Encryption?

There are multiple current methods that can be employed for system encryption, including:


loop-AES (loop-AES)
loop-AES is a descendant of cryptoloop and is a secure and fast solution to system encryption.
However loop-AES is considered less user-friendly than other options as it requires non-standard kernel support.


standard device-mapper encryption (dm-crypt)
This is the standard device mapper which can be used for those who like to have control over all aspects partition management.


LUKS for dm-crypt (LUKS)
LUKS 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.
Briefly some key features that LUKS provides include:
  • Support for either passphrase or keyfiles as encryption keys
  • Per partition key creation and revocation
  • Multiple passphrases or keyfiles for a particular partition

Caveats

For any type of encryption the security of your privacy is dependent on two things:


Key Complexity and Availability

The user provided key used for encryption, whether a passphrase or a keyfile, must be complex enough that is it not easy to guess. Having a strong encryption algorithm does nothing to provide privacy if the key used for encryption is too simple. The tenets of strong keys are based on length and randomness. There are many sources available with instructions on how to create strong encryption keys.

Part of key complexity is key availability. For example a complex key written on a sticky note pasted to the computer's keyboard would not provide much in the way privacy. Therefore in addition to creating a strong key, maintaining it in a secure location is necessary as well.


Encryption Algorithm

There are many peer-reviewed encryption algorithms in existence. The encryption algorithms and block ciphers used in any of the mentioned methods for applying encryption in this wiki page are considered strong algorithms that have been subjected to cryptographic review by the cryptography community.


discard/TRIM support for solid state disks

Solid state disk users should be aware that by default, Linux's full-disk encryption mechanisms will not forward TRIM commands from the filesystem to the underlying disk. The device-mapper maintainers have made it clear that TRIM support will never be enabled by default on dm-crypt devices because of the potential security implications; if TRIM support were enabled, an attacker may be able to tell which blocks have been used, how many blocks have been used, and other information that is exposed directly to the device when a TRIM is issued.

It may be possible to determine the filesystem utilized by your encrypted device through the data that is leaked by TRIM. Furthermore, any information that may be derived by a profile of block usage may be exposed by enabling TRIM support on an encrypted device.

As of kernel 3.1, support for dm-crypt TRIM pass-through can be toggled upon device creation or mount with dmsetup. Support for this option also exists in cryptsetup version 1.4.0 and up. This configuration has not been thoroughly tested. It is strongly advised that users who are not willing to lose data in this process wait until official userspace support is realized. A testing package of cryptsetup already supports this. You need to install cryptsetup from [testing] (at least cryptsetup-1.4.0-1) and append ":allow-discards" to the cryptdevice option. The option should look like this then:

cryptdevice=/dev/mapper/root:root:allow-discards

For more information, including specific commands and details on dm-crypt TRIM pass-through, see these mailing list threads:

* http://article.gmane.org/gmane.linux.kernel.device-mapper.devel/14134
* http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/5166


System Encryption

System encryption provides security against unauthorized physical access to a machine that is powered off. It does not effect any security advantages for a system that is powered on with its partitions mounted in an unencrypted state. For a powered on user accessible system the normal precautions to prevent viruses, trojans, worms, or other attempts to access private data should be exercised. Furthermore, system encryption has been shown to be penetrable in cases where a system has been recently shutdown. This is due to the fact that cessation of power does not immediately degrade data that was stored in RAM prior to shutdown. Therefore someone with physical access to your computer within a few moments of shutdown could cool the RAM modules and use them extract your encryption key - thus obtaining access to your data.


Note: System Encryption assume encryption of all mounted partitions this includes all partitions except for Template:Filename - meaning that the root file system, swap partition, and all other partitions must be encrypted. If the swap, Template:Filename, or root filesystems are unencrypted, only Data Encryption level of security has been accomplished.


Data Encryption

There are two common forms of data encryption:

  • Encryption of data partitions on the same physical disk as the system.
  • Encryption of data partitions on separate physical disks from the system.


Encryption of data partitions on the same physical disk as the system

The most common form of data encryption is encrypting the Template:Filename partition.


In cases where the encrypted data are located on the same physical disk as the system accessing the drive the privacy of data has already been decreased by orders of magnitude when compared to system encryption. The reason for this is that the host operating systems employ background methods to assist the user in the access and management of their data. The problem lies in where these processes store this data which is most commonly in the unencrypted system partition.

For example, mlocate will scan all currently mounted file systems regularly and write the list of filenames to Template:Filename, which is located in the non-encrypted root or Template:Filename partition. Thus an attacker will have a list of all filenames for that computer, even the ones on the encrypted Template:Filename partition, readily available to assist them in accessing the encrypted data present on the disk.

Some have compared this to reducing the level of security from partition-based encryption to filesystem level encryption like System Encryption with eCryptfs.


Encryption of data partitions on separate physical disks from the system

Popular forms of data encryption on physically separate partitions include the encryption of removable media such as:

  • USB Flash Drives
  • External Hard Disk Drives or Separate Internal Hard Disk Drives
  • CD/DVD/Blu-Ray Optical Media
  • Magnetic Storage Media


The most important part of this form of data encryption is to remember that the encryption protects the privacy of the data that is located within the encrypted media only when it is not mounted. Data encryption does not protect the privacy of data once it is made accessible to a system. For example, attaching an encrypted USB flash drive, and subsequently decrypting a file for use temporarily on a non-secured system could result in remnants of that file existing on the host system in an unencrypted form.

Initial Setup

Overview and Preparation

The Arch installer comes with all the tools required for system encryption. Setup of encrypted partitions can be accomplished either manually prior to executing the arch installer or using the menu interface from the arch installer itself. The installation of an encrypted system is largely the same as installing an unencrypted system, so you can follow the Official Arch Linux Install Guide or the Beginners' Guide after the encrypted partitions are setup.

Routine creation of an encrypted system follows these general steps:

  • Secure erasure of the harddisk drive(s)
  • Partitioning and setup of encryption (LVM optional)
  • Routine package selection and installation
  • System Configuration
Warning: Encrypting a partition will erase everything currently on that partition. Please make appropriate data backups prior to starting.

Secure Erasure of the Hard Disk Drive

Secure erasure of the hard disk drive involves overwriting the entire drive with random data.

Note: Overwriting a hard disk drive multiple times with random data serves no purpose. Data existing prior to overwriting cannot be recovered after it has been overwritten. Overwriting Hard Drive Data: The Great Wiping Controversy


Why perform secure of erasure of a drive?

There are two types of hard disk drives, new and used, both kinds should be securely overwritten. The reasoning is slightly different for each but the goal is to help ensure the privacy of data located within the encrypted partitions.

New Hard Disk Drives
In hard drives that have been directly purchased from a manufacturer there is no preexisting private data to protect. The problem is that there is no consistency in what is presently on the drive. Ideally the drive should be completely filled with random bits. However some drives have been overwritten completely with zeros. Therefore once the drive is used to write encrypted data, it is relatively simple to identify where the encrypted data ends and the zeroed data begins compared to a drive that was written with random data before usage as an encrypted drive. Since an encrypted partition is supposed to be indistinguishable from random data, the lack of random data on a zeroed drive makes an encrypted drive an easier target for cryptanalysis.
Used Hard Disk Drives
Repartitioning or reformatting a used hard drive removes the file system structure for identifying where the original data was located while leaving the actual data intact on the drive itself. It is relatively straight forward using data tools like Foremost to access the remnant data. Therefore hard drives should be securely overwritten with random data prior to encryption to prevent unintentional data recovery.


Overwriting a hard disk drive with random data

There are two sources of random data commonly used for securely overwritting hard disk partitions.

Using urandom
#dd if=/dev/urandom of=/dev/<drive> bs=1M

Where Template:Filename is the drive to be encrypted.

Note: Using Template:Filename will take a long time to completely overwrite a drive with "random" data. In the strictest sense, Template:Filename is less random than Template:Filename; however, Template:Filename uses the kernel entropy pool and will halt overwriting until more input entropy once this pool has been exhausted. This makes the use of Template:Filename for overwriting hard disks impractical.
Note: Users may also find that Template:Filename takes an excessively long time on large drives of several hundred gigabytes or more (more than twenty-four hours). Frandom offers a faster alternative.


Using badblocks
#badblocks -c 10240 -wsvt random /dev/<drive>

Where Template:Filename is the drive to be encrypted.

Note: The Template:Codeline command overwrites the drive at a much faster rate by generating data that is not truly random.
Tip: In deciding which method to use for secure erasure of a hard disk drive, remember that this will not need to be performed more than once for as long as the drive is used as an encrypted drive.

Partitioning

After the drive has been securely overwritten, it is time to create partitions and begin setting up an encrypted system.

There are multiple ways to create disk partitions:

LUKS is compatible in systems that require both LVM and/or RAID as well as with with standard primary, extended, and logical partitions.


Standard Partitions

These are the partitions that most people are familiar with. They come in 3 flavors: primary partitions, extended partitions, and logical partitions.


Primary Partitions
These are the normal partitions recognized by the system BIOS. There can be up to 4 of these stored in the MBR.
Extended Partitions
These are primary partitions that also define another partition within themselves. Extended partitions were created to work around the original 4 partition limit of primary partitions.
Logical Partitions
These are the partitions that are defined within extended partitions.

LVM: Logical Volume Manager

The LVM allows for creation of volume groups for systems that require complex combinations of multiple hard disk drives and partitions that are not possible with standard partitions. LVM is covered in detail in the Arch Linux LVM Wiki Article which is recommended reading prior to continuing with the instructions on setting up LUKS with LVM located below.


How does LVM fit into the overall system?

There is a growing preference towards logical volume management of LUKS encrypted physical media (LVM on LUKS). It is possible there may exist usage scenarios where encrypting logical volumes rather than physical disks is required (LUKS on LVM). However, the deployment of LVM on LUKS is considered much more generalizable. One reason for this is that using LUKS as the lowest level of infrastructure most closely approximates the deployment of physical disks with built-in hardware encryption. In which case, logical volume management would be layered on top of the hardware encryption -- usage of LUKS would be superfluous.


Creating Disk Partitions

Disk partitions are created using:

# cfdisk

This will display a graphical interface for creating disk partitions.

There are 2 required partitions for any encrypted system:

A root file system
An initial boot partition
  • Template:Filename
  • Will not be encrypted; the bootloader needs to access the Template:Filename directory where it will load the initramfs/encryption modules needed to load the rest of the system which is encrypted (see Mkinitcpio for details). For this reason, Template:Filename needs to reside on its own, unencrypted partition.


Note: A swap partition is optional; it can be encrypted with dm-crypt/LUKS. See Encrypting the Swap Partition for details.


Single Disk Systems

Depending on the system demands, there may be additional partitions desired. These partitions can be individually created at this level by defining separate primary or extended/logical partitions. However, if LVM is to be used, the space unoccupied by Template:Filename and swap should be defined as single large partition which will be divided up later at the LVM level.


Multiple Disk Systems

In systems that will have multiple hard disk drives, the same options exist as a single disk system. After the creation of the Template:Filename and swap partitions, the remaining free space on physical disks can divided up into their respective partitions at this level, or large partitions can define all free space per physical disk with intent to partition them within the LVM.

Configuring LUKS

Creating LUKS partitions with a passphrase is supported by the /arch/setup program.

This section of the Wiki will cover how to manually utilize LUKS from the command line to encrypt a system.

The steps for accomplishing this through the graphical installer are very similar and can be located in the dialogue for manual configuration of the hard drive.


Mapping Physical Partitions to LUKS

Once the desired partitions are created it is time to format them as LUKS partitions and then mount them through the device mapper.

When creating LUKS partitions they must be associated with a key.

A key is either a:

  • Passphrase
  • Keyfile

It is possible to define up to 8 different keys per LUKS partition.


Using LUKS to Format Partitions with a Passphrase

Cryptsetup is used to interface with LUKS for formatting, mounting and unmounting encrypted partitions.

A full list of options cryptsetup accepts can be found in the [Cryptsetup Manpage]

The options used here are:

  • -c defines the cipher type
  • -y prompts for password confirmation on password creation
  • -s defines the key size
luksFormat addresses the LUKS extensions built into cryptsetup.


In the following examples for creating LUKS partitions, we will use the AES cipher in XTS mode, at present this is most generally used preferred cipher. Other ciphers can be used with cryptsetup, and details about them can be found here: Wikipedia:Block_cipher


Formatting LUKS Partitions

First of all make sure the device mapper kernel module is loaded by executing the following: # modprobe dm_mod

In order to format a desired partition as an encrypted LUKS partition execute:

# cryptsetup -c <cipher> -y -s <key size> luksFormat /dev/<partition name>
Enter passphrase: <password>
Verify passphrase: <password>


This should be repeated for all partitions except for /boot and possibly swap.


The example below will create an encrypted root partition using the AES cipher in XTS mode (generally referred to as XTS-AES).

cryptsetup -c aes-xts-plain -y -s 512 luksFormat /dev/sda2


Note: If hibernation usage is planned, swap must be encrypted in this fashion; otherwise, if hibernation is not a planned feature for the system, encrypting the swap file will be performed in a alternative manner.


Warning: Irrespective of the chosen partitioning method, the /boot partition must remain separate and unencrypted in order to load the kernel and boot the system.


Unlocking/Mapping LUKS Partitions with the Device Mapper

Once the LUKS partitions have been created it is time to unlock them.

The unlocking process will map the partitions to a new device name using the device mapper. This alerts the kernel that /dev/<partition name> is actually an encrypted device and should be addressed through LUKS using the /dev/mapper/<name> so as not to overwrite the encrypted data.


In order to open an encrypted LUKS partition execute:

# cryptsetup luksOpen /dev/<partition name> <device-mapper name>
Enter any LUKS passphrase: <password>
key slot 0 unlocked.
Command successful.


Usually the device mapped name is descriptive of the function of the partition that is mapped, example:


  • cryptsetup luksOpen /dev/sda2 swap
Once opened, the swap partition device address would be /dev/mapper/swap instead of /dev/sda2.


  • cryptsetup luksOpen /dev/sda3 root
Once opened, the root partition device address would be /dev/mapper/root instead of /dev/sda3.


Note: Since /boot is not encrypted, it does not need a device mapped name and will be addressed as /dev/sda1.


Warning: In order to write encrypted data into the partition it must be accessed through the device mapped name.

Using LUKS to Format Partitions with a Keyfile

What is a Keyfile?

A keyfiles is any file in which the data contained within 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.


Why use a Keyfile?

There are many kinds of keyfiles, each type of keyfile used has benefits and disadvantages summarized below:


keyfile.passphrase:
this is my passphrase I would have typed during boot but I have placed it in a file instead


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.


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


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.


keyfile.binary:
where any binary file, images, text, video could be chosen as the keyfile


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 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, music have some intrinsic relationship between neighboring bits of data that is not existent for a text file. However this is controversial and has never been exploited publicly.


Creating a Keyfile with Random Characters


Here dd is used to generate a keyfile of 2048 bits of random characters.


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


The usage of dd is similar to initially wiping the volume with random data prior to encryption. While badblocks may also be used, most key files are on the order of a few kilobytes and there is no noticable speed difference between dd, or badblocks.


Creating a new LUKS encrypted partition with a Keyfile


When creating a new LUKS encrypted partition, a keyfile may be associated with the partition on its creation using:


# cryptsetup -c <desired cipher> -s <key size> -v luksFormat /dev/<volume to encrypt> /path/to/mykeyfile


This is accomplished by appending the bold area to the standard cryptsetup command which defines where the keyfile is located.

Adding Additional Passphrases or Keyfiles to a LUKS Encrypted Partition

LUKS supports the association of up to 8 keys with any single encrypted volume. Keys can be either keyfiles or passphrases.

Once an encrytped partition has been created, the initial key is associated at slot0. Additional keys will occupy slots 1 - 7.

The addition of new keys to an encrypted partition is accomplished using cryptsetup with the luksAddKey extension.


# cryptsetup luksAddKey /dev/<encrypted volume> /path/to/mykeyfile


Where /dev/<encrypted volume> is the volume that is to have the new key associated with it.

If the bolded area is present, cryptsetup will look for the keyfile defined at that location to associate with the encrypted volume specified.

Storing the Key File

External Storage on a USB Drive

Preparation for permanent device names

For reading the file from an USB stick it is important to access it through a permanent device name. The numbering of the normal device names e.g. /dev/sdb1 is somewhat arbitrary and depends on how many storage devices are attached and in what order, etc. So in order to assure that the encrypt HOOK in the initcpio finds your keyfile, you must use a permanent device name.

Quick method

A quick method (as opposed to setting up a udev rule) for doing so involves referencing your removable device by its label (or UUID). To find your label or UUID, plug in your USB drive and run the following:

# ls -l /dev/disk/by-label/
lrwxrwxrwx 1 root root 10 12. Feb 10:11 Keys -> ../../sdb1

or

# ls -l /dev/disk/by-uuid/
lrwxrwxrwx 1 root root 10 12. Feb 10:11 4803-8A7B -> ../../sdb1

In this case, I labeled the vfat partition on my USB drive as "Keys" so my device is always symlinked in /dev/disk/by-label/Keys, or if I had wanted to use the UUID I would find /dev/disk/by-uuid/4803-8A7B. This allows me to have a consistent naming of my USB devices regardless of the order they are plugged into the system. These device names can be used in the "cryptkey" kernel option or any where else. Filesystem UUIDs are stored in the filesystem itself, meaning that the UUID will be the same if you plug it into any other computer, and that a dd backup of it will always have the same UUID since dd does a bitwise copy.

Note: If you plan to store the keyfile between MBR and the 1st partition you cannot use this method, since it only allows access to the partitions (sdb1, sdb2, ...) but not to the USB device (sdb) itself. Create a udev rule instead as described in the following section.

Using udev

Optionally you may choose to set up your flash drive with a udev rule. There is some documentation in the Arch wiki about that already; if you want more in-depth, structural info, read this guide. Here is quickly how it goes.

Get the serial number from your USB flash drive:

lsusb -v | grep -A 5 Vendor

Create a udev rule for it by adding the following to a file in /etc/udev/rules.d/, such as 8-usbstick.rules:

KERNEL=="sd*", ATTRS{serial}=="$SERIAL", SYMLINK+="$SYMLINK%n"

Replace $SYMLINK and $SERIAL with their respective values. %n will expand to the partition (just like sda is subdivided into sda1, sda2, ...). You do not need to go with the 'serial' attribute. If you have a custom rule of your own, you can put it in as well (e.g. using the vendor name).

Rescan your sysfs:

udevadm trigger

Now check the contents of /dev:

ls /dev

It should show your device with your desired name.

Generating the keyfile

Optionally you can mount a tmpfs for storing the temporary keyfile.

# mkdir ./mytmpfs
# mount tmpfs ./mytmpfs -t tmpfs -o size=32m
# cd ./mytmpfs

The advantage is that it resides in RAM and not on a physical disk, so after unmounting your keyfile is securly gone. So copy your keyfile to some place you consider as secure before unmounting. If you are planning to store the keyfile as a plain file on your USB device, you can also simply execute the following command in the corresponding directory, e.g. /media/sdb1

The keyfile can be of arbitrary content and size. We will generate a random temporary keyfile of 2048 bytes:

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

If you stored your temporary keyfile on a physical storage, remember to not just (re)move the keyfile later on, but use something like

cp secretkey /destination/path
shred --remove --zero secretkey

to securely overwrite it. (However due to journaling filesystems this is also not 100% secure.)

Add the temporary keyfile with cryptsetup:

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

Storing the keyfile

To store the key file, you have two options. The first is less risky than the other, but perhaps a bit more secure (if you consider security by obscurity as more secure). In any case you have to do some further configuration, if not already done above.

Configuration of initcpio

You have to add two extra modules in your /etc/mkinitcpio.conf, one for the drive's file system and one for the codepage. Further if you created a udev rule, you should tell mkinitcpio about it:

MODULES="ata_generic ata_piix nls_cp437 vfat"
FILES="/etc/udev/rules.d/8-usbstick.rules"

In this example it is assumed that you use a FAT formatted USB drive. Replace those module names if you use another file system on your USB stick (e.g. ext2) or another codepage. Users running the stock Arch kernel should stick to the codepage mentioned here.

Additionally, insert the usb hook somewhere before the encrypt hook.

HOOKS="... usb encrypt ... filesystems ..."

Generate a new image (maybe you should take a copy of your old kernel26.img before):

mkinitcpio -g /boot/kernel26.img

Storing the key as plain (visible) file

Be sure to choose a plain name for your key - a bit of 'security through obscurity' is always nice ;-). Avoid using dots (hidden files) and similar characters - the encrypt hook will fail to find the keyfile during the boot process.

You have to add a kernel parameter in your /boot/grub/menu.lst (GRUB), it should look something like this:

kernel /vmlinuz26 root=/dev/hda3 ro vga=791 cryptkey=/dev/usbstick:vfat:/secretkey

This assumes /dev/usbstick is the FAT partition of your choice. Replace it with /dev/disk/by-... or whatever your device is.

That's all, reboot and have fun!

Storing the key between MBR and 1st partition

We will write the key directly between MBR and first partition.

Warning: You should only follow this step if you know what you are doing -- it can cause data loss and damage your partitions or MBR on the stick!

If you have a bootloader installed on your drive you have to adjust the values. E.g. Grub needs the first 16 sectors (actually, it depends on the type of the file system, so do not rely on this too much), you would have to replace seek=4 with seek=16; otherwise you would overwrite parts of your Grub installation. When in doubt, take a look at the first 64 sectors of your drive and decide on your own where to place your key.

Optional If you do not know if you've got enough free space before the first partition you can do

dd if=/dev/usbstick of=64sectors bs=512 count=64   # gives you copy of your first 64 sectors
hexcurse 64sectors                                 # determine free space
xxd 64sectors | less                               # alternative hex viewer

Write your key to the disk:

dd if=secretkey of=/dev/usbstick bs=512 seek=4

If everything went fine you can now overwrite and delete your temporary secretkey as noted above. You should not simply use rm as the keyfile would only be unlinked from your filesystem and be left physically intact.

Now you have to add a kernel parameter in your /boot/grub/menu.lst file (GRUB); it should look something like this:

kernel /vmlinuz26 root=/dev/hda3 ro vga=791 cryptkey=/dev/usbstick:2048:2048

Format for the cryptkey option:

cryptkey=BLOCKDEVICE:OFFSET:SIZE

OFFSET and SIZE match in this example, but this is coincidence - they can differ (and often will). An other possible example could be

kernel /vmlinuz26 root=/dev/hda3 ro vga=791 cryptkey=/dev/usbstick:8192:2048

That's all, reboot and have fun! And look if your partitions still work after that ;-).

Encrypting the Swap partition

Encrypting Swap without Suspend Support

In systems where suspend to disk is not a desired feature, it is possible to create a swap file that will have a random passphrase with each boot.

This is accomplished by using dm-crypt directly without LUKS extensions.


Execute the following command to setup a randomly encrypted swap partition:

# echo <device-mapper name> <swap physical partition> SWAP "-c aes-xts-plain -h whirlpool -s 512" >> /mnt/etc/crypttab


This command adds the following swap partition details to /mnt/etc/crypttab:

  • 'echo' and '>> /mnt/etc/crypttab' adds the command to the file /mnt/etc/crypttab directly
  • SWAP identifies the partition as a swap partition
  • -c defines a cipher
  • -h defines a hash algorithm
  • -s defines the key size


Example (/dev/sda2 is the physical swap partition and /dev/mapper/swapmapper is the device-mapper):

  • echo swapmapper /dev/sda2 SWAP "-c aes-xts-plain -h whirlpool -s 512" >> /mnt/etc/crypttab
Maps /dev/sda2 to /dev/mapper/swapmapper as a swap partition which is encrypted by AES with Whirlpool as the hash algorithm.


There are many hash algorithms that can be employed. For further details, read Wikipedia:Cryptographic_hash_function.


Tip: Many people prefer Whirlpool as it is patent free. On modern hardware there is minimal performance difference between algorithms.


If the partition chosen for swap was previously a LUKS partition, crypttab will not overwite to partition to create a swap partition. This is safetly measure to prevent data loss from accidental miss identification of the swap partition in crypttab. In order to use such a partition the LUKS header must be removed. This can be accomplished by

# dd if=/dev/zero of=/dev/sda2

Using UUIDs with encrypted swap partitions

If there are multiple hard drives installed in the system, their naming order (sda, sdb,...) can occasionally be scrambled upon boot. When /etc/rc.sysinit parses the /etc/crypttab file and sees the SWAP line, it will create a new dm-crypt mapping backed by that device partition and initialize encrypted swap space on top of it. This operation destroys all data on the partition, which can be catastrophic if the disk names have been switched upon reboot. To prevent such mistakes on multi-drive systems, always reference disk partitions using UUIDs rather than device names. /etc/rc.sysinit will refuse to overwrite any known file system, partition table, RAID/LVM volume or valid LUKS partition.

Create a regular swap partition with a new UUID:

# mkswap /dev/sdaX   # where X is your swap partition number 

Note the new random UUID and edit /etc/crypttab

swap  /dev/disk/by-uuid/<your UUID>  SWAP  --offset 8 -c aes-xts-plain -h whirlpool -s 512

The --offset parameter will prevent the UUID (stored within the first eight 512 byte blocks) from being overwritten by cryptsetup each time new encrypted swap space is initialized.

Edit your /etc/rc.sysinit, find the following lines

/sbin/blkid -p "$2" &>/dev/null
if [[ $? -eq 2 ]]; then
    _overwriteokay=1
fi

and replace it with

/sbin/blkid -p "$2" &>/dev/null
if [[ $? -eq 2 ]]; then
    _overwriteokay=1
else
    if /sbin/blkid -p -s TYPE "$2"|/bin/grep -q "swap" ; then
        _overwriteokay=1
    fi
fi

You can now use /dev/mapper/swap as swap device in /etc/fstab.

An additional advantage of this method is that the swap partition can be shared among several operating systems, in encrypted and unencrypted forms.

Encrypted swap with suspend-to-disk support

Warning: Do not use this setup with a key file, please read about the issue reported here

To be able to resume after suspending the computer to disk (hibernate), it is required to keep the swap filesystem intact. Therefore, it is required to have a presistent LUKS swap partition, which can be stored on the disk or input manually at startup. Because the resume takes place before the crypttab can be used, it is required to create a hook in /etc/mkinitcpio.conf to open the swap LUKS device before resuming. The following setup has the disadvantage of having to insert a key manually for the swap partition.

If you want to use a partition which is currently used by the system, you have to disable it, first:

# swapoff /dev/<device>

To create the swap partition, follow steps similar to those described in mapping partitions above.

  • Format the partition you want to use as swap with the cryptsetup command. For performance reasons, you might want to use different ciphers with different key sizes. A benchmark can be found hereTemplate:Linkrot.
# cryptsetup -c aes-xts-plain -s 512 -h sha512 -v luksFormat /dev/<device>

Check result with:

# cryptsetup luksDump /dev/<device>
  • Open the partition in /dev/mapper:
# cryptsetup luksOpen /dev/<device> swapDevice
  • Create a swap filesystem inside the mapped partition:
# mkswap /dev/mapper/swapDevice

Now you should have a LUKS swap partition which asks for the passphrase before mounting. Make sure you remove any line in /etc/crypttab which uses this device. Now you have to create a hook to open the swap at boot time.

  • Create a hook file containing the open command:

Template:File

  • Then create and edit the hook setup file:

Template:File

  • Add the hook openswap in the HOOKS array in /etc/mkinitcpio.conf, before filesystem, but after encrypt which is mandatory as well. Do not forget to add the resume hook between openswap and filesystem as well.

  • Regenerate the boot image:
# mkinitcpio -p linux
  • Add the mapped partition to /etc/fstab by adding the following line:
/dev/mapper/swapDevice swap swap defaults 0 0


  • Set-up your system to resume from /dev/mapper/swapDevice. For example, if you use GRUB with kernel hibernation support, add resume=/dev/mapper/swapDevice to the kernel line in /boot/grub/menu.lst. A line with encrypted root and swap partitions can look like this:
kernel /vmlinuz26 cryptdevice=/dev/sda2:rootDevice root=/dev/mapper/rootDevice resume=/dev/mapper/swapDevice ro

At boot time, the openswap hook will open the swap partition so the kernel resume may use it. If you use special hooks for resuming from hibernation, make sure they stand after openswap in the HOOKS array. Please note that because of initrd opening swap there is no entry for swapDevice in /etc/crypttab needed in this case.

Installing the system

Now that Template:Codeline and Template:Codeline are in place, we can enter the regular Arch setup script to install the system into the encrypted volumes.

# /arch/setup
Note: Most of the installation can be carried out normally. However, there are a few areas where it is important to make certain selections these are marked below.

Prepare hard drive

Skip the Partitioning and Auto-Prepare business and go straight to manually configuration. Instead of choosing the hardware devices (/dev/sdaX) directly you have to select the mapper devices created above: Choose Template:Codeline for your root and Template:Codeline as home partition respectively and format them with any filesystem you like. The same is valid for a swap partition which is set up like the home partition. Make sure you mount Template:Codeline as the /boot partition or else the installer will not properly set up the bootloader.

Select and Install packages

Select and install the packages as usual, the base package contains all required programs.

Configure System

Note: encrypt hook is only needed if your root partition is a LUKS partition (or for a LUKS partition that needs to be mounted before root). Encrypt hook is not needed in case any other partition (swap, for example) is encrypted. System initialization scripts (rc.sysinit and /etc/crypttab among others) take care of those.

Afterwards you can check the files presented to you by the installer, the most important one being Template:Codeline. For detailed info on mkinitcpio (and its configuration) refer to Mkinitcpio.You have to make sure that your Template:Codeline looks somehow like this:

HOOKS="... encrypt ... filesystems ..."

It is important that the Template:Codeline hook comes before the Template:Codeline one. If you store your key on an external USB device (e.g. a USB stick), you need to add the USB hook too:

HOOKS="... usb encrypt ... filesystems ..."

For safety, add in usb before encrypt; not sure if they're run in the order they appear in mkinitcpio.conf or not. If you need support for foreign keymaps for your encryption password you have to specify the hook 'keymap' as well. I suggest to put this in Template:Codeline right before 'encrypt'.

If you have USB keyboard you need the "usbinput" hook in mkinitcpio.conf. Without it, no USB keyboard will work in early userspace.

If your root partition is a LUKS partition add the used filesystem to the MODULES section.

MODULES="... ext3 ext4 xfs ..."

Install Bootloader

GRUB: You have to make some small changes to the entries generated by the installer by replacing /dev/mapper/root with /dev/sda3. The corrected config looks like this:

# (0) Arch Linux
title Arch Linux
root (hd0,0)
kernel /vmlinuz26 root=/dev/sda3 ro
initrd /kernel26.img

For kernel >= 2.6.30:

# (0) Arch Linux
title Arch Linux
root (hd0,0)
kernel /vmlinuz26 cryptdevice=/dev/sda3:root root=/dev/mapper/root ro
initrd /kernel26.img

LILO: On Lilo, edit the Arch Linux section on /etc/lilo.conf and include a line for the append option, over the initrd, with the root=/dev/sda3 parameter. The append section make the same kernel line on GRUB. Also, you can omit the root option, over the image option. The section look like this:

# Arch Linux lilo section
image = /vmlinuz26
# root = /dev/sda3
 label = Arch
 initrd = /kernel26.img
 append = "root=/dev/sda3"
 read-only
Note: If you want to use a USB flash drive with a keyfile, you have to append the cryptkey option. See the corresponding section below.

Exit Install

Now that the install is finished the only thing left to do is add entries to the Template:Codeline file so you do not have to enter the passphrase for all encrypted partitions. This works only for non-root partitions e.g. /home, swap, etc.

# vi /etc/crypttab

Add the following line for the Template:Codeline partition

home    /dev/sda5    "myotherpassword"

You can also use a keyfile instead of a passphrase. If not already done, create a keyfile and add the key to the corresponding LUKS partition as described above. Then add the following information to the Template:Codeline file for automounting:

home    /dev/sda5    /path/of/your/keyfile

After rebooting you should now be presented with the text

A password is required to access the root filesystem:

followed by a prompt for any LUKS password. Type it in and everything should boot. Once you've logged in, have a look at your mounted partitions by typing Template:Codeline. You should have Template:Codeline mounted at Template:Codeline and, if you set up a separate encrypted home partition, Template:Codeline mounted at Template:Codeline. If you set up encrypted swap, Template:Codeline should have Template:Codeline listed as your swap partition.

Note: eventually the text prompting for the password is mixed up with other boot messages. So the boot process may seem frozen at first glance, but it isn't, simply enter your password and press return.

Remote unlocking of the root (or other) partition

If you want to be able to reboot a fully LUKS encrypted system remotely or start it with a Wake-on-LAN service you will need a way to enter a passphrase for the root partition/volume at startup. This can be achived by running the net hook along with an SSH server in initrd. Install the dropbear_initrd_encryptAUR package from the AUR and follow the post-installation instructions. Replace the encrypt hook with encryptssh in /etc/mkinitcpio.conf. Put the net hook early in the HOOKS array if your DHCP server takes a long time to lease IP addresses.

If you simply would like a nice solution to mount other encrypted partitions (like /home) remotely, you may want to look at the this forum thread.

Backup the cryptheader

When the header of your crypted partition was destroyed, you will not be able to decrypt your data. So creating a backup of the headers and storing them on another disk might be a good idea.

Attention: Many people recommend NOT to backup the cryptheader, even so it's a single point failure! In short, the problem is, that LUKS isn't aware of the duplicated cryptheader, which contains the masterkey which is used to encrypt all files on your partition. Of course this masterkey 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 has got your cryptheader and one of your keys he can decrypt the masterkey and access all your data. Of course the same is true for all backups you create of your partions. 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 [1]Template:Linkrot for further details on this.

Backup

First you have to find out the payload offset of the crypted partition (replace sdaX with the corresponding partition)

cryptsetup luksDump /dev/sdaX | grep "Payload offset"
Payload offset:	4040

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

dd if=/dev/sdaX of=./backup.img bs=512 count=4040

Note: you can also backup the header into a tmpfs/ramfs and encrypt it with gpg or whatever before writing it to a physical disk. Of course you can wrap your encrypted backup into another encryption layer and so on until you feel safe enough :-)

Restore

Be careful before restore: make sure that you chose the right partition (again replace sdaX with the corresponding partition). Restoring the wrong header or restoring to an unencrypted partition will cause data loss.

dd if=./backup.img of=/dev/sdX bs=512 count=4040

Encrypting a loopback filesystem

[This paragraph has been merged from another page; its consistency with the other paragraphs should be improved]

Preparation and mapping

So, let's start by creating an encrypted container!

dd if=/dev/zero of=/bigsecret bs=1M count=10 # you can also use if=/dev/urandom, if you're really paranoid

This will create the file 'bigsecret' with a size of 10 megabytes.

losetup /dev/loop0 /bigsecret

This will create the device node /dev/loop0, so that we can mount/use our container. (Note: if it gives you the error "/dev/loop0: No such file or directory", you need to first load the kernel module with Template:Codeline)

cryptsetup luksFormat /dev/loop0

This will ask you for a password for your new container file. (Note: if you get an error like "Command failed: Failed to setup dm-crypt key mapping. Check kernel for support for the aes-cbc-essiv:sha256 cipher spec and verify that /dev/loop0 contains at least 133 sectors", then run Template:Codeline)

cryptsetup luksOpen /dev/loop0 secret

The encrypted container is now available through the devicefile /dev/mapper/secret. Now we are able to create a partition in the container:

mkfs.ext2 /dev/mapper/secret

and mount it...

mkdir /mnt/secret
mount -t ext2 /dev/mapper/secret /mnt/secret

We can now use the container as if it was a normal partition! To unmount the container:

umount /mnt/secret
cryptsetup luksClose secret
losetup -d /dev/loop0 # free the loopdevice.

so, if you want to mount the container again, you just apply the following commands:

losetup /dev/loop0 /bigsecret
cryptsetup luksOpen /dev/loop0 secret
mount -t ext2 /dev/mapper/secret /mnt/secret

Pretty easy, huh?

Encrypt using a key-file

Let's first generate a 2048 Byte random keyfile :

dd if=/dev/urandom of=keyfile bs=1k count=2

We can now format our container using this key

cryptsetup luksFormat /dev/loop0 keyfile

or our partition :

cryptsetup luksFormat /dev/hda2 keyfile

Once formatted, we can now open the luks device using the key:

cryptsetup -d keyfile luksOpen /dev/loop0 container

You can now like before format the device /dev/mapper/container with your favorite filesystem and then mount it just as easily.

The keyfile is now the only key to your file. I personally advise to encrypt your keyfile using your private GPG key and storing an offsite secured copy of the file.

Resizing the loopback filesystem

First we should unmount the encrypted container:

umount /mnt/secret
cryptsetup luksClose secret
losetup -d /dev/loop0 # free the loopdevice.

After this we need to create a second file with the size of the data we want to add:

dd if=/dev/zero of=zeros bs=1M count=1024

You could use /dev/urandom instead of /dev/zero if you're paranoid, but /dev/zero should be faster on older computers. Next we need to add the created file to our container. Be careful to really use TWO ">", or you will override your current container!

cat zeros >> /bigsecret

Now we have to map the container to the loopdevice:

losetup /dev/loop0 /bigsecret
cryptsetup luksOpen /dev/loop0 secret

After this we will resize the encrypted part of the container to the maximum size of the container file:

cryptsetup resize secret

Finally we can resize the filesystem. Here is an example for ext2/3/4:

e2fsck -f /dev/mapper/secret # Just doing a filesystem check, because it's a bad idea to resize a broken fs
resize2fs /dev/mapper/secret

You can now mount your container again:

mount /dev/mapper/secret /mnt/secret

Encrypting a LVM setup

It's really easy to use encryption with LVM. If you do not know how to set up LVM, then read Installing_with_Software_RAID_or_LVM.

The easiest and best method is to set up LVM on top of the encrypted partition instead of the other way around. This link here is easy to follow and explains everything: Arch Linux: LVM on top of an encrypted partition

The most important thing in setting LVM on top of encryption is, that you need to have encrypt hook before lvm2 hook (and those two before filesystems hook, but that's repeating). Because they are processed in order.

To use encryption on top of LVM, you have to first setup your lvm volumes and then use them as base for the encrypted partitions. That means in short that you have to setup lvm at first. Then follow this guide, but replace all occurrences of /dev/sdXy in the guide with its lvm counterpart. (eg: /dev/sda5 -> /dev/<volume group name>/home).

Don't forget to add the "encrypt" hook in /etc/mkinitcpio.conf before the "lvm2" hook, if you chose to set up encrypted partition on top of LVM. Also remember to change USELVM in /etc/rc.conf to yes.

LVM with Arch Linux Installer (>2009.08)

Since Arch Linux images 2009.08 LVM and dm_crypt is supported by the installer out of the box. This makes it very easy to configure your system for LVM on dm-crypt or vice versa. Actually the configuration is done exactly as without LVM, see the corresponding section above. It differs only in two aspects.

The partition and filesystem choice

Create a small, unencrypted boot partition and use the remaining space for a single partion which can later be split up into multiple logic volumes by LVM.

For a LVM-on-dm-crypt system set up the filesystems and mounting points for example like this:

/dev/sda1   raw->ext2;yes;/boot;no_opts;no_label;no_params
/dev/sda2   raw->dm_crypt;yes;no_mountpoint;no_opts;sda2crypt;-c_aes-xts-plain_-y_-s_512
/dev/mapper/sda2crypt   dm_crypt->lvm-vg;yes;no_mountpoint;no_opts;no_label;no_params
/dev/mapper/sda2crypt+  lvm-pv->lvm-vg;yes;no_mountpoint;no_opts;cryptpool;no_params
/dev/mapper/cryptpool   lvm-vg(cryptpool)->lvm-lv;yes;no_mountpoint;no_opts;cryptroot;10000M|lvm-lv;yes;no_mountpoint;no_opts;crypthome;20000M
/dev/mapper/cryptpool-cryptroot   lvm-lv(cryptroot)->ext3;yes;/;no_opts;cryptroot;no_params
/dev/mapper/cryptpool-crypthome   lvm-lv(crypthome)->ext3;yes;/home;no_opts;cryptroot;no_params

The configuration stage

That's it for the LVM & dm_crypt specific part. The rest is done as usual.

Applying this to a non-root partition

You might get tempted to apply all this fancy stuff to a non-root partition. Arch does not support this out of the box, however, you can easily change the cryptdev and cryptname values in /lib/initcpio/hooks/encrypt (the first one to your /dev/sd* partition, the second to the name you want to attribute). That should be enough.
The big advantage is you can have everything automated, while setting up /etc/crypttab with an external key file (i.e. not on any internal HD partition) can be a pain - you need to make sure the USB/FireWire/... device gets mounted before the encrypted partition, which means you have to change fstab order (at least).
Of course, if the cryptsetup package gets upgraded, you will have to change this script again. However, this solution is to be preferred over hacking rc.sysinit or similar files. Unlike /etc/crypttab, only one partition is supported, but with some further hacking one should be able to have multiple partitions unlocked.


If you want to do this on a software RAID partition, there's one more thing you need to do. Just setting the /dev/mdX device in /lib/initcpio/hooks/encrypt is not enough; the encrypt hook will fail to find the key for some reason, and not prompt for a passphrase either. It looks like the RAID devices aren't brought up until after the encrypt hook is run. You can solve this by putting the RAID array in /boot/grub/menu.lst, like

kernel /boot/vmlinuz26 md=1,/dev/hda5,/dev/hdb5

If you set up your root partition as a RAID array you will notice the similarities with that setup ;-). Grub can handle multiple array definitions just fine:

kernel /boot/vmlinuz26 root=/dev/md0 ro md=0,/dev/sda1,/dev/sdb1 md=1,/dev/sda5,/dev/sdb5,/dev/sdc5

LVM and dm-crypt manually (short version)

Notes

If you are enough smart enough for this, you will be smart enough to ignore/replace LVM-specific things, if you do not want to use LVM.

Note: This brief uses reiserfs for some of the partitions, so change this accordingly if you want to use a more "normal" file system, like ext4.


Partitioning scheme

/dev/sda1 -> /boot
/dev/sda2 -> LVM


The commands

cryptsetup -d /dev/random -c aes-xts-plain -s 512 create lvm /dev/sda2
dd if=/dev/urandom of=/dev/mapper/lvm
cryptsetup remove lvm
lvm pvcreate /dev/sda2
lvm vgcreate lvm /dev/sda2
lvm lvcreate -L 10G -n root lvm
lvm lvcreate -L 500M -n swap lvm
lvm lvcreate -L 500M -n tmp lvm
lvm lvcreate -l 100%FREE -n home lvm
cryptsetup luksFormat -c aes-xts-plain -s 512 /dev/lvm/root
cryptsetup luksOpen /dev/lvm/root root
mkreiserfs /dev/mapper/root
mount /dev/mapper/root /mnt
dd if=/dev/zero of=/dev/sda1 bs=1M
mkreiserfs /dev/sda1
mkdir /mnt/boot
mount /dev/sda1 /mnt/boot
mkdir -p -m 700 /mnt/etc/luks-keys
dd if=/dev/random of=/mnt/etc/luks-keys/home bs=1 count=256


Install Arch Linux

Run /arch/setup


Configuration

/etc/rc.conf

Change USELVM="no" to USELVM="yes".


/etc/mkinitcpio.conf

Put lvm2 and encrypt (in that order) before filesystems in the HOOKS array. Again, note that you are setting encryption on top of LVM.)


/boot/grub/menu.lst

Change root=/dev/hda3 to root=/dev/lvm/root.

For kernel >= 2.6.30, you should change root=/dev/hda3 to the following:

cryptdevice=/dev/lvm/root:root root=/dev/mapper/root


/etc/fstab
/dev/mapper/root        /       reiserfs        defaults        0       1
/dev/sda1               /boot   reiserfs        defaults        0       2
/dev/mapper/tmp         /tmp    tmpfs           defaults        0       0
/dev/mapper/swap        none    swap            sw              0       0


/etc/crypttab
swap	/dev/lvm/swap	SWAP		-c aes-xts-plain -h whirlpool -s 512
tmp	/dev/lvm/tmp	/dev/urandom	-c aes-xts-plain -s 512


After rebooting

The commands
cryptsetup luksFormat -c aes-xts-plain -s 512 /dev/lvm/home /etc/luks-keys/home
cryptsetup luksOpen -d /etc/luks-keys/home /dev/lvm/home home
mkreiserfs /dev/mapper/home
mount /dev/mapper/home /home


/etc/crypttab
home	/dev/lvm/home   /etc/luks-keys/home


/etc/fstab
/dev/mapper/home        /home   reiserfs        defaults        0       0

/ on LVM on LUKS

Make sure your kernel command line looks like this:

root=/dev/mapper/<volume-group>-<logical-volume> cryptdevice=/dev/<luks-part>:<volume-group>

For example:

root=/dev/mapper/vg-arch cryptdevice=/dev/sda4:vg

Or like this:

cryptdevice=/dev/<volume-group>/<logical-volume>:root root=/dev/mapper/root

Resources