Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ENV B 16

RUN apt-get update && apt-get -y install bison flex libgmp-dev libmpc-dev

RUN wget https://crypto.stanford.edu/pbc/files/pbc-0.5.14.tar.gz
RUN wget --no-check-certificate https://crypto.stanford.edu/pbc/files/pbc-0.5.14.tar.gz
RUN tar -xvf pbc-0.5.14.tar.gz
RUN cd pbc-0.5.14 && ./configure && make && make install

Expand Down
83 changes: 83 additions & 0 deletions honeybadgerbft/crypto/threshenc/go/main/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import (
"context"
"fmt"

"github.com/apache/thrift/lib/go/thrift"
"github.com/vishalmohanty/encryption"
)

var defaultCtx = context.Background()

func handleClient(client *encryption.TPKEServiceClient) (err error) {
// client.Ping(defaultCtx)
// fmt.Println("ping()")

ver_key := encryption.NewVerificationKeyThrift()
ver_key.Key = []byte{71, 111}
pub_key := encryption.NewTPKEPublicKeyThrift()
pub_key.L = 10
pub_key.K = 5
pub_key.VK = ver_key
pub_key.VKs = []*encryption.VerificationKeyThrift{ver_key}

// val, err := client.Lagrange(
// defaultCtx,
// pub_key,
// []int32{0, 1},
// 0,
// )
val, err := client.Dealer(defaultCtx, 10, 5)

if err != nil {
// switch v := err.(type) {
// case *tutorial.InvalidOperation:
// fmt.Println("Invalid operation:", v)
// default:
fmt.Println("Error during operation:", err)
// }
} else {
fmt.Println("Success! ", val)
}
return err
}

func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool, cfg *thrift.TConfiguration) error {
var transport thrift.TTransport
if secure {
transport = thrift.NewTSSLSocketConf(addr, cfg)
} else {
transport = thrift.NewTSocketConf(addr, cfg)
}
transport, err := transportFactory.GetTransport(transport)
if err != nil {
return err
}
defer transport.Close()
if err := transport.Open(); err != nil {
return err
}
iprot := protocolFactory.GetProtocol(transport)
oprot := protocolFactory.GetProtocol(transport)
return handleClient(encryption.NewTPKEServiceClient(thrift.NewTStandardClient(iprot, oprot)))
}
7 changes: 7 additions & 0 deletions honeybadgerbft/crypto/threshenc/go/main/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module main

go 1.17

require github.com/apache/thrift v0.16.0
require "github.com/vishalmohanty/encryption" v0.0.0
replace "github.com/vishalmohanty/encryption" v0.0.0 => "../../thrift/gen-go/encryption"
15 changes: 15 additions & 0 deletions honeybadgerbft/crypto/threshenc/go/main/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/apache/thrift v0.16.0 h1:qEy6UW60iVOlUy+b9ZR0d5WzUWYGOo4HfopoyBaNmoY=
github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Binary file added honeybadgerbft/crypto/threshenc/go/main/main
Binary file not shown.
83 changes: 83 additions & 0 deletions honeybadgerbft/crypto/threshenc/go/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import (
"crypto/tls"
"flag"
"fmt"
"os"

"github.com/apache/thrift/lib/go/thrift"
)

func Usage() {
fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], ":\n")
flag.PrintDefaults()
fmt.Fprint(os.Stderr, "\n")
}

func main() {
flag.Usage = Usage
// server := flag.Bool("server", false, "Run server")
protocol := flag.String("P", "binary", "Specify the protocol (binary, compact, json, simplejson)")
framed := flag.Bool("framed", false, "Use framed transport")
buffered := flag.Bool("buffered", false, "Use buffered transport")
addr := flag.String("addr", "localhost:9090", "Address to listen to")
secure := flag.Bool("secure", false, "Use tls secure transport")

flag.Parse()

var protocolFactory thrift.TProtocolFactory
switch *protocol {
case "compact":
protocolFactory = thrift.NewTCompactProtocolFactoryConf(nil)
case "simplejson":
protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(nil)
case "json":
protocolFactory = thrift.NewTJSONProtocolFactory()
case "binary", "":
protocolFactory = thrift.NewTBinaryProtocolFactoryConf(nil)
default:
fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n")
Usage()
os.Exit(1)
}

var transportFactory thrift.TTransportFactory
cfg := &thrift.TConfiguration{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
if *buffered {
transportFactory = thrift.NewTBufferedTransportFactory(8192)
} else {
transportFactory = thrift.NewTTransportFactory()
}

if *framed {
transportFactory = thrift.NewTFramedTransportFactoryConf(transportFactory, cfg)
}

if err := runClient(transportFactory, protocolFactory, *addr, *secure, cfg); err != nil {
fmt.Println("error running client:", err)
}
}
116 changes: 116 additions & 0 deletions honeybadgerbft/crypto/threshenc/py/python_encryption_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import sys
sys.path.append('../../../crypto')

import importlib
from python_encryption_helper import PythonEncryptionHelper
from threshenc.tpke import dealer, encrypt, decrypt

encttypes = importlib.import_module("threshenc.thrift.gen-py.encryption.ttypes")


class PythonEncryptionHandler(PythonEncryptionHelper):
def __init__(self):
PythonEncryptionHelper.__init__(self)

def lagrange(self, tpke_pub_key_thrift_info, S, j):
tpke_pub_key = \
self.tpke_pub_key_from_thrift(
tpke_pub_key_thrift_info=tpke_pub_key_thrift_info,
)
return tpke_pub_key.lagrange(S, j)

def dealer(self, players, k):
public_key, private_keys = dealer(
players=players,
k=k
)
return encttypes.DealerThrift(
PubKey=self.tpke_pub_key_to_thrift(
tpke_pub_key=public_key
),
PrivKeys=[self.tpke_priv_key_to_thrift(priv_key) for priv_key in private_keys]
)

def encrypt(self, tpke_pub_key_thrift_info, m):
"""
Encrypt a 32 byte message.

:return: (U,V,W)
"""
tpke_pub_key = \
self.tpke_pub_key_from_thrift(
tpke_pub_key_thrift_info=tpke_pub_key_thrift_info,
)
(U, V, W) = tpke_pub_key.encrypt(m)
return self.encryptedMessageToThrift(
U=U,
V=V,
W=W
)

def combineShares(self, tpke_pub_key_thrift_info, em, shares):
"""
Combine shares
tpke_pub_key_thrift_info: Public Key
em: Encrypted message
shares: map<i32, binary> shares

:rtype: binary
"""
tpke_pub_key = \
self.tpke_pub_key_from_thrift(
tpke_pub_key_thrift_info=tpke_pub_key_thrift_info,
)
U, V, W = self.encryptedMessageFromThrift(enc_msg_thrift_info=em)
return self.verification_key_to_thrift(
tpke_pub_key.combine_shares(
U=U,
V=V,
W=W,
shares=self.shares_from_thrift(shares=shares)
)
)

def decryptShare(self, tpke_priv_key_thrift_info, em):
tpke_priv_key = \
self.tpke_priv_key_from_thrift(
tpke_priv_key_thrift_info=tpke_priv_key_thrift_info
)
U, V, W = self.encryptedMessageFromThrift(enc_msg_thrift_info=em)
return self.verification_key_to_thrift(
tpke_priv_key.decrypt_share(U, V, W)
)

def aesEncrypt(self, key, raw):
"""AES Encrypt
key: public key
raw: message to be encrypted

:return: encrypted binary bytes
"""
pub_key = self.aes_key_from_thrift(
aes_key_thrift_info=key
)
message = self.aes_key_from_thrift(
aes_key_thrift_info=raw
)
return self.aes_key_to_thrift(
aes_key=encrypt(key=pub_key, raw=message)
)

def aesDecrypt(self, key, enc):
"""AES Decrypt
key: private key
raw: message to be decrypted

:return: decrypted binary bytes
"""
priv_key = self.aes_key_from_thrift(
aes_key_thrift_info=key
)
encrypted_message = self.aes_key_from_thrift(
aes_key_thrift_info=enc
)
return self.aes_key_to_thrift(
aes_key=decrypt(key=priv_key, enc=encrypted_message)
)
Loading