-
Notifications
You must be signed in to change notification settings - Fork 14
/
device.go
352 lines (288 loc) · 11.5 KB
/
device.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package cryptsetup
// #cgo pkg-config: libcryptsetup
// #include <libcryptsetup.h>
// #include <stdlib.h>
//extern int progress_callback(uint64_t size, uint64_t offset, void *usrptr);
import "C"
import (
"unsafe"
)
// Device is a handle to the crypto device.
// It encapsulates libcryptsetup's 'crypt_device' struct.
type Device struct {
cryptDevice *C.struct_crypt_device
freed bool
}
// Init initializes a crypt device backed by 'devicePath'.
// Returns a pointer to the newly allocated Device or any error encountered.
// C equivalent: crypt_init
func Init(devicePath string) (*Device, error) {
cryptDevicePath := C.CString(devicePath)
defer C.free(unsafe.Pointer(cryptDevicePath))
var cryptDevice *C.struct_crypt_device
if err := int(C.crypt_init(&cryptDevice, cryptDevicePath)); err < 0 {
return nil, &Error{functionName: "crypt_init", code: err}
}
return &Device{cryptDevice: cryptDevice}, nil
}
// InitByName initializes a crypt device from provided active device 'name'.
// Returns a pointer to the newly allocated Device or any error encountered.
// C equivalent: crypt_init_by_name
func InitByName(name string) (*Device, error) {
activeCryptDeviceName := C.CString(name)
defer C.free(unsafe.Pointer(activeCryptDeviceName))
var cryptDevice *C.struct_crypt_device
if err := int(C.crypt_init_by_name(&cryptDevice, activeCryptDeviceName)); err < 0 {
return nil, &Error{functionName: "crypt_init_by_name", code: err}
}
return &Device{cryptDevice: cryptDevice}, nil
}
// Free releases crypt device context and used memory.
// C equivalent: crypt_free
func (device *Device) Free() bool {
if !device.freed {
C.crypt_free(device.cryptDevice)
device.freed = true
return true
}
return false
}
// C equivalent: crypt_dump
func (device *Device) Dump() int {
return int(C.crypt_dump(device.cryptDevice))
}
// Type returns the device's type as a string.
// Returns an empty string if the information is not available.
func (device *Device) Type() string {
return C.GoString(C.crypt_get_type(device.cryptDevice))
}
// Format formats a Device, using a specific device type, and type-independent parameters.
// Returns nil on success, or an error otherwise.
// C equivalent: crypt_format
func (device *Device) Format(deviceType DeviceType, genericParams GenericParams) error {
cryptDeviceTypeName := C.CString(deviceType.Name())
defer C.free(unsafe.Pointer(cryptDeviceTypeName))
cCipher := C.CString(genericParams.Cipher)
defer C.free(unsafe.Pointer(cCipher))
cCipherMode := C.CString(genericParams.CipherMode)
defer C.free(unsafe.Pointer(cCipherMode))
var cUUID *C.char = nil
if len(genericParams.UUID) > 0 {
cUUID = C.CString(genericParams.UUID)
defer C.free(unsafe.Pointer(cUUID))
}
var cVolumeKey *C.char = nil
if len(genericParams.VolumeKey) > 0 {
cVolumeKey = C.CString(genericParams.VolumeKey)
defer C.free(unsafe.Pointer(cVolumeKey))
}
cVolumeKeySize := C.size_t(genericParams.VolumeKeySize)
cTypeParams, freeCTypeParams := deviceType.Unmanaged()
defer freeCTypeParams()
err := C.crypt_format(device.cryptDevice, cryptDeviceTypeName, cCipher, cCipherMode, cUUID, cVolumeKey, cVolumeKeySize, cTypeParams)
if err < 0 {
return &Error{functionName: "crypt_format", code: int(err)}
}
return nil
}
var progressCallback func(size, offset uint64) int
//export progress_callback
func progress_callback(size C.uint64_t, offset C.uint64_t, usrptr unsafe.Pointer) C.int {
if progressCallback != nil {
ret := progressCallback(uint64(size), uint64(offset))
return C.int(ret)
}
return 0
}
// Wipe wipes/fills (part of) a device with the selected pattern.
// Returns nil on success, or an error otherwise.
// C equivalent: crypt_wipe
func (device *Device) Wipe(devicePath string, pattern int, offset, length uint64, wipeBlockSize, flags int, progress func(size, offset uint64) int) error {
cWipeBlockSize := C.size_t(wipeBlockSize)
cDevicePath := C.CString(devicePath)
defer C.free(unsafe.Pointer(cDevicePath))
progressCallback = progress
err := C.crypt_wipe(device.cryptDevice, cDevicePath, 0, C.uint64_t(offset), C.uint64_t(length), cWipeBlockSize, C.uint32_t(flags), (*[0]byte)(C.progress_callback), nil)
if err < 0 {
return &Error{functionName: "crypt_wipe", code: int(err)}
}
return nil
}
// Resize the crypt device.
// Set newSize to 0 to use all of the underlying device size
// Returns nil on success, or an error otherwise.
// C equivalent: crypt_resize
func (device *Device) Resize(name string, newSize uint64) error {
cryptDeviceName := C.CString(name)
defer C.free(unsafe.Pointer(cryptDeviceName))
err := C.crypt_resize(device.cryptDevice, cryptDeviceName, C.uint64_t(newSize))
if err < 0 {
return &Error{functionName: "crypt_resize", code: int(err)}
}
return nil
}
// Load loads crypt device parameters from the device type parameters if it is
// specified, otherwise it loads the device from the on-disk header.
// Returns nil on success, or an error otherwise.
// C equivalent: crypt_load
func (device *Device) Load(deviceType DeviceType) error {
var cryptDeviceTypeName *C.char
var cTypeParams unsafe.Pointer
if deviceType != nil {
cryptDeviceTypeName = C.CString(deviceType.Name())
defer C.free(unsafe.Pointer(cryptDeviceTypeName))
var freeCTypeParams func()
cTypeParams, freeCTypeParams = deviceType.Unmanaged()
defer freeCTypeParams()
}
err := C.crypt_load(device.cryptDevice, cryptDeviceTypeName, cTypeParams)
if err < 0 {
return &Error{functionName: "crypt_load", code: int(err)}
}
return nil
}
// KeyslotAddByVolumeKey adds a key slot using a volume key to perform the required security check.
// Returns nil on success, or an error otherwise.
// C equivalent: crypt_keyslot_add_by_volume_key
func (device *Device) KeyslotAddByVolumeKey(keyslot int, volumeKey string, passphrase string) error {
var cVolumeKey *C.char = nil
if len(volumeKey) > 0 {
cVolumeKey = C.CString(volumeKey)
defer C.free(unsafe.Pointer(cVolumeKey))
}
cPassphrase := C.CString(passphrase)
defer C.free(unsafe.Pointer(cPassphrase))
err := C.crypt_keyslot_add_by_volume_key(device.cryptDevice, C.int(keyslot), cVolumeKey, C.size_t(len(volumeKey)), cPassphrase, C.size_t(len(passphrase)))
if err < 0 {
return &Error{functionName: "crypt_keyslot_add_by_volume_key", code: int(err)}
}
return nil
}
// KeyslotAddByPassphrase adds a key slot using a previously added passphrase to perform the required security check.
// Returns nil on success, or an error otherwise.
// C equivalent: crypt_keyslot_add_by_passphrase
func (device *Device) KeyslotAddByPassphrase(keyslot int, currentPassphrase string, newPassphrase string) error {
cCurrentPassphrase := C.CString(currentPassphrase)
defer C.free(unsafe.Pointer(cCurrentPassphrase))
cNewPassphrase := C.CString(newPassphrase)
defer C.free(unsafe.Pointer(cNewPassphrase))
err := C.crypt_keyslot_add_by_passphrase(
device.cryptDevice, C.int(keyslot),
cCurrentPassphrase, C.size_t(len(currentPassphrase)),
cNewPassphrase, C.size_t(len(newPassphrase)),
)
if err < 0 {
return &Error{functionName: "crypt_keyslot_add_by_passphrase", code: int(err)}
}
return nil
}
// KeyslotChangeByPassphrase changes a defined a key slot using a previously added passphrase to perform the required security check.
// Returns nil on success, or an error otherwise.
// C equivalent: crypt_keyslot_change_by_passphrase
func (device *Device) KeyslotChangeByPassphrase(currentKeyslot int, newKeyslot int, currentPassphrase string, newPassphrase string) error {
cCurrentPassphrase := C.CString(currentPassphrase)
defer C.free(unsafe.Pointer(cCurrentPassphrase))
cNewPassphrase := C.CString(newPassphrase)
defer C.free(unsafe.Pointer(cNewPassphrase))
err := C.crypt_keyslot_change_by_passphrase(
device.cryptDevice,
C.int(currentKeyslot),
C.int(newKeyslot),
cCurrentPassphrase, C.size_t(len(currentPassphrase)),
cNewPassphrase, C.size_t(len(newPassphrase)),
)
if err < 0 {
return &Error{functionName: "crypt_keyslot_change_by_passphrase", code: int(err)}
}
return nil
}
// ActivateByPassphrase activates a device by using a passphrase from a specific keyslot.
// If deviceName is empty only check passphrase.
// Returns nil on success, or an error otherwise.
// C equivalent: crypt_activate_by_passphrase
func (device *Device) ActivateByPassphrase(deviceName string, keyslot int, passphrase string, flags int) error {
var cryptDeviceName *C.char = nil
if len(deviceName) > 0 {
cryptDeviceName = C.CString(deviceName)
defer C.free(unsafe.Pointer(cryptDeviceName))
}
cPassphrase := C.CString(passphrase)
defer C.free(unsafe.Pointer(cPassphrase))
err := C.crypt_activate_by_passphrase(device.cryptDevice, cryptDeviceName, C.int(keyslot), cPassphrase, C.size_t(len(passphrase)), C.uint32_t(flags))
if err < 0 {
return &Error{functionName: "crypt_activate_by_passphrase", code: int(err)}
}
return nil
}
// ActivateByVolumeKey activates a device by using a volume key.
// If deviceName is empty only check passphrase.
// Returns nil on success, or an error otherwise.
// C equivalent: crypt_activate_by_volume_key
func (device *Device) ActivateByVolumeKey(deviceName string, volumeKey string, volumeKeySize int, flags int) error {
var cryptDeviceName *C.char = nil
if len(deviceName) > 0 {
cryptDeviceName = C.CString(deviceName)
defer C.free(unsafe.Pointer(cryptDeviceName))
}
var cVolumeKey *C.char = nil
if len(volumeKey) > 0 {
cVolumeKey = C.CString(volumeKey)
defer C.free(unsafe.Pointer(cVolumeKey))
}
err := C.crypt_activate_by_volume_key(device.cryptDevice, cryptDeviceName, cVolumeKey, C.size_t(volumeKeySize), C.uint32_t(flags))
if err < 0 {
return &Error{functionName: "crypt_activate_by_volume_key", code: int(err)}
}
return nil
}
// Deactivate deactivates a device.
// Returns nil on success, or an error otherwise.
// C equivalent: crypt_deactivate
func (device *Device) Deactivate(deviceName string) error {
cryptDeviceName := C.CString(deviceName)
defer C.free(unsafe.Pointer(cryptDeviceName))
err := C.crypt_deactivate(device.cryptDevice, cryptDeviceName)
if err < 0 {
return &Error{functionName: "crypt_deactivate", code: int(err)}
}
return nil
}
// SetDebugLevel sets the debug level for the library.
// C equivalent: crypt_set_debug_level
func SetDebugLevel(debugLevel int) {
C.crypt_set_debug_level(C.int(debugLevel))
}
// VolumeKeyGet gets the volume key from a crypt device.
// Returns a slice of bytes having the volume key and the unlocked key slot number, or an error otherwise.
// C equivalent: crypt_volume_key_get
func (device *Device) VolumeKeyGet(keyslot int, passphrase string) ([]byte, int, error) {
cPassphrase := C.CString(passphrase)
defer C.free(unsafe.Pointer(cPassphrase))
cVKSize := C.crypt_get_volume_key_size(device.cryptDevice)
cVKSizePointer := C.malloc(C.size_t(cVKSize))
if cVKSizePointer == nil {
return []byte{}, 0, &Error{functionName: "malloc"}
}
defer C.free(cVKSizePointer)
err := C.crypt_volume_key_get(
device.cryptDevice, C.int(keyslot),
(*C.char)(cVKSizePointer), (*C.size_t)(unsafe.Pointer(&cVKSize)),
cPassphrase, C.size_t(len(passphrase)),
)
if err < 0 {
return []byte{}, 0, &Error{functionName: "crypt_volume_key_get", code: int(err)}
}
return C.GoBytes(unsafe.Pointer(cVKSizePointer), C.int(cVKSize)), int(err), nil
}
// GetDeviceName gets the path to the underlying device.
// C equivalent: crypt_get_device_name
func (device *Device) GetDeviceName() string {
res := C.crypt_get_device_name(device.cryptDevice)
return C.GoString(res)
}
// GetUUID gets the device's UUID.
// C equivalent: crypt_get_uuid
func (device *Device) GetUUID() string {
res := C.crypt_get_uuid(device.cryptDevice)
return C.GoString(res)
}