Run go fmt
This commit is contained in:
parent
5bd08abf40
commit
89fef80d32
@ -1,8 +1,8 @@
|
||||
package cryptfs
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
)
|
||||
import "os"
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package cryptfs
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testRange struct {
|
||||
@ -22,9 +22,9 @@ func TestSplitRange(t *testing.T) {
|
||||
key := make([]byte, 16)
|
||||
f := NewCryptFS(key, true)
|
||||
|
||||
for _, r := range(ranges) {
|
||||
for _, r := range ranges {
|
||||
parts := f.SplitRange(r.offset, r.length)
|
||||
for _, p := range(parts) {
|
||||
for _, p := range parts {
|
||||
if p.Length > DEFAULT_PLAINBS || p.Skip >= DEFAULT_PLAINBS {
|
||||
fmt.Printf("Test fail: n=%d, length=%d, offset=%d\n", p.BlockNo, p.Length, p.Skip)
|
||||
t.Fail()
|
||||
@ -45,15 +45,15 @@ func TestCiphertextRange(t *testing.T) {
|
||||
key := make([]byte, 16)
|
||||
f := NewCryptFS(key, true)
|
||||
|
||||
for _, r := range(ranges) {
|
||||
for _, r := range ranges {
|
||||
alignedOffset, alignedLength, skipBytes := f.CiphertextRange(r.offset, r.length)
|
||||
if alignedLength < r.length {
|
||||
t.Fail()
|
||||
}
|
||||
if alignedOffset % f.cipherBS != 0 {
|
||||
if alignedOffset%f.cipherBS != 0 {
|
||||
t.Fail()
|
||||
}
|
||||
if r.offset % f.plainBS != 0 && skipBytes == 0 {
|
||||
if r.offset%f.plainBS != 0 && skipBytes == 0 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package cryptfs
|
||||
// CryptFS is the crypto backend of GoCryptFS
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"crypto/cipher"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -4,11 +4,11 @@ package cryptfs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"errors"
|
||||
"crypto/cipher"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -113,7 +113,7 @@ func (be *CryptFS) SplitRange(offset uint64, length uint64) []intraBlock {
|
||||
for length > 0 {
|
||||
b.BlockNo = offset / be.plainBS
|
||||
b.Skip = offset % be.plainBS
|
||||
b.Length = be.minu64(length, be.plainBS - b.Skip)
|
||||
b.Length = be.minu64(length, be.plainBS-b.Skip)
|
||||
parts = append(parts, b)
|
||||
offset += b.Length
|
||||
length -= b.Length
|
||||
@ -131,7 +131,7 @@ func (be *CryptFS) PlainSize(size uint64) uint64 {
|
||||
|
||||
overhead := be.cipherBS - be.plainBS
|
||||
nBlocks := (size + be.cipherBS - 1) / be.cipherBS
|
||||
if nBlocks * overhead > size {
|
||||
if nBlocks*overhead > size {
|
||||
Warn.Printf("PlainSize: Negative size, returning 0 instead\n")
|
||||
return 0
|
||||
}
|
||||
@ -164,7 +164,7 @@ func (be *CryptFS) CiphertextRange(offset uint64, length uint64) (alignedOffset
|
||||
skip := offset % be.plainBS
|
||||
|
||||
firstBlockNo := offset / be.plainBS
|
||||
lastBlockNo := ( offset + length - 1 ) / be.plainBS
|
||||
lastBlockNo := (offset + length - 1) / be.plainBS
|
||||
|
||||
alignedOffset = firstBlockNo * be.cipherBS
|
||||
alignedLength = (lastBlockNo - firstBlockNo + 1) * be.cipherBS
|
||||
@ -191,10 +191,10 @@ func (be *CryptFS) CropPlaintext(plaintext []byte, blocks []intraBlock) []byte {
|
||||
last := blocks[len(blocks)-1]
|
||||
length := (last.BlockNo - blocks[0].BlockNo + 1) * be.plainBS
|
||||
var cropped []byte
|
||||
if offset + length > uint64(len(plaintext)) {
|
||||
if offset+length > uint64(len(plaintext)) {
|
||||
cropped = plaintext[offset:len(plaintext)]
|
||||
} else {
|
||||
cropped = plaintext[offset:offset+length]
|
||||
cropped = plaintext[offset : offset+length]
|
||||
}
|
||||
return cropped
|
||||
}
|
||||
@ -209,7 +209,7 @@ func (be *CryptFS) MergeBlocks(oldData []byte, newData []byte, offset int) []byt
|
||||
// Copy old and new data into it
|
||||
copy(out, oldData)
|
||||
l := len(newData)
|
||||
copy(out[offset:offset + l], newData)
|
||||
copy(out[offset:offset+l], newData)
|
||||
|
||||
// Crop to length
|
||||
outLen := len(oldData)
|
||||
|
@ -3,12 +3,12 @@ package cryptfs
|
||||
// Filename encryption / decryption function
|
||||
|
||||
import (
|
||||
"crypto/cipher"
|
||||
"crypto/aes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -30,7 +30,7 @@ func (be *CryptFS) decryptName(cipherName string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(bin) % aes.BlockSize != 0 {
|
||||
if len(bin)%aes.BlockSize != 0 {
|
||||
return "", errors.New(fmt.Sprintf("Name len=%d is not a multiple of 16", len(bin)))
|
||||
}
|
||||
|
||||
@ -120,7 +120,7 @@ func (be *CryptFS) pad16(orig []byte) (padded []byte) {
|
||||
if oldLen == 0 {
|
||||
panic("Padding zero-length string makes no sense")
|
||||
}
|
||||
padLen := aes.BlockSize - oldLen % aes.BlockSize
|
||||
padLen := aes.BlockSize - oldLen%aes.BlockSize
|
||||
if padLen == 0 {
|
||||
padLen = aes.BlockSize
|
||||
}
|
||||
@ -137,11 +137,11 @@ func (be *CryptFS) pad16(orig []byte) (padded []byte) {
|
||||
// unPad16 - remove padding
|
||||
func (be *CryptFS) unPad16(orig []byte) ([]byte, error) {
|
||||
oldLen := len(orig)
|
||||
if oldLen % aes.BlockSize != 0 {
|
||||
if oldLen%aes.BlockSize != 0 {
|
||||
return nil, errors.New("Unaligned size")
|
||||
}
|
||||
// The last byte is always a padding byte
|
||||
padByte := orig[oldLen -1]
|
||||
padByte := orig[oldLen-1]
|
||||
// The padding byte's value is the padding length
|
||||
padLen := int(padByte)
|
||||
// Padding must be at least 1 byte
|
||||
|
@ -29,9 +29,9 @@ func (ib *intraBlock) PlaintextRange() (offset uint64, length uint64) {
|
||||
}
|
||||
|
||||
// CropBlock - crop a potentially larger plaintext block down to the relevant part
|
||||
func (ib *intraBlock) CropBlock(d []byte) []byte{
|
||||
func (ib *intraBlock) CropBlock(d []byte) []byte {
|
||||
lenHave := len(d)
|
||||
lenWant := int(ib.Skip+ib.Length)
|
||||
lenWant := int(ib.Skip + ib.Length)
|
||||
if lenHave < lenWant {
|
||||
return d[ib.Skip:lenHave]
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package cryptfs
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTranslatePath(t *testing.T) {
|
||||
@ -14,7 +14,7 @@ func TestTranslatePath(t *testing.T) {
|
||||
key := make([]byte, 16)
|
||||
fs := NewCryptFS(key, true)
|
||||
|
||||
for _, n := range(s) {
|
||||
for _, n := range s {
|
||||
c := fs.EncryptPath(n)
|
||||
d, err := fs.DecryptPath(c)
|
||||
if err != nil {
|
||||
@ -36,13 +36,13 @@ func TestPad16(t *testing.T) {
|
||||
key := make([]byte, 16)
|
||||
fs := NewCryptFS(key, true)
|
||||
|
||||
for i := range(s) {
|
||||
for i := range s {
|
||||
orig := s[i]
|
||||
padded := fs.pad16(orig)
|
||||
if len(padded) <= len(orig) {
|
||||
t.Errorf("Padded length not bigger than orig: %d", len(padded))
|
||||
}
|
||||
if len(padded) % 16 != 0 {
|
||||
if len(padded)%16 != 0 {
|
||||
t.Errorf("Length is not aligend: %d", len(padded))
|
||||
}
|
||||
unpadded, err := fs.unPad16(padded)
|
||||
@ -52,7 +52,7 @@ func TestPad16(t *testing.T) {
|
||||
if len(unpadded) != len(orig) {
|
||||
t.Errorf("Size mismatch: orig=%d unpadded=%d", len(s[i]), len(unpadded))
|
||||
}
|
||||
if ! bytes.Equal(orig, unpadded) {
|
||||
if !bytes.Equal(orig, unpadded) {
|
||||
t.Error("Content mismatch orig vs unpadded")
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package cryptfs
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"sync"
|
||||
"crypto/rand"
|
||||
)
|
||||
|
||||
type nonce96 struct {
|
||||
|
@ -66,8 +66,8 @@ func (be opensslGCM) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
|
||||
}
|
||||
|
||||
l := len(ciphertext)
|
||||
tag := ciphertext[l-AUTH_TAG_LEN:l]
|
||||
ciphertext = ciphertext[0:l-AUTH_TAG_LEN]
|
||||
tag := ciphertext[l-AUTH_TAG_LEN : l]
|
||||
ciphertext = ciphertext[0 : l-AUTH_TAG_LEN]
|
||||
plainBuf := bytes.NewBuffer(dst)
|
||||
|
||||
dctx, err := openssl.NewGCMDecryptionCipherCtx(128, nil, be.key[:], nonce[:])
|
||||
|
14
main.go
14
main.go
@ -1,18 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"runtime/pprof"
|
||||
"io/ioutil"
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"encoding/hex"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
"time"
|
||||
|
||||
"github.com/rfjakob/gocryptfs/pathfs_frontend"
|
||||
"github.com/rfjakob/gocryptfs/cryptfs"
|
||||
"github.com/rfjakob/gocryptfs/pathfs_frontend"
|
||||
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
|
||||
@ -206,9 +206,9 @@ func pathfsFrontend(key []byte, cipherdir string, mountpoint string, debug bool)
|
||||
mOpts.AllowOther = false
|
||||
// Set values shown in "df -T" and friends
|
||||
// First column, "Filesystem"
|
||||
mOpts.Options = append(mOpts.Options, "fsname=" + cipherdir)
|
||||
mOpts.Options = append(mOpts.Options, "fsname="+cipherdir)
|
||||
// Second column, "Type", will be shown as "fuse." + Name
|
||||
mOpts.Name="gocryptfs"
|
||||
mOpts.Name = "gocryptfs"
|
||||
|
||||
state, err := fuse.NewServer(conn.RawFS(), mountpoint, &mOpts)
|
||||
if err != nil {
|
||||
|
@ -227,12 +227,12 @@ func BenchmarkStreamRead(t *testing.B) {
|
||||
if t.N > mb {
|
||||
// Grow file so we can satisfy the test
|
||||
fmt.Printf("Growing file to %d MB... ", t.N)
|
||||
f2, err := os.OpenFile(fn, os.O_WRONLY | os.O_APPEND, 0666)
|
||||
f2, err := os.OpenFile(fn, os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.FailNow()
|
||||
}
|
||||
for h := 0; h < t.N - mb ; h++ {
|
||||
for h := 0; h < t.N-mb; h++ {
|
||||
_, err = f2.Write(buf)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
@ -7,10 +7,10 @@ package benchmark
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"github.com/spacemonkeygo/openssl"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"github.com/spacemonkeygo/openssl"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func BenchmarkAESGCMSeal4K(b *testing.B) {
|
||||
@ -86,7 +86,7 @@ func BenchmarkOpensslGCMenc4K(b *testing.B) {
|
||||
|
||||
func BenchmarkOpensslGCMdec4K(b *testing.B) {
|
||||
buf := makeOpensslCiphertext()
|
||||
b.SetBytes(int64(1024*4))
|
||||
b.SetBytes(int64(1024 * 4))
|
||||
|
||||
tag := buf[4096:]
|
||||
buf = buf[0:4096]
|
||||
|
@ -1,9 +1,9 @@
|
||||
package pathfs_frontend
|
||||
|
||||
import (
|
||||
"io"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"syscall"
|
||||
@ -101,7 +101,7 @@ func (f *file) doRead(off uint64, length uint64) ([]byte, fuse.Status) {
|
||||
lenHave := len(plaintext)
|
||||
lenWant := skip + int(length)
|
||||
if lenHave > lenWant {
|
||||
out = plaintext[skip:skip + int(length)]
|
||||
out = plaintext[skip : skip+int(length)]
|
||||
} else if lenHave > skip {
|
||||
out = plaintext[skip:lenHave]
|
||||
} else {
|
||||
@ -139,7 +139,7 @@ func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) {
|
||||
status := fuse.OK
|
||||
dataBuf := bytes.NewBuffer(data)
|
||||
blocks := f.cfs.SplitRange(uint64(off), uint64(len(data)))
|
||||
for _, b := range(blocks) {
|
||||
for _, b := range blocks {
|
||||
|
||||
blockData := dataBuf.Next(int(b.Length))
|
||||
|
||||
@ -180,7 +180,7 @@ func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) {
|
||||
|
||||
// Write - FUSE call
|
||||
func (f *file) Write(data []byte, off int64) (uint32, fuse.Status) {
|
||||
cryptfs.Debug.Printf("ino%d: FUSE Write %s: offset=%d length=%d\n", f.ino, off, len(data))
|
||||
cryptfs.Debug.Printf("ino%d: FUSE Write: offset=%d length=%d\n", f.ino, off, len(data))
|
||||
|
||||
fi, err := f.fd.Stat()
|
||||
if err != nil {
|
||||
@ -248,8 +248,8 @@ func (f *file) Truncate(newSize uint64) fuse.Status {
|
||||
|
||||
// File grows
|
||||
if newSize > oldSize {
|
||||
blocks := f.cfs.SplitRange(oldSize, newSize - oldSize)
|
||||
for _, b := range(blocks) {
|
||||
blocks := f.cfs.SplitRange(oldSize, newSize-oldSize)
|
||||
for _, b := range blocks {
|
||||
// First and last block may be partial
|
||||
if b.IsPartial() {
|
||||
off, _ := b.PlaintextRange()
|
||||
@ -261,7 +261,7 @@ func (f *file) Truncate(newSize uint64) fuse.Status {
|
||||
} else {
|
||||
off, length := b.CiphertextRange()
|
||||
f.lock.Lock()
|
||||
err := syscall.Ftruncate(int(f.fd.Fd()), int64(off + length))
|
||||
err := syscall.Ftruncate(int(f.fd.Fd()), int64(off+length))
|
||||
f.lock.Unlock()
|
||||
if err != nil {
|
||||
cryptfs.Warn.Printf("grow Ftruncate returned error: %v", err)
|
||||
|
@ -1,10 +1,10 @@
|
||||
package pathfs_frontend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"fmt"
|
||||
|
||||
"github.com/hanwen/go-fuse/fuse"
|
||||
"github.com/hanwen/go-fuse/fuse/nodefs"
|
||||
@ -24,7 +24,6 @@ func NewFS(key []byte, backing string, useOpenssl bool) *FS {
|
||||
CryptFS: cryptfs.NewCryptFS(key, useOpenssl),
|
||||
FileSystem: pathfs.NewLoopbackFileSystem(backing),
|
||||
backing: backing,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,7 +51,7 @@ func (fs *FS) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Stat
|
||||
|
||||
func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, fuse.Status) {
|
||||
cryptfs.Debug.Printf("OpenDir(%s)\n", dirName)
|
||||
cipherEntries, status := fs.FileSystem.OpenDir(fs.EncryptPath(dirName), context);
|
||||
cipherEntries, status := fs.FileSystem.OpenDir(fs.EncryptPath(dirName), context)
|
||||
var plain []fuse.DirEntry
|
||||
if cipherEntries != nil {
|
||||
for i := range cipherEntries {
|
||||
@ -76,7 +75,7 @@ func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, f
|
||||
// We always need read access to do read-modify-write cycles
|
||||
func (fs *FS) mangleOpenFlags(flags uint32) (newFlags int, writeOnly bool) {
|
||||
newFlags = int(flags)
|
||||
if newFlags & os.O_WRONLY > 0 {
|
||||
if newFlags&os.O_WRONLY > 0 {
|
||||
writeOnly = true
|
||||
newFlags = newFlags ^ os.O_WRONLY | os.O_RDWR
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user