hash.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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. | Author: Sara Golemon <pollita@php.net> |
  14. | Scott MacVicar <scottmac@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <math.h>
  21. #include "php_hash.h"
  22. #include "ext/standard/info.h"
  23. #include "ext/standard/file.h"
  24. #include "ext/standard/php_var.h"
  25. #include "ext/spl/spl_exceptions.h"
  26. #include "zend_interfaces.h"
  27. #include "zend_exceptions.h"
  28. #include "zend_smart_str.h"
  29. #include "hash_arginfo.h"
  30. #ifdef PHP_WIN32
  31. # define __alignof__ __alignof
  32. #else
  33. # ifndef HAVE_ALIGNOF
  34. # include <stddef.h>
  35. # define __alignof__(type) offsetof (struct { char c; type member;}, member)
  36. # endif
  37. #endif
  38. HashTable php_hash_hashtable;
  39. zend_class_entry *php_hashcontext_ce;
  40. static zend_object_handlers php_hashcontext_handlers;
  41. #ifdef PHP_MHASH_BC
  42. struct mhash_bc_entry {
  43. char *mhash_name;
  44. char *hash_name;
  45. int value;
  46. };
  47. #define MHASH_NUM_ALGOS 42
  48. static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
  49. {"CRC32", "crc32", 0}, /* used by bzip */
  50. {"MD5", "md5", 1},
  51. {"SHA1", "sha1", 2},
  52. {"HAVAL256", "haval256,3", 3},
  53. {NULL, NULL, 4},
  54. {"RIPEMD160", "ripemd160", 5},
  55. {NULL, NULL, 6},
  56. {"TIGER", "tiger192,3", 7},
  57. {"GOST", "gost", 8},
  58. {"CRC32B", "crc32b", 9}, /* used by ethernet (IEEE 802.3), gzip, zip, png, etc */
  59. {"HAVAL224", "haval224,3", 10},
  60. {"HAVAL192", "haval192,3", 11},
  61. {"HAVAL160", "haval160,3", 12},
  62. {"HAVAL128", "haval128,3", 13},
  63. {"TIGER128", "tiger128,3", 14},
  64. {"TIGER160", "tiger160,3", 15},
  65. {"MD4", "md4", 16},
  66. {"SHA256", "sha256", 17},
  67. {"ADLER32", "adler32", 18},
  68. {"SHA224", "sha224", 19},
  69. {"SHA512", "sha512", 20},
  70. {"SHA384", "sha384", 21},
  71. {"WHIRLPOOL", "whirlpool", 22},
  72. {"RIPEMD128", "ripemd128", 23},
  73. {"RIPEMD256", "ripemd256", 24},
  74. {"RIPEMD320", "ripemd320", 25},
  75. {NULL, NULL, 26}, /* support needs to be added for snefru 128 */
  76. {"SNEFRU256", "snefru256", 27},
  77. {"MD2", "md2", 28},
  78. {"FNV132", "fnv132", 29},
  79. {"FNV1A32", "fnv1a32", 30},
  80. {"FNV164", "fnv164", 31},
  81. {"FNV1A64", "fnv1a64", 32},
  82. {"JOAAT", "joaat", 33},
  83. {"CRC32C", "crc32c", 34}, /* Castagnoli's CRC, used by iSCSI, SCTP, Btrfs, ext4, etc */
  84. {"MURMUR3A", "murmur3a", 35},
  85. {"MURMUR3C", "murmur3c", 36},
  86. {"MURMUR3F", "murmur3f", 37},
  87. {"XXH32", "xxh32", 38},
  88. {"XXH64", "xxh64", 39},
  89. {"XXH3", "xxh3", 40},
  90. {"XXH128", "xxh128", 41},
  91. };
  92. #endif
  93. /* Hash Registry Access */
  94. PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(zend_string *algo) /* {{{ */
  95. {
  96. zend_string *lower = zend_string_tolower(algo);
  97. php_hash_ops *ops = zend_hash_find_ptr(&php_hash_hashtable, lower);
  98. zend_string_release(lower);
  99. return ops;
  100. }
  101. /* }}} */
  102. PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops) /* {{{ */
  103. {
  104. size_t algo_len = strlen(algo);
  105. char *lower = zend_str_tolower_dup(algo, algo_len);
  106. zend_hash_add_ptr(&php_hash_hashtable, zend_string_init_interned(lower, algo_len, 1), (void *) ops);
  107. efree(lower);
  108. }
  109. /* }}} */
  110. PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_context) /* {{{ */
  111. {
  112. php_hash_ops *hash_ops = (php_hash_ops *)ops;
  113. memcpy(dest_context, orig_context, hash_ops->context_size);
  114. return SUCCESS;
  115. }
  116. /* }}} */
  117. static inline size_t align_to(size_t pos, size_t alignment) {
  118. size_t offset = pos & (alignment - 1);
  119. return pos + (offset ? alignment - offset : 0);
  120. }
  121. static size_t parse_serialize_spec(
  122. const char **specp, size_t *pos, size_t *sz, size_t *max_alignment) {
  123. size_t count, alignment;
  124. const char *spec = *specp;
  125. /* parse size */
  126. if (*spec == 's' || *spec == 'S') {
  127. *sz = 2;
  128. alignment = __alignof__(uint16_t); /* usually 2 */
  129. } else if (*spec == 'l' || *spec == 'L') {
  130. *sz = 4;
  131. alignment = __alignof__(uint32_t); /* usually 4 */
  132. } else if (*spec == 'q' || *spec == 'Q') {
  133. *sz = 8;
  134. alignment = __alignof__(uint64_t); /* usually 8 */
  135. } else if (*spec == 'i' || *spec == 'I') {
  136. *sz = sizeof(int);
  137. alignment = __alignof__(int); /* usually 4 */
  138. } else {
  139. ZEND_ASSERT(*spec == 'b' || *spec == 'B');
  140. *sz = 1;
  141. alignment = 1;
  142. }
  143. /* process alignment */
  144. *pos = align_to(*pos, alignment);
  145. *max_alignment = *max_alignment < alignment ? alignment : *max_alignment;
  146. /* parse count */
  147. ++spec;
  148. if (isdigit((unsigned char) *spec)) {
  149. count = 0;
  150. while (isdigit((unsigned char) *spec)) {
  151. count = 10 * count + *spec - '0';
  152. ++spec;
  153. }
  154. } else {
  155. count = 1;
  156. }
  157. *specp = spec;
  158. return count;
  159. }
  160. static uint64_t one_from_buffer(size_t sz, const unsigned char *buf) {
  161. if (sz == 2) {
  162. const uint16_t *x = (const uint16_t *) buf;
  163. return *x;
  164. } else if (sz == 4) {
  165. const uint32_t *x = (const uint32_t *) buf;
  166. return *x;
  167. } else if (sz == 8) {
  168. const uint64_t *x = (const uint64_t *) buf;
  169. return *x;
  170. } else {
  171. ZEND_ASSERT(sz == 1);
  172. return *buf;
  173. }
  174. }
  175. static void one_to_buffer(size_t sz, unsigned char *buf, uint64_t val) {
  176. if (sz == 2) {
  177. uint16_t *x = (uint16_t *) buf;
  178. *x = val;
  179. } else if (sz == 4) {
  180. uint32_t *x = (uint32_t *) buf;
  181. *x = val;
  182. } else if (sz == 8) {
  183. uint64_t *x = (uint64_t *) buf;
  184. *x = val;
  185. } else {
  186. ZEND_ASSERT(sz == 1);
  187. *buf = val;
  188. }
  189. }
  190. /* Serialize a hash context according to a `spec` string.
  191. Spec contents:
  192. b[COUNT] -- serialize COUNT bytes
  193. s[COUNT] -- serialize COUNT 16-bit integers
  194. l[COUNT] -- serialize COUNT 32-bit integers
  195. q[COUNT] -- serialize COUNT 64-bit integers
  196. i[COUNT] -- serialize COUNT `int`s
  197. B[COUNT] -- skip COUNT bytes
  198. S[COUNT], L[COUNT], etc. -- uppercase versions skip instead of read
  199. . (must be last character) -- assert that the hash context has exactly
  200. this size
  201. Example: "llllllb64l16." is the spec for an MD5 context: 6 32-bit
  202. integers, followed by 64 bytes, then 16 32-bit integers, and that's
  203. exactly the size of the context.
  204. The serialization result is an array. Each integer is serialized as a
  205. 32-bit integer, except that a run of 2 or more bytes is encoded as a
  206. string, and each 64-bit integer is serialized as two 32-bit integers, least
  207. significant bits first. This allows 32-bit and 64-bit architectures to
  208. interchange serialized HashContexts. */
  209. PHP_HASH_API int php_hash_serialize_spec(const php_hashcontext_object *hash, zval *zv, const char *spec) /* {{{ */
  210. {
  211. size_t pos = 0, max_alignment = 1;
  212. unsigned char *buf = (unsigned char *) hash->context;
  213. zval tmp;
  214. if (buf == NULL) {
  215. return FAILURE;
  216. }
  217. array_init(zv);
  218. while (*spec != '\0' && *spec != '.') {
  219. char spec_ch = *spec;
  220. size_t sz, count = parse_serialize_spec(&spec, &pos, &sz, &max_alignment);
  221. if (pos + count * sz > hash->ops->context_size) {
  222. return FAILURE;
  223. }
  224. if (isupper((unsigned char) spec_ch)) {
  225. pos += count * sz;
  226. } else if (sz == 1 && count > 1) {
  227. ZVAL_STRINGL(&tmp, (char *) buf + pos, count);
  228. zend_hash_next_index_insert(Z_ARRVAL_P(zv), &tmp);
  229. pos += count;
  230. } else {
  231. while (count > 0) {
  232. uint64_t val = one_from_buffer(sz, buf + pos);
  233. pos += sz;
  234. ZVAL_LONG(&tmp, (int32_t) val);
  235. zend_hash_next_index_insert(Z_ARRVAL_P(zv), &tmp);
  236. if (sz == 8) {
  237. ZVAL_LONG(&tmp, (int32_t) (val >> 32));
  238. zend_hash_next_index_insert(Z_ARRVAL_P(zv), &tmp);
  239. }
  240. --count;
  241. }
  242. }
  243. }
  244. if (*spec == '.' && align_to(pos, max_alignment) != hash->ops->context_size) {
  245. return FAILURE;
  246. }
  247. return SUCCESS;
  248. }
  249. /* }}} */
  250. /* Unserialize a hash context serialized by `php_hash_serialize_spec` with `spec`.
  251. Returns SUCCESS on success and a negative error code on failure.
  252. Codes: FAILURE (-1) == generic failure
  253. -999 == spec wrong size for context
  254. -1000 - POS == problem at byte offset POS */
  255. PHP_HASH_API int php_hash_unserialize_spec(php_hashcontext_object *hash, const zval *zv, const char *spec) /* {{{ */
  256. {
  257. size_t pos = 0, max_alignment = 1, j = 0;
  258. unsigned char *buf = (unsigned char *) hash->context;
  259. zval *elt;
  260. if (Z_TYPE_P(zv) != IS_ARRAY) {
  261. return FAILURE;
  262. }
  263. while (*spec != '\0' && *spec != '.') {
  264. char spec_ch = *spec;
  265. size_t sz, count = parse_serialize_spec(&spec, &pos, &sz, &max_alignment);
  266. if (pos + count * sz > hash->ops->context_size) {
  267. return -999;
  268. }
  269. if (isupper((unsigned char) spec_ch)) {
  270. pos += count * sz;
  271. } else if (sz == 1 && count > 1) {
  272. elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
  273. if (!elt || Z_TYPE_P(elt) != IS_STRING || Z_STRLEN_P(elt) != count) {
  274. return -1000 - pos;
  275. }
  276. ++j;
  277. memcpy(buf + pos, Z_STRVAL_P(elt), count);
  278. pos += count;
  279. } else {
  280. while (count > 0) {
  281. uint64_t val;
  282. elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
  283. if (!elt || Z_TYPE_P(elt) != IS_LONG) {
  284. return -1000 - pos;
  285. }
  286. ++j;
  287. val = (uint32_t) Z_LVAL_P(elt);
  288. if (sz == 8) {
  289. elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
  290. if (!elt || Z_TYPE_P(elt) != IS_LONG) {
  291. return -1000 - pos;
  292. }
  293. ++j;
  294. val += ((uint64_t) Z_LVAL_P(elt)) << 32;
  295. }
  296. one_to_buffer(sz, buf + pos, val);
  297. pos += sz;
  298. --count;
  299. }
  300. }
  301. }
  302. if (*spec == '.' && align_to(pos, max_alignment) != hash->ops->context_size) {
  303. return -999;
  304. }
  305. return SUCCESS;
  306. }
  307. /* }}} */
  308. PHP_HASH_API int php_hash_serialize(const php_hashcontext_object *hash, zend_long *magic, zval *zv) /* {{{ */
  309. {
  310. if (hash->ops->serialize_spec) {
  311. *magic = PHP_HASH_SERIALIZE_MAGIC_SPEC;
  312. return php_hash_serialize_spec(hash, zv, hash->ops->serialize_spec);
  313. } else {
  314. return FAILURE;
  315. }
  316. }
  317. /* }}} */
  318. PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv) /* {{{ */
  319. {
  320. if (hash->ops->serialize_spec
  321. && magic == PHP_HASH_SERIALIZE_MAGIC_SPEC) {
  322. return php_hash_unserialize_spec(hash, zv, hash->ops->serialize_spec);
  323. } else {
  324. return FAILURE;
  325. }
  326. }
  327. /* }}} */
  328. /* Userspace */
  329. static void php_hash_do_hash(
  330. zval *return_value, zend_string *algo, char *data, size_t data_len, bool raw_output, bool isfilename, HashTable *args
  331. ) /* {{{ */ {
  332. zend_string *digest;
  333. const php_hash_ops *ops;
  334. void *context;
  335. php_stream *stream = NULL;
  336. ops = php_hash_fetch_ops(algo);
  337. if (!ops) {
  338. zend_argument_value_error(1, "must be a valid hashing algorithm");
  339. RETURN_THROWS();
  340. }
  341. if (isfilename) {
  342. if (CHECK_NULL_PATH(data, data_len)) {
  343. zend_argument_value_error(1, "must not contain any null bytes");
  344. RETURN_THROWS();
  345. }
  346. stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
  347. if (!stream) {
  348. /* Stream will report errors opening file */
  349. RETURN_FALSE;
  350. }
  351. }
  352. context = php_hash_alloc_context(ops);
  353. ops->hash_init(context, args);
  354. if (isfilename) {
  355. char buf[1024];
  356. ssize_t n;
  357. while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
  358. ops->hash_update(context, (unsigned char *) buf, n);
  359. }
  360. php_stream_close(stream);
  361. if (n < 0) {
  362. efree(context);
  363. RETURN_FALSE;
  364. }
  365. } else {
  366. ops->hash_update(context, (unsigned char *) data, data_len);
  367. }
  368. digest = zend_string_alloc(ops->digest_size, 0);
  369. ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
  370. efree(context);
  371. if (raw_output) {
  372. ZSTR_VAL(digest)[ops->digest_size] = 0;
  373. RETURN_NEW_STR(digest);
  374. } else {
  375. zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
  376. php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
  377. ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
  378. zend_string_release_ex(digest, 0);
  379. RETURN_NEW_STR(hex_digest);
  380. }
  381. }
  382. /* }}} */
  383. /* {{{ Generate a hash of a given input string
  384. Returns lowercase hexits by default */
  385. PHP_FUNCTION(hash)
  386. {
  387. zend_string *algo;
  388. char *data;
  389. size_t data_len;
  390. bool raw_output = 0;
  391. HashTable *args = NULL;
  392. ZEND_PARSE_PARAMETERS_START(2, 4)
  393. Z_PARAM_STR(algo)
  394. Z_PARAM_STRING(data, data_len)
  395. Z_PARAM_OPTIONAL
  396. Z_PARAM_BOOL(raw_output)
  397. Z_PARAM_ARRAY_HT(args)
  398. ZEND_PARSE_PARAMETERS_END();
  399. php_hash_do_hash(return_value, algo, data, data_len, raw_output, 0, args);
  400. }
  401. /* }}} */
  402. /* {{{ Generate a hash of a given file
  403. Returns lowercase hexits by default */
  404. PHP_FUNCTION(hash_file)
  405. {
  406. zend_string *algo;
  407. char *data;
  408. size_t data_len;
  409. bool raw_output = 0;
  410. HashTable *args = NULL;
  411. ZEND_PARSE_PARAMETERS_START(2, 3)
  412. Z_PARAM_STR(algo)
  413. Z_PARAM_STRING(data, data_len)
  414. Z_PARAM_OPTIONAL
  415. Z_PARAM_BOOL(raw_output)
  416. Z_PARAM_ARRAY_HT(args)
  417. ZEND_PARSE_PARAMETERS_END();
  418. php_hash_do_hash(return_value, algo, data, data_len, raw_output, 1, args);
  419. }
  420. /* }}} */
  421. static inline void php_hash_string_xor_char(unsigned char *out, const unsigned char *in, const unsigned char xor_with, const size_t length) {
  422. size_t i;
  423. for (i=0; i < length; i++) {
  424. out[i] = in[i] ^ xor_with;
  425. }
  426. }
  427. static inline void php_hash_string_xor(unsigned char *out, const unsigned char *in, const unsigned char *xor_with, const size_t length) {
  428. size_t i;
  429. for (i=0; i < length; i++) {
  430. out[i] = in[i] ^ xor_with[i];
  431. }
  432. }
  433. static inline void php_hash_hmac_prep_key(unsigned char *K, const php_hash_ops *ops, void *context, const unsigned char *key, const size_t key_len) {
  434. memset(K, 0, ops->block_size);
  435. if (key_len > ops->block_size) {
  436. /* Reduce the key first */
  437. ops->hash_init(context, NULL);
  438. ops->hash_update(context, key, key_len);
  439. ops->hash_final(K, context);
  440. } else {
  441. memcpy(K, key, key_len);
  442. }
  443. /* XOR the key with 0x36 to get the ipad) */
  444. php_hash_string_xor_char(K, K, 0x36, ops->block_size);
  445. }
  446. static inline void php_hash_hmac_round(unsigned char *final, const php_hash_ops *ops, void *context, const unsigned char *key, const unsigned char *data, const zend_long data_size) {
  447. ops->hash_init(context, NULL);
  448. ops->hash_update(context, key, ops->block_size);
  449. ops->hash_update(context, data, data_size);
  450. ops->hash_final(final, context);
  451. }
  452. static void php_hash_do_hash_hmac(
  453. zval *return_value, zend_string *algo, char *data, size_t data_len, char *key, size_t key_len, bool raw_output, bool isfilename
  454. ) /* {{{ */ {
  455. zend_string *digest;
  456. unsigned char *K;
  457. const php_hash_ops *ops;
  458. void *context;
  459. php_stream *stream = NULL;
  460. ops = php_hash_fetch_ops(algo);
  461. if (!ops || !ops->is_crypto) {
  462. zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
  463. RETURN_THROWS();
  464. }
  465. if (isfilename) {
  466. if (CHECK_NULL_PATH(data, data_len)) {
  467. zend_argument_value_error(2, "must not contain any null bytes");
  468. RETURN_THROWS();
  469. }
  470. stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
  471. if (!stream) {
  472. /* Stream will report errors opening file */
  473. RETURN_FALSE;
  474. }
  475. }
  476. context = php_hash_alloc_context(ops);
  477. K = emalloc(ops->block_size);
  478. digest = zend_string_alloc(ops->digest_size, 0);
  479. php_hash_hmac_prep_key(K, ops, context, (unsigned char *) key, key_len);
  480. if (isfilename) {
  481. char buf[1024];
  482. ssize_t n;
  483. ops->hash_init(context, NULL);
  484. ops->hash_update(context, K, ops->block_size);
  485. while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
  486. ops->hash_update(context, (unsigned char *) buf, n);
  487. }
  488. php_stream_close(stream);
  489. if (n < 0) {
  490. efree(context);
  491. efree(K);
  492. zend_string_release(digest);
  493. RETURN_FALSE;
  494. }
  495. ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
  496. } else {
  497. php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) data, data_len);
  498. }
  499. php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
  500. php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
  501. /* Zero the key */
  502. ZEND_SECURE_ZERO(K, ops->block_size);
  503. efree(K);
  504. efree(context);
  505. if (raw_output) {
  506. ZSTR_VAL(digest)[ops->digest_size] = 0;
  507. RETURN_NEW_STR(digest);
  508. } else {
  509. zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
  510. php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
  511. ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
  512. zend_string_release_ex(digest, 0);
  513. RETURN_NEW_STR(hex_digest);
  514. }
  515. }
  516. /* }}} */
  517. /* {{{ Generate a hash of a given input string with a key using HMAC
  518. Returns lowercase hexits by default */
  519. PHP_FUNCTION(hash_hmac)
  520. {
  521. zend_string *algo;
  522. char *data, *key;
  523. size_t data_len, key_len;
  524. bool raw_output = 0;
  525. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
  526. RETURN_THROWS();
  527. }
  528. php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, raw_output, 0);
  529. }
  530. /* }}} */
  531. /* {{{ Generate a hash of a given file with a key using HMAC
  532. Returns lowercase hexits by default */
  533. PHP_FUNCTION(hash_hmac_file)
  534. {
  535. zend_string *algo;
  536. char *data, *key;
  537. size_t data_len, key_len;
  538. bool raw_output = 0;
  539. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
  540. RETURN_THROWS();
  541. }
  542. php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, raw_output, 1);
  543. }
  544. /* }}} */
  545. /* {{{ Initialize a hashing context */
  546. PHP_FUNCTION(hash_init)
  547. {
  548. zend_string *algo, *key = NULL;
  549. zend_long options = 0;
  550. void *context;
  551. const php_hash_ops *ops;
  552. php_hashcontext_object *hash;
  553. HashTable *args = NULL;
  554. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lSh", &algo, &options, &key, &args) == FAILURE) {
  555. RETURN_THROWS();
  556. }
  557. ops = php_hash_fetch_ops(algo);
  558. if (!ops) {
  559. zend_argument_value_error(1, "must be a valid hashing algorithm");
  560. RETURN_THROWS();
  561. }
  562. if (options & PHP_HASH_HMAC) {
  563. if (!ops->is_crypto) {
  564. zend_argument_value_error(1, "must be a cryptographic hashing algorithm if HMAC is requested");
  565. RETURN_THROWS();
  566. }
  567. if (!key || (ZSTR_LEN(key) == 0)) {
  568. /* Note: a zero length key is no key at all */
  569. zend_argument_value_error(3, "cannot be empty when HMAC is requested");
  570. RETURN_THROWS();
  571. }
  572. }
  573. object_init_ex(return_value, php_hashcontext_ce);
  574. hash = php_hashcontext_from_object(Z_OBJ_P(return_value));
  575. context = php_hash_alloc_context(ops);
  576. ops->hash_init(context, args);
  577. hash->ops = ops;
  578. hash->context = context;
  579. hash->options = options;
  580. hash->key = NULL;
  581. if (options & PHP_HASH_HMAC) {
  582. char *K = emalloc(ops->block_size);
  583. size_t i, block_size;
  584. memset(K, 0, ops->block_size);
  585. if (ZSTR_LEN(key) > ops->block_size) {
  586. /* Reduce the key first */
  587. ops->hash_update(context, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key));
  588. ops->hash_final((unsigned char *) K, context);
  589. /* Make the context ready to start over */
  590. ops->hash_init(context, args);
  591. } else {
  592. memcpy(K, ZSTR_VAL(key), ZSTR_LEN(key));
  593. }
  594. /* XOR ipad */
  595. block_size = ops->block_size;
  596. for(i = 0; i < block_size; i++) {
  597. K[i] ^= 0x36;
  598. }
  599. ops->hash_update(context, (unsigned char *) K, ops->block_size);
  600. hash->key = (unsigned char *) K;
  601. }
  602. }
  603. /* }}} */
  604. #define PHP_HASHCONTEXT_VERIFY(hash) { \
  605. if (!hash->context) { \
  606. zend_argument_type_error(1, "must be a valid Hash Context resource"); \
  607. RETURN_THROWS(); \
  608. } \
  609. }
  610. /* {{{ Pump data into the hashing algorithm */
  611. PHP_FUNCTION(hash_update)
  612. {
  613. zval *zhash;
  614. php_hashcontext_object *hash;
  615. zend_string *data;
  616. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zhash, php_hashcontext_ce, &data) == FAILURE) {
  617. RETURN_THROWS();
  618. }
  619. hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
  620. PHP_HASHCONTEXT_VERIFY(hash);
  621. hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(data), ZSTR_LEN(data));
  622. RETURN_TRUE;
  623. }
  624. /* }}} */
  625. /* {{{ Pump data into the hashing algorithm from an open stream */
  626. PHP_FUNCTION(hash_update_stream)
  627. {
  628. zval *zhash, *zstream;
  629. php_hashcontext_object *hash;
  630. php_stream *stream = NULL;
  631. zend_long length = -1, didread = 0;
  632. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Or|l", &zhash, php_hashcontext_ce, &zstream, &length) == FAILURE) {
  633. RETURN_THROWS();
  634. }
  635. hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
  636. PHP_HASHCONTEXT_VERIFY(hash);
  637. php_stream_from_zval(stream, zstream);
  638. while (length) {
  639. char buf[1024];
  640. zend_long toread = 1024;
  641. ssize_t n;
  642. if (length > 0 && toread > length) {
  643. toread = length;
  644. }
  645. if ((n = php_stream_read(stream, buf, toread)) <= 0) {
  646. RETURN_LONG(didread);
  647. }
  648. hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
  649. length -= n;
  650. didread += n;
  651. }
  652. RETURN_LONG(didread);
  653. }
  654. /* }}} */
  655. /* {{{ Pump data into the hashing algorithm from a file */
  656. PHP_FUNCTION(hash_update_file)
  657. {
  658. zval *zhash, *zcontext = NULL;
  659. php_hashcontext_object *hash;
  660. php_stream_context *context = NULL;
  661. php_stream *stream;
  662. zend_string *filename;
  663. char buf[1024];
  664. ssize_t n;
  665. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|r!", &zhash, php_hashcontext_ce, &filename, &zcontext) == FAILURE) {
  666. RETURN_THROWS();
  667. }
  668. hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
  669. PHP_HASHCONTEXT_VERIFY(hash);
  670. context = php_stream_context_from_zval(zcontext, 0);
  671. stream = php_stream_open_wrapper_ex(ZSTR_VAL(filename), "rb", REPORT_ERRORS, NULL, context);
  672. if (!stream) {
  673. /* Stream will report errors opening file */
  674. RETURN_FALSE;
  675. }
  676. while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
  677. hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
  678. }
  679. php_stream_close(stream);
  680. RETURN_BOOL(n >= 0);
  681. }
  682. /* }}} */
  683. /* {{{ Output resulting digest */
  684. PHP_FUNCTION(hash_final)
  685. {
  686. zval *zhash;
  687. php_hashcontext_object *hash;
  688. bool raw_output = 0;
  689. zend_string *digest;
  690. size_t digest_len;
  691. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &zhash, php_hashcontext_ce, &raw_output) == FAILURE) {
  692. RETURN_THROWS();
  693. }
  694. hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
  695. PHP_HASHCONTEXT_VERIFY(hash);
  696. digest_len = hash->ops->digest_size;
  697. digest = zend_string_alloc(digest_len, 0);
  698. hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
  699. if (hash->options & PHP_HASH_HMAC) {
  700. size_t i, block_size;
  701. /* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
  702. block_size = hash->ops->block_size;
  703. for(i = 0; i < block_size; i++) {
  704. hash->key[i] ^= 0x6A;
  705. }
  706. /* Feed this result into the outer hash */
  707. hash->ops->hash_init(hash->context, NULL);
  708. hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size);
  709. hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(digest), hash->ops->digest_size);
  710. hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
  711. /* Zero the key */
  712. ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
  713. efree(hash->key);
  714. hash->key = NULL;
  715. }
  716. ZSTR_VAL(digest)[digest_len] = 0;
  717. /* Invalidate the object from further use */
  718. efree(hash->context);
  719. hash->context = NULL;
  720. if (raw_output) {
  721. RETURN_NEW_STR(digest);
  722. } else {
  723. zend_string *hex_digest = zend_string_safe_alloc(digest_len, 2, 0, 0);
  724. php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), digest_len);
  725. ZSTR_VAL(hex_digest)[2 * digest_len] = 0;
  726. zend_string_release_ex(digest, 0);
  727. RETURN_NEW_STR(hex_digest);
  728. }
  729. }
  730. /* }}} */
  731. /* {{{ Copy hash object */
  732. PHP_FUNCTION(hash_copy)
  733. {
  734. zval *zhash;
  735. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zhash, php_hashcontext_ce) == FAILURE) {
  736. RETURN_THROWS();
  737. }
  738. RETVAL_OBJ(Z_OBJ_HANDLER_P(zhash, clone_obj)(Z_OBJ_P(zhash)));
  739. if (php_hashcontext_from_object(Z_OBJ_P(return_value))->context == NULL) {
  740. zval_ptr_dtor(return_value);
  741. zend_throw_error(NULL, "Cannot copy hash");
  742. RETURN_THROWS();
  743. }
  744. }
  745. /* }}} */
  746. /* {{{ Return a list of registered hashing algorithms */
  747. PHP_FUNCTION(hash_algos)
  748. {
  749. zend_string *str;
  750. if (zend_parse_parameters_none() == FAILURE) {
  751. RETURN_THROWS();
  752. }
  753. array_init(return_value);
  754. ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
  755. add_next_index_str(return_value, zend_string_copy(str));
  756. } ZEND_HASH_FOREACH_END();
  757. }
  758. /* }}} */
  759. /* {{{ Return a list of registered hashing algorithms suitable for hash_hmac() */
  760. PHP_FUNCTION(hash_hmac_algos)
  761. {
  762. zend_string *str;
  763. const php_hash_ops *ops;
  764. if (zend_parse_parameters_none() == FAILURE) {
  765. RETURN_THROWS();
  766. }
  767. array_init(return_value);
  768. ZEND_HASH_FOREACH_STR_KEY_PTR(&php_hash_hashtable, str, ops) {
  769. if (ops->is_crypto) {
  770. add_next_index_str(return_value, zend_string_copy(str));
  771. }
  772. } ZEND_HASH_FOREACH_END();
  773. }
  774. /* }}} */
  775. /* {{{ RFC5869 HMAC-based key derivation function */
  776. PHP_FUNCTION(hash_hkdf)
  777. {
  778. zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
  779. zend_long length = 0;
  780. unsigned char *prk, *digest, *K;
  781. size_t i;
  782. size_t rounds;
  783. const php_hash_ops *ops;
  784. void *context;
  785. if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lSS", &algo, &ikm, &length, &info, &salt) == FAILURE) {
  786. RETURN_THROWS();
  787. }
  788. ops = php_hash_fetch_ops(algo);
  789. if (!ops || !ops->is_crypto) {
  790. zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
  791. RETURN_THROWS();
  792. }
  793. if (ZSTR_LEN(ikm) == 0) {
  794. zend_argument_value_error(2, "cannot be empty");
  795. RETURN_THROWS();
  796. }
  797. if (length < 0) {
  798. zend_argument_value_error(3, "must be greater than or equal to 0");
  799. RETURN_THROWS();
  800. } else if (length == 0) {
  801. length = ops->digest_size;
  802. } else if (length > (zend_long) (ops->digest_size * 255)) {
  803. zend_argument_value_error(3, "must be less than or equal to %zd", ops->digest_size * 255);
  804. RETURN_THROWS();
  805. }
  806. context = php_hash_alloc_context(ops);
  807. // Extract
  808. ops->hash_init(context, NULL);
  809. K = emalloc(ops->block_size);
  810. php_hash_hmac_prep_key(K, ops, context,
  811. (unsigned char *) (salt ? ZSTR_VAL(salt) : ""), salt ? ZSTR_LEN(salt) : 0);
  812. prk = emalloc(ops->digest_size);
  813. php_hash_hmac_round(prk, ops, context, K, (unsigned char *) ZSTR_VAL(ikm), ZSTR_LEN(ikm));
  814. php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
  815. php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
  816. ZEND_SECURE_ZERO(K, ops->block_size);
  817. // Expand
  818. returnval = zend_string_alloc(length, 0);
  819. digest = emalloc(ops->digest_size);
  820. for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
  821. // chr(i)
  822. unsigned char c[1];
  823. c[0] = (i & 0xFF);
  824. php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
  825. ops->hash_init(context, NULL);
  826. ops->hash_update(context, K, ops->block_size);
  827. if (i > 1) {
  828. ops->hash_update(context, digest, ops->digest_size);
  829. }
  830. if (info != NULL && ZSTR_LEN(info) > 0) {
  831. ops->hash_update(context, (unsigned char *) ZSTR_VAL(info), ZSTR_LEN(info));
  832. }
  833. ops->hash_update(context, c, 1);
  834. ops->hash_final(digest, context);
  835. php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
  836. php_hash_hmac_round(digest, ops, context, K, digest, ops->digest_size);
  837. memcpy(
  838. ZSTR_VAL(returnval) + ((i - 1) * ops->digest_size),
  839. digest,
  840. (i == rounds ? length - ((i - 1) * ops->digest_size) : ops->digest_size)
  841. );
  842. }
  843. ZEND_SECURE_ZERO(K, ops->block_size);
  844. ZEND_SECURE_ZERO(digest, ops->digest_size);
  845. ZEND_SECURE_ZERO(prk, ops->digest_size);
  846. efree(K);
  847. efree(context);
  848. efree(prk);
  849. efree(digest);
  850. ZSTR_VAL(returnval)[length] = 0;
  851. RETURN_STR(returnval);
  852. }
  853. /* {{{ Generate a PBKDF2 hash of the given password and salt
  854. Returns lowercase hexits by default */
  855. PHP_FUNCTION(hash_pbkdf2)
  856. {
  857. zend_string *returnval, *algo;
  858. char *salt, *pass = NULL;
  859. unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
  860. zend_long loops, i, j, iterations, digest_length = 0, length = 0;
  861. size_t pass_len, salt_len = 0;
  862. bool raw_output = 0;
  863. const php_hash_ops *ops;
  864. void *context;
  865. HashTable *args;
  866. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lbh", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output, &args) == FAILURE) {
  867. RETURN_THROWS();
  868. }
  869. ops = php_hash_fetch_ops(algo);
  870. if (!ops || !ops->is_crypto) {
  871. zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
  872. RETURN_THROWS();
  873. }
  874. if (salt_len > INT_MAX - 4) {
  875. zend_argument_value_error(3, "must be less than or equal to INT_MAX - 4 bytes");
  876. RETURN_THROWS();
  877. }
  878. if (iterations <= 0) {
  879. zend_argument_value_error(4, "must be greater than 0");
  880. RETURN_THROWS();
  881. }
  882. if (length < 0) {
  883. zend_argument_value_error(5, "must be greater than or equal to 0");
  884. RETURN_THROWS();
  885. }
  886. context = php_hash_alloc_context(ops);
  887. ops->hash_init(context, args);
  888. K1 = emalloc(ops->block_size);
  889. K2 = emalloc(ops->block_size);
  890. digest = emalloc(ops->digest_size);
  891. temp = emalloc(ops->digest_size);
  892. /* Setup Keys that will be used for all hmac rounds */
  893. php_hash_hmac_prep_key(K1, ops, context, (unsigned char *) pass, pass_len);
  894. /* Convert K1 to opad -- 0x6A = 0x36 ^ 0x5C */
  895. php_hash_string_xor_char(K2, K1, 0x6A, ops->block_size);
  896. /* Setup Main Loop to build a long enough result */
  897. if (length == 0) {
  898. length = ops->digest_size;
  899. if (!raw_output) {
  900. length = length * 2;
  901. }
  902. }
  903. digest_length = length;
  904. if (!raw_output) {
  905. digest_length = (zend_long) ceil((float) length / 2.0);
  906. }
  907. loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
  908. result = safe_emalloc(loops, ops->digest_size, 0);
  909. computed_salt = safe_emalloc(salt_len, 1, 4);
  910. memcpy(computed_salt, (unsigned char *) salt, salt_len);
  911. for (i = 1; i <= loops; i++) {
  912. /* digest = hash_hmac(salt + pack('N', i), password) { */
  913. /* pack("N", i) */
  914. computed_salt[salt_len] = (unsigned char) (i >> 24);
  915. computed_salt[salt_len + 1] = (unsigned char) ((i & 0xFF0000) >> 16);
  916. computed_salt[salt_len + 2] = (unsigned char) ((i & 0xFF00) >> 8);
  917. computed_salt[salt_len + 3] = (unsigned char) (i & 0xFF);
  918. php_hash_hmac_round(digest, ops, context, K1, computed_salt, (zend_long) salt_len + 4);
  919. php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
  920. /* } */
  921. /* temp = digest */
  922. memcpy(temp, digest, ops->digest_size);
  923. /*
  924. * Note that the loop starting at 1 is intentional, since we've already done
  925. * the first round of the algorithm.
  926. */
  927. for (j = 1; j < iterations; j++) {
  928. /* digest = hash_hmac(digest, password) { */
  929. php_hash_hmac_round(digest, ops, context, K1, digest, ops->digest_size);
  930. php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
  931. /* } */
  932. /* temp ^= digest */
  933. php_hash_string_xor(temp, temp, digest, ops->digest_size);
  934. }
  935. /* result += temp */
  936. memcpy(result + ((i - 1) * ops->digest_size), temp, ops->digest_size);
  937. }
  938. /* Zero potentially sensitive variables */
  939. ZEND_SECURE_ZERO(K1, ops->block_size);
  940. ZEND_SECURE_ZERO(K2, ops->block_size);
  941. ZEND_SECURE_ZERO(computed_salt, salt_len + 4);
  942. efree(K1);
  943. efree(K2);
  944. efree(computed_salt);
  945. efree(context);
  946. efree(digest);
  947. efree(temp);
  948. returnval = zend_string_alloc(length, 0);
  949. if (raw_output) {
  950. memcpy(ZSTR_VAL(returnval), result, length);
  951. } else {
  952. php_hash_bin2hex(ZSTR_VAL(returnval), result, digest_length);
  953. }
  954. ZSTR_VAL(returnval)[length] = 0;
  955. efree(result);
  956. RETURN_NEW_STR(returnval);
  957. }
  958. /* }}} */
  959. /* {{{ Compares two strings using the same time whether they're equal or not.
  960. A difference in length will leak */
  961. PHP_FUNCTION(hash_equals)
  962. {
  963. zval *known_zval, *user_zval;
  964. char *known_str, *user_str;
  965. int result = 0;
  966. size_t j;
  967. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &known_zval, &user_zval) == FAILURE) {
  968. RETURN_THROWS();
  969. }
  970. /* We only allow comparing string to prevent unexpected results. */
  971. if (Z_TYPE_P(known_zval) != IS_STRING) {
  972. zend_argument_type_error(1, "must be of type string, %s given", zend_zval_type_name(known_zval));
  973. RETURN_THROWS();
  974. }
  975. if (Z_TYPE_P(user_zval) != IS_STRING) {
  976. zend_argument_type_error(2, "must be of type string, %s given", zend_zval_type_name(user_zval));
  977. RETURN_THROWS();
  978. }
  979. if (Z_STRLEN_P(known_zval) != Z_STRLEN_P(user_zval)) {
  980. RETURN_FALSE;
  981. }
  982. known_str = Z_STRVAL_P(known_zval);
  983. user_str = Z_STRVAL_P(user_zval);
  984. /* This is security sensitive code. Do not optimize this for speed. */
  985. for (j = 0; j < Z_STRLEN_P(known_zval); j++) {
  986. result |= known_str[j] ^ user_str[j];
  987. }
  988. RETURN_BOOL(0 == result);
  989. }
  990. /* }}} */
  991. /* {{{ */
  992. PHP_METHOD(HashContext, __construct) {
  993. /* Normally unreachable as private/final */
  994. zend_throw_exception(zend_ce_error, "Illegal call to private/final constructor", 0);
  995. }
  996. /* }}} */
  997. /* Module Housekeeping */
  998. #define PHP_HASH_HAVAL_REGISTER(p,b) php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
  999. #ifdef PHP_MHASH_BC
  1000. #if 0
  1001. /* See #69823, we should not insert module into module_registry while doing startup */
  1002. PHP_MINFO_FUNCTION(mhash)
  1003. {
  1004. php_info_print_table_start();
  1005. php_info_print_table_row(2, "MHASH support", "Enabled");
  1006. php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
  1007. php_info_print_table_end();
  1008. }
  1009. zend_module_entry mhash_module_entry = {
  1010. STANDARD_MODULE_HEADER,
  1011. "mhash",
  1012. NULL,
  1013. NULL,
  1014. NULL,
  1015. NULL,
  1016. NULL,
  1017. PHP_MINFO(mhash),
  1018. PHP_MHASH_VERSION,
  1019. STANDARD_MODULE_PROPERTIES,
  1020. };
  1021. #endif
  1022. static void mhash_init(INIT_FUNC_ARGS)
  1023. {
  1024. char buf[128];
  1025. int len;
  1026. int algo_number = 0;
  1027. for (algo_number = 0; algo_number < MHASH_NUM_ALGOS; algo_number++) {
  1028. struct mhash_bc_entry algorithm = mhash_to_hash[algo_number];
  1029. if (algorithm.mhash_name == NULL) {
  1030. continue;
  1031. }
  1032. len = slprintf(buf, 127, "MHASH_%s", algorithm.mhash_name);
  1033. zend_register_long_constant(buf, len, algorithm.value, CONST_CS | CONST_PERSISTENT, module_number);
  1034. }
  1035. /* TODO: this cause #69823 zend_register_internal_module(&mhash_module_entry); */
  1036. }
  1037. /* {{{ Hash data with hash */
  1038. PHP_FUNCTION(mhash)
  1039. {
  1040. zend_long algorithm;
  1041. zend_string *algo = NULL;
  1042. char *data, *key = NULL;
  1043. size_t data_len, key_len = 0;
  1044. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!", &algorithm, &data, &data_len, &key, &key_len) == FAILURE) {
  1045. RETURN_THROWS();
  1046. }
  1047. /* need to convert the first parameter from int constant to string algorithm name */
  1048. if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
  1049. struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
  1050. if (algorithm_lookup.hash_name) {
  1051. algo = zend_string_init(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name), 0);
  1052. }
  1053. }
  1054. if (key) {
  1055. php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, 1, 0);
  1056. } else {
  1057. php_hash_do_hash(return_value, algo, data, data_len, 1, 0, NULL);
  1058. }
  1059. if (algo) {
  1060. zend_string_release(algo);
  1061. }
  1062. }
  1063. /* }}} */
  1064. /* {{{ Gets the name of hash */
  1065. PHP_FUNCTION(mhash_get_hash_name)
  1066. {
  1067. zend_long algorithm;
  1068. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
  1069. RETURN_THROWS();
  1070. }
  1071. if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
  1072. struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
  1073. if (algorithm_lookup.mhash_name) {
  1074. RETURN_STRING(algorithm_lookup.mhash_name);
  1075. }
  1076. }
  1077. RETURN_FALSE;
  1078. }
  1079. /* }}} */
  1080. /* {{{ Gets the number of available hashes */
  1081. PHP_FUNCTION(mhash_count)
  1082. {
  1083. if (zend_parse_parameters_none() == FAILURE) {
  1084. RETURN_THROWS();
  1085. }
  1086. RETURN_LONG(MHASH_NUM_ALGOS - 1);
  1087. }
  1088. /* }}} */
  1089. /* {{{ Gets the block size of hash */
  1090. PHP_FUNCTION(mhash_get_block_size)
  1091. {
  1092. zend_long algorithm;
  1093. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
  1094. RETURN_THROWS();
  1095. }
  1096. RETVAL_FALSE;
  1097. if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
  1098. struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
  1099. if (algorithm_lookup.mhash_name) {
  1100. const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
  1101. if (ops) {
  1102. RETVAL_LONG(ops->digest_size);
  1103. }
  1104. }
  1105. }
  1106. }
  1107. /* }}} */
  1108. #define SALT_SIZE 8
  1109. /* {{{ Generates a key using hash functions */
  1110. PHP_FUNCTION(mhash_keygen_s2k)
  1111. {
  1112. zend_long algorithm, l_bytes;
  1113. int bytes;
  1114. char *password, *salt;
  1115. size_t password_len, salt_len;
  1116. char padded_salt[SALT_SIZE];
  1117. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) {
  1118. RETURN_THROWS();
  1119. }
  1120. bytes = (int)l_bytes;
  1121. if (bytes <= 0){
  1122. zend_argument_value_error(4, "must be a greater than 0");
  1123. RETURN_THROWS();
  1124. }
  1125. salt_len = MIN(salt_len, SALT_SIZE);
  1126. memcpy(padded_salt, salt, salt_len);
  1127. if (salt_len < SALT_SIZE) {
  1128. memset(padded_salt + salt_len, 0, SALT_SIZE - salt_len);
  1129. }
  1130. salt_len = SALT_SIZE;
  1131. RETVAL_FALSE;
  1132. if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
  1133. struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
  1134. if (algorithm_lookup.mhash_name) {
  1135. const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
  1136. if (ops) {
  1137. unsigned char null = '\0';
  1138. void *context;
  1139. char *key, *digest;
  1140. int i = 0, j = 0;
  1141. size_t block_size = ops->digest_size;
  1142. size_t times = bytes / block_size;
  1143. if ((bytes % block_size) != 0) {
  1144. times++;
  1145. }
  1146. context = php_hash_alloc_context(ops);
  1147. ops->hash_init(context, NULL);
  1148. key = ecalloc(1, times * block_size);
  1149. digest = emalloc(ops->digest_size + 1);
  1150. for (i = 0; i < times; i++) {
  1151. ops->hash_init(context, NULL);
  1152. for (j=0;j<i;j++) {
  1153. ops->hash_update(context, &null, 1);
  1154. }
  1155. ops->hash_update(context, (unsigned char *)padded_salt, salt_len);
  1156. ops->hash_update(context, (unsigned char *)password, password_len);
  1157. ops->hash_final((unsigned char *)digest, context);
  1158. memcpy( &key[i*block_size], digest, block_size);
  1159. }
  1160. RETVAL_STRINGL(key, bytes);
  1161. ZEND_SECURE_ZERO(key, bytes);
  1162. efree(digest);
  1163. efree(context);
  1164. efree(key);
  1165. }
  1166. }
  1167. }
  1168. }
  1169. /* }}} */
  1170. #endif
  1171. /* ----------------------------------------------------------------------- */
  1172. /* {{{ php_hashcontext_create */
  1173. static zend_object* php_hashcontext_create(zend_class_entry *ce) {
  1174. php_hashcontext_object *objval = zend_object_alloc(sizeof(php_hashcontext_object), ce);
  1175. zend_object *zobj = &objval->std;
  1176. zend_object_std_init(zobj, ce);
  1177. object_properties_init(zobj, ce);
  1178. zobj->handlers = &php_hashcontext_handlers;
  1179. return zobj;
  1180. }
  1181. /* }}} */
  1182. /* {{{ php_hashcontext_dtor */
  1183. static void php_hashcontext_dtor(zend_object *obj) {
  1184. php_hashcontext_object *hash = php_hashcontext_from_object(obj);
  1185. if (hash->context) {
  1186. efree(hash->context);
  1187. hash->context = NULL;
  1188. }
  1189. if (hash->key) {
  1190. ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
  1191. efree(hash->key);
  1192. hash->key = NULL;
  1193. }
  1194. }
  1195. /* }}} */
  1196. static void php_hashcontext_free(zend_object *obj) {
  1197. php_hashcontext_dtor(obj);
  1198. zend_object_std_dtor(obj);
  1199. }
  1200. /* {{{ php_hashcontext_clone */
  1201. static zend_object *php_hashcontext_clone(zend_object *zobj) {
  1202. php_hashcontext_object *oldobj = php_hashcontext_from_object(zobj);
  1203. zend_object *znew = php_hashcontext_create(zobj->ce);
  1204. php_hashcontext_object *newobj = php_hashcontext_from_object(znew);
  1205. zend_objects_clone_members(znew, zobj);
  1206. newobj->ops = oldobj->ops;
  1207. newobj->options = oldobj->options;
  1208. newobj->context = php_hash_alloc_context(newobj->ops);
  1209. newobj->ops->hash_init(newobj->context, NULL);
  1210. if (SUCCESS != newobj->ops->hash_copy(newobj->ops, oldobj->context, newobj->context)) {
  1211. efree(newobj->context);
  1212. newobj->context = NULL;
  1213. return znew;
  1214. }
  1215. newobj->key = ecalloc(1, newobj->ops->block_size);
  1216. if (oldobj->key) {
  1217. memcpy(newobj->key, oldobj->key, newobj->ops->block_size);
  1218. }
  1219. return znew;
  1220. }
  1221. /* }}} */
  1222. /* Serialization format: 5-element array
  1223. Index 0: hash algorithm (string)
  1224. Index 1: options (long, 0)
  1225. Index 2: hash-determined serialization of context state (usually array)
  1226. Index 3: magic number defining layout of context state (long, usually 2)
  1227. Index 4: properties (array)
  1228. HashContext serializations are not necessarily portable between architectures or
  1229. PHP versions. If the format of a serialized hash context changes, that should
  1230. be reflected in either a different value of `magic` or a different format of
  1231. the serialized context state. Most context states are unparsed and parsed using
  1232. a spec string, such as "llb128.", using the format defined by
  1233. `php_hash_serialize_spec`/`php_hash_unserialize_spec`. Some hash algorithms must
  1234. also check the unserialized state for validity, to ensure that using an
  1235. unserialized context is safe from memory errors.
  1236. Currently HASH_HMAC contexts cannot be serialized, because serializing them
  1237. would require serializing the HMAC key in plaintext. */
  1238. /* {{{ Serialize the object */
  1239. PHP_METHOD(HashContext, __serialize)
  1240. {
  1241. zval *object = ZEND_THIS;
  1242. php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
  1243. zend_long magic = 0;
  1244. zval tmp;
  1245. if (zend_parse_parameters_none() == FAILURE) {
  1246. RETURN_THROWS();
  1247. }
  1248. array_init(return_value);
  1249. if (!hash->ops->hash_serialize) {
  1250. goto serialize_failure;
  1251. } else if (hash->options & PHP_HASH_HMAC) {
  1252. zend_throw_exception(NULL, "HashContext with HASH_HMAC option cannot be serialized", 0);
  1253. RETURN_THROWS();
  1254. }
  1255. ZVAL_STRING(&tmp, hash->ops->algo);
  1256. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
  1257. ZVAL_LONG(&tmp, hash->options);
  1258. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
  1259. if (hash->ops->hash_serialize(hash, &magic, &tmp) != SUCCESS) {
  1260. goto serialize_failure;
  1261. }
  1262. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
  1263. ZVAL_LONG(&tmp, magic);
  1264. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
  1265. /* members */
  1266. ZVAL_ARR(&tmp, zend_std_get_properties(&hash->std));
  1267. Z_TRY_ADDREF(tmp);
  1268. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
  1269. return;
  1270. serialize_failure:
  1271. zend_throw_exception_ex(NULL, 0, "HashContext for algorithm \"%s\" cannot be serialized", hash->ops->algo);
  1272. RETURN_THROWS();
  1273. }
  1274. /* }}} */
  1275. /* {{{ unserialize the object */
  1276. PHP_METHOD(HashContext, __unserialize)
  1277. {
  1278. zval *object = ZEND_THIS;
  1279. php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
  1280. HashTable *data;
  1281. zval *algo_zv, *magic_zv, *options_zv, *hash_zv, *members_zv;
  1282. zend_long magic, options;
  1283. int unserialize_result;
  1284. const php_hash_ops *ops;
  1285. if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
  1286. RETURN_THROWS();
  1287. }
  1288. if (hash->context) {
  1289. zend_throw_exception(NULL, "HashContext::__unserialize called on initialized object", 0);
  1290. RETURN_THROWS();
  1291. }
  1292. algo_zv = zend_hash_index_find(data, 0);
  1293. options_zv = zend_hash_index_find(data, 1);
  1294. hash_zv = zend_hash_index_find(data, 2);
  1295. magic_zv = zend_hash_index_find(data, 3);
  1296. members_zv = zend_hash_index_find(data, 4);
  1297. if (!algo_zv || Z_TYPE_P(algo_zv) != IS_STRING
  1298. || !magic_zv || Z_TYPE_P(magic_zv) != IS_LONG
  1299. || !options_zv || Z_TYPE_P(options_zv) != IS_LONG
  1300. || !hash_zv
  1301. || !members_zv || Z_TYPE_P(members_zv) != IS_ARRAY) {
  1302. zend_throw_exception(NULL, "Incomplete or ill-formed serialization data", 0);
  1303. RETURN_THROWS();
  1304. }
  1305. magic = Z_LVAL_P(magic_zv);
  1306. options = Z_LVAL_P(options_zv);
  1307. if (options & PHP_HASH_HMAC) {
  1308. zend_throw_exception(NULL, "HashContext with HASH_HMAC option cannot be serialized", 0);
  1309. RETURN_THROWS();
  1310. }
  1311. ops = php_hash_fetch_ops(Z_STR_P(algo_zv));
  1312. if (!ops) {
  1313. zend_throw_exception(NULL, "Unknown hash algorithm", 0);
  1314. RETURN_THROWS();
  1315. } else if (!ops->hash_unserialize) {
  1316. zend_throw_exception_ex(NULL, 0, "Hash algorithm \"%s\" cannot be unserialized", ops->algo);
  1317. RETURN_THROWS();
  1318. }
  1319. hash->ops = ops;
  1320. hash->context = php_hash_alloc_context(ops);
  1321. hash->options = options;
  1322. ops->hash_init(hash->context, NULL);
  1323. unserialize_result = ops->hash_unserialize(hash, magic, hash_zv);
  1324. if (unserialize_result != SUCCESS) {
  1325. zend_throw_exception_ex(NULL, 0, "Incomplete or ill-formed serialization data (\"%s\" code %d)", ops->algo, unserialize_result);
  1326. /* free context */
  1327. php_hashcontext_dtor(Z_OBJ_P(object));
  1328. RETURN_THROWS();
  1329. }
  1330. object_properties_load(&hash->std, Z_ARRVAL_P(members_zv));
  1331. }
  1332. /* }}} */
  1333. /* {{{ PHP_MINIT_FUNCTION */
  1334. PHP_MINIT_FUNCTION(hash)
  1335. {
  1336. zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
  1337. php_hash_register_algo("md2", &php_hash_md2_ops);
  1338. php_hash_register_algo("md4", &php_hash_md4_ops);
  1339. php_hash_register_algo("md5", &php_hash_md5_ops);
  1340. php_hash_register_algo("sha1", &php_hash_sha1_ops);
  1341. php_hash_register_algo("sha224", &php_hash_sha224_ops);
  1342. php_hash_register_algo("sha256", &php_hash_sha256_ops);
  1343. php_hash_register_algo("sha384", &php_hash_sha384_ops);
  1344. php_hash_register_algo("sha512/224", &php_hash_sha512_224_ops);
  1345. php_hash_register_algo("sha512/256", &php_hash_sha512_256_ops);
  1346. php_hash_register_algo("sha512", &php_hash_sha512_ops);
  1347. php_hash_register_algo("sha3-224", &php_hash_sha3_224_ops);
  1348. php_hash_register_algo("sha3-256", &php_hash_sha3_256_ops);
  1349. php_hash_register_algo("sha3-384", &php_hash_sha3_384_ops);
  1350. php_hash_register_algo("sha3-512", &php_hash_sha3_512_ops);
  1351. php_hash_register_algo("ripemd128", &php_hash_ripemd128_ops);
  1352. php_hash_register_algo("ripemd160", &php_hash_ripemd160_ops);
  1353. php_hash_register_algo("ripemd256", &php_hash_ripemd256_ops);
  1354. php_hash_register_algo("ripemd320", &php_hash_ripemd320_ops);
  1355. php_hash_register_algo("whirlpool", &php_hash_whirlpool_ops);
  1356. php_hash_register_algo("tiger128,3", &php_hash_3tiger128_ops);
  1357. php_hash_register_algo("tiger160,3", &php_hash_3tiger160_ops);
  1358. php_hash_register_algo("tiger192,3", &php_hash_3tiger192_ops);
  1359. php_hash_register_algo("tiger128,4", &php_hash_4tiger128_ops);
  1360. php_hash_register_algo("tiger160,4", &php_hash_4tiger160_ops);
  1361. php_hash_register_algo("tiger192,4", &php_hash_4tiger192_ops);
  1362. php_hash_register_algo("snefru", &php_hash_snefru_ops);
  1363. php_hash_register_algo("snefru256", &php_hash_snefru_ops);
  1364. php_hash_register_algo("gost", &php_hash_gost_ops);
  1365. php_hash_register_algo("gost-crypto", &php_hash_gost_crypto_ops);
  1366. php_hash_register_algo("adler32", &php_hash_adler32_ops);
  1367. php_hash_register_algo("crc32", &php_hash_crc32_ops);
  1368. php_hash_register_algo("crc32b", &php_hash_crc32b_ops);
  1369. php_hash_register_algo("crc32c", &php_hash_crc32c_ops);
  1370. php_hash_register_algo("fnv132", &php_hash_fnv132_ops);
  1371. php_hash_register_algo("fnv1a32", &php_hash_fnv1a32_ops);
  1372. php_hash_register_algo("fnv164", &php_hash_fnv164_ops);
  1373. php_hash_register_algo("fnv1a64", &php_hash_fnv1a64_ops);
  1374. php_hash_register_algo("joaat", &php_hash_joaat_ops);
  1375. php_hash_register_algo("murmur3a", &php_hash_murmur3a_ops);
  1376. php_hash_register_algo("murmur3c", &php_hash_murmur3c_ops);
  1377. php_hash_register_algo("murmur3f", &php_hash_murmur3f_ops);
  1378. php_hash_register_algo("xxh32", &php_hash_xxh32_ops);
  1379. php_hash_register_algo("xxh64", &php_hash_xxh64_ops);
  1380. php_hash_register_algo("xxh3", &php_hash_xxh3_64_ops);
  1381. php_hash_register_algo("xxh128", &php_hash_xxh3_128_ops);
  1382. PHP_HASH_HAVAL_REGISTER(3,128);
  1383. PHP_HASH_HAVAL_REGISTER(3,160);
  1384. PHP_HASH_HAVAL_REGISTER(3,192);
  1385. PHP_HASH_HAVAL_REGISTER(3,224);
  1386. PHP_HASH_HAVAL_REGISTER(3,256);
  1387. PHP_HASH_HAVAL_REGISTER(4,128);
  1388. PHP_HASH_HAVAL_REGISTER(4,160);
  1389. PHP_HASH_HAVAL_REGISTER(4,192);
  1390. PHP_HASH_HAVAL_REGISTER(4,224);
  1391. PHP_HASH_HAVAL_REGISTER(4,256);
  1392. PHP_HASH_HAVAL_REGISTER(5,128);
  1393. PHP_HASH_HAVAL_REGISTER(5,160);
  1394. PHP_HASH_HAVAL_REGISTER(5,192);
  1395. PHP_HASH_HAVAL_REGISTER(5,224);
  1396. PHP_HASH_HAVAL_REGISTER(5,256);
  1397. REGISTER_LONG_CONSTANT("HASH_HMAC", PHP_HASH_HMAC, CONST_CS | CONST_PERSISTENT);
  1398. php_hashcontext_ce = register_class_HashContext();
  1399. php_hashcontext_ce->create_object = php_hashcontext_create;
  1400. memcpy(&php_hashcontext_handlers, &std_object_handlers,
  1401. sizeof(zend_object_handlers));
  1402. php_hashcontext_handlers.offset = XtOffsetOf(php_hashcontext_object, std);
  1403. php_hashcontext_handlers.free_obj = php_hashcontext_free;
  1404. php_hashcontext_handlers.clone_obj = php_hashcontext_clone;
  1405. #ifdef PHP_MHASH_BC
  1406. mhash_init(INIT_FUNC_ARGS_PASSTHRU);
  1407. #endif
  1408. return SUCCESS;
  1409. }
  1410. /* }}} */
  1411. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  1412. PHP_MSHUTDOWN_FUNCTION(hash)
  1413. {
  1414. zend_hash_destroy(&php_hash_hashtable);
  1415. return SUCCESS;
  1416. }
  1417. /* }}} */
  1418. /* {{{ PHP_MINFO_FUNCTION */
  1419. PHP_MINFO_FUNCTION(hash)
  1420. {
  1421. char buffer[2048];
  1422. zend_string *str;
  1423. char *s = buffer, *e = s + sizeof(buffer);
  1424. ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
  1425. s += slprintf(s, e - s, "%s ", ZSTR_VAL(str));
  1426. } ZEND_HASH_FOREACH_END();
  1427. *s = 0;
  1428. php_info_print_table_start();
  1429. php_info_print_table_row(2, "hash support", "enabled");
  1430. php_info_print_table_row(2, "Hashing Engines", buffer);
  1431. php_info_print_table_end();
  1432. #ifdef PHP_MHASH_BC
  1433. php_info_print_table_start();
  1434. php_info_print_table_row(2, "MHASH support", "Enabled");
  1435. php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
  1436. php_info_print_table_end();
  1437. #endif
  1438. }
  1439. /* }}} */
  1440. /* {{{ hash_module_entry */
  1441. zend_module_entry hash_module_entry = {
  1442. STANDARD_MODULE_HEADER,
  1443. PHP_HASH_EXTNAME,
  1444. ext_functions,
  1445. PHP_MINIT(hash),
  1446. PHP_MSHUTDOWN(hash),
  1447. NULL, /* RINIT */
  1448. NULL, /* RSHUTDOWN */
  1449. PHP_MINFO(hash),
  1450. PHP_HASH_VERSION,
  1451. STANDARD_MODULE_PROPERTIES
  1452. };
  1453. /* }}} */