Skip to content

Commit 44e860d

Browse files
committed
xray: add support for inspecting AES-SIV files (-aessiv flag)
#299 : In GCM mode the auth tags are at the end of each block, but in SIV mode the auth tags follow immediately after the nonce. As a result, in AES-SIV mode the output of gocryptfs-xray is misleading and does not actually print the auth tag, but just the last 16-byte of the ciphertext. diff --git a/gocryptfs-xray/xray_main.go b/gocryptfs-xray/xray_main.go index 74c9fb3..5a81caf 100644 --- a/gocryptfs-xray/xray_main.go +++ b/gocryptfs-xray/xray_main.go @@ -16,9 +16,10 @@ import ( ) const ( - ivLen = contentenc.DefaultIVBits / 8 - blockSize = contentenc.DefaultBS + ivLen + cryptocore.AuthTagLen - myName = "gocryptfs-xray" + ivLen = contentenc.DefaultIVBits / 8 + authTagLen = cryptocore.AuthTagLen + blockSize = contentenc.DefaultBS + ivLen + cryptocore.AuthTagLen + myName = "gocryptfs-xray" ) func errExit(err error) { @@ -26,13 +27,18 @@ func errExit(err error) { os.Exit(1) } -func prettyPrintHeader(h *contentenc.FileHeader) { +func prettyPrintHeader(h *contentenc.FileHeader, aessiv bool) { id := hex.EncodeToString(h.ID) - fmt.Printf("Header: Version: %d, Id: %s\n", h.Version, id) + msg := "Header: Version: %d, Id: %s" + if aessiv { + msg += ", assuming AES-SIV mode" + } + fmt.Printf(msg+"\n", h.Version, id) } func main() { dumpmasterkey := flag.Bool("dumpmasterkey", false, "Decrypt and dump the master key") + aessiv := flag.Bool("aessiv", false, "Assume AES-SIV mode instead of AES-GCM") flag.Parse() if flag.NArg() != 1 { fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] FILE\n"+ @@ -54,7 +60,7 @@ func main() { if *dumpmasterkey { dumpMasterKey(fn) } else { - inspectCiphertext(fd) + inspectCiphertext(fd, *aessiv) } } @@ -72,7 +78,7 @@ func dumpMasterKey(fn string) { } } -func inspectCiphertext(fd *os.File) { +func inspectCiphertext(fd *os.File, aessiv bool) { headerBytes := make([]byte, contentenc.HeaderLen) n, err := fd.ReadAt(headerBytes, 0) if err == io.EOF && n == 0 { @@ -88,34 +94,30 @@ func inspectCiphertext(fd *os.File) { if err != nil { errExit(err) } - prettyPrintHeader(header) + prettyPrintHeader(header, aessiv) var i int64 + buf := make([]byte, blockSize) for i = 0; ; i++ { - blockLen := int64(blockSize) off := contentenc.HeaderLen + i*blockSize - iv := make([]byte, ivLen) - _, err := fd.ReadAt(iv, off) - if err == io.EOF { - break - } else if err != nil { + n, err := fd.ReadAt(buf, off) + if err != nil && err != io.EOF { errExit(err) } - tag := make([]byte, cryptocore.AuthTagLen) - _, err = fd.ReadAt(tag, off+blockSize-cryptocore.AuthTagLen) - if err == io.EOF { - fi, err2 := fd.Stat() - if err2 != nil { - errExit(err2) - } - _, err2 = fd.ReadAt(tag, fi.Size()-cryptocore.AuthTagLen) - if err2 != nil { - errExit(err2) - } - blockLen = (fi.Size() - contentenc.HeaderLen) % blockSize - } else if err != nil { - errExit(err) + if n == 0 && err == io.EOF { + break + } + // A block contains at least the IV, the Auth Tag and 1 data byte + if n < ivLen+authTagLen+1 { + errExit(fmt.Errorf("corrupt block: truncated data, len=%d", n)) + } + data := buf[:n] + // Parse block data + iv := data[:ivLen] + tag := data[len(data)-authTagLen:] + if aessiv { + tag = data[ivLen : ivLen+authTagLen] } fmt.Printf("Block %2d: IV: %s, Tag: %s, Offset: %5d Len: %d\n", - i, hex.EncodeToString(iv), hex.EncodeToString(tag), off, blockLen) + i, hex.EncodeToString(iv), hex.EncodeToString(tag), off, len(data)) } } diff --git a/gocryptfs-xray/xray_tests/aessiv_fs.masterkey.txt b/gocryptfs-xray/xray_tests/aessiv_fs.masterkey.txt new file mode 100644 index 0000000..70835ac --- /dev/null +++ b/gocryptfs-xray/xray_tests/aessiv_fs.masterkey.txt @@ -0,0 +1,5 @@ +Your master key is: + + 29dd219d-e227ff20-8474469d-9fc9fdc6- + b434ab35-404e808c-489d441e-2c1003f2 + diff --git a/gocryptfs-xray/xray_tests/aessiv_fs.xray.txt b/gocryptfs-xray/xray_tests/aessiv_fs.xray.txt new file mode 100644 index 0000000..6a48079 --- /dev/null +++ b/gocryptfs-xray/xray_tests/aessiv_fs.xray.txt @@ -0,0 +1,3 @@ +Header: Version: 2, Id: c2f21142e108952a47edfe16053d2bb9, assuming AES-SIV mode +Block 0: IV: 7621fdc35be7671ac6f369214436e8ff, Tag: e8108c158b22cad6bb3296645357eb75, Offset: 18 Len: 4128 +Block 1: IV: f096d86a4dc3461ef17655cfcf865b13, Tag: 925f23d647e4ab7add2c8d36362cc5a9, Offset: 4146 Len: 936 diff --git a/gocryptfs-xray/xray_tests/aessiv_fs/Ldq-c4ADpM5iGSSrPjUAqQ b/gocryptfs-xray/xray_tests/aessiv_fs/Ldq-c4ADpM5iGSSrPjUAqQ new file mode 100644 index 0000000..bfd4dfe Binary files /dev/null and b/gocryptfs-xray/xray_tests/aessiv_fs/Ldq-c4ADpM5iGSSrPjUAqQ differ diff --git a/gocryptfs-xray/xray_tests/aessiv_fs/gocryptfs.conf b/gocryptfs-xray/xray_tests/aessiv_fs/gocryptfs.conf new file mode 100644 index 0000000..9b8b95f --- /dev/null +++ b/gocryptfs-xray/xray_tests/aessiv_fs/gocryptfs.conf @@ -0,0 +1,21 @@ +{ + "Creator": "gocryptfs v1.7-beta1-7-g6b94f5e", + "EncryptedKey": "D0kHfg/pryMO9Ydo15EwpYjNHf3iWKq2GJyNocbjwJt9blEeMoLD5DnoARuDzQs54hblw+9MHwFjCSHYmJrFbA==", + "ScryptObject": { + "Salt": "ehn0LM/Hy/4QkXAMCZq3c3p0O9G7gu5e3OQSR8MiJ6c=", + "N": 65536, + "R": 8, + "P": 1, + "KeyLen": 32 + }, + "Version": 2, + "FeatureFlags": [ + "GCMIV128", + "HKDF", + "DirIV", + "EMENames", + "LongNames", + "Raw64", + "AESSIV" + ] +} diff --git a/gocryptfs-xray/xray_tests/aessiv_fs/gocryptfs.diriv b/gocryptfs-xray/xray_tests/aessiv_fs/gocryptfs.diriv new file mode 100644 index 0000000..dd57ce1 --- /dev/null +++ b/gocryptfs-xray/xray_tests/aessiv_fs/gocryptfs.diriv @@ -0,0 +1 @@ +�.¨Í1A�iõ&Á�4�öÉ \ No newline at end of file diff --git a/gocryptfs-xray/xray_tests/xray_test.go b/gocryptfs-xray/xray_tests/xray_test.go index a3374b0..8e5fc0c 100644 --- a/gocryptfs-xray/xray_tests/xray_test.go +++ b/gocryptfs-xray/xray_tests/xray_test.go @@ -24,3 +24,20 @@ func TestAesgcmXray(t *testing.T) { fmt.Printf("have:\n%s", string(out)) } } + +func TestAessivXray(t *testing.T) { + expected, err := ioutil.ReadFile("aessiv_fs.xray.txt") + if err != nil { + t.Fatal(err) + } + cmd := exec.Command("../gocryptfs-xray", "-aessiv", "aessiv_fs/Ldq-c4ADpM5iGSSrPjUAqQ") + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatal(err) + } + if bytes.Compare(out, expected) != 0 { + t.Errorf("Unexpected output") + fmt.Printf("expected:\n%s", string(expected)) + fmt.Printf("have:\n%s", string(out)) + } +}
1 parent 30a8fda commit 44e860d

File tree

7 files changed

+78
-29
lines changed

7 files changed

+78
-29
lines changed

gocryptfs-xray/xray_main.go

+31-29
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,29 @@ import (
1616
)
1717

1818
const (
19-
ivLen = contentenc.DefaultIVBits / 8
20-
blockSize = contentenc.DefaultBS + ivLen + cryptocore.AuthTagLen
21-
myName = "gocryptfs-xray"
19+
ivLen = contentenc.DefaultIVBits / 8
20+
authTagLen = cryptocore.AuthTagLen
21+
blockSize = contentenc.DefaultBS + ivLen + cryptocore.AuthTagLen
22+
myName = "gocryptfs-xray"
2223
)
2324

2425
func errExit(err error) {
2526
fmt.Println(err)
2627
os.Exit(1)
2728
}
2829

29-
func prettyPrintHeader(h *contentenc.FileHeader) {
30+
func prettyPrintHeader(h *contentenc.FileHeader, aessiv bool) {
3031
id := hex.EncodeToString(h.ID)
31-
fmt.Printf("Header: Version: %d, Id: %s\n", h.Version, id)
32+
msg := "Header: Version: %d, Id: %s"
33+
if aessiv {
34+
msg += ", assuming AES-SIV mode"
35+
}
36+
fmt.Printf(msg+"\n", h.Version, id)
3237
}
3338

3439
func main() {
3540
dumpmasterkey := flag.Bool("dumpmasterkey", false, "Decrypt and dump the master key")
41+
aessiv := flag.Bool("aessiv", false, "Assume AES-SIV mode instead of AES-GCM")
3642
flag.Parse()
3743
if flag.NArg() != 1 {
3844
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] FILE\n"+
@@ -54,7 +60,7 @@ func main() {
5460
if *dumpmasterkey {
5561
dumpMasterKey(fn)
5662
} else {
57-
inspectCiphertext(fd)
63+
inspectCiphertext(fd, *aessiv)
5864
}
5965
}
6066

@@ -72,7 +78,7 @@ func dumpMasterKey(fn string) {
7278
}
7379
}
7480

75-
func inspectCiphertext(fd *os.File) {
81+
func inspectCiphertext(fd *os.File, aessiv bool) {
7682
headerBytes := make([]byte, contentenc.HeaderLen)
7783
n, err := fd.ReadAt(headerBytes, 0)
7884
if err == io.EOF && n == 0 {
@@ -88,34 +94,30 @@ func inspectCiphertext(fd *os.File) {
8894
if err != nil {
8995
errExit(err)
9096
}
91-
prettyPrintHeader(header)
97+
prettyPrintHeader(header, aessiv)
9298
var i int64
99+
buf := make([]byte, blockSize)
93100
for i = 0; ; i++ {
94-
blockLen := int64(blockSize)
95101
off := contentenc.HeaderLen + i*blockSize
96-
iv := make([]byte, ivLen)
97-
_, err := fd.ReadAt(iv, off)
98-
if err == io.EOF {
99-
break
100-
} else if err != nil {
102+
n, err := fd.ReadAt(buf, off)
103+
if err != nil && err != io.EOF {
101104
errExit(err)
102105
}
103-
tag := make([]byte, cryptocore.AuthTagLen)
104-
_, err = fd.ReadAt(tag, off+blockSize-cryptocore.AuthTagLen)
105-
if err == io.EOF {
106-
fi, err2 := fd.Stat()
107-
if err2 != nil {
108-
errExit(err2)
109-
}
110-
_, err2 = fd.ReadAt(tag, fi.Size()-cryptocore.AuthTagLen)
111-
if err2 != nil {
112-
errExit(err2)
113-
}
114-
blockLen = (fi.Size() - contentenc.HeaderLen) % blockSize
115-
} else if err != nil {
116-
errExit(err)
106+
if n == 0 && err == io.EOF {
107+
break
108+
}
109+
// A block contains at least the IV, the Auth Tag and 1 data byte
110+
if n < ivLen+authTagLen+1 {
111+
errExit(fmt.Errorf("corrupt block: truncated data, len=%d", n))
112+
}
113+
data := buf[:n]
114+
// Parse block data
115+
iv := data[:ivLen]
116+
tag := data[len(data)-authTagLen:]
117+
if aessiv {
118+
tag = data[ivLen : ivLen+authTagLen]
117119
}
118120
fmt.Printf("Block %2d: IV: %s, Tag: %s, Offset: %5d Len: %d\n",
119-
i, hex.EncodeToString(iv), hex.EncodeToString(tag), off, blockLen)
121+
i, hex.EncodeToString(iv), hex.EncodeToString(tag), off, len(data))
120122
}
121123
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Your master key is:
2+
3+
29dd219d-e227ff20-8474469d-9fc9fdc6-
4+
b434ab35-404e808c-489d441e-2c1003f2
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Header: Version: 2, Id: c2f21142e108952a47edfe16053d2bb9, assuming AES-SIV mode
2+
Block 0: IV: 7621fdc35be7671ac6f369214436e8ff, Tag: e8108c158b22cad6bb3296645357eb75, Offset: 18 Len: 4128
3+
Block 1: IV: f096d86a4dc3461ef17655cfcf865b13, Tag: 925f23d647e4ab7add2c8d36362cc5a9, Offset: 4146 Len: 936
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"Creator": "gocryptfs v1.7-beta1-7-g6b94f5e",
3+
"EncryptedKey": "D0kHfg/pryMO9Ydo15EwpYjNHf3iWKq2GJyNocbjwJt9blEeMoLD5DnoARuDzQs54hblw+9MHwFjCSHYmJrFbA==",
4+
"ScryptObject": {
5+
"Salt": "ehn0LM/Hy/4QkXAMCZq3c3p0O9G7gu5e3OQSR8MiJ6c=",
6+
"N": 65536,
7+
"R": 8,
8+
"P": 1,
9+
"KeyLen": 32
10+
},
11+
"Version": 2,
12+
"FeatureFlags": [
13+
"GCMIV128",
14+
"HKDF",
15+
"DirIV",
16+
"EMENames",
17+
"LongNames",
18+
"Raw64",
19+
"AESSIV"
20+
]
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.��1Ai�&�4���

gocryptfs-xray/xray_tests/xray_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,20 @@ func TestAesgcmXray(t *testing.T) {
2424
fmt.Printf("have:\n%s", string(out))
2525
}
2626
}
27+
28+
func TestAessivXray(t *testing.T) {
29+
expected, err := ioutil.ReadFile("aessiv_fs.xray.txt")
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
cmd := exec.Command("../gocryptfs-xray", "-aessiv", "aessiv_fs/Ldq-c4ADpM5iGSSrPjUAqQ")
34+
out, err := cmd.CombinedOutput()
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
if bytes.Compare(out, expected) != 0 {
39+
t.Errorf("Unexpected output")
40+
fmt.Printf("expected:\n%s", string(expected))
41+
fmt.Printf("have:\n%s", string(out))
42+
}
43+
}

0 commit comments

Comments
 (0)