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 6577d24e authored by Cédric Moreau's avatar Cédric Moreau

Implemented Ed25519 using C/C++ binding

parent 8931be85
......@@ -3,4 +3,6 @@ node_modules/
*.html
npm-debug.log
bin/jpgp*.jar
.idea/
\ No newline at end of file
.idea/
naclb/build
naclb/node_modules
var nacl = require('tweetnacl');
var scrypt = require('scrypt');
var base58 = require('./base58');
var nacl = require('tweetnacl');
var scrypt = require('scrypt');
var base58 = require('./base58');
var naclBinding = require('../../naclb');
const crypto_sign_BYTES = 64;
var SEED_LENGTH = 32; // Length of the key
// TODO: change key parameters
var TEST_PARAMS = {
......@@ -24,7 +26,7 @@ module.exports = {
* Verify a signature against data & public key.
* Return true of false as callback argument.
*/
verify: function (msg, sig, pub, done) {
verifyOld: function (msg, sig, pub, done) {
var dMsg = nacl.util.decodeUTF8(msg);
var dSig = nacl.util.decodeBase64(sig);
var dPub = base58.decode(pub);
......@@ -33,6 +35,26 @@ module.exports = {
return verified;
},
/**
* Verify a signature against data & public key.
* Return true of false as callback argument.
*/
verify: function (rawMsg, rawSig, rawPub, done) {
var msg = nacl.util.decodeUTF8(rawMsg);
var sig = nacl.util.decodeBase64(rawSig);
var pub = base58.decode(rawPub);
var m = new Uint8Array(crypto_sign_BYTES + msg.length);
var sm = 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];
// Call to verification lib...
verified = naclBinding.verify(m, sm, pub);
if (typeof done == 'function') done(null, verified);
return verified;
},
/**
* Verify a signature against data & public key.
* Return a callback error if signature fails, nothing otherwise.
......
{
"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];
using namespace v8;
/**
* Verify 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));
}
// TODO: Sign
void Init(Handle<Object> exports) {
exports->Set(String::NewSymbol("verify"),
FunctionTemplate::New(Verify)->GetFunction());
}
NODE_MODULE(nacl, Init)
{
"name": "nacl_bin",
"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 msg = nacl.util.decodeUTF8(rawMsg);
var sig = nacl.util.decodeBase64(rawSig);
var pub = base58.decode(rawPub);
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];
// 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));
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
......@@ -41,7 +41,8 @@
"inquirer": "~0.5.0",
"event-stream": "3.1.5",
"tweetnacl": "0.11.2",
"scrypt": "3.0.1"
"scrypt": "3.0.1",
"naclb": "file:./naclb"
},
"devDependencies": {
"mocha": "",
......
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