view nacl.c @ 10:130a6a714966 default tip

Use libsodium instead of libnacl
author Ivo Smits <Ivo@UCIS.nl>
date Thu, 10 Jul 2014 16:34:04 +0000
parents 765303f4f2da
children
line wrap: on
line source

/* 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 "sodium/crypto_box_curve25519xsalsa20poly1305.h"
#include "sodium/crypto_scalarmult_curve25519.h"
#include "sodium/crypto_sign_edwards25519sha512batch.h"
#include "sodium/crypto_sign_ed25519.h"

#include "php.h"
#include "php_ini.h"
#include "php_nacl.h"

ZEND_BEGIN_ARG_INFO_EX(arginfo_nacl_crypto_box_curve25519xsalsa20poly1305_keypair, 0, 0, 1)
	ZEND_ARG_INFO(1, zsk)
	ZEND_ARG_INFO(0, rb)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_nacl_crypto_sign_edwards25519sha512batch_keypair, 0, 0, 1)
	ZEND_ARG_INFO(1, zsk)
	ZEND_ARG_INFO(0, rb)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_nacl_crypto_sign_ed25519_keypair, 0, 0, 1)
	ZEND_ARG_INFO(1, zsk)
	ZEND_ARG_INFO(0, rb)
ZEND_END_ARG_INFO()

static zend_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)
	PHP_FE(nacl_crypto_box_curve25519xsalsa20poly1305_keypair, arginfo_nacl_crypto_box_curve25519xsalsa20poly1305_keypair)
	PHP_FE(nacl_crypto_sign_edwards25519sha512batch, NULL)
	PHP_FE(nacl_crypto_sign_edwards25519sha512batch_open, NULL)
	PHP_FE(nacl_crypto_sign_edwards25519sha512batch_keypair, arginfo_nacl_crypto_sign_edwards25519sha512batch_keypair)
	PHP_FE(nacl_crypto_sign_ed25519, NULL)
	PHP_FE(nacl_crypto_sign_ed25519_open, NULL)
	PHP_FE(nacl_crypto_sign_ed25519_keypair, arginfo_nacl_crypto_sign_ed25519_keypair)
	{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);
	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);
	REGISTER_LONG_CONSTANT("NACL_CRYPTO_SIGN_ed25519_SECRETKEYBYTES", crypto_sign_ed25519_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT);
	REGISTER_LONG_CONSTANT("NACL_CRYPTO_SIGN_ed25519_PUBLICKEYBYTES", crypto_sign_ed25519_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT);
	REGISTER_LONG_CONSTANT("NACL_CRYPTO_SIGN_ed25519_SEEDBYTES", crypto_sign_ed25519_SEEDBYTES, CONST_CS | CONST_PERSISTENT);
	REGISTER_LONG_CONSTANT("NACL_CRYPTO_SIGN_ed25519_BYTES", crypto_sign_ed25519_BYTES, 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 *)
	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;
	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;
	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 *)
	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;
	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;
	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 *)
	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;
	unsigned char *k = emalloc(crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES);
	crypto_box_curve25519xsalsa20poly1305_beforenm(k, pk, sk);
	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 *)
	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;
	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);
	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 *)
	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;
	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);
	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) {
	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;
	unsigned char* pk = emalloc(crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES);
	crypto_scalarmult_curve25519(pk, sk, base);
	RETURN_STRINGL((char*)pk, crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES, 0);
}
PHP_FUNCTION(nacl_crypto_box_curve25519xsalsa20poly1305_keypair) {
	unsigned char *rb;
	int lrb;
	zval *zsk;
	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;
	if (rb) {
		if (lrb < crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES) RETURN_FALSE;
		unsigned char base[32] = {9};
		ret = crypto_scalarmult_curve25519(spk, ssk, base);
	} else {
		ret = crypto_box_curve25519xsalsa20poly1305_keypair(spk, ssk);
	}
	if (ret) RETURN_FALSE;
	zval_dtor(zsk);
	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 *);
	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;
	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);
	efree(smb);
	if (ret == 0) RETURN_STRINGL(sm, smlen, 0);
	efree(sm);
	RETURN_FALSE;
}
PHP_FUNCTION(nacl_crypto_sign_edwards25519sha512batch_open) { //(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
	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;
	if (lsm < crypto_sign_edwards25519sha512batch_BYTES) RETURN_FALSE;
	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);
	efree(mb);
	if (ret == 0) RETURN_STRINGL(m, mlen, 0);
	efree(m);
	RETURN_FALSE;
}
PHP_FUNCTION(nacl_crypto_sign_edwards25519sha512batch_keypair) { //(unsigned char *,unsigned char *);
	unsigned char *rb;
	int lrb;
	zval *zsk;
	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);
	if (ret) RETURN_FALSE;
	zval_dtor(zsk);
	ZVAL_STRINGL(zsk, (char*)ssk, crypto_sign_edwards25519sha512batch_SECRETKEYBYTES, 0);
	RETURN_STRINGL((char*)spk, crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES, 0);
}
PHP_FUNCTION(nacl_crypto_sign_ed25519) { //(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
	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_ed25519_SECRETKEYBYTES) RETURN_FALSE;
	long long unsigned int smlen = lm + crypto_sign_ed25519_BYTES;
	unsigned char* smb = emalloc(smlen);
	int ret = crypto_sign_ed25519(smb, &smlen, m, lm, sk);
	char* sm = emalloc(smlen);
	memcpy(sm, smb, smlen);
	efree(smb);
	if (ret == 0) RETURN_STRINGL(sm, smlen, 0);
	efree(sm);
	RETURN_FALSE;
}
PHP_FUNCTION(nacl_crypto_sign_ed25519_open) { //(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
	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_ed25519_PUBLICKEYBYTES) RETURN_FALSE;
	if (lsm < crypto_sign_ed25519_BYTES) RETURN_FALSE;
	long long unsigned int mlen = lsm;
	unsigned char* mb = emalloc(mlen);
	int ret = crypto_sign_ed25519_open(mb, &mlen, sm, lsm, pk);
	char* m = emalloc(mlen);
	memcpy(m, mb, mlen);
	efree(mb);
	if (ret == 0) RETURN_STRINGL(m, mlen, 0);
	efree(m);
	RETURN_FALSE;
}
PHP_FUNCTION(nacl_crypto_sign_ed25519_keypair) { //(unsigned char *,unsigned char *);
	unsigned char *rb;
	int lrb;
	zval *zsk;
	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_ed25519_PUBLICKEYBYTES);
	unsigned char *ssk = emalloc(crypto_sign_ed25519_SECRETKEYBYTES);
	int ret;
	if (rb) {
		if (lrb < crypto_sign_ed25519_SEEDBYTES) RETURN_FALSE;
		ret = crypto_sign_ed25519_seed_keypair(spk, ssk, rb);
	} else {
		ret = crypto_sign_ed25519_keypair(spk, ssk);
	}
	if (ret) RETURN_FALSE;
	zval_dtor(zsk);
	ZVAL_STRINGL(zsk, (char*)ssk, crypto_sign_ed25519_SECRETKEYBYTES, 0);
	RETURN_STRINGL((char*)spk, crypto_sign_ed25519_PUBLICKEYBYTES, 0);
}