2008-01-07 09:09:04 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Author: Valient Gough <vgough@pobox.com>
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
|
|
|
* Copyright (c) 2004, Valient Gough
|
|
|
|
*
|
2012-10-03 07:12:17 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
2008-01-07 09:09:04 +01:00
|
|
|
*
|
2012-10-03 07:12:17 +02:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
2008-01-07 09:09:04 +01:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
2012-10-03 07:12:17 +02:00
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-01-07 09:09:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _NameIO_incl_
|
|
|
|
#define _NameIO_incl_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2013-01-29 04:07:54 +01:00
|
|
|
#include "base/Interface.h"
|
2013-03-05 07:36:32 +01:00
|
|
|
#include "base/shared_ptr.h"
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
namespace encfs {
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
class CipherV1;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
|
|
|
class NameIO
|
|
|
|
{
|
2013-03-05 07:36:32 +01:00
|
|
|
public:
|
|
|
|
typedef shared_ptr<NameIO> (*Constructor)(const Interface &iface,
|
|
|
|
const shared_ptr<CipherV1> &cipher);
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
struct Algorithm
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
std::string description;
|
|
|
|
Interface iface;
|
|
|
|
bool needsStreamMode;
|
|
|
|
};
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
typedef std::list<Algorithm> AlgorithmList;
|
|
|
|
static AlgorithmList GetAlgorithmList( bool includeHidden = false );
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
static shared_ptr<NameIO> New(const Interface &iface,
|
|
|
|
const shared_ptr<CipherV1> &cipher);
|
|
|
|
static shared_ptr<NameIO> New(const std::string &name,
|
|
|
|
const shared_ptr<CipherV1> &cipher);
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
static bool Register( const char *name, const char *description,
|
|
|
|
const Interface &iface, Constructor constructor,
|
|
|
|
bool needsStreamMode,
|
|
|
|
bool hidden = false);
|
2008-01-07 09:09:04 +01:00
|
|
|
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
NameIO();
|
|
|
|
virtual ~NameIO();
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
virtual Interface interface() const =0;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
void setChainedNameIV( bool enable );
|
|
|
|
bool getChainedNameIV() const;
|
|
|
|
void setReverseEncryption( bool enable );
|
|
|
|
bool getReverseEncryption() const;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
std::string encodePath( const char *plaintextPath ) const;
|
|
|
|
std::string decodePath( const char *encodedPath ) const;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
std::string encodePath( const char *plaintextPath, uint64_t *iv ) const;
|
|
|
|
std::string decodePath( const char *encodedPath, uint64_t *iv ) const;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
virtual int maxEncodedNameLen( int plaintextNameLen ) const =0;
|
|
|
|
virtual int maxDecodedNameLen( int encodedNameLen ) const =0;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
std::string encodeName( const char *plaintextName, int length ) const;
|
|
|
|
std::string decodeName( const char *encodedName, int length ) const;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
protected:
|
|
|
|
virtual int encodeName( const char *plaintextName, int length,
|
|
|
|
char *encodedName ) const;
|
|
|
|
virtual int decodeName( const char *encodedName, int length,
|
|
|
|
char *plaintextName ) const;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
virtual int encodeName( const char *plaintextName, int length,
|
|
|
|
uint64_t *iv, char *encodedName ) const =0;
|
|
|
|
virtual int decodeName( const char *encodedName, int length,
|
|
|
|
uint64_t *iv, char *plaintextName ) const =0;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
private:
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
std::string recodePath( const char *path,
|
|
|
|
int (NameIO::*codingLen)(int) const,
|
|
|
|
int (NameIO::*codingFunc)(const char *, int,
|
|
|
|
uint64_t *, char *) const,
|
|
|
|
uint64_t *iv ) const;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
std::string _encodePath( const char *plaintextPath, uint64_t *iv ) const;
|
|
|
|
std::string _decodePath( const char *encodedPath, uint64_t *iv ) const;
|
|
|
|
std::string _encodeName( const char *plaintextName, int length ) const;
|
|
|
|
std::string _decodeName( const char *encodedName, int length ) const;
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
bool chainedNameIV;
|
|
|
|
bool reverseEncryption;
|
2008-01-07 09:09:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-03-05 07:36:32 +01:00
|
|
|
Helper macros for creating temporary buffers with an optimization that
|
|
|
|
below a given size (OptimizedSize) is allocated on the stack, and when a
|
|
|
|
larger size is requested it is allocated on the heap.
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
BUFFER_RESET should be called for the same name as BUFFER_INIT
|
|
|
|
*/
|
2008-01-07 09:09:04 +01:00
|
|
|
#define BUFFER_INIT( Name, OptimizedSize, Size ) \
|
2013-03-05 07:36:32 +01:00
|
|
|
char Name ## _Raw [ OptimizedSize ]; \
|
|
|
|
char *Name = Name ## _Raw; \
|
|
|
|
if( sizeof(Name ## _Raw) < Size ) { \
|
|
|
|
Name = new char[ Size ];\
|
|
|
|
} \
|
|
|
|
memset( Name, 0, Size )
|
2008-01-07 09:09:04 +01:00
|
|
|
|
|
|
|
#define BUFFER_RESET( Name ) \
|
2013-03-05 07:36:32 +01:00
|
|
|
do { \
|
|
|
|
if( Name != Name ## _Raw ) { \
|
|
|
|
delete[] Name; \
|
|
|
|
Name = Name ## _Raw; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
} // namespace encfs
|
2008-01-07 09:09:04 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|