mirror of
https://github.com/QuilibriumNetwork/ceremonyclient.git
synced 2026-02-28 13:57:26 +08:00
update tripleratchet bindings and remove bad unwrap handling
This commit is contained in:
parent
e0993a94ea
commit
7da27675df
241
channel/channel_test.go
Normal file
241
channel/channel_test.go
Normal file
@ -0,0 +1,241 @@
|
||||
package channel_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"source.quilibrium.com/quilibrium/monorepo/channel"
|
||||
generated "source.quilibrium.com/quilibrium/monorepo/channel/generated/channel"
|
||||
"source.quilibrium.com/quilibrium/monorepo/nekryptology/pkg/core/curves"
|
||||
)
|
||||
|
||||
type peer struct {
|
||||
privKey *curves.ScalarEd448
|
||||
pubKey *curves.PointEd448
|
||||
pubKeyB64 string
|
||||
identityKey *curves.ScalarEd448
|
||||
identityPubKey *curves.PointEd448
|
||||
signedPreKey *curves.ScalarEd448
|
||||
signedPrePubKey *curves.PointEd448
|
||||
}
|
||||
|
||||
func generatePeer() *peer {
|
||||
privKey := &curves.ScalarEd448{}
|
||||
privKey = privKey.Random(rand.Reader).(*curves.ScalarEd448)
|
||||
identityKey := &curves.ScalarEd448{}
|
||||
identityKey = identityKey.Random(rand.Reader).(*curves.ScalarEd448)
|
||||
signedPreKey := &curves.ScalarEd448{}
|
||||
signedPreKey = signedPreKey.Random(rand.Reader).(*curves.ScalarEd448)
|
||||
|
||||
pubkey := privKey.Point().Generator().Mul(privKey).(*curves.PointEd448)
|
||||
pubKeyB64 := base64.StdEncoding.EncodeToString(pubkey.ToAffineCompressed())
|
||||
return &peer{
|
||||
privKey: privKey,
|
||||
pubKey: pubkey,
|
||||
pubKeyB64: pubKeyB64,
|
||||
identityKey: identityKey,
|
||||
identityPubKey: identityKey.Point().Generator().Mul(identityKey).(*curves.PointEd448),
|
||||
signedPreKey: signedPreKey,
|
||||
signedPrePubKey: signedPreKey.Point().Generator().Mul(signedPreKey).(*curves.PointEd448),
|
||||
}
|
||||
}
|
||||
|
||||
func remapOutputs(maps map[string]map[string]string) map[string]map[string]string {
|
||||
out := map[string]map[string]string{}
|
||||
for k := range maps {
|
||||
out[k] = map[string]string{}
|
||||
}
|
||||
|
||||
for k := range maps {
|
||||
for ik, iv := range maps[k] {
|
||||
out[ik][k] = iv
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func TestChannel(t *testing.T) {
|
||||
peers := []*peer{}
|
||||
for i := 0; i < 4; i++ {
|
||||
peers = append(peers, generatePeer())
|
||||
}
|
||||
|
||||
sort.Slice(peers, func(i, j int) bool {
|
||||
return bytes.Compare(peers[i].pubKey.ToAffineCompressed(), peers[j].pubKey.ToAffineCompressed()) <= 0
|
||||
})
|
||||
|
||||
trs := map[string]*generated.TripleRatchetStateAndMetadata{}
|
||||
|
||||
peerids := [][]byte{}
|
||||
outs := map[string]map[string]string{}
|
||||
for i := 0; i < 4; i++ {
|
||||
outs[peers[i].pubKeyB64] = make(map[string]string)
|
||||
peerids = append(peerids,
|
||||
append(
|
||||
append(
|
||||
append([]byte{}, peers[i].pubKey.ToAffineCompressed()...),
|
||||
peers[i].identityPubKey.ToAffineCompressed()...,
|
||||
),
|
||||
peers[i].signedPrePubKey.ToAffineCompressed()...,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
for i := 0; i < 4; i++ {
|
||||
otherPeerIds := [][]byte{}
|
||||
for j := 0; j < 4; j++ {
|
||||
if i != j {
|
||||
otherPeerIds = append(otherPeerIds, peerids[j])
|
||||
}
|
||||
}
|
||||
|
||||
tr := channel.NewTripleRatchet(
|
||||
otherPeerIds,
|
||||
peers[i].privKey.Bytes(),
|
||||
peers[i].identityKey.Bytes(),
|
||||
peers[i].signedPreKey.Bytes(),
|
||||
2,
|
||||
true,
|
||||
)
|
||||
trs[peers[i].pubKeyB64] = &tr
|
||||
outs[peers[i].pubKeyB64] = trs[peers[i].pubKeyB64].Metadata
|
||||
}
|
||||
|
||||
outs = remapOutputs(outs)
|
||||
|
||||
for k := range trs {
|
||||
for ik := range trs[k].Metadata {
|
||||
delete(trs[k].Metadata, ik)
|
||||
}
|
||||
|
||||
for ik, iv := range outs[k] {
|
||||
trs[k].Metadata[ik] = iv
|
||||
}
|
||||
}
|
||||
|
||||
// round 1
|
||||
next := map[string]*generated.TripleRatchetStateAndMetadata{}
|
||||
outs = map[string]map[string]string{}
|
||||
for i := 0; i < 4; i++ {
|
||||
tr := channel.TripleRatchetInitRound1(
|
||||
*trs[peers[i].pubKeyB64],
|
||||
)
|
||||
next[peers[i].pubKeyB64] = &tr
|
||||
outs[peers[i].pubKeyB64] = next[peers[i].pubKeyB64].Metadata
|
||||
}
|
||||
|
||||
trs = next
|
||||
outs = remapOutputs(outs)
|
||||
|
||||
for k, _ := range trs {
|
||||
for ik := range trs[k].Metadata {
|
||||
delete(trs[k].Metadata, ik)
|
||||
}
|
||||
|
||||
for ik, iv := range outs[k] {
|
||||
trs[k].Metadata[ik] = iv
|
||||
}
|
||||
}
|
||||
|
||||
// round 2
|
||||
next = map[string]*generated.TripleRatchetStateAndMetadata{}
|
||||
outs = map[string]map[string]string{}
|
||||
for i := 0; i < 4; i++ {
|
||||
tr := channel.TripleRatchetInitRound2(
|
||||
*trs[peers[i].pubKeyB64],
|
||||
)
|
||||
next[peers[i].pubKeyB64] = &tr
|
||||
outs[peers[i].pubKeyB64] = next[peers[i].pubKeyB64].Metadata
|
||||
}
|
||||
|
||||
trs = next
|
||||
outs = remapOutputs(outs)
|
||||
|
||||
for k := range trs {
|
||||
for ik := range trs[k].Metadata {
|
||||
delete(trs[k].Metadata, ik)
|
||||
}
|
||||
|
||||
for ik, iv := range outs[k] {
|
||||
trs[k].Metadata[ik] = iv
|
||||
}
|
||||
}
|
||||
|
||||
// round 3
|
||||
next = map[string]*generated.TripleRatchetStateAndMetadata{}
|
||||
outs = map[string]map[string]string{}
|
||||
for i := 0; i < 4; i++ {
|
||||
tr := channel.TripleRatchetInitRound3(
|
||||
*trs[peers[i].pubKeyB64],
|
||||
)
|
||||
next[peers[i].pubKeyB64] = &tr
|
||||
outs[peers[i].pubKeyB64] = next[peers[i].pubKeyB64].Metadata
|
||||
}
|
||||
|
||||
trs = next
|
||||
outs = remapOutputs(outs)
|
||||
|
||||
for k := range trs {
|
||||
for ik := range trs[k].Metadata {
|
||||
delete(trs[k].Metadata, ik)
|
||||
}
|
||||
|
||||
for ik, iv := range outs[k] {
|
||||
trs[k].Metadata[ik] = iv
|
||||
}
|
||||
}
|
||||
|
||||
// round 4
|
||||
next = map[string]*generated.TripleRatchetStateAndMetadata{}
|
||||
outs = map[string]map[string]string{}
|
||||
for i := 0; i < 4; i++ {
|
||||
tr := channel.TripleRatchetInitRound4(
|
||||
*trs[peers[i].pubKeyB64],
|
||||
)
|
||||
next[peers[i].pubKeyB64] = &tr
|
||||
outs[peers[i].pubKeyB64] = next[peers[i].pubKeyB64].Metadata
|
||||
}
|
||||
|
||||
trs = next
|
||||
outs = remapOutputs(outs)
|
||||
|
||||
for k := range trs {
|
||||
for ik := range trs[k].Metadata {
|
||||
delete(trs[k].Metadata, ik)
|
||||
}
|
||||
|
||||
for ik, iv := range outs[k] {
|
||||
trs[k].Metadata[ik] = iv
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < 4; i++ {
|
||||
send := channel.TripleRatchetEncrypt(
|
||||
generated.TripleRatchetStateAndMessage{
|
||||
RatchetState: trs[peers[i].pubKeyB64].RatchetState,
|
||||
Message: []byte(fmt.Sprintf("hi-%d", i)),
|
||||
},
|
||||
)
|
||||
trs[peers[i].pubKeyB64].RatchetState = send.RatchetState
|
||||
for j := 0; j < 4; j++ {
|
||||
if i != j {
|
||||
msg := channel.TripleRatchetDecrypt(
|
||||
generated.TripleRatchetStateAndEnvelope{
|
||||
RatchetState: trs[peers[j].pubKeyB64].RatchetState,
|
||||
Envelope: send.Envelope,
|
||||
},
|
||||
)
|
||||
trs[peers[j].pubKeyB64].RatchetState = msg.RatchetState
|
||||
if !bytes.Equal(msg.Message, []byte(fmt.Sprintf("hi-%d", i))) {
|
||||
assert.FailNow(t, "mismatch messages")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -8,6 +8,19 @@ replace source.quilibrium.com/quilibrium/monorepo/nekryptology => ../nekryptolog
|
||||
require github.com/stretchr/testify v1.9.0
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
|
||||
github.com/btcsuite/btcd v0.21.0-beta.0.20201114000516-e9c7a5ac6401 // indirect
|
||||
github.com/bwesterb/go-ristretto v1.2.3 // indirect
|
||||
github.com/consensys/gnark-crypto v0.5.3 // indirect
|
||||
github.com/kr/pretty v0.2.1 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cloudflare/circl v1.3.3
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
golang.org/x/crypto v0.24.0 // indirect
|
||||
|
||||
@ -1,13 +1,84 @@
|
||||
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
|
||||
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
|
||||
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
|
||||
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
|
||||
github.com/btcsuite/btcd v0.21.0-beta.0.20201114000516-e9c7a5ac6401 h1:0tjUthKCaF8zwF9Qg7lfnep0xdo4n8WiFUfQPaMHX6g=
|
||||
github.com/btcsuite/btcd v0.21.0-beta.0.20201114000516-e9c7a5ac6401/go.mod h1:Sv4JPQ3/M+teHz9Bo5jBpkNcP0x6r7rdihlNL/7tTAs=
|
||||
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
|
||||
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
|
||||
github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts=
|
||||
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
|
||||
github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY=
|
||||
github.com/btcsuite/goleveldb v1.0.0/go.mod h1:QiK9vBlgftBg6rWQIj6wFzbPfRjiykIEhBH4obrXJ/I=
|
||||
github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
|
||||
github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
|
||||
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
|
||||
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
|
||||
github.com/bwesterb/go-ristretto v1.2.3 h1:1w53tCkGhCQ5djbat3+MH0BAQ5Kfgbt56UZQ/JMzngw=
|
||||
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
|
||||
github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs=
|
||||
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
|
||||
github.com/consensys/bavard v0.1.8-0.20210915155054-088da2f7f54a/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
|
||||
github.com/consensys/gnark-crypto v0.5.3 h1:4xLFGZR3NWEH2zy+YzvzHicpToQR8FXFbfLNvpGB+rE=
|
||||
github.com/consensys/gnark-crypto v0.5.3/go.mod h1:hOdPlWQV1gDLp7faZVeg8Y0iEPFaOUnCc4XeCCk96p0=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
|
||||
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
|
||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
|
||||
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
|
||||
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
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/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
|
||||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA=
|
||||
|
||||
@ -6,26 +6,33 @@ use protocols::{doubleratchet::{DoubleRatchetParticipant, P2PChannelEnvelope}, t
|
||||
|
||||
pub(crate) mod protocols;
|
||||
|
||||
uniffi::include_scaffolding!("lib");
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct DoubleRatchetStateAndEnvelope {
|
||||
pub ratchet_state: String,
|
||||
pub envelope: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct DoubleRatchetStateAndMessage {
|
||||
pub ratchet_state: String,
|
||||
pub message: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct TripleRatchetStateAndMetadata {
|
||||
pub ratchet_state: String,
|
||||
pub metadata: HashMap<String, String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct TripleRatchetStateAndEnvelope {
|
||||
pub ratchet_state: String,
|
||||
pub envelope: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct TripleRatchetStateAndMessage {
|
||||
pub ratchet_state: String,
|
||||
pub message: Vec<u8>,
|
||||
@ -156,35 +163,35 @@ pub fn double_ratchet_decrypt(ratchet_state_and_envelope: DoubleRatchetStateAndE
|
||||
pub fn new_triple_ratchet(peers: &Vec<Vec<u8>>, peer_key: &Vec<u8>, identity_key: &Vec<u8>, signed_pre_key: &Vec<u8>, threshold: u64, async_dkg_ratchet: bool) -> TripleRatchetStateAndMetadata {
|
||||
if peer_key.len() != 56 {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: "".to_string(),
|
||||
ratchet_state: "invalid peerkey".to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
|
||||
if identity_key.len() != 56 {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: "".to_string(),
|
||||
ratchet_state: "invalid idk".to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
|
||||
if signed_pre_key.len() != 56 {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: "".to_string(),
|
||||
ratchet_state: "invalid spk".to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
|
||||
if peers.len() < 3 {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: "".to_string(),
|
||||
ratchet_state: "invalid peer count".to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
|
||||
if threshold > peers.len() as u64 {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: "".to_string(),
|
||||
ratchet_state: "invalid threshold".to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -205,7 +212,7 @@ pub fn new_triple_ratchet(peers: &Vec<Vec<u8>>, peer_key: &Vec<u8>, identity_key
|
||||
for pk in peers.iter() {
|
||||
if pk.len() != 171 {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: "".to_string(),
|
||||
ratchet_state: "invalid peer key size".to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -228,7 +235,7 @@ pub fn new_triple_ratchet(peers: &Vec<Vec<u8>>, peer_key: &Vec<u8>, identity_key
|
||||
|
||||
if participant.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: "".to_string(),
|
||||
ratchet_state: participant.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -239,7 +246,7 @@ pub fn new_triple_ratchet(peers: &Vec<Vec<u8>>, peer_key: &Vec<u8>, identity_key
|
||||
|
||||
if participant_json.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: "".to_string(),
|
||||
ratchet_state: participant_json.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -261,7 +268,7 @@ fn metadata_to_json(ratchet_state: &String, metadata: HashMap<Vec<u8>, P2PChanne
|
||||
let env = v.to_json();
|
||||
if env.is_err() {
|
||||
return Err(TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state.to_string(),
|
||||
ratchet_state: env.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
});
|
||||
}
|
||||
@ -276,9 +283,15 @@ fn json_to_metadata(ratchet_state_and_metadata: TripleRatchetStateAndMetadata, r
|
||||
for (k,v) in ratchet_state_and_metadata.metadata {
|
||||
let env = P2PChannelEnvelope::from_json(v);
|
||||
let kb = BASE64_STANDARD.decode(k);
|
||||
if env.is_err() || kb.is_err() {
|
||||
if env.is_err() {
|
||||
return Err(TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state.clone(),
|
||||
ratchet_state: env.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
});
|
||||
}
|
||||
if kb.is_err() {
|
||||
return Err(TripleRatchetStateAndMetadata{
|
||||
ratchet_state: kb.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
});
|
||||
}
|
||||
@ -293,7 +306,7 @@ pub fn triple_ratchet_init_round_1(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let tr = TripleRatchetParticipant::from_json(&ratchet_state);
|
||||
if tr.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: tr.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -307,7 +320,7 @@ pub fn triple_ratchet_init_round_1(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let result = trp.initialize(&metadata);
|
||||
if result.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: result.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -321,7 +334,7 @@ pub fn triple_ratchet_init_round_1(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let json = trp.to_json();
|
||||
if json.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: json.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -337,7 +350,7 @@ pub fn triple_ratchet_init_round_2(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let tr = TripleRatchetParticipant::from_json(&ratchet_state);
|
||||
if tr.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: tr.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -353,7 +366,7 @@ pub fn triple_ratchet_init_round_2(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let r = trp.receive_poly_frag(&k, &v);
|
||||
if r.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: r.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -372,7 +385,7 @@ pub fn triple_ratchet_init_round_2(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let json = trp.to_json();
|
||||
if json.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: json.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -388,7 +401,7 @@ pub fn triple_ratchet_init_round_3(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let tr = TripleRatchetParticipant::from_json(&ratchet_state);
|
||||
if tr.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: tr.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -404,7 +417,7 @@ pub fn triple_ratchet_init_round_3(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let r = trp.receive_commitment(&k, &v);
|
||||
if r.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: r.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -423,7 +436,7 @@ pub fn triple_ratchet_init_round_3(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let json = trp.to_json();
|
||||
if json.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: json.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -439,7 +452,7 @@ pub fn triple_ratchet_init_round_4(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let tr = TripleRatchetParticipant::from_json(&ratchet_state);
|
||||
if tr.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: tr.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -455,7 +468,7 @@ pub fn triple_ratchet_init_round_4(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let r = trp.recombine(&k, &v);
|
||||
if r.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: r.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -469,7 +482,7 @@ pub fn triple_ratchet_init_round_4(ratchet_state_and_metadata: TripleRatchetStat
|
||||
let json = trp.to_json();
|
||||
if json.is_err() {
|
||||
return TripleRatchetStateAndMetadata{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: json.err().unwrap().to_string(),
|
||||
metadata: HashMap::new(),
|
||||
};
|
||||
}
|
||||
@ -485,7 +498,7 @@ pub fn triple_ratchet_encrypt(ratchet_state_and_message: TripleRatchetStateAndMe
|
||||
let tr = TripleRatchetParticipant::from_json(&ratchet_state);
|
||||
if tr.is_err() {
|
||||
return TripleRatchetStateAndEnvelope{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: tr.err().unwrap().to_string(),
|
||||
envelope: "".to_string(),
|
||||
};
|
||||
}
|
||||
@ -495,7 +508,7 @@ pub fn triple_ratchet_encrypt(ratchet_state_and_message: TripleRatchetStateAndMe
|
||||
|
||||
if result.is_err() {
|
||||
return TripleRatchetStateAndEnvelope{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: result.err().unwrap().to_string(),
|
||||
envelope: "".to_string(),
|
||||
};
|
||||
}
|
||||
@ -505,7 +518,7 @@ pub fn triple_ratchet_encrypt(ratchet_state_and_message: TripleRatchetStateAndMe
|
||||
|
||||
if envelope_json.is_err() {
|
||||
return TripleRatchetStateAndEnvelope{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: envelope_json.err().unwrap().to_string(),
|
||||
envelope: "".to_string(),
|
||||
};
|
||||
}
|
||||
@ -513,7 +526,7 @@ pub fn triple_ratchet_encrypt(ratchet_state_and_message: TripleRatchetStateAndMe
|
||||
let json = trp.to_json();
|
||||
if json.is_err() {
|
||||
return TripleRatchetStateAndEnvelope{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: json.err().unwrap().to_string(),
|
||||
envelope: "".to_string(),
|
||||
};
|
||||
}
|
||||
@ -529,7 +542,7 @@ pub fn triple_ratchet_decrypt(ratchet_state_and_envelope: TripleRatchetStateAndE
|
||||
let tr = TripleRatchetParticipant::from_json(&ratchet_state);
|
||||
if tr.is_err() {
|
||||
return TripleRatchetStateAndMessage{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: tr.err().unwrap().to_string(),
|
||||
message: vec![],
|
||||
};
|
||||
}
|
||||
@ -538,7 +551,7 @@ pub fn triple_ratchet_decrypt(ratchet_state_and_envelope: TripleRatchetStateAndE
|
||||
let env = P2PChannelEnvelope::from_json(ratchet_state_and_envelope.envelope);
|
||||
if env.is_err() {
|
||||
return TripleRatchetStateAndMessage{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: env.err().unwrap().to_string(),
|
||||
message: vec![],
|
||||
};
|
||||
}
|
||||
@ -547,7 +560,7 @@ pub fn triple_ratchet_decrypt(ratchet_state_and_envelope: TripleRatchetStateAndE
|
||||
|
||||
if result.is_err() {
|
||||
return TripleRatchetStateAndMessage{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: result.err().unwrap().to_string(),
|
||||
message: vec![],
|
||||
};
|
||||
}
|
||||
@ -557,7 +570,7 @@ pub fn triple_ratchet_decrypt(ratchet_state_and_envelope: TripleRatchetStateAndE
|
||||
let json = trp.to_json();
|
||||
if json.is_err() {
|
||||
return TripleRatchetStateAndMessage{
|
||||
ratchet_state: ratchet_state,
|
||||
ratchet_state: json.err().unwrap().to_string(),
|
||||
message: vec![],
|
||||
};
|
||||
}
|
||||
|
||||
@ -307,7 +307,6 @@ impl DoubleRatchetParticipant {
|
||||
}
|
||||
|
||||
let (header, should_ratchet) = self.decrypt_header(&envelope.message_header, &self.current_receiving_header_key)?;
|
||||
|
||||
let (receiving_ephemeral_key, previous_receiving_chain_length, current_receiving_chain_length) =
|
||||
self.decode_header(&header)?;
|
||||
|
||||
|
||||
@ -408,13 +408,23 @@ impl TripleRatchetParticipant {
|
||||
pub fn initialize(&mut self, init_messages: &HashMap<Vec<u8>, P2PChannelEnvelope>)
|
||||
-> Result<HashMap<Vec<u8>, P2PChannelEnvelope>, TripleRatchetError> {
|
||||
for (k, m) in init_messages {
|
||||
let msg = self.peer_channels.get_mut(k).unwrap().ratchet_decrypt(m).unwrap();
|
||||
if msg != b"init" {
|
||||
let channel = self.peer_channels.get_mut(k);
|
||||
if channel.is_none() {
|
||||
return Err(TripleRatchetError::InvalidData("Invalid peer channel".into()))
|
||||
}
|
||||
let msg = channel.unwrap().ratchet_decrypt(m);
|
||||
if msg.is_err() {
|
||||
return Err(TripleRatchetError::CryptoError(msg.err().unwrap().to_string()))
|
||||
}
|
||||
if msg.unwrap() != b"init" {
|
||||
return Err(TripleRatchetError::InvalidData("Invalid init message".into()));
|
||||
}
|
||||
}
|
||||
|
||||
self.dkg_ratchet.sample_polynomial(&mut OsRng);
|
||||
let maybeerr = self.dkg_ratchet.sample_polynomial(&mut OsRng);
|
||||
if maybeerr.is_err() {
|
||||
return Err(TripleRatchetError::InvalidData(maybeerr.err().unwrap().to_string().into()))
|
||||
}
|
||||
|
||||
let result = self.dkg_ratchet.get_poly_frags().unwrap();
|
||||
|
||||
@ -435,11 +445,18 @@ impl TripleRatchetParticipant {
|
||||
|
||||
pub fn receive_poly_frag(&mut self, peer_id: &[u8], frag: &P2PChannelEnvelope)
|
||||
-> Result<Option<HashMap<Vec<u8>, P2PChannelEnvelope>>, TripleRatchetError> {
|
||||
let b = self.peer_channels.get_mut(peer_id).unwrap().ratchet_decrypt(frag).unwrap();
|
||||
let channel = self.peer_channels.get_mut(peer_id);
|
||||
if channel.is_none() {
|
||||
return Err(TripleRatchetError::InvalidData("Invalid peer channel".into()))
|
||||
}
|
||||
let b = channel.unwrap().ratchet_decrypt(frag);
|
||||
if b.is_err() {
|
||||
return Err(TripleRatchetError::CryptoError(b.err().unwrap().to_string()))
|
||||
}
|
||||
|
||||
let result = self.dkg_ratchet.set_poly_frag_for_party(
|
||||
*self.peer_id_map.get(peer_id).unwrap(),
|
||||
&b,
|
||||
&b.unwrap(),
|
||||
).unwrap();
|
||||
|
||||
if result.is_some() {
|
||||
@ -457,11 +474,18 @@ impl TripleRatchetParticipant {
|
||||
|
||||
pub fn receive_commitment(&mut self, peer_id: &[u8], zkcommit: &P2PChannelEnvelope)
|
||||
-> Result<Option<HashMap<Vec<u8>, P2PChannelEnvelope>>, TripleRatchetError> {
|
||||
let b = self.peer_channels.get_mut(peer_id).unwrap().ratchet_decrypt(zkcommit).unwrap();
|
||||
let channel = self.peer_channels.get_mut(peer_id);
|
||||
if channel.is_none() {
|
||||
return Err(TripleRatchetError::InvalidData("Invalid peer channel".into()))
|
||||
}
|
||||
let b = channel.unwrap().ratchet_decrypt(zkcommit);
|
||||
if b.is_err() {
|
||||
return Err(TripleRatchetError::CryptoError(b.err().unwrap().to_string()))
|
||||
}
|
||||
|
||||
let result = self.dkg_ratchet.receive_commitments(
|
||||
*self.peer_id_map.get(peer_id).unwrap(),
|
||||
&b,
|
||||
&b.unwrap(),
|
||||
).unwrap();
|
||||
|
||||
if let Some(reveal) = result {
|
||||
@ -478,9 +502,16 @@ impl TripleRatchetParticipant {
|
||||
}
|
||||
|
||||
pub fn recombine(&mut self, peer_id: &[u8], reveal: &P2PChannelEnvelope) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let b = self.peer_channels.get_mut(peer_id).unwrap().ratchet_decrypt(reveal).unwrap();
|
||||
let channel = self.peer_channels.get_mut(peer_id);
|
||||
if channel.is_none() {
|
||||
return Err("Invalid peer channel".into())
|
||||
}
|
||||
let b = channel.unwrap().ratchet_decrypt(reveal);
|
||||
if b.is_err() {
|
||||
return Err(Box::new(TripleRatchetError::CryptoError(b.err().unwrap().to_string())))
|
||||
}
|
||||
|
||||
let rev: FeldmanReveal = serde_json::from_slice(&b).unwrap();
|
||||
let rev: FeldmanReveal = serde_json::from_slice(&b.unwrap()).unwrap();
|
||||
|
||||
let done = self.dkg_ratchet.recombine(
|
||||
*self.peer_id_map.get(peer_id).unwrap(),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user