Files
tomcrypt/src/modes/cbc/cbc_decrypt.c
T

92 lines
2.4 KiB
C
Raw Normal View History

2004-05-12 20:42:16 +00:00
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
* guarantee it works.
*
2005-04-17 11:37:13 +00:00
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
2004-05-12 20:42:16 +00:00
*/
2004-12-30 23:55:53 +00:00
#include "tomcrypt.h"
/**
@file cbc_decrypt.c
2005-04-17 11:37:13 +00:00
CBC implementation, encrypt block, Tom St Denis
2004-12-30 23:55:53 +00:00
*/
2004-05-12 20:42:16 +00:00
2005-04-17 11:37:13 +00:00
2004-05-12 20:42:16 +00:00
#ifdef CBC
2004-12-30 23:55:53 +00:00
/**
2005-04-17 11:37:13 +00:00
CBC decrypt
@param ct Ciphertext
@param pt [out] Plaintext
@param len The number of bytes to process (must be multiple of block length)
@param cbc CBC state
@return CRYPT_OK if successful
2004-12-30 23:55:53 +00:00
*/
2005-04-17 11:37:13 +00:00
int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc)
2004-05-12 20:42:16 +00:00
{
int x, err;
2005-04-17 11:37:13 +00:00
unsigned char tmp[16];
#ifdef LTC_FAST
LTC_FAST_TYPE tmpy;
#else
unsigned char tmpy;
#endif
2004-05-12 20:42:16 +00:00
2005-04-17 11:37:13 +00:00
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(cbc != NULL);
2004-05-12 20:42:16 +00:00
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
return err;
}
2005-04-17 11:37:13 +00:00
2004-05-12 20:42:16 +00:00
/* is blocklen valid? */
if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) {
return CRYPT_INVALID_ARG;
2005-04-17 11:37:13 +00:00
}
2004-05-12 20:42:16 +00:00
2005-04-17 11:37:13 +00:00
if (len % cbc->blocklen) {
return CRYPT_INVALID_ARG;
2004-05-12 20:42:16 +00:00
}
2005-04-17 11:37:13 +00:00
#ifdef LTC_FAST
if (len % sizeof(LTC_FAST_TYPE)) {
return CRYPT_INVALID_ARG;
}
#endif
if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
} else {
while (len) {
/* decrypt */
cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key);
2004-05-12 20:42:16 +00:00
2005-04-17 11:37:13 +00:00
/* xor IV against plaintext */
#if defined(LTC_FAST)
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
tmpy = *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) ^ *((LTC_FAST_TYPE*)((unsigned char *)tmp + x));
*((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) = *((LTC_FAST_TYPE*)((unsigned char *)ct + x));
*((LTC_FAST_TYPE*)((unsigned char *)pt + x)) = tmpy;
}
#else
for (x = 0; x < cbc->blocklen; x++) {
tmpy = tmp[x] ^ cbc->IV[x];
cbc->IV[x] = ct[x];
pt[x] = tmpy;
}
#endif
ct += cbc->blocklen;
pt += cbc->blocklen;
len -= cbc->blocklen;
}
2004-05-12 20:42:16 +00:00
}
return CRYPT_OK;
}
#endif