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   | http://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   +----------------------------------------------------------------------+
15 */
16 
17 #ifndef PHP_HASH_H
18 #define PHP_HASH_H
19 
20 #include "php.h"
21 
22 #define PHP_HASH_EXTNAME	"hash"
23 #define PHP_HASH_VERSION	PHP_VERSION
24 #define PHP_MHASH_VERSION	PHP_VERSION
25 
26 #define PHP_HASH_HMAC		0x0001
27 
28 #define PHP_HASH_SERIALIZE_MAGIC_SPEC          2
29 
30 #define L64 INT64_C
31 
32 typedef struct _php_hashcontext_object php_hashcontext_object;
33 
34 typedef void (*php_hash_init_func_t)(void *context);
35 typedef void (*php_hash_update_func_t)(void *context, const unsigned char *buf, size_t count);
36 typedef void (*php_hash_final_func_t)(unsigned char *digest, void *context);
37 typedef int  (*php_hash_copy_func_t)(const void *ops, void *orig_context, void *dest_context);
38 typedef int  (*php_hash_serialize_func_t)(const php_hashcontext_object *hash, zend_long *magic, zval *zv);
39 typedef int  (*php_hash_unserialize_func_t)(php_hashcontext_object *hash, zend_long magic, const zval *zv);
40 
41 typedef struct _php_hash_ops {
42 	const char *algo;
43 	php_hash_init_func_t hash_init;
44 	php_hash_update_func_t hash_update;
45 	php_hash_final_func_t hash_final;
46 	php_hash_copy_func_t hash_copy;
47 	php_hash_serialize_func_t hash_serialize;
48 	php_hash_unserialize_func_t hash_unserialize;
49 	const char *serialize_spec;
50 
51 	size_t digest_size;
52 	size_t block_size;
53 	size_t context_size;
54 	unsigned is_crypto: 1;
55 } php_hash_ops;
56 
57 struct _php_hashcontext_object {
58 	const php_hash_ops *ops;
59 	void *context;
60 
61 	zend_long options;
62 	unsigned char *key;
63 
64 	zend_object std;
65 };
66 
php_hashcontext_from_object(zend_object * obj)67 static inline php_hashcontext_object *php_hashcontext_from_object(zend_object *obj) {
68 	return ((php_hashcontext_object*)(obj + 1)) - 1;
69 }
70 
71 extern const php_hash_ops php_hash_md2_ops;
72 extern const php_hash_ops php_hash_md4_ops;
73 extern const php_hash_ops php_hash_md5_ops;
74 extern const php_hash_ops php_hash_sha1_ops;
75 extern const php_hash_ops php_hash_sha224_ops;
76 extern const php_hash_ops php_hash_sha256_ops;
77 extern const php_hash_ops php_hash_sha384_ops;
78 extern const php_hash_ops php_hash_sha512_ops;
79 extern const php_hash_ops php_hash_sha512_256_ops;
80 extern const php_hash_ops php_hash_sha512_224_ops;
81 extern const php_hash_ops php_hash_sha3_224_ops;
82 extern const php_hash_ops php_hash_sha3_256_ops;
83 extern const php_hash_ops php_hash_sha3_384_ops;
84 extern const php_hash_ops php_hash_sha3_512_ops;
85 extern const php_hash_ops php_hash_ripemd128_ops;
86 extern const php_hash_ops php_hash_ripemd160_ops;
87 extern const php_hash_ops php_hash_ripemd256_ops;
88 extern const php_hash_ops php_hash_ripemd320_ops;
89 extern const php_hash_ops php_hash_whirlpool_ops;
90 extern const php_hash_ops php_hash_3tiger128_ops;
91 extern const php_hash_ops php_hash_3tiger160_ops;
92 extern const php_hash_ops php_hash_3tiger192_ops;
93 extern const php_hash_ops php_hash_4tiger128_ops;
94 extern const php_hash_ops php_hash_4tiger160_ops;
95 extern const php_hash_ops php_hash_4tiger192_ops;
96 extern const php_hash_ops php_hash_snefru_ops;
97 extern const php_hash_ops php_hash_gost_ops;
98 extern const php_hash_ops php_hash_gost_crypto_ops;
99 extern const php_hash_ops php_hash_adler32_ops;
100 extern const php_hash_ops php_hash_crc32_ops;
101 extern const php_hash_ops php_hash_crc32b_ops;
102 extern const php_hash_ops php_hash_crc32c_ops;
103 extern const php_hash_ops php_hash_fnv132_ops;
104 extern const php_hash_ops php_hash_fnv1a32_ops;
105 extern const php_hash_ops php_hash_fnv164_ops;
106 extern const php_hash_ops php_hash_fnv1a64_ops;
107 extern const php_hash_ops php_hash_joaat_ops;
108 
109 #define PHP_HASH_HAVAL_OPS(p,b)	extern const php_hash_ops php_hash_##p##haval##b##_ops;
110 
111 PHP_HASH_HAVAL_OPS(3,128)
112 PHP_HASH_HAVAL_OPS(3,160)
113 PHP_HASH_HAVAL_OPS(3,192)
114 PHP_HASH_HAVAL_OPS(3,224)
115 PHP_HASH_HAVAL_OPS(3,256)
116 
117 PHP_HASH_HAVAL_OPS(4,128)
118 PHP_HASH_HAVAL_OPS(4,160)
119 PHP_HASH_HAVAL_OPS(4,192)
120 PHP_HASH_HAVAL_OPS(4,224)
121 PHP_HASH_HAVAL_OPS(4,256)
122 
123 PHP_HASH_HAVAL_OPS(5,128)
124 PHP_HASH_HAVAL_OPS(5,160)
125 PHP_HASH_HAVAL_OPS(5,192)
126 PHP_HASH_HAVAL_OPS(5,224)
127 PHP_HASH_HAVAL_OPS(5,256)
128 
129 extern zend_module_entry hash_module_entry;
130 #define phpext_hash_ptr &hash_module_entry
131 
132 #ifdef PHP_WIN32
133 #	define PHP_HASH_API __declspec(dllexport)
134 #elif defined(__GNUC__) && __GNUC__ >= 4
135 #	define PHP_HASH_API __attribute__ ((visibility("default")))
136 #else
137 #	define PHP_HASH_API
138 #endif
139 
140 extern PHP_HASH_API zend_class_entry *php_hashcontext_ce;
141 PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(zend_string *algo);
142 PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops);
143 PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_context);
144 PHP_HASH_API int php_hash_serialize(const php_hashcontext_object *context, zend_long *magic, zval *zv);
145 PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *context, zend_long magic, const zval *zv);
146 PHP_HASH_API int php_hash_serialize_spec(const php_hashcontext_object *context, zval *zv, const char *spec);
147 PHP_HASH_API int php_hash_unserialize_spec(php_hashcontext_object *hash, const zval *zv, const char *spec);
148 
php_hash_alloc_context(const php_hash_ops * ops)149 static inline void *php_hash_alloc_context(const php_hash_ops *ops) {
150 	/* Zero out context memory so serialization doesn't expose internals */
151 	return ecalloc(1, ops->context_size);
152 }
153 
php_hash_bin2hex(char * out,const unsigned char * in,size_t in_len)154 static inline void php_hash_bin2hex(char *out, const unsigned char *in, size_t in_len)
155 {
156 	static const char hexits[17] = "0123456789abcdef";
157 	size_t i;
158 
159 	for(i = 0; i < in_len; i++) {
160 		out[i * 2]       = hexits[in[i] >> 4];
161 		out[(i * 2) + 1] = hexits[in[i] &  0x0F];
162 	}
163 }
164 
165 #endif	/* PHP_HASH_H */
166