ocb_done_decrypt.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_done_decrypt(ocb_state *ocb,
  15. const unsigned char *ct, unsigned long ctlen,
  16. unsigned char *pt,
  17. const unsigned char *tag, unsigned long taglen, int *res)
  18. {
  19. int err;
  20. unsigned char *tagbuf;
  21. unsigned long tagbuflen;
  22. _ARGCHK(ocb != NULL);
  23. _ARGCHK(pt != NULL);
  24. _ARGCHK(ct != NULL);
  25. _ARGCHK(tag != NULL);
  26. _ARGCHK(res != NULL);
  27. /* default to failed */
  28. *res = 0;
  29. /* allocate memory */
  30. tagbuf = XMALLOC(MAXBLOCKSIZE);
  31. if (tagbuf == NULL) {
  32. return CRYPT_MEM;
  33. }
  34. tagbuflen = MAXBLOCKSIZE;
  35. if ((err = __ocb_done(ocb, ct, ctlen, pt, tagbuf, &tagbuflen, 1)) != CRYPT_OK) {
  36. goto __ERR;
  37. }
  38. if (taglen <= tagbuflen && memcmp(tagbuf, tag, taglen) == 0) {
  39. *res = 1;
  40. }
  41. err = CRYPT_OK;
  42. __ERR:
  43. #ifdef CLEAN_STACK
  44. zeromem(tagbuf, MAXBLOCKSIZE);
  45. #endif
  46. XFREE(tagbuf);
  47. return err;
  48. }
  49. #endif