2016-07-06 21:51:25 +02:00
|
|
|
// Package namtransforms encrypts and decrypts filenames.
|
2016-02-06 19:20:54 +01:00
|
|
|
package nametransform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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-06-16 19:02:47 +02:00
|
|
|
type NameTransform struct {
|
|
|
|
cryptoCore *cryptocore.CryptoCore
|
|
|
|
longNames bool
|
|
|
|
DirIVCache dirIVCache
|
|
|
|
}
|
|
|
|
|
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"
|
2016-06-16 19:02:47 +02:00
|
|
|
// Used by DecryptPathDirIV().
|
|
|
|
// The encryption is either CBC or EME, depending on "useEME".
|
2016-02-06 19:20:54 +01:00
|
|
|
//
|
|
|
|
// 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 {
|
|
|
|
return "", fmt.Errorf("Decoded length %d is not a multiple of the AES block size", len(bin))
|
|
|
|
}
|
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.
|
|
|
|
return "", fmt.Errorf("Invalid padding")
|
2016-02-06 19:20:54 +01:00
|
|
|
}
|
|
|
|
plain := string(bin)
|
|
|
|
return plain, err
|
|
|
|
}
|
|
|
|
|
2016-06-16 19:02:47 +02:00
|
|
|
// encryptName - encrypt "plainName", return base64-encoded "cipherName64".
|
|
|
|
// 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
|
|
|
|
}
|