1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2013-2016 Jakub Zelenka                                |
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: Jakub Zelenka <bukka@php.net>                                |
16   +----------------------------------------------------------------------+
17 */
18 
19 #ifndef PHP_CRYPTO_KDF_H
20 #define PHP_CRYPTO_KDF_H
21 
22 #include "php.h"
23 #include "php_crypto.h"
24 
25 /* PBKDF2 feature test */
26 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
27 #define PHP_CRYPTO_HAS_PBKDF2 1
28 #endif
29 
30 typedef enum {
31 	PHP_CRYPTO_KDF_TYPE_NONE,
32 	PHP_CRYPTO_KDF_TYPE_PBKDF2
33 } php_crypto_kdf_type;
34 
35 PHPC_OBJ_STRUCT_BEGIN(crypto_kdf)
36 	php_crypto_kdf_type type;
37 	union {
38 		struct {
39 			const EVP_MD *md;
40 			int iter;
41 		} pbkdf2;
42 	} ctx;
43 	char *salt;
44 	int salt_len;
45 	int key_len;
46 PHPC_OBJ_STRUCT_END()
47 
48 
49 #define PHP_CRYPTO_PBKDF2_CTX_MD(pobj) (pobj)->ctx.pbkdf2.md
50 #define PHP_CRYPTO_PBKDF2_CTX_ITER(pobj) (pobj)->ctx.pbkdf2.iter
51 
52 #define PHP_CRYPTO_PBKDF2_ITER_DEFAULT 1000
53 
54 /* Exceptions */
55 PHP_CRYPTO_EXCEPTION_EXPORT(KDF)
56 PHP_CRYPTO_EXCEPTION_EXPORT(PBKDF2)
57 
58 /* CLASSES */
59 
60 /* Class entries */
61 extern PHP_CRYPTO_API zend_class_entry *php_crypto_kdf_ce;
62 extern PHP_CRYPTO_API zend_class_entry *php_crypto_pbkdf2_ce;
63 
64 /* USER METHODS */
65 
66 /* Module init for Crypto KDF */
67 PHP_MINIT_FUNCTION(crypto_kdf);
68 
69 /* KDF methods */
70 PHP_CRYPTO_METHOD(KDF, __construct);
71 PHP_CRYPTO_METHOD(KDF, getLength);
72 PHP_CRYPTO_METHOD(KDF, setLength);
73 PHP_CRYPTO_METHOD(KDF, getSalt);
74 PHP_CRYPTO_METHOD(KDF, setSalt);
75 
76 #ifdef PHP_CRYPTO_HAS_PBKDF2
77 /* PBKDF2 methods */
78 PHP_CRYPTO_METHOD(PBKDF2, __construct);
79 PHP_CRYPTO_METHOD(PBKDF2, derive);
80 PHP_CRYPTO_METHOD(PBKDF2, getIterations);
81 PHP_CRYPTO_METHOD(PBKDF2, setIterations);
82 PHP_CRYPTO_METHOD(PBKDF2, getHashAlgorithm);
83 PHP_CRYPTO_METHOD(PBKDF2, setHashAlgorithm);
84 #endif
85 
86 #endif	/* PHP_CRYPTO_KDF_H */
87 
88 /*
89  * Local variables:
90  * tab-width: 4
91  * c-basic-offset: 4
92  * End:
93  * vim600: noet sw=4 ts=4 fdm=marker
94  * vim<600: noet sw=4 ts=4
95  */
96