Typo
[linux_from_scratch_hints.git] / OLD / cryptoapi.txt
blobe6a3fdda441539419f625a5cca16e6ae6725e98b
1 TITLE:          Encrypted Filesystem Howto
2 LFS VERSION:    All
3 AUTHOR:         Christophe Devine <devine@cr0.net>
5 SYNOPSIS:
6         Make your personal data secure by building your LFS system
7         inside a filesystem encrypted with strong cryptography.
9 HINT:
11 Contents
12 --------
14     0. Changelog
16     1. Setting up the partition layout
18     2. Enabling strong cryto in your current system
19         2.1. Installing Linux-2.4.20
20         2.2. Installing util-linux-2.11y
22     3. Creating the encrypted partition
24     4. Building the LFS system
26     5. Setting up the boot partition
28     6. Setting up the bootscripts
30     7. Boot partition maintenance
33     0. Changelog
34     ------------
36         2003-04-19 - added section 7 (boot partition maintenance)
38         2003-02-19 - some changes, thanks to Jari Ruusu for his comments
40         2003-01-15 - switched to loop-aes, updated the packages version
42         2002-10-19 - first version of the cryptoapi hint released
45     1. Setting up the partition layout
46     ----------------------------------
48 Your hard disk should have at least three partitions:
50   - one small (~ 8 Mb) unencrypted partition (let's say hda1),
51     which will ask the password to mount your encrypted partition.
53   - the encrypted partition holding the LFS system (hda2).
55   - other temporary partitions for the host distribution.
58     2. Enabling strong cryto in your current system
59     -----------------------------------------------
61 The host distribution you're using needs to have strong cryptography
62 support, which is probably not the case. Therefore, you must recompile
63 your kernel and parts of util-linux.
66         2.1. Installing Linux-2.4.20
67         ----------------------------
69 There are two main projects which add strong crypto support in the kernel:
70 CryptoAPI and loop-aes. This hint uses loop-aes, since it has a *FAST* and
71 highly optimized implementation of AES in assembly language, and therefore
72 provides maximum performance if you have an x86 CPU.
74 If necessary, download and unpack the kernel sources:
76 ftp://ftp.kernel.org/pub/linux/kernel/v2.4/linux-2.4.20.tar.bz2
78 You also have to download and unpack:
80 http://loop-aes.sourceforge.net/loop-AES-v1.6i.tar.bz2
82 Then you must patch the kernel:
84 linux-2.4.20 $ patch -Np1 -i ../loop-AES-v1.6i/kernel-2.4.20.diff
86 A small, but yet important bug has to be fixed (read Jari's comment
87 at http://loop-aes.sourceforge.net/loop-AES-v1.6i.important-readme),
88 by patching the loop driver; download and apply the required patch:
90 http://linuxfromscratch.org/~devine/loop-AES-v1.6i-bugfix.patch
92 linux-2.4.20 $ patch -Np1 -i ../loop-AES-v1.6i-bugfix.patch
94 Next, configure your kernel; make sure the following options are set:
96     Block devices  --->
98         <*> Loopback device support
99         [*]   AES encrypted loop device support (NEW)
101 Finally compile the kernel, install it and reboot.
104         2.2. Installing util-linux-2.11y
105         --------------------------------
107 The losetup program, which is part of the util-linux package, must be
108 patched and recompiled in order to add strong cryptography support:
110 First of all, download and unpack:
112 ftp://ftp.kernel.org/pub/linux/utils/util-linux/util-linux-2.11y.tar.bz2
114 Apply the patch provided with loop-aes:
116 util-linux-2.11y $ patch -Np1 -i ../loop-AES-v1.6i/util-linux-2.11y.diff
118 If you wish to use passwords that are less than 20 characters, enter:
120 util-linux-2.11y $ export CFLAGS="-O2 -DLOOP_PASSWORD_MIN_LENGTH=15"
122 If security is important, please do not enable passwords shorter than 20
123 characters. Security is not free, one has to 'pay' in form of long passwords.
125 Compile losetup and install it as root:
127 util-linux-2.11y $ ./configure && make lib mount
128 util-linux-2.11y # cp mount/losetup /sbin
129 util-linux-2.11y # cp mount/losetup.8 /usr/share/man/man8
132     3. Creating the encrypted partition
133     -----------------------------------
135 Fill the target partition with random data:
137 # shred -n 1 -v /dev/hda2
139 Setup the encrypted loop device :
141 # losetup -e aes128 -S xxxxxxxxxx /dev/loop0 /dev/hda2
142 Password:
144 For better security, it is recommended you use the -S xxxxxxxxxx option,
145 where "xxxxxxxxxx" is your chosen seed. This makes optimized dictionary
146 attacks much more difficult.
148 Make sure you don't forget you password ! For minimum security, it should
149 have at least 10 characters and contain letters (both uppercase and lower-
150 case), special characters and numbers.  Also, note that using 256-bit AES
151 would not be any more secure, since even 128-bit AES is almost impossible
152 to crack using brute-force (even with millions of CPU-years).
154 Now create the ext2 (or ext3 or reiserfs) filesystem and mount it:
156 # mke2fs /dev/loop0
157 # mount  /dev/loop0 /mnt/lfs
159 You can compare the encrypted and unencrypted data:
161 # xxd /dev/loop0 | less
162 # xxd /dev/hda2  | less
165     4. Building the LFS system
166     --------------------------
168 Two steps in the book must be adapted :
170     * Chapter 6, Installing util-linux :
171     
172         Use util-linux-2.11y, instead of the version used in the book,
173         and apply the patch as described in section 2.2. of this hint.
175     * Chapter 8, Making the LFS system bootable :
177         Refer to section 5. below :
180     5. Setting up the boot partition
181     --------------------------------
183 The following instructions assume that you're still chrooted inside $LFS.
185 Create and mount the boot partition :
187 # mke2fs /dev/hda1
188 # mkdir /loader
189 # mount -t ext2 /dev/hda1 /loader
191 Create the filesystem hierarchy :
193 # mkdir /loader/{bin,boot,dev,etc,lib,mnt,sbin}
195 Copy the required files in it :
197 # cp /bin/{sh,mount,umount} /loader/bin/
198 # cp /boot/boot-text.b /loader/boot/boot.b
199 # cp -a /dev/{console,hda,hda1,hda2,loop0} /loader/dev/
200 # cp /lib/{ld-linux.so.2,libc.so.6,libdl.so.2,libncurses.so.5} /loader/lib/
201 # cp /sbin/{losetup,pivot_root} /loader/sbin
202 # cat > /loader/sbin/init << "EOF"
203 #!/bin/sh
205 /sbin/losetup -e aes128 -S xxxxxxxxxx /dev/loop0 /dev/hda2
206 /bin/mount -n -t ext2 /dev/loop0 /mnt
208 while [ $? -ne 0 ]
210     /sbin/losetup -d /dev/loop0
211     /sbin/losetup -e aes128 -S xxxxxxxxxx /dev/loop0 /dev/hda2
212     /bin/mount -n -t ext2 /dev/loop0 /mnt
213 done
215 cd /mnt
216 /sbin/pivot_root . loader
217 exec /usr/sbin/chroot . /sbin/init
219 # chmod 755 /loader/sbin/init
220 # cat > /loader/etc/lilo.conf << EOF
221 boot=/dev/hda
222 lba32
223 vga=4
224 default=Linux
225 image=/vmlinuz
226     label=Linux
227     root=/dev/hda1
228     read-only
231 Copy the kernel you've compiled in section 2.1. to /loader/vmlinuz and run:
233 # lilo -r /loader
235 You may also choose not to use /dev/hda1  as a bootloader at all,
236 but instead burn the kernel and the ramdisk on a bootable cd-rom.
239     6. Setting up the bootscripts
240     -----------------------------
242 Make sure your /etc/fstab contains:
244 /dev/loop0      /      ext2    defaults             0 1
246 Also, it is a good idea to check the boot partition integrity inside the
247 encrypted partition, in order to spot if someone, say a government agency
248 like the FBI or the NSA, has modified your boot partition so as to grab
249 your password. Add the following lines at the beginning of the system
250 initialisation script:
253 echo -n "Checking master boot record integrity: "
254 if [ "`dd if=/dev/hda count=1 2>/dev/null | md5sum`" = "e051a4532356709c73b86789acfbdbbd  -" ]
255 then
256     echo "OK."
257 else
258     echo -n "FAILED! press Enter to continue."
259     read
262 echo -n "Checking bootloader integrity: "
263 if [ "`dd if=/dev/hda1 2>/dev/null | md5sum`" = "f3686a17fac8a1090d962bef59c86d3b  -" ]
264 then
265     echo "OK."
266 else
267     echo -n "FAILED! press Enter to continue."
268     read
272 (you should replace the two md5sums above with the correct ones).
275 Now, if you're low on RAM you'll need some swap space. For example,
276 let's say hda3 will hold your encrypted swap partition; you need to
277 create the swap device first:
279 # losetup -e aes128 /dev/loop1 /dev/hda3
280 # mkswap /dev/loop1
282 Then add the following lines at the beginning of the system startup script:
284 echo "password chosen above" | losetup -p 0 -e aes128 /dev/loop1 /dev/hda3
285 swapon /dev/loop1
288     7. Boot partition maintenance
289     -----------------------------
291 In the future, you might want to update your kernel. To do so, /loader
292 must be remounted in read-write mode. If you use a "real" /etc/mtab file
293 (not a symlink to /proc/mounts), run:
295 # echo "/dev/hda1 /loader ext2 ro 0 0" >> /etc/mtab
297 Then:
299 # mount -o remount,rw /loader
301 You can now update the kernel and run lilo. /loader must be remounted
302 in read-only mode:
304 # mount -o remount,ro /loader
306 Finally, don't forget to update the md5sum of the boot partition.