sha1.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Declarations of functions and data types used for SHA1 sum
  2. library functions.
  3. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. 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, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #ifndef SHA1_H
  16. # define SHA1_H 1
  17. #include <stdio.h>
  18. #if defined HAVE_LIMITS_H || _LIBC
  19. # include <limits.h>
  20. #endif
  21. #include "ansidecl.h"
  22. /* The following contortions are an attempt to use the C preprocessor
  23. to determine an unsigned integral type that is 32 bits wide. An
  24. alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
  25. doing that would require that the configure script compile and *run*
  26. the resulting executable. Locally running cross-compiled executables
  27. is usually not possible. */
  28. #ifdef _LIBC
  29. # include <sys/types.h>
  30. typedef u_int32_t sha1_uint32;
  31. typedef uintptr_t sha1_uintptr;
  32. #elif defined (HAVE_SYS_TYPES_H) && defined (HAVE_STDINT_H)
  33. #include <stdint.h>
  34. #include <sys/types.h>
  35. typedef uint32_t sha1_uint32;
  36. typedef uintptr_t sha1_uintptr;
  37. #else
  38. # define INT_MAX_32_BITS 2147483647
  39. /* If UINT_MAX isn't defined, assume it's a 32-bit type.
  40. This should be valid for all systems GNU cares about because
  41. that doesn't include 16-bit systems, and only modern systems
  42. (that certainly have <limits.h>) have 64+-bit integral types. */
  43. # ifndef INT_MAX
  44. # define INT_MAX INT_MAX_32_BITS
  45. # endif
  46. # if INT_MAX == INT_MAX_32_BITS
  47. typedef unsigned int sha1_uint32;
  48. # else
  49. # if SHRT_MAX == INT_MAX_32_BITS
  50. typedef unsigned short sha1_uint32;
  51. # else
  52. # if LONG_MAX == INT_MAX_32_BITS
  53. typedef unsigned long sha1_uint32;
  54. # else
  55. /* The following line is intended to evoke an error.
  56. Using #error is not portable enough. */
  57. "Cannot determine unsigned 32-bit data type."
  58. # endif
  59. # endif
  60. # endif
  61. #endif
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. /* Structure to save state of computation between the single steps. */
  66. struct sha1_ctx
  67. {
  68. sha1_uint32 A;
  69. sha1_uint32 B;
  70. sha1_uint32 C;
  71. sha1_uint32 D;
  72. sha1_uint32 E;
  73. sha1_uint32 total[2];
  74. sha1_uint32 buflen;
  75. sha1_uint32 buffer[32];
  76. };
  77. /* Initialize structure containing state of computation. */
  78. extern void sha1_init_ctx (struct sha1_ctx *ctx);
  79. /* Starting with the result of former calls of this function (or the
  80. initialization function update the context for the next LEN bytes
  81. starting at BUFFER.
  82. It is necessary that LEN is a multiple of 64!!! */
  83. extern void sha1_process_block (const void *buffer, size_t len,
  84. struct sha1_ctx *ctx);
  85. /* Starting with the result of former calls of this function (or the
  86. initialization function update the context for the next LEN bytes
  87. starting at BUFFER.
  88. It is NOT required that LEN is a multiple of 64. */
  89. extern void sha1_process_bytes (const void *buffer, size_t len,
  90. struct sha1_ctx *ctx);
  91. /* Process the remaining bytes in the buffer and put result from CTX
  92. in first 20 bytes following RESBUF. The result is always in little
  93. endian byte order, so that a byte-wise output yields to the wanted
  94. ASCII representation of the message digest.
  95. IMPORTANT: On some systems it is required that RESBUF be correctly
  96. aligned for a 32 bits value. */
  97. extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf);
  98. /* Put result from CTX in first 20 bytes following RESBUF. The result is
  99. always in little endian byte order, so that a byte-wise output yields
  100. to the wanted ASCII representation of the message digest.
  101. IMPORTANT: On some systems it is required that RESBUF is correctly
  102. aligned for a 32 bits value. */
  103. extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf);
  104. /* Compute SHA1 message digest for bytes read from STREAM. The
  105. resulting message digest number will be written into the 20 bytes
  106. beginning at RESBLOCK. */
  107. extern int sha1_stream (FILE *stream, void *resblock);
  108. /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
  109. result is always in little endian byte order, so that a byte-wise
  110. output yields to the wanted ASCII representation of the message
  111. digest. */
  112. extern void *sha1_buffer (const char *buffer, size_t len, void *resblock);
  113. #ifdef __cplusplus
  114. }
  115. #endif
  116. #endif