hl_sha2ext.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * hashlib++ - a simple hash library for C++
  3. *
  4. * Copyright (c) 2007-2010 Benjamin Grüdelbach
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1) Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2) Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. //----------------------------------------------------------------------
  29. /*
  30. * The hashlib++ SHA384 and SHA512 implementations are derivative from the
  31. * sourcecode published by Aaron D. Gifford
  32. *
  33. * Copyright (c) 2000-2001, Aaron D. Gifford
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. * 1. Redistributions of source code must retain the above copyright
  40. * notice, this list of conditions and the following disclaimer.
  41. * 2. Redistributions in binary form must reproduce the above copyright
  42. * notice, this list of conditions and the following disclaimer in the
  43. * documentation and/or other materials provided with the distribution.
  44. * 3. Neither the name of the copyright holder nor the names of contributors
  45. * may be used to endorse or promote products derived from this software
  46. * without specific prior written permission.
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  49. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  52. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  53. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  54. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  58. * SUCH DAMAGE.
  59. */
  60. //----------------------------------------------------------------------
  61. /**
  62. * @file hl_sha2ext.h
  63. * @brief This file contains the declaration of the SHA384 and
  64. * SHA512 classes
  65. * @date Mo 12 Nov 2007
  66. */
  67. //----------------------------------------------------------------------
  68. //include protection
  69. #ifndef SHA2ext_H
  70. #define SHA2ext_H
  71. //----------------------------------------------------------------------
  72. //lenght defines
  73. #define SHA384_BLOCK_LENGTH 128
  74. #define SHA384_DIGEST_LENGTH 48
  75. #define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
  76. #define SHA512_BLOCK_LENGTH 128
  77. #define SHA512_DIGEST_LENGTH 64
  78. #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
  79. #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
  80. //----------------------------------------------------------------------
  81. //hl includes
  82. #include "hl_types.h"
  83. //----------------------------------------------------------------------
  84. //typedefs
  85. /**
  86. * Exactly 1 byte
  87. */
  88. typedef hl_uint8 sha2_byte;
  89. /**
  90. * Exactly 4 bytes
  91. */
  92. typedef hl_uint32 sha2_word32;
  93. /**
  94. * Exactly 8 bytes
  95. */
  96. typedef hl_uint64 sha2_word64;
  97. /**
  98. * @brief This struct represents a SHA512-hash context
  99. */
  100. typedef struct HL_SHA512_CTX
  101. {
  102. hl_uint64 state[8];
  103. hl_uint64 bitcount[2];
  104. hl_uint8 buffer[SHA512_BLOCK_LENGTH];
  105. } HL_SHA512_CTX;
  106. /**
  107. * @brief This struct represents a SHA384-hash context
  108. */
  109. typedef HL_SHA512_CTX HL_SHA_384_CTX;
  110. //----------------------------------------------------------------------
  111. /**
  112. * @brief This class represents the implementation of
  113. * the SHA384 and SHA512 algorithm.
  114. *
  115. * Basically the class provides six public member-functions
  116. * to create a hash: SHA384_Init(), SHA384_Update(), SHA384_End(),
  117. * SHA512_Init(), SHA512_Update() and SHA512_End().
  118. * If you want to create a hash based on a string or file quickly
  119. * you should use the sha384wrapper or sha512wrapper classes.
  120. */
  121. class SHA2ext
  122. {
  123. private:
  124. /**
  125. * @brief Finalize the sha384 operation
  126. * @param digest The digest to finalize the operation with.
  127. * @param context The context to finalize.
  128. */
  129. void SHA384_Final(hl_uint8 digest[SHA384_DIGEST_LENGTH],
  130. HL_SHA_384_CTX* context);
  131. /**
  132. * @brief Finalize the sha512 operation
  133. * @param digest The digest to finalize the operation with.
  134. * @param context The context to finalize.
  135. */
  136. void SHA512_Final(hl_uint8 digest[SHA512_DIGEST_LENGTH],
  137. HL_SHA512_CTX* context);
  138. /**
  139. * @brief Internal method
  140. *
  141. * used by SHA512 and SHA384
  142. * @author Benjamin Grüdelbach
  143. * @param context The context of the operation
  144. */
  145. void SHA512_Last(HL_SHA512_CTX* context);
  146. /**
  147. * @brief Internal data transformation
  148. * @param context The context to use
  149. * @param data The data to transform
  150. */
  151. void SHA512_Transform(HL_SHA512_CTX* context,
  152. const sha2_word64* data);
  153. public:
  154. /**
  155. * @brief Initialize the SHA384 context
  156. * @param context The context to init.
  157. */
  158. void SHA384_Init(HL_SHA_384_CTX* context);
  159. /**
  160. * @brief Initialize the SHA512 context
  161. * @param context The context to init.
  162. */
  163. void SHA512_Init(HL_SHA512_CTX* context);
  164. /**
  165. * @brief Updates the SHA512 context
  166. * @param context The context to update.
  167. * @param data The data for updating the context.
  168. * @param len The length of the given data.
  169. */
  170. void SHA384_Update(HL_SHA_384_CTX* context,
  171. const hl_uint8* data,
  172. unsigned int len);
  173. /**
  174. * @brief Updates the SHA284 context
  175. * @param context The context to update.
  176. * @param data The data for updating the context.
  177. * @param len The length of the given data.
  178. */
  179. void SHA512_Update(HL_SHA512_CTX* context,
  180. const hl_uint8* data,
  181. unsigned int len);
  182. /**
  183. * @brief Ends the SHA384 operation and return the
  184. * created hash in the given buffer.
  185. * @param context The context to end.
  186. * @param buffer This OUT-Parameter contains the created
  187. * hash after ending the operation.
  188. */
  189. char* SHA384_End(HL_SHA_384_CTX* context,
  190. char buffer[SHA384_DIGEST_STRING_LENGTH]);
  191. /**
  192. * @brief Ends the SHA512 operation and return the
  193. * created hash in the given buffer.
  194. * @param context The context to end.
  195. * @param buffer This OUT-Parameter contains the created
  196. * hash after ending the operation.
  197. */
  198. char* SHA512_End(HL_SHA512_CTX* context,
  199. char buffer[SHA512_DIGEST_STRING_LENGTH]);
  200. };
  201. //----------------------------------------------------------------------
  202. //end of include protection
  203. #endif
  204. //----------------------------------------------------------------------
  205. //EOF