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