cfb_decrypt.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. #include "mycrypt.h"
  12. #ifdef CFB
  13. int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb)
  14. {
  15. int err;
  16. _ARGCHK(pt != NULL);
  17. _ARGCHK(ct != NULL);
  18. _ARGCHK(cfb != NULL);
  19. if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
  20. return err;
  21. }
  22. /* is blocklen/padlen valid? */
  23. if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) ||
  24. cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) {
  25. return CRYPT_INVALID_ARG;
  26. }
  27. while (len-- > 0) {
  28. if (cfb->padlen == cfb->blocklen) {
  29. cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key);
  30. cfb->padlen = 0;
  31. }
  32. cfb->pad[cfb->padlen] = *ct;
  33. *pt = *ct ^ cfb->IV[cfb->padlen];
  34. ++pt;
  35. ++ct;
  36. ++cfb->padlen;
  37. }
  38. return CRYPT_OK;
  39. }
  40. #endif