Security Scol plugin
xts.h
Go to the documentation of this file.
1// xts.h - written and placed in the public domain by Jeffrey Walton
2
21
22#ifndef CRYPTOPP_XTS_MODE_H
23#define CRYPTOPP_XTS_MODE_H
24
25#include "cryptlib.h"
26#include "secblock.h"
27#include "modes.h"
28#include "misc.h"
29
41#ifndef CRYPTOPP_XTS_WIDE_BLOCK_CIPHERS
42# define CRYPTOPP_XTS_WIDE_BLOCK_CIPHERS 0
43#endif // CRYPTOPP_XTS_WIDE_BLOCK_CIPHERS
44
45NAMESPACE_BEGIN(CryptoPP)
46
47
49class CRYPTOPP_NO_VTABLE XTS_ModeBase : public BlockOrientedCipherModeBase
50{
51public:
56 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName()
57 {return "XTS";}
58
59 virtual ~XTS_ModeBase() {}
60
61 std::string AlgorithmName() const
62 {return GetBlockCipher().AlgorithmName() + "/XTS";}
63 std::string AlgorithmProvider() const
64 {return GetBlockCipher().AlgorithmProvider();}
65
66 size_t MinKeyLength() const
67 {return GetBlockCipher().MinKeyLength()*2;}
68 size_t MaxKeyLength() const
69 {return GetBlockCipher().MaxKeyLength()*2;}
70 size_t DefaultKeyLength() const
71 {return GetBlockCipher().DefaultKeyLength()*2;}
72 size_t GetValidKeyLength(size_t n) const
73 {return 2*GetBlockCipher().GetValidKeyLength((n+1)/2);}
74 bool IsValidKeyLength(size_t keylength) const
75 {return keylength == GetValidKeyLength(keylength);}
76
80 void ThrowIfInvalidKeyLength(size_t length);
81
84 unsigned int BlockSize() const
85 {return GetBlockCipher().BlockSize();}
86
93 unsigned int GetOptimalBlockSize() const
94 {return GetBlockCipher().BlockSize()*ParallelBlocks;}
95 unsigned int MinLastBlockSize() const
96 {return GetBlockCipher().BlockSize()+1;}
97 unsigned int OptimalDataAlignment() const
98 {return GetBlockCipher().OptimalDataAlignment();}
99
107 void ThrowIfInvalidBlockSize(size_t length);
108
109 void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs);
110 IV_Requirement IVRequirement() const {return UNIQUE_IV;}
111 void Resynchronize(const byte *iv, int ivLength=-1);
112 void ProcessData(byte *outString, const byte *inString, size_t length);
113 size_t ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength);
114
120 void Resynchronize(word64 sector, ByteOrder order=BIG_ENDIAN_ORDER);
121
122protected:
123 virtual void ResizeBuffers();
124
125 inline size_t ProcessLastPlainBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength);
126 inline size_t ProcessLastCipherBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength);
127
128 virtual BlockCipher& AccessBlockCipher() = 0;
129 virtual BlockCipher& AccessTweakCipher() = 0;
130
131 const BlockCipher& GetBlockCipher() const
132 {return const_cast<XTS_ModeBase*>(this)->AccessBlockCipher();}
133 const BlockCipher& GetTweakCipher() const
134 {return const_cast<XTS_ModeBase*>(this)->AccessTweakCipher();}
135
136 // Buffers are sized based on ParallelBlocks
137 AlignedSecByteBlock m_xregister;
138 AlignedSecByteBlock m_xworkspace;
139
140 // Intel lacks the SSE registers to run 8 or 12 parallel blocks.
141 // Do not change this value after compiling. It has no effect.
142#if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86
143 enum {ParallelBlocks = 4};
144#else
145 enum {ParallelBlocks = 12};
146#endif
147};
148
174template <class CIPHER>
175class CRYPTOPP_NO_VTABLE XTS_Final : public XTS_ModeBase
176{
177protected:
178 BlockCipher& AccessBlockCipher()
179 {return *m_cipher;}
180 BlockCipher& AccessTweakCipher()
181 {return m_tweaker;}
182
183protected:
184 typename CIPHER::Encryption m_tweaker;
185};
186
212template <class CIPHER>
218
219// C++03 lacks the mechanics to typedef a template
220#define XTS_Mode XTS
221
222NAMESPACE_END
223
224#endif // CRYPTOPP_XTS_MODE_H
Interface for one direction (encryption or decryption) of a block cipher.
Definition cryptlib.h:1283
Block cipher mode of operation default implementation.
Definition modes.h:251
Block cipher mode of operation aggregate.
Definition modes.h:347
Interface for retrieving values given their names.
Definition cryptlib.h:322
IV_Requirement
Secure IVs requirements as enumerated values.
Definition cryptlib.h:719
XTS block cipher mode of operation implementation.
Definition xts.h:176
XTS block cipher mode of operation default implementation.
Definition xts.h:50
unsigned int MinLastBlockSize() const
Provides the size of the last block.
Definition xts.h:95
size_t DefaultKeyLength() const
Returns default key length.
Definition xts.h:70
bool IsValidKeyLength(size_t keylength) const
Returns whether keylength is a valid key length.
Definition xts.h:74
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition xts.h:63
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition xts.h:110
CRYPTOPP_STATIC_CONSTEXPR const char * StaticAlgorithmName()
The algorithm name.
Definition xts.h:56
size_t GetValidKeyLength(size_t n) const
Returns a valid key length for the algorithm.
Definition xts.h:72
size_t MaxKeyLength() const
Returns largest valid key length.
Definition xts.h:68
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition xts.h:97
unsigned int BlockSize() const
Definition xts.h:84
unsigned int GetOptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Definition xts.h:93
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition xts.h:61
size_t MinKeyLength() const
Returns smallest valid key length.
Definition xts.h:66
Abstract base classes that provide a uniform interface to this library.
ByteOrder
Provides the byte ordering.
Definition cryptlib.h:143
@ BIG_ENDIAN_ORDER
byte order is big-endian
Definition cryptlib.h:147
Utility functions for the Crypto++ library.
Classes for block cipher modes of operation.
Classes and functions for secure memory allocations.
Block cipher mode of operation information.
Definition modes.h:45
XTS block cipher mode of operation.
Definition xts.h:214