1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
15
CBC implementation, encrypt block, Tom St Denis
24
@param pt [out] Plaintext
25
@param len The number of bytes to process (must be multiple of block length)
27
@return CRYPT_OK if successful
29
int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc)
32
unsigned char tmp[16];
39
LTC_ARGCHK(pt != NULL);
40
LTC_ARGCHK(ct != NULL);
41
LTC_ARGCHK(cbc != NULL);
43
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
47
/* is blocklen valid? */
48
if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) {
49
return CRYPT_INVALID_ARG;
52
if (len % cbc->blocklen) {
53
return CRYPT_INVALID_ARG;
56
if (len % sizeof(LTC_FAST_TYPE)) {
57
return CRYPT_INVALID_ARG;
61
if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
62
cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
66
cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key);
68
/* xor IV against plaintext */
70
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
71
tmpy = *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) ^ *((LTC_FAST_TYPE*)((unsigned char *)tmp + x));
72
*((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) = *((LTC_FAST_TYPE*)((unsigned char *)ct + x));
73
*((LTC_FAST_TYPE*)((unsigned char *)pt + x)) = tmpy;
76
for (x = 0; x < cbc->blocklen; x++) {
77
tmpy = tmp[x] ^ cbc->IV[x];
93
/* $Source: /cvs/libtom/libtomcrypt/src/modes/cbc/cbc_decrypt.c,v $ */
94
/* $Revision: 1.9 $ */
95
/* $Date: 2005/05/05 14:35:59 $ */