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 
NAMESPACE_BEGIN(CryptoPP)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:
StaticAlgorithmName()94 	static std::string StaticAlgorithmName()
95 		{return T_BlockCipher::StaticAlgorithmName() + std::string("/CCM");}
IsForwardTransformation()96 	bool IsForwardTransformation() const
97 		{return T_IsEncryption;}
98 
99 private:
AccessBlockCipher()100 	BlockCipher & AccessBlockCipher() {return m_cipher;}
DefaultDigestSize()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
124