ocb_decrypt.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. /* OCB Implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef OCB_MODE
  14. int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt)
  15. {
  16. unsigned char Z[MAXBLOCKSIZE], tmp[MAXBLOCKSIZE];
  17. int err, x;
  18. _ARGCHK(ocb != NULL);
  19. _ARGCHK(pt != NULL);
  20. _ARGCHK(ct != NULL);
  21. /* check if valid cipher */
  22. if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
  23. return err;
  24. }
  25. _ARGCHK(cipher_descriptor[ocb->cipher].ecb_decrypt != NULL);
  26. /* check length */
  27. if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
  28. return CRYPT_INVALID_ARG;
  29. }
  30. /* Get Z[i] value */
  31. ocb_shift_xor(ocb, Z);
  32. /* xor ct in, encrypt, xor Z out */
  33. for (x = 0; x < ocb->block_len; x++) {
  34. tmp[x] = ct[x] ^ Z[x];
  35. }
  36. cipher_descriptor[ocb->cipher].ecb_decrypt(tmp, pt, &ocb->key);
  37. for (x = 0; x < ocb->block_len; x++) {
  38. pt[x] ^= Z[x];
  39. }
  40. /* compute checksum */
  41. for (x = 0; x < ocb->block_len; x++) {
  42. ocb->checksum[x] ^= pt[x];
  43. }
  44. #ifdef CLEAN_STACK
  45. zeromem(Z, sizeof(Z));
  46. zeromem(tmp, sizeof(tmp));
  47. #endif
  48. return CRYPT_OK;
  49. }
  50. #endif