From f03a4f754510dba0d7cfa4fc0397d96029c81696 Mon Sep 17 00:00:00 2001 From: Stephen Skeirik <7319434+sskeirik@users.noreply.github.com> Date: Mon, 3 Aug 2020 13:57:06 -0400 Subject: [PATCH] Add Michelson hooks (#104) * fix Makefile PHONY targets * add libcryptopp submodule and build recipe * make CI build libcryptopp instead of using package Packaged version does not provide needed symbols * add blake2b hash function with 256 bit digest size * add sha2 hash function with 512 bit digest size * Update libcryptopp submodule to ignore untracked files Co-authored-by: Everett Hildenbrandt * extract common utility functions * move problematic hash functions to separate file * remove inline attribute from allocString to fix compile error Co-authored-by: Everett Hildenbrandt --- .gitmodules | 4 ++++ Dockerfile | 1 - Jenkinsfile | 2 +- Makefile | 12 ++++++++++-- deps/cryptopp | 1 + plugin-c/crypto.cpp | 34 +++++++++++----------------------- plugin-c/hash_ext.cpp | 22 ++++++++++++++++++++++ plugin-c/plugin_util.cpp | 27 +++++++++++++++++++++++++++ plugin-c/plugin_util.h | 16 ++++++++++++++++ 9 files changed, 92 insertions(+), 27 deletions(-) create mode 160000 deps/cryptopp create mode 100644 plugin-c/hash_ext.cpp create mode 100644 plugin-c/plugin_util.cpp create mode 100644 plugin-c/plugin_util.h diff --git a/.gitmodules b/.gitmodules index 08ae63fe5..467be6fa9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,7 @@ path = deps/secp256k1 url = https://github.com/bitcoin-core/secp256k1 ignore = untracked +[submodule "deps/cryptopp"] + path = deps/cryptopp + url = https://github.com/weidai11/cryptopp + ignore = untracked diff --git a/Dockerfile b/Dockerfile index 58d116138..ba53a543a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,6 @@ RUN apt-get update \ cmake \ git \ libboost-test-dev \ - libcrypto++-dev \ libgflags-dev \ libgmp-dev \ libjemalloc-dev \ diff --git a/Jenkinsfile b/Jenkinsfile index 955228ea4..88b4b51c6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -15,7 +15,7 @@ pipeline { when { changeRequest() } steps { sh ''' - make -j16 CXX=clang++-8 libff + make -j16 CXX=clang++-8 libcryptopp libff make -j16 CXX=clang++-8 ''' } diff --git a/Makefile b/Makefile index 8d96d2e69..92b101bc2 100644 --- a/Makefile +++ b/Makefile @@ -17,8 +17,8 @@ endif INCLUDES := -I $(K_RELEASE)/include/kllvm -I $(PREFIX)/include -I dummy-version -I plugin -I plugin-c -I deps/cpp-httplib CPPFLAGS += --std=c++14 $(INCLUDES) -.PHONY: build libff -build: client-c/json.o client-c/main.o plugin-c/blake2.o plugin-c/blockchain.o plugin-c/crypto.o plugin-c/world.o +.PHONY: build libcryptopp libff libsecp256k1 +build: client-c/json.o client-c/main.o plugin-c/blake2.o plugin-c/blockchain.o plugin-c/crypto.o plugin-c/plugin_util.o plugin-c/world.o plugin-c/blockchain.o: plugin/proto/msg.pb.h @@ -30,6 +30,14 @@ clean: rm -rf */*.o */*/*.o plugin/proto/*.pb.* build deps/libff/build cd deps/secp256k1 && $(MAKE) clean +# libcryptopp + +libcryptopp: $(PREFIX)/lib/libcryptopp.a +$(PREFIX)/lib/libcryptopp.a: + cd deps/cryptopp \ + && $(MAKE) \ + && $(MAKE) install PREFIX=$(PREFIX) + # libff libff: $(PREFIX)/lib/libff.a diff --git a/deps/cryptopp b/deps/cryptopp new file mode 160000 index 000000000..9dcc26c58 --- /dev/null +++ b/deps/cryptopp @@ -0,0 +1 @@ +Subproject commit 9dcc26c58213abb8351fbb1b2a7a1d2c667366e4 diff --git a/plugin-c/crypto.cpp b/plugin-c/crypto.cpp index 83f218469..4c960f7d8 100644 --- a/plugin-c/crypto.cpp +++ b/plugin-c/crypto.cpp @@ -1,42 +1,30 @@ -#include #include #include #include #include #include -#include #include #include -#include "runtime/alloc.h" -#include "runtime/header.h" #include "blake2.h" +#include "plugin_util.h" using namespace CryptoPP; using namespace libff; extern "C" { -static inline string* allocString(size_t len) { - struct string *result = (struct string *)koreAllocToken(len + sizeof(string)); - set_len(result, len); - return result; -} -static string *hexEncode(unsigned char *digest, size_t len) { - uint64_t hexLen = len * 2; - char byte[3]; - struct string *result = allocString(hexLen); - for (size_t i = 0, j = 0; i < len; i++, j += 2) { - sprintf(byte, "%02x", digest[i]); - result->data[j] = byte[0]; - result->data[j+1] = byte[1]; - } - return result; +struct string *hook_KRYPTO_sha512raw(struct string *str) { + SHA512 h; + unsigned char digest[64]; + h.CalculateDigest(digest, (unsigned char *)str->data, len(str)); + return raw(digest, sizeof(digest)); } -static string *raw(unsigned char *digest, size_t len) { - struct string *result = allocString(len); - memcpy(result->data, digest, len); - return result; +struct string *hook_KRYPTO_sha512(struct string *str) { + SHA512 h; + unsigned char digest[64]; + h.CalculateDigest(digest, (unsigned char *)str->data, len(str)); + return hexEncode(digest, sizeof(digest)); } struct string *hook_KRYPTO_sha3raw(struct string *str) { diff --git a/plugin-c/hash_ext.cpp b/plugin-c/hash_ext.cpp new file mode 100644 index 000000000..4fe3f7581 --- /dev/null +++ b/plugin-c/hash_ext.cpp @@ -0,0 +1,22 @@ +#include +#include "plugin_util.h" + +using namespace CryptoPP; + +extern "C" { + +struct string *hook_KRYPTO_blake2b256raw(struct string *str) { + BLAKE2b h(false,32); + unsigned char digest[32]; + h.CalculateDigest(digest, (unsigned char *)str->data, len(str)); + return raw(digest, sizeof(digest)); +} + +struct string *hook_KRYPTO_blake2b256(struct string *str) { + BLAKE2b h(false,32); + unsigned char digest[32]; + h.CalculateDigest(digest, (unsigned char *)str->data, len(str)); + return hexEncode(digest, sizeof(digest)); +} + +} diff --git a/plugin-c/plugin_util.cpp b/plugin-c/plugin_util.cpp new file mode 100644 index 000000000..6025a284c --- /dev/null +++ b/plugin-c/plugin_util.cpp @@ -0,0 +1,27 @@ +#include "plugin_util.h" + +extern "C" { +string* allocString(size_t len) { + struct string *result = (struct string *)koreAllocToken(len + sizeof(string)); + set_len(result, len); + return result; +} + +string *hexEncode(unsigned char *digest, size_t len) { + uint64_t hexLen = len * 2; + char byte[3]; + struct string *result = allocString(hexLen); + for (size_t i = 0, j = 0; i < len; i++, j += 2) { + sprintf(byte, "%02x", digest[i]); + result->data[j] = byte[0]; + result->data[j+1] = byte[1]; + } + return result; +} + +string *raw(unsigned char *digest, size_t len) { + struct string *result = allocString(len); + memcpy(result->data, digest, len); + return result; +} +} diff --git a/plugin-c/plugin_util.h b/plugin-c/plugin_util.h new file mode 100644 index 000000000..ac3f1128d --- /dev/null +++ b/plugin-c/plugin_util.h @@ -0,0 +1,16 @@ +#ifndef PLUGIN_UTIL_H +#define PLUGIN_UTIL_H + +#include +#include +#include +#include "runtime/alloc.h" +#include "runtime/header.h" + +extern "C" { +string* allocString(size_t len); +string *hexEncode(unsigned char *digest, size_t len); +string *raw(unsigned char *digest, size_t len); +} + +#endif