diff --git a/test/c/05-sign-keypair.js b/test/c/05-sign-keypair.js new file mode 100644 index 00000000..e71685c5 --- /dev/null +++ b/test/c/05-sign-keypair.js @@ -0,0 +1,52 @@ +var nacl = require('../../' + (process.env.NACL_SRC || 'nacl.min.js')); +var crypto = require('crypto'); +var spawn = require('child_process').spawn; +var execFile = require('child_process').execFile; +var path = require('path'); +var test = require('tape'); + +function csign(sk, msg, callback) { + var hexsk = (new Buffer(sk)).toString('hex'); + var p = spawn(path.resolve(__dirname, 'csign'), [hexsk]); + var result = []; + p.stdout.on('data', function(data) { + result.push(data); + }); + p.on('close', function(code) { + callback(Buffer.concat(result).toString('base64')); + }); + p.on('error', function(err) { + throw err; + }); + p.stdin.write(msg); + p.stdin.end(); +} + +function csignkeypair(callback) { + execFile(path.resolve(__dirname, 'csign-keypair'), [], function(err, stdout) { + if (err) throw err; + callback(stdout.toString('utf8')); + }); +} + +test('nacl.sign (C) with keypair from C', function(t) { + function check(num) { + csignkeypair(function(hexSecretKey) { + var secretKey = new Uint8Array(nacl.sign.secretKeyLength); + var b = new Buffer(hexSecretKey, 'hex'); + for (var i = 0; i < b.length; i++) secretKey[i] = b[i]; + var msg = nacl.randomBytes(num); + var signedMsg = nacl.util.encodeBase64(nacl.sign(msg, secretKey)); + csign(secretKey, new Buffer(msg), function(signedFromC) { + t.equal(signedMsg, signedFromC, 'signed messages should be equal'); + if (num >= 100) { + t.end(); + return; + } + check(num+1); + }); + }); + } + + check(0); +}); diff --git a/test/c/Makefile b/test/c/Makefile index 80a79d74..933f7c48 100644 --- a/test/c/Makefile +++ b/test/c/Makefile @@ -1,6 +1,6 @@ CFLAGS=-O3 -all: csecretbox cscalarmult cbox chash csign +all: csecretbox cscalarmult cbox chash csign csign-keypair csecretbox: csecretbox.o tweetnacl.o @@ -12,24 +12,27 @@ chash: chash.o tweetnacl.o csign: csign.o tweetnacl.o -test: test_secretbox +csign-keypair: csign-keypair.o tweetnacl.o -#test_scalarmult test_box test_hash test_sign clean +test: test_secretbox test_scalarmult test_box test_hash test_sign test_signkeypair clean test_secretbox: csecretbox - tap --timeout 1800 ./secretbox.js + node ./00-secretbox.js test_scalarmult: cscalarmult - node ./scalarmult.js + node ./01-scalarmult.js test_box: cbox - node ./box.js + node ./02-box.js test_hash: chash - node ./hash.js + node ./03-hash.js test_sign: csign - node ./sign.js + node ./04-sign.js + +test_signkeypair: csign csign-keypair + node ./05-sign-keypair.js clean: - rm *.o csecretbox cscalarmult cbox chash csign + rm *.o csecretbox cscalarmult cbox chash csign csign-keypair diff --git a/test/c/chash.c b/test/c/chash.c index 9dc04608..4849f8bb 100644 --- a/test/c/chash.c +++ b/test/c/chash.c @@ -1,5 +1,5 @@ /* - * This program sings a message using tweenacl.c. + * This program sings a message using tweetnacl.c. * Written by @dchest. Public domain. */ #include diff --git a/test/c/csign-keypair.c b/test/c/csign-keypair.c new file mode 100644 index 00000000..5d372b02 --- /dev/null +++ b/test/c/csign-keypair.c @@ -0,0 +1,53 @@ +/* + * This program generates a sign secret key using tweetnacl.c. + * Written by @dchest. Public domain. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tweetnacl.h" + +void randombytes(unsigned char *x,unsigned long long xlen) { + static int fd = -1; + int i; + + if (fd == -1) { + for (;;) { + fd = open("/dev/urandom",O_RDONLY); + if (fd != -1) break; + sleep(1); + } + } + + while (xlen > 0) { + if (xlen < 1048576) i = xlen; else i = 1048576; + + i = read(fd,x,i); + if (i < 1) { + sleep(1); + continue; + } + + x += i; + xlen -= i; + } +} + +int +main(int argc, const char **argv) +{ + unsigned char pk[crypto_sign_PUBLICKEYBYTES], + sk[crypto_sign_SECRETKEYBYTES]; + int i; + + crypto_sign_keypair(pk, sk); + for (i = 0; i < sizeof(sk); i++) + printf("%02x", sk[i]); + return 0; +} diff --git a/test/c/csign.c b/test/c/csign.c index ea5214a2..737c5cae 100644 --- a/test/c/csign.c +++ b/test/c/csign.c @@ -1,5 +1,5 @@ /* - * This program sings a message using tweenacl.c. + * This program signs a message using tweetnacl.c. * Written by @dchest. Public domain. */ #include