{"id":300,"date":"2009-06-16T05:17:08","date_gmt":"2009-06-16T03:17:08","guid":{"rendered":"http:\/\/raftaman.net\/?p=300"},"modified":"2021-05-15T11:46:42","modified_gmt":"2021-05-15T09:46:42","slug":"300","status":"publish","type":"post","link":"https:\/\/possiblelossofprecision.net\/?p=300","title":{"rendered":"Unlocking a luks volume with a USB key"},"content":{"rendered":"\n<p>A <a href=\"http:\/\/en.wikipedia.org\/wiki\/Linux_Unified_Key_Setup\">luks<\/a> encrypted disk partition is great. The only thing that can bug you from time to time is that you have to specify the key before you can use it. Or maybe, if you try to mount the volume with <tt.>\/etc\/fstab<\/code>, you&#8217;ll be prompted for the password during boot.<\/p>\n<p>Wouldn&#8217;t it be great, if you could use a real <em>key<\/em> to unlock your encrypted volume? Not a keyfile, but a physically existent key like the ones you use to unlock your front door?!<\/p>\n<p>Well, it&#8217;s not actually a key, but these <a href=\"http:\/\/www.lacie.com\/products\/range.htm?id=10052\">LaCie USB Flash Drives<\/a> come very close:<\/p>\n<p><a href=\"http:\/\/www.amazon.com\/gp\/product\/B001V7XPSA?ie=UTF8&#038;tag=devblog04-21&#038;linkCode=as2&#038;camp=1638&#038;creative=6742&#038;creativeASIN=B001V7XPSA\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/possiblelossofprecision.net\/wordpress\/wp-content\/uploads\/2009\/06\/Keys_3_ring.jpg\" alt=\"LaCie iamaKey USB Flash Drives\" title=\"LaCie iamaKey USB Flash Drives\" width=\"630\" height=\"450\" class=\"alignnone size-full wp-image-309\" srcset=\"https:\/\/possiblelossofprecision.net\/wordpress\/wp-content\/uploads\/2009\/06\/Keys_3_ring.jpg 630w, https:\/\/possiblelossofprecision.net\/wordpress\/wp-content\/uploads\/2009\/06\/Keys_3_ring-300x214.jpg 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/p>\n<p>This article will show you, how to <a href=\"#section1\">generate a random key<\/a> for your luks encrypted volume, <a href=\"#section3\">hide it on any USB flash drive<\/a> and use <a href=\"#section4\">udev to unlock<\/a> and <a href=\"#section5\">mount<\/a> your luks volume whenever you plug this flash drive into a USB port<\/p>\n<p><!--more--><\/p>\n<h2><a name=\"section1\">1. Generating a random keyfile<\/a><\/h2>\n<p>First, we need a random keyfile. Linux normally comes with two different random number generators: A blocking one called <a href=\"http:\/\/en.wikipedia.org\/wiki\/\/dev\/random\"><code>\/dev\/random<\/code><\/a> and its non-blocking counterpart <a href=\"http:\/\/en.wikipedia.org\/wiki\/\/dev\/urandom\"><code>\/dev\/urandom<\/code><\/a>.<\/p>\n<p>The size of your keyfile and which RNG you use is totally up to you. Quite often you&#8217;ll find tutorials, that recommend something like<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\ndd if=\/dev\/urandom of=secretkey bs=512 count=4\r\n<\/pre>\n<p>which generates a 2048byte or 2<sup>14<\/sup>bit keyfile. For more the paranoid under us:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\ndd if=\/dev\/random of=secretkey bs=1 count=4096\r\n<\/pre>\n<p>which generates a 4096byte or 2<sup>15<\/sup>bit keyfile. Notice that this uses the non-blocking RNG <code>\/dev\/random<\/code> and therefore can take quite some time (5mins+) depending on the current filling degree of the entropy pool.<\/p>\n<h2><a name=\"section2\">2. Adding keyfile to LUKS-Volume<\/a><\/h2>\n<p>Adding this keyfile to your existing luks volume is no big deal<\/p>\n<pre class=\"brush: plain; highlight: [1]; light: true; title: ; notranslate\" title=\"\">\r\n# cryptsetup luksAddKey \/dev\/md0 secretkey \r\nEnter any LUKS passphrase: \r\nVerify passphrase: \r\nkey slot 0 unlocked.\r\nCommand successful.\r\n<\/pre>\n<p>where <code>\/dev\/md0<\/code> of course is the path to your luks device or partition<\/p>\n<h2><a name=\"section3\">3. Hiding key<\/a><\/h2>\n<p>You could now just copy this keyfile to your USB drive as you can do it with any other file. But someone looking for the key would easily find it. So we&#8217;ll hide the key directly between MBR and the first partition.<\/p>\n<p><strong>WARNING: You should only follow this step if you know what you are doing &#8211; it can cause data loss and damage your partitions or MBR on the stick!<\/strong><\/p>\n<p>If you have a bootloader installed on your drive you have to adjust the values, e.g. Grub needs the first 16 sectors, so you would have to replace seek=4 with seek=16; otherwise you will 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. <\/p>\n<p><em>Optional:<\/em><\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\ndd if=\/dev\/usbstick of=64sectors bs=512 count=64  # copy first 64 sectors\r\nghex2 64sectors                                   # determine free space\r\n<\/pre>\n<p>Now you can write your key to the disk:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\ndd if=secretkey of=\/dev\/usbstick bs=512 seek=4\r\n<\/pre>\n<p>You should not simply use <code>rm<\/code> to delete the keyfile because <code>rm<\/code> only unlinks it from your filesystem (it would still be left physically intact). If everything went fine you can overwrite and delete your temporary secretkey with<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\nshred --remove --zero secretkey\r\n<\/pre>\n<h2><a name=\"section4\">4. Udev Auto-Magic<\/a><\/h2>\n<p>We need to achieve two things: First, we have to make sure our USB drive containing the key can always be found under the same name. Second, we need to execute a shell script that unlocks the luks volume and mounts it whenever the USB drive is plugged into a USB port. This can be done with a small udev-rule<\/p>\n<pre class=\"brush: plain; highlight: [5,6]; light: true; title: ; notranslate\" title=\"\">\r\nBUS==&quot;usb&quot;, \r\nKERNEL==&quot;sd*&quot;, \r\nATTRS{manufacturer}==&quot;laCie&quot;, \r\n&#x5B;...]\r\nSYMLINK+=&quot;usbkey%n&quot;, \r\nRUN+=&quot;\/usr\/local\/bin\/unlock-luks&quot;\r\n<\/pre>\n<p>A tutorial on how to write udev-rules would go way beyond the scope of this article. Notice the highlighted lines: <code>SYMLINK+=\"usbkey%n\"<\/code> ensures, that our USB drive can be found under <code>\/dev\/usbkey<\/code> and <code>RUN+=\"\/usr\/local\/bin\/unlock-luks\"<\/code> runs a shell script every time we plug it in.<\/p>\n<p>Save it as <code>\/etc\/udev\/rules.d\/99-unlock-lucks.rules<\/code> and reload all udev rules with<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n# udevadm control --reload-rules\r\n<\/pre>\n<p>To make sure that this happens only when the USB drive containing the key is plugged in, you have to specify some more attributes besides <code>ATTRS{manufacturer}<\/code>. You can query those attributes with <a href=\"https:\/\/possiblelossofprecision.net\/?p=343\">udevadm<\/a>. A nice document describing how to write udev rules can be found on <a href=\"http:\/\/www.reactivated.net\/writing_udev_rules.html\">http:\/\/www.reactivated.net\/writing_udev_rules.html<\/a>.<\/p>\n<h2><a name=\"section5\">5. Mounting script<\/a><\/h2>\n<p>The <a href=\"#section4\">udev-rule<\/a> runs <code>\/usr\/loca\/bin\/unlock-luks<\/code> every time the USB drive containing the key is plugged in. We can do nearly everything within this script but it suggests itself to unlock the luks volume and mount it somewhere:<\/p>\n<pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\ndd if=\/dev\/usbkey bs=512 skip=4 count=8 | cryptsetup luksOpen \/dev\/md0 luksVolume --key-file=- &amp;&amp; mount \/dev\/mapper\/luksVolume \/mnt\/\r\n<\/pre>\n<p>Notice, that this skips the first 2048bytes and reads the next 4096bytes. If you generated a smaller of bigger keyfile, or placed you keyfile somewhere else on you USB drive in <a href=\"#section3\">Section 3<\/a>, YMWV!<\/p>\n<p>\n","protected":false},"excerpt":{"rendered":"<p>A luks encrypted disk partition is great. The only thing that can bug you from time to time is that you have to specify the key before you can use it. Or maybe, if you try to mount the volume with \/etc\/fstab, you&#8217;ll be prompted for the password during boot. Wouldn&#8217;t it be great, if you could use a real&#8230; <a href=\"https:\/\/possiblelossofprecision.net\/?p=300\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,20,19],"class_list":["post-300","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-hardware","tag-luks","tag-udev"],"_links":{"self":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=300"}],"version-history":[{"count":90,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions"}],"predecessor-version":[{"id":1831,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions\/1831"}],"wp:attachment":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}