summaryrefslogtreecommitdiffstats
path: root/v1header.go
blob: ded4a61db9c78948331bcafe598b4b1e32d5e802 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package luksy

import (
	"encoding/binary"
	"fmt"
	"syscall"
)

type (
	V1Header  [592]uint8
	V1KeySlot [48]uint8
)

const (
	// Mostly verbatim from LUKS1 On-Disk Format Specification version 1.2.3
	V1Magic               = "LUKS\xba\xbe"
	v1MagicStart          = 0
	v1MagicLength         = 6
	v1VersionStart        = v1MagicStart + v1MagicLength
	v1VersionLength       = 2
	v1CipherNameStart     = v1VersionStart + v1VersionLength
	v1CipherNameLength    = 32
	v1CipherModeStart     = v1CipherNameStart + v1CipherNameLength
	v1CipherModeLength    = 32
	v1HashSpecStart       = v1CipherModeStart + v1CipherModeLength
	v1HashSpecLength      = 32
	v1PayloadOffsetStart  = v1HashSpecStart + v1HashSpecLength
	v1PayloadOffsetLength = 4
	v1KeyBytesStart       = v1PayloadOffsetStart + v1PayloadOffsetLength
	v1KeyBytesLength      = 4
	v1MKDigestStart       = v1KeyBytesStart + v1KeyBytesLength
	v1MKDigestLength      = v1DigestSize
	v1MKDigestSaltStart   = v1MKDigestStart + v1MKDigestLength
	v1MKDigestSaltLength  = v1SaltSize
	v1MKDigestIterStart   = v1MKDigestSaltStart + v1MKDigestSaltLength
	v1MKDigestIterLength  = 4
	v1UUIDStart           = v1MKDigestIterStart + v1MKDigestIterLength
	v1UUIDLength          = 40
	v1KeySlot1Start       = v1UUIDStart + v1UUIDLength
	v1KeySlot1Length      = 48
	v1KeySlot2Start       = v1KeySlot1Start + v1KeySlot1Length
	v1KeySlot2Length      = 48
	v1KeySlot3Start       = v1KeySlot2Start + v1KeySlot2Length
	v1KeySlot3Length      = 48
	v1KeySlot4Start       = v1KeySlot3Start + v1KeySlot3Length
	v1KeySlot4Length      = 48
	v1KeySlot5Start       = v1KeySlot4Start + v1KeySlot4Length
	v1KeySlot5Length      = 48
	v1KeySlot6Start       = v1KeySlot5Start + v1KeySlot5Length
	v1KeySlot6Length      = 48
	v1KeySlot7Start       = v1KeySlot6Start + v1KeySlot6Length
	v1KeySlot7Length      = 48
	v1KeySlot8Start       = v1KeySlot7Start + v1KeySlot7Length
	v1KeySlot8Length      = 48
	v1HeaderStructSize    = v1KeySlot8Start + v1KeySlot8Length

	v1KeySlotActiveStart             = 0
	v1KeySlotActiveLength            = 4
	v1KeySlotIterationsStart         = v1KeySlotActiveStart + v1KeySlotActiveLength
	v1KeySlotIterationsLength        = 4
	v1KeySlotSaltStart               = v1KeySlotIterationsStart + v1KeySlotIterationsLength
	v1KeySlotSaltLength              = v1SaltSize
	v1KeySlotKeyMaterialOffsetStart  = v1KeySlotSaltStart + v1KeySlotSaltLength
	v1KeySlotKeyMaterialOffsetLength = 4
	v1KeySlotStripesStart            = v1KeySlotKeyMaterialOffsetStart + v1KeySlotKeyMaterialOffsetLength
	v1KeySlotStripesLength           = 4
	v1KeySlotStructSize              = v1KeySlotStripesStart + v1KeySlotStripesLength

	v1DigestSize               = 20
	v1SaltSize                 = 32
	v1NumKeys                  = 8
	v1KeySlotActiveKeyDisabled = 0x0000dead
	v1KeySlotActiveKeyEnabled  = 0x00ac71f3
	V1Stripes                  = 4000
	V1AlignKeyslots            = 4096
	V1SectorSize               = 512
)

func (h V1Header) readu2(offset int) uint16 {
	return binary.BigEndian.Uint16(h[offset:])
}

func (h V1Header) readu4(offset int) uint32 {
	return binary.BigEndian.Uint32(h[offset:])
}

func (h *V1Header) writeu2(offset int, value uint16) {
	binary.BigEndian.PutUint16(h[offset:], value)
}

func (h *V1Header) writeu4(offset int, value uint32) {
	binary.BigEndian.PutUint32(h[offset:], value)
}

func (h V1Header) Magic() string {
	return trimZeroPad(string(h[v1MagicStart : v1MagicStart+v1MagicLength]))
}

func (h *V1Header) SetMagic(magic string) error {
	switch magic {
	case V1Magic:
		copy(h[v1MagicStart:v1MagicStart+v1MagicLength], []uint8(magic))
		return nil
	}
	return fmt.Errorf("magic %q not acceptable, only %q is an acceptable magic value: %w", magic, V1Magic, syscall.EINVAL)
}

func (h V1Header) Version() uint16 {
	return h.readu2(v1VersionStart)
}

func (h *V1Header) SetVersion(version uint16) error {
	switch version {
	case 1:
		h.writeu2(v1VersionStart, version)
		return nil
	}
	return fmt.Errorf("version %d not acceptable, only 1 is an acceptable version: %w", version, syscall.EINVAL)
}

func (h *V1Header) setZeroString(offset int, value string, length int) {
	for len(value) < length {
		value = value + "\000"
	}
	copy(h[offset:offset+length], []uint8(value))
}

func (h *V1Header) setInt8(offset int, s []uint8, length int) {
	t := make([]byte, length)
	copy(t, s)
	copy(h[offset:offset+length], s)
}

func (h V1Header) CipherName() string {
	return trimZeroPad(string(h[v1CipherNameStart : v1CipherNameStart+v1CipherNameLength]))
}

func (h *V1Header) SetCipherName(name string) {
	h.setZeroString(v1CipherNameStart, name, v1CipherNameLength)
}

func (h V1Header) CipherMode() string {
	return trimZeroPad(string(h[v1CipherModeStart : v1CipherModeStart+v1CipherModeLength]))
}

func (h *V1Header) SetCipherMode(mode string) {
	h.setZeroString(v1CipherModeStart, mode, v1CipherModeLength)
}

func (h V1Header) HashSpec() string {
	return trimZeroPad(string(h[v1HashSpecStart : v1HashSpecStart+v1HashSpecLength]))
}

func (h *V1Header) SetHashSpec(spec string) {
	h.setZeroString(v1HashSpecStart, spec, v1HashSpecLength)
}

func (h V1Header) PayloadOffset() uint32 {
	return h.readu4(v1PayloadOffsetStart)
}

func (h *V1Header) SetPayloadOffset(offset uint32) {
	h.writeu4(v1PayloadOffsetStart, offset)
}

func (h V1Header) KeyBytes() uint32 {
	return h.readu4(v1KeyBytesStart)
}

func (h *V1Header) SetKeyBytes(bytes uint32) {
	h.writeu4(v1KeyBytesStart, bytes)
}

func (h *V1Header) KeySlot(slot int) (V1KeySlot, error) {
	var ks V1KeySlot
	if slot < 0 || slot >= v1NumKeys {
		return ks, fmt.Errorf("invalid key slot number (must be 0..%d)", v1NumKeys-1)
	}
	switch slot {
	case 0:
		copy(ks[:], h[v1KeySlot1Start:v1KeySlot1Start+v1KeySlot1Length])
	case 1:
		copy(ks[:], h[v1KeySlot2Start:v1KeySlot2Start+v1KeySlot2Length])
	case 2:
		copy(ks[:], h[v1KeySlot3Start:v1KeySlot3Start+v1KeySlot3Length])
	case 3:
		copy(ks[:], h[v1KeySlot4Start:v1KeySlot4Start+v1KeySlot4Length])
	case 4:
		copy(ks[:], h[v1KeySlot5Start:v1KeySlot5Start+v1KeySlot5Length])
	case 5:
		copy(ks[:], h[v1KeySlot6Start:v1KeySlot6Start+v1KeySlot6Length])
	case 6:
		copy(ks[:], h[v1KeySlot7Start:v1KeySlot7Start+v1KeySlot7Length])
	case 7:
		copy(ks[:], h[v1KeySlot8Start:v1KeySlot8Start+v1KeySlot8Length])
	}
	return ks, nil
}

func (h *V1Header) SetKeySlot(slot int, ks V1KeySlot) error {
	if slot < 0 || slot >= v1NumKeys {
		return fmt.Errorf("invalid key slot number (must be 0..%d)", v1NumKeys-1)
	}
	switch slot {
	case 0:
		copy(h[v1KeySlot1Start:v1KeySlot1Start+v1KeySlot1Length], ks[:])
	case 1:
		copy(h[v1KeySlot2Start:v1KeySlot2Start+v1KeySlot2Length], ks[:])
	case 2:
		copy(h[v1KeySlot3Start:v1KeySlot3Start+v1KeySlot3Length], ks[:])
	case 3:
		copy(h[v1KeySlot4Start:v1KeySlot4Start+v1KeySlot4Length], ks[:])
	case 4:
		copy(h[v1KeySlot5Start:v1KeySlot5Start+v1KeySlot5Length], ks[:])
	case 5:
		copy(h[v1KeySlot6Start:v1KeySlot6Start+v1KeySlot6Length], ks[:])
	case 6:
		copy(h[v1KeySlot7Start:v1KeySlot7Start+v1KeySlot7Length], ks[:])
	case 7:
		copy(h[v1KeySlot8Start:v1KeySlot8Start+v1KeySlot8Length], ks[:])
	}
	return nil
}

func (h V1Header) MKDigest() []uint8 {
	return dupInt8(h[v1MKDigestStart : v1MKDigestStart+v1MKDigestLength])
}

func (h *V1Header) SetMKDigest(digest []uint8) {
	h.setInt8(v1MKDigestStart, digest, v1MKDigestLength)
}

func (h V1Header) MKDigestSalt() []uint8 {
	return dupInt8(h[v1MKDigestSaltStart : v1MKDigestSaltStart+v1MKDigestSaltLength])
}

func (h *V1Header) SetMKDigestSalt(salt []uint8) {
	h.setInt8(v1MKDigestSaltStart, salt, v1MKDigestSaltLength)
}

func (h V1Header) MKDigestIter() uint32 {
	return h.readu4(v1MKDigestIterStart)
}

func (h *V1Header) SetMKDigestIter(bytes uint32) {
	h.writeu4(v1MKDigestIterStart, bytes)
}

func (h V1Header) UUID() string {
	return trimZeroPad(string(h[v1UUIDStart : v1UUIDStart+v1UUIDLength]))
}

func (h *V1Header) SetUUID(uuid string) {
	h.setZeroString(v1UUIDStart, uuid, v1UUIDLength)
}

func (s V1KeySlot) readu4(offset int) uint32 {
	return binary.BigEndian.Uint32(s[offset:])
}

func (s *V1KeySlot) writeu4(offset int, value uint32) {
	binary.BigEndian.PutUint32(s[offset:], value)
}

func (s *V1KeySlot) setInt8(offset int, i []uint8, length int) {
	for len(s) < length {
		i = append(i, 0)
	}
	copy(s[offset:offset+length], i)
}

func (s V1KeySlot) Active() (bool, error) {
	active := s.readu4(v1KeySlotActiveStart)
	switch active {
	case v1KeySlotActiveKeyDisabled:
		return false, nil
	case v1KeySlotActiveKeyEnabled:
		return true, nil
	}
	return false, fmt.Errorf("got invalid active value %#0x: %w", active, syscall.EINVAL)
}

func (s *V1KeySlot) SetActive(active bool) {
	if active {
		s.writeu4(v1KeySlotActiveStart, v1KeySlotActiveKeyEnabled)
		return
	}
	s.writeu4(v1KeySlotActiveStart, v1KeySlotActiveKeyDisabled)
}

func (s V1KeySlot) Iterations() uint32 {
	return s.readu4(v1KeySlotIterationsStart)
}

func (s *V1KeySlot) SetIterations(iterations uint32) {
	s.writeu4(v1KeySlotIterationsStart, iterations)
}

func (s V1KeySlot) KeySlotSalt() []uint8 {
	return dupInt8(s[v1KeySlotSaltStart : v1KeySlotSaltStart+v1KeySlotSaltLength])
}

func (s *V1KeySlot) SetKeySlotSalt(salt []uint8) {
	s.setInt8(v1KeySlotSaltStart, salt, v1KeySlotSaltLength)
}

func (s V1KeySlot) KeyMaterialOffset() uint32 {
	return s.readu4(v1KeySlotKeyMaterialOffsetStart)
}

func (s *V1KeySlot) SetKeyMaterialOffset(material uint32) {
	s.writeu4(v1KeySlotKeyMaterialOffsetStart, material)
}

func (s V1KeySlot) Stripes() uint32 {
	return s.readu4(v1KeySlotStripesStart)
}

func (s *V1KeySlot) SetStripes(stripes uint32) {
	s.writeu4(v1KeySlotStripesStart, stripes)
}