mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-23 17:36:37 +02:00
encrypt/decrypt same buffer
This commit is contained in:
parent
d8d8c752b1
commit
81b57141d4
2 changed files with 21 additions and 1 deletions
20
aes.cpp
20
aes.cpp
|
@ -1,3 +1,4 @@
|
||||||
|
#include <stdlib.h>
|
||||||
#include "aes.h"
|
#include "aes.h"
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
|
@ -15,16 +16,33 @@ namespace crypto
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
void CBCDecryption::Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
|
void CBCDecryption::Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < numBlocks; i++)
|
for (int i = 0; i < numBlocks; i++)
|
||||||
{
|
{
|
||||||
|
ChipherBlock tmp = in[i];
|
||||||
m_ECBDecryption.ProcessData (out[i].buf, in[i].buf, 16);
|
m_ECBDecryption.ProcessData (out[i].buf, in[i].buf, 16);
|
||||||
out[i].ll[0] ^= m_IV.ll[0];
|
out[i].ll[0] ^= m_IV.ll[0];
|
||||||
out[i].ll[1] ^= m_IV.ll[1];
|
out[i].ll[1] ^= m_IV.ll[1];
|
||||||
m_IV = in[i];
|
m_IV = tmp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
aes.h
2
aes.h
|
@ -25,6 +25,7 @@ namespace crypto
|
||||||
void SetIV (uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes
|
void SetIV (uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes
|
||||||
|
|
||||||
void Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
void Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
||||||
|
bool Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -42,6 +43,7 @@ namespace crypto
|
||||||
void SetIV (uint8_t * iv) { memcpy (m_IV.buf, iv, 16); }; // 16 bytes
|
void SetIV (uint8_t * iv) { memcpy (m_IV.buf, iv, 16); }; // 16 bytes
|
||||||
|
|
||||||
void Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
void Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
|
||||||
|
bool Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue