sha256.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Declarations of functions and data types used for SHA256 and SHA224 sum
  2. library functions.
  3. Copyright (C) 2005-2006, 2008-2015 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. # if HAVE_OPENSSL_SHA256
  19. # include <openssl/sha.h>
  20. # endif
  21. # ifdef __cplusplus
  22. extern "C" {
  23. # endif
  24. enum { SHA224_DIGEST_SIZE = 224 / 8 };
  25. enum { SHA256_DIGEST_SIZE = 256 / 8 };
  26. # if HAVE_OPENSSL_SHA256
  27. # define GL_OPENSSL_NAME 224
  28. # include "gl_openssl.h"
  29. # define GL_OPENSSL_NAME 256
  30. # include "gl_openssl.h"
  31. # else
  32. /* Structure to save state of computation between the single steps. */
  33. struct sha256_ctx
  34. {
  35. uint32_t state[8];
  36. uint32_t total[2];
  37. size_t buflen;
  38. uint32_t buffer[32];
  39. };
  40. /* Initialize structure containing state of computation. */
  41. extern void sha256_init_ctx (struct sha256_ctx *ctx);
  42. extern void sha224_init_ctx (struct sha256_ctx *ctx);
  43. /* Starting with the result of former calls of this function (or the
  44. initialization function update the context for the next LEN bytes
  45. starting at BUFFER.
  46. It is necessary that LEN is a multiple of 64!!! */
  47. extern void sha256_process_block (const void *buffer, size_t len,
  48. struct sha256_ctx *ctx);
  49. /* Starting with the result of former calls of this function (or the
  50. initialization function update the context for the next LEN bytes
  51. starting at BUFFER.
  52. It is NOT required that LEN is a multiple of 64. */
  53. extern void sha256_process_bytes (const void *buffer, size_t len,
  54. struct sha256_ctx *ctx);
  55. /* Process the remaining bytes in the buffer and put result from CTX
  56. in first 32 (28) bytes following RESBUF. The result is always in little
  57. endian byte order, so that a byte-wise output yields to the wanted
  58. ASCII representation of the message digest. */
  59. extern void *sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf);
  60. extern void *sha224_finish_ctx (struct sha256_ctx *ctx, void *resbuf);
  61. /* Put result from CTX in first 32 (28) bytes following RESBUF. The result is
  62. always in little endian byte order, so that a byte-wise output yields
  63. to the wanted ASCII representation of the message digest. */
  64. extern void *sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf);
  65. extern void *sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf);
  66. /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER. The
  67. result is always in little endian byte order, so that a byte-wise
  68. output yields to the wanted ASCII representation of the message
  69. digest. */
  70. extern void *sha256_buffer (const char *buffer, size_t len, void *resblock);
  71. extern void *sha224_buffer (const char *buffer, size_t len, void *resblock);
  72. # endif
  73. /* Compute SHA256 (SHA224) message digest for bytes read from STREAM. The
  74. resulting message digest number will be written into the 32 (28) bytes
  75. beginning at RESBLOCK. */
  76. extern int sha256_stream (FILE *stream, void *resblock);
  77. extern int sha224_stream (FILE *stream, void *resblock);
  78. # ifdef __cplusplus
  79. }
  80. # endif
  81. #endif