sha2.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2002, Dr Brian Gladman <[email protected]>, Worcester, UK.
  4. All rights reserved.
  5. LICENSE TERMS
  6. The free distribution and use of this software in both source and binary
  7. form is allowed (with or without changes) provided that:
  8. 1. distributions of this source code include the above copyright
  9. notice, this list of conditions and the following disclaimer;
  10. 2. distributions in binary form include the above copyright
  11. notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other associated materials;
  13. 3. the copyright holder's name is not used to endorse products
  14. built using this software without specific written permission.
  15. ALTERNATIVELY, provided that this notice is retained in full, this product
  16. may be distributed under the terms of the GNU General Public License (GPL),
  17. in which case the provisions of the GPL apply INSTEAD OF those given above.
  18. DISCLAIMER
  19. This software is provided 'as is' with no explicit or implied warranties
  20. in respect of its properties, including, but not limited to, correctness
  21. and/or fitness for purpose.
  22. ---------------------------------------------------------------------------
  23. Issue Date: 26/08/2003
  24. */
  25. #ifndef _SHA2_H
  26. #define _SHA2_H
  27. #include <limits.h>
  28. /* Defines for suffixes to 32 and 64 bit unsigned numeric values */
  29. #define sfx_lo(x,y) x##y
  30. #define sfx_hi(x,y) sfx_lo(x,y)
  31. #define n_u32(p) sfx_hi(0x##p,s_u32)
  32. #define n_u64(p) sfx_hi(0x##p,s_u64)
  33. /* define an unsigned 32-bit type */
  34. #if UINT_MAX == 0xffffffff
  35. typedef unsigned int sha2_32t;
  36. #define s_u32 u
  37. #elif ULONG_MAX == 0xffffffff
  38. typedef unsigned long sha2_32t;
  39. #define s_u32 ul
  40. #else
  41. #error Please define sha2_32t as an unsigned 32 bit type in sha2.h
  42. #endif
  43. /* define an unsigned 64-bit type */
  44. #if _MSC_VER < 1300
  45. typedef unsigned __int64 sha2_64t;
  46. #define s_u64 ui64
  47. #elif ULONG_MAX == 0xffffffffffffffff
  48. typedef unsigned long sha2_64t;
  49. #define s_u64 ul
  50. #elif ULONG_MAX == 0xffffffff
  51. typedef unsigned long long sha2_64t; /* a somewhat dangerous guess */
  52. #define s_u64 ull
  53. #else
  54. #error Please define sha2_64t as an unsigned 64 bit type in sha2.h
  55. #endif
  56. #if defined(__cplusplus)
  57. extern "C"
  58. {
  59. #endif
  60. #define SHA256_DIGEST_SIZE 32
  61. #define SHA384_DIGEST_SIZE 48
  62. #define SHA512_DIGEST_SIZE 64
  63. #define SHA256_BLOCK_SIZE 64
  64. #define SHA384_BLOCK_SIZE 128
  65. #define SHA512_BLOCK_SIZE 128
  66. #define SHA2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
  67. #define SHA2_GOOD 0
  68. #define SHA2_BAD 1
  69. /* type to hold the SHA256 context */
  70. typedef struct
  71. { sha2_32t count[2];
  72. sha2_32t hash[8];
  73. sha2_32t wbuf[16];
  74. } sha256_ctx;
  75. /* type to hold the SHA384/512 context */
  76. typedef struct
  77. { sha2_64t count[2];
  78. sha2_64t hash[8];
  79. sha2_64t wbuf[16];
  80. } sha512_ctx;
  81. typedef sha512_ctx sha384_ctx;
  82. /* type to hold a SHA2 context (256/384/512) */
  83. typedef struct
  84. { union
  85. { sha256_ctx ctx256[1];
  86. sha512_ctx ctx512[1];
  87. } uu[1];
  88. sha2_32t sha2_len;
  89. } sha2_ctx;
  90. #ifndef SHA2_DLL /* implement normal or DLL functions */
  91. #define sha2_void void
  92. #define sha2_int int
  93. #else
  94. #define sha2_void void __declspec(dllexport) _stdcall
  95. #define sha2_int int __declspec(dllexport) _stdcall
  96. #endif
  97. sha2_void sha256_compile(sha256_ctx ctx[1]);
  98. sha2_void sha512_compile(sha512_ctx ctx[1]);
  99. sha2_void sha256_begin(sha256_ctx ctx[1]);
  100. sha2_void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);
  101. sha2_void sha256_end(unsigned char hval[], sha256_ctx ctx[1]);
  102. sha2_void sha256Lib(unsigned char hval[], const unsigned char data[], unsigned long len);
  103. sha2_void sha384_begin(sha384_ctx ctx[1]);
  104. #define sha384_hash sha512_hash
  105. sha2_void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
  106. sha2_void sha384Lib(unsigned char hval[], const unsigned char data[], unsigned long len);
  107. sha2_void sha512_begin(sha512_ctx ctx[1]);
  108. sha2_void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);
  109. sha2_void sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
  110. sha2_void sha512Lib(unsigned char hval[], const unsigned char data[], unsigned long len);
  111. sha2_int sha2_begin(unsigned long size, sha2_ctx ctx[1]);
  112. sha2_void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);
  113. sha2_void sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
  114. sha2_int sha2Lib(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);
  115. #if defined(__cplusplus)
  116. }
  117. #endif
  118. #endif