fixed: new iv was used before it was sent to the client, so the client could not decrypt new iv.

This commit is contained in:
Nick Bolton 2013-04-11 22:30:24 +00:00
parent bae07f8cd5
commit 607f8c9321
3 changed files with 32 additions and 1 deletions

View File

@ -103,6 +103,10 @@ CClientProxy1_4::cryptoIv()
LOG((CLOG_DEBUG2 "send crypto iv change to \"%s\"", getName().c_str())); LOG((CLOG_DEBUG2 "send crypto iv change to \"%s\"", getName().c_str()));
CProtocolUtil::writef(getStream(), kMsgDCryptoIv, &data); CProtocolUtil::writef(getStream(), kMsgDCryptoIv, &data);
// change IV only after we've sent the current IV, otherwise
// the client won't be able to decrypt the new IV.
cryptoStream->setIv(iv);
} }
bool bool

View File

@ -124,7 +124,6 @@ void
CCryptoStream::newIv(byte* out) CCryptoStream::newIv(byte* out)
{ {
m_autoSeedRandomPool.GenerateBlock(out, CRYPTO_IV_SIZE); m_autoSeedRandomPool.GenerateBlock(out, CRYPTO_IV_SIZE);
setIv(out);
} }
void void

View File

@ -56,6 +56,9 @@ UInt8 g_readWriteIvChangeTrigger_buffer[4 + 4 + 16]; // abcd, DCIV, 16-byte IV
void readWriteIvChangeTrigger_mockWrite(const void* in, UInt32 n); void readWriteIvChangeTrigger_mockWrite(const void* in, UInt32 n);
UInt8 readWriteIvChangeTrigger_mockRead(void* out, UInt32 n); UInt8 readWriteIvChangeTrigger_mockRead(void* out, UInt32 n);
UInt8 g_newIvDoesNotChangeIv_buffer[1];
void newIvDoesNotChangeIv_mockWrite(const void* in, UInt32 n);
TEST(CCryptoTests, write) TEST(CCryptoTests, write)
{ {
const UInt32 size = 4; const UInt32 size = 4;
@ -248,6 +251,25 @@ TEST(CCryptoTests, createKey)
EXPECT_EQ(hash3[31], 233); EXPECT_EQ(hash3[31], 233);
} }
TEST(CCryptoTests, newIvDoesNotChangeIv)
{
NiceMock<CMockEventQueue> eventQueue;
NiceMock<CMockStream> innerStream;
CCryptoOptions options("ctr", "mock");
ON_CALL(innerStream, write(_, _)).WillByDefault(Invoke(newIvDoesNotChangeIv_mockWrite));
CCryptoStream cs1(&eventQueue, &innerStream, options, false);
cs1.write("a", 1);
EXPECT_EQ(175, g_newIvDoesNotChangeIv_buffer[0]);
byte iv[CRYPTO_IV_SIZE];
cs1.newIv(iv);
cs1.write("a", 1);
EXPECT_EQ(92, g_newIvDoesNotChangeIv_buffer[0]);
}
void void
write_mockWrite(const void* in, UInt32 n) write_mockWrite(const void* in, UInt32 n)
{ {
@ -318,3 +340,9 @@ readWriteIvChangeTrigger_mockRead(void* out, UInt32 n)
g_readWriteIvChangeTrigger_readBufferIndex += n; g_readWriteIvChangeTrigger_readBufferIndex += n;
return n; return n;
} }
void
newIvDoesNotChangeIv_mockWrite(const void* in, UInt32 n)
{
memcpy(g_newIvDoesNotChangeIv_buffer, in, 1);
}