hash_haval.c 19 KB

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