From 23e209935fc0fd558d52804bb2ee6fbd79d3deb8 Mon Sep 17 00:00:00 2001 From: Lennart Braun Date: Sat, 15 Sep 2018 23:31:07 +0300 Subject: [PATCH 1/4] fix compilation with glibc 2.28 In glibc 2.28 function fadd, fsub etc. are added to math.h [0]. Miracl defines functions with the same names. Hence, inclusion of both, math.h and Miracl, in the same translation unit results in an error. To address this issue, the use of Miracl is moved to ecc-pk-crypto.cpp such that inclusion of the header does not include Miracl anymore. Therefore, math.h can now be included together with ecc-pk-crypto.h in other source files (as long as fadd etc. from math.h are not used anywhere). This change was already committed in upstream ENCRYPTO_utils [1]. [0]: https://savannah.gnu.org/forum/forum.php?forum_id=9205 [1]: https://github.com/encryptogroup/ENCRYPTO_utils/commit/f0b707c48294e84d35a6ef31d1f31b21a90a0c1a --- src/util/cbitvector.h | 2 ++ src/util/crypto/ecc-pk-crypto.cpp | 31 ++++++++++++++++++++++++++++--- src/util/crypto/ecc-pk-crypto.h | 18 ++++++++++-------- src/util/ot/kk-ot-extension.cpp | 2 ++ 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/util/cbitvector.h b/src/util/cbitvector.h index e47b2ed..c482962 100644 --- a/src/util/cbitvector.h +++ b/src/util/cbitvector.h @@ -17,6 +17,8 @@ #define DEFAULT_BITSIZE 128 +typedef int BOOL; // as in Miracl + static const uint8_t REVERSE_NIBBLE_ORDER[16] = {0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE, 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF}; diff --git a/src/util/crypto/ecc-pk-crypto.cpp b/src/util/crypto/ecc-pk-crypto.cpp index 4ea7f12..c63b4eb 100644 --- a/src/util/crypto/ecc-pk-crypto.cpp +++ b/src/util/crypto/ecc-pk-crypto.cpp @@ -7,6 +7,10 @@ #include "ecc-pk-crypto.h" +#include "../../externals/miracl_lib/ecn.h" +#include "../../externals/miracl_lib/big.h" +#include "../../externals/miracl_lib/ec2.h" + char *ecx163 = (char *) "2fe13c0537bbc11acaa07d793de4e6d5e5c94eee8"; char *ecy163 = (char *) "289070fb05d38ff58321f2e800536d538ccdaa3d9"; @@ -203,6 +207,13 @@ void ecc_fe::sample_fe_from_bytes(uint8_t* buf, uint32_t bytelen) { } +void ecc_fe::print() { + cout << (*val) << endl; +} + +void ecc_fe::init() { + val = new EC2(); +} ecc_num::ecc_num(ecc_field* fld) { @@ -245,20 +256,34 @@ void ecc_num::export_to_bytes(uint8_t* buf, uint32_t field_size_bytes) { big_to_bytes ((int32_t) field_size_bytes, val->getbig(), (char*) buf, true); } +void ecc_num::print() { + cout << (*val) << endl; +} + // ecc_brickexp methods +struct ecc_brickexp::ecc_brickexp_impl { + ebrick2 br; +}; + ecc_brickexp::ecc_brickexp(fe* point, ecc_fparams* fparams) { Big x, y; fe2ec2(point)->getxy(x, y); - ebrick2_init(&br, x.getbig(), y.getbig(), fparams->BA->getbig(), fparams->BB->getbig(), - fparams->m, fparams->a, fparams->b, fparams->c, 8, fparams->secparam); + impl = std::make_unique(); + ebrick2_init(&impl->br, x.getbig(), y.getbig(), fparams->BA->getbig(), + fparams->BB->getbig(), fparams->m, fparams->a, fparams->b, + fparams->c, 8, fparams->secparam); +} + +ecc_brickexp::~ecc_brickexp() { + ebrick2_end(&impl->br); } void ecc_brickexp::pow(fe* result, num* e) { Big xtmp, ytmp; - mul2_brick(&br, num2Big(e)->getbig(), xtmp.getbig(), ytmp.getbig()); + mul2_brick(&impl->br, num2Big(e)->getbig(), xtmp.getbig(), ytmp.getbig()); *fe2ec2(result) = EC2(xtmp, ytmp); } diff --git a/src/util/crypto/ecc-pk-crypto.h b/src/util/crypto/ecc-pk-crypto.h index 8c72bce..26fec4d 100644 --- a/src/util/crypto/ecc-pk-crypto.h +++ b/src/util/crypto/ecc-pk-crypto.h @@ -9,10 +9,11 @@ #define ECC_PK_CRYPTO_H_ #include "pk-crypto.h" +#include -#include "../../externals/miracl_lib/ecn.h" -#include "../../externals/miracl_lib/big.h" -#include "../../externals/miracl_lib/ec2.h" +// forward declarations +class Big; +class EC2; #define fe2ec2(fieldele) (((ecc_fe*) (fieldele))->get_val()) @@ -94,7 +95,7 @@ public: void export_to_bytes(uint8_t* buf, uint32_t field_size_bytes); void import_from_bytes(uint8_t* buf, uint32_t field_size_bytes); void set_rnd(uint32_t bits); - void print() {cout << (*val) << endl;}; + void print(); private: Big* val; @@ -118,11 +119,11 @@ public: void import_from_bytes(uint8_t* buf); void sample_fe_from_bytes(uint8_t* buf, uint32_t bytelen); - void print() {cout << (*val) << endl;}; + void print(); private: - void init() {val = new EC2();}; + void init(); EC2* val; ecc_field* field; }; @@ -130,11 +131,12 @@ private: class ecc_brickexp : public brickexp { public: ecc_brickexp(fe* point, ecc_fparams* fparams); - ~ecc_brickexp() {ebrick2_end(&br);} + ~ecc_brickexp(); void pow(fe* res, num* e); private: - ebrick2 br; + struct ecc_brickexp_impl; // used to hide MIRACL's ebrick2 type in the implementation + std::unique_ptr impl; }; void point_to_byte(uint8_t* pBufIdx, uint32_t field_size_bytes, EC2* to_export); diff --git a/src/util/ot/kk-ot-extension.cpp b/src/util/ot/kk-ot-extension.cpp index 69f7ca5..9f8390a 100644 --- a/src/util/ot/kk-ot-extension.cpp +++ b/src/util/ot/kk-ot-extension.cpp @@ -1,5 +1,7 @@ #include "kk-ot-extension.h" +#define TRUE 1 // as in Miracl + /* * ---------------------------------- OT Extension Sender Part -------------------------------------- */ From c0611322c0598c4c303b6282c4d04e864412964e Mon Sep 17 00:00:00 2001 From: Lennart Braun Date: Sun, 16 Sep 2018 00:19:52 +0300 Subject: [PATCH 2/4] make destructors of base classes virtual See https://isocpp.org/wiki/faq/virtual-functions#virtual-dtors --- src/util/crypto/pk-crypto.h | 8 ++++---- src/util/ot/baseOT.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/crypto/pk-crypto.h b/src/util/crypto/pk-crypto.h index aa9ce39..999f1bf 100644 --- a/src/util/crypto/pk-crypto.h +++ b/src/util/crypto/pk-crypto.h @@ -18,7 +18,7 @@ class brickexp; class pk_crypto { public: pk_crypto(seclvl sp, uint8_t* seed){}; - ~pk_crypto(){}; + virtual ~pk_crypto(){}; virtual num* get_num() = 0; virtual num* get_rnd_num(uint32_t bitlen=0) = 0; virtual fe* get_fe() = 0; @@ -45,7 +45,7 @@ protected: class num { public: num(){}; - ~num(){}; + virtual ~num(){}; virtual void set(num* src) = 0; virtual void set_si(int32_t src) = 0; virtual void set_add(num* a, num* b) = 0; @@ -60,7 +60,7 @@ public: class fe { public: fe(){}; - ~fe(){}; + virtual ~fe(){}; virtual void set(fe* src) = 0; virtual void set_mul(fe* a, fe* b) = 0; virtual void set_pow(fe* b, num* e) = 0; @@ -78,7 +78,7 @@ protected: class brickexp { public: brickexp(){}; - ~brickexp(){}; + virtual ~brickexp(){}; virtual void pow(fe* result, num* e) = 0; }; diff --git a/src/util/ot/baseOT.h b/src/util/ot/baseOT.h index 64262a1..0fce20e 100644 --- a/src/util/ot/baseOT.h +++ b/src/util/ot/baseOT.h @@ -23,7 +23,7 @@ class BaseOT { public: BaseOT(crypto* crypt, field_type ftype){m_cCrypto = crypt; m_cPKCrypto = crypt->gen_field(ftype); }; - ~BaseOT(){delete m_cPKCrypto; }; + virtual ~BaseOT(){delete m_cPKCrypto; }; virtual void Sender(uint32_t nSndVals, uint32_t nOTs, CSocket* sock, uint8_t* ret) = 0; virtual void Receiver(uint32_t nSndVals, uint32_t uint32_t, CBitVector& choices, CSocket* sock, uint8_t* ret) = 0; From aad023de7b14ba4f3116459dd5e64230761bc982 Mon Sep 17 00:00:00 2001 From: Lennart Braun Date: Sun, 16 Sep 2018 00:24:00 +0300 Subject: [PATCH 3/4] fix malloc/free mismatch Buffer was allocated with malloc and free with delete[]. --- src/util/ot/naor-pinkas.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/ot/naor-pinkas.cpp b/src/util/ot/naor-pinkas.cpp index 380811f..1bdfb51 100644 --- a/src/util/ot/naor-pinkas.cpp +++ b/src/util/ot/naor-pinkas.cpp @@ -81,7 +81,7 @@ void NaorPinkas::Receiver(uint32_t nSndVals, uint32_t nOTs, CBitVector& choices, delete bc;//BrickDelete(&bc); delete bg;//BrickDelete(&bg); - delete [] pBuf; + free(pBuf); free(PK_sigma); free(pDec); free(pC); From 36d88beb4ed76a9b991f5110a077d895ddb29261 Mon Sep 17 00:00:00 2001 From: Lennart Braun Date: Sun, 16 Sep 2018 00:32:50 +0300 Subject: [PATCH 4/4] add missing return statements Functions are supposed to return a void* but did not return anything. Return NULL for now. --- src/hashing/cuckoo.cpp | 1 + src/hashing/simple_hashing.cpp | 1 + src/ot-based/ot-psi.cpp | 2 ++ 3 files changed, 4 insertions(+) diff --git a/src/hashing/cuckoo.cpp b/src/hashing/cuckoo.cpp index dc5505d..7b4e675 100644 --- a/src/hashing/cuckoo.cpp +++ b/src/hashing/cuckoo.cpp @@ -155,6 +155,7 @@ void *gen_cuckoo_entries(void *ctx_void) { for(i = ctx->startpos; i < ctx->endpos; i++, eleptr+=inbytelen) { gen_cuckoo_entry(eleptr, ctx->cuckoo_entries + i, hs, i); } + return NULL; } diff --git a/src/hashing/simple_hashing.cpp b/src/hashing/simple_hashing.cpp index 55ce6c6..8bfa1ce 100644 --- a/src/hashing/simple_hashing.cpp +++ b/src/hashing/simple_hashing.cpp @@ -112,6 +112,7 @@ void *gen_entries(void *ctx_tmp) { } free(tmpbuf); free(address); + return NULL; } inline void insert_element(sht_ctx* table, uint8_t* element, uint32_t* address, uint8_t* tmpbuf, hs_t* hs) { diff --git a/src/ot-based/ot-psi.cpp b/src/ot-based/ot-psi.cpp index d884d8d..112951e 100644 --- a/src/ot-based/ot-psi.cpp +++ b/src/ot-based/ot-psi.cpp @@ -587,6 +587,7 @@ void *otpsi_query_hash_table(void* ctx_tmp) {//GHashTable *map, uint8_t* element free(matches); //return size_intersect; + return NULL; } //TODO if this works correctly, combine with other find intersection methods and outsource to hashing_util.h @@ -717,6 +718,7 @@ void print_bin_content(uint8_t* hash_table, uint32_t nbins, uint32_t elebytelen, void *receive_masks(void *ctx_tmp) { mask_rcv_ctx* ctx = (mask_rcv_ctx*) ctx_tmp; ctx->sock->Receive(ctx->rcv_buf, ctx->maskbytelen * ctx->nmasks); + return NULL; } uint32_t get_stash_size(uint32_t neles) {