eax_decrypt_verify_memory.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /* EAX Implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef EAX_MODE
  14. int eax_decrypt_verify_memory(int cipher,
  15. const unsigned char *key, unsigned long keylen,
  16. const unsigned char *nonce, unsigned long noncelen,
  17. const unsigned char *header, unsigned long headerlen,
  18. const unsigned char *ct, unsigned long ctlen,
  19. unsigned char *pt,
  20. unsigned char *tag, unsigned long taglen,
  21. int *res)
  22. {
  23. int err;
  24. eax_state *eax;
  25. unsigned char *buf;
  26. unsigned long buflen;
  27. _ARGCHK(res != NULL);
  28. /* default to zero */
  29. *res = 0;
  30. /* allocate ram */
  31. buf = XMALLOC(taglen);
  32. eax = XMALLOC(sizeof(eax_state));
  33. if (eax == NULL || buf == NULL) {
  34. if (eax != NULL) {
  35. XFREE(eax);
  36. }
  37. if (buf != NULL) {
  38. XFREE(buf);
  39. }
  40. return CRYPT_MEM;
  41. }
  42. if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
  43. goto __ERR;
  44. }
  45. if ((err = eax_decrypt(eax, ct, pt, ctlen)) != CRYPT_OK) {
  46. goto __ERR;
  47. }
  48. buflen = taglen;
  49. if ((err = eax_done(eax, buf, &buflen)) != CRYPT_OK) {
  50. goto __ERR;
  51. }
  52. /* compare tags */
  53. if (buflen >= taglen && memcmp(buf, tag, taglen) == 0) {
  54. *res = 1;
  55. }
  56. err = CRYPT_OK;
  57. __ERR:
  58. #ifdef CLEAN_STACK
  59. zeromem(buf, taglen);
  60. zeromem(eax, sizeof(eax_state));
  61. #endif
  62. XFREE(eax);
  63. XFREE(buf);
  64. return err;
  65. }
  66. #endif