From ec00b7429b668b3366d80a8fa85e9c22b4792d70 Mon Sep 17 00:00:00 2001 From: Lucas Jones Date: Mon, 7 Jul 2014 21:51:55 +0100 Subject: [PATCH] Initial FRESH support --- binding.gyp | 3 ++- fresh.c | 42 ++++++++++++++++++++++++++++++++++++++++++ fresh.h | 16 ++++++++++++++++ multihashing.cc | 24 ++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 fresh.c create mode 100644 fresh.h diff --git a/binding.gyp b/binding.gyp index 05a3c4a..2037c2b 100644 --- a/binding.gyp +++ b/binding.gyp @@ -23,6 +23,7 @@ "nist5.c", "sha1.c", "x15.c", + "fresh.c", "sha3/sph_hefty1.c", "sha3/sph_fugue.c", "sha3/aes_helper.c", @@ -48,7 +49,7 @@ "crypto/c_skein.c", "crypto/hash.c", "crypto/aesb.c", - "crypto/wild_keccak.cpp" + "crypto/wild_keccak.cpp", ], "include_dirs": [ "crypto", diff --git a/fresh.c b/fresh.c new file mode 100644 index 0000000..2c1d88a --- /dev/null +++ b/fresh.c @@ -0,0 +1,42 @@ +#include "fresh.h" +#include +#include +#include +#include + +#include "sha3/sph_shavite.h" +#include "sha3/sph_simd.h" +#include "sha3/sph_echo.h" + +void fresh_hash(const char* input, char* output, uint32_t len) +{ + sph_shavite512_context ctx_shavite1; + sph_simd512_context ctx_simd1; + sph_echo512_context ctx_echo1; + + //these uint512 in the c++ source of the client are backed by an array of uint32 + uint32_t hashA[16], hashB[16]; + + sph_shavite512_init (&ctx_shavite1); + sph_shavite512 (&ctx_shavite1, input, len); + sph_shavite512_close(&ctx_shavite1, hashA); + + sph_simd512_init (&ctx_simd1); + sph_simd512 (&ctx_simd1, hashA, 64); + sph_simd512_close(&ctx_simd1, hashB); + + sph_shavite512_init (&ctx_shavite1); + sph_shavite512 (&ctx_shavite1, hashB, 64); + sph_shavite512_close(&ctx_shavite1, hashA); + + sph_simd512_init (&ctx_simd1); + sph_simd512 (&ctx_simd1, hashA, 64); + sph_simd512_close(&ctx_simd1, hashB); + + sph_echo512_init (&ctx_echo1); + sph_echo512 (&ctx_echo1, hashB, 64); + sph_echo512_close(&ctx_echo1, hashA); + + memcpy(output, hashA, 32); + +} diff --git a/fresh.h b/fresh.h new file mode 100644 index 0000000..bf30032 --- /dev/null +++ b/fresh.h @@ -0,0 +1,16 @@ +#ifndef FRESH_H +#define FRESH_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +void fresh_hash(const char* input, char* output, uint32_t len); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/multihashing.cc b/multihashing.cc index afac362..8786495 100644 --- a/multihashing.cc +++ b/multihashing.cc @@ -22,6 +22,7 @@ extern "C" { #include "nist5.h" #include "sha1.h", #include "x15.h" + #include "fresh.h" } #include "boolberry.h" @@ -551,6 +552,28 @@ Handle x15(const Arguments& args) { return scope.Close(buff->handle_); } +Handle fresh(const Arguments& args) { + HandleScope scope; + + if (args.Length() < 1) + return except("You must provide one argument."); + + Local target = args[0]->ToObject(); + + if(!Buffer::HasInstance(target)) + return except("Argument should be a buffer object."); + + char * input = Buffer::Data(target); + char output[32]; + + uint32_t input_len = Buffer::Length(target); + + fresh_hash(input, output, input_len); + + Buffer* buff = Buffer::New(output, 32); + return scope.Close(buff->handle_); +} + void init(Handle exports) { exports->Set(String::NewSymbol("quark"), FunctionTemplate::New(quark)->GetFunction()); exports->Set(String::NewSymbol("x11"), FunctionTemplate::New(x11)->GetFunction()); @@ -573,6 +596,7 @@ void init(Handle exports) { exports->Set(String::NewSymbol("nist5"), FunctionTemplate::New(nist5)->GetFunction()); exports->Set(String::NewSymbol("sha1"), FunctionTemplate::New(sha1)->GetFunction()); exports->Set(String::NewSymbol("x15"), FunctionTemplate::New(x15)->GetFunction()); + exports->Set(String::NewSymbol("fresh"), FunctionTemplate::New(fresh)->GetFunction()); } NODE_MODULE(multihashing, init)