2016-10-02 06:14:18 +02:00
|
|
|
// Package nametransform encrypts and decrypts filenames.
|
2016-02-06 19:20:54 +01:00
|
|
|
package nametransform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"encoding/base64"
|
2016-09-21 21:43:56 +02:00
|
|
|
"syscall"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
|
|
|
"github.com/rfjakob/eme"
|
2016-06-16 19:02:47 +02:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
2016-07-03 15:35:58 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2016-02-06 19:20:54 +01:00
|
|
|
)
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// NameTransform is used to transform filenames.
|
2016-06-16 19:02:47 +02:00
|
|
|
type NameTransform struct {
|
|
|
|
cryptoCore *cryptocore.CryptoCore
|
|
|
|
longNames bool
|
|
|
|
DirIVCache dirIVCache
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// New returns a new NameTransform instance.
|
2016-06-23 21:56:50 +02:00
|
|
|
func New(c *cryptocore.CryptoCore, longNames bool) *NameTransform {
|
2016-06-16 19:02:47 +02:00
|
|
|
return &NameTransform{
|
|
|
|
cryptoCore: c,
|
|
|
|
longNames: longNames,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
// DecryptName - decrypt base64-encoded encrypted filename "cipherName"
|
|
|
|
//
|
|
|
|
// This function is exported because it allows for a very efficient readdir
|
|
|
|
// implementation (read IV once, decrypt all names using this function).
|
|
|
|
func (n *NameTransform) DecryptName(cipherName string, iv []byte) (string, error) {
|
|
|
|
bin, err := base64.URLEncoding.DecodeString(cipherName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(bin)%aes.BlockSize != 0 {
|
2016-09-25 19:50:16 +02:00
|
|
|
tlog.Debug.Printf("DecryptName %q: decoded length %d is not a multiple of 16", cipherName, len(bin))
|
2016-09-25 18:01:24 +02:00
|
|
|
return "", syscall.EINVAL
|
2016-02-06 19:20:54 +01:00
|
|
|
}
|
2016-06-23 21:56:50 +02:00
|
|
|
bin = eme.Transform(n.cryptoCore.BlockCipher, iv, bin, eme.DirectionDecrypt)
|
2016-02-06 19:20:54 +01:00
|
|
|
bin, err = unPad16(bin)
|
|
|
|
if err != nil {
|
2016-07-03 15:35:58 +02:00
|
|
|
tlog.Debug.Printf("pad16 error detail: %v", err)
|
|
|
|
// unPad16 returns detailed errors including the position of the
|
|
|
|
// incorrect bytes. Kill the padding oracle by lumping everything into
|
|
|
|
// a generic error.
|
2016-09-21 21:43:56 +02:00
|
|
|
return "", syscall.EINVAL
|
2016-02-06 19:20:54 +01:00
|
|
|
}
|
|
|
|
plain := string(bin)
|
|
|
|
return plain, err
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// EncryptName encrypts "plainName", returns a base64-encoded "cipherName64".
|
2016-06-16 19:02:47 +02:00
|
|
|
// Used internally by EncryptPathDirIV().
|
|
|
|
// The encryption is either CBC or EME, depending on "useEME".
|
2016-02-06 22:54:14 +01:00
|
|
|
//
|
|
|
|
// This function is exported because fusefrontend needs access to the full (not hashed)
|
2016-06-16 19:02:47 +02:00
|
|
|
// name if longname is used. Otherwise you should use EncryptPathDirIV()
|
2016-02-06 22:54:14 +01:00
|
|
|
func (n *NameTransform) EncryptName(plainName string, iv []byte) (cipherName64 string) {
|
2016-02-06 19:20:54 +01:00
|
|
|
bin := []byte(plainName)
|
|
|
|
bin = pad16(bin)
|
2016-06-23 21:56:50 +02:00
|
|
|
bin = eme.Transform(n.cryptoCore.BlockCipher, iv, bin, eme.DirectionEncrypt)
|
2016-02-06 19:20:54 +01:00
|
|
|
cipherName64 = base64.URLEncoding.EncodeToString(bin)
|
|
|
|
return cipherName64
|
|
|
|
}
|