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
4 //! \headerfile ccm.h
5 //! \brief CCM block cipher mode of operation
7 #ifndef CRYPTOPP_CCM_H
8 #define CRYPTOPP_CCM_H
10 #include "authenc.h"
11 #include "modes.h"
15 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CCM_Base : public AuthenticatedSymmetricCipherBase
16 {
17 public:
18  CCM_Base()
19  : m_digestSize(0), m_L(0), m_messageLength(0), m_aadLength(0) {}
21  // AuthenticatedSymmetricCipher
22  std::string AlgorithmName() const
23  {return GetBlockCipher().AlgorithmName() + std::string("/CCM");}
24  size_t MinKeyLength() const
25  {return GetBlockCipher().MinKeyLength();}
26  size_t MaxKeyLength() const
27  {return GetBlockCipher().MaxKeyLength();}
28  size_t DefaultKeyLength() const
29  {return GetBlockCipher().DefaultKeyLength();}
30  size_t GetValidKeyLength(size_t n) const
31  {return GetBlockCipher().GetValidKeyLength(n);}
32  bool IsValidKeyLength(size_t n) const
33  {return GetBlockCipher().IsValidKeyLength(n);}
34  unsigned int OptimalDataAlignment() const
35  {return GetBlockCipher().OptimalDataAlignment();}
37  {return UNIQUE_IV;}
38  unsigned int IVSize() const
39  {return 8;}
40  unsigned int MinIVLength() const
41  {return 7;}
42  unsigned int MaxIVLength() const
43  {return 13;}
44  unsigned int DigestSize() const
45  {return m_digestSize;}
46  lword MaxHeaderLength() const
47  {return W64LIT(0)-1;}
48  lword MaxMessageLength() const
49  {return m_L<8 ? (W64LIT(1)<<(8*m_L))-1 : W64LIT(0)-1;}
51  {return true;}
52  void UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength);
54 protected:
55  // AuthenticatedSymmetricCipherBase
56  bool AuthenticationIsOnPlaintext() const
57  {return true;}
58  unsigned int AuthenticationBlockSize() const
59  {return GetBlockCipher().BlockSize();}
60  void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params);
61  void Resync(const byte *iv, size_t len);
62  size_t AuthenticateBlocks(const byte *data, size_t len);
63  void AuthenticateLastHeaderBlock();
64  void AuthenticateLastConfidentialBlock();
65  void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
66  SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
68  virtual BlockCipher & AccessBlockCipher() =0;
69  virtual int DefaultDigestSize() const =0;
71  const BlockCipher & GetBlockCipher() const {return const_cast<CCM_Base *>(this)->AccessBlockCipher();};
72  byte *CBC_Buffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
74  enum {REQUIRED_BLOCKSIZE = 16};
75  int m_digestSize, m_L;
76  word64 m_messageLength, m_aadLength;
78 };
80 template <class T_BlockCipher, int T_DefaultDigestSize, bool T_IsEncryption>
81 class CCM_Final : public CCM_Base
82 {
83 public:
84  static std::string StaticAlgorithmName()
85  {return T_BlockCipher::StaticAlgorithmName() + std::string("/CCM");}
87  {return T_IsEncryption;}
89 private:
90  BlockCipher & AccessBlockCipher() {return m_cipher;}
91  int DefaultDigestSize() const {return T_DefaultDigestSize;}
92  typename T_BlockCipher::Encryption m_cipher;
93 };
95 /// <a href="">CCM</a>
96 //! \brief CCM mode of operation
97 template <class T_BlockCipher, int T_DefaultDigestSize = 16>
99 {
102 };
106 #endif
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: ccm.h:22
size_t GetValidKeyLength(size_t n) const
Definition: ccm.h:30
unsigned int MinIVLength() const
returns minimal length of IVs accepted by this object
Definition: ccm.h:40
Class file for modes of operation.
unsigned int MaxIVLength() const
returns maximal length of IVs accepted by this object
Definition: ccm.h:42
bool IsValidKeyLength(size_t n) const
Returns whether keylength is a valid key length.
Definition: ccm.h:32
Definition: ccm.h:98
Provides Encryption and Decryption typedefs used by derived classes to implement an authenticated enc...
Definition: seckey.h:414
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1001
unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: ccm.h:38
size_t DefaultKeyLength() const
Returns default (recommended) key length in bytes.
Definition: ccm.h:28
bool NeedsPrespecifiedDataLengths() const
if this function returns true, SpecifyDataLengths() must be called before attempting to input data ...
Definition: ccm.h:50
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Definition: cryptlib.h:1008
size_t MinKeyLength() const
Returns smallest valid key length in bytes.
Definition: ccm.h:24
size_t MaxKeyLength() const
Returns largest valid key length in bytes.
Definition: ccm.h:26
Definition: ccm.h:15
IV_Requirement IVRequirement() const
returns the minimal requirement for secure IVs
Definition: ccm.h:36
lword MaxHeaderLength() const
the maximum length of AAD that can be input before the encrypted data
Definition: ccm.h:46
Provides IV requirements as an enumerated value.
Definition: cryptlib.h:549
lword MaxMessageLength() const
the maximum length of encrypted data
Definition: ccm.h:48
Crypto++ library namespace.
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: ccm.h:34
Base classes for working with authenticated encryption modes of encryption.
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: ccm.h:44
Definition: ccm.h:81
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
Definition: ccm.h:86
Interface for retrieving values given their names.
Definition: cryptlib.h:257