Skip to content

Commit

Permalink
eliminated multiple of 16 check for AES
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jun 8, 2014
1 parent eb44fdf commit a6cc2e6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
2 changes: 0 additions & 2 deletions SSU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ namespace ssu
header->time = htobe32 (i2p::util::GetSecondsSinceEpoch ());
uint8_t * encrypted = &header->flag;
uint16_t encryptedLen = len - (encrypted - buf);
encryptedLen = (encryptedLen>>4)<<4; // make sure 16 bytes boundary, TODO: do we really need it?
m_SessionKeyEncryption.Encrypt (encrypted, encryptedLen, encrypted);
// assume actual buffer size is 18 (16 + 2) bytes more
memcpy (buf + len, header->iv, 16);
Expand Down Expand Up @@ -557,7 +556,6 @@ namespace ssu
SSUHeader * header = (SSUHeader *)buf;
uint8_t * encrypted = &header->flag;
uint16_t encryptedLen = len - (encrypted - buf);
encryptedLen = (encryptedLen>>4)<<4; // make sure 16 bytes boundary
if (encryptedLen > 0)
{
m_SessionKeyDecryption.SetIV (header->iv);
Expand Down
15 changes: 5 additions & 10 deletions aes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,10 @@ namespace crypto
#endif
}

bool CBCEncryption::Encrypt (const uint8_t * in, std::size_t len, uint8_t * out)
void 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;
// len/16
Encrypt (len >> 4, (const ChipherBlock *)in, (ChipherBlock *)out);
}

void CBCEncryption::Encrypt (const uint8_t * in, uint8_t * out)
Expand Down Expand Up @@ -260,12 +258,9 @@ namespace crypto
#endif
}

bool CBCDecryption::Decrypt (const uint8_t * in, std::size_t len, uint8_t * out)
void 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 multiple of 16
Decrypt (d.quot, (const ChipherBlock *)in, (ChipherBlock *)out);
return true;
Decrypt (len >> 4, (const ChipherBlock *)in, (ChipherBlock *)out);
}

void CBCDecryption::Decrypt (const uint8_t * in, uint8_t * out)
Expand Down
4 changes: 2 additions & 2 deletions aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ namespace crypto
void SetIV (const uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes

void Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
bool Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
void Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
void Encrypt (const uint8_t * in, uint8_t * out); // one block

private:
Expand All @@ -129,7 +129,7 @@ namespace crypto
void SetIV (const uint8_t * iv) { memcpy (m_IV.buf, iv, 16); }; // 16 bytes

void Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out);
bool Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
void Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
void Decrypt (const uint8_t * in, uint8_t * out); // one block

private:
Expand Down

0 comments on commit a6cc2e6

Please sign in to comment.