From 8721b8f455b4c5700f1b9f372a87a9cc6a223ccf Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Sat, 28 Apr 2018 10:52:59 -0700 Subject: [PATCH] Replacing code with proper Sort, fixing test --- model/group.go | 3 +- model/group_test.go | 11 +++---- model/message.go | 61 ++++++++++++++++---------------------- model/message_test.go | 2 +- model/profile_test | 2 +- model/threat_model_test.go | 7 +---- 6 files changed, 36 insertions(+), 50 deletions(-) diff --git a/model/group.go b/model/group.go index 9a9969c..4dd6b3c 100644 --- a/model/group.go +++ b/model/group.go @@ -8,7 +8,6 @@ import ( "github.com/s-rah/go-ricochet/utils" "golang.org/x/crypto/nacl/secretbox" "io" - "log" "time" ) @@ -55,7 +54,7 @@ func (g *Group) Invite() []byte { ServerHost: g.GroupServer, SignedGroupId: g.SignedGroupID[:], } - log.Printf("INVITEBEFORE %v", gci) + cp := &protocol.CwtchPeerPacket{ GroupChatInvite: gci, } diff --git a/model/group_test.go b/model/group_test.go index b73e9c9..be38fd6 100644 --- a/model/group_test.go +++ b/model/group_test.go @@ -10,11 +10,12 @@ import ( func TestGroup(t *testing.T) { g := NewGroup("server.onion") dgm := &protocol.DecryptedGroupMessage{ - Onion: proto.String("onion"), - Text: proto.String("Hello World!"), - Timestamp: proto.Int32(int32(time.Now().Unix())), - SignedGroupId: []byte{}, - Signature: []byte{}, + Onion: proto.String("onion"), + Text: proto.String("Hello World!"), + Timestamp: proto.Int32(int32(time.Now().Unix())), + SignedGroupId: []byte{}, + Signature: []byte{}, + PreviousMessageSig: []byte{}, } encMessage := g.EncryptMessage(dgm) ok, message := g.DecryptMessage(encMessage) diff --git a/model/message.go b/model/message.go index e9ee555..4cdc38f 100644 --- a/model/message.go +++ b/model/message.go @@ -1,7 +1,7 @@ package model import ( - "log" + "sort" "sync" "time" ) @@ -21,13 +21,6 @@ type Message struct { PreviousMessageSig []byte } -func (m *Message) IsBefore(message *Message) bool { - if m.Timestamp.Before(message.Timestamp) { - return true - } - return false -} - func compareSignatures(a []byte, b []byte) bool { if len(a) != len(b) { return false @@ -40,34 +33,32 @@ func compareSignatures(a []byte, b []byte) bool { return true } +func (t Timeline) Len() int { + return len(t.Messages) +} +func (t Timeline) Swap(i, j int) { + t.Messages[i], t.Messages[j] = t.Messages[j], t.Messages[i] +} +func (t Timeline) Less(i, j int) bool { + + if compareSignatures(t.Messages[i].PreviousMessageSig, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) { + return true + } + + if t.Messages[i].Timestamp.Before(t.Messages[j].Timestamp) { + return true + } + + if compareSignatures(t.Messages[i].Signature, t.Messages[j].PreviousMessageSig) { + return true + } + + return false +} + func (t *Timeline) Insert(mi *Message) { t.lock.Lock() - insert := false - place := len(t.Messages) - for i, m := range t.Messages { - - if compareSignatures(m.Signature, mi.PreviousMessageSig) { - log.Printf("comp %d %v %v %x %x", place, mi.Message, m.Message, m.Signature[0:3], mi.PreviousMessageSig[0:3]) - insert = true - place = i + 1 - break - } - - if mi.IsBefore(&m) { - place = i - insert = true - break - } - } - - if insert == false { - t.Messages = append(t.Messages, *mi) - } else { - temp := make([]Message, len(t.Messages)+1) - copy(temp[0:place], t.Messages[0:place]) - temp[place] = *mi - copy(temp[place+1:], t.Messages[place:]) - t.Messages = temp - } + t.Messages = append(t.Messages, *mi) + sort.Sort(t) t.lock.Unlock() } diff --git a/model/message_test.go b/model/message_test.go index 085ce78..0ae1148 100644 --- a/model/message_test.go +++ b/model/message_test.go @@ -7,7 +7,7 @@ import ( "time" ) -func TestMessageInsert(t *testing.T) { +func TestTranscriptConsistency(t *testing.T) { timeline := new(Timeline) // Setup the Group diff --git a/model/profile_test b/model/profile_test index 9bb01a0..49b12fe 100644 --- a/model/profile_test +++ b/model/profile_test @@ -1 +1 @@ -{"Name":"Sarah","Ed25519PublicKey":"j8RkHDyZPCFwuXjVxGtw8Qgv1l45moymwACxEXjQJnI=","Trusted":false,"Blocked":false,"Contacts":{},"Ed25519PrivateKey":"/TkSp0/IJ07+jLw/UAkyFC3mgd5WIAyJ7dRSz7M9kVePxGQcPJk8IXC5eNXEa3DxCC/WXjmajKbAALEReNAmcg==","OnionPrivateKey":{"N":110864111714001498673773375154532122354477779797956408709787762036989130004412378714867422979598016165479959916442576967379813396145904497664600025217339110732358507374657769751998789538776326442350416409397455856063370247022405840568758973841985645812850695658944529625449004363327908735536586292681231570813,"E":65537,"D":29258368192095608908884817685777310347483828667553504814753332196953842753808024509091764161544277089249452778045849081096193791289493937647541419902636631139610918217176285511499379852356646785160340611490902468063399499748079623910235136509595342699975150851412254552332235937599936863279394875342688545217,"Primes":[10479919459766681018465636257798402760984111495664175755942255404420416305820714717114953497775485113275270637658830475401064210738386560406839990913034719,10578717912825421118563933365610407240275592456669407680773574905407145537212719150546363664213151033473733068380589365500463635725956703879386470742958627],"Precomputed":{"Dp":8284056755927383160071597514918996216378258022687682149573348508872845365565133068650383233169949895375519101941128107453074315841465367073200569590456743,"Dq":6372702186525895688861316344573277352428100007466136918640473889031754670020876025200580396770300788890901041238776082975392592557803541040300560979782513,"Qinv":10360189342388704857040075408428853576974943026521455545260795237873844460252519600072106320342798867873256125020528899224644053095341386063420556723083365,"CRTValues":[]}},"Onion":"p2c3insyliefym26","Groups":{}} \ No newline at end of file +{"Name":"Sarah","Ed25519PublicKey":"h+bF9c0SF514SkwRTOBgze4RSH3SkzHryQtp1QX7K2c=","Trusted":false,"Blocked":false,"Contacts":{},"Ed25519PrivateKey":"JjIjdHerRDvam7YGAWRB9TKbcmaF/GmTO7W7T1Te7r+H5sX1zRIXnXhKTBFM4GDN7hFIfdKTMevJC2nVBfsrZw==","OnionPrivateKey":{"N":150334126587032943045375148713167145976886112307031077390166965660647397991493965621432524457750584545841938427575399071242079289459226603323287102292760674425182877649538534621234856024017965368741906106193407381804550153239904811184111592035493922499127339448275867606246302761523482667273192866558668080259,"E":65537,"D":17366978537168720139715508047475295820544192689267624195812351755752650414172159447638214179312902262791542423900595791207619852915083153238027475341539754340816153242259456537434649233588080599834443260629563433852148656237140776914076713110935213796722635544420400426643090954804506516101309871975668441593,"Primes":[11730504892727445290895356094153341596474633870692621163241582324218351073716900128761763998613547851126172912259562433380314953294172830665823918633624781,12815656952688840304471638290754355930607045572334470447975334064993954543008612625605710491332383344451860137114481415847832249366470174722911129087069839],"Precomputed":{"Dp":5911521508340469286393959530060918456852888182970920230684647442238117108070983727552600509364323128601310904881159779777097852548127715311963723708938853,"Dq":4480992097149164221385897911601630623157307311748239777764541863364762933198687128122356163829311142379333430611385044236890245116997483158757778400448683,"Qinv":6197546791251079424328183450454885431371453146097562073580934109929339902017994626469294302315196356224665374413915068750396374123676212919551474229908475,"CRTValues":[]}},"Onion":"lzxwmhuxme3i5kf5","Groups":{}} \ No newline at end of file diff --git a/model/threat_model_test.go b/model/threat_model_test.go index b1d60c0..4940981 100644 --- a/model/threat_model_test.go +++ b/model/threat_model_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -// Nothing in this file constitutes a cryptographic proof, but acts to prevent aggressions in expected protocol +// Nothing in this file constitutes a cryptographic proof, but acts to prevent regressions in expected protocol // behaviour. // When a new member joins the group, they should be unable to decrypt any past messages @@ -16,8 +16,3 @@ func TestGroupForwardSecrecy(t *testing.T) { func TestGroupBackwardSecrecy(t *testing.T) { t.Fatalf("Failed") } - -// If a server reorders stuff, we will still be able to put things in the right order. -func TestTranscriptConsistency(t *testing.T) { - t.Fatalf("Failed") -}