-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_tomcrypt_crypt_stream.h
160 lines (127 loc) · 4.03 KB
/
php_tomcrypt_crypt_stream.h
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <tomcrypt.h>
#include "Zend/zend_types.h"
#include "php_tomcrypt_compat.h"
#include "php_tomcrypt_cipher.h"
#include "php_tomcrypt_crypt.h"
typedef enum {
/* -1 is reserved (invalid cipher / cipher not found),
positive values are used for block ciphers. */
PHP_TOMCRYPT_STREAM_CIPHER_RC4 = -2,
PHP_TOMCRYPT_STREAM_CIPHER_CHACHA = -3,
PHP_TOMCRYPT_STREAM_CIPHER_SOBER128 = -4,
} php_tomcrypt_stream_cipher;
static void php_tomcrypt_xcrypt_stream_chacha(PLTC_CRYPT_PARAM)
{
#ifdef LTC_CHACHA
chacha_state state;
char *output, *nonce;
int err, num_rounds;
pltc_size nonce_len;
pltc_long counter;
GET_OPT_STRING(options, "nonce", nonce, nonce_len, NULL);
GET_OPT_LONG(options, "counter", counter, 0);
GET_OPT_LONG(options, "rounds", num_rounds, 0);
output = emalloc(input_len + 1);
output[input_len] = '\0';
if ((err = chacha_setup(&state, key, key_len, num_rounds)) != CRYPT_OK) {
goto error;
}
if (nonce_len == 12) {
if ((err = chacha_ivctr32(&state, nonce, nonce_len, counter)) != CRYPT_OK) {
goto error;
}
} else if ((err = chacha_ivctr64(&state, nonce, nonce_len, counter)) != CRYPT_OK) {
goto error;
}
if ((err = chacha_crypt(&state, input, input_len, output)) != CRYPT_OK) {
goto error;
}
if ((err = chacha_done(&state)) != CRYPT_OK) {
goto error;
}
PLTC_RETURN_STRINGL(output, input_len, 0);
error:
efree(output);
TOMCRYPT_G(last_error) = err;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", error_to_string(err));
RETURN_FALSE;
#else
TOMCRYPT_G(last_error) = CRYPT_INVALID_ARG;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported cipher");
RETURN_FALSE;
#endif
}
static void php_tomcrypt_xcrypt_stream_rc4(PLTC_CRYPT_PARAM)
{
#ifdef LTC_RC4_STREAM
rc4_state state;
char *output;
int err;
output = emalloc(input_len + 1);
output[input_len] = '\0';
if ((err = rc4_stream_setup(&state, key, key_len)) != CRYPT_OK) {
goto error;
}
if ((err = rc4_stream_crypt(&state, input, input_len, output)) != CRYPT_OK) {
goto error;
}
if ((err = rc4_stream_done(&state)) != CRYPT_OK) {
goto error;
}
PLTC_RETURN_STRINGL(output, input_len, 0);
error:
efree(output);
TOMCRYPT_G(last_error) = err;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", error_to_string(err));
RETURN_FALSE;
#else
TOMCRYPT_G(last_error) = CRYPT_INVALID_ARG;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported cipher");
RETURN_FALSE;
#endif
}
static void php_tomcrypt_xcrypt_stream_sober128(PLTC_CRYPT_PARAM)
{
#if defined(LTC_SOBER128) && defined(LTC_SOBER128_STREAM)
sober128_state state;
char *output, *nonce;
int err;
pltc_size nonce_len;
GET_OPT_STRING(options, "nonce", nonce, nonce_len, NULL);
output = emalloc(input_len + 1);
output[input_len] = '\0';
if ((err = sober128_stream_setup(&state, key, key_len)) != CRYPT_OK) {
goto error;
}
if ((err = sober128_stream_setiv(&state, nonce, nonce_len)) != CRYPT_OK) {
goto error;
}
if ((err = sober128_stream_crypt(&state, input, input_len, output)) != CRYPT_OK) {
goto error;
}
if ((err = sober128_stream_done(&state)) != CRYPT_OK) {
goto error;
}
PLTC_RETURN_STRINGL(output, input_len, 0);
error:
efree(output);
TOMCRYPT_G(last_error) = err;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", error_to_string(err));
RETURN_FALSE;
#else
TOMCRYPT_G(last_error) = CRYPT_INVALID_ARG;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported cipher");
RETURN_FALSE;
#endif
}
static void php_tomcrypt_xcrypt_stream(PLTC_CRYPT_PARAM)
{
typedef void (*stream_xcrypt)(PLTC_CRYPT_PARAM);
stream_xcrypt funcs[] = {
php_tomcrypt_xcrypt_stream_chacha,
php_tomcrypt_xcrypt_stream_rc4,
php_tomcrypt_xcrypt_stream_sober128,
};
/* Map the 1st stream cipher (-2) to 0, the 2nd (-3) to 1, and so on. */
funcs[(-cipher)-2](PLTC_CRYPT_PARAM_PASSTHRU);
}