i2pd/aes.cpp

49 lines
1.2 KiB
C++
Raw Normal View History

2014-05-06 21:26:28 +04:00
#include <stdlib.h>
2014-05-06 20:22:22 +04:00
#include "aes.h"
namespace i2p
{
namespace crypto
{
void CBCEncryption::Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
{
for (int i = 0; i < numBlocks; i++)
{
m_LastBlock.ll[0] ^= in[i].ll[0];
m_LastBlock.ll[1] ^= in[i].ll[1];
m_ECBEncryption.ProcessData (m_LastBlock.buf, m_LastBlock.buf, 16);
out[i] = m_LastBlock;
}
}
2014-05-06 21:26:28 +04:00
bool CBCEncryption::Encrypt (const uint8_t * in, std::size_t len, uint8_t * out)
{
div_t d = div (len, 16);
if (d.rem) return false; // len is not multipple of 16
Encrypt (d.quot, (const ChipherBlock *)in, (ChipherBlock *)out);
return true;
}
2014-05-06 20:22:22 +04:00
void CBCDecryption::Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
{
for (int i = 0; i < numBlocks; i++)
{
2014-05-06 21:26:28 +04:00
ChipherBlock tmp = in[i];
2014-05-06 20:22:22 +04:00
m_ECBDecryption.ProcessData (out[i].buf, in[i].buf, 16);
out[i].ll[0] ^= m_IV.ll[0];
out[i].ll[1] ^= m_IV.ll[1];
2014-05-06 21:26:28 +04:00
m_IV = tmp;
2014-05-06 20:22:22 +04:00
}
}
2014-05-06 21:26:28 +04:00
bool CBCDecryption::Decrypt (const uint8_t * in, std::size_t len, uint8_t * out)
{
div_t d = div (len, 16);
if (d.rem) return false; // len is not multipple of 16
Decrypt (d.quot, (const ChipherBlock *)in, (ChipherBlock *)out);
return true;
}
2014-05-06 20:22:22 +04:00
}
}