<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>
<channel>
	<title>G-Loaded Journal &#187; Container</title>
	<atom:link href="http://www.g-loaded.eu/tag/container/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.g-loaded.eu</link>
	<description>An open-source software and technology related journal</description>
	<lastBuildDate>Mon, 05 Dec 2011 19:55:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>Encrypt devices using dm-crypt and LUKS</title>
		<link>http://www.g-loaded.eu/2005/11/10/encrypt-devices-using-dm-crypt-and-luks/</link>
		<comments>http://www.g-loaded.eu/2005/11/10/encrypt-devices-using-dm-crypt-and-luks/#comments</comments>
		<pubDate>Thu, 10 Nov 2005 14:28:22 +0000</pubDate>
		<dc:creator>George Notaras</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[Container]]></category>
		<category><![CDATA[Desktop]]></category>
		<category><![CDATA[dm-crypt]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Filesystem]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[LUKS]]></category>
		<category><![CDATA[Security]]></category>
		<guid isPermaLink="false">http://www.g-loaded.eu/2005/11/10/encrypt-devices-using-dm-crypt-and-luks/</guid>
		<description><![CDATA[Secure your data in encrypted partitions and removable volumes, or even volumes within a file, using dm-crypt and the flexibility of LUKS. In this article I describe how I encrypted a ZIP diskette and a partition contained in a file.]]></description>
			<content:encoded><![CDATA[<p>&quot;<em><strong>Encryption</strong> is the process of obscuring information to make it unreadable without special knowledge.</em>&quot; -WikiPedia-</p>
<p>There are many different methods to encrypt data using various encryption algorithms (ciphers). In this document I describe in short how to encrypt a device with one of the most contemporary methods, using <strong>dm-crypt</strong> and <strong>LUKS</strong>. Actually, devices cannot be encrypted. It&#8217;s the block devices which are volumes that can be. This means that you can encrypt a hard disk partition, a ZIP disk, a usb flash stick, or even a volume within a file.<br />
<span id="more-96"></span><br />
<strong>dm-crypt</strong> is a device mapper that uses the 2.6 kernel&#8217;s cryptoapi. We will not use dm-crypt directly to setup the block device mappings because of its complexity, but instead we&#8217;ll use an enhanced version of a program called <strong>cryptsetup</strong>, which has the <strong>LUKS</strong> extension enabled. The reason for using LUKS (Linux Uniﬁed Key Setup) is that it uses a partition header to store the encryption-setup information, so, in contrast to other existing methods, the user can:</p>
<ul>
<li>change the encrypted volume&#8217;s passphrase without having to re-encrypt the data</li>
<li>have multiple passphrases for the same data</li>
<li>transport or migrate data on different systems</li>
</ul>
<p>The most important thing though is that all this convenience does not have an impact on data security, meaning that data is still very safe. Well, this is LUKS. Win32 compatibility is not yet possible, but will be in the near future through <a href="http://www.truecrypt.org/">TrueCrypt</a>.</p>
<h4>Prerequisites</h4>
<p>A Linux 2.6 kernel with <strong>device mapper</strong> and <strong>dm-crypt</strong> support is needed. On Fedora Core 4 systems you really don&#8217;t have to do anything as the kernel has device mapper and dm-crypt enabled as modules (<em>dm-mod</em> and <em>dm-crypt</em> respectively). The modules are loaded automatically when the system needs them.</p>
<p>Before reading any further, <strong>you must be sure</strong> that you know the exact device node you are going to encrypt. This is no joke, as you can <strong>lose your data</strong>, by a simple and fool mistake. So, take note!</p>
<p>Furthermore, you need to install <strong>cryptsetup-luks</strong> package and <strong>util-linux</strong> package. We will need the latter for encrypting a filesystem within a file, but this is probably already installed.</p>
<h4>Encrypting a volume</h4>
<p>In this section I will encrypt a ZIP disk. The very same process can be used for any block device that is a volume, meaning that it can be formatted with a mountable filesystem. So, you can encrypt a hard disk partition, a USB stick, a Compact Flash card etc.</p>
<p>First of all, <strong>be sure</strong> that you know your volume&#8217;s exact device node in /dev. If you use multiple hotplugable devices, which you connect to your machine in a random order, you should consider writing some UDEV rules, so that you know which node corresponds with a certain actual device. You can read my <a href="http://www.g-loaded.eu/2005/09/18/udev/">blog post</a> on that. A simple mistake can lead to loss of data. I have setup UDEV in a such way, that every time I connect my USB ZIP drive and insert a disk, a symlink to the disk&#8217;s node is created in a directory /dev/mydev/. So, I can reach the ZIP diskette by directing the commands to the symlink /dev/mydev/zipdisk.</p>
<p>If your volume is already mounted, <strong>unmount</strong> it before proceeding. Mine is, so I run:</p>
<pre class="console"># umount /dev/mydev/zipdisk</pre>
<h5>Fill the disk with random data</h5>
<p>Filling the disk with random data before encrypting is a good habit and it strengthens security as well. If you don&#8217;t want to do so, you can simply skip this step. This process is CPU intensive and the time it takes depends on the available CPU horsepower, the size of the partition and the speed it can be written. It can be many hours, even days, for very large hard disk partitions. The worst thing is that there is no progress indicator, so you just wait for it to finish. I run this:</p>
<pre class="console"># dd if=/dev/urandom of=/dev/mydev/zipdisk</pre>
<p>Substitute /dev/mydev/zipdisk with the path to your device node. In my case, it took my USB 1.1 ZIP drive 10 whole minutes to fill an 100MB ZIP disk. Well, it&#8217;s not the fastest thing in the world, but I still like it.</p>
<h5>Create the LUKS partition</h5>
<p>Now, I will create a LUKS partition on the ZIP disk. As root I run:</p>
<pre class="console"># cryptsetup --verbose --cipher &quot;aes-cbc-essiv:sha256&quot; --key-size 256 --verify-passphrase luksFormat /dev/mydev/zipdisk</pre>
<p>If you use a kernel older than 2.6.10, do not include the &#8211;cipher option. The default AES with 256-bit key is absolutely fine. Cryptsetup will ask for a passphrase twice. What this does is the initialization of the LUKS partition.</p>
<h5>Set up the device mapping</h5>
<p>In order to use this partition, a device mapping must be set up between the physical partition on the ZIP diskette and a new virtual block device, which can then be mounted. I call it virtual because it&#8217;s just a layer between the physical ZIP disk and the system. Writes to this virtual block device will be encrypted and reads decrypted. To create the device mapping as root:</p>
<pre class="console"># cryptsetup luksOpen /dev/mydev/zipdisk encr-zipdisk</pre>
<p><strong>encr-zipdisk</strong> is just a name for the new block device that is created in <strong>/dev/mapper/</strong>. After creating a filesystem on it, this virtual block device can be mounted and used as normal.</p>
<h5>Create a filesystem on the new block device</h5>
<p>You can create any file system you like on the new volume. For my ZIP diskette, an MSDOS FAT16 filesystem is more than enough, so I run as root:</p>
<pre class="console"># mkdosfs -v -F 16 -n &quot;ENCR1&quot; /dev/mapper/encr-zipdisk</pre>
<p>What worths mentioning is that you create the filesystem on the virtual volume, /dev/mapper/encr-zipdisk in my case, and not the physical one.</p>
<h5>Mount the new volume</h5>
<p>Now I can mount the volume as normal. For example:</p>
<pre class="console"># mount -t vfat -o rw /dev/mapper/encr-zipdisk /mnt/tmp/</pre>
<p>An fstab entry with all the proper mount options could simplify mounting. All data that is written to the disk gets encrypted.</p>
<h5>Unmounting</h5>
<p>Before removing the disk from the drive, I have to unmount it:</p>
<pre class="console"># umount /mnt/tmp/</pre>
<p>And then delete the device mapping as root:</p>
<pre class="console"># cryptsetup luksClose encr-zipdisk</pre>
<p>This removes the association and I normally can eject the ZIP disk.</p>
<h5>In short</h5>
<p>Just a review of the procedure to mount and unmount the disk, considering that a proper fstab entry has been added.</p>
<p>To mount:</p>
<pre class="console"># cryptsetup luksOpen /dev/mydev/zipdisk encr-zipdisk
# mount /dev/mapper/encr-zipdisk</pre>
<p>To unmount:</p>
<pre class="console"># umount /dev/mapper/encr-zipdisk
# cryptsetup luksClose encr-zipdisk</pre>
<p>Pretty fast and easy.</p>
<h4>Encrypt a filesystem within a file</h4>
<p>In order to use LUKS to encrypt a filesystem that is contained in a file, you actually have to follow the same steps as when encrypting a physical partition, plus two. These include:</p>
<ul>
<li>The creation of a file that will contain the encrypted partition</li>
<li>Set up an association between this file and a free loop device, so that it can be used by cryptsetup as a block device. At the moment, cryptsetup cannot use a file as a block device directly. That&#8217;s why this step is needed.</li>
</ul>
<p>So, let&#8217;s create the file. The following command creates an 100MB file, named &quot;<strong>container1</strong>&quot;, which is full of random data:</p>
<pre class="console">dd if=/dev/urandom of=container1 bs=1024 count=100000</pre>
<p>To create a mapping between this file and a free loop device, we&#8217;ll use <strong>losetup</strong> (part of util-linux). Check which loop device is free in your system with the command:</p>
<pre class="console">losetup -f</pre>
<p>For me it was <em>/dev/loop0</em>. So, I map the &quot;container1&quot; file to /dev/loop0. As root:</p>
<pre class="console"># losetup /dev/loop0 /path/to/container1</pre>
<p>From now on, the steps are exactly the same as before. We just use /dev/loop0 instead of the ZIP disk:</p>
<pre class="console"># cryptsetup --verbose --cipher &quot;aes-cbc-essiv:sha256&quot; --key-size 256 --verify-passphrase luksFormat /dev/loop0
# cryptsetup luksOpen /dev/loop0 encr-container1
# mkfs.ext3 /dev/mapper/encr-container1
# mount -t ext3 -o rw,defaults /dev/mapper/encr-container1 /mnt/tmp/</pre>
<p>We can now copy some files to our encrypted partition, like on a regular disk partition. We unmount it and delete the device mappings with the following commands:</p>
<pre class="console"># umount /mnt/tmp/
# cryptsetup luksClose encr-container1
# losetup -d /dev/loop0</pre>
<p>So, to mount a LUKS encrypted filesystem within a file you need to create <strong>two device mappings</strong> before you mount it for use. Of course some automation can be achieved using scripts, but you will still have to supply the passphrase in order to use the encrypted partitions.</p>
<h4>Further Reading</h4>
<p>Apart from reading the cryptsetup and losetup man pages, you can check the following URLs for more info:</p>
<ol>
<li><a href="http://www.saout.de/misc/dm-crypt/">The dm-crypt homepage</a></li>
<li><a href="http://www.saout.de/tikiwiki/tiki-index.php">The dm-crypt wiki</a></li>
<li><a href="http://www.saout.de/tikiwiki/tiki-index.php?page=EncryptedDeviceUsingLUKS">A HOWTO about encrypting a USB HDD</a></li>
<li><a href="http://luks.endorphin.org/">The LUKS homepage</a></li>
</ol>
<div class="cc-block"><em><a href="http://www.g-loaded.eu/2005/11/10/encrypt-devices-using-dm-crypt-and-luks/">Encrypt devices using dm-crypt and LUKS</a></em>, unless otherwise expressly stated, is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License</a>. Terms and conditions beyond the scope of this license may be available at <a href="http://www.g-loaded.eu/about/disclaimer-and-license/">www.g-loaded.eu</a>.</div>
<h4>Related Articles</h4>
<ul><li><a href="http://www.g-loaded.eu/2006/11/05/truecrypt-on-2618-kernels/" rel="bookmark">TrueCrypt on 2.6.18 kernels</a></li>
<li><a href="http://www.g-loaded.eu/2005/09/19/a-different-approach-hal/" rel="bookmark">A different approach&#8230;HAL</a></li>
<li><a href="http://www.g-loaded.eu/2005/09/18/udev/" rel="bookmark">UDEV</a></li>
<li><a href="http://www.g-loaded.eu/2005/11/10/packet-writing-on-cdrw-and-dvdrw-media/" rel="bookmark">Packet Writing on CDRW and DVDRW media</a></li>
<li><a href="http://www.g-loaded.eu/2009/01/04/always-use-a-block-device-label-or-its-uuid-in-fstab/" rel="bookmark">Always use a block device label or its UUID in fstab</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.g-loaded.eu/2005/11/10/encrypt-devices-using-dm-crypt-and-luks/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

