void EncryptCBC::encrypt_block(const uint8_t src[16], uint8_t dst[16])
{
    if (src != dst) cbc_copy(src, dst);

    cbc_xor(l_cbciv, dst);
    serpent_encrypt((const uint32_t*)dst, (uint32_t*)dst, l_key);
    cbc_copy(dst, l_cbciv);
}
void DecryptCBC::decrypt_block(const uint8_t src[16], uint8_t dst[16])
{
    cbc_copy(src, l_cbcivsave); // this is needed if (src == dst)

    serpent_decrypt((const uint32_t*)src, (uint32_t*)dst, l_key);

    cbc_xor(l_cbciv, dst);
    cbc_copy(l_cbcivsave, l_cbciv);
}
Пример #3
0
static size_t
cbc_encrypt(crypt_mode_t *mode, const void *indata, void *outdata, size_t n, int eof)
{
	crypt_cipher_t *cipher = mode->cipher;
	size_t blksz = mode->cipher->blockSize;
	const uint8_t *src = indata;
	uint8_t *dst = outdata;
	uint8_t *prev = mode->em_buf;

	if (n == 0)
		return 0;
	if (mode->direction != KCL_DIRECTION_ENCRYPTION) {
		(*cipher->keysched)(cipher->ctx, KCL_DIRECTION_ENCRYPTION);
		mode->direction = KCL_DIRECTION_ENCRYPTION;
	}
	while (n >= blksz) {
		cbc_xor(prev, src, blksz);
		(*cipher->process)(cipher->ctx, prev, dst);
		memcpy(prev, dst, blksz);
		n -= blksz;
		src += blksz;
		dst += blksz;
	}
	if (eof && mode->padding) {
		/* process the padding method defined in RFC2630 */
		uint8_t pad[CRYPT_MAX_BLOCKSIZE];
		uint8_t padbyte = blksz - n;
		unsigned int i;
		for (i = 0; i < n; i++)
			pad[i] = src[i];
		for (; i < blksz; i++)
			pad[i] = padbyte;
		cbc_xor(prev, pad, blksz);
		(*cipher->process)(cipher->ctx, prev, dst);
		dst += blksz;
	}
	return dst - (uint8_t *)outdata;
}