xref: /PHP-7.2/ext/hash/php_hash.h (revision 7a7ec01a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Sara Golemon <pollita@php.net>                               |
16   +----------------------------------------------------------------------+
17 */
18 
19 /* $Id$ */
20 
21 #ifndef PHP_HASH_H
22 #define PHP_HASH_H
23 
24 #include "php.h"
25 
26 #define PHP_HASH_EXTNAME	"hash"
27 #define PHP_HASH_VERSION	"1.0"
28 #define PHP_MHASH_VERSION	"1.0"
29 
30 #define PHP_HASH_HMAC		0x0001
31 
32 #define L64 INT64_C
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, unsigned int 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 
39 typedef struct _php_hash_ops {
40 	php_hash_init_func_t hash_init;
41 	php_hash_update_func_t hash_update;
42 	php_hash_final_func_t hash_final;
43 	php_hash_copy_func_t hash_copy;
44 
45 	int digest_size;
46 	int block_size;
47 	int context_size;
48 	unsigned is_crypto: 1;
49 } php_hash_ops;
50 
51 typedef struct _php_hashcontext_object {
52 	const php_hash_ops *ops;
53 	void *context;
54 
55 	zend_long options;
56 	unsigned char *key;
57 
58 	zend_object std;
59 } php_hashcontext_object;
60 
php_hashcontext_from_object(zend_object * obj)61 static inline php_hashcontext_object *php_hashcontext_from_object(zend_object *obj) {
62 	return ((php_hashcontext_object*)(obj + 1)) - 1;
63 }
64 
65 extern const php_hash_ops php_hash_md2_ops;
66 extern const php_hash_ops php_hash_md4_ops;
67 extern const php_hash_ops php_hash_md5_ops;
68 extern const php_hash_ops php_hash_sha1_ops;
69 extern const php_hash_ops php_hash_sha224_ops;
70 extern const php_hash_ops php_hash_sha256_ops;
71 extern const php_hash_ops php_hash_sha384_ops;
72 extern const php_hash_ops php_hash_sha512_ops;
73 extern const php_hash_ops php_hash_sha512_256_ops;
74 extern const php_hash_ops php_hash_sha512_224_ops;
75 extern const php_hash_ops php_hash_sha3_224_ops;
76 extern const php_hash_ops php_hash_sha3_256_ops;
77 extern const php_hash_ops php_hash_sha3_384_ops;
78 extern const php_hash_ops php_hash_sha3_512_ops;
79 extern const php_hash_ops php_hash_ripemd128_ops;
80 extern const php_hash_ops php_hash_ripemd160_ops;
81 extern const php_hash_ops php_hash_ripemd256_ops;
82 extern const php_hash_ops php_hash_ripemd320_ops;
83 extern const php_hash_ops php_hash_whirlpool_ops;
84 extern const php_hash_ops php_hash_3tiger128_ops;
85 extern const php_hash_ops php_hash_3tiger160_ops;
86 extern const php_hash_ops php_hash_3tiger192_ops;
87 extern const php_hash_ops php_hash_4tiger128_ops;
88 extern const php_hash_ops php_hash_4tiger160_ops;
89 extern const php_hash_ops php_hash_4tiger192_ops;
90 extern const php_hash_ops php_hash_snefru_ops;
91 extern const php_hash_ops php_hash_gost_ops;
92 extern const php_hash_ops php_hash_gost_crypto_ops;
93 extern const php_hash_ops php_hash_adler32_ops;
94 extern const php_hash_ops php_hash_crc32_ops;
95 extern const php_hash_ops php_hash_crc32b_ops;
96 extern const php_hash_ops php_hash_fnv132_ops;
97 extern const php_hash_ops php_hash_fnv1a32_ops;
98 extern const php_hash_ops php_hash_fnv164_ops;
99 extern const php_hash_ops php_hash_fnv1a64_ops;
100 extern const php_hash_ops php_hash_joaat_ops;
101 
102 #define PHP_HASH_HAVAL_OPS(p,b)	extern const php_hash_ops php_hash_##p##haval##b##_ops;
103 
104 PHP_HASH_HAVAL_OPS(3,128)
105 PHP_HASH_HAVAL_OPS(3,160)
106 PHP_HASH_HAVAL_OPS(3,192)
107 PHP_HASH_HAVAL_OPS(3,224)
108 PHP_HASH_HAVAL_OPS(3,256)
109 
110 PHP_HASH_HAVAL_OPS(4,128)
111 PHP_HASH_HAVAL_OPS(4,160)
112 PHP_HASH_HAVAL_OPS(4,192)
113 PHP_HASH_HAVAL_OPS(4,224)
114 PHP_HASH_HAVAL_OPS(4,256)
115 
116 PHP_HASH_HAVAL_OPS(5,128)
117 PHP_HASH_HAVAL_OPS(5,160)
118 PHP_HASH_HAVAL_OPS(5,192)
119 PHP_HASH_HAVAL_OPS(5,224)
120 PHP_HASH_HAVAL_OPS(5,256)
121 
122 extern zend_module_entry hash_module_entry;
123 #define phpext_hash_ptr &hash_module_entry
124 
125 #ifdef PHP_WIN32
126 #	define PHP_HASH_API __declspec(dllexport)
127 #elif defined(__GNUC__) && __GNUC__ >= 4
128 #	define PHP_HASH_API __attribute__ ((visibility("default")))
129 #else
130 #	define PHP_HASH_API
131 #endif
132 
133 #ifdef ZTS
134 #include "TSRM.h"
135 #endif
136 
137 PHP_FUNCTION(hash);
138 PHP_FUNCTION(hash_file);
139 PHP_FUNCTION(hash_hkdf);
140 PHP_FUNCTION(hash_hmac);
141 PHP_FUNCTION(hash_hmac_file);
142 PHP_FUNCTION(hash_init);
143 PHP_FUNCTION(hash_update);
144 PHP_FUNCTION(hash_update_stream);
145 PHP_FUNCTION(hash_update_file);
146 PHP_FUNCTION(hash_final);
147 PHP_FUNCTION(hash_algos);
148 PHP_FUNCTION(hash_pbkdf2);
149 PHP_FUNCTION(hash_equals);
150 
151 extern PHP_HASH_API zend_class_entry *php_hashcontext_ce;
152 PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, size_t algo_len);
153 PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops);
154 PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_context);
155 
php_hash_bin2hex(char * out,const unsigned char * in,int in_len)156 static inline void php_hash_bin2hex(char *out, const unsigned char *in, int in_len)
157 {
158 	static const char hexits[17] = "0123456789abcdef";
159 	int i;
160 
161 	for(i = 0; i < in_len; i++) {
162 		out[i * 2]       = hexits[in[i] >> 4];
163 		out[(i * 2) + 1] = hexits[in[i] &  0x0F];
164 	}
165 }
166 
167 #endif	/* PHP_HASH_H */
168 
169 
170 /*
171  * Local variables:
172  * tab-width: 4
173  * c-basic-offset: 4
174  * End:
175  * vim600: noet sw=4 ts=4 fdm=marker
176  * vim<600: noet sw=4 ts=4
177  */
178