fixed decrypting loop

This commit is contained in:
Jack Andersen 2015-07-02 15:57:31 -10:00
parent b461f63ae4
commit 84ca6a62e3
3 changed files with 17 additions and 14 deletions

View File

@ -11,8 +11,8 @@ namespace NOD
class IAES class IAES
{ {
public: public:
virtual void encrypt(uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)=0; virtual void encrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)=0;
virtual void decrypt(uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)=0; virtual void decrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)=0;
virtual void setKey(const uint8_t* key)=0; virtual void setKey(const uint8_t* key)=0;
}; };

View File

@ -290,9 +290,10 @@ public:
size_t block = m_offset / 0x7c00; size_t block = m_offset / 0x7c00;
size_t cacheOffset = m_offset % 0x7c00; size_t cacheOffset = m_offset % 0x7c00;
uint64_t cacheSize; uint64_t cacheSize;
uint64_t rem = length;
uint8_t* dst = (uint8_t*)buf; uint8_t* dst = (uint8_t*)buf;
while (length) while (rem)
{ {
if (block != m_curBlock) if (block != m_curBlock)
{ {
@ -300,13 +301,13 @@ public:
m_curBlock = block; m_curBlock = block;
} }
cacheSize = length; cacheSize = rem;
if (cacheSize + cacheOffset > 0x7c00) if (cacheSize + cacheOffset > 0x7c00)
cacheSize = 0x7c00 - cacheOffset; cacheSize = 0x7c00 - cacheOffset;
memcpy(dst, m_decBuf + cacheOffset, cacheSize); memcpy(dst, m_decBuf + cacheOffset, cacheSize);
dst += cacheSize; dst += cacheSize;
length -= cacheSize; rem -= cacheSize;
cacheOffset = 0; cacheOffset = 0;
++block; ++block;
} }

View File

@ -76,8 +76,8 @@ protected:
void _decrypt(uint8_t* buff); void _decrypt(uint8_t* buff);
public: public:
void encrypt(uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len); void encrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len);
void decrypt(uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len); void decrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len);
void setKey(const uint8_t* key); void setKey(const uint8_t* key);
}; };
@ -400,10 +400,10 @@ void SoftwareAES::setKey(const uint8_t* key)
} }
// CBC mode decryption // CBC mode decryption
void SoftwareAES::decrypt(uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, size_t len) void SoftwareAES::decrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, size_t len)
{ {
uint8_t block[16]; uint8_t block[16];
uint8_t* ctext_ptr; const uint8_t* ctext_ptr;
unsigned int blockno = 0, i; unsigned int blockno = 0, i;
//fprintf( stderr,"aes_decrypt(%p, %p, %p, %lld)\n", iv, inbuf, outbuf, len ); //fprintf( stderr,"aes_decrypt(%p, %p, %p, %lld)\n", iv, inbuf, outbuf, len );
@ -440,9 +440,11 @@ void SoftwareAES::decrypt(uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, si
} }
// CBC mode encryption // CBC mode encryption
void SoftwareAES::encrypt(uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len) void SoftwareAES::encrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)
{ {
uint8_t block[16]; uint8_t block[16];
uint8_t feedback[16];
memcpy(feedback, iv, 16);
unsigned int blockno = 0, i; unsigned int blockno = 0, i;
//printf("aes_decrypt(%p, %p, %p, %lld)\n", iv, inbuf, outbuf, len); //printf("aes_decrypt(%p, %p, %p, %lld)\n", iv, inbuf, outbuf, len);
@ -466,10 +468,10 @@ void SoftwareAES::encrypt(uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, ui
memcpy(block, inbuf + blockno * sizeof(block), fraction); memcpy(block, inbuf + blockno * sizeof(block), fraction);
for (i = 0; i < fraction; i++) for (i = 0; i < fraction; i++)
block[i] = inbuf[blockno * sizeof(block) + i] ^ iv[i]; block[i] = inbuf[blockno * sizeof(block) + i] ^ feedback[i];
_encrypt(block); _encrypt(block);
memcpy(iv, block, sizeof(block)); memcpy(feedback, block, sizeof(block));
memcpy(outbuf + blockno * sizeof(block), block, sizeof(block)); memcpy(outbuf + blockno * sizeof(block), block, sizeof(block));
// debug_printf("Block %d output: ", blockno); // debug_printf("Block %d output: ", blockno);
// hexdump(outbuf + blockno*sizeof(block), 16); // hexdump(outbuf + blockno*sizeof(block), 16);
@ -485,7 +487,7 @@ class NiAES : public IAES
__m128i m_ekey[11]; __m128i m_ekey[11];
__m128i m_dkey[11]; __m128i m_dkey[11];
public: public:
void encrypt(uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len) void encrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)
{ {
__m128i feedback,data; __m128i feedback,data;
uint64_t i,j; uint64_t i,j;
@ -505,7 +507,7 @@ public:
_mm_storeu_si128(&((__m128i*)outbuf)[i], feedback); _mm_storeu_si128(&((__m128i*)outbuf)[i], feedback);
} }
} }
void decrypt(uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len) void decrypt(const uint8_t* iv, const uint8_t* inbuf, uint8_t* outbuf, uint64_t len)
{ {
__m128i data,feedback,last_in; __m128i data,feedback,last_in;
uint64_t i,j; uint64_t i,j;