sha256.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Declarations of functions and data types used for SHA256 and SHA224 sum
  2. library functions.
  3. Copyright (C) 2005-2006, 2008-2011 Free Software Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef SHA256_H
  15. # define SHA256_H 1
  16. # include <stdio.h>
  17. # include <stdint.h>
  18. # ifdef __cplusplus
  19. extern "C" {
  20. # endif
  21. /* Structure to save state of computation between the single steps. */
  22. struct sha256_ctx
  23. {
  24. uint32_t state[8];
  25. uint32_t total[2];
  26. size_t buflen;
  27. uint32_t buffer[32];
  28. };
  29. enum { SHA224_DIGEST_SIZE = 224 / 8 };
  30. enum { SHA256_DIGEST_SIZE = 256 / 8 };
  31. /* Initialize structure containing state of computation. */
  32. extern void sha256_init_ctx (struct sha256_ctx *ctx);
  33. extern void sha224_init_ctx (struct sha256_ctx *ctx);
  34. /* Starting with the result of former calls of this function (or the
  35. initialization function update the context for the next LEN bytes
  36. starting at BUFFER.
  37. It is necessary that LEN is a multiple of 64!!! */
  38. extern void sha256_process_block (const void *buffer, size_t len,
  39. struct sha256_ctx *ctx);
  40. /* Starting with the result of former calls of this function (or the
  41. initialization function update the context for the next LEN bytes
  42. starting at BUFFER.
  43. It is NOT required that LEN is a multiple of 64. */
  44. extern void sha256_process_bytes (const void *buffer, size_t len,
  45. struct sha256_ctx *ctx);
  46. /* Process the remaining bytes in the buffer and put result from CTX
  47. in first 32 (28) bytes following RESBUF. The result is always in little
  48. endian byte order, so that a byte-wise output yields to the wanted
  49. ASCII representation of the message digest. */
  50. extern void *sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf);
  51. extern void *sha224_finish_ctx (struct sha256_ctx *ctx, void *resbuf);
  52. /* Put result from CTX in first 32 (28) bytes following RESBUF. The result is
  53. always in little endian byte order, so that a byte-wise output yields
  54. to the wanted ASCII representation of the message digest. */
  55. extern void *sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf);
  56. extern void *sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf);
  57. /* Compute SHA256 (SHA224) message digest for bytes read from STREAM. The
  58. resulting message digest number will be written into the 32 (28) bytes
  59. beginning at RESBLOCK. */
  60. extern int sha256_stream (FILE *stream, void *resblock);
  61. extern int sha224_stream (FILE *stream, void *resblock);
  62. /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER. The
  63. result is always in little endian byte order, so that a byte-wise
  64. output yields to the wanted ASCII representation of the message
  65. digest. */
  66. extern void *sha256_buffer (const char *buffer, size_t len, void *resblock);
  67. extern void *sha224_buffer (const char *buffer, size_t len, void *resblock);
  68. # ifdef __cplusplus
  69. }
  70. # endif
  71. #endif