hash_tiger.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Michael Wallner <mike@php.net> |
  16. | Sara Golemon <pollita@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #include "php_hash.h"
  21. #include "php_hash_tiger.h"
  22. #include "php_hash_tiger_tables.h"
  23. #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
  24. # if defined(__LITTLE_ENDIAN__)
  25. # undef WORDS_BIGENDIAN
  26. # else
  27. # if defined(__BIG_ENDIAN__)
  28. # define WORDS_BIGENDIAN
  29. # endif
  30. # endif
  31. #endif
  32. /* {{{ */
  33. #define save_abc \
  34. aa = a; \
  35. bb = b; \
  36. cc = c;
  37. #define round(a,b,c,x,mul) \
  38. c ^= x; \
  39. a -= t1[(unsigned char)(c)] ^ \
  40. t2[(unsigned char)(((php_hash_uint32)(c))>>(2*8))] ^ \
  41. t3[(unsigned char)((c)>>(4*8))] ^ \
  42. t4[(unsigned char)(((php_hash_uint32)((c)>>(4*8)))>>(2*8))] ; \
  43. b += t4[(unsigned char)(((php_hash_uint32)(c))>>(1*8))] ^ \
  44. t3[(unsigned char)(((php_hash_uint32)(c))>>(3*8))] ^ \
  45. t2[(unsigned char)(((php_hash_uint32)((c)>>(4*8)))>>(1*8))] ^ \
  46. t1[(unsigned char)(((php_hash_uint32)((c)>>(4*8)))>>(3*8))]; \
  47. b *= mul;
  48. #define pass(a,b,c,mul) \
  49. round(a,b,c,x0,mul) \
  50. round(b,c,a,x1,mul) \
  51. round(c,a,b,x2,mul) \
  52. round(a,b,c,x3,mul) \
  53. round(b,c,a,x4,mul) \
  54. round(c,a,b,x5,mul) \
  55. round(a,b,c,x6,mul) \
  56. round(b,c,a,x7,mul)
  57. #define key_schedule \
  58. x0 -= x7 ^ L64(0xA5A5A5A5A5A5A5A5); \
  59. x1 ^= x0; \
  60. x2 += x1; \
  61. x3 -= x2 ^ ((~x1)<<19); \
  62. x4 ^= x3; \
  63. x5 += x4; \
  64. x6 -= x5 ^ ((~x4)>>23); \
  65. x7 ^= x6; \
  66. x0 += x7; \
  67. x1 -= x0 ^ ((~x7)<<19); \
  68. x2 ^= x1; \
  69. x3 += x2; \
  70. x4 -= x3 ^ ((~x2)>>23); \
  71. x5 ^= x4; \
  72. x6 += x5; \
  73. x7 -= x6 ^ L64(0x0123456789ABCDEF);
  74. #define feedforward \
  75. a ^= aa; \
  76. b -= bb; \
  77. c += cc;
  78. #define compress(passes) \
  79. save_abc \
  80. pass(a,b,c,5) \
  81. key_schedule \
  82. pass(c,a,b,7) \
  83. key_schedule \
  84. pass(b,c,a,9) \
  85. for(pass_no=0; pass_no<passes; pass_no++) { \
  86. key_schedule \
  87. pass(a,b,c,9) \
  88. tmpa=a; a=c; c=b; b=tmpa; \
  89. } \
  90. feedforward
  91. #define split_ex(str) \
  92. x0=str[0]; x1=str[1]; x2=str[2]; x3=str[3]; \
  93. x4=str[4]; x5=str[5]; x6=str[6]; x7=str[7];
  94. #ifdef WORDS_BIGENDIAN
  95. # define split(str) \
  96. { \
  97. int i; \
  98. php_hash_uint64 tmp[8]; \
  99. \
  100. for (i = 0; i < 64; ++i) { \
  101. ((unsigned char *) tmp)[i^7] = ((unsigned char *) str)[i]; \
  102. } \
  103. split_ex(tmp); \
  104. }
  105. #else
  106. # define split split_ex
  107. #endif
  108. #define tiger_compress(passes, str, state) \
  109. { \
  110. register php_hash_uint64 a, b, c, tmpa, x0, x1, x2, x3, x4, x5, x6, x7; \
  111. php_hash_uint64 aa, bb, cc; \
  112. unsigned int pass_no; \
  113. \
  114. a = state[0]; \
  115. b = state[1]; \
  116. c = state[2]; \
  117. \
  118. split(str); \
  119. \
  120. compress(passes); \
  121. \
  122. state[0] = a; \
  123. state[1] = b; \
  124. state[2] = c; \
  125. }
  126. /* }}} */
  127. static inline void TigerFinalize(PHP_TIGER_CTX *context)
  128. {
  129. context->passed += (php_hash_uint64) context->length << 3;
  130. context->buffer[context->length++] = 0x1;
  131. if (context->length % 8) {
  132. memset(&context->buffer[context->length], 0, 8-context->length%8);
  133. context->length += 8-context->length%8;
  134. }
  135. if (context->length > 56) {
  136. memset(&context->buffer[context->length], 0, 64 - context->length);
  137. tiger_compress(context->passes, ((php_hash_uint64 *) context->buffer), context->state);
  138. memset(context->buffer, 0, 56);
  139. } else {
  140. memset(&context->buffer[context->length], 0, 56 - context->length);
  141. }
  142. #ifndef WORDS_BIGENDIAN
  143. memcpy(&context->buffer[56], &context->passed, sizeof(php_hash_uint64));
  144. #else
  145. context->buffer[56] = (unsigned char) (context->passed & 0xff);
  146. context->buffer[57] = (unsigned char) ((context->passed >> 8) & 0xff);
  147. context->buffer[58] = (unsigned char) ((context->passed >> 16) & 0xff);
  148. context->buffer[59] = (unsigned char) ((context->passed >> 24) & 0xff);
  149. context->buffer[60] = (unsigned char) ((context->passed >> 32) & 0xff);
  150. context->buffer[61] = (unsigned char) ((context->passed >> 40) & 0xff);
  151. context->buffer[62] = (unsigned char) ((context->passed >> 48) & 0xff);
  152. context->buffer[63] = (unsigned char) ((context->passed >> 56) & 0xff);
  153. #endif
  154. tiger_compress(context->passes, ((php_hash_uint64 *) context->buffer), context->state);
  155. }
  156. static inline void TigerDigest(unsigned char *digest_str, unsigned int digest_len, PHP_TIGER_CTX *context)
  157. {
  158. unsigned int i;
  159. for (i = 0; i < digest_len; ++i) {
  160. digest_str[i] = (unsigned char) ((context->state[i/8] >> (8 * (i%8))) & 0xff);
  161. }
  162. }
  163. PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context)
  164. {
  165. memset(context, 0, sizeof(*context));
  166. context->state[0] = L64(0x0123456789ABCDEF);
  167. context->state[1] = L64(0xFEDCBA9876543210);
  168. context->state[2] = L64(0xF096A5B4C3B2E187);
  169. }
  170. PHP_HASH_API void PHP_4TIGERInit(PHP_TIGER_CTX *context)
  171. {
  172. memset(context, 0, sizeof(*context));
  173. context->passes = 1;
  174. context->state[0] = L64(0x0123456789ABCDEF);
  175. context->state[1] = L64(0xFEDCBA9876543210);
  176. context->state[2] = L64(0xF096A5B4C3B2E187);
  177. }
  178. PHP_HASH_API void PHP_TIGERUpdate(PHP_TIGER_CTX *context, const unsigned char *input, size_t len)
  179. {
  180. if (context->length + len < 64) {
  181. memcpy(&context->buffer[context->length], input, len);
  182. context->length += len;
  183. } else {
  184. size_t i = 0, r = (context->length + len) % 64;
  185. if (context->length) {
  186. i = 64 - context->length;
  187. memcpy(&context->buffer[context->length], input, i);
  188. tiger_compress(context->passes, ((const php_hash_uint64 *) context->buffer), context->state);
  189. memset(context->buffer, 0, 64);
  190. context->passed += 512;
  191. }
  192. for (; i + 64 <= len; i += 64) {
  193. memcpy(context->buffer, &input[i], 64);
  194. tiger_compress(context->passes, ((const php_hash_uint64 *) context->buffer), context->state);
  195. context->passed += 512;
  196. }
  197. memset(&context->buffer[r], 0, 64-r);
  198. memcpy(context->buffer, &input[i], r);
  199. context->length = r;
  200. }
  201. }
  202. PHP_HASH_API void PHP_TIGER128Final(unsigned char digest[16], PHP_TIGER_CTX *context)
  203. {
  204. TigerFinalize(context);
  205. TigerDigest(digest, 16, context);
  206. memset(context, 0, sizeof(*context));
  207. }
  208. PHP_HASH_API void PHP_TIGER160Final(unsigned char digest[20], PHP_TIGER_CTX *context)
  209. {
  210. TigerFinalize(context);
  211. TigerDigest(digest, 20, context);
  212. memset(context, 0, sizeof(*context));
  213. }
  214. PHP_HASH_API void PHP_TIGER192Final(unsigned char digest[24], PHP_TIGER_CTX *context)
  215. {
  216. TigerFinalize(context);
  217. TigerDigest(digest, 24, context);
  218. memset(context, 0, sizeof(*context));
  219. }
  220. #define PHP_HASH_TIGER_OPS(p, b) \
  221. const php_hash_ops php_hash_##p##tiger##b##_ops = { \
  222. (php_hash_init_func_t) PHP_##p##TIGERInit, \
  223. (php_hash_update_func_t) PHP_TIGERUpdate, \
  224. (php_hash_final_func_t) PHP_TIGER##b##Final, \
  225. (php_hash_copy_func_t) php_hash_copy, \
  226. b/8, \
  227. 64, \
  228. sizeof(PHP_TIGER_CTX) \
  229. }
  230. PHP_HASH_TIGER_OPS(3, 128);
  231. PHP_HASH_TIGER_OPS(3, 160);
  232. PHP_HASH_TIGER_OPS(3, 192);
  233. PHP_HASH_TIGER_OPS(4, 128);
  234. PHP_HASH_TIGER_OPS(4, 160);
  235. PHP_HASH_TIGER_OPS(4, 192);
  236. /*
  237. * Local variables:
  238. * tab-width: 4
  239. * c-basic-offset: 4
  240. * End:
  241. * vim600: sw=4 ts=4 fdm=marker
  242. * vim<600: sw=4 ts=4
  243. */