Repository navigation
Expand file tree
/
Copy pathvolume.go
More file actions
104 lines (93 loc) · 3.68 KB
/
Copy pathvolume.go
File metadata and controls
104 lines (93 loc) · 3.68 KB
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
package luks
import (
"crypto"
"crypto/hmac"
_ "crypto/sha1" // register crypto.SHA1 for HMAC
_ "crypto/sha256" // register crypto.SHA256
_ "crypto/sha512" // register crypto.SHA384 and crypto.SHA512
"fmt"
"strings"
"github.com/anatol/devmapper.go"
)
// Volume represents information provided by an unsealed (i.e. with recovered password) LUKS slot
type Volume struct {
BackingDevice string
Flags []string // luks-named flags
UUID string
key []byte // keep decoded key field private for security reasons
LuksType string
StorageEncryption string
StorageIvTweak uint64
StorageSectorSize uint64
StorageOffset uint64 // offset of underlying storage in bytes
StorageSize uint64 // length of underlying device in bytes, zero means that size should be calculated using `diskSize` function
}
// map of LUKS flag names to its dm-crypt counterparts
var flagsKernelNames = map[string]string{
FlagAllowDiscards: devmapper.CryptFlagAllowDiscards,
FlagSameCPUCrypt: devmapper.CryptFlagSameCPUCrypt,
FlagSubmitFromCryptCPUs: devmapper.CryptFlagSubmitFromCryptCPUs,
FlagNoReadWorkqueue: devmapper.CryptFlagNoReadWorkqueue,
FlagNoWriteWorkqueue: devmapper.CryptFlagNoWriteWorkqueue,
}
// hmacAllowedHashes restricts HMAC to standard-library hash implementations.
// The hash is selected by a crypto.Hash identifier rather than a caller-supplied
// constructor, so callers cannot inject an implementation that observes the
// volume key during the HMAC computation.
var hmacAllowedHashes = map[crypto.Hash]bool{
crypto.SHA1: true,
crypto.SHA256: true,
crypto.SHA384: true,
crypto.SHA512: true,
}
// HMAC returns HMAC(volume key, message) using the given hash. The hash is
// identified by a crypto.Hash value and must be one of the supported algorithms;
// the volume key is only ever fed to a trusted standard-library implementation.
// It never leaves the package — callers receive only the resulting digest, which
// carries no key material. This lets a consumer bind a value to the unlocked
// volume (e.g. measure it into a TPM PCR) without the master key crossing the
// package boundary.
func (v *Volume) HMAC(h crypto.Hash, message []byte) ([]byte, error) {
if !hmacAllowedHashes[h] {
return nil, fmt.Errorf("luks: HMAC: unsupported hash %s", h)
}
if !h.Available() {
return nil, fmt.Errorf("luks: HMAC: hash %s is not available", h)
}
mac := hmac.New(h.New, v.key)
mac.Write(message)
return mac.Sum(nil), nil
}
// SetupMapper creates a device mapper for the given LUKS volume
func (v *Volume) SetupMapper(name string) error {
kernelFlags := make([]string, 0, len(v.Flags))
for _, f := range v.Flags {
flag, ok := flagsKernelNames[f]
if !ok {
return fmt.Errorf("unknown LUKS flag: %v", f)
}
kernelFlags = append(kernelFlags, flag)
}
// dm-crypt requires both size and offset to be aligned to the sector size
if v.StorageSize%v.StorageSectorSize != 0 {
return fmt.Errorf("storage size must be multiple of sector size")
}
if v.StorageOffset%v.StorageSectorSize != 0 {
return fmt.Errorf("offset must be multiple of sector size")
}
table := devmapper.CryptTable{
Start: 0,
Length: v.StorageSize,
BackendDevice: v.BackingDevice,
BackendOffset: v.StorageOffset,
Encryption: v.StorageEncryption,
Key: v.key,
IVTweak: v.StorageIvTweak,
Flags: kernelFlags,
SectorSize: v.StorageSectorSize,
}
// Build a UUID in the format expected by cryptsetup's dm_prepare_uuid():
// "CRYPT-<type>-<uuid_no_dashes>-<dm_name>"
uuid := fmt.Sprintf("CRYPT-%v-%v-%v", v.LuksType, strings.ReplaceAll(v.UUID, "-", ""), name)
return devmapper.CreateAndLoad(name, uuid, 0, table)
}