hmac_memory.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. /* Submited by Dobes Vandermeer ([email protected]) */
  12. #include "mycrypt.h"
  13. #ifdef HMAC
  14. int hmac_memory(int hash, const unsigned char *key, unsigned long keylen,
  15. const unsigned char *data, unsigned long len,
  16. unsigned char *dst, unsigned long *dstlen)
  17. {
  18. hmac_state *hmac;
  19. int err;
  20. _ARGCHK(key != NULL);
  21. _ARGCHK(data != NULL);
  22. _ARGCHK(dst != NULL);
  23. _ARGCHK(dstlen != NULL);
  24. /* allocate ram for hmac state */
  25. hmac = XMALLOC(sizeof(hmac_state));
  26. if (hmac == NULL) {
  27. return CRYPT_MEM;
  28. }
  29. if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) {
  30. goto __ERR;
  31. }
  32. if ((err = hmac_process(hmac, data, len)) != CRYPT_OK) {
  33. goto __ERR;
  34. }
  35. if ((err = hmac_done(hmac, dst, dstlen)) != CRYPT_OK) {
  36. goto __ERR;
  37. }
  38. err = CRYPT_OK;
  39. __ERR:
  40. #ifdef CLEAN_STACK
  41. zeromem(hmac, sizeof(hmac_state));
  42. #endif
  43. XFREE(hmac);
  44. return err;
  45. }
  46. #endif