Commit e5da5d27 authored by pengjun's avatar pengjun

add java sdk pre service

parent 8edee9ab
...@@ -2,8 +2,6 @@ package crypto ...@@ -2,8 +2,6 @@ package crypto
import ( import (
"fmt" "fmt"
"gitlab.33.cn/pengjun/reencrypt/sdk/types"
"math/big"
"testing" "testing"
) )
...@@ -25,13 +23,3 @@ func TestAES(t *testing.T) { ...@@ -25,13 +23,3 @@ func TestAES(t *testing.T) {
fmt.Println(string(cipher)) fmt.Println(string(cipher))
} }
func TestUtil(t *testing.T) {
var num = big.NewInt(123)
fmt.Println(num)
numstr := types.BigToString(num)
fmt.Println(numstr)
res := types.StringToBig(numstr)
fmt.Println(res)
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ package sdk ...@@ -2,6 +2,7 @@ package sdk
import ( import (
"crypto/rand" "crypto/rand"
"errors"
"fmt" "fmt"
secp256k1 "github.com/btcsuite/btcd/btcec" secp256k1 "github.com/btcsuite/btcd/btcec"
"gitlab.33.cn/pengjun/reencrypt/sdk/crypto" "gitlab.33.cn/pengjun/reencrypt/sdk/crypto"
...@@ -173,7 +174,7 @@ func GenerateKeyFragments(privOwner []byte, pubRecipient []byte, numSplit, thres ...@@ -173,7 +174,7 @@ func GenerateKeyFragments(privOwner []byte, pubRecipient []byte, numSplit, thres
return nil, err return nil, err
} }
dAliceHash.Write(pubRecipientKey.X.Bytes()) dAliceHash.Write(pubRecipientKey.X.Bytes())
dAliceHash.Write(dh_Alice_poit_x.Bytes()) dAliceHash.Write(dh_Alice_poit_x)
dAlice := dAliceHash.Sum(nil) dAlice := dAliceHash.Sum(nil)
dAliceBN := hashToModInt(dAlice) dAliceBN := hashToModInt(dAlice)
...@@ -184,7 +185,7 @@ func GenerateKeyFragments(privOwner []byte, pubRecipient []byte, numSplit, thres ...@@ -184,7 +185,7 @@ func GenerateKeyFragments(privOwner []byte, pubRecipient []byte, numSplit, thres
kFrags := make([]*KFrag, numSplit) kFrags := make([]*KFrag, numSplit)
if numSplit == 1 { if numSplit == 1 {
id := getRandomInt(baseN) id := getRandomInt(baseN)
kFrags[0] = &KFrag{Random: types.BigToString(id), Value: types.BigToString(f0), PrecurPub: precurPub} kFrags[0] = &KFrag{Random: id.String(), Value: f0.String(), PrecurPub: precurPub}
} else { } else {
coeffs := makeShamirPolyCoeff(threshold) coeffs := makeShamirPolyCoeff(threshold)
coeffs = append(coeffs, f0) coeffs = append(coeffs, f0)
...@@ -198,11 +199,11 @@ func GenerateKeyFragments(privOwner []byte, pubRecipient []byte, numSplit, thres ...@@ -198,11 +199,11 @@ func GenerateKeyFragments(privOwner []byte, pubRecipient []byte, numSplit, thres
return nil, err return nil, err
} }
dShareHash.Write(pubRecipientKey.X.Bytes()) dShareHash.Write(pubRecipientKey.X.Bytes())
dShareHash.Write(dh_Alice_poit_x.Bytes()) dShareHash.Write(dh_Alice_poit_x)
dShareHash.Write(id.Bytes()) dShareHash.Write(id.Bytes())
share := hashToModInt(dShareHash.Sum(nil)) share := hashToModInt(dShareHash.Sum(nil))
rk := hornerPolyEval(coeffs, share) rk := hornerPolyEval(coeffs, share)
kFrags[i] = &KFrag{Random: types.BigToString(id), Value: types.BigToString(rk), PrecurPub: precurPub} kFrags[i] = &KFrag{Random: id.String(), Value: rk.String(), PrecurPub: precurPub}
} }
} }
...@@ -224,7 +225,7 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([ ...@@ -224,7 +225,7 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
return nil, err return nil, err
} }
dBobHash.Write(privRecipientKey.X.Bytes()) dBobHash.Write(privRecipientKey.X.Bytes())
dBobHash.Write(dh_Bob_poit_x.Bytes()) dBobHash.Write(dh_Bob_poit_x)
dhBob := dBobHash.Sum(nil) dhBob := dBobHash.Sum(nil)
dhBobBN := hashToModInt(dhBob) dhBobBN := hashToModInt(dhBob)
...@@ -253,13 +254,13 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([ ...@@ -253,13 +254,13 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
return nil, err return nil, err
} }
xs.Write(privRecipientKey.X.Bytes()) xs.Write(privRecipientKey.X.Bytes())
xs.Write(dh_Bob_poit_x.Bytes()) xs.Write(dh_Bob_poit_x)
randomByte, err := types.FromHex(reKeyFrags[x].Random) random, ret := new(big.Int).SetString(reKeyFrags[x].Random, 10)
if err != nil { if !ret {
fmt.Errorf("get randomByte err", err) fmt.Errorf("AssembleReencryptFragment.get value int",)
return nil, err return nil, errors.New("get big int value from keyFragment failed")
} }
xs.Write(randomByte) xs.Write(random.Bytes())
ids[x] = hashToModInt(xs.Sum(nil)) ids[x] = hashToModInt(xs.Sum(nil))
} }
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"encoding/hex" "encoding/hex"
secp256k1 "github.com/btcsuite/btcd/btcec" secp256k1 "github.com/btcsuite/btcd/btcec"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"math/big"
) )
//FromHex hex -> []byte //FromHex hex -> []byte
...@@ -28,7 +27,7 @@ func ToHex(b []byte) string { ...@@ -28,7 +27,7 @@ func ToHex(b []byte) string {
if len(hex) == 0 { if len(hex) == 0 {
return "" return ""
} }
return "0x" + hex return hex
} }
//Encode 编码 //Encode 编码
...@@ -46,20 +45,8 @@ func Decode(data []byte, msg proto.Message) error { ...@@ -46,20 +45,8 @@ func Decode(data []byte, msg proto.Message) error {
} }
// ECDH Calculate a shared secret using elliptic curve Diffie-Hellman // ECDH Calculate a shared secret using elliptic curve Diffie-Hellman
func ECDH(priv *secp256k1.PrivateKey, pub *secp256k1.PublicKey) *big.Int { func ECDH(priv *secp256k1.PrivateKey, pub *secp256k1.PublicKey) []byte {
x, _ := secp256k1.S256().ScalarMult(pub.X, pub.Y, priv.D.Bytes()) ecKey := &secp256k1.PublicKey{}
return x ecKey.X, ecKey.Y = secp256k1.S256().ScalarMult(pub.X, pub.Y, priv.D.Bytes())
} return ecKey.SerializeCompressed()
func BigToString(num *big.Int) string {
return ToHex(num.Bytes())
}
func StringToBig(num string) *big.Int {
numbyte, err := FromHex(num)
if err != nil {
panic(err)
}
return new(big.Int).SetBytes(numbyte)
} }
package server package server
import ( import (
"errors"
secp256k1 "github.com/btcsuite/btcd/btcec" secp256k1 "github.com/btcsuite/btcd/btcec"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
"gitlab.33.cn/pengjun/reencrypt/common" "gitlab.33.cn/pengjun/reencrypt/common"
"gitlab.33.cn/pengjun/reencrypt/sdk/crypto" "gitlab.33.cn/pengjun/reencrypt/sdk/crypto"
"gitlab.33.cn/pengjun/reencrypt/sdk/types" "gitlab.33.cn/pengjun/reencrypt/sdk/types"
"math/big"
"time" "time"
) )
...@@ -55,8 +57,9 @@ func checkDhProof(key string, pub string, dh string) bool { ...@@ -55,8 +57,9 @@ func checkDhProof(key string, pub string, dh string) bool {
return false return false
} }
proof := types.ECDH(crypto.PrivateECDSAFromByte(keybyte[:]), pubkey).String() private := crypto.PrivateECDSAFromByte(keybyte[:])
if proof != dh { proof := types.ECDH(private, pubkey)
if types.ToHex(proof) != dh {
return false return false
} }
return true return true
...@@ -125,7 +128,11 @@ func (service *NodeService) Reencrypt(req *common.ReqReeencryptParam, result *in ...@@ -125,7 +128,11 @@ func (service *NodeService) Reencrypt(req *common.ReqReeencryptParam, result *in
return err return err
} }
rk := types.StringToBig(keyFragment.Value) rk, ret := new(big.Int).SetString(keyFragment.Value, 10)
if !ret {
rlog.Error("Reencrypt.get value int",)
return errors.New("get big int value from keyFragment failed")
}
var reKeyR, reKeyU secp256k1.PublicKey var reKeyR, reKeyU secp256k1.PublicKey
reKeyR.X, reKeyR.Y = secp256k1.S256().ScalarMult(pubkeyR.X, pubkeyR.Y, rk.Bytes()) reKeyR.X, reKeyR.Y = secp256k1.S256().ScalarMult(pubkeyR.X, pubkeyR.Y, rk.Bytes())
reKeyU.X, reKeyU.Y = secp256k1.S256().ScalarMult(pubkeyU.X, pubkeyU.Y, rk.Bytes()) reKeyU.X, reKeyU.Y = secp256k1.S256().ScalarMult(pubkeyU.X, pubkeyU.Y, rk.Bytes())
...@@ -137,7 +144,7 @@ func (service *NodeService) Reencrypt(req *common.ReqReeencryptParam, result *in ...@@ -137,7 +144,7 @@ func (service *NodeService) Reencrypt(req *common.ReqReeencryptParam, result *in
rep.PrecurPub = keyFragment.PrecurPub rep.PrecurPub = keyFragment.PrecurPub
*result = rep *result = rep
rlog.Info("Reencrypt start", "pubOwner", req.PubOwner, "pubRecipient", req.PubRecipient) rlog.Info("Reencrypt end", "pubOwner", req.PubOwner, "pubRecipient", req.PubRecipient)
return nil return nil
} }
...@@ -48,7 +48,7 @@ func handleReencrypt(t *testing.T, numSplit, threshold int, suffix string) { ...@@ -48,7 +48,7 @@ func handleReencrypt(t *testing.T, numSplit, threshold int, suffix string) {
Random: keyFragment.Random, Random: keyFragment.Random,
Value: keyFragment.Value, Value: keyFragment.Value,
Expire: 1000000, Expire: 1000000,
DhProof: dhproof.String(), DhProof: types.ToHex(dhproof),
PrecurPub: keyFragment.PrecurPub, PrecurPub: keyFragment.PrecurPub,
} }
...@@ -91,4 +91,3 @@ func TestReencryptSingle(t *testing.T) { ...@@ -91,4 +91,3 @@ func TestReencryptSingle(t *testing.T) {
func TestReencryptFrag(t *testing.T) { func TestReencryptFrag(t *testing.T) {
handleReencrypt(t, 3, 2, "frag") handleReencrypt(t, 3, 2, "frag")
} }
...@@ -111,7 +111,7 @@ func ownerEncrypt(cmd *cobra.Command, args []string) { ...@@ -111,7 +111,7 @@ func ownerEncrypt(cmd *cobra.Command, args []string) {
Random: keyFrags[0].Random, Random: keyFrags[0].Random,
Value: keyFrags[0].Value, Value: keyFrags[0].Value,
Expire: 1000000, Expire: 1000000,
DhProof: dhproof.String(), DhProof: types.ToHex(dhproof),
PrecurPub: keyFrags[0].PrecurPub, PrecurPub: keyFrags[0].PrecurPub,
} }
jclient.Call("CollectFragment", param, &result) jclient.Call("CollectFragment", param, &result)
......
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