eax_done.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_done(eax_state *eax, unsigned char *tag, unsigned long *taglen)
  15. {
  16. int err;
  17. unsigned char *headermac, *ctmac;
  18. unsigned long x, len;
  19. _ARGCHK(eax != NULL);
  20. _ARGCHK(tag != NULL);
  21. _ARGCHK(taglen != NULL);
  22. /* allocate ram */
  23. headermac = XMALLOC(MAXBLOCKSIZE);
  24. ctmac = XMALLOC(MAXBLOCKSIZE);
  25. if (headermac == NULL || ctmac == NULL) {
  26. if (headermac != NULL) {
  27. XFREE(headermac);
  28. }
  29. if (ctmac != NULL) {
  30. XFREE(ctmac);
  31. }
  32. return CRYPT_MEM;
  33. }
  34. /* finish ctomac */
  35. len = MAXBLOCKSIZE;
  36. if ((err = omac_done(&eax->ctomac, ctmac, &len)) != CRYPT_OK) {
  37. goto __ERR;
  38. }
  39. /* finish headeromac */
  40. /* note we specifically don't reset len so the two lens are minimal */
  41. if ((err = omac_done(&eax->headeromac, headermac, &len)) != CRYPT_OK) {
  42. goto __ERR;
  43. }
  44. /* compute N xor H xor C */
  45. for (x = 0; x < len && x < *taglen; x++) {
  46. tag[x] = eax->N[x] ^ headermac[x] ^ ctmac[x];
  47. }
  48. *taglen = x;
  49. err = CRYPT_OK;
  50. __ERR:
  51. #ifdef CLEAN_STACK
  52. zeromem(ctmac, MAXBLOCKSIZE);
  53. zeromem(headermac, MAXBLOCKSIZE);
  54. zeromem(eax, sizeof(*eax));
  55. #endif
  56. XFREE(ctmac);
  57. XFREE(headermac);
  58. return err;
  59. }
  60. #endif