-
Notifications
You must be signed in to change notification settings - Fork 9
/
luks1_test.go
154 lines (124 loc) · 3.92 KB
/
luks1_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package luks
import (
"os"
"os/exec"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func prepareLuks1Disk(t *testing.T, password string, cryptsetupArgs ...string) (*os.File, error) {
disk, err := os.CreateTemp("", "luksv1.go.disk")
require.NoError(t, err)
require.NoError(t, disk.Truncate(2*1024*1024))
args := []string{"luksFormat", "--type", "luks1", "--iter-time", "5", "-q", disk.Name()}
args = append(args, cryptsetupArgs...)
cmd := exec.Command("cryptsetup", args...)
cmd.Stdin = strings.NewReader(password)
if testing.Verbose() {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
require.NoError(t, cmd.Run())
return disk, err
}
func runLuks1Test(t *testing.T, cryptsetupArgs ...string) {
t.Parallel()
password := "foobar"
disk, err := prepareLuks1Disk(t, password, cryptsetupArgs...)
require.NoError(t, err)
defer disk.Close()
defer os.Remove(disk.Name())
d, err := initV1Device(disk.Name(), disk)
require.NoError(t, err)
tokens, err := d.Tokens()
require.NoError(t, err)
require.Empty(t, tokens)
_, err = d.UnsealVolume(0, []byte(password))
require.NoError(t, err)
uuid, err := blkidUUID(disk.Name())
require.NoError(t, err)
require.Equal(t, uuid, d.UUID())
}
func TestLuks1Unlock(t *testing.T) {
runLuks1Test(t)
}
func TestLuks1Sha256(t *testing.T) {
runLuks1Test(t, "--hash", "sha256")
}
func TestLuks1Sha512(t *testing.T) {
runLuks1Test(t, "--hash", "sha512")
}
func TestLuks1UnlockMultipleKeySlots(t *testing.T) {
t.Parallel()
password := "barfoo"
disk, err := prepareLuks1Disk(t, password)
require.NoError(t, err)
defer disk.Close()
defer os.Remove(disk.Name())
// now let's add a new keyslot and try to unlock again
addKeyCmd := exec.Command("cryptsetup", "luksAddKey", "-q", disk.Name())
password2 := "newpwd111"
addKeyCmd.Stdin = strings.NewReader(password + "\n" + password2)
if testing.Verbose() {
addKeyCmd.Stdout = os.Stdout
addKeyCmd.Stderr = os.Stderr
}
require.NoError(t, addKeyCmd.Run())
d, err := initV1Device(disk.Name(), disk)
require.NoError(t, err)
tokens, err := d.Tokens()
require.NoError(t, err)
require.Empty(t, tokens)
_, err = d.UnsealVolume(0, []byte(password))
require.NoError(t, err)
_, err = d.UnsealVolume(1, []byte(password2))
require.NoError(t, err)
}
func TestReadLuksMetaInitialized(t *testing.T) {
t.Parallel()
password := "barfoo"
disk, err := prepareLuks1Disk(t, password)
require.NoError(t, err)
defer disk.Close()
defer os.Remove(disk.Name())
// now let's init luksmeta slots
initMeta := exec.Command("luksmeta", "init", "-f", "-d", disk.Name())
if testing.Verbose() {
initMeta.Stdout = os.Stdout
initMeta.Stderr = os.Stderr
}
require.NoError(t, initMeta.Run())
uuid1 := "6a6888f3-445d-479b-bc39-1b64e7215464"
saveMeta1 := exec.Command("luksmeta", "save", "-d", disk.Name(), "-s", "3", "-u", uuid1)
saveMeta1.Stdin = strings.NewReader("testdata1")
if testing.Verbose() {
saveMeta1.Stdout = os.Stdout
saveMeta1.Stderr = os.Stderr
}
require.NoError(t, saveMeta1.Run())
uuid2 := "f1e1503f-6123-4369-a0fc-58bbe0df93c0"
saveMeta2 := exec.Command("luksmeta", "save", "-d", disk.Name(), "-s", "6", "-u", uuid2)
saveMeta2.Stdin = strings.NewReader("testdata2")
if testing.Verbose() {
saveMeta2.Stdout = os.Stdout
saveMeta2.Stderr = os.Stderr
}
require.NoError(t, saveMeta2.Run())
d, err := initV1Device(disk.Name(), disk)
require.NoError(t, err)
tokens, err := d.Tokens()
require.NoError(t, err)
require.Len(t, tokens, 2)
t1 := tokens[0]
require.Equal(t, 3, t1.ID)
require.Equal(t, "testdata1", string(t1.Payload), "Wrong metadata for token %d", t1.ID)
t2 := tokens[1]
require.Equal(t, 6, t2.ID)
require.Equal(t, "testdata2", string(t2.Payload), "Wrong metadata for token %d", t2.ID)
uuid, err := blkidUUID(disk.Name())
require.NoError(t, err)
require.Equal(t, uuid, d.UUID())
// check that we can unlock data for a partition with luks tokens
_, err = d.UnsealVolume(0, []byte(password))
require.NoError(t, err)
}