sha512.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (C) 2015 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* SHA512
  17. * Daniel Beer <dlbeer@gmail.com>, 22 Apr 2014
  18. *
  19. * This file is in the public domain.
  20. */
  21. #ifndef SHA512_H_
  22. #define SHA512_H_
  23. #include <sys/types.h>
  24. #include <stdint.h>
  25. #include <stddef.h>
  26. #include <string.h>
  27. /* Feed a full block in */
  28. #define SHA512_BLOCK_SIZE 128
  29. /* SHA512 state. State is updated as data is fed in, and then the final
  30. * hash can be read out in slices.
  31. *
  32. * Data is fed in as a sequence of full blocks terminated by a single
  33. * partial block.
  34. */
  35. struct sha512_state {
  36. uint64_t h[8];
  37. uint8_t partial[SHA512_BLOCK_SIZE];
  38. size_t len;
  39. };
  40. /* Set up a new context */
  41. void sha512_init(struct sha512_state *s);
  42. void sha512_add(struct sha512_state *s, const void *data, size_t len);
  43. /* Fetch a slice of the hash result. */
  44. #define SHA512_HASH_SIZE 64
  45. void sha512_final(struct sha512_state *s, uint8_t *hash);
  46. static inline void *
  47. sha512_final_get(struct sha512_state *s)
  48. {
  49. sha512_final(s, s->partial);
  50. return s->partial;
  51. }
  52. #endif