changeset 7:a139bed53614

Fixed some compiler warnings, added support for unpatched nacl library
author Ivo Smits <Ivo@UCIS.nl>
date Tue, 12 Jul 2011 21:26:46 +0000
parents ded86f4d6275
children a1fc155ca80b
files nacl.c randombytes.c
diffstat 2 files changed, 74 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/nacl.c	Tue Jul 12 20:51:44 2011 +0000
+++ b/nacl.c	Tue Jul 12 21:26:46 2011 +0000
@@ -35,6 +35,10 @@
 #include "php_ini.h"
 #include "php_nacl.h"
 
+#if !(defined crypto_box_curve25519xsalsa20poly1305_KEYPAIRRANDOMBYTES || defined crypto_sign_edwards25519sha512batch_KEYPAIRRANDOMBYTES)
+#include "randombytes.c"
+#endif
+
 static function_entry nacl_functions[] = {
 	PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305, NULL)
 	PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_open, NULL)
@@ -77,23 +81,27 @@
 	REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_NONCEBYTES", crypto_box_curve25519xsalsa20poly1305_NONCEBYTES, CONST_CS | CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_ZEROBYTES", crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, CONST_CS | CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_BOXZEROBYTES", crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, CONST_CS | CONST_PERSISTENT);
-	REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_KEYPAIRRANDOMBYTES", crypto_box_curve25519xsalsa20poly1305_KEYPAIRRANDOMBYTES, CONST_CS | CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("NACL_CRYPTO_SIGN_edwards25519sha512batch_BYTES", crypto_sign_edwards25519sha512batch_BYTES, CONST_CS | CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("NACL_CRYPTO_SIGN_edwards25519sha512batch_PUBLICKEYBYTES", crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("NACL_CRYPTO_SIGN_edwards25519sha512batch_SECRETKEYBYTES", crypto_sign_edwards25519sha512batch_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT);
+#if defined crypto_box_curve25519xsalsa20poly1305_KEYPAIRRANDOMBYTES
+	REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_KEYPAIRRANDOMBYTES", crypto_box_curve25519xsalsa20poly1305_KEYPAIRRANDOMBYTES, CONST_CS | CONST_PERSISTENT);
+#endif
+#if defined crypto_sign_edwards25519sha512batch_KEYPAIRRANDOMBYTES
 	REGISTER_LONG_CONSTANT("NACL_CRYPTO_SIGN_edwards25519sha512batch_KEYPAIRRANDOMBYTES", crypto_sign_edwards25519sha512batch_KEYPAIRRANDOMBYTES, CONST_CS | CONST_PERSISTENT);
+#endif
 }
 
 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305) { //(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *)
-	char *m, *n, *pk, *sk;
+	unsigned char *m, *n, *pk, *sk;
 	int lm, ln, lpk, lsk;
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss", &m, &lm, &n, &ln, &pk, &lpk, &sk, &lsk) == FAILURE) RETURN_NULL();
 	if (ln != crypto_box_curve25519xsalsa20poly1305_NONCEBYTES) RETURN_FALSE;
 	if (lpk != crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) RETURN_FALSE;
 	if (lsk != crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) RETURN_FALSE;
 	int mlen = lm + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES;
-	char* mb = ecalloc(mlen, 1);
-	char* cb = ecalloc(mlen, 1);
+	unsigned char* mb = ecalloc(mlen, 1);
+	unsigned char* cb = ecalloc(mlen, 1);
 	memcpy(mb + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, m, lm);
 	int ret = crypto_box_curve25519xsalsa20poly1305(cb, mb, mlen, n, pk, sk);
 	int clen = mlen - crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;
@@ -106,15 +114,15 @@
 	RETURN_FALSE;
 }
 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_open) { //(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *)
-	char *c, *n, *pk, *sk;
+	unsigned char *c, *n, *pk, *sk;
 	int lc, ln, lpk, lsk;
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss", &c, &lc, &n, &ln, &pk, &lpk, &sk, &lsk) == FAILURE) RETURN_NULL();
 	if (ln != crypto_box_curve25519xsalsa20poly1305_NONCEBYTES) RETURN_FALSE;
 	if (lpk != crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) RETURN_FALSE;
 	if (lsk != crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) RETURN_FALSE;
 	int clen = lc + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;
-	char* mb = ecalloc(clen, 1);
-	char* cb = ecalloc(clen, 1);
+	unsigned char* mb = ecalloc(clen, 1);
+	unsigned char* cb = ecalloc(clen, 1);
 	memcpy(cb + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, c, lc);
 	int ret = crypto_box_curve25519xsalsa20poly1305_open(mb, cb, clen, n, pk, sk);
 	int mlen = clen - crypto_box_curve25519xsalsa20poly1305_ZEROBYTES;
@@ -127,28 +135,28 @@
 	RETURN_FALSE;
 }
 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_beforenm) { //(unsigned char *,const unsigned char *,const unsigned char *)
-	char *pk, *sk;
+	unsigned char *pk, *sk;
 	int lpk, lsk;
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &pk, &lpk, &sk, &lsk) == FAILURE) RETURN_NULL();
 	if (lpk != crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES) RETURN_FALSE;
 	if (lsk != crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) RETURN_FALSE;
-	char* k = emalloc(crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES);
+	unsigned char *k = emalloc(crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES);
 	crypto_box_curve25519xsalsa20poly1305_beforenm(k, pk, sk);
-	RETURN_STRINGL(k, crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES, 0);
+	RETURN_STRINGL((char*)k, crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES, 0);
 }
 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_afternm) { //(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *)
-	char *m, *n, *k;
+	unsigned char *m, *n, *k;
 	int lm, ln, lk;
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &m, &lm, &n, &ln, &k, &lk) == FAILURE) RETURN_NULL();
 	if (ln != crypto_box_curve25519xsalsa20poly1305_NONCEBYTES) RETURN_FALSE;
 	if (lk != crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES) RETURN_FALSE;
 	int mlen = lm + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES;
-	char* mb = ecalloc(mlen, 1);
-	char* cb = ecalloc(mlen, 1);
+	unsigned char *mb = ecalloc(mlen, 1);
+	unsigned char *cb = ecalloc(mlen, 1);
 	memcpy(mb + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, m, lm);
 	int ret = crypto_box_curve25519xsalsa20poly1305_afternm(cb, mb, mlen, n, k);
 	int clen = mlen - crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;
-	char* c = emalloc(clen);
+	char *c = emalloc(clen);
 	memcpy(c, cb + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, clen);
 	efree(mb);
 	efree(cb);
@@ -157,18 +165,18 @@
 	RETURN_FALSE;
 }
 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_open_afternm) { //(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *)
-	char *c, *n, *k;
+	unsigned char *c, *n, *k;
 	int lc, ln, lk;
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &c, &lc, &n, &ln, &k, &lk) == FAILURE) RETURN_NULL();
 	if (ln != crypto_box_curve25519xsalsa20poly1305_NONCEBYTES) RETURN_FALSE;
 	if (lk != crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES) RETURN_FALSE;
 	int clen = lc + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;
-	char* mb = ecalloc(clen, 1);
-	char* cb = ecalloc(clen, 1);
+	unsigned char *mb = ecalloc(clen, 1);
+	unsigned char *cb = ecalloc(clen, 1);
 	memcpy(cb + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, c, lc);
 	int ret = crypto_box_curve25519xsalsa20poly1305_open_afternm(mb, cb, clen, n, k);
 	int mlen = clen - crypto_box_curve25519xsalsa20poly1305_ZEROBYTES;
-	char* m = emalloc(mlen);
+	char *m = emalloc(mlen);
 	memcpy(m, mb + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, mlen);
 	efree(mb);
 	efree(cb);
@@ -177,37 +185,45 @@
 	RETURN_FALSE;
 }
 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_getpublickey) {
-	char base[32] = {9};
-	char *sk;
+	unsigned char base[32] = {9};
+	unsigned char *sk;
 	int lsk;
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sk, &lsk) == FAILURE) RETURN_NULL();
 	if (lsk != crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) RETURN_FALSE;
-	char* pk = emalloc(crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES);
+	unsigned char* pk = emalloc(crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES);
 	crypto_scalarmult_curve25519(pk, sk, base);
-	RETURN_STRINGL(pk, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, 0);
+	RETURN_STRINGL((char*)pk, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, 0);
 }
 PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_keypair) {
-	char *rb;
+	unsigned char *rb;
 	int lrb;
 	zval *zsk;
+#if defined crypto_box_curve25519xsalsa20poly1305_KEYPAIRRANDOMBYTES
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &zsk, &rb, &lrb) == FAILURE) RETURN_NULL();
 	if (!zsk) RETURN_FALSE;
 	if (lrb != crypto_box_curve25519xsalsa20poly1305_KEYPAIRRANDOMBYTES) RETURN_FALSE;
-	char *spk = emalloc(crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES);
-	char *ssk = emalloc(crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES);
+	unsigned char *spk = emalloc(crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES);
+	unsigned char *ssk = emalloc(crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES);
 	int ret = crypto_box_curve25519xsalsa20poly1305_keypair(spk, ssk, rb);
+#else
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &zsk, &rb, &lrb) == FAILURE) RETURN_NULL();
+	if (!zsk) RETURN_FALSE;
+	unsigned char *spk = emalloc(crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES);
+	unsigned char *ssk = emalloc(crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES);
+	int ret = crypto_box_curve25519xsalsa20poly1305_keypair(spk, ssk);
+#endif
 	if (ret) RETURN_FALSE;
 	zval_dtor(zsk);
-	ZVAL_STRINGL(zsk, ssk, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES, 0);
-	RETURN_STRINGL(spk, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, 0);
+	ZVAL_STRINGL(zsk, (char*)ssk, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES, 0);
+	RETURN_STRINGL((char*)spk, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, 0);
 }
 PHP_FUNCTION(nacl_crypto_sign_edwards25519sha512batch) { //(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
-	char *m, *sk;
+	unsigned char *m, *sk;
 	int lm, lsk;
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &m, &lm, &sk, &lsk) == FAILURE) RETURN_NULL();
 	if (lsk != crypto_sign_edwards25519sha512batch_SECRETKEYBYTES) RETURN_FALSE;
-	int smlen = lm + crypto_sign_edwards25519sha512batch_BYTES;
-	char* smb = emalloc(smlen);
+	long long unsigned int smlen = lm + crypto_sign_edwards25519sha512batch_BYTES;
+	unsigned char* smb = emalloc(smlen);
 	int ret = crypto_sign_edwards25519sha512batch(smb, &smlen, m, lm, sk);
 	char* sm = emalloc(smlen);
 	memcpy(sm, smb, smlen);
@@ -217,12 +233,12 @@
 	RETURN_FALSE;
 }
 PHP_FUNCTION(nacl_crypto_sign_edwards25519sha512batch_open) { //(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
-	char *sm, *pk;
+	unsigned char *sm, *pk;
 	int lsm, lpk;
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &sm, &lsm, &pk, &lpk) == FAILURE) RETURN_NULL();
 	if (lpk != crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES) RETURN_FALSE;
-	int mlen = lsm;
-	char* mb = emalloc(mlen);
+	long long unsigned int mlen = lsm;
+	unsigned char* mb = emalloc(mlen);
 	int ret = crypto_sign_edwards25519sha512batch_open(mb, &mlen, sm, lsm, pk);
 	char* m = emalloc(mlen);
 	memcpy(m, mb, mlen);
@@ -232,17 +248,25 @@
 	RETURN_FALSE;
 }
 PHP_FUNCTION(nacl_crypto_sign_edwards25519sha512batch_keypair) { //(unsigned char *,unsigned char *);
-	char *rb;
+	unsigned char *rb;
 	int lrb;
 	zval *zsk;
+#if defined crypto_sign_edwards25519sha512batch_KEYPAIRRANDOMBYTES
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &zsk, &rb, &lrb) == FAILURE) RETURN_NULL();
 	if (lrb != crypto_sign_edwards25519sha512batch_KEYPAIRRANDOMBYTES) RETURN_FALSE;
 	if (!zsk) RETURN_FALSE;
-	char *spk = emalloc(crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES);
-	char *ssk = emalloc(crypto_sign_edwards25519sha512batch_SECRETKEYBYTES);
+	unsigned char *spk = emalloc(crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES);
+	unsigned char *ssk = emalloc(crypto_sign_edwards25519sha512batch_SECRETKEYBYTES);
 	int ret = crypto_sign_edwards25519sha512batch_keypair(spk, ssk, rb);
+#else
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &zsk, &rb, &lrb) == FAILURE) RETURN_NULL();
+	if (!zsk) RETURN_FALSE;
+	unsigned char *spk = emalloc(crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES);
+	unsigned char *ssk = emalloc(crypto_sign_edwards25519sha512batch_SECRETKEYBYTES);
+	int ret = crypto_sign_edwards25519sha512batch_keypair(spk, ssk);
+#endif
 	if (ret) RETURN_FALSE;
 	zval_dtor(zsk);
-	ZVAL_STRINGL(zsk, ssk, crypto_sign_edwards25519sha512batch_SECRETKEYBYTES, 0);
-	RETURN_STRINGL(spk, crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES, 0);
+	ZVAL_STRINGL(zsk, (char*)ssk, crypto_sign_edwards25519sha512batch_SECRETKEYBYTES, 0);
+	RETURN_STRINGL((char*)spk, crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES, 0);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/randombytes.c	Tue Jul 12 21:26:46 2011 +0000
@@ -0,0 +1,12 @@
+#include <time.h>
+
+void randombytes(unsigned char *x,unsigned long long xlen) {
+	int fd = open("/dev/urandom",O_RDONLY);
+	if (fd != -1) {
+		fread(x, 1, xlen, fd);
+		fclose(fd);
+	} else {
+		srand(time(NULL));
+		for (int i = 0; i < xlen; i++) x[i] = rand() % 256;
+	}
+}