2016-07-26 02:18:56 +08:00
|
|
|
package crypt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2016-07-26 02:18:56 +08:00
|
|
|
"crypto/aes"
|
|
|
|
gocipher "crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base32"
|
2016-08-21 01:46:10 +08:00
|
|
|
"fmt"
|
2016-07-26 02:18:56 +08:00
|
|
|
"io"
|
2017-03-13 02:14:36 +08:00
|
|
|
"strconv"
|
2016-07-26 02:18:56 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2021-04-07 03:45:34 +08:00
|
|
|
"time"
|
2016-07-26 02:18:56 +08:00
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/backend/crypt/pkcs7"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
2021-04-07 03:45:34 +08:00
|
|
|
"github.com/rclone/rclone/lib/version"
|
2018-10-13 21:41:15 +08:00
|
|
|
"github.com/rfjakob/eme"
|
2016-07-26 02:18:56 +08:00
|
|
|
"golang.org/x/crypto/nacl/secretbox"
|
|
|
|
"golang.org/x/crypto/scrypt"
|
|
|
|
)
|
|
|
|
|
2016-10-21 00:47:33 +08:00
|
|
|
// Constants
|
2016-07-26 02:18:56 +08:00
|
|
|
const (
|
|
|
|
nameCipherBlockSize = aes.BlockSize
|
|
|
|
fileMagic = "RCLONE\x00\x00"
|
|
|
|
fileMagicSize = len(fileMagic)
|
|
|
|
fileNonceSize = 24
|
|
|
|
fileHeaderSize = fileMagicSize + fileNonceSize
|
|
|
|
blockHeaderSize = secretbox.Overhead
|
|
|
|
blockDataSize = 64 * 1024
|
|
|
|
blockSize = blockHeaderSize + blockDataSize
|
2016-08-21 01:46:10 +08:00
|
|
|
encryptedSuffix = ".bin" // when file name encryption is off we add this suffix to make sure the cloud provider doesn't process the file
|
2016-07-26 02:18:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Errors returned by cipher
|
|
|
|
var (
|
|
|
|
ErrorBadDecryptUTF8 = errors.New("bad decryption - utf-8 invalid")
|
|
|
|
ErrorBadDecryptControlChar = errors.New("bad decryption - contains control chars")
|
|
|
|
ErrorNotAMultipleOfBlocksize = errors.New("not a multiple of blocksize")
|
|
|
|
ErrorTooShortAfterDecode = errors.New("too short after base32 decode")
|
2018-12-19 04:35:41 +08:00
|
|
|
ErrorTooLongAfterDecode = errors.New("too long after base32 decode")
|
2016-07-26 02:18:56 +08:00
|
|
|
ErrorEncryptedFileTooShort = errors.New("file is too short to be encrypted")
|
|
|
|
ErrorEncryptedFileBadHeader = errors.New("file has truncated block header")
|
|
|
|
ErrorEncryptedBadMagic = errors.New("not an encrypted file - bad magic string")
|
|
|
|
ErrorEncryptedBadBlock = errors.New("failed to authenticate decrypted block - bad password?")
|
|
|
|
ErrorBadBase32Encoding = errors.New("bad base32 filename encoding")
|
|
|
|
ErrorFileClosed = errors.New("file already closed")
|
2016-08-21 01:46:10 +08:00
|
|
|
ErrorNotAnEncryptedFile = errors.New("not an encrypted file - no \"" + encryptedSuffix + "\" suffix")
|
2016-11-02 06:30:20 +08:00
|
|
|
ErrorBadSeek = errors.New("Seek beyond end of file")
|
2016-08-20 03:02:02 +08:00
|
|
|
defaultSalt = []byte{0xA8, 0x0D, 0xF4, 0x3A, 0x8F, 0xBD, 0x03, 0x08, 0xA7, 0xCA, 0xB8, 0x3E, 0x58, 0x1F, 0x86, 0xB1}
|
2017-03-13 02:14:36 +08:00
|
|
|
obfuscQuoteRune = '!'
|
2016-07-26 02:18:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Global variables
|
|
|
|
var (
|
|
|
|
fileMagicBytes = []byte(fileMagic)
|
|
|
|
)
|
|
|
|
|
2016-10-21 00:47:33 +08:00
|
|
|
// ReadSeekCloser is the interface of the read handles
|
|
|
|
type ReadSeekCloser interface {
|
|
|
|
io.Reader
|
|
|
|
io.Seeker
|
|
|
|
io.Closer
|
2018-01-23 02:31:59 +08:00
|
|
|
fs.RangeSeeker
|
2016-10-21 00:47:33 +08:00
|
|
|
}
|
|
|
|
|
2018-01-23 02:31:59 +08:00
|
|
|
// OpenRangeSeek opens the file handle at the offset with the limit given
|
2019-06-17 16:34:30 +08:00
|
|
|
type OpenRangeSeek func(ctx context.Context, offset, limit int64) (io.ReadCloser, error)
|
2016-10-21 00:47:33 +08:00
|
|
|
|
2016-08-21 01:46:10 +08:00
|
|
|
// NameEncryptionMode is the type of file name encryption in use
|
|
|
|
type NameEncryptionMode int
|
|
|
|
|
|
|
|
// NameEncryptionMode levels
|
|
|
|
const (
|
|
|
|
NameEncryptionOff NameEncryptionMode = iota
|
|
|
|
NameEncryptionStandard
|
2017-03-13 02:14:36 +08:00
|
|
|
NameEncryptionObfuscated
|
2016-08-21 01:46:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewNameEncryptionMode turns a string into a NameEncryptionMode
|
|
|
|
func NewNameEncryptionMode(s string) (mode NameEncryptionMode, err error) {
|
|
|
|
s = strings.ToLower(s)
|
|
|
|
switch s {
|
|
|
|
case "off":
|
|
|
|
mode = NameEncryptionOff
|
|
|
|
case "standard":
|
|
|
|
mode = NameEncryptionStandard
|
2017-03-13 02:14:36 +08:00
|
|
|
case "obfuscate":
|
|
|
|
mode = NameEncryptionObfuscated
|
2016-08-21 01:46:10 +08:00
|
|
|
default:
|
|
|
|
err = errors.Errorf("Unknown file name encryption mode %q", s)
|
|
|
|
}
|
|
|
|
return mode, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// String turns mode into a human readable string
|
|
|
|
func (mode NameEncryptionMode) String() (out string) {
|
|
|
|
switch mode {
|
|
|
|
case NameEncryptionOff:
|
|
|
|
out = "off"
|
|
|
|
case NameEncryptionStandard:
|
|
|
|
out = "standard"
|
2017-03-13 02:14:36 +08:00
|
|
|
case NameEncryptionObfuscated:
|
|
|
|
out = "obfuscate"
|
2016-08-21 01:46:10 +08:00
|
|
|
default:
|
|
|
|
out = fmt.Sprintf("Unknown mode #%d", mode)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-04-09 23:17:17 +08:00
|
|
|
// Cipher defines an encoding and decoding cipher for the crypt backend
|
|
|
|
type Cipher struct {
|
2017-11-06 15:35:53 +08:00
|
|
|
dataKey [32]byte // Key for secretbox
|
|
|
|
nameKey [32]byte // 16,24 or 32 bytes
|
|
|
|
nameTweak [nameCipherBlockSize]byte // used to tweak the name crypto
|
|
|
|
block gocipher.Block
|
|
|
|
mode NameEncryptionMode
|
|
|
|
buffers sync.Pool // encrypt/decrypt buffers
|
|
|
|
cryptoRand io.Reader // read crypto random numbers from here
|
|
|
|
dirNameEncrypt bool
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
|
|
|
|
2016-08-20 03:02:02 +08:00
|
|
|
// newCipher initialises the cipher. If salt is "" then it uses a built in salt val
|
2020-04-09 23:17:17 +08:00
|
|
|
func newCipher(mode NameEncryptionMode, password, salt string, dirNameEncrypt bool) (*Cipher, error) {
|
|
|
|
c := &Cipher{
|
2017-11-06 15:35:53 +08:00
|
|
|
mode: mode,
|
|
|
|
cryptoRand: rand.Reader,
|
|
|
|
dirNameEncrypt: dirNameEncrypt,
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
|
|
|
c.buffers.New = func() interface{} {
|
|
|
|
return make([]byte, blockSize)
|
|
|
|
}
|
2016-08-20 03:02:02 +08:00
|
|
|
err := c.Key(password, salt)
|
2016-07-26 02:18:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key creates all the internal keys from the password passed in using
|
2016-08-20 03:02:02 +08:00
|
|
|
// scrypt.
|
|
|
|
//
|
|
|
|
// If salt is "" we use a fixed salt just to make attackers lives
|
|
|
|
// slighty harder than using no salt.
|
2016-07-26 02:18:56 +08:00
|
|
|
//
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 08:17:24 +08:00
|
|
|
// Note that empty password makes all 0x00 keys which is used in the
|
2016-07-26 02:18:56 +08:00
|
|
|
// tests.
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) Key(password, salt string) (err error) {
|
2016-07-26 02:18:56 +08:00
|
|
|
const keySize = len(c.dataKey) + len(c.nameKey) + len(c.nameTweak)
|
2016-08-20 03:02:02 +08:00
|
|
|
var saltBytes = defaultSalt
|
|
|
|
if salt != "" {
|
|
|
|
saltBytes = []byte(salt)
|
|
|
|
}
|
2016-07-26 02:18:56 +08:00
|
|
|
var key []byte
|
|
|
|
if password == "" {
|
|
|
|
key = make([]byte, keySize)
|
|
|
|
} else {
|
2016-08-20 03:02:02 +08:00
|
|
|
key, err = scrypt.Key([]byte(password), saltBytes, 16384, 8, 1, keySize)
|
2016-07-26 02:18:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
copy(c.dataKey[:], key)
|
|
|
|
copy(c.nameKey[:], key[len(c.dataKey):])
|
|
|
|
copy(c.nameTweak[:], key[len(c.dataKey)+len(c.nameKey):])
|
|
|
|
// Key the name cipher
|
|
|
|
c.block, err = aes.NewCipher(c.nameKey[:])
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBlock gets a block from the pool of size blockSize
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) getBlock() []byte {
|
2016-07-26 02:18:56 +08:00
|
|
|
return c.buffers.Get().([]byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
// putBlock returns a block to the pool of size blockSize
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) putBlock(buf []byte) {
|
2016-07-26 02:18:56 +08:00
|
|
|
if len(buf) != blockSize {
|
|
|
|
panic("bad blocksize returned to pool")
|
|
|
|
}
|
|
|
|
c.buffers.Put(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// encodeFileName encodes a filename using a modified version of
|
|
|
|
// standard base32 as described in RFC4648
|
|
|
|
//
|
|
|
|
// The standard encoding is modified in two ways
|
|
|
|
// * it becomes lower case (no-one likes upper case filenames!)
|
|
|
|
// * we strip the padding character `=`
|
|
|
|
func encodeFileName(in []byte) string {
|
|
|
|
encoded := base32.HexEncoding.EncodeToString(in)
|
|
|
|
encoded = strings.TrimRight(encoded, "=")
|
|
|
|
return strings.ToLower(encoded)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeFileName decodes a filename as encoded by encodeFileName
|
|
|
|
func decodeFileName(in string) ([]byte, error) {
|
|
|
|
if strings.HasSuffix(in, "=") {
|
|
|
|
return nil, ErrorBadBase32Encoding
|
|
|
|
}
|
|
|
|
// First figure out how many padding characters to add
|
|
|
|
roundUpToMultipleOf8 := (len(in) + 7) &^ 7
|
|
|
|
equals := roundUpToMultipleOf8 - len(in)
|
|
|
|
in = strings.ToUpper(in) + "========"[:equals]
|
|
|
|
return base32.HexEncoding.DecodeString(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryptSegment encrypts a path segment
|
|
|
|
//
|
|
|
|
// This uses EME with AES
|
|
|
|
//
|
|
|
|
// EME (ECB-Mix-ECB) is a wide-block encryption mode presented in the
|
|
|
|
// 2003 paper "A Parallelizable Enciphering Mode" by Halevi and
|
|
|
|
// Rogaway.
|
|
|
|
//
|
2020-05-20 18:39:20 +08:00
|
|
|
// This makes for deterministic encryption which is what we want - the
|
2016-07-26 02:18:56 +08:00
|
|
|
// same filename must encrypt to the same thing.
|
|
|
|
//
|
|
|
|
// This means that
|
|
|
|
// * filenames with the same name will encrypt the same
|
|
|
|
// * filenames which start the same won't have a common prefix
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) encryptSegment(plaintext string) string {
|
2016-07-26 02:18:56 +08:00
|
|
|
if plaintext == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
paddedPlaintext := pkcs7.Pad(nameCipherBlockSize, []byte(plaintext))
|
|
|
|
ciphertext := eme.Transform(c.block, c.nameTweak[:], paddedPlaintext, eme.DirectionEncrypt)
|
|
|
|
return encodeFileName(ciphertext)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decryptSegment decrypts a path segment
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) decryptSegment(ciphertext string) (string, error) {
|
2016-07-26 02:18:56 +08:00
|
|
|
if ciphertext == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
rawCiphertext, err := decodeFileName(ciphertext)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(rawCiphertext)%nameCipherBlockSize != 0 {
|
|
|
|
return "", ErrorNotAMultipleOfBlocksize
|
|
|
|
}
|
|
|
|
if len(rawCiphertext) == 0 {
|
|
|
|
// not possible if decodeFilename() working correctly
|
|
|
|
return "", ErrorTooShortAfterDecode
|
|
|
|
}
|
2018-12-19 04:35:41 +08:00
|
|
|
if len(rawCiphertext) > 2048 {
|
|
|
|
return "", ErrorTooLongAfterDecode
|
|
|
|
}
|
2016-07-26 02:18:56 +08:00
|
|
|
paddedPlaintext := eme.Transform(c.block, c.nameTweak[:], rawCiphertext, eme.DirectionDecrypt)
|
|
|
|
plaintext, err := pkcs7.Unpad(nameCipherBlockSize, paddedPlaintext)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(plaintext), err
|
|
|
|
}
|
|
|
|
|
2017-03-13 02:14:36 +08:00
|
|
|
// Simple obfuscation routines
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) obfuscateSegment(plaintext string) string {
|
2017-03-13 02:14:36 +08:00
|
|
|
if plaintext == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the string isn't valid UTF8 then don't rotate; just
|
2017-04-14 20:30:18 +08:00
|
|
|
// prepend a !.
|
2017-03-13 02:14:36 +08:00
|
|
|
if !utf8.ValidString(plaintext) {
|
2017-04-14 20:30:18 +08:00
|
|
|
return "!." + plaintext
|
2017-03-13 02:14:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate a simple rotation based on the filename and
|
|
|
|
// the nameKey
|
|
|
|
var dir int
|
|
|
|
for _, runeValue := range plaintext {
|
|
|
|
dir += int(runeValue)
|
|
|
|
}
|
|
|
|
dir = dir % 256
|
|
|
|
|
|
|
|
// We'll use this number to store in the result filename...
|
|
|
|
var result bytes.Buffer
|
|
|
|
_, _ = result.WriteString(strconv.Itoa(dir) + ".")
|
|
|
|
|
|
|
|
// but we'll augment it with the nameKey for real calculation
|
|
|
|
for i := 0; i < len(c.nameKey); i++ {
|
|
|
|
dir += int(c.nameKey[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now for each character, depending on the range it is in
|
|
|
|
// we will actually rotate a different amount
|
|
|
|
for _, runeValue := range plaintext {
|
|
|
|
switch {
|
|
|
|
case runeValue == obfuscQuoteRune:
|
|
|
|
// Quote the Quote character
|
|
|
|
_, _ = result.WriteRune(obfuscQuoteRune)
|
|
|
|
_, _ = result.WriteRune(obfuscQuoteRune)
|
|
|
|
|
|
|
|
case runeValue >= '0' && runeValue <= '9':
|
|
|
|
// Number
|
|
|
|
thisdir := (dir % 9) + 1
|
|
|
|
newRune := '0' + (int(runeValue)-'0'+thisdir)%10
|
|
|
|
_, _ = result.WriteRune(rune(newRune))
|
|
|
|
|
|
|
|
case (runeValue >= 'A' && runeValue <= 'Z') ||
|
|
|
|
(runeValue >= 'a' && runeValue <= 'z'):
|
|
|
|
// ASCII letter. Try to avoid trivial A->a mappings
|
|
|
|
thisdir := dir%25 + 1
|
|
|
|
// Calculate the offset of this character in A-Za-z
|
|
|
|
pos := int(runeValue - 'A')
|
|
|
|
if pos >= 26 {
|
|
|
|
pos -= 6 // It's lower case
|
|
|
|
}
|
|
|
|
// Rotate the character to the new location
|
|
|
|
pos = (pos + thisdir) % 52
|
|
|
|
if pos >= 26 {
|
|
|
|
pos += 6 // and handle lower case offset again
|
|
|
|
}
|
|
|
|
_, _ = result.WriteRune(rune('A' + pos))
|
|
|
|
|
|
|
|
case runeValue >= 0xA0 && runeValue <= 0xFF:
|
|
|
|
// Latin 1 supplement
|
|
|
|
thisdir := (dir % 95) + 1
|
|
|
|
newRune := 0xA0 + (int(runeValue)-0xA0+thisdir)%96
|
|
|
|
_, _ = result.WriteRune(rune(newRune))
|
|
|
|
|
|
|
|
case runeValue >= 0x100:
|
|
|
|
// Some random Unicode range; we have no good rules here
|
|
|
|
thisdir := (dir % 127) + 1
|
|
|
|
base := int(runeValue - runeValue%256)
|
|
|
|
newRune := rune(base + (int(runeValue)-base+thisdir)%256)
|
|
|
|
// If the new character isn't a valid UTF8 char
|
|
|
|
// then don't rotate it. Quote it instead
|
|
|
|
if !utf8.ValidRune(newRune) {
|
|
|
|
_, _ = result.WriteRune(obfuscQuoteRune)
|
|
|
|
_, _ = result.WriteRune(runeValue)
|
|
|
|
} else {
|
|
|
|
_, _ = result.WriteRune(newRune)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Leave character untouched
|
|
|
|
_, _ = result.WriteRune(runeValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result.String()
|
|
|
|
}
|
|
|
|
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) deobfuscateSegment(ciphertext string) (string, error) {
|
2017-03-13 02:14:36 +08:00
|
|
|
if ciphertext == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
pos := strings.Index(ciphertext, ".")
|
|
|
|
if pos == -1 {
|
|
|
|
return "", ErrorNotAnEncryptedFile
|
|
|
|
} // No .
|
|
|
|
num := ciphertext[:pos]
|
2017-04-14 20:30:18 +08:00
|
|
|
if num == "!" {
|
2017-03-13 02:14:36 +08:00
|
|
|
// No rotation; probably original was not valid unicode
|
|
|
|
return ciphertext[pos+1:], nil
|
|
|
|
}
|
|
|
|
dir, err := strconv.Atoi(num)
|
|
|
|
if err != nil {
|
|
|
|
return "", ErrorNotAnEncryptedFile // Not a number
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the nameKey to get the real rotate distance
|
|
|
|
for i := 0; i < len(c.nameKey); i++ {
|
|
|
|
dir += int(c.nameKey[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
var result bytes.Buffer
|
|
|
|
|
|
|
|
inQuote := false
|
|
|
|
for _, runeValue := range ciphertext[pos+1:] {
|
|
|
|
switch {
|
|
|
|
case inQuote:
|
|
|
|
_, _ = result.WriteRune(runeValue)
|
|
|
|
inQuote = false
|
|
|
|
|
|
|
|
case runeValue == obfuscQuoteRune:
|
|
|
|
inQuote = true
|
|
|
|
|
|
|
|
case runeValue >= '0' && runeValue <= '9':
|
|
|
|
// Number
|
|
|
|
thisdir := (dir % 9) + 1
|
|
|
|
newRune := '0' + int(runeValue) - '0' - thisdir
|
|
|
|
if newRune < '0' {
|
|
|
|
newRune += 10
|
|
|
|
}
|
|
|
|
_, _ = result.WriteRune(rune(newRune))
|
|
|
|
|
|
|
|
case (runeValue >= 'A' && runeValue <= 'Z') ||
|
|
|
|
(runeValue >= 'a' && runeValue <= 'z'):
|
|
|
|
thisdir := dir%25 + 1
|
|
|
|
pos := int(runeValue - 'A')
|
|
|
|
if pos >= 26 {
|
|
|
|
pos -= 6
|
|
|
|
}
|
|
|
|
pos = pos - thisdir
|
|
|
|
if pos < 0 {
|
|
|
|
pos += 52
|
|
|
|
}
|
|
|
|
if pos >= 26 {
|
|
|
|
pos += 6
|
|
|
|
}
|
|
|
|
_, _ = result.WriteRune(rune('A' + pos))
|
|
|
|
|
|
|
|
case runeValue >= 0xA0 && runeValue <= 0xFF:
|
|
|
|
thisdir := (dir % 95) + 1
|
|
|
|
newRune := 0xA0 + int(runeValue) - 0xA0 - thisdir
|
|
|
|
if newRune < 0xA0 {
|
|
|
|
newRune += 96
|
|
|
|
}
|
|
|
|
_, _ = result.WriteRune(rune(newRune))
|
|
|
|
|
|
|
|
case runeValue >= 0x100:
|
|
|
|
thisdir := (dir % 127) + 1
|
|
|
|
base := int(runeValue - runeValue%256)
|
|
|
|
newRune := rune(base + (int(runeValue) - base - thisdir))
|
|
|
|
if int(newRune) < base {
|
|
|
|
newRune += 256
|
|
|
|
}
|
2019-01-12 01:17:46 +08:00
|
|
|
_, _ = result.WriteRune(newRune)
|
2017-03-13 02:14:36 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
_, _ = result.WriteRune(runeValue)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.String(), nil
|
|
|
|
}
|
|
|
|
|
2016-08-21 01:46:10 +08:00
|
|
|
// encryptFileName encrypts a file path
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) encryptFileName(in string) string {
|
2016-08-21 01:46:10 +08:00
|
|
|
segments := strings.Split(in, "/")
|
|
|
|
for i := range segments {
|
2017-11-06 15:35:53 +08:00
|
|
|
// Skip directory name encryption if the user chose to
|
|
|
|
// leave them intact
|
|
|
|
if !c.dirNameEncrypt && i != (len(segments)-1) {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-07 03:45:34 +08:00
|
|
|
|
|
|
|
// Strip version string so that only the non-versioned part
|
|
|
|
// of the file name gets encrypted/obfuscated
|
|
|
|
hasVersion := false
|
|
|
|
var t time.Time
|
|
|
|
if i == (len(segments)-1) && version.Match(segments[i]) {
|
|
|
|
var s string
|
|
|
|
t, s = version.Remove(segments[i])
|
|
|
|
// version.Remove can fail, in which case it returns segments[i]
|
|
|
|
if s != segments[i] {
|
|
|
|
segments[i] = s
|
|
|
|
hasVersion = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 02:14:36 +08:00
|
|
|
if c.mode == NameEncryptionStandard {
|
|
|
|
segments[i] = c.encryptSegment(segments[i])
|
|
|
|
} else {
|
|
|
|
segments[i] = c.obfuscateSegment(segments[i])
|
|
|
|
}
|
2021-04-07 03:45:34 +08:00
|
|
|
|
|
|
|
// Add back a version to the encrypted/obfuscated
|
|
|
|
// file name, if we stripped it off earlier
|
|
|
|
if hasVersion {
|
|
|
|
segments[i] = version.Add(segments[i], t)
|
|
|
|
}
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
2016-08-21 01:46:10 +08:00
|
|
|
return strings.Join(segments, "/")
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
|
|
|
|
2016-08-21 01:46:10 +08:00
|
|
|
// EncryptFileName encrypts a file path
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) EncryptFileName(in string) string {
|
2016-08-21 01:46:10 +08:00
|
|
|
if c.mode == NameEncryptionOff {
|
|
|
|
return in + encryptedSuffix
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
2016-08-21 01:46:10 +08:00
|
|
|
return c.encryptFileName(in)
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
|
|
|
|
2016-08-21 01:46:10 +08:00
|
|
|
// EncryptDirName encrypts a directory path
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) EncryptDirName(in string) string {
|
2017-11-06 15:35:53 +08:00
|
|
|
if c.mode == NameEncryptionOff || !c.dirNameEncrypt {
|
2016-08-21 01:46:10 +08:00
|
|
|
return in
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
2016-08-21 01:46:10 +08:00
|
|
|
return c.encryptFileName(in)
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
|
|
|
|
2016-08-21 01:46:10 +08:00
|
|
|
// decryptFileName decrypts a file path
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) decryptFileName(in string) (string, error) {
|
2016-07-26 02:18:56 +08:00
|
|
|
segments := strings.Split(in, "/")
|
|
|
|
for i := range segments {
|
|
|
|
var err error
|
2017-11-06 15:35:53 +08:00
|
|
|
// Skip directory name decryption if the user chose to
|
|
|
|
// leave them intact
|
|
|
|
if !c.dirNameEncrypt && i != (len(segments)-1) {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-07 03:45:34 +08:00
|
|
|
|
|
|
|
// Strip version string so that only the non-versioned part
|
|
|
|
// of the file name gets decrypted/deobfuscated
|
|
|
|
hasVersion := false
|
|
|
|
var t time.Time
|
|
|
|
if i == (len(segments)-1) && version.Match(segments[i]) {
|
|
|
|
var s string
|
|
|
|
t, s = version.Remove(segments[i])
|
|
|
|
// version.Remove can fail, in which case it returns segments[i]
|
|
|
|
if s != segments[i] {
|
|
|
|
segments[i] = s
|
|
|
|
hasVersion = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 02:14:36 +08:00
|
|
|
if c.mode == NameEncryptionStandard {
|
|
|
|
segments[i], err = c.decryptSegment(segments[i])
|
|
|
|
} else {
|
|
|
|
segments[i], err = c.deobfuscateSegment(segments[i])
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:18:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-04-07 03:45:34 +08:00
|
|
|
|
|
|
|
// Add back a version to the decrypted/deobfuscated
|
|
|
|
// file name, if we stripped it off earlier
|
|
|
|
if hasVersion {
|
|
|
|
segments[i] = version.Add(segments[i], t)
|
|
|
|
}
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
|
|
|
return strings.Join(segments, "/"), nil
|
|
|
|
}
|
|
|
|
|
2016-08-21 01:46:10 +08:00
|
|
|
// DecryptFileName decrypts a file path
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) DecryptFileName(in string) (string, error) {
|
2016-08-21 01:46:10 +08:00
|
|
|
if c.mode == NameEncryptionOff {
|
|
|
|
remainingLength := len(in) - len(encryptedSuffix)
|
2021-04-07 03:45:34 +08:00
|
|
|
if remainingLength == 0 || !strings.HasSuffix(in, encryptedSuffix) {
|
|
|
|
return "", ErrorNotAnEncryptedFile
|
2016-08-21 01:46:10 +08:00
|
|
|
}
|
2021-04-07 03:45:34 +08:00
|
|
|
decrypted := in[:remainingLength]
|
|
|
|
if version.Match(decrypted) {
|
|
|
|
_, unversioned := version.Remove(decrypted)
|
|
|
|
if unversioned == "" {
|
|
|
|
return "", ErrorNotAnEncryptedFile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Leave the version string on, if it was there
|
|
|
|
return decrypted, nil
|
2016-08-21 01:46:10 +08:00
|
|
|
}
|
|
|
|
return c.decryptFileName(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptDirName decrypts a directory path
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) DecryptDirName(in string) (string, error) {
|
2017-11-06 15:35:53 +08:00
|
|
|
if c.mode == NameEncryptionOff || !c.dirNameEncrypt {
|
2016-08-21 01:46:10 +08:00
|
|
|
return in, nil
|
|
|
|
}
|
|
|
|
return c.decryptFileName(in)
|
|
|
|
}
|
|
|
|
|
2020-04-09 23:43:34 +08:00
|
|
|
// NameEncryptionMode returns the encryption mode in use for names
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) NameEncryptionMode() NameEncryptionMode {
|
2018-02-25 19:57:14 +08:00
|
|
|
return c.mode
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:18:56 +08:00
|
|
|
// nonce is an NACL secretbox nonce
|
|
|
|
type nonce [fileNonceSize]byte
|
|
|
|
|
|
|
|
// pointer returns the nonce as a *[24]byte for secretbox
|
|
|
|
func (n *nonce) pointer() *[fileNonceSize]byte {
|
|
|
|
return (*[fileNonceSize]byte)(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromReader fills the nonce from an io.Reader - normally the OSes
|
|
|
|
// crypto random number generator
|
|
|
|
func (n *nonce) fromReader(in io.Reader) error {
|
|
|
|
read, err := io.ReadFull(in, (*n)[:])
|
|
|
|
if read != fileNonceSize {
|
|
|
|
return errors.Wrap(err, "short read of nonce")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromBuf fills the nonce from the buffer passed in
|
|
|
|
func (n *nonce) fromBuf(buf []byte) {
|
|
|
|
read := copy((*n)[:], buf)
|
|
|
|
if read != fileNonceSize {
|
|
|
|
panic("buffer to short to read nonce")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 18:29:57 +08:00
|
|
|
// carry 1 up the nonce from position i
|
|
|
|
func (n *nonce) carry(i int) {
|
|
|
|
for ; i < len(*n); i++ {
|
2016-07-26 02:18:56 +08:00
|
|
|
digit := (*n)[i]
|
|
|
|
newDigit := digit + 1
|
|
|
|
(*n)[i] = newDigit
|
|
|
|
if newDigit >= digit {
|
|
|
|
// exit if no carry
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 18:29:57 +08:00
|
|
|
// increment to add 1 to the nonce
|
|
|
|
func (n *nonce) increment() {
|
|
|
|
n.carry(0)
|
|
|
|
}
|
|
|
|
|
2020-05-25 14:05:53 +08:00
|
|
|
// add a uint64 to the nonce
|
2016-09-10 18:29:57 +08:00
|
|
|
func (n *nonce) add(x uint64) {
|
|
|
|
carry := uint16(0)
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
digit := (*n)[i]
|
|
|
|
xDigit := byte(x)
|
|
|
|
x >>= 8
|
|
|
|
carry += uint16(digit) + uint16(xDigit)
|
|
|
|
(*n)[i] = byte(carry)
|
|
|
|
carry >>= 8
|
|
|
|
}
|
|
|
|
if carry != 0 {
|
|
|
|
n.carry(8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:18:56 +08:00
|
|
|
// encrypter encrypts an io.Reader on the fly
|
|
|
|
type encrypter struct {
|
2016-11-02 06:30:20 +08:00
|
|
|
mu sync.Mutex
|
2016-07-26 02:18:56 +08:00
|
|
|
in io.Reader
|
2020-04-09 23:17:17 +08:00
|
|
|
c *Cipher
|
2016-07-26 02:18:56 +08:00
|
|
|
nonce nonce
|
|
|
|
buf []byte
|
|
|
|
readBuf []byte
|
|
|
|
bufIndex int
|
|
|
|
bufSize int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// newEncrypter creates a new file handle encrypting on the fly
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) newEncrypter(in io.Reader, nonce *nonce) (*encrypter, error) {
|
2016-07-26 02:18:56 +08:00
|
|
|
fh := &encrypter{
|
|
|
|
in: in,
|
|
|
|
c: c,
|
|
|
|
buf: c.getBlock(),
|
|
|
|
readBuf: c.getBlock(),
|
|
|
|
bufSize: fileHeaderSize,
|
|
|
|
}
|
|
|
|
// Initialise nonce
|
2017-02-13 00:30:18 +08:00
|
|
|
if nonce != nil {
|
|
|
|
fh.nonce = *nonce
|
|
|
|
} else {
|
|
|
|
err := fh.nonce.fromReader(c.cryptoRand)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
|
|
|
// Copy magic into buffer
|
|
|
|
copy(fh.buf, fileMagicBytes)
|
|
|
|
// Copy nonce into buffer
|
|
|
|
copy(fh.buf[fileMagicSize:], fh.nonce[:])
|
|
|
|
return fh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read as per io.Reader
|
|
|
|
func (fh *encrypter) Read(p []byte) (n int, err error) {
|
2016-11-02 06:30:20 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
|
2016-07-26 02:18:56 +08:00
|
|
|
if fh.err != nil {
|
|
|
|
return 0, fh.err
|
|
|
|
}
|
|
|
|
if fh.bufIndex >= fh.bufSize {
|
|
|
|
// Read data
|
|
|
|
// FIXME should overlap the reads with a go-routine and 2 buffers?
|
|
|
|
readBuf := fh.readBuf[:blockDataSize]
|
|
|
|
n, err = io.ReadFull(fh.in, readBuf)
|
2017-01-21 00:00:55 +08:00
|
|
|
if n == 0 {
|
|
|
|
// err can't be nil since:
|
|
|
|
// n == len(buf) if and only if err == nil.
|
2016-07-26 02:18:56 +08:00
|
|
|
return fh.finish(err)
|
|
|
|
}
|
2017-01-21 00:00:55 +08:00
|
|
|
// possibly err != nil here, but we will process the
|
|
|
|
// data and the next call to ReadFull will return 0, err
|
2016-07-26 02:18:56 +08:00
|
|
|
// Encrypt the block using the nonce
|
2020-10-10 08:55:53 +08:00
|
|
|
secretbox.Seal(fh.buf[:0], readBuf[:n], fh.nonce.pointer(), &fh.c.dataKey)
|
2016-07-26 02:18:56 +08:00
|
|
|
fh.bufIndex = 0
|
|
|
|
fh.bufSize = blockHeaderSize + n
|
|
|
|
fh.nonce.increment()
|
|
|
|
}
|
|
|
|
n = copy(p, fh.buf[fh.bufIndex:fh.bufSize])
|
|
|
|
fh.bufIndex += n
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// finish sets the final error and tidies up
|
|
|
|
func (fh *encrypter) finish(err error) (int, error) {
|
|
|
|
if fh.err != nil {
|
|
|
|
return 0, fh.err
|
|
|
|
}
|
|
|
|
fh.err = err
|
|
|
|
fh.c.putBlock(fh.buf)
|
2016-11-02 06:30:20 +08:00
|
|
|
fh.buf = nil
|
2016-07-26 02:18:56 +08:00
|
|
|
fh.c.putBlock(fh.readBuf)
|
2016-11-02 06:30:20 +08:00
|
|
|
fh.readBuf = nil
|
2016-07-26 02:18:56 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt data encrypts the data stream
|
2020-04-09 23:43:34 +08:00
|
|
|
func (c *Cipher) encryptData(in io.Reader) (io.Reader, *encrypter, error) {
|
2018-02-01 23:41:58 +08:00
|
|
|
in, wrap := accounting.UnWrap(in) // unwrap the accounting off the Reader
|
2017-02-13 00:30:18 +08:00
|
|
|
out, err := c.newEncrypter(in, nil)
|
2016-07-26 02:18:56 +08:00
|
|
|
if err != nil {
|
2020-04-09 23:43:34 +08:00
|
|
|
return nil, nil, err
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
2020-04-09 23:43:34 +08:00
|
|
|
return wrap(out), out, nil // and wrap the accounting back on
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptData encrypts the data stream
|
|
|
|
func (c *Cipher) EncryptData(in io.Reader) (io.Reader, error) {
|
|
|
|
out, _, err := c.encryptData(in)
|
|
|
|
return out, err
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// decrypter decrypts an io.ReaderCloser on the fly
|
|
|
|
type decrypter struct {
|
2016-11-02 06:30:20 +08:00
|
|
|
mu sync.Mutex
|
2016-10-21 00:47:33 +08:00
|
|
|
rc io.ReadCloser
|
|
|
|
nonce nonce
|
|
|
|
initialNonce nonce
|
2020-04-09 23:17:17 +08:00
|
|
|
c *Cipher
|
2016-10-21 00:47:33 +08:00
|
|
|
buf []byte
|
|
|
|
readBuf []byte
|
|
|
|
bufIndex int
|
|
|
|
bufSize int
|
|
|
|
err error
|
2018-01-23 02:31:59 +08:00
|
|
|
limit int64 // limit of bytes to read, -1 for unlimited
|
|
|
|
open OpenRangeSeek
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// newDecrypter creates a new file handle decrypting on the fly
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) newDecrypter(rc io.ReadCloser) (*decrypter, error) {
|
2016-07-26 02:18:56 +08:00
|
|
|
fh := &decrypter{
|
|
|
|
rc: rc,
|
|
|
|
c: c,
|
|
|
|
buf: c.getBlock(),
|
|
|
|
readBuf: c.getBlock(),
|
2018-01-23 02:31:59 +08:00
|
|
|
limit: -1,
|
2016-07-26 02:18:56 +08:00
|
|
|
}
|
|
|
|
// Read file header (magic + nonce)
|
|
|
|
readBuf := fh.readBuf[:fileHeaderSize]
|
|
|
|
_, err := io.ReadFull(fh.rc, readBuf)
|
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
|
|
// This read from 0..fileHeaderSize-1 bytes
|
|
|
|
return nil, fh.finishAndClose(ErrorEncryptedFileTooShort)
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, fh.finishAndClose(err)
|
|
|
|
}
|
|
|
|
// check the magic
|
|
|
|
if !bytes.Equal(readBuf[:fileMagicSize], fileMagicBytes) {
|
|
|
|
return nil, fh.finishAndClose(ErrorEncryptedBadMagic)
|
|
|
|
}
|
2019-02-08 01:41:17 +08:00
|
|
|
// retrieve the nonce
|
2016-07-26 02:18:56 +08:00
|
|
|
fh.nonce.fromBuf(readBuf[fileMagicSize:])
|
2016-10-21 00:47:33 +08:00
|
|
|
fh.initialNonce = fh.nonce
|
|
|
|
return fh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newDecrypterSeek creates a new file handle decrypting on the fly
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) newDecrypterSeek(ctx context.Context, open OpenRangeSeek, offset, limit int64) (fh *decrypter, err error) {
|
2018-01-23 02:31:59 +08:00
|
|
|
var rc io.ReadCloser
|
|
|
|
doRangeSeek := false
|
|
|
|
setLimit := false
|
2016-10-21 00:47:33 +08:00
|
|
|
// Open initially with no seek
|
2018-01-23 02:31:59 +08:00
|
|
|
if offset == 0 && limit < 0 {
|
|
|
|
// If no offset or limit then open whole file
|
2019-06-17 16:34:30 +08:00
|
|
|
rc, err = open(ctx, 0, -1)
|
2018-01-23 02:31:59 +08:00
|
|
|
} else if offset == 0 {
|
|
|
|
// If no offset open the header + limit worth of the file
|
|
|
|
_, underlyingLimit, _, _ := calculateUnderlying(offset, limit)
|
2019-06-17 16:34:30 +08:00
|
|
|
rc, err = open(ctx, 0, int64(fileHeaderSize)+underlyingLimit)
|
2018-01-23 02:31:59 +08:00
|
|
|
setLimit = true
|
|
|
|
} else {
|
|
|
|
// Otherwise just read the header to start with
|
2019-06-17 16:34:30 +08:00
|
|
|
rc, err = open(ctx, 0, int64(fileHeaderSize))
|
2018-01-23 02:31:59 +08:00
|
|
|
doRangeSeek = true
|
|
|
|
}
|
2016-10-21 00:47:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Open the stream which fills in the nonce
|
|
|
|
fh, err = c.newDecrypter(rc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-23 02:31:59 +08:00
|
|
|
fh.open = open // will be called by fh.RangeSeek
|
|
|
|
if doRangeSeek {
|
2019-06-17 16:34:30 +08:00
|
|
|
_, err = fh.RangeSeek(ctx, offset, io.SeekStart, limit)
|
2016-10-21 00:47:33 +08:00
|
|
|
if err != nil {
|
|
|
|
_ = fh.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 02:31:59 +08:00
|
|
|
if setLimit {
|
|
|
|
fh.limit = limit
|
|
|
|
}
|
2016-07-26 02:18:56 +08:00
|
|
|
return fh, nil
|
|
|
|
}
|
|
|
|
|
2016-11-02 06:30:20 +08:00
|
|
|
// read data into internal buffer - call with fh.mu held
|
|
|
|
func (fh *decrypter) fillBuffer() (err error) {
|
|
|
|
// FIXME should overlap the reads with a go-routine and 2 buffers?
|
|
|
|
readBuf := fh.readBuf
|
|
|
|
n, err := io.ReadFull(fh.rc, readBuf)
|
2017-01-21 00:00:55 +08:00
|
|
|
if n == 0 {
|
|
|
|
// err can't be nil since:
|
|
|
|
// n == len(buf) if and only if err == nil.
|
2016-11-02 06:30:20 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-01-21 00:00:55 +08:00
|
|
|
// possibly err != nil here, but we will process the data and
|
|
|
|
// the next call to ReadFull will return 0, err
|
|
|
|
|
2016-11-02 06:30:20 +08:00
|
|
|
// Check header + 1 byte exists
|
|
|
|
if n <= blockHeaderSize {
|
2017-01-21 00:00:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return err // return pending error as it is likely more accurate
|
|
|
|
}
|
2016-11-02 06:30:20 +08:00
|
|
|
return ErrorEncryptedFileBadHeader
|
|
|
|
}
|
|
|
|
// Decrypt the block using the nonce
|
2020-10-10 08:55:53 +08:00
|
|
|
_, ok := secretbox.Open(fh.buf[:0], readBuf[:n], fh.nonce.pointer(), &fh.c.dataKey)
|
2016-11-02 06:30:20 +08:00
|
|
|
if !ok {
|
2016-12-12 23:20:40 +08:00
|
|
|
if err != nil {
|
2017-01-21 00:00:55 +08:00
|
|
|
return err // return pending error as it is likely more accurate
|
2016-12-12 23:20:40 +08:00
|
|
|
}
|
2016-11-02 06:30:20 +08:00
|
|
|
return ErrorEncryptedBadBlock
|
|
|
|
}
|
|
|
|
fh.bufIndex = 0
|
|
|
|
fh.bufSize = n - blockHeaderSize
|
|
|
|
fh.nonce.increment()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:18:56 +08:00
|
|
|
// Read as per io.Reader
|
|
|
|
func (fh *decrypter) Read(p []byte) (n int, err error) {
|
2016-11-02 06:30:20 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
|
2016-07-26 02:18:56 +08:00
|
|
|
if fh.err != nil {
|
|
|
|
return 0, fh.err
|
|
|
|
}
|
|
|
|
if fh.bufIndex >= fh.bufSize {
|
2016-11-02 06:30:20 +08:00
|
|
|
err = fh.fillBuffer()
|
|
|
|
if err != nil {
|
2016-07-26 02:18:56 +08:00
|
|
|
return 0, fh.finish(err)
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 02:31:59 +08:00
|
|
|
toCopy := fh.bufSize - fh.bufIndex
|
|
|
|
if fh.limit >= 0 && fh.limit < int64(toCopy) {
|
|
|
|
toCopy = int(fh.limit)
|
|
|
|
}
|
|
|
|
n = copy(p, fh.buf[fh.bufIndex:fh.bufIndex+toCopy])
|
2016-07-26 02:18:56 +08:00
|
|
|
fh.bufIndex += n
|
2018-01-23 02:31:59 +08:00
|
|
|
if fh.limit >= 0 {
|
|
|
|
fh.limit -= int64(n)
|
|
|
|
if fh.limit == 0 {
|
|
|
|
return n, fh.finish(io.EOF)
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 02:18:56 +08:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2018-01-23 02:31:59 +08:00
|
|
|
// calculateUnderlying converts an (offset, limit) in a crypted file
|
|
|
|
// into an (underlyingOffset, underlyingLimit) for the underlying
|
|
|
|
// file.
|
|
|
|
//
|
|
|
|
// It also returns number of bytes to discard after reading the first
|
|
|
|
// block and number of blocks this is from the start so the nonce can
|
|
|
|
// be incremented.
|
|
|
|
func calculateUnderlying(offset, limit int64) (underlyingOffset, underlyingLimit, discard, blocks int64) {
|
|
|
|
// blocks we need to seek, plus bytes we need to discard
|
|
|
|
blocks, discard = offset/blockDataSize, offset%blockDataSize
|
|
|
|
|
|
|
|
// Offset in underlying stream we need to seek
|
|
|
|
underlyingOffset = int64(fileHeaderSize) + blocks*(blockHeaderSize+blockDataSize)
|
|
|
|
|
|
|
|
// work out how many blocks we need to read
|
|
|
|
underlyingLimit = int64(-1)
|
|
|
|
if limit >= 0 {
|
|
|
|
// bytes to read beyond the first block
|
|
|
|
bytesToRead := limit - (blockDataSize - discard)
|
|
|
|
|
|
|
|
// Read the first block
|
|
|
|
blocksToRead := int64(1)
|
|
|
|
|
|
|
|
if bytesToRead > 0 {
|
|
|
|
// Blocks that need to be read plus left over blocks
|
|
|
|
extraBlocksToRead, endBytes := bytesToRead/blockDataSize, bytesToRead%blockDataSize
|
|
|
|
if endBytes != 0 {
|
|
|
|
// If left over bytes must read another block
|
|
|
|
extraBlocksToRead++
|
|
|
|
}
|
|
|
|
blocksToRead += extraBlocksToRead
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must read a whole number of blocks
|
|
|
|
underlyingLimit = blocksToRead * (blockHeaderSize + blockDataSize)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RangeSeek behaves like a call to Seek(offset int64, whence
|
|
|
|
// int) with the output wrapped in an io.LimitedReader
|
|
|
|
// limiting the total length to limit.
|
|
|
|
//
|
|
|
|
// RangeSeek with a limit of < 0 is equivalent to a regular Seek.
|
2019-06-17 16:34:30 +08:00
|
|
|
func (fh *decrypter) RangeSeek(ctx context.Context, offset int64, whence int, limit int64) (int64, error) {
|
2016-11-02 06:30:20 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
|
2016-10-21 00:47:33 +08:00
|
|
|
if fh.open == nil {
|
|
|
|
return 0, fh.finish(errors.New("can't seek - not initialised with newDecrypterSeek"))
|
|
|
|
}
|
2018-04-07 02:53:06 +08:00
|
|
|
if whence != io.SeekStart {
|
2016-10-21 00:47:33 +08:00
|
|
|
return 0, fh.finish(errors.New("can only seek from the start"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset error or return it if not EOF
|
|
|
|
if fh.err == io.EOF {
|
2016-11-02 06:30:20 +08:00
|
|
|
fh.unFinish()
|
2016-10-21 00:47:33 +08:00
|
|
|
} else if fh.err != nil {
|
|
|
|
return 0, fh.err
|
|
|
|
}
|
|
|
|
|
2018-01-23 02:31:59 +08:00
|
|
|
underlyingOffset, underlyingLimit, discard, blocks := calculateUnderlying(offset, limit)
|
2016-10-25 22:15:44 +08:00
|
|
|
|
|
|
|
// Move the nonce on the correct number of blocks from the start
|
|
|
|
fh.nonce = fh.initialNonce
|
|
|
|
fh.nonce.add(uint64(blocks))
|
|
|
|
|
|
|
|
// Can we seek underlying stream directly?
|
2018-01-23 02:31:59 +08:00
|
|
|
if do, ok := fh.rc.(fs.RangeSeeker); ok {
|
2016-10-25 22:15:44 +08:00
|
|
|
// Seek underlying stream directly
|
2019-06-17 16:34:30 +08:00
|
|
|
_, err := do.RangeSeek(ctx, underlyingOffset, 0, underlyingLimit)
|
2016-10-21 00:47:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, fh.finish(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if not reopen with seek
|
|
|
|
_ = fh.rc.Close() // close underlying file
|
|
|
|
fh.rc = nil
|
|
|
|
|
|
|
|
// Re-open the underlying object with the offset given
|
2019-06-17 16:34:30 +08:00
|
|
|
rc, err := fh.open(ctx, underlyingOffset, underlyingLimit)
|
2016-10-21 00:47:33 +08:00
|
|
|
if err != nil {
|
2018-01-23 02:31:59 +08:00
|
|
|
return 0, fh.finish(errors.Wrap(err, "couldn't reopen file with offset and limit"))
|
2016-10-21 00:47:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the file handle
|
|
|
|
fh.rc = rc
|
2016-10-25 22:15:44 +08:00
|
|
|
}
|
2016-10-21 00:47:33 +08:00
|
|
|
|
2016-11-02 06:30:20 +08:00
|
|
|
// Fill the buffer
|
|
|
|
err := fh.fillBuffer()
|
2016-10-25 22:15:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, fh.finish(err)
|
2016-10-21 00:47:33 +08:00
|
|
|
}
|
|
|
|
|
2016-11-02 06:30:20 +08:00
|
|
|
// Discard bytes from the buffer
|
|
|
|
if int(discard) > fh.bufSize {
|
|
|
|
return 0, fh.finish(ErrorBadSeek)
|
|
|
|
}
|
|
|
|
fh.bufIndex = int(discard)
|
|
|
|
|
2018-01-23 02:31:59 +08:00
|
|
|
// Set the limit
|
|
|
|
fh.limit = limit
|
|
|
|
|
2016-10-21 00:47:33 +08:00
|
|
|
return offset, nil
|
2016-09-10 18:29:57 +08:00
|
|
|
}
|
|
|
|
|
2018-01-23 02:31:59 +08:00
|
|
|
// Seek implements the io.Seeker interface
|
|
|
|
func (fh *decrypter) Seek(offset int64, whence int) (int64, error) {
|
2019-06-17 16:34:30 +08:00
|
|
|
return fh.RangeSeek(context.TODO(), offset, whence, -1)
|
2018-01-23 02:31:59 +08:00
|
|
|
}
|
|
|
|
|
2016-07-26 02:18:56 +08:00
|
|
|
// finish sets the final error and tidies up
|
|
|
|
func (fh *decrypter) finish(err error) error {
|
|
|
|
if fh.err != nil {
|
|
|
|
return fh.err
|
|
|
|
}
|
|
|
|
fh.err = err
|
|
|
|
fh.c.putBlock(fh.buf)
|
2016-11-02 06:30:20 +08:00
|
|
|
fh.buf = nil
|
2016-07-26 02:18:56 +08:00
|
|
|
fh.c.putBlock(fh.readBuf)
|
2016-11-02 06:30:20 +08:00
|
|
|
fh.readBuf = nil
|
2016-07-26 02:18:56 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-02 06:30:20 +08:00
|
|
|
// unFinish undoes the effects of finish
|
|
|
|
func (fh *decrypter) unFinish() {
|
|
|
|
// Clear error
|
|
|
|
fh.err = nil
|
|
|
|
|
|
|
|
// reinstate the buffers
|
|
|
|
fh.buf = fh.c.getBlock()
|
|
|
|
fh.readBuf = fh.c.getBlock()
|
|
|
|
|
|
|
|
// Empty the buffer
|
|
|
|
fh.bufIndex = 0
|
|
|
|
fh.bufSize = 0
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:18:56 +08:00
|
|
|
// Close
|
|
|
|
func (fh *decrypter) Close() error {
|
2016-11-02 06:30:20 +08:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
|
2016-07-26 02:18:56 +08:00
|
|
|
// Check already closed
|
|
|
|
if fh.err == ErrorFileClosed {
|
|
|
|
return fh.err
|
|
|
|
}
|
|
|
|
// Closed before reading EOF so not finish()ed yet
|
|
|
|
if fh.err == nil {
|
|
|
|
_ = fh.finish(io.EOF)
|
|
|
|
}
|
|
|
|
// Show file now closed
|
|
|
|
fh.err = ErrorFileClosed
|
2016-11-05 18:01:33 +08:00
|
|
|
if fh.rc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-26 02:18:56 +08:00
|
|
|
return fh.rc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// finishAndClose does finish then Close()
|
|
|
|
//
|
|
|
|
// Used when we are returning a nil fh from new
|
|
|
|
func (fh *decrypter) finishAndClose(err error) error {
|
|
|
|
_ = fh.finish(err)
|
|
|
|
_ = fh.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptData decrypts the data stream
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) DecryptData(rc io.ReadCloser) (io.ReadCloser, error) {
|
2016-07-26 02:18:56 +08:00
|
|
|
out, err := c.newDecrypter(rc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2016-10-21 00:47:33 +08:00
|
|
|
// DecryptDataSeek decrypts the data stream from offset
|
|
|
|
//
|
|
|
|
// The open function must return a ReadCloser opened to the offset supplied
|
|
|
|
//
|
|
|
|
// You must use this form of DecryptData if you might want to Seek the file handle
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) DecryptDataSeek(ctx context.Context, open OpenRangeSeek, offset, limit int64) (ReadSeekCloser, error) {
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := c.newDecrypterSeek(ctx, open, offset, limit)
|
2016-10-21 00:47:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:18:56 +08:00
|
|
|
// EncryptedSize calculates the size of the data when encrypted
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) EncryptedSize(size int64) int64 {
|
2016-07-26 02:18:56 +08:00
|
|
|
blocks, residue := size/blockDataSize, size%blockDataSize
|
|
|
|
encryptedSize := int64(fileHeaderSize) + blocks*(blockHeaderSize+blockDataSize)
|
|
|
|
if residue != 0 {
|
|
|
|
encryptedSize += blockHeaderSize + residue
|
|
|
|
}
|
|
|
|
return encryptedSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptedSize calculates the size of the data when decrypted
|
2020-04-09 23:17:17 +08:00
|
|
|
func (c *Cipher) DecryptedSize(size int64) (int64, error) {
|
2016-07-26 02:18:56 +08:00
|
|
|
size -= int64(fileHeaderSize)
|
|
|
|
if size < 0 {
|
|
|
|
return 0, ErrorEncryptedFileTooShort
|
|
|
|
}
|
|
|
|
blocks, residue := size/blockSize, size%blockSize
|
|
|
|
decryptedSize := blocks * blockDataSize
|
|
|
|
if residue != 0 {
|
|
|
|
residue -= blockHeaderSize
|
|
|
|
if residue <= 0 {
|
|
|
|
return 0, ErrorEncryptedFileBadHeader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
decryptedSize += residue
|
|
|
|
return decryptedSize, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check interfaces
|
|
|
|
var (
|
2018-01-23 02:31:59 +08:00
|
|
|
_ io.ReadCloser = (*decrypter)(nil)
|
|
|
|
_ io.Seeker = (*decrypter)(nil)
|
|
|
|
_ fs.RangeSeeker = (*decrypter)(nil)
|
|
|
|
_ io.Reader = (*encrypter)(nil)
|
2016-07-26 02:18:56 +08:00
|
|
|
)
|