forked from mgp25/curve25519-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurve.c
183 lines (157 loc) · 4.66 KB
/
curve.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
const unsigned char basepoint[32] = {9};
void curve25519_clamp(unsigned char secret[32])
{
secret[0] &= 248;
secret[31] &= 127;
secret[31] |= 64;
}
PHP_FUNCTION(curve25519_sign){
const char *random;
int random_len;
const char *privatekey;
int private_len;
const char *message;
int message_len;
char signature[64];
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &random, &random_len,&privatekey,&private_len,&message,&message_len) == FAILURE) {
RETURN_FALSE;
}
if (private_len != 32) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Private must be 32 bytes");
RETURN_FALSE
}
if (random_len != 64) {
php_error_docref(NULL TSRMLS_CC,E_WARNING, "Random must be 64-byte string");
RETURN_FALSE
}
curve25519_sign((unsigned char *)signature, (unsigned char *)privatekey,
(unsigned char *)message, message_len, (unsigned char *)random);
RETURN_STRINGL((char*)signature, 64,1);
}
PHP_FUNCTION(curve25519_verify){
const char *publickey;
unsigned int public_len;
const char *message;
unsigned int message_len;
const char *signature;
unsigned int signature_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &publickey, &public_len,&message,&message_len,&signature,&signature_len) == FAILURE) {
RETURN_FALSE;
}
if (public_len != 32) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Public must be 32 bytes");
RETURN_FALSE
}
if (signature_len != 64) {
php_error_docref(NULL TSRMLS_CC,E_WARNING, "Signature must be 64-byte string");
RETURN_FALSE
}
int result = curve25519_verify((unsigned char *)signature, (unsigned char *)publickey,
(unsigned char *)message, message_len);
RETURN_LONG(result);
}
PHP_FUNCTION(curve25519_private){
unsigned char *random;
int random_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &random, &random_len) == FAILURE) {
RETURN_FALSE;
}
if (random_len != 32) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Random must be 32 bytes");
RETURN_FALSE
}
random[0] &= 248;
random[31] &= 127;
random[31] |= 64;
RETURN_STRINGL(random, 32, 1);
}
PHP_FUNCTION(curve25519_public)
{
const char *private;
int private_len;
char basepoint[32] = {9};
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &private, &private_len) == FAILURE) {
RETURN_FALSE;
}
if (private_len != 32) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Private must be 32 bytes");
RETURN_FALSE
}
char public[32];
curve25519_donna(public, private, basepoint);
RETURN_STRINGL((char*)public, 32, 1);
}
PHP_FUNCTION(curve25519_shared)
{
const char *private;
int private_len;
const char *public;
int public_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &private, &private_len, &public, &public_len) == FAILURE) {
RETURN_FALSE;
}
if (private_len != 32) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Private must be 32 bytes");
RETURN_FALSE;
}
if (public_len != 32) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Public must be 32 bytes");
RETURN_FALSE;
}
char shared_key[32];
curve25519_donna(shared_key, private, public);
RETURN_STRINGL(shared_key, 32, 1);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_curve25519_sign, 0, 0, 1)
ZEND_ARG_INFO(0, random)
ZEND_ARG_INFO(0, private)
ZEND_ARG_INFO(0, message)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_curve25519_verify, 0, 0, 1)
ZEND_ARG_INFO(0, public)
ZEND_ARG_INFO(0, message)
ZEND_ARG_INFO(0, signature)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_curve25519_private, 0, 0, 1)
ZEND_ARG_INFO(0, random)
ZEND_END_ARG_INFO()
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, 1)
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(curve25519_private, arginfo_curve25519_private)
PHP_FE(curve25519_sign, arginfo_curve25519_sign)
PHP_FE(curve25519_verify, arginfo_curve25519_verify)
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