Crypto++ 8.7
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
13NAMESPACE_BEGIN(CryptoPP)
14
15/// \brief CCM block cipher base implementation
16/// \details Base implementation of the AuthenticatedSymmetricCipher interface
17/// \since Crypto++ 5.6.0
18class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CCM_Base : public AuthenticatedSymmetricCipherBase
19{
20public:
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();}
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;}
52 {return W64LIT(0)-1;}
54 {return m_L<8 ? (W64LIT(1)<<(8*m_L))-1 : W64LIT(0)-1;}
56 {return true;}
57 void UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength);
58
59protected:
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;
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
90template <class T_BlockCipher, int T_DefaultDigestSize, bool T_IsEncryption>
91class CCM_Final : public CCM_Base
92{
93public:
94 static std::string StaticAlgorithmName()
95 {return T_BlockCipher::StaticAlgorithmName() + std::string("/CCM");}
97 {return T_IsEncryption;}
98
99private:
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
114template <class T_BlockCipher, int T_DefaultDigestSize = 16>
116{
119};
120
121NAMESPACE_END
122
123#endif
Classes for authenticated encryption modes of operation.
Base class for authenticated encryption modes of operation.
Definition: authenc.h:41
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1283
CCM block cipher base implementation.
Definition: ccm.h:19
unsigned int MinIVLength() const
Provides the minimum size of an IV.
Definition: ccm.h:45
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition: ccm.h:41
unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: ccm.h:43
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: ccm.h:27
size_t DefaultKeyLength() const
Returns default key length.
Definition: ccm.h:33
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: ccm.h:25
size_t GetValidKeyLength(size_t keylength) const
Returns a valid key length for the algorithm.
Definition: ccm.h:35
unsigned int MaxIVLength() const
Provides the maximum size of an IV.
Definition: ccm.h:47
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: ccm.h:49
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: ccm.h:39
bool IsValidKeyLength(size_t keylength) const
Returns whether keylength is a valid key length.
Definition: ccm.h:37
size_t MinKeyLength() const
Returns smallest valid key length.
Definition: ccm.h:29
lword MaxHeaderLength() const
Provides the maximum length of AAD that can be input.
Definition: ccm.h:51
lword MaxMessageLength() const
Provides the maximum length of encrypted data.
Definition: ccm.h:53
bool NeedsPrespecifiedDataLengths() const
Determines if data lengths must be specified prior to inputting data.
Definition: ccm.h:55
size_t MaxKeyLength() const
Returns largest valid key length.
Definition: ccm.h:31
CCM block cipher final implementation.
Definition: ccm.h:92
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
Definition: ccm.h:96
Interface for retrieving values given their names.
Definition: cryptlib.h:322
IV_Requirement
Secure IVs requirements as enumerated values.
Definition: cryptlib.h:719
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode.
Definition: cryptlib.h:1291
#define W64LIT(x)
Declare an unsigned word64.
Definition: config_int.h:119
unsigned long long word64
64-bit unsigned datatype
Definition: config_int.h:91
word64 lword
Large word type.
Definition: config_int.h:158
Classes for block cipher modes of operation.
Crypto++ library namespace.
Provides Encryption and Decryption typedefs used by derived classes to implement an authenticated enc...
Definition: seckey.h:426
CCM block cipher mode of operation.
Definition: ccm.h:116