Crypto++ 8.2 Free C++ class library of cryptographic schemes

ccm.h
Go to the documentation of this file.
1 // ccm.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file ccm.h
4 /// \brief CCM block cipher mode of operation
5 /// \since Crypto++ 5.6.0
6
7 #ifndef CRYPTOPP_CCM_H
8 #define CRYPTOPP_CCM_H
9
10 #include "authenc.h"
11 #include "modes.h"
12
13 NAMESPACE_BEGIN(CryptoPP)
14
15 /// \brief CCM block cipher base implementation
16 /// \details Base implementation of the AuthenticatedSymmetricCipher interface
17 /// \since Crypto++ 5.6.0
18 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CCM_Base : public AuthenticatedSymmetricCipherBase
19 {
20 public:
21 CCM_Base()
22 : m_digestSize(0), m_L(0), m_messageLength(0), m_aadLength(0) {}
23
24 // AuthenticatedSymmetricCipher
25 std::string AlgorithmName() const
26 {return GetBlockCipher().AlgorithmName() + std::string("/CCM");}
27 std::string AlgorithmProvider() const
28 {return GetBlockCipher().AlgorithmProvider();}
29 size_t MinKeyLength() const
30 {return GetBlockCipher().MinKeyLength();}
31 size_t MaxKeyLength() const
32 {return GetBlockCipher().MaxKeyLength();}
33 size_t DefaultKeyLength() const
34 {return GetBlockCipher().DefaultKeyLength();}
35 size_t GetValidKeyLength(size_t keylength) const
36 {return GetBlockCipher().GetValidKeyLength(keylength);}
37 bool IsValidKeyLength(size_t keylength) const
38 {return GetBlockCipher().IsValidKeyLength(keylength);}
39 unsigned int OptimalDataAlignment() const
40 {return GetBlockCipher().OptimalDataAlignment();}
41 IV_Requirement IVRequirement() const
42 {return UNIQUE_IV;}
43 unsigned int IVSize() const
44 {return 8;}
45 unsigned int MinIVLength() const
46 {return 7;}
47 unsigned int MaxIVLength() const
48 {return 13;}
49 unsigned int DigestSize() const
50 {return m_digestSize;}
51 lword MaxHeaderLength() const
52 {return W64LIT(0)-1;}
53 lword MaxMessageLength() const
54 {return m_L<8 ? (W64LIT(1)<<(8*m_L))-1 : W64LIT(0)-1;}
55 bool NeedsPrespecifiedDataLengths() const
56 {return true;}
57 void UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength);
58
59 protected:
60 // AuthenticatedSymmetricCipherBase
61 bool AuthenticationIsOnPlaintext() const
62 {return true;}
63 unsigned int AuthenticationBlockSize() const
64 {return GetBlockCipher().BlockSize();}
65 void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params);
66 void Resync(const byte *iv, size_t len);
67 size_t AuthenticateBlocks(const byte *data, size_t len);
68 void AuthenticateLastHeaderBlock();
69 void AuthenticateLastConfidentialBlock();
70 void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
71 SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
72
73 virtual BlockCipher & AccessBlockCipher() =0;
74 virtual int DefaultDigestSize() const =0;
75
76 const BlockCipher & GetBlockCipher() const {return const_cast<CCM_Base *>(this)->AccessBlockCipher();}
77 byte *CBC_Buffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
78
79 enum {REQUIRED_BLOCKSIZE = 16};
80 int m_digestSize, m_L;
81 word64 m_messageLength, m_aadLength;
82 CTR_Mode_ExternalCipher::Encryption m_ctr;
83 };
84
85 /// \brief CCM block cipher final implementation
86 /// \tparam T_BlockCipher block cipher
87 /// \tparam T_DefaultDigestSize default digest size, in bytes
88 /// \tparam T_IsEncryption direction in which to operate the cipher
89 /// \since Crypto++ 5.6.0
90 template <class T_BlockCipher, int T_DefaultDigestSize, bool T_IsEncryption>
91 class CCM_Final : public CCM_Base
92 {
93 public:
94 static std::string StaticAlgorithmName()
95 {return T_BlockCipher::StaticAlgorithmName() + std::string("/CCM");}
96 bool IsForwardTransformation() const
97 {return T_IsEncryption;}
98
99 private:
100 BlockCipher & AccessBlockCipher() {return m_cipher;}
101 int DefaultDigestSize() const {return T_DefaultDigestSize;}
102 typename T_BlockCipher::Encryption m_cipher;
103 };
104
105 /// \brief CCM block cipher mode of operation
106 /// \tparam T_BlockCipher block cipher
107 /// \tparam T_DefaultDigestSize default digest size, in bytes
108 /// \details \p CCM provides the \p Encryption and \p Decryption typedef. See GCM_Base
109 /// and GCM_Final for the AuthenticatedSymmetricCipher implementation.
110 /// \sa <a href="http://www.cryptopp.com/wiki/CCM_Mode">CCM Mode</a> and
111 /// <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
112 /// on the Crypto++ wiki.
113 /// \since Crypto++ 5.6.0
114 template <class T_BlockCipher, int T_DefaultDigestSize = 16>
115 struct CCM : public AuthenticatedSymmetricCipherDocumentation
116 {
117 typedef CCM_Final<T_BlockCipher, T_DefaultDigestSize, true> Encryption;
118 typedef CCM_Final<T_BlockCipher, T_DefaultDigestSize, false> Decryption;
119 };
120
121 NAMESPACE_END
122
123 #endif
CipherModeFinalTemplate_ExternalCipher< ConcretePolicyHolder< Empty, AdditiveCipherTemplate< AbstractPolicyHolder< AdditiveCipherAbstractPolicy, CTR_ModePolicy > > > >
CCM_Base::AlgorithmProvider
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: ccm.h:27
CCM_Base::MaxHeaderLength
lword MaxHeaderLength() const
Provides the maximum length of AAD that can be input.
Definition: ccm.h:51
CCM_Base::MinKeyLength
size_t MinKeyLength() const
Returns smallest valid key length.
Definition: ccm.h:29
modes.h
Classes for block cipher modes of operation.
CCM_Base::NeedsPrespecifiedDataLengths
bool NeedsPrespecifiedDataLengths() const
Determines if data lengths must be specified prior to inputting data.
Definition: ccm.h:55
CCM
CCM block cipher mode of operation.
Definition: ccm.h:115
AuthenticatedSymmetricCipherDocumentation
Provides Encryption and Decryption typedefs used by derived classes to implement an authenticated enc...
Definition: seckey.h:425
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1278
CCM_Base::MaxMessageLength
lword MaxMessageLength() const
Provides the maximum length of encrypted data.
Definition: ccm.h:53
CCM_Base::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: ccm.h:49
CCM_Base::DefaultKeyLength
size_t DefaultKeyLength() const
Returns default key length.
Definition: ccm.h:33
CCM_Base::IsValidKeyLength
bool IsValidKeyLength(size_t keylength) const
Returns whether keylength is a valid key length.
Definition: ccm.h:37
CCM_Base::OptimalDataAlignment
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: ccm.h:39
SymmetricCipher
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Definition: cryptlib.h:1286
AuthenticatedSymmetricCipherBase
Base class for authenticated encryption modes of operation.
Definition: authenc.h:40
CCM_Base::IVSize
unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: ccm.h:43
CCM_Base
CCM block cipher base implementation.
Definition: ccm.h:18
CCM_Base::MinIVLength
unsigned int MinIVLength() const
Provides the minimum size of an IV.
Definition: ccm.h:45
CCM_Base::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: ccm.h:25
SimpleKeyingInterface::IV_Requirement
IV_Requirement
Secure IVs requirements as enumerated values.
Definition: cryptlib.h:719
CCM_Base::MaxKeyLength
size_t MaxKeyLength() const
Returns largest valid key length.
Definition: ccm.h:31
CCM_Base::GetValidKeyLength
size_t GetValidKeyLength(size_t keylength) const
Returns a valid key length for the algorithm.
Definition: ccm.h:35
CCM_Final::IsForwardTransformation
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
Definition: ccm.h:96
CryptoPP
Crypto++ library namespace.
CCM_Base::MaxIVLength
unsigned int MaxIVLength() const
Provides the maximum size of an IV.
Definition: ccm.h:47
CCM_Base::IVRequirement
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition: ccm.h:41
authenc.h
Classes for authenticated encryption modes of operation.
CCM_Final
CCM block cipher final implementation.
Definition: ccm.h:91
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:321