Coverage Report

Created: 2024-11-21 07:03

/src/cryptopp/ccm.h
Line
Count
Source (jump to first uncovered line)
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
5
    : m_digestSize(0), m_L(0), m_messageLength(0), m_aadLength(0) {}
23
24
  // AuthenticatedSymmetricCipher
25
  std::string AlgorithmName() const
26
0
    {return GetBlockCipher().AlgorithmName() + std::string("/CCM");}
27
  std::string AlgorithmProvider() const
28
0
    {return GetBlockCipher().AlgorithmProvider();}
29
  size_t MinKeyLength() const
30
0
    {return GetBlockCipher().MinKeyLength();}
31
  size_t MaxKeyLength() const
32
0
    {return GetBlockCipher().MaxKeyLength();}
33
  size_t DefaultKeyLength() const
34
0
    {return GetBlockCipher().DefaultKeyLength();}
35
  size_t GetValidKeyLength(size_t keylength) const
36
0
    {return GetBlockCipher().GetValidKeyLength(keylength);}
37
  bool IsValidKeyLength(size_t keylength) const
38
0
    {return GetBlockCipher().IsValidKeyLength(keylength);}
39
  unsigned int OptimalDataAlignment() const
40
0
    {return GetBlockCipher().OptimalDataAlignment();}
41
  IV_Requirement IVRequirement() const
42
0
    {return UNIQUE_IV;}
43
  unsigned int IVSize() const
44
0
    {return 8;}
45
  unsigned int MinIVLength() const
46
0
    {return 7;}
47
  unsigned int MaxIVLength() const
48
0
    {return 13;}
49
  unsigned int DigestSize() const
50
0
    {return m_digestSize;}
51
  lword MaxHeaderLength() const
52
0
    {return W64LIT(0)-1;}
53
  lword MaxMessageLength() const
54
0
    {return m_L<8 ? (W64LIT(1)<<(8*m_L))-1 : W64LIT(0)-1;}
55
  bool NeedsPrespecifiedDataLengths() const
56
0
    {return true;}
57
  void UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength);
58
59
protected:
60
  // AuthenticatedSymmetricCipherBase
61
  bool AuthenticationIsOnPlaintext() const
62
0
    {return true;}
63
  unsigned int AuthenticationBlockSize() const
64
0
    {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
0
  SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
72
73
  virtual BlockCipher & AccessBlockCipher() =0;
74
  virtual int DefaultDigestSize() const =0;
75
76
0
  const BlockCipher & GetBlockCipher() const {return const_cast<CCM_Base *>(this)->AccessBlockCipher();}
77
0
  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:
94
  static std::string StaticAlgorithmName()
95
    {return T_BlockCipher::StaticAlgorithmName() + std::string("/CCM");}
96
  bool IsForwardTransformation() const
97
0
    {return T_IsEncryption;}
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 4, true>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 6, true>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 8, true>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 10, true>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 12, true>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 14, true>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 16, true>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 4, false>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 6, false>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 8, false>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 10, false>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 12, false>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 14, false>::IsForwardTransformation() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 16, false>::IsForwardTransformation() const
98
99
private:
100
5
  BlockCipher & AccessBlockCipher() {return m_cipher;}
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 4, true>::AccessBlockCipher()
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 6, true>::AccessBlockCipher()
CryptoPP::CCM_Final<CryptoPP::Rijndael, 8, true>::AccessBlockCipher()
Line
Count
Source
100
2
  BlockCipher & AccessBlockCipher() {return m_cipher;}
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 10, true>::AccessBlockCipher()
CryptoPP::CCM_Final<CryptoPP::Rijndael, 12, true>::AccessBlockCipher()
Line
Count
Source
100
1
  BlockCipher & AccessBlockCipher() {return m_cipher;}
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 14, true>::AccessBlockCipher()
CryptoPP::CCM_Final<CryptoPP::Rijndael, 16, true>::AccessBlockCipher()
Line
Count
Source
100
1
  BlockCipher & AccessBlockCipher() {return m_cipher;}
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 4, false>::AccessBlockCipher()
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 6, false>::AccessBlockCipher()
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 8, false>::AccessBlockCipher()
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 10, false>::AccessBlockCipher()
CryptoPP::CCM_Final<CryptoPP::Rijndael, 12, false>::AccessBlockCipher()
Line
Count
Source
100
1
  BlockCipher & AccessBlockCipher() {return m_cipher;}
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 14, false>::AccessBlockCipher()
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 16, false>::AccessBlockCipher()
101
0
  int DefaultDigestSize() const {return T_DefaultDigestSize;}
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 4, true>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 6, true>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 8, true>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 10, true>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 12, true>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 14, true>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 16, true>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 4, false>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 6, false>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 8, false>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 10, false>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 12, false>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 14, false>::DefaultDigestSize() const
Unexecuted instantiation: CryptoPP::CCM_Final<CryptoPP::Rijndael, 16, false>::DefaultDigestSize() const
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