Crypto++  5.6.3
Free C++ class library of cryptographic schemes
Go to the documentation of this file.
1 // ccm.h - written and placed in the public domain by Wei Dai
3 //! \file ccm.h
4 //! \brief CCM block cipher mode of operation
6 #ifndef CRYPTOPP_CCM_H
7 #define CRYPTOPP_CCM_H
9 #include "authenc.h"
10 #include "modes.h"
14 //! \class CCM_Base
15 //! \brief CCM block cipher base implementation
16 //! \details Base implementation of the AuthenticatedSymmetricCipher interface
17 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CCM_Base : public AuthenticatedSymmetricCipherBase
18 {
19 public:
20  CCM_Base()
21  : m_digestSize(0), m_L(0), m_messageLength(0), m_aadLength(0) {}
23  // AuthenticatedSymmetricCipher
24  std::string AlgorithmName() const
25  {return GetBlockCipher().AlgorithmName() + std::string("/CCM");}
26  size_t MinKeyLength() const
27  {return GetBlockCipher().MinKeyLength();}
28  size_t MaxKeyLength() const
29  {return GetBlockCipher().MaxKeyLength();}
30  size_t DefaultKeyLength() const
31  {return GetBlockCipher().DefaultKeyLength();}
32  size_t GetValidKeyLength(size_t n) const
33  {return GetBlockCipher().GetValidKeyLength(n);}
34  bool IsValidKeyLength(size_t n) const
35  {return GetBlockCipher().IsValidKeyLength(n);}
36  unsigned int OptimalDataAlignment() const
37  {return GetBlockCipher().OptimalDataAlignment();}
39  {return UNIQUE_IV;}
40  unsigned int IVSize() const
41  {return 8;}
42  unsigned int MinIVLength() const
43  {return 7;}
44  unsigned int MaxIVLength() const
45  {return 13;}
46  unsigned int DigestSize() const
47  {return m_digestSize;}
48  lword MaxHeaderLength() const
49  {return W64LIT(0)-1;}
50  lword MaxMessageLength() const
51  {return m_L<8 ? (W64LIT(1)<<(8*m_L))-1 : W64LIT(0)-1;}
53  {return true;}
54  void UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength);
56 protected:
57  // AuthenticatedSymmetricCipherBase
58  bool AuthenticationIsOnPlaintext() const
59  {return true;}
60  unsigned int AuthenticationBlockSize() const
61  {return GetBlockCipher().BlockSize();}
62  void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params);
63  void Resync(const byte *iv, size_t len);
64  size_t AuthenticateBlocks(const byte *data, size_t len);
65  void AuthenticateLastHeaderBlock();
66  void AuthenticateLastConfidentialBlock();
67  void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
68  SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
70  virtual BlockCipher & AccessBlockCipher() =0;
71  virtual int DefaultDigestSize() const =0;
73  const BlockCipher & GetBlockCipher() const {return const_cast<CCM_Base *>(this)->AccessBlockCipher();};
74  byte *CBC_Buffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
76  enum {REQUIRED_BLOCKSIZE = 16};
77  int m_digestSize, m_L;
78  word64 m_messageLength, m_aadLength;
80 };
82 //! \class CCM_Final
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 template <class T_BlockCipher, int T_DefaultDigestSize, bool T_IsEncryption>
88 class CCM_Final : public CCM_Base
89 {
90 public:
91  static std::string StaticAlgorithmName()
92  {return T_BlockCipher::StaticAlgorithmName() + std::string("/CCM");}
94  {return T_IsEncryption;}
96 private:
97  BlockCipher & AccessBlockCipher() {return m_cipher;}
98  int DefaultDigestSize() const {return T_DefaultDigestSize;}
99  typename T_BlockCipher::Encryption m_cipher;
100 };
102 //! \class CCM
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="">CCM</a> at the Crypto Lounge
109 template <class T_BlockCipher, int T_DefaultDigestSize = 16>
111 {
114 };
118 #endif
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: ccm.h:24
size_t GetValidKeyLength(size_t n) const
Definition: ccm.h:32
unsigned int MinIVLength() const
Provides the minimum size of an IV.
Definition: ccm.h:42
Class file for modes of operation.
unsigned int MaxIVLength() const
Provides the maximum size of an IV.
Definition: ccm.h:44
bool IsValidKeyLength(size_t n) const
Returns whether keylength is a valid key length.
Definition: ccm.h:34
CCM block cipher mode of operation.
Definition: ccm.h:110
Provides Encryption and Decryption typedefs used by derived classes to implement an authenticated enc...
Definition: seckey.h:425
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1091
unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: ccm.h:40
size_t DefaultKeyLength() const
Returns default (recommended) key length in bytes.
Definition: ccm.h:30
bool NeedsPrespecifiedDataLengths() const
Determines if data lengths must be specified prior to inputting data.
Definition: ccm.h:52
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Definition: cryptlib.h:1099
Base implementation for one direction (encryption or decryption) of a stream cipher or block cipher m...
Definition: authenc.h:17
size_t MinKeyLength() const
Returns smallest valid key length in bytes.
Definition: ccm.h:26
size_t MaxKeyLength() const
Returns largest valid key length in bytes.
Definition: ccm.h:28
CCM block cipher base implementation.
Definition: ccm.h:17
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition: ccm.h:38
lword MaxHeaderLength() const
Provides the maximum length of AAD that can be input.
Definition: ccm.h:48
Secure IVs requirements as enumerated values.
Definition: cryptlib.h:590
lword MaxMessageLength() const
Provides the maximum length of encrypted data.
Definition: ccm.h:50
Crypto++ library namespace.
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: ccm.h:36
Base classes for working with authenticated encryption modes of encryption.
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: ccm.h:46
CCM block cipher final implementation.
Definition: ccm.h:88
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
Definition: ccm.h:93
Interface for retrieving values given their names.
Definition: cryptlib.h:277