hash_tiger.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #include "php_hash.h"
  20. #include "php_hash_tiger.h"
  21. #include "php_hash_tiger_tables.h"
  22. #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
  23. # if defined(__LITTLE_ENDIAN__)
  24. # undef WORDS_BIGENDIAN
  25. # else
  26. # if defined(__BIG_ENDIAN__)
  27. # define WORDS_BIGENDIAN
  28. # endif
  29. # endif
  30. #endif
  31. /* {{{ */
  32. #define save_abc \
  33. aa = a; \
  34. bb = b; \
  35. cc = c;
  36. #define round(a,b,c,x,mul) \
  37. c ^= x; \
  38. a -= t1[(unsigned char)(c)] ^ \
  39. t2[(unsigned char)(((uint32_t)(c))>>(2*8))] ^ \
  40. t3[(unsigned char)((c)>>(4*8))] ^ \
  41. t4[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(2*8))] ; \
  42. b += t4[(unsigned char)(((uint32_t)(c))>>(1*8))] ^ \
  43. t3[(unsigned char)(((uint32_t)(c))>>(3*8))] ^ \
  44. t2[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(1*8))] ^ \
  45. t1[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(3*8))]; \
  46. b *= mul;
  47. #define pass(a,b,c,mul) \
  48. round(a,b,c,x0,mul) \
  49. round(b,c,a,x1,mul) \
  50. round(c,a,b,x2,mul) \
  51. round(a,b,c,x3,mul) \
  52. round(b,c,a,x4,mul) \
  53. round(c,a,b,x5,mul) \
  54. round(a,b,c,x6,mul) \
  55. round(b,c,a,x7,mul)
  56. #define key_schedule \
  57. x0 -= x7 ^ L64(0xA5A5A5A5A5A5A5A5); \
  58. x1 ^= x0; \
  59. x2 += x1; \
  60. x3 -= x2 ^ ((~x1)<<19); \
  61. x4 ^= x3; \
  62. x5 += x4; \
  63. x6 -= x5 ^ ((~x4)>>23); \
  64. x7 ^= x6; \
  65. x0 += x7; \
  66. x1 -= x0 ^ ((~x7)<<19); \
  67. x2 ^= x1; \
  68. x3 += x2; \
  69. x4 -= x3 ^ ((~x2)>>23); \
  70. x5 ^= x4; \
  71. x6 += x5; \
  72. x7 -= x6 ^ L64(0x0123456789ABCDEF);
  73. #define feedforward \
  74. a ^= aa; \
  75. b -= bb; \
  76. c += cc;
  77. #define compress(passes) \
  78. save_abc \
  79. pass(a,b,c,5) \
  80. key_schedule \
  81. pass(c,a,b,7) \
  82. key_schedule \
  83. pass(b,c,a,9) \
  84. for(pass_no=0; pass_no<passes; pass_no++) { \
  85. key_schedule \
  86. pass(a,b,c,9) \
  87. tmpa=a; a=c; c=b; b=tmpa; \
  88. } \
  89. feedforward
  90. #define split_ex(str) \
  91. x0=str[0]; x1=str[1]; x2=str[2]; x3=str[3]; \
  92. x4=str[4]; x5=str[5]; x6=str[6]; x7=str[7];
  93. #ifdef WORDS_BIGENDIAN
  94. # define split(str) \
  95. { \
  96. int i; \
  97. uint64_t tmp[8]; \
  98. \
  99. for (i = 0; i < 64; ++i) { \
  100. ((unsigned char *) tmp)[i^7] = ((unsigned char *) str)[i]; \
  101. } \
  102. split_ex(tmp); \
  103. }
  104. #else
  105. # define split split_ex
  106. #endif
  107. #define tiger_compress(passes, str, state) \
  108. { \
  109. register uint64_t a, b, c, tmpa, x0, x1, x2, x3, x4, x5, x6, x7; \
  110. uint64_t aa, bb, cc; \
  111. unsigned int pass_no; \
  112. \
  113. a = state[0]; \
  114. b = state[1]; \
  115. c = state[2]; \
  116. \
  117. split(str); \
  118. \
  119. compress(passes); \
  120. \
  121. state[0] = a; \
  122. state[1] = b; \
  123. state[2] = c; \
  124. }
  125. /* }}} */
  126. static inline void TigerFinalize(PHP_TIGER_CTX *context)
  127. {
  128. context->passed += (uint64_t) context->length << 3;
  129. context->buffer[context->length++] = 0x1;
  130. if (context->length % 8) {
  131. memset(&context->buffer[context->length], 0, 8-context->length%8);
  132. context->length += 8-context->length%8;
  133. }
  134. if (context->length > 56) {
  135. memset(&context->buffer[context->length], 0, 64 - context->length);
  136. tiger_compress(context->passes, ((uint64_t *) context->buffer), context->state);
  137. memset(context->buffer, 0, 56);
  138. } else {
  139. memset(&context->buffer[context->length], 0, 56 - context->length);
  140. }
  141. #ifndef WORDS_BIGENDIAN
  142. memcpy(&context->buffer[56], &context->passed, sizeof(uint64_t));
  143. #else
  144. context->buffer[56] = (unsigned char) (context->passed & 0xff);
  145. context->buffer[57] = (unsigned char) ((context->passed >> 8) & 0xff);
  146. context->buffer[58] = (unsigned char) ((context->passed >> 16) & 0xff);
  147. context->buffer[59] = (unsigned char) ((context->passed >> 24) & 0xff);
  148. context->buffer[60] = (unsigned char) ((context->passed >> 32) & 0xff);
  149. context->buffer[61] = (unsigned char) ((context->passed >> 40) & 0xff);
  150. context->buffer[62] = (unsigned char) ((context->passed >> 48) & 0xff);
  151. context->buffer[63] = (unsigned char) ((context->passed >> 56) & 0xff);
  152. #endif
  153. tiger_compress(context->passes, ((uint64_t *) context->buffer), context->state);
  154. }
  155. static inline void TigerDigest(unsigned char *digest_str, unsigned int digest_len, PHP_TIGER_CTX *context)
  156. {
  157. unsigned int i;
  158. for (i = 0; i < digest_len; ++i) {
  159. digest_str[i] = (unsigned char) ((context->state[i/8] >> (8 * (i%8))) & 0xff);
  160. }
  161. }
  162. PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context)
  163. {
  164. memset(context, 0, sizeof(*context));
  165. context->state[0] = L64(0x0123456789ABCDEF);
  166. context->state[1] = L64(0xFEDCBA9876543210);
  167. context->state[2] = L64(0xF096A5B4C3B2E187);
  168. }
  169. PHP_HASH_API void PHP_4TIGERInit(PHP_TIGER_CTX *context)
  170. {
  171. memset(context, 0, sizeof(*context));
  172. context->passes = 1;
  173. context->state[0] = L64(0x0123456789ABCDEF);
  174. context->state[1] = L64(0xFEDCBA9876543210);
  175. context->state[2] = L64(0xF096A5B4C3B2E187);
  176. }
  177. PHP_HASH_API void PHP_TIGERUpdate(PHP_TIGER_CTX *context, const unsigned char *input, size_t len)
  178. {
  179. if (context->length + len < 64) {
  180. memcpy(&context->buffer[context->length], input, len);
  181. context->length += len;
  182. } else {
  183. size_t i = 0, r = (context->length + len) % 64;
  184. if (context->length) {
  185. i = 64 - context->length;
  186. memcpy(&context->buffer[context->length], input, i);
  187. tiger_compress(context->passes, ((const uint64_t *) context->buffer), context->state);
  188. ZEND_SECURE_ZERO(context->buffer, 64);
  189. context->passed += 512;
  190. }
  191. for (; i + 64 <= len; i += 64) {
  192. memcpy(context->buffer, &input[i], 64);
  193. tiger_compress(context->passes, ((const uint64_t *) context->buffer), context->state);
  194. context->passed += 512;
  195. }
  196. ZEND_SECURE_ZERO(&context->buffer[r], 64-r);
  197. memcpy(context->buffer, &input[i], r);
  198. context->length = r;
  199. }
  200. }
  201. PHP_HASH_API void PHP_TIGER128Final(unsigned char digest[16], PHP_TIGER_CTX *context)
  202. {
  203. TigerFinalize(context);
  204. TigerDigest(digest, 16, context);
  205. ZEND_SECURE_ZERO(context, sizeof(*context));
  206. }
  207. PHP_HASH_API void PHP_TIGER160Final(unsigned char digest[20], PHP_TIGER_CTX *context)
  208. {
  209. TigerFinalize(context);
  210. TigerDigest(digest, 20, context);
  211. ZEND_SECURE_ZERO(context, sizeof(*context));
  212. }
  213. PHP_HASH_API void PHP_TIGER192Final(unsigned char digest[24], PHP_TIGER_CTX *context)
  214. {
  215. TigerFinalize(context);
  216. TigerDigest(digest, 24, context);
  217. ZEND_SECURE_ZERO(context, sizeof(*context));
  218. }
  219. #define PHP_HASH_TIGER_OPS(p, b) \
  220. const php_hash_ops php_hash_##p##tiger##b##_ops = { \
  221. (php_hash_init_func_t) PHP_##p##TIGERInit, \
  222. (php_hash_update_func_t) PHP_TIGERUpdate, \
  223. (php_hash_final_func_t) PHP_TIGER##b##Final, \
  224. (php_hash_copy_func_t) php_hash_copy, \
  225. b/8, \
  226. 64, \
  227. sizeof(PHP_TIGER_CTX), \
  228. 1 \
  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. */