sha2.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
  4. LICENSE TERMS
  5. The free distribution and use of this software in both source and binary
  6. form is allowed (with or without changes) provided that:
  7. 1. distributions of this source code include the above copyright
  8. notice, this list of conditions and the following disclaimer;
  9. 2. distributions in binary form include the above copyright
  10. notice, this list of conditions and the following disclaimer
  11. in the documentation and/or other associated materials;
  12. 3. the copyright holder's name is not used to endorse products
  13. built using this software without specific written permission.
  14. ALTERNATIVELY, provided that this notice is retained in full, this product
  15. may be distributed under the terms of the GNU General Public License (GPL),
  16. in which case the provisions of the GPL apply INSTEAD OF those given above.
  17. DISCLAIMER
  18. This software is provided 'as is' with no explicit or implied warranties
  19. in respect of its properties, including, but not limited to, correctness
  20. and/or fitness for purpose.
  21. ---------------------------------------------------------------------------
  22. Issue Date: 01/08/2005
  23. */
  24. #ifndef _SHA2_H
  25. #define _SHA2_H
  26. #include <stdlib.h>
  27. #define SHA_64BIT
  28. /* define the hash functions that you need */
  29. #define SHA_2 /* for dynamic hash length */
  30. #define SHA_224
  31. #define SHA_256
  32. #ifdef SHA_64BIT
  33. # define SHA_384
  34. # define SHA_512
  35. # define NEED_UINT_64T
  36. #endif
  37. #include "brg_types.h"
  38. #if defined(__cplusplus)
  39. extern "C"
  40. {
  41. #endif
  42. /* Note that the following function prototypes are the same */
  43. /* for both the bit and byte oriented implementations. But */
  44. /* the length fields are in bytes or bits as is appropriate */
  45. /* for the version used. Bit sequences are arrays of bytes */
  46. /* in which bit sequence indexes increase from the most to */
  47. /* the least significant end of each byte */
  48. #define SHA224_DIGEST_SIZE 28
  49. #define SHA224_BLOCK_SIZE 64
  50. #define SHA256_DIGEST_SIZE 32
  51. #define SHA256_BLOCK_SIZE 64
  52. /* type to hold the SHA256 (and SHA224) context */
  53. typedef struct
  54. { uint_32t count[2];
  55. uint_32t hash[8];
  56. uint_32t wbuf[16];
  57. } sha256_ctx;
  58. typedef sha256_ctx sha224_ctx;
  59. VOID_RETURN sha256_compile(sha256_ctx ctx[1]);
  60. VOID_RETURN sha224_begin(sha224_ctx ctx[1]);
  61. #define sha224_hash sha256_hash
  62. VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1]);
  63. VOID_RETURN sha224(unsigned char hval[], const unsigned char data[], unsigned long len);
  64. VOID_RETURN sha256_begin(sha256_ctx ctx[1]);
  65. VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);
  66. VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1]);
  67. VOID_RETURN sha256(unsigned char hval[], const unsigned char data[], unsigned long len);
  68. #ifndef SHA_64BIT
  69. typedef struct
  70. { union
  71. { sha256_ctx ctx256[1];
  72. } uu[1];
  73. uint_32t sha2_len;
  74. } sha2_ctx;
  75. #define SHA2_MAX_DIGEST_SIZE SHA256_DIGEST_SIZE
  76. #else
  77. #define SHA384_DIGEST_SIZE 48
  78. #define SHA384_BLOCK_SIZE 128
  79. #define SHA512_DIGEST_SIZE 64
  80. #define SHA512_BLOCK_SIZE 128
  81. #define SHA2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
  82. /* type to hold the SHA384 (and SHA512) context */
  83. typedef struct
  84. { uint_64t count[2];
  85. uint_64t hash[8];
  86. uint_64t wbuf[16];
  87. } sha512_ctx;
  88. typedef sha512_ctx sha384_ctx;
  89. typedef struct
  90. { union
  91. { sha256_ctx ctx256[1];
  92. sha512_ctx ctx512[1];
  93. } uu[1];
  94. uint_32t sha2_len;
  95. } sha2_ctx;
  96. VOID_RETURN sha512_compile(sha512_ctx ctx[1]);
  97. VOID_RETURN sha384_begin(sha384_ctx ctx[1]);
  98. #define sha384_hash sha512_hash
  99. VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
  100. VOID_RETURN sha384(unsigned char hval[], const unsigned char data[], unsigned long len);
  101. VOID_RETURN sha512_begin(sha512_ctx ctx[1]);
  102. VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);
  103. VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
  104. VOID_RETURN sha512(unsigned char hval[], const unsigned char data[], unsigned long len);
  105. INT_RETURN sha2_begin(unsigned long size, sha2_ctx ctx[1]);
  106. VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);
  107. VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
  108. INT_RETURN sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);
  109. #endif
  110. #if defined(__cplusplus)
  111. }
  112. #endif
  113. #endif