forked from lt/php-curve25519-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curve25519.c
140 lines (118 loc) · 3.19 KB
/
curve25519.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
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_curve25519.h"
#include "ext/standard/info.h"
#include "zend_exceptions.h"
#include "ext/spl/spl_exceptions.h"
const unsigned char basepoint[32] = {9};
PHP_FUNCTION(curve25519_public)
{
char *secret;
#if PHP_VERSION_ID >= 70000
size_t secret_len;
#else
int secret_len;
#endif
char public[32];
char *unclamped;
#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &secret, &secret_len) == FAILURE) {
RETURN_FALSE;
}
#else
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(secret, secret_len)
ZEND_PARSE_PARAMETERS_END();
#endif
if (secret_len != 32) {
zend_throw_exception(spl_ce_InvalidArgumentException, "Secret must be 32 bytes", 0 TSRMLS_CC);
}
// curve25519_donna clamps, and would modify $secret
// BUG: estrdup() is for NULL-terminated string, causing errors if 'secret' contain 0x00 (very rare but possible - I had such a case)
// unclamped = estrdup(secret);
unclamped = emalloc(32);
memmove(unclamped, secret, 32);
curve25519_donna(public, unclamped, basepoint);
efree(unclamped);
#if PHP_VERSION_ID >= 70000
RETURN_STRINGL(public, 32);
#else
RETURN_STRINGL(public, 32, 1);
#endif
}
PHP_FUNCTION(curve25519_shared)
{
char *secret;
char *public;
#if PHP_VERSION_ID >= 70000
size_t secret_len;
size_t public_len;
#else
int secret_len;
int public_len;
#endif
char shared[32];
char *unclamped;
#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &secret, &secret_len, &public, &public_len) == FAILURE) {
RETURN_FALSE;
}
#else
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STRING(secret, secret_len)
Z_PARAM_STRING(public, public_len)
ZEND_PARSE_PARAMETERS_END();
#endif
if (secret_len != 32) {
zend_throw_exception(spl_ce_InvalidArgumentException, "Secret must be 32 bytes", 0 TSRMLS_CC);
}
if (public_len != 32) {
zend_throw_exception(spl_ce_InvalidArgumentException, "Public must be 32 bytes", 0 TSRMLS_CC);
}
// BUG: estrdup() is for NULL-terminated string, causing errors if 'secret' contain 0x00 (very rare but possible - I had such a case)
// unclamped = estrdup(secret);
unclamped = emalloc(32);
memmove(unclamped, secret, 32);
curve25519_donna(shared, unclamped, public);
efree(unclamped);
#if PHP_VERSION_ID >= 70000
RETURN_STRINGL(shared, 32);
#else
RETURN_STRINGL(shared, 32, 1);
#endif
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_curve25519_public, 0, 0, 1)
ZEND_ARG_INFO(0, secret)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_curve25519_shared, 0, 0, 2)
ZEND_ARG_INFO(0, secret)
ZEND_ARG_INFO(0, public)
ZEND_END_ARG_INFO()
const zend_function_entry curve25519_functions[] = {
PHP_FE(curve25519_public, arginfo_curve25519_public)
PHP_FE(curve25519_shared, arginfo_curve25519_shared)
PHP_FE_END
};
PHP_MINFO_FUNCTION(curve25519)
{
php_info_print_table_start();
php_info_print_table_row(2, "curve25519 support", "enabled");
php_info_print_table_end();
}
zend_module_entry curve25519_module_entry = {
STANDARD_MODULE_HEADER,
"curve25519",
curve25519_functions,
NULL,
NULL,
NULL,
NULL,
PHP_MINFO(curve25519),
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_CURVE25519
ZEND_GET_MODULE(curve25519)
#endif