From 499e24fbba4db17071b9712127eb6a4eb9148a8b Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 23 Apr 2018 01:08:18 +0200 Subject: [PATCH] [server] [client] seems to work except normal timeline (with renewal and desync) --- src/git.xdrm.io/schastsp/client/client.go | 10 +- .../schastsp/client/client.internal.go | 95 ++++++++++++------- .../schastsp/client/keyset/keyset.go | 6 +- src/git.xdrm.io/schastsp/server/server.go | 2 +- .../schastsp/server/server.internal.go | 8 +- 5 files changed, 78 insertions(+), 43 deletions(-) diff --git a/src/git.xdrm.io/schastsp/client/client.go b/src/git.xdrm.io/schastsp/client/client.go index 3813953..c4f075f 100644 --- a/src/git.xdrm.io/schastsp/client/client.go +++ b/src/git.xdrm.io/schastsp/client/client.go @@ -9,7 +9,7 @@ import ( "io" ) -const DEBUG = true +const DEBUG = false /* (1) Structure ---------------------------------------------------------*/ @@ -134,9 +134,13 @@ func (c *T) Receive(r io.Reader) error { /* (3) Manage rescue mode */ err = c.rescue(y1, y2) + if err != nil { return err } - /* (4) Dispatch err */ - return err + /* (4) Reset key to not consumed */ + c.key.MigrationCode(0) + c.updateConfig() + + return nil } diff --git a/src/git.xdrm.io/schastsp/client/client.internal.go b/src/git.xdrm.io/schastsp/client/client.internal.go index 513e091..8265e1e 100644 --- a/src/git.xdrm.io/schastsp/client/client.internal.go +++ b/src/git.xdrm.io/schastsp/client/client.internal.go @@ -112,7 +112,18 @@ func (c *T) generateKeyWithConstraints() { /* (1) Get current time id ---------------------------------------------------------*/ - _, timeMod := timeid.Generate(c.ctx.Window()) + /* (1) Get time id */ + timeId, timeMod := timeid.Generate(c.ctx.Window()) + + /* (2) Convert timeId to byte array */ + timeIdBytes := make([]byte, 4) + binary.BigEndian.PutUint32(timeIdBytes, timeId) + + + /* (3) Hash time id */ + hashedTimeId, err := scha.Hash(timeIdBytes, 1) + if err != nil { continue } + /* (2) Generate a new sync ---------------------------------------------------------*/ @@ -122,38 +133,46 @@ func (c *T) generateKeyWithConstraints() { ---------------------------------------------------------*/ /* (1) Get next hash */ syncHash, err := newKey.CurrentHash() - if err != nil { - continue - } + if err != nil { continue } + + /* (2) Get x1 */ + x1 := make([]byte, scha.HSIZE) + copy(x1, xor.ByteArray(keyHash, hashedTimeId)) + + /* (3) Get x2 */ + x2 := make([]byte, scha.HSIZE) + copy(x2, xor.ByteArray(syncHash, hashedTimeId)) + + /* (4) Get x1 xor x2 */ + x := make([]byte, scha.HSIZE) + copy(x, xor.ByteArray(x1, x2)) if DEBUG { - fmt.Printf("+ hash is '%x'\n", keyHash) - } - if DEBUG { - fmt.Printf("+ next hash is '%x'\n", syncHash) + fmt.Printf("+ x1 : '%x'\n", x1) + fmt.Printf("+ x2 : '%x'\n", x2) } /* (2) Get time mod difference (first byte) */ - timeConstraintValue := xor.Byte(keyHash[0], syncHash[0]) + timeConstraintValue := x[0] % 2 == byte(timeMod) if DEBUG { - fmt.Printf(" %.2x ^ %.2x = %.2x[%d] %% 2 = %d == %d ? %t\n", keyHash[0], syncHash[0], timeConstraintValue, timeConstraintValue, uint32(timeConstraintValue)%2, timeMod, uint32(timeConstraintValue)%2 == timeMod) + fmt.Printf(" %.2x ^ %.2x = %.2x[%d] %% 2 = %d == %d ? %t\n", x1[0], x2[0], x[0], x[0], x[0]%2, timeMod, timeConstraintValue) } /* (4) Retry if invalid time constraint */ - if uint32(timeConstraintValue)%2 != timeMod { + if !timeConstraintValue { continue } /* (5) Get migration mod difference (second byte) */ - migrationConstraintValue := xor.Byte(keyHash[1], syncHash[2]) + migrationConstraintValue := x[1] % 3 == byte(c.key.MigrationCode()) if DEBUG { - fmt.Printf(" %.2x ^ %.2x = %.2x[%d] %% 3 = %d == %d ? %t\n", keyHash[1], syncHash[1], migrationConstraintValue, migrationConstraintValue, uint8(migrationConstraintValue)%3, c.key.MigrationCode(), uint8(migrationConstraintValue)%3 == c.key.MigrationCode()) + fmt.Printf(" %.2x ^ %.2x = %.2x[%d] %% 3 = %d == %d ? %t\n", x1[1], x2[1], x[1], x[1], x[1]%3, c.key.MigrationCode(), migrationConstraintValue) } /* (6) Retry if invalid time constraint */ - if uint8(migrationConstraintValue)%3 != c.key.MigrationCode() { + if !migrationConstraintValue { continue } @@ -185,7 +204,7 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error { c.migrateKey() } - /* (2) Decrement and get useful hashes + /* (2) Decrement ---------------------------------------------------------*/ /* (1) Decrement hash */ remainingHashes := c.key.Decrement() @@ -196,17 +215,6 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error { fmt.Printf("Migration code is %d\n", c.key.MigrationCode()) } - /* (2) Store current hash */ - h0, err := c.key.CurrentHash() - if err != nil { - return err - } - - /* (3) Copy into next hash (same value) */ - h1, err := c.key.CurrentHash() - if err != nil { - return err - } /* (3) New sync hash if key consumed ---------------------------------------------------------*/ @@ -215,18 +223,38 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error { /* (1) Generate sync with constraints */ c.generateKeyWithConstraints() - /* (2) Notify key need for renewal */ + /* (2) Notify key needs renewal */ c.key.MigrationCode(2) /* (3) Store config */ c.updateConfig() } - /* (4) Manage time id + /* (4) Get useful hashes + ---------------------------------------------------------*/ + /* (1) Store current hash */ + h0, err := c.key.CurrentHash() + if err != nil { + return err + } + + /* (2) Copy into next hash (same value) */ + h1 := make([]byte, scha.HSIZE) + // 1. If migration code = 0 -> use same hash + copy(h1, h0) + + // 2. Else -> use 'sync' + if c.key.MigrationCode() > 0 { + h1, err = c.sync.CurrentHash() + if err != nil { return err } + } + + + /* (5) Manage time id ---------------------------------------------------------*/ /* (1) Get current time id */ timeId, timeMod := timeid.Generate(c.ctx.Window()) - fmt.Printf("Client.time[%d, %d]\n", timeId, timeMod); + /* (2) Convert time id to byte array */ timeIdBytes := make([]byte, 4) binary.BigEndian.PutUint32(timeIdBytes, timeId) @@ -234,7 +262,8 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error { /* (2) Get digest of time id */ hashedTimeId, err := scha.Hash(timeIdBytes, 1) - /* (5) Calculate x1 and x2 + + /* (6) Calculate x1 and x2 ---------------------------------------------------------*/ /* (1) Calculate x1 = h ^ h(timeId) */ copy(x1, xor.ByteArray(h0, hashedTimeId)) @@ -301,8 +330,10 @@ func (c *T) rescue(y1 []byte, y2 []byte) error { /* (4) Try to rescue the key */ err = c.key.Rescue(receivedHash) + if err != nil { return err } - /* (5) Dispatch error */ - return err + c.updateConfig() + + return nil } diff --git a/src/git.xdrm.io/schastsp/client/keyset/keyset.go b/src/git.xdrm.io/schastsp/client/keyset/keyset.go index 20f70f6..06ff300 100644 --- a/src/git.xdrm.io/schastsp/client/keyset/keyset.go +++ b/src/git.xdrm.io/schastsp/client/keyset/keyset.go @@ -230,10 +230,8 @@ func (s *T) Rescue(lastHash []byte) error { for i := s.depth; i <= s.depth+s.ctx.MinDepth(); i++ { /* (1) Process hash */ - currentHash, err := s.CurrentHash() - if err != nil { - return err - } + currentHash, err := scha.Hash(s.sec, i) + if err != nil { return err } /* (2) If not found -> try again */ if string(currentHash) != string(lastHash) { diff --git a/src/git.xdrm.io/schastsp/server/server.go b/src/git.xdrm.io/schastsp/server/server.go index 1208e28..df6ab5f 100644 --- a/src/git.xdrm.io/schastsp/server/server.go +++ b/src/git.xdrm.io/schastsp/server/server.go @@ -9,7 +9,7 @@ import ( "git.xdrm.io/schastsp/context" ) -const DEBUG = true +const DEBUG = false /* (1) Structure ---------------------------------------------------------*/ diff --git a/src/git.xdrm.io/schastsp/server/server.internal.go b/src/git.xdrm.io/schastsp/server/server.internal.go index 04be474..04f4e11 100644 --- a/src/git.xdrm.io/schastsp/server/server.internal.go +++ b/src/git.xdrm.io/schastsp/server/server.internal.go @@ -92,6 +92,7 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) { /* (2) Extract migration code */ mcode := uint8(x[1]) % 3; + if DEBUG { fmt.Printf(" extracted code is o = %d\n", mcode) } /* (3) Fail if no migration but different hashes */ if mcode == 0 && string(x1[2:]) != string(x2[2:]) { @@ -103,11 +104,11 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) { ---------------------------------------------------------*/ /* (1) Extract time mod */ timeMod := uint32(x[0]) % 2 + if DEBUG { fmt.Printf(" extracted time mod m = %d\n", timeMod) } /* (2) Try to guess time id */ timeId := timeid.Guess(s.ctx.Window(), timeMod) timeIdBytes := make([]byte, 4) - fmt.Printf("Server.time[%d, %d]\n", timeId, timeMod); binary.BigEndian.PutUint32(timeIdBytes, timeId) /* (3) Hash guessed time id */ @@ -137,13 +138,13 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) { /* (1) Hash 1 time to check if matches */ hashedH0, err := scha.Hash(h0, 1) if err != nil { return 2, err } - if DEBUG { fmt.Printf(" hashed is h(h0) = %x\n", hashedH0) } /* (2) If migration code = 2 (Rescue mode) -> hash MIN times */ if mcode == 2 { hashedH0, err = scha.Hash(h0, s.ctx.MinDepth()) if err != nil { return 2, err } - } + if DEBUG { fmt.Printf(" hashed is h(min) = %x\n", hashedH0) } + } else if DEBUG { fmt.Printf(" hashed is h(h0) = %x\n", hashedH0) } /* (3) Fail if does not match */ if string(hashedH0) != string(s.hash) { @@ -155,6 +156,7 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) { ---------------------------------------------------------*/ copy(s.hash, h1) s.store(); + fmt.Printf("Stored next: %x\n", s.hash) return 0, nil