0
|
1 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. |
|
2 Redistribution and use in source and binary forms, with or without modification, are |
|
3 permitted provided that the following conditions are met: |
|
4 |
|
5 1. Redistributions of source code must retain the above copyright notice, this list of |
|
6 conditions and the following disclaimer. |
|
7 |
|
8 2. Redistributions in binary form must reproduce the above copyright notice, this list |
|
9 of conditions and the following disclaimer in the documentation and/or other materials |
|
10 provided with the distribution. |
|
11 |
|
12 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
13 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
14 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR |
|
15 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
16 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
17 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
|
18 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
19 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
20 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
21 |
|
22 The views and conclusions contained in the software and documentation are those of the |
|
23 authors and should not be interpreted as representing official policies, either expressed |
|
24 or implied, of Ivo Smits.*/ |
|
25 |
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include "config.h" |
|
28 #endif |
|
29 #include "nacl_crypto_box.h" |
|
30 |
|
31 #include "php.h" |
|
32 #include "php_ini.h" |
|
33 #include "php_nacl.h" |
|
34 |
|
35 static function_entry nacl_functions[] = { |
|
36 PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305, NULL) |
|
37 PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_open, NULL) |
|
38 PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_getpublickey, NULL) |
|
39 PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_beforenm, NULL) |
|
40 PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_afternm, NULL) |
|
41 PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_open_afternm, NULL) |
|
42 {NULL, NULL, NULL} |
|
43 }; |
|
44 |
|
45 zend_module_entry nacl_module_entry = { |
|
46 #if ZEND_MODULE_API_NO >= 20010901 |
|
47 STANDARD_MODULE_HEADER, |
|
48 #endif |
|
49 PHP_NACL_EXTNAME, |
|
50 nacl_functions, |
|
51 PHP_MINIT(nacl), |
|
52 NULL, |
|
53 NULL, |
|
54 NULL, |
|
55 NULL, |
|
56 #if ZEND_MODULE_API_NO >= 20010901 |
|
57 PHP_NACL_VERSION, |
|
58 #endif |
|
59 STANDARD_MODULE_PROPERTIES |
|
60 }; |
|
61 |
|
62 #ifdef COMPILE_DL_NACL |
|
63 ZEND_GET_MODULE(nacl) |
|
64 #endif |
|
65 |
|
66 PHP_MINIT_FUNCTION(nacl) { |
|
67 REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_PUBLICKEYBYTES", crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT); |
|
68 REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_SECRETKEYBYTES", crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT); |
|
69 REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_BEFORENMBYTES", crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES, CONST_CS | CONST_PERSISTENT); |
|
70 REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_NONCEBYTES", crypto_box_curve25519xsalsa20poly1305_NONCEBYTES, CONST_CS | CONST_PERSISTENT); |
|
71 REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_ZEROBYTES", crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, CONST_CS | CONST_PERSISTENT); |
|
72 REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_BOXZEROBYTES", crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, CONST_CS | CONST_PERSISTENT); |
|
73 } |
|
74 |
|
75 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305) { //(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *) |
|
76 char *m, *n, *pk, *sk; |
|
77 int lm, ln, lpk, lsk; |
|
78 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss", &m, &lm, &pk, &lpk, &sk, &lsk, &n, &ln) == FAILURE) RETURN_NULL(); |
|
79 if (ln != crypto_box_curve25519xsalsa20poly1305_NONCEBYTES) RETURN_FALSE; |
|
80 if (lpk != crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) RETURN_FALSE; |
|
81 if (lsk != crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) RETURN_FALSE; |
|
82 int mlen = lm + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES; |
|
83 char* mb = ecalloc(mlen, 1); |
|
84 char* cb = ecalloc(mlen, 1); |
|
85 memcpy(mb + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, m, lm); |
|
86 int ret = crypto_box_curve25519xsalsa20poly1305(cb, mb, mlen, n, pk, sk); |
|
87 int clen = mlen - crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES; |
|
88 char* c = emalloc(clen); |
|
89 memcpy(c, cb + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, clen); |
|
90 efree(mb); |
|
91 efree(cb); |
|
92 if (ret == 0) RETURN_STRINGL(c, clen, 0); |
|
93 efree(c); |
|
94 RETURN_FALSE; |
|
95 } |
|
96 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_open) { //(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *) |
|
97 char *c, *n, *pk, *sk; |
|
98 int lc, ln, lpk, lsk; |
|
99 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss", &c, &lc, &pk, &lpk, &sk, &lsk, &n, &ln) == FAILURE) RETURN_NULL(); |
|
100 if (ln != crypto_box_curve25519xsalsa20poly1305_NONCEBYTES) RETURN_FALSE; |
|
101 if (lpk != crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) RETURN_FALSE; |
|
102 if (lsk != crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) RETURN_FALSE; |
|
103 int clen = lc + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES; |
|
104 char* mb = ecalloc(clen, 1); |
|
105 char* cb = ecalloc(clen, 1); |
|
106 memcpy(cb + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, c, lc); |
|
107 int ret = crypto_box_curve25519xsalsa20poly1305_open(mb, cb, clen, n, pk, sk); |
|
108 int mlen = clen - crypto_box_curve25519xsalsa20poly1305_ZEROBYTES; |
|
109 char* m = emalloc(mlen); |
|
110 memcpy(m, mb + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, mlen); |
|
111 efree(mb); |
|
112 efree(cb); |
|
113 if (ret == 0) RETURN_STRINGL(m, mlen, 0); |
|
114 efree(m); |
|
115 RETURN_FALSE; |
|
116 } |
|
117 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_beforenm) { //(unsigned char *,const unsigned char *,const unsigned char *) |
|
118 char *pk, *sk; |
|
119 int lpk, lsk; |
|
120 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &pk, &lpk, &sk, &lsk) == FAILURE) RETURN_NULL(); |
|
121 if (lpk != crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) RETURN_FALSE; |
|
122 if (lsk != crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) RETURN_FALSE; |
|
123 char* k = emalloc(crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES); |
|
124 crypto_box_curve25519xsalsa20poly1305_beforenm(k, pk, sk); |
|
125 RETURN_STRINGL(k, crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES, 0); |
|
126 } |
|
127 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_afternm) { //(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *) |
|
128 char *m, *n, *k; |
|
129 int lm, ln, lk; |
|
130 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &m, &lm, &k, &lk, &n, &ln) == FAILURE) RETURN_NULL(); |
|
131 if (ln != crypto_box_curve25519xsalsa20poly1305_NONCEBYTES) RETURN_FALSE; |
|
132 if (lk != crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES) RETURN_FALSE; |
|
133 int mlen = lm + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES; |
|
134 char* mb = ecalloc(mlen, 1); |
|
135 char* cb = ecalloc(mlen, 1); |
|
136 memcpy(mb + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, m, lm); |
|
137 int ret = crypto_box_curve25519xsalsa20poly1305_afternm(cb, mb, mlen, n, k); |
|
138 int clen = mlen - crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES; |
|
139 char* c = emalloc(clen); |
|
140 memcpy(c, cb + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, clen); |
|
141 efree(mb); |
|
142 efree(cb); |
|
143 if (ret == 0) RETURN_STRINGL(c, clen, 0); |
|
144 efree(c); |
|
145 RETURN_FALSE; |
|
146 } |
|
147 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_open_afternm) { //(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *) |
|
148 char *c, *n, *k; |
|
149 int lc, ln, lk; |
|
150 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &c, &lc, &k, &lk, &n, &ln) == FAILURE) RETURN_NULL(); |
|
151 if (ln != crypto_box_curve25519xsalsa20poly1305_NONCEBYTES) RETURN_FALSE; |
|
152 if (lk != crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES) RETURN_FALSE; |
|
153 int clen = lc + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES; |
|
154 char* mb = ecalloc(clen, 1); |
|
155 char* cb = ecalloc(clen, 1); |
|
156 memcpy(cb + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, c, lc); |
|
157 int ret = crypto_box_curve25519xsalsa20poly1305_open_afternm(mb, cb, clen, n, k); |
|
158 int mlen = clen - crypto_box_curve25519xsalsa20poly1305_ZEROBYTES; |
|
159 char* m = emalloc(mlen); |
|
160 memcpy(m, mb + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, mlen); |
|
161 efree(mb); |
|
162 efree(cb); |
|
163 if (ret == 0) RETURN_STRINGL(m, mlen, 0); |
|
164 efree(m); |
|
165 RETURN_FALSE; |
|
166 } |
|
167 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_getpublickey) { |
|
168 char base[32] = {9}; |
|
169 char *sk; |
|
170 int lsk; |
|
171 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sk, &lsk) == FAILURE) RETURN_NULL(); |
|
172 if (lsk != crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) RETURN_FALSE; |
|
173 char* pk = emalloc(crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES); |
|
174 crypto_scalarmult_curve25519(pk, sk, base); |
|
175 RETURN_STRINGL(pk, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, 0); |
|
176 } |