Commit e5da5d27 authored by pengjun's avatar pengjun

add java sdk pre service

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