Coverage Report

Created: 2024-11-21 07:03

/src/cryptopp/strciphr.cpp
Line
Count
Source (jump to first uncovered line)
1
// strciphr.cpp - originally written and placed in the public domain by Wei Dai.
2
3
#include "pch.h"
4
5
#ifndef CRYPTOPP_IMPORTS
6
7
#include "strciphr.h"
8
9
// Squash MS LNK4221 and libtool warnings
10
#ifndef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
11
extern const char STRCIPHER_FNAME[] = __FILE__;
12
#endif
13
14
NAMESPACE_BEGIN(CryptoPP)
15
16
template <class S>
17
void AdditiveCipherTemplate<S>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
18
166
{
19
166
  PolicyInterface &policy = this->AccessPolicy();
20
166
  policy.CipherSetKey(params, key, length);
21
166
  m_leftOver = 0;
22
166
  unsigned int bufferByteSize = policy.CanOperateKeystream() ? GetBufferByteSize(policy) : RoundUpToMultipleOf(1024U, GetBufferByteSize(policy));
23
166
  m_buffer.New(bufferByteSize);
24
25
166
  if (this->IsResynchronizable())
26
166
  {
27
166
    size_t ivLength;
28
166
    const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
29
166
    policy.CipherResynchronize(m_buffer, iv, ivLength);
30
166
  }
31
166
}
CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::SymmetricCipher> >::UncheckedSetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&)
Line
Count
Source
18
72
{
19
72
  PolicyInterface &policy = this->AccessPolicy();
20
72
  policy.CipherSetKey(params, key, length);
21
72
  m_leftOver = 0;
22
72
  unsigned int bufferByteSize = policy.CanOperateKeystream() ? GetBufferByteSize(policy) : RoundUpToMultipleOf(1024U, GetBufferByteSize(policy));
23
72
  m_buffer.New(bufferByteSize);
24
25
72
  if (this->IsResynchronizable())
26
72
  {
27
72
    size_t ivLength;
28
72
    const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
29
72
    policy.CipherResynchronize(m_buffer, iv, ivLength);
30
72
  }
31
72
}
CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::OFB_ModePolicy> >::UncheckedSetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&)
Line
Count
Source
18
41
{
19
41
  PolicyInterface &policy = this->AccessPolicy();
20
41
  policy.CipherSetKey(params, key, length);
21
41
  m_leftOver = 0;
22
41
  unsigned int bufferByteSize = policy.CanOperateKeystream() ? GetBufferByteSize(policy) : RoundUpToMultipleOf(1024U, GetBufferByteSize(policy));
23
41
  m_buffer.New(bufferByteSize);
24
25
41
  if (this->IsResynchronizable())
26
41
  {
27
41
    size_t ivLength;
28
41
    const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
29
41
    policy.CipherResynchronize(m_buffer, iv, ivLength);
30
41
  }
31
41
}
CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::CTR_ModePolicy> >::UncheckedSetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&)
Line
Count
Source
18
53
{
19
53
  PolicyInterface &policy = this->AccessPolicy();
20
53
  policy.CipherSetKey(params, key, length);
21
53
  m_leftOver = 0;
22
53
  unsigned int bufferByteSize = policy.CanOperateKeystream() ? GetBufferByteSize(policy) : RoundUpToMultipleOf(1024U, GetBufferByteSize(policy));
23
53
  m_buffer.New(bufferByteSize);
24
25
53
  if (this->IsResynchronizable())
26
53
  {
27
53
    size_t ivLength;
28
53
    const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
29
53
    policy.CipherResynchronize(m_buffer, iv, ivLength);
30
53
  }
31
53
}
32
33
template <class S>
34
void AdditiveCipherTemplate<S>::GenerateBlock(byte *outString, size_t length)
35
0
{
36
0
  if (m_leftOver > 0)
37
0
  {
38
0
    const size_t len = STDMIN(m_leftOver, length);
39
0
    std::memcpy(outString, PtrSub(KeystreamBufferEnd(), m_leftOver), len);
40
41
0
    length -= len; m_leftOver -= len;
42
0
    outString = PtrAdd(outString, len);
43
0
    if (!length) {return;}
44
0
  }
45
46
0
  PolicyInterface &policy = this->AccessPolicy();
47
0
  size_t bytesPerIteration = policy.GetBytesPerIteration();
48
49
0
  if (length >= bytesPerIteration)
50
0
  {
51
0
    const size_t iterations = length / bytesPerIteration;
52
0
    policy.WriteKeystream(outString, iterations);
53
0
    length -= iterations * bytesPerIteration;
54
0
    outString = PtrAdd(outString, iterations * bytesPerIteration);
55
0
  }
56
57
0
  if (length > 0)
58
0
  {
59
0
    size_t bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
60
0
    size_t bufferIterations = bufferByteSize / bytesPerIteration;
61
62
0
    policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
63
0
    std::memcpy(outString, PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
64
0
    m_leftOver = bufferByteSize - length;
65
0
  }
66
0
}
Unexecuted instantiation: CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::SymmetricCipher> >::GenerateBlock(unsigned char*, unsigned long)
Unexecuted instantiation: CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::OFB_ModePolicy> >::GenerateBlock(unsigned char*, unsigned long)
Unexecuted instantiation: CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::CTR_ModePolicy> >::GenerateBlock(unsigned char*, unsigned long)
67
68
template <class S>
69
void AdditiveCipherTemplate<S>::ProcessData(byte *outString, const byte *inString, size_t length)
70
342
{
71
342
  CRYPTOPP_ASSERT(outString); CRYPTOPP_ASSERT(inString);
72
342
  CRYPTOPP_ASSERT(length % this->MandatoryBlockSize() == 0);
73
74
342
  PolicyInterface &policy = this->AccessPolicy();
75
342
  size_t bytesPerIteration = policy.GetBytesPerIteration();
76
77
342
  if (m_leftOver > 0)
78
95
  {
79
95
    const size_t len = STDMIN(m_leftOver, length);
80
95
    xorbuf(outString, inString, PtrSub(KeystreamBufferEnd(), m_leftOver), len);
81
82
95
    inString = PtrAdd(inString, len);
83
95
    outString = PtrAdd(outString, len);
84
95
    length -= len; m_leftOver -= len;
85
95
  }
86
87
342
  if (!length) { return; }
88
89
258
  const word32 alignment = policy.GetAlignment();
90
258
  const bool inAligned = IsAlignedOn(inString, alignment);
91
258
  const bool outAligned = IsAlignedOn(outString, alignment);
92
258
  CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
93
94
258
  if (policy.CanOperateKeystream() && length >= bytesPerIteration)
95
111
  {
96
111
    const size_t iterations = length / bytesPerIteration;
97
111
    KeystreamOperationFlags flags = static_cast<KeystreamOperationFlags>(
98
111
      (inAligned ? EnumToInt(INPUT_ALIGNED) : 0) | (outAligned ? EnumToInt(OUTPUT_ALIGNED) : 0));
99
111
    KeystreamOperation operation = KeystreamOperation(flags);
100
111
    policy.OperateKeystream(operation, outString, inString, iterations);
101
102
111
    inString = PtrAdd(inString, iterations * bytesPerIteration);
103
111
    outString = PtrAdd(outString, iterations * bytesPerIteration);
104
111
    length -= iterations * bytesPerIteration;
105
111
  }
106
107
258
  size_t bufferByteSize = m_buffer.size();
108
258
  size_t bufferIterations = bufferByteSize / bytesPerIteration;
109
110
282
  while (length >= bufferByteSize)
111
24
  {
112
24
    policy.WriteKeystream(m_buffer, bufferIterations);
113
24
    xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
114
115
24
    inString = PtrAdd(inString, bufferByteSize);
116
24
    outString = PtrAdd(outString, bufferByteSize);
117
24
    length -= bufferByteSize;
118
24
  }
119
120
258
  if (length > 0)
121
199
  {
122
199
    bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
123
199
    bufferIterations = bufferByteSize / bytesPerIteration;
124
125
199
    policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
126
199
    xorbuf(outString, inString, PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
127
128
199
    m_leftOver = bufferByteSize - length;
129
199
  }
130
258
}
CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::SymmetricCipher> >::ProcessData(unsigned char*, unsigned char const*, unsigned long)
Line
Count
Source
70
54
{
71
54
  CRYPTOPP_ASSERT(outString); CRYPTOPP_ASSERT(inString);
72
54
  CRYPTOPP_ASSERT(length % this->MandatoryBlockSize() == 0);
73
74
54
  PolicyInterface &policy = this->AccessPolicy();
75
54
  size_t bytesPerIteration = policy.GetBytesPerIteration();
76
77
54
  if (m_leftOver > 0)
78
0
  {
79
0
    const size_t len = STDMIN(m_leftOver, length);
80
0
    xorbuf(outString, inString, PtrSub(KeystreamBufferEnd(), m_leftOver), len);
81
82
0
    inString = PtrAdd(inString, len);
83
0
    outString = PtrAdd(outString, len);
84
0
    length -= len; m_leftOver -= len;
85
0
  }
86
87
54
  if (!length) { return; }
88
89
49
  const word32 alignment = policy.GetAlignment();
90
49
  const bool inAligned = IsAlignedOn(inString, alignment);
91
49
  const bool outAligned = IsAlignedOn(outString, alignment);
92
49
  CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
93
94
49
  if (policy.CanOperateKeystream() && length >= bytesPerIteration)
95
0
  {
96
0
    const size_t iterations = length / bytesPerIteration;
97
0
    KeystreamOperationFlags flags = static_cast<KeystreamOperationFlags>(
98
0
      (inAligned ? EnumToInt(INPUT_ALIGNED) : 0) | (outAligned ? EnumToInt(OUTPUT_ALIGNED) : 0));
99
0
    KeystreamOperation operation = KeystreamOperation(flags);
100
0
    policy.OperateKeystream(operation, outString, inString, iterations);
101
102
0
    inString = PtrAdd(inString, iterations * bytesPerIteration);
103
0
    outString = PtrAdd(outString, iterations * bytesPerIteration);
104
0
    length -= iterations * bytesPerIteration;
105
0
  }
106
107
49
  size_t bufferByteSize = m_buffer.size();
108
49
  size_t bufferIterations = bufferByteSize / bytesPerIteration;
109
110
49
  while (length >= bufferByteSize)
111
0
  {
112
0
    policy.WriteKeystream(m_buffer, bufferIterations);
113
0
    xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
114
115
0
    inString = PtrAdd(inString, bufferByteSize);
116
0
    outString = PtrAdd(outString, bufferByteSize);
117
0
    length -= bufferByteSize;
118
0
  }
119
120
49
  if (length > 0)
121
49
  {
122
49
    bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
123
49
    bufferIterations = bufferByteSize / bytesPerIteration;
124
125
49
    policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
126
49
    xorbuf(outString, inString, PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
127
128
49
    m_leftOver = bufferByteSize - length;
129
49
  }
130
49
}
CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::OFB_ModePolicy> >::ProcessData(unsigned char*, unsigned char const*, unsigned long)
Line
Count
Source
70
147
{
71
147
  CRYPTOPP_ASSERT(outString); CRYPTOPP_ASSERT(inString);
72
147
  CRYPTOPP_ASSERT(length % this->MandatoryBlockSize() == 0);
73
74
147
  PolicyInterface &policy = this->AccessPolicy();
75
147
  size_t bytesPerIteration = policy.GetBytesPerIteration();
76
77
147
  if (m_leftOver > 0)
78
95
  {
79
95
    const size_t len = STDMIN(m_leftOver, length);
80
95
    xorbuf(outString, inString, PtrSub(KeystreamBufferEnd(), m_leftOver), len);
81
82
95
    inString = PtrAdd(inString, len);
83
95
    outString = PtrAdd(outString, len);
84
95
    length -= len; m_leftOver -= len;
85
95
  }
86
87
147
  if (!length) { return; }
88
89
74
  const word32 alignment = policy.GetAlignment();
90
74
  const bool inAligned = IsAlignedOn(inString, alignment);
91
74
  const bool outAligned = IsAlignedOn(outString, alignment);
92
74
  CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
93
94
74
  if (policy.CanOperateKeystream() && length >= bytesPerIteration)
95
0
  {
96
0
    const size_t iterations = length / bytesPerIteration;
97
0
    KeystreamOperationFlags flags = static_cast<KeystreamOperationFlags>(
98
0
      (inAligned ? EnumToInt(INPUT_ALIGNED) : 0) | (outAligned ? EnumToInt(OUTPUT_ALIGNED) : 0));
99
0
    KeystreamOperation operation = KeystreamOperation(flags);
100
0
    policy.OperateKeystream(operation, outString, inString, iterations);
101
102
0
    inString = PtrAdd(inString, iterations * bytesPerIteration);
103
0
    outString = PtrAdd(outString, iterations * bytesPerIteration);
104
0
    length -= iterations * bytesPerIteration;
105
0
  }
106
107
74
  size_t bufferByteSize = m_buffer.size();
108
74
  size_t bufferIterations = bufferByteSize / bytesPerIteration;
109
110
98
  while (length >= bufferByteSize)
111
24
  {
112
24
    policy.WriteKeystream(m_buffer, bufferIterations);
113
24
    xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
114
115
24
    inString = PtrAdd(inString, bufferByteSize);
116
24
    outString = PtrAdd(outString, bufferByteSize);
117
24
    length -= bufferByteSize;
118
24
  }
119
120
74
  if (length > 0)
121
68
  {
122
68
    bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
123
68
    bufferIterations = bufferByteSize / bytesPerIteration;
124
125
68
    policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
126
68
    xorbuf(outString, inString, PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
127
128
68
    m_leftOver = bufferByteSize - length;
129
68
  }
130
74
}
CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::CTR_ModePolicy> >::ProcessData(unsigned char*, unsigned char const*, unsigned long)
Line
Count
Source
70
141
{
71
141
  CRYPTOPP_ASSERT(outString); CRYPTOPP_ASSERT(inString);
72
141
  CRYPTOPP_ASSERT(length % this->MandatoryBlockSize() == 0);
73
74
141
  PolicyInterface &policy = this->AccessPolicy();
75
141
  size_t bytesPerIteration = policy.GetBytesPerIteration();
76
77
141
  if (m_leftOver > 0)
78
0
  {
79
0
    const size_t len = STDMIN(m_leftOver, length);
80
0
    xorbuf(outString, inString, PtrSub(KeystreamBufferEnd(), m_leftOver), len);
81
82
0
    inString = PtrAdd(inString, len);
83
0
    outString = PtrAdd(outString, len);
84
0
    length -= len; m_leftOver -= len;
85
0
  }
86
87
141
  if (!length) { return; }
88
89
135
  const word32 alignment = policy.GetAlignment();
90
135
  const bool inAligned = IsAlignedOn(inString, alignment);
91
135
  const bool outAligned = IsAlignedOn(outString, alignment);
92
135
  CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
93
94
135
  if (policy.CanOperateKeystream() && length >= bytesPerIteration)
95
111
  {
96
111
    const size_t iterations = length / bytesPerIteration;
97
111
    KeystreamOperationFlags flags = static_cast<KeystreamOperationFlags>(
98
111
      (inAligned ? EnumToInt(INPUT_ALIGNED) : 0) | (outAligned ? EnumToInt(OUTPUT_ALIGNED) : 0));
99
111
    KeystreamOperation operation = KeystreamOperation(flags);
100
111
    policy.OperateKeystream(operation, outString, inString, iterations);
101
102
111
    inString = PtrAdd(inString, iterations * bytesPerIteration);
103
111
    outString = PtrAdd(outString, iterations * bytesPerIteration);
104
111
    length -= iterations * bytesPerIteration;
105
111
  }
106
107
135
  size_t bufferByteSize = m_buffer.size();
108
135
  size_t bufferIterations = bufferByteSize / bytesPerIteration;
109
110
135
  while (length >= bufferByteSize)
111
0
  {
112
0
    policy.WriteKeystream(m_buffer, bufferIterations);
113
0
    xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
114
115
0
    inString = PtrAdd(inString, bufferByteSize);
116
0
    outString = PtrAdd(outString, bufferByteSize);
117
0
    length -= bufferByteSize;
118
0
  }
119
120
135
  if (length > 0)
121
82
  {
122
82
    bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
123
82
    bufferIterations = bufferByteSize / bytesPerIteration;
124
125
82
    policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
126
82
    xorbuf(outString, inString, PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
127
128
82
    m_leftOver = bufferByteSize - length;
129
82
  }
130
135
}
131
132
template <class S>
133
void AdditiveCipherTemplate<S>::Resynchronize(const byte *iv, int length)
134
78
{
135
78
  PolicyInterface &policy = this->AccessPolicy();
136
78
  m_leftOver = 0;
137
78
  m_buffer.New(GetBufferByteSize(policy));
138
78
  policy.CipherResynchronize(m_buffer, iv, this->ThrowIfInvalidIVLength(length));
139
78
}
Unexecuted instantiation: CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::SymmetricCipher> >::Resynchronize(unsigned char const*, int)
Unexecuted instantiation: CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::OFB_ModePolicy> >::Resynchronize(unsigned char const*, int)
CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::CTR_ModePolicy> >::Resynchronize(unsigned char const*, int)
Line
Count
Source
134
78
{
135
78
  PolicyInterface &policy = this->AccessPolicy();
136
78
  m_leftOver = 0;
137
78
  m_buffer.New(GetBufferByteSize(policy));
138
78
  policy.CipherResynchronize(m_buffer, iv, this->ThrowIfInvalidIVLength(length));
139
78
}
140
141
template <class BASE>
142
void AdditiveCipherTemplate<BASE>::Seek(lword position)
143
120
{
144
120
  PolicyInterface &policy = this->AccessPolicy();
145
120
  unsigned int bytesPerIteration = policy.GetBytesPerIteration();
146
147
120
  policy.SeekToIteration(position / bytesPerIteration);
148
120
  position %= bytesPerIteration;
149
150
120
  if (position > 0)
151
0
  {
152
0
    policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bytesPerIteration), 1);
153
0
    m_leftOver = bytesPerIteration - static_cast<unsigned int>(position);
154
0
  }
155
120
  else
156
120
    m_leftOver = 0;
157
120
}
Unexecuted instantiation: CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::SymmetricCipher> >::Seek(unsigned long)
Unexecuted instantiation: CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::OFB_ModePolicy> >::Seek(unsigned long)
CryptoPP::AdditiveCipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::AdditiveCipherAbstractPolicy, CryptoPP::CTR_ModePolicy> >::Seek(unsigned long)
Line
Count
Source
143
120
{
144
120
  PolicyInterface &policy = this->AccessPolicy();
145
120
  unsigned int bytesPerIteration = policy.GetBytesPerIteration();
146
147
120
  policy.SeekToIteration(position / bytesPerIteration);
148
120
  position %= bytesPerIteration;
149
150
120
  if (position > 0)
151
0
  {
152
0
    policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bytesPerIteration), 1);
153
0
    m_leftOver = bytesPerIteration - static_cast<unsigned int>(position);
154
0
  }
155
120
  else
156
120
    m_leftOver = 0;
157
120
}
158
159
template <class BASE>
160
void CFB_CipherTemplate<BASE>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
161
157
{
162
157
  PolicyInterface &policy = this->AccessPolicy();
163
157
  policy.CipherSetKey(params, key, length);
164
165
157
  if (this->IsResynchronizable())
166
157
  {
167
157
    size_t ivLength;
168
157
    const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
169
157
    policy.CipherResynchronize(iv, ivLength);
170
157
  }
171
172
157
  m_leftOver = policy.GetBytesPerIteration();
173
157
}
Unexecuted instantiation: CryptoPP::CFB_CipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::CFB_CipherAbstractPolicy, CryptoPP::SymmetricCipher> >::UncheckedSetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&)
CryptoPP::CFB_CipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::CFB_CipherAbstractPolicy, CryptoPP::CFB_ModePolicy> >::UncheckedSetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&)
Line
Count
Source
161
157
{
162
157
  PolicyInterface &policy = this->AccessPolicy();
163
157
  policy.CipherSetKey(params, key, length);
164
165
157
  if (this->IsResynchronizable())
166
157
  {
167
157
    size_t ivLength;
168
157
    const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
169
157
    policy.CipherResynchronize(iv, ivLength);
170
157
  }
171
172
157
  m_leftOver = policy.GetBytesPerIteration();
173
157
}
174
175
template <class BASE>
176
void CFB_CipherTemplate<BASE>::Resynchronize(const byte *iv, int length)
177
150
{
178
150
  PolicyInterface &policy = this->AccessPolicy();
179
150
  policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length));
180
150
  m_leftOver = policy.GetBytesPerIteration();
181
150
}
Unexecuted instantiation: CryptoPP::CFB_CipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::CFB_CipherAbstractPolicy, CryptoPP::SymmetricCipher> >::Resynchronize(unsigned char const*, int)
CryptoPP::CFB_CipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::CFB_CipherAbstractPolicy, CryptoPP::CFB_ModePolicy> >::Resynchronize(unsigned char const*, int)
Line
Count
Source
177
150
{
178
150
  PolicyInterface &policy = this->AccessPolicy();
179
150
  policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length));
180
150
  m_leftOver = policy.GetBytesPerIteration();
181
150
}
182
183
template <class BASE>
184
void CFB_CipherTemplate<BASE>::ProcessData(byte *outString, const byte *inString, size_t length)
185
520
{
186
520
  CRYPTOPP_ASSERT(outString); CRYPTOPP_ASSERT(inString);
187
520
  CRYPTOPP_ASSERT(length % this->MandatoryBlockSize() == 0);
188
189
520
  PolicyInterface &policy = this->AccessPolicy();
190
520
  unsigned int bytesPerIteration = policy.GetBytesPerIteration();
191
520
  byte *reg = policy.GetRegisterBegin();
192
193
520
  if (m_leftOver)
194
495
  {
195
495
    const size_t len = STDMIN(m_leftOver, length);
196
495
    CombineMessageAndShiftRegister(outString, PtrAdd(reg, bytesPerIteration - m_leftOver), inString, len);
197
198
495
    inString = PtrAdd(inString, len);
199
495
    outString = PtrAdd(outString, len);
200
495
    m_leftOver -= len; length -= len;
201
495
  }
202
203
520
  if (!length) { return; }
204
205
330
  const word32 alignment = policy.GetAlignment();
206
330
  const bool inAligned = IsAlignedOn(inString, alignment);
207
330
  const bool outAligned = IsAlignedOn(outString, alignment);
208
330
  CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
209
210
330
  if (policy.CanIterate() && length >= bytesPerIteration && outAligned)
211
246
  {
212
246
    CipherDir cipherDir = GetCipherDir(*this);
213
246
    policy.Iterate(outString, inString, cipherDir, length / bytesPerIteration);
214
215
246
    const size_t remainder = length % bytesPerIteration;
216
246
    inString = PtrAdd(inString, length - remainder);
217
246
    outString = PtrAdd(outString, length - remainder);
218
246
    length = remainder;
219
246
  }
220
221
815
  while (length >= bytesPerIteration)
222
485
  {
223
485
    policy.TransformRegister();
224
485
    CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
225
226
485
    inString = PtrAdd(inString, bytesPerIteration);
227
485
    outString = PtrAdd(outString, bytesPerIteration);
228
485
    length -= bytesPerIteration;
229
485
  }
230
231
330
  if (length > 0)
232
79
  {
233
79
    policy.TransformRegister();
234
79
    CombineMessageAndShiftRegister(outString, reg, inString, length);
235
79
    m_leftOver = bytesPerIteration - length;
236
79
  }
237
330
}
Unexecuted instantiation: CryptoPP::CFB_CipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::CFB_CipherAbstractPolicy, CryptoPP::SymmetricCipher> >::ProcessData(unsigned char*, unsigned char const*, unsigned long)
CryptoPP::CFB_CipherTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::CFB_CipherAbstractPolicy, CryptoPP::CFB_ModePolicy> >::ProcessData(unsigned char*, unsigned char const*, unsigned long)
Line
Count
Source
185
520
{
186
520
  CRYPTOPP_ASSERT(outString); CRYPTOPP_ASSERT(inString);
187
520
  CRYPTOPP_ASSERT(length % this->MandatoryBlockSize() == 0);
188
189
520
  PolicyInterface &policy = this->AccessPolicy();
190
520
  unsigned int bytesPerIteration = policy.GetBytesPerIteration();
191
520
  byte *reg = policy.GetRegisterBegin();
192
193
520
  if (m_leftOver)
194
495
  {
195
495
    const size_t len = STDMIN(m_leftOver, length);
196
495
    CombineMessageAndShiftRegister(outString, PtrAdd(reg, bytesPerIteration - m_leftOver), inString, len);
197
198
495
    inString = PtrAdd(inString, len);
199
495
    outString = PtrAdd(outString, len);
200
495
    m_leftOver -= len; length -= len;
201
495
  }
202
203
520
  if (!length) { return; }
204
205
330
  const word32 alignment = policy.GetAlignment();
206
330
  const bool inAligned = IsAlignedOn(inString, alignment);
207
330
  const bool outAligned = IsAlignedOn(outString, alignment);
208
330
  CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
209
210
330
  if (policy.CanIterate() && length >= bytesPerIteration && outAligned)
211
246
  {
212
246
    CipherDir cipherDir = GetCipherDir(*this);
213
246
    policy.Iterate(outString, inString, cipherDir, length / bytesPerIteration);
214
215
246
    const size_t remainder = length % bytesPerIteration;
216
246
    inString = PtrAdd(inString, length - remainder);
217
246
    outString = PtrAdd(outString, length - remainder);
218
246
    length = remainder;
219
246
  }
220
221
815
  while (length >= bytesPerIteration)
222
485
  {
223
485
    policy.TransformRegister();
224
485
    CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
225
226
485
    inString = PtrAdd(inString, bytesPerIteration);
227
485
    outString = PtrAdd(outString, bytesPerIteration);
228
485
    length -= bytesPerIteration;
229
485
  }
230
231
330
  if (length > 0)
232
79
  {
233
79
    policy.TransformRegister();
234
79
    CombineMessageAndShiftRegister(outString, reg, inString, length);
235
79
    m_leftOver = bytesPerIteration - length;
236
79
  }
237
330
}
238
239
template <class BASE>
240
void CFB_EncryptionTemplate<BASE>::CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length)
241
666
{
242
666
  xorbuf(reg, message, length);
243
666
  std::memcpy(output, reg, length);
244
666
}
Unexecuted instantiation: CryptoPP::CFB_EncryptionTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::CFB_CipherAbstractPolicy, CryptoPP::SymmetricCipher> >::CombineMessageAndShiftRegister(unsigned char*, unsigned char*, unsigned char const*, unsigned long)
CryptoPP::CFB_EncryptionTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::CFB_CipherAbstractPolicy, CryptoPP::CFB_ModePolicy> >::CombineMessageAndShiftRegister(unsigned char*, unsigned char*, unsigned char const*, unsigned long)
Line
Count
Source
241
666
{
242
666
  xorbuf(reg, message, length);
243
666
  std::memcpy(output, reg, length);
244
666
}
245
246
template <class BASE>
247
void CFB_DecryptionTemplate<BASE>::CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length)
248
393
{
249
1.47k
  for (size_t i=0; i<length; i++)
250
1.08k
  {
251
1.08k
    byte b = message[i];
252
1.08k
    output[i] = reg[i] ^ b;
253
1.08k
    reg[i] = b;
254
1.08k
  }
255
393
}
Unexecuted instantiation: CryptoPP::CFB_DecryptionTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::CFB_CipherAbstractPolicy, CryptoPP::SymmetricCipher> >::CombineMessageAndShiftRegister(unsigned char*, unsigned char*, unsigned char const*, unsigned long)
CryptoPP::CFB_DecryptionTemplate<CryptoPP::AbstractPolicyHolder<CryptoPP::CFB_CipherAbstractPolicy, CryptoPP::CFB_ModePolicy> >::CombineMessageAndShiftRegister(unsigned char*, unsigned char*, unsigned char const*, unsigned long)
Line
Count
Source
248
393
{
249
1.47k
  for (size_t i=0; i<length; i++)
250
1.08k
  {
251
1.08k
    byte b = message[i];
252
1.08k
    output[i] = reg[i] ^ b;
253
1.08k
    reg[i] = b;
254
1.08k
  }
255
393
}
256
257
NAMESPACE_END
258
259
#endif