Mise à jour de GitLab prévue ce samedi 8 mai 2021 à partir de 9h00 CET | GitLab upgrade planned this Saturday May 4th of 2021 from 9:00 AM CET

Commit 79b951e8 authored by Cédric Moreau's avatar Cédric Moreau

Implemented Ed25519 using C/C++ binding for signing too

parent 6577d24e
......@@ -18,6 +18,14 @@ var enc = nacl.util.encodeBase64,
module.exports = {
sign: function (msg, sec, done) {
var m = nacl.util.decodeUTF8(msg);
var signedMsg = naclBinding.sign(m, sec);
var sig = new Uint8Array(crypto_sign_BYTES);
for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];
done(null, nacl.util.encodeBase64(sig));
},
sign2: function (msg, sec, done) {
var sig = nacl.sign.detached(nacl.util.decodeUTF8(msg), sec);
done(null, nacl.util.encodeBase64(sig));
},
......
......@@ -10,10 +10,12 @@ typedef unsigned long long u64;
typedef long long i64;
typedef i64 gf[16];
const int crypto_sign_BYTES = 64;
using namespace v8;
/**
* Verify signature using Ed25519 scheme.
* Verifies a signature using Ed25519 scheme.
*
* arg0 (Uint8Array): clear message to be verified
* arg1 (Uint8Array): signature to check message against
......@@ -46,11 +48,48 @@ Handle<Value> Verify(const Arguments& args) {
return scope.Close(Boolean::New(false));
}
// TODO: Sign
/**
* 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)
......@@ -5,10 +5,12 @@ 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);
......@@ -22,12 +24,6 @@ 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];
// return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0);
var end = new Date();
// console.log(end.getTime() - start.getTime());
// console.log(dMsg);
// console.log(dSig);
// console.log(dPub);
console.log(addon.nacl(m, sm, pub));
// console.log(addon.verify(m, sm, pub));
// console.log(addon.sign(m, sec));
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment