sha256.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Declarations of functions and data types used for SHA256 sum
  2. library functions.
  3. Copyright (C) 2005-2006, 2008-2022 Free Software Foundation, Inc.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #pragma once
  15. #include <stdio.h>
  16. #include <stdint.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. enum { SHA256_DIGEST_SIZE = 256 / 8 };
  21. /* Structure to save state of computation between the single steps. */
  22. struct sha256_ctx {
  23. uint32_t state[8];
  24. uint32_t total[2];
  25. size_t buflen; /* ≥ 0, ≤ 128 */
  26. uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
  27. };
  28. /* Initialize structure containing state of computation. */
  29. extern void sha256_init_ctx(struct sha256_ctx* ctx);
  30. /* Starting with the result of former calls of this function (or the
  31. initialization function update the context for the next LEN bytes
  32. starting at BUFFER.
  33. It is necessary that LEN is a multiple of 64!!! */
  34. extern void sha256_process_block(const void* buffer, size_t len, struct sha256_ctx* ctx);
  35. /* Starting with the result of former calls of this function (or the
  36. initialization function update the context for the next LEN bytes
  37. starting at BUFFER.
  38. It is NOT required that LEN is a multiple of 64. */
  39. extern void sha256_process_bytes(const void* buffer, size_t len, struct sha256_ctx* ctx);
  40. /* Process the remaining bytes in the buffer and put result from CTX
  41. in first 32 (28) bytes following RESBUF. The result is always in little
  42. endian byte order, so that a byte-wise output yields to the wanted
  43. ASCII representation of the message digest. */
  44. extern void* sha256_finish_ctx(struct sha256_ctx* ctx, void* restrict resbuf);
  45. /* Put result from CTX in first 32 (28) bytes following RESBUF. The result is
  46. always in little endian byte order, so that a byte-wise output yields
  47. to the wanted ASCII representation of the message digest. */
  48. extern void* sha256_read_ctx(const struct sha256_ctx* ctx, void* restrict resbuf);
  49. /* Compute SHA256 message digest for LEN bytes beginning at BUFFER.
  50. The result is always in little endian byte order, so that a byte-wise
  51. output yields to the wanted ASCII representation of the message
  52. digest. */
  53. extern void* sha256_buffer(const char* buffer, size_t len, void* restrict resblock);
  54. #ifdef __cplusplus
  55. }
  56. #endif
  57. /*
  58. * Hey Emacs!
  59. * Local Variables:
  60. * coding: utf-8
  61. * End:
  62. */