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
13 CBC implementation, encrypt block, Tom St Denis
22 @param pt [out] Plaintext
23 @param len The number of bytes to process (must be multiple of block length)
25 @return CRYPT_OK if successful
27 int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc)
30 unsigned char tmp[16];
37 LTC_ARGCHK(pt != NULL);
38 LTC_ARGCHK(ct != NULL);
39 LTC_ARGCHK(cbc != NULL);
41 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
45 /* is blocklen valid? */
46 if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV) || cbc->blocklen > (int)sizeof(tmp)) {
47 return CRYPT_INVALID_ARG;
50 if (len % cbc->blocklen) {
51 return CRYPT_INVALID_ARG;
54 if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
55 return CRYPT_INVALID_ARG;
59 if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
60 return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
64 if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
68 /* xor IV against plaintext */
70 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
71 tmpy = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)tmp + x));
72 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
73 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) = tmpy;
76 for (x = 0; x < cbc->blocklen; x++) {
77 tmpy = tmp[x] ^ cbc->IV[x];
93 /* ref: $Format:%D$ */
94 /* git commit: $Format:%H$ */
95 /* commit time: $Format:%ai$ */