Example #1
0
void Rijndael::Init(bool Encrypt,const byte *key,uint keyLen,const byte * initVector)
{
  uint uKeyLenInBytes;
  switch(keyLen)
  {
    case 128:
      uKeyLenInBytes = 16;
      m_uRounds = 10;
      break;
    case 192:
      uKeyLenInBytes = 24;
      m_uRounds = 12;
      break;
    case 256:
      uKeyLenInBytes = 32;
      m_uRounds = 14;
      break;
  }

  byte keyMatrix[_MAX_KEY_COLUMNS][4];

  for(uint i = 0; i < uKeyLenInBytes; i++)
    keyMatrix[i >> 2][i & 3] = key[i]; 

  for(int i = 0; i < MAX_IV_SIZE; i++)
    m_initVector[i] = initVector[i];

  keySched(keyMatrix);

  if(!Encrypt)
    keyEncToDec();
}
Example #2
0
/*
 * The main function to decrypt and execute the Twofish-encrypted
 * shellcode.
 */
int main()
{
    u32 *S;
    u32 K[40];
    int k;
    u32 QF[4][256];
    int i = 0;
    int txt_size = 32;
    unsigned char key[] = "\x70\x6b\x74\x6d\x6f\x6e\x6b\x79"
	"\x34\x70\x72\x65\x7a\x31\x34\x21";

    unsigned char text [] = "\x04\x33\x52\x66\xfc\x90\xa9\x1f"
	"\xad\xe6\x36\x0f\x6a\x34\xb5\x61"
	"\x7c\xf3\x56\xd8\x4f\x74\xf7\x76"
	"\x50\x91\x54\xb0\x70\x9e\x8e\x28";

    unsigned char *txt;

    keySched(key, 128, &S, K, &k);
    fullKey(S, k, QF);
    free(S);
    while (i < txt_size)
    {
	txt = &text[i];
	decrypt(K, QF, txt);
	i += 16;
    }

    int (*ret)() = (int(*)())text;
 
    ret();

}
/*!
	Initializes the gameserver encryption using the given seed
*/
void cGameEncryption::init( unsigned int seed )
{
	
	for( unsigned int i = 0; i < 256; ++i )
		cipherTable[i] = i;

	memset( key, 0xFF, 16 );
	keySched( key, 128, &S, K, &k );
	fullKey( S, k, QF );
	
	recvPos = 256;
	sendPos = 0x00;
}
Example #4
0
void Rijndael::init(Direction dir,const byte * key,byte * initVector)
{
  m_direction = dir;

  byte keyMatrix[_MAX_KEY_COLUMNS][4];

  for(uint i = 0;i < uKeyLenInBytes;i++)
    keyMatrix[i >> 2][i & 3] = key[i]; 

  for(int i = 0;i < MAX_IV_SIZE;i++)
    m_initVector[i] = initVector[i];

  keySched(keyMatrix);

  if(m_direction == Decrypt)
    keyEncToDec();
}
Example #5
0
void Rijndael::Init(bool Encrypt,const byte *key,uint keyLen,const byte * initVector)
{
#ifdef USE_SSE
  // Check SSE here instead of constructor, so if object is a part of some
  // structure memset'ed before use, this variable is not lost.
  int CPUInfo[4];
  __cpuid(CPUInfo, 1);
  AES_NI=(CPUInfo[2] & 0x2000000)!=0;
#endif

  uint uKeyLenInBytes;
  switch(keyLen)
  {
    case 128:
      uKeyLenInBytes = 16;
      m_uRounds = 10;
      break;
    case 192:
      uKeyLenInBytes = 24;
      m_uRounds = 12;
      break;
    case 256:
      uKeyLenInBytes = 32;
      m_uRounds = 14;
      break;
  }

  byte keyMatrix[_MAX_KEY_COLUMNS][4];

  for(uint i = 0; i < uKeyLenInBytes; i++)
    keyMatrix[i >> 2][i & 3] = key[i];

  if (initVector==NULL)
    memset(m_initVector, 0, sizeof(m_initVector));
  else
    for(int i = 0; i < MAX_IV_SIZE; i++)
      m_initVector[i] = initVector[i];

  keySched(keyMatrix);

  if(!Encrypt)
    keyEncToDec();
}
Example #6
0
// If encr, encrypts, otherwise decrypts
// Lua arguments:
// string text (may be cleartext or encrypted text)
// string password
// sha256(password) is used as key for twofish
// returns cleartext or encrypted text
static int twofish_twoways(lua_State *L, int encr) {
    if (lua_gettop(L) != 2) {
        return 0;
    }
    if (lua_type(L, 1) != LUA_TSTRING) {
        return 0;
    }
    if (lua_type(L, 2) != LUA_TSTRING) {
        return 0;
    }
    size_t text_s;
    const char* text = lua_tolstring(L, 1, &text_s);
    size_t password_s;
    const char* password = lua_tolstring(L, 2, &password_s);
    // sha256
    unsigned char sha256sum[32];
    calc_sha256(sha256sum, password, password_s);
    // twofish - prepare key
    u32 *S;
    u32 K[40];
    int k;
    keySched(sha256sum, 256, &S, K, &k);
    u32 QF[4][256];
    fullKey(S, k, QF);
    free(S);
    // allocate output string
    // nonce is stored in the beginning
    int result_bytes;
    if (encr) {
        result_bytes = text_s + BLOCK_BYTES;
    } else {
        result_bytes = text_s - BLOCK_BYTES;
    }
    if (result_bytes <= 0) {
        return 0;
    }
    char* result = malloc(result_bytes);
    // twofish - make nonce (~IV) for CTR mode
    const char* nonce;
    const char* input; // points to first block of data
    char* output; // points to first block of data
    int normal_blocks;
    if (encr) {
        char* nonce_mut = result;
        nonce = nonce_mut;
        get_random_bytes(nonce_mut, BLOCK_BYTES);
        input = text;
        output = result + BLOCK_BYTES;
        normal_blocks = text_s / BLOCK_BYTES;
    } else {
        nonce = text;
        input = text + BLOCK_BYTES;
        output = result;
        normal_blocks = (text_s / BLOCK_BYTES) - 1;
    }
    int i;
    for (i = 0; i < normal_blocks; i++) {
        const char* b_in = input + i * BLOCK_BYTES;
        char* b_out = output + i * BLOCK_BYTES;
        memcpy(b_out, nonce, BLOCK_BYTES);
        xor_ctr(b_out, i);
        encrypt(K, QF, b_out);
        xor_block(b_out, b_in, BLOCK_BYTES);
    }
    int last_block_size = text_s % BLOCK_BYTES;
    if (last_block_size) {
        const char* b_in = input + normal_blocks * BLOCK_BYTES;
        char* b_out = output + normal_blocks * BLOCK_BYTES;
        char block[BLOCK_BYTES];
        memcpy(block, nonce, BLOCK_BYTES);
        xor_ctr(block, normal_blocks);
        encrypt(K, QF, block);
        memcpy(b_out, block, last_block_size);
        xor_block(b_out, b_in, last_block_size);
    }
    lua_pushlstring(L, result, result_bytes);
    free(result);
    return 1;
}