Skip to content
Snippets Groups Projects
Commit 6e01cc3c authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Externalized naclb library

parent ac5be766
Branches
Tags
No related merge requests found
var nacl = require('tweetnacl');
var scrypt = require('scrypt');
var base58 = require('./base58');
var naclBinding = require('../../naclb');
var naclBinding = require('naclb');
const crypto_sign_BYTES = 64;
var SEED_LENGTH = 32; // Length of the key
......
{
"targets": [
{
"target_name": "nacl",
"sources": [ "nacl.cc", "tweetnacl.cpp", "randombytes.cpp" ]
}
]
}
// Exposes 2 methods: verify(msg, sig, pub), sign(msg, sec)
module.exports = require('bindings')('nacl');
#include <node.h>
#include <v8.h>
#include <stdio.h>
#include <stdlib.h>
#include "tweetnacl.h"
typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef long long i64;
typedef i64 gf[16];
const int crypto_sign_BYTES = 64;
using namespace v8;
/**
* Verifies a signature using Ed25519 scheme.
*
* arg0 (Uint8Array): clear message to be verified
* arg1 (Uint8Array): signature to check message against
* arg2 (Uint8Array): public key to use for verification
*/
Handle<Value> Verify(const Arguments& args) {
HandleScope scope;
// Reading clear message
Local<Object> msg = args[0]->ToObject();
u64 mlen = msg->GetIndexedPropertiesExternalArrayDataLength();
u8* m = static_cast<u8*>(msg->GetIndexedPropertiesExternalArrayData());
// Reading detached signature
Local<Object> sig = args[1]->ToObject();
u64 smlen = sig->GetIndexedPropertiesExternalArrayDataLength();
const u8* sm = static_cast<u8*>(sig->GetIndexedPropertiesExternalArrayData());
// Reading public key
Local<Object> pub = args[2]->ToObject();
const u8* pubk = static_cast<u8*>(pub->GetIndexedPropertiesExternalArrayData());
// Verifying authenticity
int res = crypto_sign_open(m,&mlen,sm,smlen,pubk);
if (res == 0)
// Good signature
return scope.Close(Boolean::New(true));
else
// Wrong signature or error
return scope.Close(Boolean::New(false));
}
/**
* Signs a message using Ed25519 scheme.
*
* arg0 (Uint8Array): clear message to be signed
* arg1 (Uint8Array): sec key to use for verification
*/
Handle<Value> Sign(const Arguments& args) {
HandleScope scope;
// Reading clear message
Local<Object> msg = args[0]->ToObject();
u64 mlen = msg->GetIndexedPropertiesExternalArrayDataLength();
const u8* m = static_cast<u8*>(msg->GetIndexedPropertiesExternalArrayData());
// Reading public key
Local<Object> sec = args[1]->ToObject();
const u8* seck = static_cast<u8*>(sec->GetIndexedPropertiesExternalArrayData());
u8* sm;
u64 smlen = 0;
sm = (u8*) malloc(mlen + crypto_sign_BYTES);
// Signing
crypto_sign(sm,&smlen,m,mlen,seck);
// Result
Local<Value> size = Integer::NewFromUnsigned(smlen);
Local<Object> array = Array::New(size->IntegerValue());
for (int i = 0; i < size->IntegerValue(); i++) {
array->Set(i, Integer::NewFromUnsigned(sm[i]));
}
return scope.Close(array);
}
void Init(Handle<Object> exports) {
exports->Set(String::NewSymbol("verify"),
FunctionTemplate::New(Verify)->GetFunction());
exports->Set(String::NewSymbol("sign"),
FunctionTemplate::New(Sign)->GetFunction());
}
NODE_MODULE(nacl, Init)
{
"name": "naclb",
"version": "0.0.1",
"description": "Node.js Addon for NaCl Binding",
"main": "index.js",
"private": true,
"scripts": {
"test": "node test.js"
},
"gypfile": true,
"dependencies": {
"bindings": "~1.2.1",
"nan": "^1.3.0"
}
}
#ifdef WIN32
#include "Windows.h"
#endif
#include <stdio.h>
#include <stdlib.h>
void randombytes(unsigned char * ptr,unsigned int length)
{
char failed = 0;
#ifdef WIN32
static HCRYPTPROV prov = 0;
if (prov == 0) {
if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
failed = 1;
}
}
if (!failed && !CryptGenRandom(prov, length, ptr)) {
failed = 1;
}
#else
FILE* fh = fopen("/dev/urandom", "rb");
if (fh != NULL) {
if (fread(ptr, length, 1, fh) == 0) {
failed = 1;
}
fclose(fh);
} else {
failed = 1;
}
#endif
/*
* yes, this is horrible error handling but we don't have better
* options from here and I don't want to start changing the design
* of the library
*/
if (failed) {
fprintf(stderr, "Generating random data failed. Please report "
"this to https://github.com/ultramancool\n");
exit(1);
}
}
void randombytes(unsigned char * ptr,unsigned int length);
var addon = require('bindings')('nacl');
var nacl = require('tweetnacl');
var base58 = require('../app/lib/base58');
var rawPub = "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd";
var rawMsg = "UID:CAT\nMETA:TS:1411321474\n";
var rawSig = "YvMQqaOAgLtnJzg5ZGhI17sZvXjGgzpSMxNz8ikttMspU5/45MQAqnOfuJnfbrzkkspGlUUjDnUPsOmHPcVyBQ==";
var rawSec = "51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP";
var msg = nacl.util.decodeUTF8(rawMsg);
var sig = nacl.util.decodeBase64(rawSig);
var pub = base58.decode(rawPub);
var sec = base58.decode(rawSec);
const crypto_sign_BYTES = 64;
// checkArrayTypes(msg, sig, publicKey);
// if (sig.length !== crypto_sign_BYTES)
// throw new Error('bad signature size');
// if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
// throw new Error('bad public key size');
var start = new Date();
var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
var m = new Uint8Array(crypto_sign_BYTES + msg.length);
var i;
for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i];
// console.log(addon.verify(m, sm, pub));
// console.log(addon.sign(m, sec));
This diff is collapsed.
typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef long long i64;
typedef i64 gf[16];
int crypto_verify_16(const u8 * x, const u8 * y);
int crypto_verify_32(const u8 * x, const u8 * y);
int crypto_core_salsa20(u8 * out, const u8 * in, const u8 * k, const u8 * c);
int crypto_core_hsalsa20(u8 * out, const u8 * in, const u8 * k, const u8 * c);
int crypto_stream_salsa20_xor(u8 * c, const u8 * m, u64 b, const u8 * n, const u8 * k);
int crypto_stream_salsa20(u8 * c, u64 d, const u8 * n, const u8 * k);
int crypto_stream(u8 * c, u64 d, const u8 * n, const u8 * k);
int crypto_stream_xor(u8 * c, const u8 * m, u64 d, const u8 * n, const u8 * k);
int crypto_onetimeauth(u8 * out, const u8 * m, u64 n, const u8 * k);
int crypto_onetimeauth_verify(const u8 * h, const u8 * m, u64 n, const u8 * k);
int crypto_secretbox(u8 * c, const u8 * m, u64 d, const u8 * n, const u8 * k);
int crypto_secretbox_open(u8 * m, const u8 * c, u64 d, const u8 * n, const u8 * k);
int crypto_scalarmult(u8 * q, const u8 * n, const u8 * p);
int crypto_scalarmult_base(u8 * q, const u8 * n);
int crypto_box_keypair(u8 * y, u8 * x);
int crypto_box_beforenm(u8 * k, const u8 * y, const u8 * x);
int crypto_box_afternm(u8 * c, const u8 * m, u64 d, const u8 * n, const u8 * k);
int crypto_box(u8 * c, const u8 * m, u64 d, const u8 * n, const u8 * y, const u8 * x);
int crypto_box_open(u8 * m, const u8 * c, u64 d, const u8 * n, const u8 * y, const u8 * x);
int crypto_hashblocks(u8 * x, const u8 * m, u64 n);
int crypto_hash(u8 * out, const u8 * m, u64 n);
int crypto_sign_keypair(u8 * pk, u8 * sk);
int crypto_sign(u8 * sm, u64 * smlen, const u8 * m, u64 n, const u8 * sk);
int crypto_sign_open(u8 * m, u64 * mlen, const u8 * sm, u64 n, const u8 * pk);
\ No newline at end of file
......@@ -44,7 +44,7 @@
"scrypt": "3.0.1",
"bindings": "~1.2.1",
"nan": "^1.3.0",
"naclb": "./naclb/"
"naclb": "0.0.1"
},
"devDependencies": {
"mocha": "",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment