hash_tiger.c 7.9 KB

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