sha256.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Declarations of functions and data types used for SHA256 and SHA224 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. #if HAVE_OPENSSL_SHA256
  18. #ifndef OPENSSL_API_COMPAT
  19. #define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */
  20. #endif
  21. #include <openssl/sha.h>
  22. #endif
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. enum { SHA224_DIGEST_SIZE = 224 / 8 };
  27. enum { SHA256_DIGEST_SIZE = 256 / 8 };
  28. #if HAVE_OPENSSL_SHA256
  29. #define GL_OPENSSL_NAME 224
  30. #include "gl_openssl.h"
  31. #define GL_OPENSSL_NAME 256
  32. #include "gl_openssl.h"
  33. #else
  34. /* Structure to save state of computation between the single steps. */
  35. struct sha256_ctx {
  36. uint32_t state[8];
  37. uint32_t total[2];
  38. size_t buflen; /* ≥ 0, ≤ 128 */
  39. uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
  40. };
  41. /* Initialize structure containing state of computation. */
  42. extern void sha256_init_ctx(struct sha256_ctx* ctx);
  43. extern void sha224_init_ctx(struct sha256_ctx* ctx);
  44. /* Starting with the result of former calls of this function (or the
  45. initialization function update the context for the next LEN bytes
  46. starting at BUFFER.
  47. It is necessary that LEN is a multiple of 64!!! */
  48. extern void sha256_process_block(const void* buffer, size_t len, 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, struct sha256_ctx* ctx);
  54. /* Process the remaining bytes in the buffer and put result from CTX
  55. in first 32 (28) bytes following RESBUF. The result is always in little
  56. endian byte order, so that a byte-wise output yields to the wanted
  57. ASCII representation of the message digest. */
  58. extern void* sha256_finish_ctx(struct sha256_ctx* ctx, void* restrict resbuf);
  59. extern void* sha224_finish_ctx(struct sha256_ctx* ctx, void* restrict resbuf);
  60. /* Put result from CTX in first 32 (28) bytes following RESBUF. The result is
  61. always in little endian byte order, so that a byte-wise output yields
  62. to the wanted ASCII representation of the message digest. */
  63. extern void* sha256_read_ctx(const struct sha256_ctx* ctx, void* restrict resbuf);
  64. extern void* sha224_read_ctx(const struct sha256_ctx* ctx, void* restrict resbuf);
  65. /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER.
  66. The result is always in little endian byte order, so that a byte-wise
  67. output yields to the wanted ASCII representation of the message
  68. digest. */
  69. extern void* sha256_buffer(const char* buffer, size_t len, void* restrict resblock);
  70. extern void* sha224_buffer(const char* buffer, size_t len, void* restrict resblock);
  71. #endif
  72. /* Compute SHA256 (SHA224) message digest for bytes read from STREAM.
  73. STREAM is an open file stream. Regular files are handled more efficiently.
  74. The contents of STREAM from its current position to its end will be read.
  75. The case that the last operation on STREAM was an 'ungetc' is not supported.
  76. The resulting message digest number will be written into the 32 (28) bytes
  77. beginning at RESBLOCK. */
  78. extern int sha256_stream(FILE* stream, void* resblock);
  79. extern int sha224_stream(FILE* stream, void* resblock);
  80. #ifdef __cplusplus
  81. }
  82. #endif
  83. /*
  84. * Hey Emacs!
  85. * Local Variables:
  86. * coding: utf-8
  87. * End:
  88. */