Bazaar/Protect : using new Cypher package instead of StreamCypher

Added Initialization Vectors handling in encryption

git-svn-id: svn://ultimatepp.org/upp/trunk@2739 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
micio 2010-10-03 20:44:12 +00:00
parent 34b1a0f02e
commit f2da9caeda
6 changed files with 29 additions and 20 deletions

View file

@ -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);
}

View file

@ -2,7 +2,7 @@
#define _Protect_h_
#include <Core/Core.h>
#include <StreamCypher/StreamCypher.h>
#include <Cypher/Cypher.h>
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"); \
Buffer<byte>buf(len); \
Buffer<byte>nonce(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

View file

@ -2,7 +2,7 @@ description "Software copy protection module\377";
uses
Core,
StreamCypher;
Cypher;
file
Protect.h,

View file

@ -1,5 +1,5 @@
#ifndef _Protect_icpp_init_stub
#define _Protect_icpp_init_stub
#include "Core/init"
#include "StreamCypher/init"
#include "Cypher/init"
#endif

View file

@ -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++;
}

View file

@ -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)