-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtomcrypt_misc.c
102 lines (84 loc) · 3.42 KB
/
tomcrypt_misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 2017 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: François Poirotte <[email protected]> |
+----------------------------------------------------------------------+
*/
#include <tomcrypt.h>
#include "php_tomcrypt_compat.h"
ZEND_EXTERN_MODULE_GLOBALS(tomcrypt)
/* {{{ proto void tomcrypt_clear_error()
Clear the last error returned by LibTomCrypt. */
PHP_FUNCTION(tomcrypt_clear)
{
TOMCRYPT_G(last_error) = 0;
}
/* }}} */
/* {{{ proto int tomcrypt_errno()
Retrieve the error number returned by the last LibTomCrypt
function that failed. */
PHP_FUNCTION(tomcrypt_errno)
{
RETURN_LONG(TOMCRYPT_G(last_error));
}
/* }}} */
/* {{{ proto string tomcrypt_error(int errno=null)
Retrieve the error message for the given errno */
PHP_FUNCTION(tomcrypt_error)
{
pltc_long err = TOMCRYPT_G(last_error);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &err) == FAILURE) {
return;
}
PLTC_RETVAL_STRING((char *) error_to_string(err), 1);
}
/* }}} */
/* {{{ proto string tomcrypt_hkdf(string algo, string input, int length=0, string salt='', string info='')
Expand the given input to produce keying material of the desired length */
PHP_FUNCTION(tomcrypt_hkdf)
{
#ifdef LTC_HKDF
char *algo, *input, *salt, *info, *output;
pltc_size algo_len, input_len, salt_len = 0, info_len = 0;
pltc_long output_len = 0;
int index, err;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|lss",
&algo, &algo_len, &input, &input_len, &output_len, &salt, &salt_len, &info, &info_len) == FAILURE) {
return;
}
if ((index = find_hash(algo)) == -1) {
TOMCRYPT_G(last_error) = CRYPT_INVALID_ARG;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown hashing algorithm: %s", algo);
RETURN_FALSE;
}
if (output_len < 0 || output_len > 255 * hash_descriptor[index].hashsize) {
TOMCRYPT_G(last_error) = CRYPT_INVALID_ARG;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid output length requested (%d)", output_len);
RETURN_FALSE;
}
output = emalloc(output_len + 1);
output[output_len] = '\0';
if ((err = hkdf(index, salt, salt_len, info, info_len, input, input_len, output, output_len)) != CRYPT_OK) {
efree(output);
TOMCRYPT_G(last_error) = err;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", error_to_string(err));
RETURN_FALSE;
}
PLTC_RETURN_STRINGL(output, output_len, 0);
#else
TOMCRYPT_G(last_error) = CRYPT_ERROR;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported operation");
RETURN_FALSE;
#endif
}