[server] [client] seems to work except normal timeline (with renewal and desync)

This commit is contained in:
xdrm-brackets 2018-04-23 01:08:18 +02:00
parent 08581354a4
commit 499e24fbba
5 changed files with 78 additions and 43 deletions

View File

@ -9,7 +9,7 @@ import (
"io" "io"
) )
const DEBUG = true const DEBUG = false
/* (1) Structure /* (1) Structure
---------------------------------------------------------*/ ---------------------------------------------------------*/
@ -134,9 +134,13 @@ func (c *T) Receive(r io.Reader) error {
/* (3) Manage rescue mode */ /* (3) Manage rescue mode */
err = c.rescue(y1, y2) err = c.rescue(y1, y2)
if err != nil { return err }
/* (4) Dispatch err */ /* (4) Reset key to not consumed */
return err c.key.MigrationCode(0)
c.updateConfig()
return nil
} }

View File

@ -112,7 +112,18 @@ func (c *T) generateKeyWithConstraints() {
/* (1) Get current time id /* (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 /* (2) Generate a new sync
---------------------------------------------------------*/ ---------------------------------------------------------*/
@ -122,38 +133,46 @@ func (c *T) generateKeyWithConstraints() {
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Get next hash */ /* (1) Get next hash */
syncHash, err := newKey.CurrentHash() syncHash, err := newKey.CurrentHash()
if err != nil { if err != nil { continue }
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 { if DEBUG {
fmt.Printf("+ hash is '%x'\n", keyHash) fmt.Printf("+ x1 : '%x'\n", x1)
} fmt.Printf("+ x2 : '%x'\n", x2)
if DEBUG {
fmt.Printf("+ next hash is '%x'\n", syncHash)
} }
/* (2) Get time mod difference (first byte) */ /* (2) Get time mod difference (first byte) */
timeConstraintValue := xor.Byte(keyHash[0], syncHash[0]) timeConstraintValue := x[0] % 2 == byte(timeMod)
if DEBUG { 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 */ /* (4) Retry if invalid time constraint */
if uint32(timeConstraintValue)%2 != timeMod { if !timeConstraintValue {
continue continue
} }
/* (5) Get migration mod difference (second byte) */ /* (5) Get migration mod difference (second byte) */
migrationConstraintValue := xor.Byte(keyHash[1], syncHash[2]) migrationConstraintValue := x[1] % 3 == byte(c.key.MigrationCode())
if DEBUG { 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 */ /* (6) Retry if invalid time constraint */
if uint8(migrationConstraintValue)%3 != c.key.MigrationCode() { if !migrationConstraintValue {
continue continue
} }
@ -185,7 +204,7 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error {
c.migrateKey() c.migrateKey()
} }
/* (2) Decrement and get useful hashes /* (2) Decrement
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Decrement hash */ /* (1) Decrement hash */
remainingHashes := c.key.Decrement() 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()) 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 /* (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 */ /* (1) Generate sync with constraints */
c.generateKeyWithConstraints() c.generateKeyWithConstraints()
/* (2) Notify key need for renewal */ /* (2) Notify key needs renewal */
c.key.MigrationCode(2) c.key.MigrationCode(2)
/* (3) Store config */ /* (3) Store config */
c.updateConfig() 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 */ /* (1) Get current time id */
timeId, timeMod := timeid.Generate(c.ctx.Window()) timeId, timeMod := timeid.Generate(c.ctx.Window())
fmt.Printf("Client.time[%d, %d]\n", timeId, timeMod);
/* (2) Convert time id to byte array */ /* (2) Convert time id to byte array */
timeIdBytes := make([]byte, 4) timeIdBytes := make([]byte, 4)
binary.BigEndian.PutUint32(timeIdBytes, timeId) binary.BigEndian.PutUint32(timeIdBytes, timeId)
@ -234,7 +262,8 @@ func (c *T) generateRequest(x1 []byte, x2 []byte) error {
/* (2) Get digest of time id */ /* (2) Get digest of time id */
hashedTimeId, err := scha.Hash(timeIdBytes, 1) hashedTimeId, err := scha.Hash(timeIdBytes, 1)
/* (5) Calculate x1 and x2
/* (6) Calculate x1 and x2
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Calculate x1 = h ^ h(timeId) */ /* (1) Calculate x1 = h ^ h(timeId) */
copy(x1, xor.ByteArray(h0, hashedTimeId)) 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 */ /* (4) Try to rescue the key */
err = c.key.Rescue(receivedHash) err = c.key.Rescue(receivedHash)
if err != nil { return err }
/* (5) Dispatch error */ c.updateConfig()
return err
return nil
} }

View File

@ -230,10 +230,8 @@ func (s *T) Rescue(lastHash []byte) error {
for i := s.depth; i <= s.depth+s.ctx.MinDepth(); i++ { for i := s.depth; i <= s.depth+s.ctx.MinDepth(); i++ {
/* (1) Process hash */ /* (1) Process hash */
currentHash, err := s.CurrentHash() currentHash, err := scha.Hash(s.sec, i)
if err != nil { if err != nil { return err }
return err
}
/* (2) If not found -> try again */ /* (2) If not found -> try again */
if string(currentHash) != string(lastHash) { if string(currentHash) != string(lastHash) {

View File

@ -9,7 +9,7 @@ import (
"git.xdrm.io/schastsp/context" "git.xdrm.io/schastsp/context"
) )
const DEBUG = true const DEBUG = false
/* (1) Structure /* (1) Structure
---------------------------------------------------------*/ ---------------------------------------------------------*/

View File

@ -92,6 +92,7 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) {
/* (2) Extract migration code */ /* (2) Extract migration code */
mcode := uint8(x[1]) % 3; mcode := uint8(x[1]) % 3;
if DEBUG { fmt.Printf(" extracted code is o = %d\n", mcode) }
/* (3) Fail if no migration but different hashes */ /* (3) Fail if no migration but different hashes */
if mcode == 0 && string(x1[2:]) != string(x2[2:]) { 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 */ /* (1) Extract time mod */
timeMod := uint32(x[0]) % 2 timeMod := uint32(x[0]) % 2
if DEBUG { fmt.Printf(" extracted time mod m = %d\n", timeMod) }
/* (2) Try to guess time id */ /* (2) Try to guess time id */
timeId := timeid.Guess(s.ctx.Window(), timeMod) timeId := timeid.Guess(s.ctx.Window(), timeMod)
timeIdBytes := make([]byte, 4) timeIdBytes := make([]byte, 4)
fmt.Printf("Server.time[%d, %d]\n", timeId, timeMod);
binary.BigEndian.PutUint32(timeIdBytes, timeId) binary.BigEndian.PutUint32(timeIdBytes, timeId)
/* (3) Hash guessed time id */ /* (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 */ /* (1) Hash 1 time to check if matches */
hashedH0, err := scha.Hash(h0, 1) hashedH0, err := scha.Hash(h0, 1)
if err != nil { return 2, err } 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 */ /* (2) If migration code = 2 (Rescue mode) -> hash MIN times */
if mcode == 2 { if mcode == 2 {
hashedH0, err = scha.Hash(h0, s.ctx.MinDepth()) hashedH0, err = scha.Hash(h0, s.ctx.MinDepth())
if err != nil { return 2, err } 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 */ /* (3) Fail if does not match */
if string(hashedH0) != string(s.hash) { if string(hashedH0) != string(s.hash) {
@ -155,6 +156,7 @@ func (s *T) manageRequest(x1 []byte, x2 []byte) (byte, error) {
---------------------------------------------------------*/ ---------------------------------------------------------*/
copy(s.hash, h1) copy(s.hash, h1)
s.store(); s.store();
fmt.Printf("Stored next: %x\n", s.hash)
return 0, nil return 0, nil