hash_memory.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #include "mycrypt.h"
  12. int hash_memory(int hash, const unsigned char *data, unsigned long len, unsigned char *dst, unsigned long *outlen)
  13. {
  14. hash_state *md;
  15. int err;
  16. _ARGCHK(data != NULL);
  17. _ARGCHK(dst != NULL);
  18. _ARGCHK(outlen != NULL);
  19. if ((err = hash_is_valid(hash)) != CRYPT_OK) {
  20. return err;
  21. }
  22. if (*outlen < hash_descriptor[hash].hashsize) {
  23. return CRYPT_BUFFER_OVERFLOW;
  24. }
  25. md = XMALLOC(sizeof(hash_state));
  26. if (md == NULL) {
  27. return CRYPT_MEM;
  28. }
  29. hash_descriptor[hash].init(md);
  30. if ((err = hash_descriptor[hash].process(md, data, len)) != CRYPT_OK) {
  31. goto __ERR;
  32. }
  33. err = hash_descriptor[hash].done(md, dst);
  34. *outlen = hash_descriptor[hash].hashsize;
  35. __ERR:
  36. #ifdef CLEAN_STACK
  37. zeromem(md, sizeof(hash_state));
  38. #endif
  39. XFREE(md);
  40. return err;
  41. }