// decryptSegment decrypts a path segment func (c *cipher) decryptSegment(ciphertext string) (string, error) { 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 } paddedPlaintext := eme.Transform(c.block, c.nameTweak[:], rawCiphertext, eme.DirectionDecrypt) plaintext, err := pkcs7.Unpad(nameCipherBlockSize, paddedPlaintext) if err != nil { return "", err } err = checkValidString(plaintext) if err != nil { return "", err } return string(plaintext), err }
// decryptName - decrypt base64-encoded encrypted filename "cipherName". // The used encryption is either CBC or EME, depending on the "EMENames" argument. func (be *CryptFS) decryptName(cipherName string, iv []byte, EMENames bool) (string, error) { // Make sure relative symlinks still work after encryption // by passing these through unchanged if cipherName == "." || cipherName == ".." { return cipherName, nil } 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)) } if EMENames { bin = eme.Transform(be.blockCipher, iv, bin, eme.DirectionDecrypt) } else { cbc := cipher.NewCBCDecrypter(be.blockCipher, iv) cbc.CryptBlocks(bin, bin) } bin, err = be.unPad16(bin) if err != nil { return "", err } plain := string(bin) return plain, err }
// EncryptName encrypts "plainName", returns a base64-encoded "cipherName64". // Used internally by EncryptPathDirIV(). // The encryption is either CBC or EME, depending on "useEME". // // This function is exported because fusefrontend needs access to the full (not hashed) // name if longname is used. Otherwise you should use EncryptPathDirIV() func (n *NameTransform) EncryptName(plainName string, iv []byte) (cipherName64 string) { bin := []byte(plainName) bin = pad16(bin) bin = eme.Transform(n.cryptoCore.BlockCipher, iv, bin, eme.DirectionEncrypt) cipherName64 = n.b64.EncodeToString(bin) return cipherName64 }
// 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. // // This makes for determinstic encryption which is what we want - the // 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 func (c *cipher) encryptSegment(plaintext string) string { if plaintext == "" { return "" } paddedPlaintext := pkcs7.Pad(nameCipherBlockSize, []byte(plaintext)) ciphertext := eme.Transform(c.block, c.nameTweak[:], paddedPlaintext, eme.DirectionEncrypt) return encodeFileName(ciphertext) }
// encryptName - encrypt "plainName", return base64-encoded "cipherName64" // The used encryption is either CBC or EME, depending on the "EMENames" argument. func (be *CryptFS) encryptName(plainName string, iv []byte, EMENames bool) (cipherName64 string) { // Make sure relative symlinks still work after encryption // by passing these trough unchanged if plainName == "." || plainName == ".." { return plainName } bin := []byte(plainName) bin = be.pad16(bin) if EMENames { bin = eme.Transform(be.blockCipher, iv, bin, eme.DirectionEncrypt) } else { cbc := cipher.NewCBCEncrypter(be.blockCipher, iv) cbc.CryptBlocks(bin, bin) } cipherName64 = base64.URLEncoding.EncodeToString(bin) return cipherName64 }
// 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 := n.b64.DecodeString(cipherName) if err != nil { return "", err } if len(bin)%aes.BlockSize != 0 { tlog.Debug.Printf("DecryptName %q: decoded length %d is not a multiple of 16", cipherName, len(bin)) return "", syscall.EINVAL } bin = eme.Transform(n.cryptoCore.BlockCipher, iv, bin, eme.DirectionDecrypt) bin, err = unPad16(bin) if err != nil { 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 "", syscall.EINVAL } plain := string(bin) return plain, err }