hash.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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. | Scott MacVicar <scottmac@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <math.h>
  23. #include "php_hash.h"
  24. #include "ext/standard/info.h"
  25. #include "ext/standard/file.h"
  26. #include "zend_interfaces.h"
  27. #include "zend_exceptions.h"
  28. HashTable php_hash_hashtable;
  29. zend_class_entry *php_hashcontext_ce;
  30. static zend_object_handlers php_hashcontext_handlers;
  31. #ifdef PHP_MHASH_BC
  32. struct mhash_bc_entry {
  33. char *mhash_name;
  34. char *hash_name;
  35. int value;
  36. };
  37. #define MHASH_NUM_ALGOS 34
  38. static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
  39. {"CRC32", "crc32", 0},
  40. {"MD5", "md5", 1},
  41. {"SHA1", "sha1", 2},
  42. {"HAVAL256", "haval256,3", 3},
  43. {NULL, NULL, 4},
  44. {"RIPEMD160", "ripemd160", 5},
  45. {NULL, NULL, 6},
  46. {"TIGER", "tiger192,3", 7},
  47. {"GOST", "gost", 8},
  48. {"CRC32B", "crc32b", 9},
  49. {"HAVAL224", "haval224,3", 10},
  50. {"HAVAL192", "haval192,3", 11},
  51. {"HAVAL160", "haval160,3", 12},
  52. {"HAVAL128", "haval128,3", 13},
  53. {"TIGER128", "tiger128,3", 14},
  54. {"TIGER160", "tiger160,3", 15},
  55. {"MD4", "md4", 16},
  56. {"SHA256", "sha256", 17},
  57. {"ADLER32", "adler32", 18},
  58. {"SHA224", "sha224", 19},
  59. {"SHA512", "sha512", 20},
  60. {"SHA384", "sha384", 21},
  61. {"WHIRLPOOL", "whirlpool", 22},
  62. {"RIPEMD128", "ripemd128", 23},
  63. {"RIPEMD256", "ripemd256", 24},
  64. {"RIPEMD320", "ripemd320", 25},
  65. {NULL, NULL, 26}, /* support needs to be added for snefru 128 */
  66. {"SNEFRU256", "snefru256", 27},
  67. {"MD2", "md2", 28},
  68. {"FNV132", "fnv132", 29},
  69. {"FNV1A32", "fnv1a32", 30},
  70. {"FNV164", "fnv164", 31},
  71. {"FNV1A64", "fnv1a64", 32},
  72. {"JOAAT", "joaat", 33},
  73. };
  74. #endif
  75. /* Hash Registry Access */
  76. PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, size_t algo_len) /* {{{ */
  77. {
  78. char *lower = zend_str_tolower_dup(algo, algo_len);
  79. php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, lower, algo_len);
  80. efree(lower);
  81. return ops;
  82. }
  83. /* }}} */
  84. PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops) /* {{{ */
  85. {
  86. size_t algo_len = strlen(algo);
  87. char *lower = zend_str_tolower_dup(algo, algo_len);
  88. zend_hash_add_ptr(&php_hash_hashtable, zend_string_init_interned(lower, algo_len, 1), (void *) ops);
  89. efree(lower);
  90. }
  91. /* }}} */
  92. PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_context) /* {{{ */
  93. {
  94. php_hash_ops *hash_ops = (php_hash_ops *)ops;
  95. memcpy(dest_context, orig_context, hash_ops->context_size);
  96. return SUCCESS;
  97. }
  98. /* }}} */
  99. /* Userspace */
  100. static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
  101. {
  102. zend_string *digest;
  103. char *algo, *data;
  104. size_t algo_len, data_len;
  105. zend_bool raw_output = raw_output_default;
  106. const php_hash_ops *ops;
  107. void *context;
  108. php_stream *stream = NULL;
  109. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|b", &algo, &algo_len, &data, &data_len, &raw_output) == FAILURE) {
  110. return;
  111. }
  112. ops = php_hash_fetch_ops(algo, algo_len);
  113. if (!ops) {
  114. php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
  115. RETURN_FALSE;
  116. }
  117. if (isfilename) {
  118. if (CHECK_NULL_PATH(data, data_len)) {
  119. php_error_docref(NULL, E_WARNING, "Invalid path");
  120. RETURN_FALSE;
  121. }
  122. stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
  123. if (!stream) {
  124. /* Stream will report errors opening file */
  125. RETURN_FALSE;
  126. }
  127. }
  128. context = emalloc(ops->context_size);
  129. ops->hash_init(context);
  130. if (isfilename) {
  131. char buf[1024];
  132. size_t n;
  133. while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
  134. ops->hash_update(context, (unsigned char *) buf, n);
  135. }
  136. php_stream_close(stream);
  137. } else {
  138. ops->hash_update(context, (unsigned char *) data, data_len);
  139. }
  140. digest = zend_string_alloc(ops->digest_size, 0);
  141. ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
  142. efree(context);
  143. if (raw_output) {
  144. ZSTR_VAL(digest)[ops->digest_size] = 0;
  145. RETURN_NEW_STR(digest);
  146. } else {
  147. zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
  148. php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
  149. ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
  150. zend_string_release_ex(digest, 0);
  151. RETURN_NEW_STR(hex_digest);
  152. }
  153. }
  154. /* }}} */
  155. /* {{{ proto string hash(string algo, string data[, bool raw_output = false])
  156. Generate a hash of a given input string
  157. Returns lowercase hexits by default */
  158. PHP_FUNCTION(hash)
  159. {
  160. php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
  161. }
  162. /* }}} */
  163. /* {{{ proto string hash_file(string algo, string filename[, bool raw_output = false])
  164. Generate a hash of a given file
  165. Returns lowercase hexits by default */
  166. PHP_FUNCTION(hash_file)
  167. {
  168. php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
  169. }
  170. /* }}} */
  171. static inline void php_hash_string_xor_char(unsigned char *out, const unsigned char *in, const unsigned char xor_with, const int length) {
  172. int i;
  173. for (i=0; i < length; i++) {
  174. out[i] = in[i] ^ xor_with;
  175. }
  176. }
  177. static inline void php_hash_string_xor(unsigned char *out, const unsigned char *in, const unsigned char *xor_with, const int length) {
  178. int i;
  179. for (i=0; i < length; i++) {
  180. out[i] = in[i] ^ xor_with[i];
  181. }
  182. }
  183. 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) {
  184. memset(K, 0, ops->block_size);
  185. if (key_len > (size_t)ops->block_size) {
  186. /* Reduce the key first */
  187. ops->hash_init(context);
  188. ops->hash_update(context, key, key_len);
  189. ops->hash_final(K, context);
  190. } else {
  191. memcpy(K, key, key_len);
  192. }
  193. /* XOR the key with 0x36 to get the ipad) */
  194. php_hash_string_xor_char(K, K, 0x36, ops->block_size);
  195. }
  196. 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) {
  197. ops->hash_init(context);
  198. ops->hash_update(context, key, ops->block_size);
  199. ops->hash_update(context, data, data_size);
  200. ops->hash_final(final, context);
  201. }
  202. static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
  203. {
  204. zend_string *digest;
  205. char *algo, *data, *key;
  206. unsigned char *K;
  207. size_t algo_len, data_len, key_len;
  208. zend_bool raw_output = raw_output_default;
  209. const php_hash_ops *ops;
  210. void *context;
  211. php_stream *stream = NULL;
  212. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|b", &algo, &algo_len, &data, &data_len,
  213. &key, &key_len, &raw_output) == FAILURE) {
  214. return;
  215. }
  216. ops = php_hash_fetch_ops(algo, algo_len);
  217. if (!ops) {
  218. php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
  219. RETURN_FALSE;
  220. }
  221. else if (!ops->is_crypto) {
  222. php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo);
  223. RETURN_FALSE;
  224. }
  225. if (isfilename) {
  226. if (CHECK_NULL_PATH(data, data_len)) {
  227. php_error_docref(NULL, E_WARNING, "Invalid path");
  228. RETURN_FALSE;
  229. }
  230. stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
  231. if (!stream) {
  232. /* Stream will report errors opening file */
  233. RETURN_FALSE;
  234. }
  235. }
  236. context = emalloc(ops->context_size);
  237. K = emalloc(ops->block_size);
  238. digest = zend_string_alloc(ops->digest_size, 0);
  239. php_hash_hmac_prep_key(K, ops, context, (unsigned char *) key, key_len);
  240. if (isfilename) {
  241. char buf[1024];
  242. int n;
  243. ops->hash_init(context);
  244. ops->hash_update(context, K, ops->block_size);
  245. while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
  246. ops->hash_update(context, (unsigned char *) buf, n);
  247. }
  248. php_stream_close(stream);
  249. ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
  250. } else {
  251. php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) data, data_len);
  252. }
  253. php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
  254. php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
  255. /* Zero the key */
  256. ZEND_SECURE_ZERO(K, ops->block_size);
  257. efree(K);
  258. efree(context);
  259. if (raw_output) {
  260. ZSTR_VAL(digest)[ops->digest_size] = 0;
  261. RETURN_NEW_STR(digest);
  262. } else {
  263. zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
  264. php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
  265. ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
  266. zend_string_release_ex(digest, 0);
  267. RETURN_NEW_STR(hex_digest);
  268. }
  269. }
  270. /* }}} */
  271. /* {{{ proto string hash_hmac(string algo, string data, string key[, bool raw_output = false])
  272. Generate a hash of a given input string with a key using HMAC
  273. Returns lowercase hexits by default */
  274. PHP_FUNCTION(hash_hmac)
  275. {
  276. php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
  277. }
  278. /* }}} */
  279. /* {{{ proto string hash_hmac_file(string algo, string filename, string key[, bool raw_output = false])
  280. Generate a hash of a given file with a key using HMAC
  281. Returns lowercase hexits by default */
  282. PHP_FUNCTION(hash_hmac_file)
  283. {
  284. php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
  285. }
  286. /* }}} */
  287. static void php_hashcontext_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *objval) {
  288. zend_string *algo, *key = NULL;
  289. zend_long options = 0;
  290. int argc = ZEND_NUM_ARGS();
  291. void *context;
  292. const php_hash_ops *ops;
  293. php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(objval));
  294. if (zend_parse_parameters(argc, "S|lS", &algo, &options, &key) == FAILURE) {
  295. zval_ptr_dtor(return_value);
  296. RETURN_NULL();
  297. }
  298. ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
  299. if (!ops) {
  300. php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
  301. zval_ptr_dtor(return_value);
  302. RETURN_FALSE;
  303. }
  304. if (options & PHP_HASH_HMAC) {
  305. if (!ops->is_crypto) {
  306. php_error_docref(NULL, E_WARNING, "HMAC requested with a non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
  307. zval_ptr_dtor(return_value);
  308. RETURN_FALSE;
  309. }
  310. if (!key || (ZSTR_LEN(key) == 0)) {
  311. /* Note: a zero length key is no key at all */
  312. php_error_docref(NULL, E_WARNING, "HMAC requested without a key");
  313. zval_ptr_dtor(return_value);
  314. RETURN_FALSE;
  315. }
  316. }
  317. context = emalloc(ops->context_size);
  318. ops->hash_init(context);
  319. hash->ops = ops;
  320. hash->context = context;
  321. hash->options = options;
  322. hash->key = NULL;
  323. if (options & PHP_HASH_HMAC) {
  324. char *K = emalloc(ops->block_size);
  325. int i, block_size;
  326. memset(K, 0, ops->block_size);
  327. if (ZSTR_LEN(key) > (size_t)ops->block_size) {
  328. /* Reduce the key first */
  329. ops->hash_update(context, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key));
  330. ops->hash_final((unsigned char *) K, context);
  331. /* Make the context ready to start over */
  332. ops->hash_init(context);
  333. } else {
  334. memcpy(K, ZSTR_VAL(key), ZSTR_LEN(key));
  335. }
  336. /* XOR ipad */
  337. block_size = ops->block_size;
  338. for(i=0; i < block_size; i++) {
  339. K[i] ^= 0x36;
  340. }
  341. ops->hash_update(context, (unsigned char *) K, ops->block_size);
  342. hash->key = (unsigned char *) K;
  343. }
  344. }
  345. /* {{{ proto HashContext hash_init(string algo[, int options, string key])
  346. Initialize a hashing context */
  347. PHP_FUNCTION(hash_init)
  348. {
  349. object_init_ex(return_value, php_hashcontext_ce);
  350. php_hashcontext_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, return_value);
  351. }
  352. /* }}} */
  353. #define PHP_HASHCONTEXT_VERIFY(func, hash) { \
  354. if (!hash->context) { \
  355. php_error(E_WARNING, "%s(): supplied resource is not a valid Hash Context resource", func); \
  356. RETURN_NULL(); \
  357. } \
  358. }
  359. /* {{{ proto bool hash_update(HashContext context, string data)
  360. Pump data into the hashing algorithm */
  361. PHP_FUNCTION(hash_update)
  362. {
  363. zval *zhash;
  364. php_hashcontext_object *hash;
  365. zend_string *data;
  366. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zhash, php_hashcontext_ce, &data) == FAILURE) {
  367. return;
  368. }
  369. hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
  370. PHP_HASHCONTEXT_VERIFY("hash_update", hash);
  371. hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(data), ZSTR_LEN(data));
  372. RETURN_TRUE;
  373. }
  374. /* }}} */
  375. /* {{{ proto int hash_update_stream(HashContext context, resource handle[, int length])
  376. Pump data into the hashing algorithm from an open stream */
  377. PHP_FUNCTION(hash_update_stream)
  378. {
  379. zval *zhash, *zstream;
  380. php_hashcontext_object *hash;
  381. php_stream *stream = NULL;
  382. zend_long length = -1, didread = 0;
  383. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Or|l", &zhash, php_hashcontext_ce, &zstream, &length) == FAILURE) {
  384. return;
  385. }
  386. hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
  387. PHP_HASHCONTEXT_VERIFY("hash_update_stream", hash);
  388. php_stream_from_zval(stream, zstream);
  389. while (length) {
  390. char buf[1024];
  391. zend_long n, toread = 1024;
  392. if (length > 0 && toread > length) {
  393. toread = length;
  394. }
  395. if ((n = php_stream_read(stream, buf, toread)) <= 0) {
  396. /* Nada mas */
  397. RETURN_LONG(didread);
  398. }
  399. hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
  400. length -= n;
  401. didread += n;
  402. }
  403. RETURN_LONG(didread);
  404. }
  405. /* }}} */
  406. /* {{{ proto bool hash_update_file(HashContext context, string filename[, resource context])
  407. Pump data into the hashing algorithm from a file */
  408. PHP_FUNCTION(hash_update_file)
  409. {
  410. zval *zhash, *zcontext = NULL;
  411. php_hashcontext_object *hash;
  412. php_stream_context *context;
  413. php_stream *stream;
  414. zend_string *filename;
  415. char buf[1024];
  416. size_t n;
  417. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|r", &zhash, php_hashcontext_ce, &filename, &zcontext) == FAILURE) {
  418. return;
  419. }
  420. hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
  421. PHP_HASHCONTEXT_VERIFY("hash_update_file", hash);
  422. context = php_stream_context_from_zval(zcontext, 0);
  423. stream = php_stream_open_wrapper_ex(ZSTR_VAL(filename), "rb", REPORT_ERRORS, NULL, context);
  424. if (!stream) {
  425. /* Stream will report errors opening file */
  426. RETURN_FALSE;
  427. }
  428. while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
  429. hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
  430. }
  431. php_stream_close(stream);
  432. RETURN_TRUE;
  433. }
  434. /* }}} */
  435. /* {{{ proto string hash_final(HashContext context[, bool raw_output=false])
  436. Output resulting digest */
  437. PHP_FUNCTION(hash_final)
  438. {
  439. zval *zhash;
  440. php_hashcontext_object *hash;
  441. zend_bool raw_output = 0;
  442. zend_string *digest;
  443. int digest_len;
  444. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &zhash, php_hashcontext_ce, &raw_output) == FAILURE) {
  445. return;
  446. }
  447. hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
  448. PHP_HASHCONTEXT_VERIFY("hash_final", hash);
  449. digest_len = hash->ops->digest_size;
  450. digest = zend_string_alloc(digest_len, 0);
  451. hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
  452. if (hash->options & PHP_HASH_HMAC) {
  453. int i, block_size;
  454. /* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
  455. block_size = hash->ops->block_size;
  456. for(i=0; i < block_size; i++) {
  457. hash->key[i] ^= 0x6A;
  458. }
  459. /* Feed this result into the outter hash */
  460. hash->ops->hash_init(hash->context);
  461. hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size);
  462. hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(digest), hash->ops->digest_size);
  463. hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
  464. /* Zero the key */
  465. ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
  466. efree(hash->key);
  467. hash->key = NULL;
  468. }
  469. ZSTR_VAL(digest)[digest_len] = 0;
  470. /* Invalidate the object from further use */
  471. efree(hash->context);
  472. hash->context = NULL;
  473. if (raw_output) {
  474. RETURN_NEW_STR(digest);
  475. } else {
  476. zend_string *hex_digest = zend_string_safe_alloc(digest_len, 2, 0, 0);
  477. php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), digest_len);
  478. ZSTR_VAL(hex_digest)[2 * digest_len] = 0;
  479. zend_string_release_ex(digest, 0);
  480. RETURN_NEW_STR(hex_digest);
  481. }
  482. }
  483. /* }}} */
  484. /* {{{ proto HashContext hash_copy(HashContext context)
  485. Copy hash object */
  486. PHP_FUNCTION(hash_copy)
  487. {
  488. zval *zhash;
  489. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zhash, php_hashcontext_ce) == FAILURE) {
  490. return;
  491. }
  492. RETVAL_OBJ(Z_OBJ_HANDLER_P(zhash, clone_obj)(zhash));
  493. if (php_hashcontext_from_object(Z_OBJ_P(return_value))->context == NULL) {
  494. zval_ptr_dtor(return_value);
  495. RETURN_FALSE;
  496. }
  497. }
  498. /* }}} */
  499. /* {{{ proto array hash_algos(void)
  500. Return a list of registered hashing algorithms */
  501. PHP_FUNCTION(hash_algos)
  502. {
  503. zend_string *str;
  504. array_init(return_value);
  505. ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
  506. add_next_index_str(return_value, zend_string_copy(str));
  507. } ZEND_HASH_FOREACH_END();
  508. }
  509. /* }}} */
  510. /* {{{ proto array hash_hmac_algos(void)
  511. Return a list of registered hashing algorithms suitable for hash_hmac() */
  512. PHP_FUNCTION(hash_hmac_algos)
  513. {
  514. zend_string *str;
  515. const php_hash_ops *ops;
  516. array_init(return_value);
  517. ZEND_HASH_FOREACH_STR_KEY_PTR(&php_hash_hashtable, str, ops) {
  518. if (ops->is_crypto) {
  519. add_next_index_str(return_value, zend_string_copy(str));
  520. }
  521. } ZEND_HASH_FOREACH_END();
  522. }
  523. /* }}} */
  524. /* {{{ proto string hash_hkdf(string algo, string ikm [, int length = 0, string info = '', string salt = ''])
  525. RFC5869 HMAC-based key derivation function */
  526. PHP_FUNCTION(hash_hkdf)
  527. {
  528. zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
  529. zend_long length = 0;
  530. unsigned char *prk, *digest, *K;
  531. int i, rounds;
  532. const php_hash_ops *ops;
  533. void *context;
  534. if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lSS", &algo, &ikm, &length, &info, &salt) == FAILURE) {
  535. return;
  536. }
  537. ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
  538. if (!ops) {
  539. php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
  540. RETURN_FALSE;
  541. }
  542. if (!ops->is_crypto) {
  543. php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
  544. RETURN_FALSE;
  545. }
  546. if (ZSTR_LEN(ikm) == 0) {
  547. php_error_docref(NULL, E_WARNING, "Input keying material cannot be empty");
  548. RETURN_FALSE;
  549. }
  550. if (length < 0) {
  551. php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
  552. RETURN_FALSE;
  553. } else if (length == 0) {
  554. length = ops->digest_size;
  555. } else if (length > ops->digest_size * 255) {
  556. php_error_docref(NULL, E_WARNING, "Length must be less than or equal to %d: " ZEND_LONG_FMT, ops->digest_size * 255, length);
  557. RETURN_FALSE;
  558. }
  559. context = emalloc(ops->context_size);
  560. // Extract
  561. ops->hash_init(context);
  562. K = emalloc(ops->block_size);
  563. php_hash_hmac_prep_key(K, ops, context,
  564. (unsigned char *) (salt ? ZSTR_VAL(salt) : ""), salt ? ZSTR_LEN(salt) : 0);
  565. prk = emalloc(ops->digest_size);
  566. php_hash_hmac_round(prk, ops, context, K, (unsigned char *) ZSTR_VAL(ikm), ZSTR_LEN(ikm));
  567. php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
  568. php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
  569. ZEND_SECURE_ZERO(K, ops->block_size);
  570. // Expand
  571. returnval = zend_string_alloc(length, 0);
  572. digest = emalloc(ops->digest_size);
  573. for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
  574. // chr(i)
  575. unsigned char c[1];
  576. c[0] = (i & 0xFF);
  577. php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
  578. ops->hash_init(context);
  579. ops->hash_update(context, K, ops->block_size);
  580. if (i > 1) {
  581. ops->hash_update(context, digest, ops->digest_size);
  582. }
  583. if (info != NULL && ZSTR_LEN(info) > 0) {
  584. ops->hash_update(context, (unsigned char *) ZSTR_VAL(info), ZSTR_LEN(info));
  585. }
  586. ops->hash_update(context, c, 1);
  587. ops->hash_final(digest, context);
  588. php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
  589. php_hash_hmac_round(digest, ops, context, K, digest, ops->digest_size);
  590. memcpy(
  591. ZSTR_VAL(returnval) + ((i - 1) * ops->digest_size),
  592. digest,
  593. (i == rounds ? length - ((i - 1) * ops->digest_size) : ops->digest_size)
  594. );
  595. }
  596. ZEND_SECURE_ZERO(K, ops->block_size);
  597. ZEND_SECURE_ZERO(digest, ops->digest_size);
  598. ZEND_SECURE_ZERO(prk, ops->digest_size);
  599. efree(K);
  600. efree(context);
  601. efree(prk);
  602. efree(digest);
  603. ZSTR_VAL(returnval)[length] = 0;
  604. RETURN_STR(returnval);
  605. }
  606. /* {{{ proto string hash_pbkdf2(string algo, string password, string salt, int iterations [, int length = 0, bool raw_output = false])
  607. Generate a PBKDF2 hash of the given password and salt
  608. Returns lowercase hexits by default */
  609. PHP_FUNCTION(hash_pbkdf2)
  610. {
  611. zend_string *returnval;
  612. char *algo, *salt, *pass = NULL;
  613. unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
  614. zend_long loops, i, j, iterations, digest_length = 0, length = 0;
  615. size_t algo_len, pass_len, salt_len = 0;
  616. zend_bool raw_output = 0;
  617. const php_hash_ops *ops;
  618. void *context;
  619. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssl|lb", &algo, &algo_len, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output) == FAILURE) {
  620. return;
  621. }
  622. ops = php_hash_fetch_ops(algo, algo_len);
  623. if (!ops) {
  624. php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
  625. RETURN_FALSE;
  626. }
  627. else if (!ops->is_crypto) {
  628. php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo);
  629. RETURN_FALSE;
  630. }
  631. if (iterations <= 0) {
  632. php_error_docref(NULL, E_WARNING, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
  633. RETURN_FALSE;
  634. }
  635. if (length < 0) {
  636. php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
  637. RETURN_FALSE;
  638. }
  639. if (salt_len > INT_MAX - 4) {
  640. php_error_docref(NULL, E_WARNING, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
  641. RETURN_FALSE;
  642. }
  643. context = emalloc(ops->context_size);
  644. ops->hash_init(context);
  645. K1 = emalloc(ops->block_size);
  646. K2 = emalloc(ops->block_size);
  647. digest = emalloc(ops->digest_size);
  648. temp = emalloc(ops->digest_size);
  649. /* Setup Keys that will be used for all hmac rounds */
  650. php_hash_hmac_prep_key(K1, ops, context, (unsigned char *) pass, pass_len);
  651. /* Convert K1 to opad -- 0x6A = 0x36 ^ 0x5C */
  652. php_hash_string_xor_char(K2, K1, 0x6A, ops->block_size);
  653. /* Setup Main Loop to build a long enough result */
  654. if (length == 0) {
  655. length = ops->digest_size;
  656. if (!raw_output) {
  657. length = length * 2;
  658. }
  659. }
  660. digest_length = length;
  661. if (!raw_output) {
  662. digest_length = (zend_long) ceil((float) length / 2.0);
  663. }
  664. loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
  665. result = safe_emalloc(loops, ops->digest_size, 0);
  666. computed_salt = safe_emalloc(salt_len, 1, 4);
  667. memcpy(computed_salt, (unsigned char *) salt, salt_len);
  668. for (i = 1; i <= loops; i++) {
  669. /* digest = hash_hmac(salt + pack('N', i), password) { */
  670. /* pack("N", i) */
  671. computed_salt[salt_len] = (unsigned char) (i >> 24);
  672. computed_salt[salt_len + 1] = (unsigned char) ((i & 0xFF0000) >> 16);
  673. computed_salt[salt_len + 2] = (unsigned char) ((i & 0xFF00) >> 8);
  674. computed_salt[salt_len + 3] = (unsigned char) (i & 0xFF);
  675. php_hash_hmac_round(digest, ops, context, K1, computed_salt, (zend_long) salt_len + 4);
  676. php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
  677. /* } */
  678. /* temp = digest */
  679. memcpy(temp, digest, ops->digest_size);
  680. /*
  681. * Note that the loop starting at 1 is intentional, since we've already done
  682. * the first round of the algorithm.
  683. */
  684. for (j = 1; j < iterations; j++) {
  685. /* digest = hash_hmac(digest, password) { */
  686. php_hash_hmac_round(digest, ops, context, K1, digest, ops->digest_size);
  687. php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
  688. /* } */
  689. /* temp ^= digest */
  690. php_hash_string_xor(temp, temp, digest, ops->digest_size);
  691. }
  692. /* result += temp */
  693. memcpy(result + ((i - 1) * ops->digest_size), temp, ops->digest_size);
  694. }
  695. /* Zero potentially sensitive variables */
  696. ZEND_SECURE_ZERO(K1, ops->block_size);
  697. ZEND_SECURE_ZERO(K2, ops->block_size);
  698. ZEND_SECURE_ZERO(computed_salt, salt_len + 4);
  699. efree(K1);
  700. efree(K2);
  701. efree(computed_salt);
  702. efree(context);
  703. efree(digest);
  704. efree(temp);
  705. returnval = zend_string_alloc(length, 0);
  706. if (raw_output) {
  707. memcpy(ZSTR_VAL(returnval), result, length);
  708. } else {
  709. php_hash_bin2hex(ZSTR_VAL(returnval), result, digest_length);
  710. }
  711. ZSTR_VAL(returnval)[length] = 0;
  712. efree(result);
  713. RETURN_NEW_STR(returnval);
  714. }
  715. /* }}} */
  716. /* {{{ proto bool hash_equals(string known_string, string user_string)
  717. Compares two strings using the same time whether they're equal or not.
  718. A difference in length will leak */
  719. PHP_FUNCTION(hash_equals)
  720. {
  721. zval *known_zval, *user_zval;
  722. char *known_str, *user_str;
  723. int result = 0;
  724. size_t j;
  725. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &known_zval, &user_zval) == FAILURE) {
  726. return;
  727. }
  728. /* We only allow comparing string to prevent unexpected results. */
  729. if (Z_TYPE_P(known_zval) != IS_STRING) {
  730. php_error_docref(NULL, E_WARNING, "Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
  731. RETURN_FALSE;
  732. }
  733. if (Z_TYPE_P(user_zval) != IS_STRING) {
  734. php_error_docref(NULL, E_WARNING, "Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
  735. RETURN_FALSE;
  736. }
  737. if (Z_STRLEN_P(known_zval) != Z_STRLEN_P(user_zval)) {
  738. RETURN_FALSE;
  739. }
  740. known_str = Z_STRVAL_P(known_zval);
  741. user_str = Z_STRVAL_P(user_zval);
  742. /* This is security sensitive code. Do not optimize this for speed. */
  743. for (j = 0; j < Z_STRLEN_P(known_zval); j++) {
  744. result |= known_str[j] ^ user_str[j];
  745. }
  746. RETURN_BOOL(0 == result);
  747. }
  748. /* }}} */
  749. /* {{{ proto HashContext::__construct() */
  750. static PHP_METHOD(HashContext, __construct) {
  751. /* Normally unreachable as private/final */
  752. zend_throw_exception(zend_ce_error, "Illegal call to private/final constructor", 0);
  753. }
  754. /* }}} */
  755. static const zend_function_entry php_hashcontext_methods[] = {
  756. PHP_ME(HashContext, __construct, NULL, ZEND_ACC_PRIVATE | ZEND_ACC_CTOR)
  757. PHP_FE_END
  758. };
  759. /* Module Housekeeping */
  760. #define PHP_HASH_HAVAL_REGISTER(p,b) php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
  761. #ifdef PHP_MHASH_BC
  762. #if 0
  763. /* See #69823, we should not insert module into module_registry while doing startup */
  764. PHP_MINFO_FUNCTION(mhash)
  765. {
  766. php_info_print_table_start();
  767. php_info_print_table_row(2, "MHASH support", "Enabled");
  768. php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
  769. php_info_print_table_end();
  770. }
  771. zend_module_entry mhash_module_entry = {
  772. STANDARD_MODULE_HEADER,
  773. "mhash",
  774. NULL,
  775. NULL,
  776. NULL,
  777. NULL,
  778. NULL,
  779. PHP_MINFO(mhash),
  780. PHP_MHASH_VERSION,
  781. STANDARD_MODULE_PROPERTIES,
  782. };
  783. #endif
  784. static void mhash_init(INIT_FUNC_ARGS)
  785. {
  786. char buf[128];
  787. int len;
  788. int algo_number = 0;
  789. for (algo_number = 0; algo_number < MHASH_NUM_ALGOS; algo_number++) {
  790. struct mhash_bc_entry algorithm = mhash_to_hash[algo_number];
  791. if (algorithm.mhash_name == NULL) {
  792. continue;
  793. }
  794. len = slprintf(buf, 127, "MHASH_%s", algorithm.mhash_name);
  795. zend_register_long_constant(buf, len, algorithm.value, CONST_CS | CONST_PERSISTENT, module_number);
  796. }
  797. /* TODO: this cause #69823 zend_register_internal_module(&mhash_module_entry); */
  798. }
  799. /* {{{ proto string mhash(int hash, string data [, string key])
  800. Hash data with hash */
  801. PHP_FUNCTION(mhash)
  802. {
  803. zval *z_algorithm;
  804. zend_long algorithm;
  805. if (zend_parse_parameters(1, "z", &z_algorithm) == FAILURE) {
  806. return;
  807. }
  808. algorithm = zval_get_long(z_algorithm);
  809. /* need to convert the first parameter from int constant to string algorithm name */
  810. if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
  811. struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
  812. if (algorithm_lookup.hash_name) {
  813. ZVAL_STRING(z_algorithm, algorithm_lookup.hash_name);
  814. }
  815. }
  816. if (ZEND_NUM_ARGS() == 3) {
  817. php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
  818. } else if (ZEND_NUM_ARGS() == 2) {
  819. php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
  820. } else {
  821. WRONG_PARAM_COUNT;
  822. }
  823. }
  824. /* }}} */
  825. /* {{{ proto string mhash_get_hash_name(int hash)
  826. Gets the name of hash */
  827. PHP_FUNCTION(mhash_get_hash_name)
  828. {
  829. zend_long algorithm;
  830. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
  831. return;
  832. }
  833. if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
  834. struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
  835. if (algorithm_lookup.mhash_name) {
  836. RETURN_STRING(algorithm_lookup.mhash_name);
  837. }
  838. }
  839. RETURN_FALSE;
  840. }
  841. /* }}} */
  842. /* {{{ proto int mhash_count(void)
  843. Gets the number of available hashes */
  844. PHP_FUNCTION(mhash_count)
  845. {
  846. if (zend_parse_parameters_none() == FAILURE) {
  847. return;
  848. }
  849. RETURN_LONG(MHASH_NUM_ALGOS - 1);
  850. }
  851. /* }}} */
  852. /* {{{ proto int mhash_get_block_size(int hash)
  853. Gets the block size of hash */
  854. PHP_FUNCTION(mhash_get_block_size)
  855. {
  856. zend_long algorithm;
  857. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
  858. return;
  859. }
  860. RETVAL_FALSE;
  861. if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
  862. struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
  863. if (algorithm_lookup.mhash_name) {
  864. const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
  865. if (ops) {
  866. RETVAL_LONG(ops->digest_size);
  867. }
  868. }
  869. }
  870. }
  871. /* }}} */
  872. #define SALT_SIZE 8
  873. /* {{{ proto string mhash_keygen_s2k(int hash, string input_password, string salt, int bytes)
  874. Generates a key using hash functions */
  875. PHP_FUNCTION(mhash_keygen_s2k)
  876. {
  877. zend_long algorithm, l_bytes;
  878. int bytes;
  879. char *password, *salt;
  880. size_t password_len, salt_len;
  881. char padded_salt[SALT_SIZE];
  882. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) {
  883. return;
  884. }
  885. bytes = (int)l_bytes;
  886. if (bytes <= 0){
  887. php_error_docref(NULL, E_WARNING, "the byte parameter must be greater than 0");
  888. RETURN_FALSE;
  889. }
  890. salt_len = MIN(salt_len, SALT_SIZE);
  891. memcpy(padded_salt, salt, salt_len);
  892. if (salt_len < SALT_SIZE) {
  893. memset(padded_salt + salt_len, 0, SALT_SIZE - salt_len);
  894. }
  895. salt_len = SALT_SIZE;
  896. RETVAL_FALSE;
  897. if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
  898. struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
  899. if (algorithm_lookup.mhash_name) {
  900. const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
  901. if (ops) {
  902. unsigned char null = '\0';
  903. void *context;
  904. char *key, *digest;
  905. int i = 0, j = 0;
  906. int block_size = ops->digest_size;
  907. int times = bytes / block_size;
  908. if (bytes % block_size != 0) times++;
  909. context = emalloc(ops->context_size);
  910. ops->hash_init(context);
  911. key = ecalloc(1, times * block_size);
  912. digest = emalloc(ops->digest_size + 1);
  913. for (i = 0; i < times; i++) {
  914. ops->hash_init(context);
  915. for (j=0;j<i;j++) {
  916. ops->hash_update(context, &null, 1);
  917. }
  918. ops->hash_update(context, (unsigned char *)padded_salt, salt_len);
  919. ops->hash_update(context, (unsigned char *)password, password_len);
  920. ops->hash_final((unsigned char *)digest, context);
  921. memcpy( &key[i*block_size], digest, block_size);
  922. }
  923. RETVAL_STRINGL(key, bytes);
  924. ZEND_SECURE_ZERO(key, bytes);
  925. efree(digest);
  926. efree(context);
  927. efree(key);
  928. }
  929. }
  930. }
  931. }
  932. /* }}} */
  933. #endif
  934. /* ----------------------------------------------------------------------- */
  935. /* {{{ php_hashcontext_create */
  936. static zend_object* php_hashcontext_create(zend_class_entry *ce) {
  937. php_hashcontext_object *objval = zend_object_alloc(sizeof(php_hashcontext_object), ce);
  938. zend_object *zobj = &objval->std;
  939. zend_object_std_init(zobj, ce);
  940. object_properties_init(zobj, ce);
  941. zobj->handlers = &php_hashcontext_handlers;
  942. return zobj;
  943. }
  944. /* }}} */
  945. /* {{{ php_hashcontext_dtor */
  946. static void php_hashcontext_dtor(zend_object *obj) {
  947. php_hashcontext_object *hash = php_hashcontext_from_object(obj);
  948. /* Just in case the algo has internally allocated resources */
  949. if (hash->context) {
  950. unsigned char *dummy = emalloc(hash->ops->digest_size);
  951. hash->ops->hash_final(dummy, hash->context);
  952. efree(dummy);
  953. efree(hash->context);
  954. hash->context = NULL;
  955. }
  956. if (hash->key) {
  957. ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
  958. efree(hash->key);
  959. hash->key = NULL;
  960. }
  961. }
  962. /* }}} */
  963. /* {{{ php_hashcontext_clone */
  964. static zend_object *php_hashcontext_clone(zval *pzv) {
  965. php_hashcontext_object *oldobj = php_hashcontext_from_object(Z_OBJ_P(pzv));
  966. zend_object *znew = php_hashcontext_create(Z_OBJCE_P(pzv));
  967. php_hashcontext_object *newobj = php_hashcontext_from_object(znew);
  968. zend_objects_clone_members(znew, Z_OBJ_P(pzv));
  969. newobj->ops = oldobj->ops;
  970. newobj->options = oldobj->options;
  971. newobj->context = emalloc(newobj->ops->context_size);
  972. newobj->ops->hash_init(newobj->context);
  973. if (SUCCESS != newobj->ops->hash_copy(newobj->ops, oldobj->context, newobj->context)) {
  974. efree(newobj->context);
  975. newobj->context = NULL;
  976. return znew;
  977. }
  978. newobj->key = ecalloc(1, newobj->ops->block_size);
  979. if (oldobj->key) {
  980. memcpy(newobj->key, oldobj->key, newobj->ops->block_size);
  981. }
  982. return znew;
  983. }
  984. /* }}} */
  985. /* {{{ PHP_MINIT_FUNCTION
  986. */
  987. PHP_MINIT_FUNCTION(hash)
  988. {
  989. zend_class_entry ce;
  990. zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
  991. php_hash_register_algo("md2", &php_hash_md2_ops);
  992. php_hash_register_algo("md4", &php_hash_md4_ops);
  993. php_hash_register_algo("md5", &php_hash_md5_ops);
  994. php_hash_register_algo("sha1", &php_hash_sha1_ops);
  995. php_hash_register_algo("sha224", &php_hash_sha224_ops);
  996. php_hash_register_algo("sha256", &php_hash_sha256_ops);
  997. php_hash_register_algo("sha384", &php_hash_sha384_ops);
  998. php_hash_register_algo("sha512/224", &php_hash_sha512_224_ops);
  999. php_hash_register_algo("sha512/256", &php_hash_sha512_256_ops);
  1000. php_hash_register_algo("sha512", &php_hash_sha512_ops);
  1001. php_hash_register_algo("sha3-224", &php_hash_sha3_224_ops);
  1002. php_hash_register_algo("sha3-256", &php_hash_sha3_256_ops);
  1003. php_hash_register_algo("sha3-384", &php_hash_sha3_384_ops);
  1004. php_hash_register_algo("sha3-512", &php_hash_sha3_512_ops);
  1005. php_hash_register_algo("ripemd128", &php_hash_ripemd128_ops);
  1006. php_hash_register_algo("ripemd160", &php_hash_ripemd160_ops);
  1007. php_hash_register_algo("ripemd256", &php_hash_ripemd256_ops);
  1008. php_hash_register_algo("ripemd320", &php_hash_ripemd320_ops);
  1009. php_hash_register_algo("whirlpool", &php_hash_whirlpool_ops);
  1010. php_hash_register_algo("tiger128,3", &php_hash_3tiger128_ops);
  1011. php_hash_register_algo("tiger160,3", &php_hash_3tiger160_ops);
  1012. php_hash_register_algo("tiger192,3", &php_hash_3tiger192_ops);
  1013. php_hash_register_algo("tiger128,4", &php_hash_4tiger128_ops);
  1014. php_hash_register_algo("tiger160,4", &php_hash_4tiger160_ops);
  1015. php_hash_register_algo("tiger192,4", &php_hash_4tiger192_ops);
  1016. php_hash_register_algo("snefru", &php_hash_snefru_ops);
  1017. php_hash_register_algo("snefru256", &php_hash_snefru_ops);
  1018. php_hash_register_algo("gost", &php_hash_gost_ops);
  1019. php_hash_register_algo("gost-crypto", &php_hash_gost_crypto_ops);
  1020. php_hash_register_algo("adler32", &php_hash_adler32_ops);
  1021. php_hash_register_algo("crc32", &php_hash_crc32_ops);
  1022. php_hash_register_algo("crc32b", &php_hash_crc32b_ops);
  1023. php_hash_register_algo("fnv132", &php_hash_fnv132_ops);
  1024. php_hash_register_algo("fnv1a32", &php_hash_fnv1a32_ops);
  1025. php_hash_register_algo("fnv164", &php_hash_fnv164_ops);
  1026. php_hash_register_algo("fnv1a64", &php_hash_fnv1a64_ops);
  1027. php_hash_register_algo("joaat", &php_hash_joaat_ops);
  1028. PHP_HASH_HAVAL_REGISTER(3,128);
  1029. PHP_HASH_HAVAL_REGISTER(3,160);
  1030. PHP_HASH_HAVAL_REGISTER(3,192);
  1031. PHP_HASH_HAVAL_REGISTER(3,224);
  1032. PHP_HASH_HAVAL_REGISTER(3,256);
  1033. PHP_HASH_HAVAL_REGISTER(4,128);
  1034. PHP_HASH_HAVAL_REGISTER(4,160);
  1035. PHP_HASH_HAVAL_REGISTER(4,192);
  1036. PHP_HASH_HAVAL_REGISTER(4,224);
  1037. PHP_HASH_HAVAL_REGISTER(4,256);
  1038. PHP_HASH_HAVAL_REGISTER(5,128);
  1039. PHP_HASH_HAVAL_REGISTER(5,160);
  1040. PHP_HASH_HAVAL_REGISTER(5,192);
  1041. PHP_HASH_HAVAL_REGISTER(5,224);
  1042. PHP_HASH_HAVAL_REGISTER(5,256);
  1043. REGISTER_LONG_CONSTANT("HASH_HMAC", PHP_HASH_HMAC, CONST_CS | CONST_PERSISTENT);
  1044. INIT_CLASS_ENTRY(ce, "HashContext", php_hashcontext_methods);
  1045. php_hashcontext_ce = zend_register_internal_class(&ce);
  1046. php_hashcontext_ce->ce_flags |= ZEND_ACC_FINAL;
  1047. php_hashcontext_ce->create_object = php_hashcontext_create;
  1048. php_hashcontext_ce->serialize = zend_class_serialize_deny;
  1049. php_hashcontext_ce->unserialize = zend_class_unserialize_deny;
  1050. memcpy(&php_hashcontext_handlers, &std_object_handlers,
  1051. sizeof(zend_object_handlers));
  1052. php_hashcontext_handlers.offset = XtOffsetOf(php_hashcontext_object, std);
  1053. php_hashcontext_handlers.dtor_obj = php_hashcontext_dtor;
  1054. php_hashcontext_handlers.clone_obj = php_hashcontext_clone;
  1055. #ifdef PHP_MHASH_BC
  1056. mhash_init(INIT_FUNC_ARGS_PASSTHRU);
  1057. #endif
  1058. return SUCCESS;
  1059. }
  1060. /* }}} */
  1061. /* {{{ PHP_MSHUTDOWN_FUNCTION
  1062. */
  1063. PHP_MSHUTDOWN_FUNCTION(hash)
  1064. {
  1065. zend_hash_destroy(&php_hash_hashtable);
  1066. return SUCCESS;
  1067. }
  1068. /* }}} */
  1069. /* {{{ PHP_MINFO_FUNCTION
  1070. */
  1071. PHP_MINFO_FUNCTION(hash)
  1072. {
  1073. char buffer[2048];
  1074. zend_string *str;
  1075. char *s = buffer, *e = s + sizeof(buffer);
  1076. ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
  1077. s += slprintf(s, e - s, "%s ", ZSTR_VAL(str));
  1078. } ZEND_HASH_FOREACH_END();
  1079. *s = 0;
  1080. php_info_print_table_start();
  1081. php_info_print_table_row(2, "hash support", "enabled");
  1082. php_info_print_table_row(2, "Hashing Engines", buffer);
  1083. php_info_print_table_end();
  1084. #ifdef PHP_MHASH_BC
  1085. php_info_print_table_start();
  1086. php_info_print_table_row(2, "MHASH support", "Enabled");
  1087. php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
  1088. php_info_print_table_end();
  1089. #endif
  1090. }
  1091. /* }}} */
  1092. /* {{{ arginfo */
  1093. #ifdef PHP_HASH_MD5_NOT_IN_CORE
  1094. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5, 0, 0, 1)
  1095. ZEND_ARG_INFO(0, str)
  1096. ZEND_ARG_INFO(0, raw_output)
  1097. ZEND_END_ARG_INFO()
  1098. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5_file, 0, 0, 1)
  1099. ZEND_ARG_INFO(0, filename)
  1100. ZEND_ARG_INFO(0, raw_output)
  1101. ZEND_END_ARG_INFO()
  1102. #endif
  1103. #ifdef PHP_HASH_SHA1_NOT_IN_CORE
  1104. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1, 0, 0, 1)
  1105. ZEND_ARG_INFO(0, str)
  1106. ZEND_ARG_INFO(0, raw_output)
  1107. ZEND_END_ARG_INFO()
  1108. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1_file, 0, 0, 1)
  1109. ZEND_ARG_INFO(0, filename)
  1110. ZEND_ARG_INFO(0, raw_output)
  1111. ZEND_END_ARG_INFO()
  1112. #endif
  1113. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash, 0, 0, 2)
  1114. ZEND_ARG_INFO(0, algo)
  1115. ZEND_ARG_INFO(0, data)
  1116. ZEND_ARG_INFO(0, raw_output)
  1117. ZEND_END_ARG_INFO()
  1118. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_file, 0, 0, 2)
  1119. ZEND_ARG_INFO(0, algo)
  1120. ZEND_ARG_INFO(0, filename)
  1121. ZEND_ARG_INFO(0, raw_output)
  1122. ZEND_END_ARG_INFO()
  1123. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac, 0, 0, 3)
  1124. ZEND_ARG_INFO(0, algo)
  1125. ZEND_ARG_INFO(0, data)
  1126. ZEND_ARG_INFO(0, key)
  1127. ZEND_ARG_INFO(0, raw_output)
  1128. ZEND_END_ARG_INFO()
  1129. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac_file, 0, 0, 3)
  1130. ZEND_ARG_INFO(0, algo)
  1131. ZEND_ARG_INFO(0, filename)
  1132. ZEND_ARG_INFO(0, key)
  1133. ZEND_ARG_INFO(0, raw_output)
  1134. ZEND_END_ARG_INFO()
  1135. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_init, 0, 0, 1)
  1136. ZEND_ARG_INFO(0, algo)
  1137. ZEND_ARG_INFO(0, options)
  1138. ZEND_ARG_INFO(0, key)
  1139. ZEND_END_ARG_INFO()
  1140. ZEND_BEGIN_ARG_INFO(arginfo_hash_update, 0)
  1141. ZEND_ARG_INFO(0, context)
  1142. ZEND_ARG_INFO(0, data)
  1143. ZEND_END_ARG_INFO()
  1144. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_stream, 0, 0, 2)
  1145. ZEND_ARG_INFO(0, context)
  1146. ZEND_ARG_INFO(0, handle)
  1147. ZEND_ARG_INFO(0, length)
  1148. ZEND_END_ARG_INFO()
  1149. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_file, 0, 0, 2)
  1150. ZEND_ARG_INFO(0, context)
  1151. ZEND_ARG_INFO(0, filename)
  1152. ZEND_ARG_INFO(0, stream_context)
  1153. ZEND_END_ARG_INFO()
  1154. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_final, 0, 0, 1)
  1155. ZEND_ARG_INFO(0, context)
  1156. ZEND_ARG_INFO(0, raw_output)
  1157. ZEND_END_ARG_INFO()
  1158. ZEND_BEGIN_ARG_INFO(arginfo_hash_copy, 0)
  1159. ZEND_ARG_INFO(0, context)
  1160. ZEND_END_ARG_INFO()
  1161. ZEND_BEGIN_ARG_INFO(arginfo_hash_algos, 0)
  1162. ZEND_END_ARG_INFO()
  1163. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_pbkdf2, 0, 0, 4)
  1164. ZEND_ARG_INFO(0, algo)
  1165. ZEND_ARG_INFO(0, password)
  1166. ZEND_ARG_INFO(0, salt)
  1167. ZEND_ARG_INFO(0, iterations)
  1168. ZEND_ARG_INFO(0, length)
  1169. ZEND_ARG_INFO(0, raw_output)
  1170. ZEND_END_ARG_INFO()
  1171. ZEND_BEGIN_ARG_INFO(arginfo_hash_equals, 0)
  1172. ZEND_ARG_INFO(0, known_string)
  1173. ZEND_ARG_INFO(0, user_string)
  1174. ZEND_END_ARG_INFO()
  1175. ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hkdf, 0, 0, 2)
  1176. ZEND_ARG_INFO(0, ikm)
  1177. ZEND_ARG_INFO(0, algo)
  1178. ZEND_ARG_INFO(0, length)
  1179. ZEND_ARG_INFO(0, string)
  1180. ZEND_ARG_INFO(0, salt)
  1181. ZEND_END_ARG_INFO()
  1182. /* BC Land */
  1183. #ifdef PHP_MHASH_BC
  1184. ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_block_size, 0)
  1185. ZEND_ARG_INFO(0, hash)
  1186. ZEND_END_ARG_INFO()
  1187. ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_hash_name, 0)
  1188. ZEND_ARG_INFO(0, hash)
  1189. ZEND_END_ARG_INFO()
  1190. ZEND_BEGIN_ARG_INFO(arginfo_mhash_keygen_s2k, 0)
  1191. ZEND_ARG_INFO(0, hash)
  1192. ZEND_ARG_INFO(0, input_password)
  1193. ZEND_ARG_INFO(0, salt)
  1194. ZEND_ARG_INFO(0, bytes)
  1195. ZEND_END_ARG_INFO()
  1196. ZEND_BEGIN_ARG_INFO(arginfo_mhash_count, 0)
  1197. ZEND_END_ARG_INFO()
  1198. ZEND_BEGIN_ARG_INFO_EX(arginfo_mhash, 0, 0, 2)
  1199. ZEND_ARG_INFO(0, hash)
  1200. ZEND_ARG_INFO(0, data)
  1201. ZEND_ARG_INFO(0, key)
  1202. ZEND_END_ARG_INFO()
  1203. #endif
  1204. /* }}} */
  1205. /* {{{ hash_functions[]
  1206. */
  1207. static const zend_function_entry hash_functions[] = {
  1208. PHP_FE(hash, arginfo_hash)
  1209. PHP_FE(hash_file, arginfo_hash_file)
  1210. PHP_FE(hash_hmac, arginfo_hash_hmac)
  1211. PHP_FE(hash_hmac_file, arginfo_hash_hmac_file)
  1212. PHP_FE(hash_init, arginfo_hash_init)
  1213. PHP_FE(hash_update, arginfo_hash_update)
  1214. PHP_FE(hash_update_stream, arginfo_hash_update_stream)
  1215. PHP_FE(hash_update_file, arginfo_hash_update_file)
  1216. PHP_FE(hash_final, arginfo_hash_final)
  1217. PHP_FE(hash_copy, arginfo_hash_copy)
  1218. PHP_FE(hash_algos, arginfo_hash_algos)
  1219. PHP_FE(hash_hmac_algos, arginfo_hash_algos)
  1220. PHP_FE(hash_pbkdf2, arginfo_hash_pbkdf2)
  1221. PHP_FE(hash_equals, arginfo_hash_equals)
  1222. PHP_FE(hash_hkdf, arginfo_hash_hkdf)
  1223. /* BC Land */
  1224. #ifdef PHP_HASH_MD5_NOT_IN_CORE
  1225. PHP_NAMED_FE(md5, php_if_md5, arginfo_hash_md5)
  1226. PHP_NAMED_FE(md5_file, php_if_md5_file, arginfo_hash_md5_file)
  1227. #endif /* PHP_HASH_MD5_NOT_IN_CORE */
  1228. #ifdef PHP_HASH_SHA1_NOT_IN_CORE
  1229. PHP_NAMED_FE(sha1, php_if_sha1, arginfo_hash_sha1)
  1230. PHP_NAMED_FE(sha1_file, php_if_sha1_file, arginfo_hash_sha1_file)
  1231. #endif /* PHP_HASH_SHA1_NOT_IN_CORE */
  1232. #ifdef PHP_MHASH_BC
  1233. PHP_FE(mhash_keygen_s2k, arginfo_mhash_keygen_s2k)
  1234. PHP_FE(mhash_get_block_size, arginfo_mhash_get_block_size)
  1235. PHP_FE(mhash_get_hash_name, arginfo_mhash_get_hash_name)
  1236. PHP_FE(mhash_count, arginfo_mhash_count)
  1237. PHP_FE(mhash, arginfo_mhash)
  1238. #endif
  1239. PHP_FE_END
  1240. };
  1241. /* }}} */
  1242. /* {{{ hash_module_entry
  1243. */
  1244. zend_module_entry hash_module_entry = {
  1245. STANDARD_MODULE_HEADER,
  1246. PHP_HASH_EXTNAME,
  1247. hash_functions,
  1248. PHP_MINIT(hash),
  1249. PHP_MSHUTDOWN(hash),
  1250. NULL, /* RINIT */
  1251. NULL, /* RSHUTDOWN */
  1252. PHP_MINFO(hash),
  1253. PHP_HASH_VERSION,
  1254. STANDARD_MODULE_PROPERTIES
  1255. };
  1256. /* }}} */
  1257. #ifdef COMPILE_DL_HASH
  1258. ZEND_GET_MODULE(hash)
  1259. #endif
  1260. /*
  1261. * Local variables:
  1262. * tab-width: 4
  1263. * c-basic-offset: 4
  1264. * End:
  1265. * vim600: noet sw=4 ts=4 fdm=marker
  1266. * vim<600: noet sw=4 ts=4
  1267. */