Fabcoin Core  0.16.2
P2P Digital Currency
authenc.cpp
Go to the documentation of this file.
1 // authenc.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 
5 #ifndef CRYPTOPP_IMPORTS
6 
7 #include "authenc.h"
8 
10 
11 void AuthenticatedSymmetricCipherBase::AuthenticateData(const byte *input, size_t len)
12 {
13  unsigned int blockSize = AuthenticationBlockSize();
14  unsigned int &num = m_bufferedDataLength;
15  byte* data = m_buffer.begin();
16 
17  if (num != 0) // process left over data
18  {
19  if (num+len >= blockSize)
20  {
21  memcpy(data+num, input, blockSize-num);
22  AuthenticateBlocks(data, blockSize);
23  input += (blockSize-num);
24  len -= (blockSize-num);
25  num = 0;
26  // drop through and do the rest
27  }
28  else
29  {
30  memcpy(data+num, input, len);
31  num += (unsigned int)len;
32  return;
33  }
34  }
35 
36  // now process the input data in blocks of blockSize bytes and save the leftovers to m_data
37  if (len >= blockSize)
38  {
39  size_t leftOver = AuthenticateBlocks(input, len);
40  input += (len - leftOver);
41  len = leftOver;
42  }
43 
44  memcpy(data, input, len);
45  num = (unsigned int)len;
46 }
47 
48 void AuthenticatedSymmetricCipherBase::SetKey(const byte *userKey, size_t keylength, const NameValuePairs &params)
49 {
52 
53  SetKeyWithoutResync(userKey, keylength, params);
55 
56  size_t length;
57  const byte *iv = GetIVAndThrowIfInvalid(params, length);
58  if (iv)
59  Resynchronize(iv, (int)length);
60 }
61 
63 {
64  if (m_state < State_KeySet)
65  throw BadState(AlgorithmName(), "Resynchronize", "key is set");
66 
70 
71  Resync(iv, this->ThrowIfInvalidIVLength(length));
73 }
74 
75 void AuthenticatedSymmetricCipherBase::Update(const byte *input, size_t length)
76 {
77  if (length == 0)
78  return;
79 
80  switch (m_state)
81  {
82  case State_Start:
83  case State_KeySet:
84  throw BadState(AlgorithmName(), "Update", "setting key and IV");
85  case State_IVSet:
86  AuthenticateData(input, length);
87  m_totalHeaderLength += length;
88  break;
94  // fall through
95  case State_AuthFooter:
96  AuthenticateData(input, length);
97  m_totalFooterLength += length;
98  break;
99  default:
100  CRYPTOPP_ASSERT(false);
101  }
102 }
103 
104 void AuthenticatedSymmetricCipherBase::ProcessData(byte *outString, const byte *inString, size_t length)
105 {
106  m_totalMessageLength += length;
108  throw InvalidArgument(AlgorithmName() + ": message length exceeds maximum");
109 
110 reswitch:
111  switch (m_state)
112  {
113  case State_Start:
114  case State_KeySet:
115  throw BadState(AlgorithmName(), "ProcessData", "setting key and IV");
116  case State_AuthFooter:
117  throw BadState(AlgorithmName(), "ProcessData was called after footer input has started");
118  case State_IVSet:
122  goto reswitch;
124  AuthenticateData(inString, length);
125  AccessSymmetricCipher().ProcessData(outString, inString, length);
126  break;
128  AccessSymmetricCipher().ProcessData(outString, inString, length);
129  AuthenticateData(outString, length);
130  break;
131  default:
132  CRYPTOPP_ASSERT(false);
133  }
134 }
135 
137 {
139  throw InvalidArgument(AlgorithmName() + ": header length of " + IntToString(m_totalHeaderLength) + " exceeds the maximum of " + IntToString(MaxHeaderLength()));
140 
142  {
143  if (MaxFooterLength() == 0)
144  throw InvalidArgument(AlgorithmName() + ": additional authenticated data (AAD) cannot be input after data to be encrypted or decrypted");
145  else
146  throw InvalidArgument(AlgorithmName() + ": footer length of " + IntToString(m_totalFooterLength) + " exceeds the maximum of " + IntToString(MaxFooterLength()));
147  }
148 
149  switch (m_state)
150  {
151  case State_Start:
152  case State_KeySet:
153  throw BadState(AlgorithmName(), "TruncatedFinal", "setting key and IV");
154 
155  case State_IVSet:
158  // fall through
159 
164  // fall through
165 
166  case State_AuthFooter:
167  AuthenticateLastFooterBlock(mac, macSize);
169  break;
170 
171  default:
172  CRYPTOPP_ASSERT(false);
173  }
174 
176 }
177 
179 
180 #endif
virtual bool AuthenticationIsOnPlaintext() const =0
An invalid argument was detected.
Definition: cryptlib.h:184
uint8_t byte
Definition: Common.h:57
virtual void ProcessData(byte *outString, const byte *inString, size_t length)=0
Encrypt or decrypt an array of bytes.
const byte * GetIVAndThrowIfInvalid(const NameValuePairs &params, size_t &size)
Retrieves and validates the IV.
Definition: cryptlib.cpp:143
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
Definition: authenc.cpp:104
Exception thrown when the object is in the wrong state for the operation.
Definition: cryptlib.h:1129
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
void Resynchronize(const byte *iv, int length=-1)
Resynchronize with an IV.
Definition: authenc.cpp:62
virtual void AuthenticateLastHeaderBlock()=0
virtual void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params)=0
virtual lword MaxFooterLength() const
Provides the the maximum length of AAD.
Definition: cryptlib.h:1144
virtual lword MaxHeaderLength() const =0
Provides the maximum length of AAD that can be input.
size_t ThrowIfInvalidIVLength(int length)
Validates the IV length.
Definition: cryptlib.cpp:131
virtual bool IsForwardTransformation() const =0
Determines if the cipher is being operated in its forward direction.
virtual void AuthenticateLastFooterBlock(byte *mac, size_t macSize)=0
unsigned int m_bufferedDataLength
Definition: authenc.h:59
void SetKey(const byte *userKey, size_t keylength, const NameValuePairs &params)
Sets or reset the key of this object.
Definition: authenc.cpp:48
Base implementation for one direction (encryption or decryption) of a stream cipher or block cipher m...
Definition: authenc.h:19
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
virtual lword MaxMessageLength() const =0
Provides the maximum length of encrypted data.
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: authenc.cpp:75
virtual void AuthenticateLastConfidentialBlock()
Definition: authenc.h:54
void * memcpy(void *a, const void *b, size_t c)
void AuthenticateData(const byte *data, size_t len)
Definition: authenc.cpp:11
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:539
#define NAMESPACE_END
Definition: config.h:201
virtual std::string AlgorithmName() const =0
Provides the name of this algorithm.
void TruncatedFinal(byte *mac, size_t macSize)
Computes the hash of the current message.
Definition: authenc.cpp:136
virtual void Resync(const byte *iv, size_t len)=0
Base classes for working with authenticated encryption modes of encryption.
virtual SymmetricCipher & AccessSymmetricCipher()=0
uint8_t const * data
Definition: sha3.h:19
Interface for retrieving values given their names.
Definition: cryptlib.h:279