hash_memory.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. if ((err = hash_descriptor[hash].init(md)) != CRYPT_OK) {
  30. goto __ERR;
  31. }
  32. if ((err = hash_descriptor[hash].process(md, data, len)) != CRYPT_OK) {
  33. goto __ERR;
  34. }
  35. err = hash_descriptor[hash].done(md, dst);
  36. *outlen = hash_descriptor[hash].hashsize;
  37. __ERR:
  38. #ifdef CLEAN_STACK
  39. zeromem(md, sizeof(hash_state));
  40. #endif
  41. XFREE(md);
  42. return err;
  43. }