diff --git a/bazaar/Protect/Protect.cpp b/bazaar/Protect/Protect.cpp index bef458946..9de949406 100644 --- a/bazaar/Protect/Protect.cpp +++ b/bazaar/Protect/Protect.cpp @@ -27,11 +27,11 @@ bool PROTECT_WRITE_ACCESS(byte *start, size_t size, bool access) } #endif -void PROTECT_DECRYPT(byte *start, size_t size, String const &key) +void PROTECT_DECRYPT(byte *start, size_t size, String const &key, byte const *nonce, size_t nonceLen) { - Snow2 snow2(key); + Snow2 snow2((byte const *)~key, key.GetCount(), nonce, nonceLen); - snow2.Encode(start, size); + snow2(start, size); } void PROTECT_OBFUSCATE(byte *start, size_t len, byte *key, size_t keyLen) @@ -41,7 +41,7 @@ void PROTECT_OBFUSCATE(byte *start, size_t len, byte *key, size_t keyLen) k += *key++; if(!PROTECT_WRITE_ACCESS(start, len, true)) return; - Snow2 snow2(k); - snow2.Encode(start, len); + Snow2 snow2(k, "12345678"); + snow2(start, len); PROTECT_WRITE_ACCESS(start, len, false); } diff --git a/bazaar/Protect/Protect.h b/bazaar/Protect/Protect.h index 43662a2c1..f422535ea 100644 --- a/bazaar/Protect/Protect.h +++ b/bazaar/Protect/Protect.h @@ -2,7 +2,7 @@ #define _Protect_h_ #include -#include +#include using namespace Upp; @@ -21,7 +21,7 @@ using namespace Upp; if(!__decrypted) \ { \ PROTECT_WRITE_ACCESS((byte *)&&__start, (byte *)&&__end - (byte *)&&__start, true); \ - decrFunc((byte *)&&__start, (byte *)&&__end - (byte *)&&__start); \ + decrFunc((byte *)&&__start, (byte *)&&__end - (byte *)&&__start, (byte *)&&__init + 2, 6 /* sizeof(PROTECT_START_MARK) */); \ PROTECT_WRITE_ACCESS((byte *)&&__start, (byte *)&&__end - (byte *)&&__start, false); \ __decrypted = true; \ asm volatile ( \ @@ -34,6 +34,7 @@ using namespace Upp; } \ if(!__decrypted) \ goto __end; \ + __init: \ asm volatile( \ "\tjmp 1f\n" \ "\t.ascii \""PROTECT_START_MARKER"\"\n" \ @@ -89,7 +90,7 @@ using namespace Upp; #define PROTECT_START_FUNC(decrFunc) \ static bool __decrypted = false; \ - byte *__startPtr, *__endPtr; \ + byte *__startPtr, *__endPtr, *__noncePtr; \ if(!__decrypted) \ { \ __asm \ @@ -99,10 +100,14 @@ using namespace Upp; __asm mov __startPtr, eax \ __asm lea eax, __end \ __asm mov __endPtr, eax \ + __asm lea eax, __init \ + __asm inc eax \ + __asm inc eax \ + __asm mov __noncePtr, eax \ __asm pop eax \ }; \ PROTECT_WRITE_ACCESS(__startPtr, __endPtr - __startPtr, true); \ - decrFunc(__startPtr, __endPtr - __startPtr); \ + decrFunc(__startPtr, __endPtr - __startPtr, __noncePtr, 6); \ PROTECT_WRITE_ACCESS(__startPtr, __endPtr - __startPtr, false); \ __decrypted = true; \ __asm \ @@ -121,6 +126,7 @@ using namespace Upp; } \ if(!__decrypted) \ goto __end; \ + __init: \ __asm { \ __asm jmp __next \ _PROTECT_START_MARKER \ @@ -195,8 +201,10 @@ using namespace Upp; const char *crypted = PROTECT_START_MARKER "abracadabra" PROTECT_END_MARKER; \ const int len = strlen("abracadabra"); \ Bufferbuf(len); \ + Buffernonce(6); \ memcpy(buf, crypted + 6 /* sizeof(PROTECT_START_MARKER)*/, len); \ - decrFunc(buf, len); \ + memcpy(nonce, crypted, 6); \ + decrFunc(buf, len, nonce, 6); \ __keyOk = !memcmp(buf, "abracadabra", len); \ } \ if(!__keyOk) @@ -212,7 +220,7 @@ using namespace Upp; #endif bool PROTECT_WRITE_ACCESS(byte *start, size_t size, bool access); -void PROTECT_DECRYPT(byte *start, size_t size, String const &key); +void PROTECT_DECRYPT(byte *start, size_t size, String const &key, byte const *nonce, size_t nonceLen); void PROTECT_OBFUSCATE(byte *start, size_t len, byte *key, size_t keyLen); #endif diff --git a/bazaar/Protect/Protect.upp b/bazaar/Protect/Protect.upp index 9980a46ac..02ae387cb 100644 --- a/bazaar/Protect/Protect.upp +++ b/bazaar/Protect/Protect.upp @@ -2,7 +2,7 @@ description "Software copy protection module\377"; uses Core, - StreamCypher; + Cypher; file Protect.h, diff --git a/bazaar/Protect/init b/bazaar/Protect/init index 9aeddb1f2..7cf31ff7a 100644 --- a/bazaar/Protect/init +++ b/bazaar/Protect/init @@ -1,5 +1,5 @@ #ifndef _Protect_icpp_init_stub #define _Protect_icpp_init_stub #include "Core/init" -#include "StreamCypher/init" +#include "Cypher/init" #endif diff --git a/bazaar/ProtectEncrypt/ProtectEncrypt.cpp b/bazaar/ProtectEncrypt/ProtectEncrypt.cpp index 020352f60..9874c8458 100644 --- a/bazaar/ProtectEncrypt/ProtectEncrypt.cpp +++ b/bazaar/ProtectEncrypt/ProtectEncrypt.cpp @@ -31,7 +31,8 @@ int CryptBuf(byte *buf, byte *bufEnd, String const &key) while( (bStart = ProtectSearchBuf(bStart, bufEnd, (const byte *)PROTECT_START_MARKER, strlen(PROTECT_START_MARKER))) != NULL) { // overwrite start pattern, just to fool a bit - // symple pattern search + // simple pattern search and use it as the encrypt init vector + byte *nonce = bStart; for(unsigned i = 0; i < strlen(PROTECT_START_MARKER); i++) *bStart++ = (byte)(Random() & 0xff); @@ -49,8 +50,8 @@ int CryptBuf(byte *buf, byte *bufEnd, String const &key) *bEnd++ = (byte)(Random() & 0xff); // crypt buffer - Snow2 snow2(key); - snow2.Encode(bStart, bStart, size); + Snow2 snow2((byte const *)~key, key.GetCount(), nonce, strlen(PROTECT_START_MARKER)); + snow2(bStart, size); patches++; } @@ -88,8 +89,8 @@ int ObfuscateBuf(byte *buf, byte *bufEnd) *bEnd++ = (byte)(Random() & 0xff); // obfuscate buffer - Snow2 snow2(key); - snow2.Encode(bStart, bStart, size); + Snow2 snow2(key, "12345678"); + snow2(bStart, size); patches++; } diff --git a/bazaar/ProtectTest/main.cpp b/bazaar/ProtectTest/main.cpp index 7c6406f60..8462eab21 100644 --- a/bazaar/ProtectTest/main.cpp +++ b/bazaar/ProtectTest/main.cpp @@ -14,9 +14,9 @@ String GetKey(void) return k; } -void Decrypt(byte *start, size_t len) +void Decrypt(byte *start, size_t len, byte const *nonce, size_t nonceLen) { - PROTECT_DECRYPT ( start, len, GetKey() ); + PROTECT_DECRYPT ( start, len, GetKey(), nonce, nonceLen ); } double CryptedTest(double d, double e)