hl_sha2mac.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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
  31. * the 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. /**
  63. * @file hl_sha2mac.h
  64. * @brief This file contains useful macros for use with SHA384
  65. * and SHA512
  66. * @date Mo 12 Nov 2007
  67. */
  68. //----------------------------------------------------------------------
  69. /*
  70. * SHA-256/384/512 Machine Architecture Definitions
  71. * BYTE_ORDER NOTE:
  72. *
  73. * Please make sure that your system defines BYTE_ORDER. If your
  74. * architecture is little-endian, make sure it also defines
  75. * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
  76. * equivilent.
  77. *
  78. * If your system does not define the above, then you can do so by
  79. * hand like this:
  80. *
  81. * #define LITTLE_ENDIAN 1234
  82. * #define BIG_ENDIAN 4321
  83. *
  84. * And for little-endian machines, add:
  85. *
  86. * #define BYTE_ORDER LITTLE_ENDIAN
  87. *
  88. * Or for big-endian machines:
  89. *
  90. * #define BYTE_ORDER BIG_ENDIAN
  91. *
  92. * The FreeBSD machine this was written on defines
  93. * BYTE_ORDER appropriately by including <sys/types.h>
  94. * (which in turn includes <machine/endian.h> where the appropriate
  95. * definitions are actually made).
  96. */
  97. #define LITTLE_ENDIAN 1234
  98. #define BYTE_ORDER LITTLE_ENDIAN
  99. #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
  100. #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
  101. #endif
  102. /*** ENDIAN REVERSAL MACROS *******************************************/
  103. #if BYTE_ORDER == LITTLE_ENDIAN
  104. #define REVERSE32(w,x) { \
  105. sha2_word32 tmp = (w); \
  106. tmp = (tmp >> 16) | (tmp << 16); \
  107. (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
  108. }
  109. #define REVERSE64(w,x) { \
  110. sha2_word64 tmp = (w); \
  111. tmp = (tmp >> 32) | (tmp << 32); \
  112. tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
  113. ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
  114. (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
  115. ((tmp & 0x0000ffff0000ffffULL) << 16); \
  116. }
  117. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  118. /*
  119. * Macro for incrementally adding the unsigned 64-bit integer n to the
  120. * unsigned 128-bit integer (represented using a two-element array of
  121. * 64-bit words):
  122. */
  123. #define ADDINC128(w,n) { \
  124. (w)[0] += (sha2_word64)(n); \
  125. if ((w)[0] < (n)) { \
  126. (w)[1]++; \
  127. } \
  128. }
  129. /*
  130. * Macros for copying blocks of memory and for zeroing out ranges
  131. * of memory. Using these macros makes it easy to switch from
  132. * using memset()/memcpy() and using bzero()/bcopy().
  133. *
  134. * Please define either SHA2_USE_MEMSET_MEMCPY or define
  135. * SHA2_USE_BZERO_BCOPY depending on which function set you
  136. * choose to use:
  137. */
  138. #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
  139. /* Default to memset()/memcpy() if no option is specified */
  140. #define SHA2_USE_MEMSET_MEMCPY 1
  141. #endif
  142. #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
  143. /* Abort with an error if BOTH options are defined */
  144. #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
  145. #endif
  146. #ifdef SHA2_USE_MEMSET_MEMCPY
  147. #define MEMSET_BZERO(p,l) memset((p), 0, (l))
  148. #define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
  149. #endif
  150. #ifdef SHA2_USE_BZERO_BCOPY
  151. #define MEMSET_BZERO(p,l) bzero((p), (l))
  152. #define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))
  153. #endif
  154. /*** THE SIX LOGICAL FUNCTIONS ****************************************
  155. *
  156. * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
  157. *
  158. * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
  159. * S is a ROTATION) because the SHA-256/384/512 description document
  160. * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
  161. * same "backwards" definition.
  162. */
  163. /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
  164. #define R(b,x) ((x) >> (b))
  165. /* 32-bit Rotate-right (used in SHA-256): */
  166. #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
  167. /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
  168. #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
  169. /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
  170. #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  171. #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  172. /* Four of six logical functions used in SHA-256: */
  173. #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
  174. #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
  175. #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
  176. #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
  177. /* Four of six logical functions used in SHA-384 and SHA-512: */
  178. #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
  179. #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
  180. #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
  181. #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))