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

70 lines
1.7 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.
*
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
*/
2004-12-30 23:55:53 +00:00
#include "tomcrypt.h"
/**
@file cbc_decrypt.c
CBC implementation, decrypt block, Tom St Denis
*/
2004-05-12 20:42:16 +00:00
#ifdef CBC
2004-12-30 23:55:53 +00:00
/**
CBC decrypt
@param ct Ciphertext
@param pt [out] Plaintext
@param cbc CBC state
@return CRYPT_OK if successful
*/
2004-05-12 20:42:16 +00:00
int cbc_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_CBC *cbc)
{
int x, err;
unsigned char tmp[MAXBLOCKSIZE], tmp2[MAXBLOCKSIZE];
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
LTC_ARGCHK(cbc != NULL);
2004-05-12 20:42:16 +00:00
/* decrypt the block from ct into tmp */
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
return err;
}
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(cipher_descriptor[cbc->cipher].ecb_decrypt != NULL);
2004-05-31 02:36:47 +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;
}
2004-05-31 02:36:47 +00:00
/* decrypt and xor IV against the plaintext of the previous step */
cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key);
2004-05-12 20:42:16 +00:00
for (x = 0; x < cbc->blocklen; x++) {
/* copy CT in case ct == pt */
tmp2[x] = ct[x];
/* actually decrypt the byte */
pt[x] = tmp[x] ^ cbc->IV[x];
}
/* replace IV with this current ciphertext */
for (x = 0; x < cbc->blocklen; x++) {
cbc->IV[x] = tmp2[x];
}
2004-12-30 23:55:53 +00:00
#ifdef LTC_CLEAN_STACK
2004-05-12 20:42:16 +00:00
zeromem(tmp, sizeof(tmp));
zeromem(tmp2, sizeof(tmp2));
#endif
return CRYPT_OK;
}
#endif