anatol / devmapper.go Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
The following code works fine if we are using luks2 without integrity.
Lines 129 to 135 in ba4de5f
But it is giving gibberish for disk with integrity support.
I'm using the following snippet for reading from a disk image file.
func myJourney(vol *luks.Volume) {
tb, e1 := asTable(vol)
dvol, e2 := devmapper.OpenUserspaceVolume(os.O_RDONLY, 0, tb)
buf := make([]byte, vol.StorageSectorSize*1234)
_, e3 := dvol.ReadAt(buf, 0)
// ... works perfectly for crypt devices without integrity.
}
var flagsKernelNames = map[string]string{
luks.FlagAllowDiscards: devmapper.CryptFlagAllowDiscards,
luks.FlagSameCPUCrypt: devmapper.CryptFlagSameCPUCrypt,
luks.FlagSubmitFromCryptCPUs: devmapper.CryptFlagSubmitFromCryptCPUs,
luks.FlagNoReadWorkqueue: devmapper.CryptFlagNoReadWorkqueue,
luks.FlagNoWriteWorkqueue: devmapper.CryptFlagNoWriteWorkqueue,
}
func asTable(v *luks.Volume) (*devmapper.CryptTable, error) {
kernelFlags := make([]string, 0, len(v.Flags))
for _, f := range v.Flags {
flag, ok := flagsKernelNames[f]
if !ok {
return nil, fmt.Errorf("unknown LUKS flag: %v", f)
}
kernelFlags = append(kernelFlags, flag)
}
if v.StorageSize%v.StorageSectorSize != 0 {
return nil, fmt.Errorf("storage size must be multiple of sector size")
}
if v.StorageOffset%v.StorageSectorSize != 0 {
return nil, fmt.Errorf("offset must be multiple of sector size")
}
volKey := fieldByName(*v, "key").Interface().([]byte) // This is actually AES key + integrity key
return &devmapper.CryptTable{
Start: 0,
Length: v.StorageSize,
BackendDevice: v.BackingDevice,
BackendOffset: v.StorageOffset,
Encryption: v.StorageEncryption,
Key: volKey[:64], // This hack wont work, even if I'm extracting the AES key
IVTweak: v.StorageIvTweak,
Flags: kernelFlags,
SectorSize: v.StorageSectorSize,
}, nil
}
// not important, just hack to extract non-exported field values.
func fieldByName(obj any, name string) reflect.Value {
origObj := reflect.ValueOf(obj)
refObj := reflect.New(origObj.Type()).Elem()
refObj.Set(origObj)
refField := refObj.FieldByName(name)
return reflect.
NewAt(refField.Type(), unsafe.Pointer(refField.UnsafeAddr())).
Elem()
}
I tried finding the on-disk sector formats of luks2 with integrity support, but could not find it in a clear documentation. Checked this slide but this does not talk about any low level details.
Sample script to generate an encrypted disk (no filesystem)
prepare_enc_disk() {
fallocate -l "$1" /dev/shm/exp1/test_disk.img
cryptsetup luksFormat -q --type luks2 /dev/shm/exp1/test_disk.img --cipher aes-xts-plain64 --key-file /dev/shm/key.txt \
--integrity hmac-sha256 --integrity-no-wipe --sector-size 4096
cryptsetup open /dev/shm/exp1/test_disk.img tf1-crypt --key-file /dev/shm/key.txt
dmsetup ls --target crypt
write_simple_data
cryptsetup remove tf1-crypt
}
write_simple_data() {
sudo dd if=/dev/zero of=/dev/mapper/tf1-crypt bs=4MB count=10
echo "Hello world of LUKS..." | sudo tee /dev/mapper/tf1-crypt
}
main() {
prepare_enc_disk 16.5M
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.