Browse Source

eliminated multiple of 16 check for AES

pull/72/head
orignal 11 years ago
parent
commit
a6cc2e647b
  1. 2
      SSU.cpp
  2. 15
      aes.cpp
  3. 4
      aes.h

2
SSU.cpp

@ -524,7 +524,6 @@ namespace ssu
header->time = htobe32 (i2p::util::GetSecondsSinceEpoch ()); header->time = htobe32 (i2p::util::GetSecondsSinceEpoch ());
uint8_t * encrypted = &header->flag; uint8_t * encrypted = &header->flag;
uint16_t encryptedLen = len - (encrypted - buf); 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); m_SessionKeyEncryption.Encrypt (encrypted, encryptedLen, encrypted);
// assume actual buffer size is 18 (16 + 2) bytes more // assume actual buffer size is 18 (16 + 2) bytes more
memcpy (buf + len, header->iv, 16); memcpy (buf + len, header->iv, 16);
@ -557,7 +556,6 @@ namespace ssu
SSUHeader * header = (SSUHeader *)buf; SSUHeader * header = (SSUHeader *)buf;
uint8_t * encrypted = &header->flag; uint8_t * encrypted = &header->flag;
uint16_t encryptedLen = len - (encrypted - buf); uint16_t encryptedLen = len - (encrypted - buf);
encryptedLen = (encryptedLen>>4)<<4; // make sure 16 bytes boundary
if (encryptedLen > 0) if (encryptedLen > 0)
{ {
m_SessionKeyDecryption.SetIV (header->iv); m_SessionKeyDecryption.SetIV (header->iv);

15
aes.cpp

@ -197,12 +197,10 @@ namespace crypto
#endif #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); // len/16
if (d.rem) return false; // len is not multipple of 16 Encrypt (len >> 4, (const ChipherBlock *)in, (ChipherBlock *)out);
Encrypt (d.quot, (const ChipherBlock *)in, (ChipherBlock *)out);
return true;
} }
void CBCEncryption::Encrypt (const uint8_t * in, uint8_t * out) void CBCEncryption::Encrypt (const uint8_t * in, uint8_t * out)
@ -260,12 +258,9 @@ namespace crypto
#endif #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); Decrypt (len >> 4, (const ChipherBlock *)in, (ChipherBlock *)out);
if (d.rem) return false; // len is not multiple of 16
Decrypt (d.quot, (const ChipherBlock *)in, (ChipherBlock *)out);
return true;
} }
void CBCDecryption::Decrypt (const uint8_t * in, uint8_t * out) void CBCDecryption::Decrypt (const uint8_t * in, uint8_t * out)

4
aes.h

@ -109,7 +109,7 @@ namespace crypto
void SetIV (const uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes void SetIV (const 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); void Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
void Encrypt (const uint8_t * in, uint8_t * out); // one block void Encrypt (const uint8_t * in, uint8_t * out); // one block
private: private:
@ -129,7 +129,7 @@ namespace crypto
void SetIV (const uint8_t * iv) { memcpy (m_IV.buf, iv, 16); }; // 16 bytes void SetIV (const 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); void Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
void Decrypt (const uint8_t * in, uint8_t * out); // one block void Decrypt (const uint8_t * in, uint8_t * out); // one block
private: private:

Loading…
Cancel
Save