| 28 |
rodolico |
1 |
#! /usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
use warnings;
|
|
|
5 |
use Data::Dumper;
|
|
|
6 |
use File::Path qw(make_path);
|
|
|
7 |
|
|
|
8 |
# script will create the key files used for GELI encryption
|
|
|
9 |
# if only the combined key file is provided, it will be created as a single key.
|
|
|
10 |
# if both local key files are provided, the combined key will be split into
|
|
|
11 |
# the two local keys using XOR, so that both local keys are required to
|
|
|
12 |
# reconstruct the combined key.
|
|
|
13 |
|
|
|
14 |
# these are the names of the key files used to create the GELI encryption
|
|
|
15 |
# if both local key files are provided, the combined key will be split into
|
|
|
16 |
# the two local keys using XOR, so that both local keys are required to
|
|
|
17 |
# reconstruct the combined key.
|
|
|
18 |
# if only the combined key file is provided, it will be created as a single key.
|
|
|
19 |
my $rootKeyLocation = '/tmp/keys/'; # '/media/passkey/'; # should be a tmpfs or ramdisk so no record is left on disk
|
|
|
20 |
my $localKeyFile1 = $rootKeyLocation . 'local_geli_1.key';
|
|
|
21 |
my $localKeyFile2 = $rootKeyLocation . 'local_geli_2.key';
|
|
|
22 |
my $combinedKeyFile = $rootKeyLocation . 'combined_geli.key';
|
|
|
23 |
|
|
|
24 |
# sub will take three file names:
|
|
|
25 |
# 1: combined key file to create
|
|
|
26 |
# 2: local key file 1 to create (optional)
|
|
|
27 |
# 3: local key file 2 to create (optional)
|
|
|
28 |
# If both local key files are provided, the combined key will be split into
|
|
|
29 |
# the two local keys using XOR, so that both local keys are required to
|
|
|
30 |
# reconstruct the combined key.
|
|
|
31 |
# returns the actual contents of the combined key, local key 1, and local key 2 as array
|
|
|
32 |
sub makeKeys {
|
|
|
33 |
$combinedKeyFile = shift;
|
|
|
34 |
$localKeyFile1 = shift;
|
|
|
35 |
$localKeyFile2 = shift;
|
|
|
36 |
# the actual contents of each key file, returned as raw binary strings for further processing
|
|
|
37 |
my $combinedKey;
|
|
|
38 |
my $localKey1;
|
|
|
39 |
my $localKey2;
|
|
|
40 |
# create a new combined key file if it does not exist
|
|
|
41 |
$combinedKey = make256bitKeyFile( $combinedKeyFile );
|
|
|
42 |
# if both local key file names are provided, create them.
|
|
|
43 |
if ( $localKeyFile1 && $localKeyFile2 ) {
|
|
|
44 |
$localKey1 = make256bitKeyFile ( $localKeyFile1 );
|
|
|
45 |
$localKey2 = xorKeys( $combinedKey, $localKey1 );
|
|
|
46 |
writeKeyfile( $localKeyFile2, $localKey2 );
|
|
|
47 |
}
|
|
|
48 |
return ( $combinedKey, $localKey1, $localKey2 );
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
# unpack and display a binary key as hex with a label
|
|
|
52 |
sub displayKey {
|
|
|
53 |
my ( $key, $label ) = @_;
|
|
|
54 |
die "No key provided to displayKey\n" unless defined $key;
|
|
|
55 |
die "No label provided to displayKey\n" unless defined $label;
|
|
|
56 |
my $hex = unpack( 'H*', $key );
|
|
|
57 |
print "$label: $hex\n";
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
sub runCommand {
|
|
|
61 |
my $command = shift;
|
|
|
62 |
print "$command\n";
|
|
|
63 |
`$command`;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
sub readKeyfile {
|
|
|
67 |
my $keyfile = shift;
|
|
|
68 |
die "No keyfile provided to readKeyfile\n" unless defined $keyfile;
|
|
|
69 |
open( my $fh, '<:raw', $keyfile ) or die "Cannot open keyfile $keyfile: $!";
|
|
|
70 |
my $key;
|
|
|
71 |
my $read = read($fh, $key, 32);
|
|
|
72 |
close $fh;
|
|
|
73 |
die "Failed to read 32 bytes from $keyfile (got " . (defined $read ? $read : 0) . ")\n" unless defined $read && $read == 32;
|
|
|
74 |
return $key; # raw 32-byte binary key
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
sub writeKeyfile {
|
|
|
78 |
my ( $keyfile, $key ) = @_;
|
|
|
79 |
die "No keyfile provided to writeKeyfile\n" unless defined $keyfile;
|
|
|
80 |
die "No key provided to writeKeyfile\n" unless defined $key;
|
|
|
81 |
open( my $fh, '>:raw', $keyfile ) or die "Cannot open keyfile $keyfile for writing: $!";
|
|
|
82 |
my $written = syswrite( $fh, $key );
|
|
|
83 |
close $fh;
|
|
|
84 |
die "Failed to write 32 bytes to $keyfile (wrote " . (defined $written ? $written : 0) . ")\n" unless defined $written && $written == 32;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
sub xorKeys {
|
|
|
88 |
my ( $key1, $key2 ) = @_;
|
|
|
89 |
die "No key1 provided to xorKeys\n" unless defined $key1;
|
|
|
90 |
die "No key2 provided to xorKeys\n" unless defined $key2;
|
|
|
91 |
die "Keys must be 32 bytes each\n" unless length($key1) == 32 && length($key2) == 32;
|
|
|
92 |
|
|
|
93 |
my $result = $key1 ^ $key2;
|
|
|
94 |
return $result;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
# create a new 256-bit (32 byte) random key file unless it already exixts
|
|
|
98 |
# returns the raw binary contents of the key
|
|
|
99 |
sub make256bitKeyFile {
|
|
|
100 |
my $keyfile = shift;
|
|
|
101 |
die "No keyfile name provided to make256bitKeyFile\n" unless defined $keyfile;
|
|
|
102 |
if ( -f $keyfile ) {
|
|
|
103 |
print "Key file $keyfile already exists, not overwriting\n";
|
|
|
104 |
} else {
|
|
|
105 |
print "Creating new key file $keyfile\n";
|
|
|
106 |
if ( -x '/usr/bin/openssl' ) {
|
|
|
107 |
# openssl prefers /dev/urandom automatically
|
|
|
108 |
runCommand( "openssl rand -out $keyfile 32" );
|
|
|
109 |
} else {
|
|
|
110 |
# dd displays too much output, so redirect to /dev/null (stdout and stderr)
|
|
|
111 |
runCommand( "dd if=/dev/random of=$keyfile bs=32 count=1 >/dev/null 2>&1" );
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
return readKeyfile( $keyfile );
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
# ensure the root key location directory exists
|
|
|
118 |
make_path( $rootKeyLocation ) unless -d $rootKeyLocation;
|
|
|
119 |
|
|
|
120 |
# create the key(s) used for GELI encryption
|
|
|
121 |
my ( $combinedKey, $localKey1, $localKey2 ) = makeKeys( $combinedKeyFile, $localKeyFile1, $localKeyFile2 );
|
|
|
122 |
# show the results as ASCII hex for recording
|
|
|
123 |
print "Keys created, please record this sensitive information for your records:\n";
|
|
|
124 |
displayKey( $combinedKey, 'Combined Key' );
|
|
|
125 |
displayKey( $localKey1, 'Local Key 1' ) if defined $localKey1;
|
|
|
126 |
displayKey( $localKey2, 'Local Key 2' ) if defined $localKey2;
|