diff options
author | Vadim Dashevskiy <watcherhd@gmail.com> | 2012-05-15 10:38:20 +0000 |
---|---|---|
committer | Vadim Dashevskiy <watcherhd@gmail.com> | 2012-05-15 10:38:20 +0000 |
commit | 48540940b6c28bb4378abfeb500ec45a625b37b6 (patch) | |
tree | 2ef294c0763e802f91d868bdef4229b6868527de /plugins/CryptoPP/crypto/strciphr.cpp | |
parent | 5c350913f011e119127baeb32a6aedeb4f0d33bc (diff) |
initial commit
git-svn-id: http://svn.miranda-ng.org/main/trunk@2 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/CryptoPP/crypto/strciphr.cpp')
-rw-r--r-- | plugins/CryptoPP/crypto/strciphr.cpp | 256 |
1 files changed, 256 insertions, 0 deletions
diff --git a/plugins/CryptoPP/crypto/strciphr.cpp b/plugins/CryptoPP/crypto/strciphr.cpp new file mode 100644 index 0000000000..fcc1e907b1 --- /dev/null +++ b/plugins/CryptoPP/crypto/strciphr.cpp @@ -0,0 +1,256 @@ +// strciphr.cpp - written and placed in the public domain by Wei Dai
+
+#include "pch.h"
+
+// prevent Sun's CC compiler from including this file automatically
+#if !defined(__SUNPRO_CC) || defined(CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES)
+
+#ifndef CRYPTOPP_IMPORTS
+
+#include "strciphr.h"
+
+NAMESPACE_BEGIN(CryptoPP)
+
+template <class S>
+void AdditiveCipherTemplate<S>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
+{
+ PolicyInterface &policy = this->AccessPolicy();
+ policy.CipherSetKey(params, key, length);
+ m_leftOver = 0;
+ m_buffer.New(GetBufferByteSize(policy));
+
+ if (this->IsResynchronizable())
+ policy.CipherResynchronize(m_buffer, this->GetIVAndThrowIfInvalid(params));
+}
+
+template <class S>
+void AdditiveCipherTemplate<S>::GenerateBlock(byte *outString, size_t length)
+{
+ if (m_leftOver > 0)
+ {
+ size_t len = STDMIN(m_leftOver, length);
+ memcpy(outString, KeystreamBufferEnd()-m_leftOver, len);
+ length -= len;
+ m_leftOver -= len;
+ outString += len;
+
+ if (!length)
+ return;
+ }
+ assert(m_leftOver == 0);
+
+ PolicyInterface &policy = this->AccessPolicy();
+ unsigned int bytesPerIteration = policy.GetBytesPerIteration();
+
+ if (length >= bytesPerIteration)
+ {
+ size_t iterations = length / bytesPerIteration;
+ policy.WriteKeystream(outString, iterations);
+ outString += iterations * bytesPerIteration;
+ length -= iterations * bytesPerIteration;
+
+ if (!length)
+ return;
+ }
+
+ unsigned int bufferByteSize = GetBufferByteSize(policy);
+ unsigned int bufferIterations = policy.GetIterationsToBuffer();
+
+ while (length >= bufferByteSize)
+ {
+ policy.WriteKeystream(m_buffer, bufferIterations);
+ memcpy(outString, KeystreamBufferBegin(), bufferByteSize);
+ length -= bufferByteSize;
+ outString += bufferByteSize;
+ }
+
+ if (length > 0)
+ {
+ policy.WriteKeystream(m_buffer, bufferIterations);
+ memcpy(outString, KeystreamBufferBegin(), length);
+ m_leftOver = bytesPerIteration - length;
+ }
+}
+
+template <class S>
+void AdditiveCipherTemplate<S>::ProcessData(byte *outString, const byte *inString, size_t length)
+{
+ if (m_leftOver > 0)
+ {
+ size_t len = STDMIN(m_leftOver, length);
+ xorbuf(outString, inString, KeystreamBufferEnd()-m_leftOver, len);
+ length -= len;
+ m_leftOver -= len;
+ inString += len;
+ outString += len;
+
+ if (!length)
+ return;
+ }
+ assert(m_leftOver == 0);
+
+ PolicyInterface &policy = this->AccessPolicy();
+ unsigned int bytesPerIteration = policy.GetBytesPerIteration();
+
+ if (policy.CanOperateKeystream() && length >= bytesPerIteration)
+ {
+ size_t iterations = length / bytesPerIteration;
+ unsigned int alignment = policy.GetAlignment();
+ KeystreamOperation operation = KeystreamOperation((IsAlignedOn(inString, alignment) * 2) | (int)IsAlignedOn(outString, alignment));
+
+ policy.OperateKeystream(operation, outString, inString, iterations);
+
+ inString += iterations * bytesPerIteration;
+ outString += iterations * bytesPerIteration;
+ length -= iterations * bytesPerIteration;
+
+ if (!length)
+ return;
+ }
+
+ unsigned int bufferByteSize = GetBufferByteSize(policy);
+ unsigned int bufferIterations = policy.GetIterationsToBuffer();
+
+ while (length >= bufferByteSize)
+ {
+ policy.WriteKeystream(m_buffer, bufferIterations);
+ xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
+ length -= bufferByteSize;
+ inString += bufferByteSize;
+ outString += bufferByteSize;
+ }
+
+ if (length > 0)
+ {
+ policy.WriteKeystream(m_buffer, bufferIterations);
+ xorbuf(outString, inString, KeystreamBufferBegin(), length);
+ m_leftOver = bytesPerIteration - length;
+ }
+}
+
+template <class S>
+void AdditiveCipherTemplate<S>::Resynchronize(const byte *iv)
+{
+ PolicyInterface &policy = this->AccessPolicy();
+ m_leftOver = 0;
+ m_buffer.New(GetBufferByteSize(policy));
+ policy.CipherResynchronize(m_buffer, iv);
+}
+
+template <class BASE>
+void AdditiveCipherTemplate<BASE>::Seek(lword position)
+{
+ PolicyInterface &policy = this->AccessPolicy();
+ unsigned int bytesPerIteration = policy.GetBytesPerIteration();
+
+ policy.SeekToIteration(position / bytesPerIteration);
+ position %= bytesPerIteration;
+
+ if (position > 0)
+ {
+ policy.WriteKeystream(m_buffer, 1);
+ m_leftOver = bytesPerIteration - (unsigned int)position;
+ }
+ else
+ m_leftOver = 0;
+}
+
+template <class BASE>
+void CFB_CipherTemplate<BASE>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
+{
+ PolicyInterface &policy = this->AccessPolicy();
+ policy.CipherSetKey(params, key, length);
+
+ if (this->IsResynchronizable())
+ policy.CipherResynchronize(this->GetIVAndThrowIfInvalid(params));
+
+ m_leftOver = policy.GetBytesPerIteration();
+}
+
+template <class BASE>
+void CFB_CipherTemplate<BASE>::Resynchronize(const byte *iv)
+{
+ PolicyInterface &policy = this->AccessPolicy();
+ policy.CipherResynchronize(iv);
+ m_leftOver = policy.GetBytesPerIteration();
+}
+
+template <class BASE>
+void CFB_CipherTemplate<BASE>::ProcessData(byte *outString, const byte *inString, size_t length)
+{
+ assert(length % this->MandatoryBlockSize() == 0);
+
+ PolicyInterface &policy = this->AccessPolicy();
+ unsigned int bytesPerIteration = policy.GetBytesPerIteration();
+ unsigned int alignment = policy.GetAlignment();
+ byte *reg = policy.GetRegisterBegin();
+
+ if (m_leftOver)
+ {
+ size_t len = STDMIN(m_leftOver, length);
+ CombineMessageAndShiftRegister(outString, reg + bytesPerIteration - m_leftOver, inString, len);
+ m_leftOver -= len;
+ length -= len;
+ inString += len;
+ outString += len;
+ }
+
+ if (!length)
+ return;
+
+ assert(m_leftOver == 0);
+
+ if (policy.CanIterate() && length >= bytesPerIteration && IsAlignedOn(outString, alignment))
+ {
+ if (IsAlignedOn(inString, alignment))
+ policy.Iterate(outString, inString, GetCipherDir(*this), length / bytesPerIteration);
+ else
+ {
+ memcpy(outString, inString, length);
+ policy.Iterate(outString, outString, GetCipherDir(*this), length / bytesPerIteration);
+ }
+ inString += length - length % bytesPerIteration;
+ outString += length - length % bytesPerIteration;
+ length %= bytesPerIteration;
+ }
+
+ while (length >= bytesPerIteration)
+ {
+ policy.TransformRegister();
+ CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
+ length -= bytesPerIteration;
+ inString += bytesPerIteration;
+ outString += bytesPerIteration;
+ }
+
+ if (length > 0)
+ {
+ policy.TransformRegister();
+ CombineMessageAndShiftRegister(outString, reg, inString, length);
+ m_leftOver = bytesPerIteration - length;
+ }
+}
+
+template <class BASE>
+void CFB_EncryptionTemplate<BASE>::CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length)
+{
+ xorbuf(reg, message, length);
+ memcpy(output, reg, length);
+}
+
+template <class BASE>
+void CFB_DecryptionTemplate<BASE>::CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length)
+{
+ for (unsigned int i=0; i<length; i++)
+ {
+ byte b = message[i];
+ output[i] = reg[i] ^ b;
+ reg[i] = b;
+ }
+}
+
+NAMESPACE_END
+
+#endif
+
+#endif
|