PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/hash/hash_haval.c

http://github.com/infusion/PHP
C | 551 lines | 336 code | 101 blank | 114 comment | 25 complexity | 626e1897f47a2bf65c2df90098261012 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 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. | Author: Sara Golemon <pollita@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: hash_haval.c 306939 2011-01-01 02:19:59Z felipe $ */
  19. #include "php_hash.h"
  20. #include "php_hash_haval.h"
  21. static const unsigned char PADDING[128] ={
  22. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  23. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  27. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  30. static const php_hash_uint32 D0[8] = {
  31. 0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, 0xA4093822, 0x299F31D0, 0x082EFA98, 0xEC4E6C89 };
  32. static const php_hash_uint32 K2[32] = {
  33. 0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C, 0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917,
  34. 0x9216D5D9, 0x8979FB1B, 0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7, 0xB8E1AFED, 0x6A267E96,
  35. 0xBA7C9045, 0xF12C7F99, 0x24A19947, 0xB3916CF7, 0x0801F2E2, 0x858EFC16, 0x636920D8, 0x71574E69,
  36. 0xA458FEA3, 0xF4933D7E, 0x0D95748F, 0x728EB658, 0x718BCD58, 0x82154AEE, 0x7B54A41D, 0xC25A59B5 };
  37. static const php_hash_uint32 K3[32] = {
  38. 0x9C30D539, 0x2AF26013, 0xC5D1B023, 0x286085F0, 0xCA417918, 0xB8DB38EF, 0x8E79DCB0, 0x603A180E,
  39. 0x6C9E0E8B, 0xB01E8A3E, 0xD71577C1, 0xBD314B27, 0x78AF2FDA, 0x55605C60, 0xE65525F3, 0xAA55AB94,
  40. 0x57489862, 0x63E81440, 0x55CA396A, 0x2AAB10B6, 0xB4CC5C34, 0x1141E8CE, 0xA15486AF, 0x7C72E993,
  41. 0xB3EE1411, 0x636FBC2A, 0x2BA9C55D, 0x741831F6, 0xCE5C3E16, 0x9B87931E, 0xAFD6BA33, 0x6C24CF5C };
  42. static const php_hash_uint32 K4[32] = {
  43. 0x7A325381, 0x28958677, 0x3B8F4898, 0x6B4BB9AF, 0xC4BFE81B, 0x66282193, 0x61D809CC, 0xFB21A991,
  44. 0x487CAC60, 0x5DEC8032, 0xEF845D5D, 0xE98575B1, 0xDC262302, 0xEB651B88, 0x23893E81, 0xD396ACC5,
  45. 0x0F6D6FF3, 0x83F44239, 0x2E0B4482, 0xA4842004, 0x69C8F04A, 0x9E1F9B5E, 0x21C66842, 0xF6E96C9A,
  46. 0x670C9C61, 0xABD388F0, 0x6A51A0D2, 0xD8542F68, 0x960FA728, 0xAB5133A3, 0x6EEF0B6C, 0x137A3BE4 };
  47. static const php_hash_uint32 K5[32] = {
  48. 0xBA3BF050, 0x7EFB2A98, 0xA1F1651D, 0x39AF0176, 0x66CA593E, 0x82430E88, 0x8CEE8619, 0x456F9FB4,
  49. 0x7D84A5C3, 0x3B8B5EBE, 0xE06F75D8, 0x85C12073, 0x401A449F, 0x56C16AA6, 0x4ED3AA62, 0x363F7706,
  50. 0x1BFEDF72, 0x429B023D, 0x37D0D724, 0xD00A1248, 0xDB0FEAD3, 0x49F1C09B, 0x075372C9, 0x80991B7B,
  51. 0x25D479D8, 0xF6E8DEF7, 0xE3FE501A, 0xB6794C3B, 0x976CE0BD, 0x04C006BA, 0xC1A94FB6, 0x409F60C4 };
  52. static const short I2[32] = { 5, 14, 26, 18, 11, 28, 7, 16, 0, 23, 20, 22, 1, 10, 4, 8,
  53. 30, 3, 21, 9, 17, 24, 29, 6, 19, 12, 15, 13, 2, 25, 31, 27 };
  54. static const short I3[32] = { 19, 9, 4, 20, 28, 17, 8, 22, 29, 14, 25, 12, 24, 30, 16, 26,
  55. 31, 15, 7, 3, 1, 0, 18, 27, 13, 6, 21, 10, 23, 11, 5, 2 };
  56. static const short I4[32] = { 24, 4, 0, 14, 2, 7, 28, 23, 26, 6, 30, 20, 18, 25, 19, 3,
  57. 22, 11, 31, 21, 8, 27, 12, 9, 1, 29, 5, 15, 17, 10, 16, 13 };
  58. static const short I5[32] = { 27, 3, 21, 26, 17, 11, 20, 29, 19, 0, 12, 7, 13, 8, 31, 10,
  59. 5, 9, 14, 30, 18, 6, 28, 24, 2, 23, 16, 22, 4, 1, 25, 15 };
  60. static const short M0[32] = { 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1,
  61. 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1 };
  62. static const short M1[32] = { 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2,
  63. 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2 };
  64. static const short M2[32] = { 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3,
  65. 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3 };
  66. static const short M3[32] = { 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4,
  67. 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4 };
  68. static const short M4[32] = { 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5,
  69. 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5 };
  70. static const short M5[32] = { 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6,
  71. 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6 };
  72. static const short M6[32] = { 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7,
  73. 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7 };
  74. static const short M7[32] = { 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0,
  75. 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0 };
  76. /* {{{ Encode
  77. Encodes input (php_hash_uint32) into output (unsigned char). Assumes len is
  78. a multiple of 4.
  79. */
  80. static void Encode(unsigned char *output, php_hash_uint32 *input, unsigned int len)
  81. {
  82. unsigned int i, j;
  83. for (i = 0, j = 0; j < len; i++, j += 4) {
  84. output[j] = (unsigned char) (input[i] & 0xff);
  85. output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
  86. output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
  87. output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
  88. }
  89. }
  90. /* }}} */
  91. /* {{{ Decode
  92. Decodes input (unsigned char) into output (php_hash_uint32). Assumes len is
  93. a multiple of 4.
  94. */
  95. static void Decode(php_hash_uint32 *output, const unsigned char *input, unsigned int len)
  96. {
  97. unsigned int i, j;
  98. for (i = 0, j = 0; j < len; i++, j += 4) {
  99. output[i] = ((php_hash_uint32) input[j]) | (((php_hash_uint32) input[j + 1]) << 8) |
  100. (((php_hash_uint32) input[j + 2]) << 16) | (((php_hash_uint32) input[j + 3]) << 24);
  101. }
  102. }
  103. /* }}} */
  104. #define F1(x6,x5,x4,x3,x2,x1,x0) ( ((x1) & (x4)) ^ ((x2) & (x5)) ^ ((x3) & (x6)) ^ ((x0) & (x1)) ^ (x0) )
  105. #define F2(x6,x5,x4,x3,x2,x1,x0) ( ((x1) & (x2) & (x3)) ^ ((x2) & (x4) & (x5)) ^ ((x1) & (x2)) ^ ((x1) & (x4)) ^ \
  106. ((x2) & (x6)) ^ ((x3) & (x5)) ^ ((x4) & (x5)) ^ ((x0) & (x2)) ^ (x0) )
  107. #define F3(x6,x5,x4,x3,x2,x1,x0) ( ((x1) & (x2) & (x3)) ^ ((x1) & (x4)) ^ ((x2) & (x5)) ^ ((x3) & (x6)) ^ ((x0) & (x3)) ^ (x0) )
  108. #define F4(x6,x5,x4,x3,x2,x1,x0) ( ((x1) & (x2) & (x3)) ^ ((x2) & (x4) & (x5)) ^ ((x3) & (x4) & (x6)) ^ \
  109. ((x1) & (x4)) ^ ((x2) & (x6)) ^ ((x3) & (x4)) ^ ((x3) & (x5)) ^ \
  110. ((x3) & (x6)) ^ ((x4) & (x5)) ^ ((x4) & (x6)) ^ ((x0) & (x4)) ^ (x0) )
  111. #define F5(x6,x5,x4,x3,x2,x1,x0) ( ((x1) & (x4)) ^ ((x2) & (x5)) ^ ((x3) & (x6)) ^ \
  112. ((x0) & (x1) & (x2) & (x3)) ^ ((x0) & (x5)) ^ (x0) )
  113. #define ROTR(x,n) (((x) >> (n)) | ((x) << (32 - (n))))
  114. /* {{{ PHP_3HAVALTransform
  115. */
  116. static void PHP_3HAVALTransform(php_hash_uint32 state[8], const unsigned char block[128])
  117. {
  118. php_hash_uint32 E[8];
  119. php_hash_uint32 x[32];
  120. int i;
  121. Decode(x, block, 128);
  122. for(i = 0; i < 8; i++) {
  123. E[i] = state[i];
  124. }
  125. for(i = 0; i < 32; i++) {
  126. E[7 - (i % 8)] = ROTR(F1(E[M1[i]],E[M0[i]],E[M3[i]],E[M5[i]],E[M6[i]],E[M2[i]],E[M4[i]]),7) + ROTR(E[M7[i]],11) + x[i];
  127. }
  128. for(i = 0; i < 32; i++) {
  129. E[7 - (i % 8)] = ROTR(F2(E[M4[i]],E[M2[i]],E[M1[i]],E[M0[i]],E[M5[i]],E[M3[i]],E[M6[i]]),7) + ROTR(E[M7[i]],11) + x[I2[i]] + K2[i];
  130. }
  131. for(i = 0; i < 32; i++) {
  132. E[7 - (i % 8)] = ROTR(F3(E[M6[i]],E[M1[i]],E[M2[i]],E[M3[i]],E[M4[i]],E[M5[i]],E[M0[i]]),7) + ROTR(E[M7[i]],11) + x[I3[i]] + K3[i];
  133. }
  134. /* Update digest */
  135. for(i = 0; i < 8; i++) {
  136. state[i] += E[i];
  137. }
  138. /* Zeroize sensitive information. */
  139. memset((unsigned char*) x, 0, sizeof(x));
  140. }
  141. /* }}} */
  142. /* {{{ PHP_4HAVALTransform
  143. */
  144. static void PHP_4HAVALTransform(php_hash_uint32 state[8], const unsigned char block[128])
  145. {
  146. php_hash_uint32 E[8];
  147. php_hash_uint32 x[32];
  148. int i;
  149. Decode(x, block, 128);
  150. for(i = 0; i < 8; i++) {
  151. E[i] = state[i];
  152. }
  153. for(i = 0; i < 32; i++) {
  154. E[7 - (i % 8)] = ROTR(F1(E[M2[i]],E[M6[i]],E[M1[i]],E[M4[i]],E[M5[i]],E[M3[i]],E[M0[i]]),7) + ROTR(E[M7[i]],11) + x[i];
  155. }
  156. for(i = 0; i < 32; i++) {
  157. E[7 - (i % 8)] = ROTR(F2(E[M3[i]],E[M5[i]],E[M2[i]],E[M0[i]],E[M1[i]],E[M6[i]],E[M4[i]]),7) + ROTR(E[M7[i]],11) + x[I2[i]] + K2[i];
  158. }
  159. for(i = 0; i < 32; i++) {
  160. E[7 - (i % 8)] = ROTR(F3(E[M1[i]],E[M4[i]],E[M3[i]],E[M6[i]],E[M0[i]],E[M2[i]],E[M5[i]]),7) + ROTR(E[M7[i]],11) + x[I3[i]] + K3[i];
  161. }
  162. for(i = 0; i < 32; i++) {
  163. E[7 - (i % 8)] = ROTR(F4(E[M6[i]],E[M4[i]],E[M0[i]],E[M5[i]],E[M2[i]],E[M1[i]],E[M3[i]]),7) + ROTR(E[M7[i]],11) + x[I4[i]] + K4[i];
  164. }
  165. /* Update digest */
  166. for(i = 0; i < 8; i++) {
  167. state[i] += E[i];
  168. }
  169. /* Zeroize sensitive information. */
  170. memset((unsigned char*) x, 0, sizeof(x));
  171. }
  172. /* }}} */
  173. /* {{{ PHP_5HAVALTransform
  174. */
  175. static void PHP_5HAVALTransform(php_hash_uint32 state[8], const unsigned char block[128])
  176. {
  177. php_hash_uint32 E[8];
  178. php_hash_uint32 x[32];
  179. int i;
  180. Decode(x, block, 128);
  181. for(i = 0; i < 8; i++) {
  182. E[i] = state[i];
  183. }
  184. for(i = 0; i < 32; i++) {
  185. E[7 - (i % 8)] = ROTR(F1(E[M3[i]],E[M4[i]],E[M1[i]],E[M0[i]],E[M5[i]],E[M2[i]],E[M6[i]]),7) + ROTR(E[M7[i]],11) + x[i];
  186. }
  187. for(i = 0; i < 32; i++) {
  188. E[7 - (i % 8)] = ROTR(F2(E[M6[i]],E[M2[i]],E[M1[i]],E[M0[i]],E[M3[i]],E[M4[i]],E[M5[i]]),7) + ROTR(E[M7[i]],11) + x[I2[i]] + K2[i];
  189. }
  190. for(i = 0; i < 32; i++) {
  191. E[7 - (i % 8)] = ROTR(F3(E[M2[i]],E[M6[i]],E[M0[i]],E[M4[i]],E[M3[i]],E[M1[i]],E[M5[i]]),7) + ROTR(E[M7[i]],11) + x[I3[i]] + K3[i];
  192. }
  193. for(i = 0; i < 32; i++) {
  194. E[7 - (i % 8)] = ROTR(F4(E[M1[i]],E[M5[i]],E[M3[i]],E[M2[i]],E[M0[i]],E[M4[i]],E[M6[i]]),7) + ROTR(E[M7[i]],11) + x[I4[i]] + K4[i];
  195. }
  196. for(i = 0; i < 32; i++) {
  197. E[7 - (i % 8)] = ROTR(F5(E[M2[i]],E[M5[i]],E[M0[i]],E[M6[i]],E[M4[i]],E[M3[i]],E[M1[i]]),7) + ROTR(E[M7[i]],11) + x[I5[i]] + K5[i];
  198. }
  199. /* Update digest */
  200. for(i = 0; i < 8; i++) {
  201. state[i] += E[i];
  202. }
  203. /* Zeroize sensitive information. */
  204. memset((unsigned char*) x, 0, sizeof(x));
  205. }
  206. /* }}} */
  207. #define PHP_HASH_HAVAL_INIT(p,b) \
  208. const php_hash_ops php_hash_##p##haval##b##_ops = { \
  209. (php_hash_init_func_t) PHP_##p##HAVAL##b##Init, \
  210. (php_hash_update_func_t) PHP_HAVALUpdate, \
  211. (php_hash_final_func_t) PHP_HAVAL##b##Final, \
  212. (php_hash_copy_func_t) php_hash_copy, \
  213. ((b) / 8), 128, sizeof(PHP_HAVAL_CTX) }; \
  214. PHP_HASH_API void PHP_##p##HAVAL##b##Init(PHP_HAVAL_CTX *context) \
  215. { int i; context->count[0] = context->count[1] = 0; \
  216. for(i = 0; i < 8; i++) context->state[i] = D0[i]; \
  217. context->passes = p; context->output = b; \
  218. context->Transform = PHP_##p##HAVALTransform; }
  219. PHP_HASH_HAVAL_INIT(3,128)
  220. PHP_HASH_HAVAL_INIT(3,160)
  221. PHP_HASH_HAVAL_INIT(3,192)
  222. PHP_HASH_HAVAL_INIT(3,224)
  223. PHP_HASH_HAVAL_INIT(3,256)
  224. PHP_HASH_HAVAL_INIT(4,128)
  225. PHP_HASH_HAVAL_INIT(4,160)
  226. PHP_HASH_HAVAL_INIT(4,192)
  227. PHP_HASH_HAVAL_INIT(4,224)
  228. PHP_HASH_HAVAL_INIT(4,256)
  229. PHP_HASH_HAVAL_INIT(5,128)
  230. PHP_HASH_HAVAL_INIT(5,160)
  231. PHP_HASH_HAVAL_INIT(5,192)
  232. PHP_HASH_HAVAL_INIT(5,224)
  233. PHP_HASH_HAVAL_INIT(5,256)
  234. /* {{{ PHP_HAVALUpdate
  235. */
  236. PHP_HASH_API void PHP_HAVALUpdate(PHP_HAVAL_CTX *context, const unsigned char *input, unsigned int inputLen)
  237. {
  238. unsigned int i, index, partLen;
  239. /* Compute number of bytes mod 128 */
  240. index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
  241. /* Update number of bits */
  242. if ((context->count[0] += ((php_hash_uint32) inputLen << 3)) < ((php_hash_uint32) inputLen << 3)) {
  243. context->count[1]++;
  244. }
  245. context->count[1] += ((php_hash_uint32) inputLen >> 29);
  246. partLen = 128 - index;
  247. /* Transform as many times as possible.
  248. */
  249. if (inputLen >= partLen) {
  250. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  251. context->Transform(context->state, context->buffer);
  252. for (i = partLen; i + 127 < inputLen; i += 128) {
  253. context->Transform(context->state, &input[i]);
  254. }
  255. index = 0;
  256. } else {
  257. i = 0;
  258. }
  259. /* Buffer remaining input */
  260. memcpy((unsigned char*) &context->buffer[index], (unsigned char*) &input[i], inputLen - i);
  261. }
  262. /* }}} */
  263. #define PHP_HASH_HAVAL_VERSION 0x01
  264. /* {{{ PHP_HAVAL128Final
  265. */
  266. PHP_HASH_API void PHP_HAVAL128Final(unsigned char *digest, PHP_HAVAL_CTX * context)
  267. {
  268. unsigned char bits[10];
  269. unsigned int index, padLen;
  270. /* Version, Passes, and Digest Length */
  271. bits[0] = (PHP_HASH_HAVAL_VERSION & 0x07) |
  272. ((context->passes & 0x07) << 3) |
  273. ((context->output & 0x03) << 6);
  274. bits[1] = (context->output >> 2);
  275. /* Save number of bits */
  276. Encode(bits + 2, context->count, 8);
  277. /* Pad out to 118 mod 128.
  278. */
  279. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  280. padLen = (index < 118) ? (118 - index) : (246 - index);
  281. PHP_HAVALUpdate(context, PADDING, padLen);
  282. /* Append version, passes, digest length, and message length */
  283. PHP_HAVALUpdate(context, bits, 10);
  284. /* Store state in digest */
  285. context->state[3] += (context->state[7] & 0xFF000000) |
  286. (context->state[6] & 0x00FF0000) |
  287. (context->state[5] & 0x0000FF00) |
  288. (context->state[4] & 0x000000FF);
  289. context->state[2] += (((context->state[7] & 0x00FF0000) |
  290. (context->state[6] & 0x0000FF00) |
  291. (context->state[5] & 0x000000FF)) << 8) |
  292. ((context->state[4] & 0xFF000000) >> 24);
  293. context->state[1] += (((context->state[7] & 0x0000FF00) |
  294. (context->state[6] & 0x000000FF)) << 16) |
  295. (((context->state[5] & 0xFF000000) |
  296. (context->state[4] & 0x00FF0000)) >> 16);
  297. context->state[0] += ((context->state[7] & 0x000000FF) << 24) |
  298. (((context->state[6] & 0xFF000000) |
  299. (context->state[5] & 0x00FF0000) |
  300. (context->state[4] & 0x0000FF00)) >> 8);
  301. Encode(digest, context->state, 16);
  302. /* Zeroize sensitive information.
  303. */
  304. memset((unsigned char*) context, 0, sizeof(*context));
  305. }
  306. /* }}} */
  307. /* {{{ PHP_HAVAL160Final
  308. */
  309. PHP_HASH_API void PHP_HAVAL160Final(unsigned char *digest, PHP_HAVAL_CTX * context)
  310. {
  311. unsigned char bits[10];
  312. unsigned int index, padLen;
  313. /* Version, Passes, and Digest Length */
  314. bits[0] = (PHP_HASH_HAVAL_VERSION & 0x07) |
  315. ((context->passes & 0x07) << 3) |
  316. ((context->output & 0x03) << 6);
  317. bits[1] = (context->output >> 2);
  318. /* Save number of bits */
  319. Encode(bits + 2, context->count, 8);
  320. /* Pad out to 118 mod 128.
  321. */
  322. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  323. padLen = (index < 118) ? (118 - index) : (246 - index);
  324. PHP_HAVALUpdate(context, PADDING, padLen);
  325. /* Append version, passes, digest length, and message length */
  326. PHP_HAVALUpdate(context, bits, 10);
  327. /* Store state in digest */
  328. context->state[4] += ((context->state[7] & 0xFE000000) |
  329. (context->state[6] & 0x01F80000) |
  330. (context->state[5] & 0x0007F000)) >> 12;
  331. context->state[3] += ((context->state[7] & 0x01F80000) |
  332. (context->state[6] & 0x0007F000) |
  333. (context->state[5] & 0x00000FC0)) >> 6;
  334. context->state[2] += (context->state[7] & 0x0007F000) |
  335. (context->state[6] & 0x00000FC0) |
  336. (context->state[5] & 0x0000003F);
  337. context->state[1] += ROTR((context->state[7] & 0x00000FC0) |
  338. (context->state[6] & 0x0000003F) |
  339. (context->state[5] & 0xFE000000), 25);
  340. context->state[0] += ROTR((context->state[7] & 0x0000003F) |
  341. (context->state[6] & 0xFE000000) |
  342. (context->state[5] & 0x01F80000), 19);
  343. Encode(digest, context->state, 20);
  344. /* Zeroize sensitive information.
  345. */
  346. memset((unsigned char*) context, 0, sizeof(*context));
  347. }
  348. /* }}} */
  349. /* {{{ PHP_HAVAL192Final
  350. */
  351. PHP_HASH_API void PHP_HAVAL192Final(unsigned char *digest, PHP_HAVAL_CTX * context)
  352. {
  353. unsigned char bits[10];
  354. unsigned int index, padLen;
  355. /* Version, Passes, and Digest Length */
  356. bits[0] = (PHP_HASH_HAVAL_VERSION & 0x07) |
  357. ((context->passes & 0x07) << 3) |
  358. ((context->output & 0x03) << 6);
  359. bits[1] = (context->output >> 2);
  360. /* Save number of bits */
  361. Encode(bits + 2, context->count, 8);
  362. /* Pad out to 118 mod 128.
  363. */
  364. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  365. padLen = (index < 118) ? (118 - index) : (246 - index);
  366. PHP_HAVALUpdate(context, PADDING, padLen);
  367. /* Append version, passes, digest length, and message length */
  368. PHP_HAVALUpdate(context, bits, 10);
  369. /* Store state in digest */
  370. context->state[5] += ((context->state[7] & 0xFC000000) | (context->state[6] & 0x03E00000)) >> 21;
  371. context->state[4] += ((context->state[7] & 0x03E00000) | (context->state[6] & 0x001F0000)) >> 16;
  372. context->state[3] += ((context->state[7] & 0x001F0000) | (context->state[6] & 0x0000FC00)) >> 10;
  373. context->state[2] += ((context->state[7] & 0x0000FC00) | (context->state[6] & 0x000003E0)) >> 5;
  374. context->state[1] += (context->state[7] & 0x000003E0) | (context->state[6] & 0x0000001F);
  375. context->state[0] += ROTR((context->state[7] & 0x0000001F) | (context->state[6] & 0xFC000000), 26);
  376. Encode(digest, context->state, 24);
  377. /* Zeroize sensitive information.
  378. */
  379. memset((unsigned char*) context, 0, sizeof(*context));
  380. }
  381. /* }}} */
  382. /* {{{ PHP_HAVAL224Final
  383. */
  384. PHP_HASH_API void PHP_HAVAL224Final(unsigned char *digest, PHP_HAVAL_CTX * context)
  385. {
  386. unsigned char bits[10];
  387. unsigned int index, padLen;
  388. /* Version, Passes, and Digest Length */
  389. bits[0] = (PHP_HASH_HAVAL_VERSION & 0x07) |
  390. ((context->passes & 0x07) << 3) |
  391. ((context->output & 0x03) << 6);
  392. bits[1] = (context->output >> 2);
  393. /* Save number of bits */
  394. Encode(bits + 2, context->count, 8);
  395. /* Pad out to 118 mod 128.
  396. */
  397. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  398. padLen = (index < 118) ? (118 - index) : (246 - index);
  399. PHP_HAVALUpdate(context, PADDING, padLen);
  400. /* Append version, passes, digest length, and message length */
  401. PHP_HAVALUpdate(context, bits, 10);
  402. /* Store state in digest */
  403. context->state[6] += context->state[7] & 0x0000000F;
  404. context->state[5] += (context->state[7] >> 4) & 0x0000001F;
  405. context->state[4] += (context->state[7] >> 9) & 0x0000000F;
  406. context->state[3] += (context->state[7] >> 13) & 0x0000001F;
  407. context->state[2] += (context->state[7] >> 18) & 0x0000000F;
  408. context->state[1] += (context->state[7] >> 22) & 0x0000001F;
  409. context->state[0] += (context->state[7] >> 27) & 0x0000001F;
  410. Encode(digest, context->state, 28);
  411. /* Zeroize sensitive information.
  412. */
  413. memset((unsigned char*) context, 0, sizeof(*context));
  414. }
  415. /* }}} */
  416. /* {{{ PHP_HAVAL256Final
  417. */
  418. PHP_HASH_API void PHP_HAVAL256Final(unsigned char *digest, PHP_HAVAL_CTX * context)
  419. {
  420. unsigned char bits[10];
  421. unsigned int index, padLen;
  422. /* Version, Passes, and Digest Length */
  423. bits[0] = (PHP_HASH_HAVAL_VERSION & 0x07) |
  424. ((context->passes & 0x07) << 3) |
  425. ((context->output & 0x03) << 6);
  426. bits[1] = (context->output >> 2);
  427. /* Save number of bits */
  428. Encode(bits + 2, context->count, 8);
  429. /* Pad out to 118 mod 128.
  430. */
  431. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  432. padLen = (index < 118) ? (118 - index) : (246 - index);
  433. PHP_HAVALUpdate(context, PADDING, padLen);
  434. /* Append version, passes, digest length, and message length */
  435. PHP_HAVALUpdate(context, bits, 10);
  436. /* Store state in digest */
  437. Encode(digest, context->state, 32);
  438. /* Zeroize sensitive information.
  439. */
  440. memset((unsigned char*) context, 0, sizeof(*context));
  441. }
  442. /* }}} */
  443. /*
  444. * Local variables:
  445. * tab-width: 4
  446. * c-basic-offset: 4
  447. * End:
  448. * vim600: sw=4 ts=4 fdm=marker
  449. * vim<600: sw=4 ts=4
  450. */