sha1.h 4.7 KB

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