Crypto++
ccm.h
1 #ifndef CRYPTOPP_CCM_H
2 #define CRYPTOPP_CCM_H
3 
4 #include "authenc.h"
5 #include "modes.h"
6 
7 NAMESPACE_BEGIN(CryptoPP)
8 
9 //! .
10 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CCM_Base : public AuthenticatedSymmetricCipherBase
11 {
12 public:
13  CCM_Base()
14  : m_digestSize(0), m_L(0) {}
15 
16  // AuthenticatedSymmetricCipher
17  std::string AlgorithmName() const
18  {return GetBlockCipher().AlgorithmName() + std::string("/CCM");}
19  size_t MinKeyLength() const
20  {return GetBlockCipher().MinKeyLength();}
21  size_t MaxKeyLength() const
22  {return GetBlockCipher().MaxKeyLength();}
23  size_t DefaultKeyLength() const
24  {return GetBlockCipher().DefaultKeyLength();}
25  size_t GetValidKeyLength(size_t n) const
26  {return GetBlockCipher().GetValidKeyLength(n);}
27  bool IsValidKeyLength(size_t n) const
28  {return GetBlockCipher().IsValidKeyLength(n);}
29  unsigned int OptimalDataAlignment() const
30  {return GetBlockCipher().OptimalDataAlignment();}
31  IV_Requirement IVRequirement() const
32  {return UNIQUE_IV;}
33  unsigned int IVSize() const
34  {return 8;}
35  unsigned int MinIVLength() const
36  {return 7;}
37  unsigned int MaxIVLength() const
38  {return 13;}
39  unsigned int DigestSize() const
40  {return m_digestSize;}
41  lword MaxHeaderLength() const
42  {return W64LIT(0)-1;}
43  lword MaxMessageLength() const
44  {return m_L<8 ? (W64LIT(1)<<(8*m_L))-1 : W64LIT(0)-1;}
46  {return true;}
47  void UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength);
48 
49 protected:
50  // AuthenticatedSymmetricCipherBase
51  bool AuthenticationIsOnPlaintext() const
52  {return true;}
53  unsigned int AuthenticationBlockSize() const
54  {return GetBlockCipher().BlockSize();}
55  void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params);
56  void Resync(const byte *iv, size_t len);
57  size_t AuthenticateBlocks(const byte *data, size_t len);
58  void AuthenticateLastHeaderBlock();
59  void AuthenticateLastConfidentialBlock();
60  void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
61  SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
62 
63  virtual BlockCipher & AccessBlockCipher() =0;
64  virtual int DefaultDigestSize() const =0;
65 
66  const BlockCipher & GetBlockCipher() const {return const_cast<CCM_Base *>(this)->AccessBlockCipher();};
67  byte *CBC_Buffer() {return m_buffer+REQUIRED_BLOCKSIZE;}
68 
69  enum {REQUIRED_BLOCKSIZE = 16};
70  int m_digestSize, m_L;
71  word64 m_messageLength, m_aadLength;
73 };
74 
75 //! .
76 template <class T_BlockCipher, int T_DefaultDigestSize, bool T_IsEncryption>
77 class CCM_Final : public CCM_Base
78 {
79 public:
80  static std::string StaticAlgorithmName()
81  {return T_BlockCipher::StaticAlgorithmName() + std::string("/CCM");}
83  {return T_IsEncryption;}
84 
85 private:
86  BlockCipher & AccessBlockCipher() {return m_cipher;}
87  int DefaultDigestSize() const {return T_DefaultDigestSize;}
88  typename T_BlockCipher::Encryption m_cipher;
89 };
90 
91 /// <a href="http://www.cryptolounge.org/wiki/CCM">CCM</a>
92 template <class T_BlockCipher, int T_DefaultDigestSize = 16>
94 {
97 };
98 
99 NAMESPACE_END
100 
101 #endif
std::string AlgorithmName() const
returns name of this algorithm, not universally implemented yet
Definition: ccm.h:17
size_t GetValidKeyLength(size_t n) const
returns the smallest valid key length in bytes that is >= min(n, GetMaxKeyLength()) ...
Definition: ccm.h:25
unsigned int MinIVLength() const
returns minimal length of IVs accepted by this object
Definition: ccm.h:35
unsigned int MaxIVLength() const
returns maximal length of IVs accepted by this object
Definition: ccm.h:37
bool IsValidKeyLength(size_t n) const
returns whether n is a valid key length
Definition: ccm.h:27
CCM
Definition: ccm.h:93
Each class derived from this one defines two types, Encryption and Decryption, both of which implemen...
Definition: seckey.h:211
interface for one direction (encryption or decryption) of a block cipher
Definition: cryptlib.h:603
size_t DefaultKeyLength() const
returns default (recommended) key length in bytes */
Definition: ccm.h:23
bool NeedsPrespecifiedDataLengths() const
if this function returns true, SpecifyDataLengths() must be called before attempting to input data ...
Definition: ccm.h:45
interface for one direction (encryption or decryption) of a stream cipher or cipher mode ...
Definition: cryptlib.h:610
size_t MinKeyLength() const
returns smallest valid key length in bytes */
Definition: ccm.h:19
size_t MaxKeyLength() const
returns largest valid key length in bytes */
Definition: ccm.h:21
Definition: ccm.h:10
IV_Requirement IVRequirement() const
returns the minimal requirement for secure IVs
Definition: ccm.h:31
lword MaxHeaderLength() const
the maximum length of AAD that can be input before the encrypted data
Definition: ccm.h:41
lword MaxMessageLength() const
the maximum length of encrypted data
Definition: ccm.h:43
unsigned int OptimalDataAlignment() const
returns how input should be aligned for optimal performance
Definition: ccm.h:29
unsigned int DigestSize() const
size of the hash/digest/MAC returned by Final()
Definition: ccm.h:39
Definition: ccm.h:77
bool IsForwardTransformation() const
returns whether this is an encryption object
Definition: ccm.h:82
interface for retrieving values given their names
Definition: cryptlib.h:224