@ -20,8 +20,8 @@ class EncryptionProvider;
Env * NewEncryptedEnv ( Env * base_env , EncryptionProvider * provider ) ;
// BlockAccessCipherStream is the base class for any cipher stream that
// supports random access at block level (without requiring data from other blocks).
// E.g. CTR (Counter operation mode) supports this requirement.
// supports random access at block level (without requiring data from other
// blocks). E.g. CTR (Counter operation mode) supports this requirement.
class BlockAccessCipherStream {
public :
virtual ~ BlockAccessCipherStream ( ) { } ;
@ -43,11 +43,13 @@ class BlockAccessCipherStream {
// Encrypt a block of data at the given block index.
// Length of data is equal to BlockSize();
virtual Status EncryptBlock ( uint64_t blockIndex , char * data , char * scratch ) = 0 ;
virtual Status EncryptBlock ( uint64_t blockIndex , char * data ,
char * scratch ) = 0 ;
// Decrypt a block of data at the given block index.
// Length of data is equal to BlockSize();
virtual Status DecryptBlock ( uint64_t blockIndex , char * data , char * scratch ) = 0 ;
virtual Status DecryptBlock ( uint64_t blockIndex , char * data ,
char * scratch ) = 0 ;
} ;
// BlockCipher
@ -74,9 +76,9 @@ class BlockCipher {
class ROT13BlockCipher : public BlockCipher {
private :
size_t blockSize_ ;
public :
ROT13BlockCipher ( size_t blockSize )
: blockSize_ ( blockSize ) { }
ROT13BlockCipher ( size_t blockSize ) : blockSize_ ( blockSize ) { }
virtual ~ ROT13BlockCipher ( ) { } ;
// BlockSize returns the size of each block supported by this cipher stream.
@ -102,6 +104,7 @@ class CTRCipherStream final : public BlockAccessCipherStream {
BlockCipher & cipher_ ;
std : : string iv_ ;
uint64_t initialCounter_ ;
public :
CTRCipherStream ( BlockCipher & c , const char * iv , uint64_t initialCounter )
: cipher_ ( c ) , iv_ ( iv , c . BlockSize ( ) ) , initialCounter_ ( initialCounter ) { } ;
@ -116,29 +119,31 @@ class CTRCipherStream final : public BlockAccessCipherStream {
// Encrypt a block of data at the given block index.
// Length of data is equal to BlockSize();
virtual Status EncryptBlock ( uint64_t blockIndex , char * data , char * scratch ) override ;
virtual Status EncryptBlock ( uint64_t blockIndex , char * data ,
char * scratch ) override ;
// Decrypt a block of data at the given block index.
// Length of data is equal to BlockSize();
virtual Status DecryptBlock ( uint64_t blockIndex , char * data , char * scratch ) override ;
virtual Status DecryptBlock ( uint64_t blockIndex , char * data ,
char * scratch ) override ;
} ;
// The encryption provider is used to create a cipher stream for a specific file.
// The returned cipher stream will be used for actual encryption/decryption
// actions.
// The encryption provider is used to create a cipher stream for a specific
// file. The returned cipher stream will be used for actual
// encryption/decryption actions.
class EncryptionProvider {
public :
virtual ~ EncryptionProvider ( ) { } ;
// GetPrefixLength returns the length of the prefix that is added to every file
// and used for storing encryption options.
// For optimal performance, the prefix length should be a multiple of
// the page size.
// GetPrefixLength returns the length of the prefix that is added to every
// file and used for storing encryption options. For optimal performance, the
// prefix length should be a multiple of the page size.
virtual size_t GetPrefixLength ( ) = 0 ;
// CreateNewPrefix initialized an allocated block of prefix memory
// for a new file.
virtual Status CreateNewPrefix ( const std : : string & fname , char * prefix , size_t prefixLength ) = 0 ;
virtual Status CreateNewPrefix ( const std : : string & fname , char * prefix ,
size_t prefixLength ) = 0 ;
// CreateCipherStream creates a block access cipher stream for a file given
// given name and options.
@ -155,23 +160,23 @@ class EncryptionProvider {
class CTREncryptionProvider : public EncryptionProvider {
private :
BlockCipher & cipher_ ;
protected :
const static size_t defaultPrefixLength = 4096 ;
public :
CTREncryptionProvider ( BlockCipher & c )
: cipher_ ( c ) { } ;
CTREncryptionProvider ( BlockCipher & c ) : cipher_ ( c ) { } ;
virtual ~ CTREncryptionProvider ( ) { }
// GetPrefixLength returns the length of the prefix that is added to every file
// and used for storing encryption options.
// For optimal performance, the prefix length should be a multiple of
// the page size.
// GetPrefixLength returns the length of the prefix that is added to every
// file and used for storing encryption options. For optimal performance, the
// prefix length should be a multiple of the page size.
virtual size_t GetPrefixLength ( ) override ;
// CreateNewPrefix initialized an allocated block of prefix memory
// for a new file.
virtual Status CreateNewPrefix ( const std : : string & fname , char * prefix , size_t prefixLength ) override ;
virtual Status CreateNewPrefix ( const std : : string & fname , char * prefix ,
size_t prefixLength ) override ;
// CreateCipherStream creates a block access cipher stream for a file given
// given name and options.
@ -185,10 +190,11 @@ class CTREncryptionProvider : public EncryptionProvider {
// It will be encrypted later (before written to disk).
// Returns the amount of space (starting from the start of the prefix)
// that has been initialized.
virtual size_t PopulateSecretPrefixPart ( char * prefix , size_t prefixLength , size_t blockSize ) ;
virtual size_t PopulateSecretPrefixPart ( char * prefix , size_t prefixLength ,
size_t blockSize ) ;
// CreateCipherStreamFromPrefix creates a block access cipher stream for a file given
// given name and options. The given prefix is already decrypted.
// CreateCipherStreamFromPrefix creates a block access cipher stream for a
// file given given name and options. The given prefix is already decrypted.
virtual Status CreateCipherStreamFromPrefix (
const std : : string & fname , const EnvOptions & options ,
uint64_t initialCounter , const Slice & iv , const Slice & prefix ,