Exemplo n.º 1
0
void SrtpSymCrypto::encrypt(const uint8_t* input, uint8_t* output ) {
    if (algorithm == SrtpEncryptionAESCM || algorithm == SrtpEncryptionAESF8) {
        AES_encrypt(input, output, (AES_KEY *)key);
    }
    else if (algorithm == SrtpEncryptionTWOCM || algorithm == SrtpEncryptionTWOF8) {
        Twofish_encrypt((Twofish_key*)key, (Twofish_Byte*)input,
                        (Twofish_Byte*)output); 
    }
}
Exemplo n.º 2
0
int CTwofish::padEncrypt(RD_UINT8 *pInput, int nInputOctets, RD_UINT8 *pOutBuffer)
{
	int i, numBlocks, padLen;
	RD_UINT8 block[16], *iv;

	ASSERT((pInput != NULL) && (nInputOctets != NULL) && (pOutBuffer != NULL));
	if((pInput == NULL) || (nInputOctets <= 0) || (pOutBuffer == NULL)) return 0;

	numBlocks = nInputOctets / 16;

	iv = m_pInitVector;
	for(i = numBlocks; i > 0; i--)
	{
		((RD_UINT32*)block)[0] = ((RD_UINT32*)pInput)[0] ^ ((RD_UINT32*)iv)[0];
		((RD_UINT32*)block)[1] = ((RD_UINT32*)pInput)[1] ^ ((RD_UINT32*)iv)[1];
		((RD_UINT32*)block)[2] = ((RD_UINT32*)pInput)[2] ^ ((RD_UINT32*)iv)[2];
		((RD_UINT32*)block)[3] = ((RD_UINT32*)pInput)[3] ^ ((RD_UINT32*)iv)[3];

		Twofish_encrypt(&m_key, (Twofish_Byte *)block, (Twofish_Byte *)pOutBuffer);

		iv = pOutBuffer;
		pInput += 16;
		pOutBuffer += 16;
	}

	padLen = 16 - (nInputOctets - (16 * numBlocks));

	for (i = 0; i < 16 - padLen; i++)
	{
		block[i] = (RD_UINT8)(pInput[i] ^ iv[i]);
	}

	for (i = 16 - padLen; i < 16; i++)
	{
		block[i] = (RD_UINT8)((RD_UINT8)padLen ^ iv[i]);
	}

	Twofish_encrypt(&m_key, (Twofish_Byte *)block, (Twofish_Byte *)pOutBuffer);

	return 16 * (numBlocks + 1);
}
Exemplo n.º 3
0
void Twofish_cfb128_decrypt(Twofish_key* keyCtx, Twofish_Byte* in, 
			    Twofish_Byte* out, size_t len,
			    Twofish_Byte* ivec, int32_t *num)
{
  uint32_t n;

  n = *num;

  do {
    while (n && len) {
      unsigned char c;
      *(out++) = ivec[n] ^ (c = *(in++)); ivec[n] = c;
      --len;
      n = (n+1) % 16;
    }
    while (len>=16) {
      Twofish_encrypt(keyCtx, ivec, ivec);
      for (n=0; n<16; n+=sizeof(size_t)) {
	size_t t = *(size_t*)(in+n);
	*(size_t*)(out+n) = *(size_t*)(ivec+n) ^ t;
	*(size_t*)(ivec+n) = t;
      }
      len -= 16;
      out += 16;
      in  += 16;
    }
    n = 0;
    if (len) {
      Twofish_encrypt(keyCtx, ivec, ivec);
      while (len--) {
	unsigned char c;
	out[n] = ivec[n] ^ (c = in[n]); ivec[n] = c;
	++n;
      }
    }
    *num = n;
    return;
  } while (0);
}