monotone

monotone Mtn Source Tree

Root/cryptopp/strciphr.cpp

1// strciphr.cpp - written and placed in the public domain by Wei Dai
2
3#include "pch.h"
4#include "strciphr.h"
5
6NAMESPACE_BEGIN(CryptoPP)
7
8template <class S>
9byte AdditiveCipherTemplate<S>::GenerateByte()
10{
11PolicyInterface &policy = AccessPolicy();
12
13if (m_leftOver == 0)
14{
15policy.WriteKeystream(m_buffer, policy.GetIterationsToBuffer());
16m_leftOver = policy.GetBytesPerIteration();
17}
18
19return *(KeystreamBufferEnd()-m_leftOver--);
20}
21
22template <class S>
23inline void AdditiveCipherTemplate<S>::ProcessData(byte *outString, const byte *inString, unsigned int length)
24{
25if (m_leftOver > 0)
26{
27unsigned int len = STDMIN(m_leftOver, length);
28xorbuf(outString, inString, KeystreamBufferEnd()-m_leftOver, len);
29length -= len;
30m_leftOver -= len;
31inString += len;
32outString += len;
33}
34
35if (!length)
36return;
37
38assert(m_leftOver == 0);
39
40PolicyInterface &policy = AccessPolicy();
41unsigned int bytesPerIteration = policy.GetBytesPerIteration();
42unsigned int alignment = policy.GetAlignment();
43
44if (policy.CanOperateKeystream() && length >= bytesPerIteration && IsAlignedOn(outString, alignment))
45{
46if (IsAlignedOn(inString, alignment))
47policy.OperateKeystream(XOR_KEYSTREAM, outString, inString, length / bytesPerIteration);
48else
49{
50memcpy(outString, inString, length);
51policy.OperateKeystream(XOR_KEYSTREAM_INPLACE, outString, outString, length / bytesPerIteration);
52}
53inString += length - length % bytesPerIteration;
54outString += length - length % bytesPerIteration;
55length %= bytesPerIteration;
56
57if (!length)
58return;
59}
60
61unsigned int bufferByteSize = GetBufferByteSize(policy);
62unsigned int bufferIterations = policy.GetIterationsToBuffer();
63
64while (length >= bufferByteSize)
65{
66policy.WriteKeystream(m_buffer, bufferIterations);
67xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
68length -= bufferByteSize;
69inString += bufferByteSize;
70outString += bufferByteSize;
71}
72
73if (length > 0)
74{
75policy.WriteKeystream(m_buffer, bufferIterations);
76xorbuf(outString, inString, KeystreamBufferBegin(), length);
77m_leftOver = bytesPerIteration - length;
78}
79}
80
81template <class S>
82void AdditiveCipherTemplate<S>::Resynchronize(const byte *iv)
83{
84PolicyInterface &policy = AccessPolicy();
85m_leftOver = 0;
86m_buffer.New(GetBufferByteSize(policy));
87policy.CipherResynchronize(m_buffer, iv);
88}
89
90template <class BASE>
91void AdditiveCipherTemplate<BASE>::Seek(dword position)
92{
93PolicyInterface &policy = AccessPolicy();
94unsigned int bytesPerIteration = policy.GetBytesPerIteration();
95
96policy.SeekToIteration(position / bytesPerIteration);
97position %= bytesPerIteration;
98
99if (position > 0)
100{
101policy.WriteKeystream(m_buffer, 1);
102m_leftOver = bytesPerIteration - (unsigned int)position;
103}
104else
105m_leftOver = 0;
106}
107
108template <class BASE>
109void CFB_CipherTemplate<BASE>::Resynchronize(const byte *iv)
110{
111PolicyInterface &policy = AccessPolicy();
112policy.CipherResynchronize(iv);
113m_leftOver = policy.GetBytesPerIteration();
114}
115
116template <class BASE>
117void CFB_CipherTemplate<BASE>::ProcessData(byte *outString, const byte *inString, unsigned int length)
118{
119PolicyInterface &policy = AccessPolicy();
120unsigned int bytesPerIteration = policy.GetBytesPerIteration();
121unsigned int alignment = policy.GetAlignment();
122byte *reg = policy.GetRegisterBegin();
123
124if (m_leftOver)
125{
126unsigned int len = STDMIN(m_leftOver, length);
127CombineMessageAndShiftRegister(outString, reg + bytesPerIteration - m_leftOver, inString, len);
128m_leftOver -= len;
129length -= len;
130inString += len;
131outString += len;
132}
133
134if (!length)
135return;
136
137assert(m_leftOver == 0);
138
139if (policy.CanIterate() && length >= bytesPerIteration && IsAlignedOn(outString, alignment))
140{
141if (IsAlignedOn(inString, alignment))
142policy.Iterate(outString, inString, GetCipherDir(*this), length / bytesPerIteration);
143else
144{
145memcpy(outString, inString, length);
146policy.Iterate(outString, outString, GetCipherDir(*this), length / bytesPerIteration);
147}
148inString += length - length % bytesPerIteration;
149outString += length - length % bytesPerIteration;
150length %= bytesPerIteration;
151}
152
153while (length >= bytesPerIteration)
154{
155policy.TransformRegister();
156CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
157length -= bytesPerIteration;
158inString += bytesPerIteration;
159outString += bytesPerIteration;
160}
161
162if (length > 0)
163{
164policy.TransformRegister();
165CombineMessageAndShiftRegister(outString, reg, inString, length);
166m_leftOver = bytesPerIteration - length;
167}
168}
169
170template <class BASE>
171void CFB_EncryptionTemplate<BASE>::CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, unsigned int length)
172{
173xorbuf(reg, message, length);
174memcpy(output, reg, length);
175}
176
177template <class BASE>
178void CFB_DecryptionTemplate<BASE>::CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, unsigned int length)
179{
180for (unsigned int i=0; i<length; i++)
181{
182byte b = message[i];
183output[i] = reg[i] ^ b;
184reg[i] = b;
185}
186}
187
188NAMESPACE_END

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status