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@iahu.ca, http://libtomcrypt.org
15
int cbc_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_CBC *cbc)
18
unsigned char tmp[MAXBLOCKSIZE], tmp2[MAXBLOCKSIZE];
24
/* decrypt the block from ct into tmp */
25
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
28
_ARGCHK(cipher_descriptor[cbc->cipher].ecb_decrypt != NULL);
30
/* is blocklen valid? */
31
if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) {
32
return CRYPT_INVALID_ARG;
35
/* decrypt and xor IV against the plaintext of the previous step */
36
cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key);
37
for (x = 0; x < cbc->blocklen; x++) {
38
/* copy CT in case ct == pt */
41
/* actually decrypt the byte */
42
pt[x] = tmp[x] ^ cbc->IV[x];
45
/* replace IV with this current ciphertext */
46
for (x = 0; x < cbc->blocklen; x++) {
50
zeromem(tmp, sizeof(tmp));
51
zeromem(tmp2, sizeof(tmp2));