changeset 0:96c976fb3c3d

Initial commit
author Ivo Smits <Ivo@UCIS.nl>
date Sun, 27 Feb 2011 18:58:38 +0100
parents
children 84e0f7ead583
files config.m4 nacl.c nacl_crypto_box.h php_nacl.h tests/test.phpt
diffstat 5 files changed, 273 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.m4	Sun Feb 27 18:58:38 2011 +0100
@@ -0,0 +1,9 @@
+PHP_ARG_WITH(nacl, for nacl support,
+Make sure that the comment is aligned:
+[  --with-nacl             Include nacl support])
+
+if test "$PHP_NACL" != "no"; then
+  PHP_SUBST(NACL_SHARED_LIBADD)
+  PHP_ADD_LIBRARY(nacl, 1, NACL_SHARED_LIBADD)
+  PHP_NEW_EXTENSION(nacl, nacl.c, $ext_shared)
+fi
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nacl.c	Sun Feb 27 18:58:38 2011 +0100
@@ -0,0 +1,176 @@
+/* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved.
+   Redistribution and use in source and binary forms, with or without modification, are
+   permitted provided that the following conditions are met:
+
+   1. Redistributions of source code must retain the above copyright notice, this list of
+      conditions and the following disclaimer.
+
+   2. Redistributions in binary form must reproduce the above copyright notice, this list
+      of conditions and the following disclaimer in the documentation and/or other materials
+      provided with the distribution.
+
+   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
+   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+   The views and conclusions contained in the software and documentation are those of the
+   authors and should not be interpreted as representing official policies, either expressed
+   or implied, of Ivo Smits.*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "nacl_crypto_box.h"
+
+#include "php.h"
+#include "php_ini.h"
+#include "php_nacl.h"
+
+static function_entry nacl_functions[] = {
+    PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305, NULL)
+    PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_open, NULL)
+    PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_getpublickey, NULL)
+    PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_beforenm, NULL)
+    PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_afternm, NULL)
+    PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_open_afternm, NULL)
+    {NULL, NULL, NULL}
+};
+
+zend_module_entry nacl_module_entry = {
+#if ZEND_MODULE_API_NO >= 20010901
+    STANDARD_MODULE_HEADER,
+#endif
+    PHP_NACL_EXTNAME,
+    nacl_functions,
+    PHP_MINIT(nacl),
+    NULL,
+    NULL,
+    NULL,
+    NULL,
+#if ZEND_MODULE_API_NO >= 20010901
+    PHP_NACL_VERSION,
+#endif
+    STANDARD_MODULE_PROPERTIES
+};
+
+#ifdef COMPILE_DL_NACL
+ZEND_GET_MODULE(nacl)
+#endif
+
+PHP_MINIT_FUNCTION(nacl) {
+	REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_PUBLICKEYBYTES", crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_SECRETKEYBYTES", crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_BEFORENMBYTES", crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES, CONST_CS | CONST_PERSISTENT);
+	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);
+}
+
+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;
+	int lm, ln, lpk, lsk;
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss", &m, &lm, &pk, &lpk, &sk, &lsk, &n, &ln) == 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);
+	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;
+	char* c = emalloc(clen);
+	memcpy(c, cb + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, clen);
+	efree(mb);
+	efree(cb);
+	if (ret == 0) RETURN_STRINGL(c, clen, 0);
+	efree(c);
+	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;
+	int lc, ln, lpk, lsk;
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss", &c, &lc, &pk, &lpk, &sk, &lsk, &n, &ln) == 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);
+	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;
+	char* m = emalloc(mlen);
+	memcpy(m, mb + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, mlen);
+	efree(mb);
+	efree(cb);
+	if (ret == 0) RETURN_STRINGL(m, mlen, 0);
+	efree(m);
+	RETURN_FALSE;
+}
+PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_beforenm) { //(unsigned char *,const unsigned char *,const unsigned char *)
+	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);
+	crypto_box_curve25519xsalsa20poly1305_beforenm(k, pk, sk);
+	RETURN_STRINGL(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;
+	int lm, ln, lk;
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &m, &lm, &k, &lk, &n, &ln) == 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);
+	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);
+	memcpy(c, cb + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES, clen);
+	efree(mb);
+	efree(cb);
+	if (ret == 0) RETURN_STRINGL(c, clen, 0);
+	efree(c);
+	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;
+	int lc, ln, lk;
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &c, &lc, &k, &lk, &n, &ln) == 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);
+	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);
+	memcpy(m, mb + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES, mlen);
+	efree(mb);
+	efree(cb);
+	if (ret == 0) RETURN_STRINGL(m, mlen, 0);
+	efree(m);
+	RETURN_FALSE;
+}
+PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_getpublickey) {
+	char base[32] = {9};
+	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);
+	crypto_scalarmult_curve25519(pk, sk, base);
+	RETURN_STRINGL(pk, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, 0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nacl_crypto_box.h	Sun Feb 27 18:58:38 2011 +0100
@@ -0,0 +1,22 @@
+#ifndef nacl_crypto_box_H
+#define nacl_crypto_box_H
+
+#define crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES 32
+#define crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES 32
+#define crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES 32
+#define crypto_box_curve25519xsalsa20poly1305_NONCEBYTES 24
+#define crypto_box_curve25519xsalsa20poly1305_ZEROBYTES 32
+#define crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES 16
+extern int crypto_box_curve25519xsalsa20poly1305(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *);
+extern int crypto_box_curve25519xsalsa20poly1305_open(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *);
+extern int crypto_box_curve25519xsalsa20poly1305_keypair(unsigned char *,unsigned char *);
+extern int crypto_box_curve25519xsalsa20poly1305_beforenm(unsigned char *,const unsigned char *,const unsigned char *);
+extern int crypto_box_curve25519xsalsa20poly1305_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
+extern int crypto_box_curve25519xsalsa20poly1305_open_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
+
+#define crypto_scalarmult_curve25519_BYTES 32
+#define crypto_scalarmult_curve25519_SCALARBYTES 32
+extern int crypto_scalarmult_curve25519(unsigned char *,const unsigned char *,const unsigned char *);
+extern int crypto_scalarmult_curve25519_base(unsigned char *,const unsigned char *);
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/php_nacl.h	Sun Feb 27 18:58:38 2011 +0100
@@ -0,0 +1,22 @@
+#ifndef PHP_NACL_H
+#define PHP_NACL_H 1
+
+#ifdef ZTS
+#include "TSRM.h"
+#endif
+
+#define PHP_NACL_VERSION "1.0"
+#define PHP_NACL_EXTNAME "nacl"
+
+PHP_MINIT_FUNCTION(nacl);
+PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305);
+PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_open);
+PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_getpublickey);
+PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_beforenm);
+PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_afternm);
+PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_open_afternm);
+
+extern zend_module_entry nacl_module_entry;
+#define phpext_module_ptr &nacl_module_entry
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test.phpt	Sun Feb 27 18:58:38 2011 +0100
@@ -0,0 +1,44 @@
+--TEST--
+Test the nacl cryptography functions
+--FILE--
+<?php
+	echo NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_PUBLICKEYBYTES."\n";
+	echo NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_SECRETKEYBYTES."\n";
+	echo NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_BEFORENMBYTES."\n";
+	echo NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_NONCEBYTES."\n";
+	echo NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_ZEROBYTES."\n";
+	echo NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_BOXZEROBYTES."\n";
+	$m = 'HELLO WORLD';
+	echo strlen($m).' - '.bin2hex($m)."\n";
+	$n = str_repeat("\0", NACL_CRYPTO_BOX_curve25519xsalsa20poly1305_NONCEBYTES);
+	$sk = pack('H*' , 'b3d1187a0034cbf17c5deacfb457c57e83577df7fc7a3e14630bace4bb84f4fc');
+	$pk = nacl_crypto_box_curve25519xsalsa20poly1305_getpublickey($sk);
+	echo strlen($pk).' - '.bin2hex($pk)."\n";
+	$pk = pack('H*', 'd394ec9fce78084c1fbc208e5a17bef3bdc4f38cc1ed9e54b9e68ecb56be1418');
+	$c = nacl_crypto_box_curve25519xsalsa20poly1305($m, $pk, $sk, $n);
+	echo strlen($c).' - '.bin2hex($c)."\n";
+	$m = nacl_crypto_box_curve25519xsalsa20poly1305_open($c, $pk, $sk, $n);
+	echo strlen($m).' - '.bin2hex($m)."\n";
+	$k = nacl_crypto_box_curve25519xsalsa20poly1305_beforenm($pk, $sk);
+	echo strlen($k).' - '.bin2hex($k)."\n";
+	$c = nacl_crypto_box_curve25519xsalsa20poly1305_afternm($m, $k, $n);
+	echo strlen($c).' - '.bin2hex($c)."\n";
+	$m = nacl_crypto_box_curve25519xsalsa20poly1305_open_afternm($c, $k, $n);
+	echo strlen($m).' - '.bin2hex($m)."\n";
+	echo "ok\n";
+?>
+--EXPECT--
+32
+32
+32
+24
+32
+16
+11 - 48454c4c4f20574f524c44
+32 - 4867685134ac5017b584228fab5096983173b99aee10074706ce3c9f820b0a23
+27 - d7f25d6171ee1fac7242ed7c7546ddd56ee983ea24a6c7bf3bced8
+11 - 48454c4c4f20574f524c44
+32 - 76726914c6c1a5132931a2fc11179746c70982e948811ce93008b07f9a439001
+27 - d7f25d6171ee1fac7242ed7c7546ddd56ee983ea24a6c7bf3bced8
+11 - 48454c4c4f20574f524c44
+ok