hash_snefru.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_snefru.h"
  19. #include "php_hash_snefru_tables.h"
  20. #define round(L, C, N, SB) \
  21. SBE = SB[C & 0xff]; \
  22. L ^= SBE; \
  23. N ^= SBE
  24. #ifndef DBG_SNEFRU
  25. #define DBG_SNEFRU 0
  26. #endif
  27. #if DBG_SNEFRU
  28. void ph(uint32_t h[16])
  29. {
  30. int i;
  31. for (i = 0; i < 16; i++)
  32. printf ("%08lx", h[i]); printf("\n");
  33. }
  34. #endif
  35. static inline void Snefru(uint32_t input[16])
  36. {
  37. static int shifts[4] = {16, 8, 16, 24};
  38. int b, index, rshift, lshift;
  39. const uint32_t *t0,*t1;
  40. uint32_t SBE,B00,B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12,B13,B14,B15;
  41. B00 = input[0];
  42. B01 = input[1];
  43. B02 = input[2];
  44. B03 = input[3];
  45. B04 = input[4];
  46. B05 = input[5];
  47. B06 = input[6];
  48. B07 = input[7];
  49. B08 = input[8];
  50. B09 = input[9];
  51. B10 = input[10];
  52. B11 = input[11];
  53. B12 = input[12];
  54. B13 = input[13];
  55. B14 = input[14];
  56. B15 = input[15];
  57. for (index = 0; index < 8; index++) {
  58. t0 = tables[2*index+0];
  59. t1 = tables[2*index+1];
  60. for (b = 0; b < 4; b++) {
  61. round(B15, B00, B01, t0);
  62. round(B00, B01, B02, t0);
  63. round(B01, B02, B03, t1);
  64. round(B02, B03, B04, t1);
  65. round(B03, B04, B05, t0);
  66. round(B04, B05, B06, t0);
  67. round(B05, B06, B07, t1);
  68. round(B06, B07, B08, t1);
  69. round(B07, B08, B09, t0);
  70. round(B08, B09, B10, t0);
  71. round(B09, B10, B11, t1);
  72. round(B10, B11, B12, t1);
  73. round(B11, B12, B13, t0);
  74. round(B12, B13, B14, t0);
  75. round(B13, B14, B15, t1);
  76. round(B14, B15, B00, t1);
  77. rshift = shifts[b];
  78. lshift = 32-rshift;
  79. B00 = (B00 >> rshift) | (B00 << lshift);
  80. B01 = (B01 >> rshift) | (B01 << lshift);
  81. B02 = (B02 >> rshift) | (B02 << lshift);
  82. B03 = (B03 >> rshift) | (B03 << lshift);
  83. B04 = (B04 >> rshift) | (B04 << lshift);
  84. B05 = (B05 >> rshift) | (B05 << lshift);
  85. B06 = (B06 >> rshift) | (B06 << lshift);
  86. B07 = (B07 >> rshift) | (B07 << lshift);
  87. B08 = (B08 >> rshift) | (B08 << lshift);
  88. B09 = (B09 >> rshift) | (B09 << lshift);
  89. B10 = (B10 >> rshift) | (B10 << lshift);
  90. B11 = (B11 >> rshift) | (B11 << lshift);
  91. B12 = (B12 >> rshift) | (B12 << lshift);
  92. B13 = (B13 >> rshift) | (B13 << lshift);
  93. B14 = (B14 >> rshift) | (B14 << lshift);
  94. B15 = (B15 >> rshift) | (B15 << lshift);
  95. }
  96. }
  97. input[0] ^= B15;
  98. input[1] ^= B14;
  99. input[2] ^= B13;
  100. input[3] ^= B12;
  101. input[4] ^= B11;
  102. input[5] ^= B10;
  103. input[6] ^= B09;
  104. input[7] ^= B08;
  105. #if DBG_SNEFRU
  106. ph(input);
  107. #endif
  108. }
  109. static inline void SnefruTransform(PHP_SNEFRU_CTX *context, const unsigned char input[32])
  110. {
  111. int i, j;
  112. for (i = 0, j = 0; i < 32; i += 4, ++j) {
  113. context->state[8+j] = ((unsigned)input[i] << 24) | ((unsigned)input[i+1] << 16) |
  114. ((unsigned)input[i+2] << 8) | (unsigned)input[i+3];
  115. }
  116. Snefru(context->state);
  117. ZEND_SECURE_ZERO(&context->state[8], sizeof(uint32_t) * 8);
  118. }
  119. PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  120. {
  121. memset(context, 0, sizeof(*context));
  122. }
  123. static const uint32_t MAX32 = 0xffffffffLU;
  124. PHP_HASH_API void PHP_SNEFRUUpdate(PHP_SNEFRU_CTX *context, const unsigned char *input, size_t len)
  125. {
  126. if ((MAX32 - context->count[1]) < (len * 8)) {
  127. context->count[0]++;
  128. context->count[1] = MAX32 - context->count[1];
  129. context->count[1] = ((uint32_t) len * 8) - context->count[1];
  130. } else {
  131. context->count[1] += (uint32_t) len * 8;
  132. }
  133. if (context->length + len < 32) {
  134. memcpy(&context->buffer[context->length], input, len);
  135. context->length += (unsigned char)len;
  136. } else {
  137. size_t i = 0, r = (context->length + len) % 32;
  138. if (context->length) {
  139. i = 32 - context->length;
  140. memcpy(&context->buffer[context->length], input, i);
  141. SnefruTransform(context, context->buffer);
  142. }
  143. for (; i + 32 <= len; i += 32) {
  144. SnefruTransform(context, input + i);
  145. }
  146. memcpy(context->buffer, input + i, r);
  147. ZEND_SECURE_ZERO(&context->buffer[r], 32 - r);
  148. context->length = (unsigned char)r;
  149. }
  150. }
  151. PHP_HASH_API void PHP_SNEFRUFinal(unsigned char digest[32], PHP_SNEFRU_CTX *context)
  152. {
  153. uint32_t i, j;
  154. if (context->length) {
  155. SnefruTransform(context, context->buffer);
  156. }
  157. context->state[14] = context->count[0];
  158. context->state[15] = context->count[1];
  159. Snefru(context->state);
  160. for (i = 0, j = 0; j < 32; i++, j += 4) {
  161. digest[j] = (unsigned char) ((context->state[i] >> 24) & 0xff);
  162. digest[j + 1] = (unsigned char) ((context->state[i] >> 16) & 0xff);
  163. digest[j + 2] = (unsigned char) ((context->state[i] >> 8) & 0xff);
  164. digest[j + 3] = (unsigned char) (context->state[i] & 0xff);
  165. }
  166. ZEND_SECURE_ZERO(context, sizeof(*context));
  167. }
  168. static int php_snefru_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv)
  169. {
  170. PHP_SNEFRU_CTX *ctx = (PHP_SNEFRU_CTX *) hash->context;
  171. int r = FAILURE;
  172. if (magic == PHP_HASH_SERIALIZE_MAGIC_SPEC
  173. && (r = php_hash_unserialize_spec(hash, zv, PHP_SNEFRU_SPEC)) == SUCCESS
  174. && ctx->length < sizeof(ctx->buffer)) {
  175. return SUCCESS;
  176. } else {
  177. return r != SUCCESS ? r : -2000;
  178. }
  179. }
  180. const php_hash_ops php_hash_snefru_ops = {
  181. "snefru",
  182. (php_hash_init_func_t) PHP_SNEFRUInit,
  183. (php_hash_update_func_t) PHP_SNEFRUUpdate,
  184. (php_hash_final_func_t) PHP_SNEFRUFinal,
  185. php_hash_copy,
  186. php_hash_serialize,
  187. php_snefru_unserialize,
  188. PHP_SNEFRU_SPEC,
  189. 32,
  190. 32,
  191. sizeof(PHP_SNEFRU_CTX),
  192. 1
  193. };