added unit test to make sure IV works as we expect.

This commit is contained in:
Nick Bolton
2013-04-08 13:01:21 +00:00
parent 0a69c28ac5
commit b2746bc1b2
3 changed files with 140 additions and 24 deletions

View File

@@ -29,10 +29,10 @@ const byte g_iv2[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
using namespace CryptoPP;
CCryptoStream::CCryptoStream(IEventQueue& eventQueue, synergy::IStream* stream, bool adoptStream) :
CStreamFilter(eventQueue, stream, adoptStream)
{
m_encryption.SetKeyWithIV(g_key1, sizeof(g_key1), g_iv1);
m_decryption.SetKeyWithIV(g_key1, sizeof(g_key1), g_iv1);
CStreamFilter(eventQueue, stream, adoptStream),
m_key(NULL),
m_keyLength(0)
{
}
CCryptoStream::~CCryptoStream()
@@ -42,6 +42,7 @@ CCryptoStream::~CCryptoStream()
UInt32
CCryptoStream::read(void* out, UInt32 n)
{
assert(m_key != NULL);
LOG((CLOG_DEBUG4 "crypto: read %i (decrypt)", n));
byte* cypher = new byte[n];
@@ -66,6 +67,7 @@ CCryptoStream::read(void* out, UInt32 n)
void
CCryptoStream::write(const void* in, UInt32 n)
{
assert(m_key != NULL);
LOG((CLOG_DEBUG4 "crypto: write %i (encrypt)", n));
logBuffer("plaintext", static_cast<byte*>(const_cast<void*>(in)), n);
@@ -75,6 +77,26 @@ CCryptoStream::write(const void* in, UInt32 n)
getStream()->write(cypher, n);
delete[] cypher;
}
void
CCryptoStream::setKeyWithIV(const byte* key, size_t length, const byte* iv)
{
LOG((CLOG_DEBUG "crypto: key=%s (%i) iv=%s", key, length, iv));
m_encryption.SetKeyWithIV(key, length, iv);
m_decryption.SetKeyWithIV(key, length, iv);
m_key = key;
m_keyLength = length;
}
void
CCryptoStream::setIV(const byte* iv)
{
assert(m_key != NULL);
LOG((CLOG_DEBUG "crypto: new iv=%s", iv));
m_encryption.SetKeyWithIV(m_key, m_keyLength, iv);
m_decryption.SetKeyWithIV(m_key, m_keyLength, iv);
}
void
CCryptoStream::logBuffer(const char* name, const byte* buf, int length)

View File

@@ -20,7 +20,7 @@
#include "BasicTypes.h"
#include "CStreamFilter.h"
#include "cryptopp562/gcm.h"
//#include "cryptopp562/modes.h"
#include "cryptopp562/modes.h"
#include "cryptopp562/aes.h"
//! Bidirectional encrypted stream
@@ -48,12 +48,30 @@ public:
*/
virtual void write(const void* in, UInt32 n);
//! Set the key and IV
void setKeyWithIV(const byte* key, size_t length, const byte* iv);
//! Set the IV
void setIV(const byte* iv);
private:
// TODO: allow user to change between GCM/CTR/CFB
CryptoPP::GCM<CryptoPP::AES>::Encryption m_encryption;
CryptoPP::GCM<CryptoPP::AES>::Decryption m_decryption;
// TODO: allow user to change the block cypher mode.
/*
For CBC and CFB, reusing an IV leaks some information about the first block of plaintext,
and about any common prefix shared by the two messages. For OFB and CTR, reusing an IV
completely destroys security. http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation
*/
CryptoPP::OFB_Mode<CryptoPP::AES>::Encryption m_encryption;
CryptoPP::OFB_Mode<CryptoPP::AES>::Decryption m_decryption;
//CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption m_encryption;
//CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption m_decryption;
//CryptoPP::GCM<CryptoPP::AES>::Encryption m_encryption;
//CryptoPP::GCM<CryptoPP::AES>::Decryption m_decryption;
//CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption m_encryption;
//CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption m_decryption;
void logBuffer(const char* name, const byte* buf, int length);
const byte* m_key;
size_t m_keyLength;
};