2018-04-22 18:54:16 +00:00
|
|
|
package client
|
2018-04-21 23:19:42 +00:00
|
|
|
|
2018-04-22 16:45:30 +00:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2018-04-22 18:54:16 +00:00
|
|
|
"fmt"
|
|
|
|
"git.xdrm.io/schastsp/client/keyset"
|
2018-04-25 06:17:20 +00:00
|
|
|
"git.xdrm.io/schastsp/util/scha"
|
|
|
|
"git.xdrm.io/schastsp/util/timeid"
|
|
|
|
"git.xdrm.io/schastsp/util/xor"
|
2018-04-22 16:45:30 +00:00
|
|
|
)
|
2018-04-21 23:19:42 +00:00
|
|
|
|
|
|
|
/* (1) Updates 'key' and 'sync' with files
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-22 18:54:16 +00:00
|
|
|
func (c *T) updateConfig() {
|
2018-04-21 23:19:42 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
var err error
|
2018-04-21 23:19:42 +00:00
|
|
|
|
|
|
|
/* (1) Restore if both are NIL
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if c.key == nil && c.sync == nil {
|
|
|
|
|
|
|
|
/* (1) Create default key */
|
2018-04-22 18:54:16 +00:00
|
|
|
c.key, err = keyset.Create(c.ctx)
|
2018-04-21 23:19:42 +00:00
|
|
|
|
|
|
|
/* (2) Fetch key */
|
|
|
|
err = c.fkey.Fetch(c.key)
|
|
|
|
|
|
|
|
/* (3) On error -> set key to NIL */
|
2018-04-24 16:32:00 +00:00
|
|
|
if err != nil { c.key = nil }
|
2018-04-21 23:19:42 +00:00
|
|
|
|
|
|
|
/* (4) Create default sync */
|
2018-04-22 18:54:16 +00:00
|
|
|
c.sync, err = keyset.Create(c.ctx)
|
2018-04-21 23:19:42 +00:00
|
|
|
|
|
|
|
/* (5) Fetch sync */
|
|
|
|
err = c.fsync.Fetch(c.sync)
|
|
|
|
|
|
|
|
/* (6) On error -> set sync to NIL */
|
2018-04-24 16:32:00 +00:00
|
|
|
if err != nil { c.sync = nil }
|
2018-04-21 23:19:42 +00:00
|
|
|
|
|
|
|
/* (7) Exit if all keysets have been fetched */
|
|
|
|
if c.key != nil && c.sync != nil {
|
2018-04-22 18:54:16 +00:00
|
|
|
return
|
2018-04-21 23:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:32:00 +00:00
|
|
|
|
2018-04-21 23:19:42 +00:00
|
|
|
/* (2) If cannot fetch -> create new keysets
|
|
|
|
---------------------------------------------------------*/
|
2018-04-22 18:54:16 +00:00
|
|
|
if c.key == nil {
|
|
|
|
c.key, _ = keyset.Create(c.ctx)
|
2018-04-21 23:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
if c.sync == nil {
|
|
|
|
c.sync, _ = keyset.Create(c.ctx)
|
2018-04-21 23:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 16:32:00 +00:00
|
|
|
|
2018-04-21 23:19:42 +00:00
|
|
|
/* (3) Store current value
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Store key */
|
|
|
|
err = c.fkey.Store(c.key)
|
2018-04-24 16:32:00 +00:00
|
|
|
if err != nil { panic("Cannot store key") }
|
2018-04-21 23:19:42 +00:00
|
|
|
|
|
|
|
/* (2) Store sync */
|
|
|
|
err = c.fsync.Store(c.sync)
|
2018-04-24 16:32:00 +00:00
|
|
|
if err != nil { panic("Cannot store sync") }
|
2018-04-21 23:19:42 +00:00
|
|
|
|
2018-04-22 16:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Migrate current key
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-22 18:54:16 +00:00
|
|
|
func (c *T) migrateKey() {
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
var err error
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (1) Copy sync into key */
|
|
|
|
c.key = c.sync
|
|
|
|
|
|
|
|
/* (2) Regenerate sync */
|
2018-04-22 18:54:16 +00:00
|
|
|
c.sync, err = keyset.Create(c.ctx)
|
2018-04-24 16:32:00 +00:00
|
|
|
if err != nil { panic(err) }
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (3) Store keysets to files */
|
2018-04-22 18:54:16 +00:00
|
|
|
c.updateConfig()
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
/* (3) Generate a new 'sync' keyset respecting mod constraints (timeid + migration)
|
2018-04-22 16:45:30 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-22 18:54:16 +00:00
|
|
|
func (c *T) generateKeyWithConstraints() {
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* Get current hash */
|
2018-04-22 18:54:16 +00:00
|
|
|
keyHash, err := c.key.CurrentHash()
|
2018-04-24 16:32:00 +00:00
|
|
|
if err != nil { panic(err) }
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* Search key one is respects contraints */
|
|
|
|
for true {
|
|
|
|
|
|
|
|
/* (1) Get current time id
|
|
|
|
---------------------------------------------------------*/
|
2018-04-22 23:08:18 +00:00
|
|
|
/* (1) Get time id */
|
2018-04-24 16:32:00 +00:00
|
|
|
timeID, timeMod := timeid.Generate(c.ctx.Window())
|
2018-04-22 23:08:18 +00:00
|
|
|
|
|
|
|
/* (2) Convert timeId to byte array */
|
2018-04-24 16:32:00 +00:00
|
|
|
timeIDBytes := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(timeIDBytes, timeID)
|
2018-04-22 23:08:18 +00:00
|
|
|
|
|
|
|
/* (3) Hash time id */
|
2018-04-24 16:32:00 +00:00
|
|
|
hashedTimeID, err := scha.Hash(timeIDBytes, 1)
|
2018-04-22 23:08:18 +00:00
|
|
|
if err != nil { continue }
|
|
|
|
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (2) Generate a new sync
|
|
|
|
---------------------------------------------------------*/
|
2018-04-22 18:54:16 +00:00
|
|
|
newKey, _ := keyset.Create(c.ctx)
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-24 16:32:00 +00:00
|
|
|
|
2018-04-22 16:45:30 +00:00
|
|
|
/* (3) Check constraints
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Get next hash */
|
2018-04-22 18:54:16 +00:00
|
|
|
syncHash, err := newKey.CurrentHash()
|
2018-04-22 23:08:18 +00:00
|
|
|
if err != nil { continue }
|
|
|
|
|
|
|
|
/* (2) Get x1 */
|
|
|
|
x1 := make([]byte, scha.HSIZE)
|
2018-04-24 16:32:00 +00:00
|
|
|
copy(x1, xor.ByteArray(keyHash, hashedTimeID))
|
2018-04-22 23:08:18 +00:00
|
|
|
|
|
|
|
/* (3) Get x2 */
|
|
|
|
x2 := make([]byte, scha.HSIZE)
|
2018-04-24 16:32:00 +00:00
|
|
|
copy(x2, xor.ByteArray(syncHash, hashedTimeID))
|
2018-04-22 23:08:18 +00:00
|
|
|
|
|
|
|
/* (4) Get x1 xor x2 */
|
|
|
|
x := make([]byte, scha.HSIZE)
|
|
|
|
copy(x, xor.ByteArray(x1, x2))
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
if DEBUG {
|
2018-04-22 23:08:18 +00:00
|
|
|
fmt.Printf("+ x1 : '%x'\n", x1)
|
|
|
|
fmt.Printf("+ x2 : '%x'\n", x2)
|
2018-04-22 18:54:16 +00:00
|
|
|
}
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (2) Get time mod difference (first byte) */
|
2018-04-24 16:32:00 +00:00
|
|
|
timeConstraintValue := x[0]%2 == byte(timeMod)
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-24 16:32:00 +00:00
|
|
|
if DEBUG { fmt.Printf(" %.2x ^ %.2x = %.2x[%d] %% 2 = %d == %d ? %t\n", x1[0], x2[0], x[0], x[0], x[0]%2, timeMod, timeConstraintValue) }
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (4) Retry if invalid time constraint */
|
2018-04-24 16:32:00 +00:00
|
|
|
if !timeConstraintValue { continue }
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (5) Get migration mod difference (second byte) */
|
2018-04-24 16:32:00 +00:00
|
|
|
migrationConstraintValue := x[1]%3 == byte(c.key.MigrationCode())
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-24 16:32:00 +00:00
|
|
|
if DEBUG { 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) }
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (6) Retry if invalid time constraint */
|
2018-04-24 16:32:00 +00:00
|
|
|
if !migrationConstraintValue { continue }
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (7) Store new sync */
|
2018-04-22 18:54:16 +00:00
|
|
|
c.sync = newKey
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (8) Store keysets to files */
|
2018-04-22 18:54:16 +00:00
|
|
|
c.updateConfig()
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
break
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Generate the client request
|
|
|
|
*
|
|
|
|
* @x1<[]byte> Byte array to write into
|
|
|
|
* @x2<[]byte> Byte array to write into
|
|
|
|
*
|
|
|
|
* @return err<error> The error or NIL if not
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
func (c *T) generateRequest(x1 []byte, x2 []byte) error {
|
|
|
|
|
|
|
|
/* (1) Migrate if validated migration
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if c.key.MigrationCode() == 3 {
|
2018-04-22 18:54:16 +00:00
|
|
|
c.migrateKey()
|
2018-04-22 16:45:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 23:08:18 +00:00
|
|
|
/* (2) Decrement
|
2018-04-22 16:45:30 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Decrement hash */
|
|
|
|
remainingHashes := c.key.Decrement()
|
2018-04-22 18:54:16 +00:00
|
|
|
c.updateConfig()
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
if DEBUG {
|
|
|
|
fmt.Printf("Remaining %x[%d] hashes\n", remainingHashes, remainingHashes)
|
|
|
|
fmt.Printf("Migration code is %d\n", c.key.MigrationCode())
|
|
|
|
}
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (3) New sync hash if key consumed
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if c.key.MigrationCode() > 0 {
|
|
|
|
|
|
|
|
/* (1) Generate sync with constraints */
|
2018-04-22 18:54:16 +00:00
|
|
|
c.generateKeyWithConstraints()
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-22 23:08:18 +00:00
|
|
|
/* (2) Notify key needs renewal */
|
2018-04-22 18:54:16 +00:00
|
|
|
c.key.MigrationCode(2)
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (3) Store config */
|
2018-04-22 18:54:16 +00:00
|
|
|
c.updateConfig()
|
2018-04-22 16:45:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 23:08:18 +00:00
|
|
|
/* (4) Get useful hashes
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Store current hash */
|
|
|
|
h0, err := c.key.CurrentHash()
|
2018-04-24 16:32:00 +00:00
|
|
|
if err != nil { return err }
|
2018-04-22 23:08:18 +00:00
|
|
|
|
|
|
|
/* (2) Copy into next hash (same value) */
|
|
|
|
h1 := make([]byte, scha.HSIZE)
|
2018-04-24 16:32:00 +00:00
|
|
|
|
2018-04-22 23:08:18 +00:00
|
|
|
// 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
|
2018-04-22 16:45:30 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Get current time id */
|
2018-04-24 16:32:00 +00:00
|
|
|
timeID, timeMod := timeid.Generate(c.ctx.Window())
|
2018-04-22 23:08:18 +00:00
|
|
|
|
2018-04-22 16:45:30 +00:00
|
|
|
/* (2) Convert time id to byte array */
|
2018-04-24 16:32:00 +00:00
|
|
|
timeIDBytes := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(timeIDBytes, timeID)
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (2) Get digest of time id */
|
2018-04-24 16:32:00 +00:00
|
|
|
hashedTimeID, err := scha.Hash(timeIDBytes, 1)
|
2018-04-22 23:08:18 +00:00
|
|
|
|
|
|
|
/* (6) Calculate x1 and x2
|
2018-04-22 16:45:30 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Calculate x1 = h ^ h(timeId) */
|
2018-04-24 16:32:00 +00:00
|
|
|
copy(x1, xor.ByteArray(h0, hashedTimeID))
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
if DEBUG {
|
|
|
|
fmt.Printf("\n=== x1 ===\n")
|
|
|
|
fmt.Printf(" hash is h0 = %x\n", h0)
|
2018-04-24 16:32:00 +00:00
|
|
|
fmt.Printf(" time id is n = %x[%d]\n", timeIDBytes, timeID)
|
|
|
|
fmt.Printf(" h(t) = %x\n", hashedTimeID)
|
2018-04-22 22:02:55 +00:00
|
|
|
fmt.Printf(" x1 is h0+h(t) = %X\n", x1)
|
2018-04-24 16:32:00 +00:00
|
|
|
fmt.Printf(" check x1+h(t) eq h0 = %x\n", xor.ByteArray(x1, hashedTimeID))
|
2018-04-22 18:54:16 +00:00
|
|
|
fmt.Printf(" check x1+h0 eq h(t) = %x\n", xor.ByteArray(x1, h0))
|
|
|
|
}
|
2018-04-22 16:45:30 +00:00
|
|
|
|
|
|
|
/* (2) Calculate x2 = h ^ h(timeId) ^ timeMod */
|
2018-04-24 16:32:00 +00:00
|
|
|
copy(x2, xor.ByteArray(h1, hashedTimeID))
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
// only add time mod if code = 0
|
|
|
|
if c.key.MigrationCode() == 0 {
|
|
|
|
x2[0] = xor.Byte(x2[0], byte(timeMod))
|
2018-04-22 16:45:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
if DEBUG {
|
|
|
|
fmt.Printf("\n=== x2 ===\n")
|
|
|
|
fmt.Printf(" next is h1 = %x\n", h1)
|
|
|
|
fmt.Printf(" time mod is m = %x[%d]\n", timeMod, timeMod)
|
2018-04-24 16:32:00 +00:00
|
|
|
fmt.Printf(" h(t) = %x\n", hashedTimeID)
|
|
|
|
if c.key.MigrationCode() == 0 {
|
|
|
|
fmt.Printf(" x2 is h1+h(t)+m = %X\n", x2)
|
|
|
|
fmt.Printf(" check x2+x1 %% 2 eq m = %d (%t)\n", uint8(xor.ByteArray(x1, x2)[0]%2), xor.ByteArray(x1, x2)[0]%2 == byte(timeMod))
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" x2 is h1+h(t) = %X\n", x2)
|
|
|
|
fmt.Printf(" check x2+x1 %% 2 eq m = %d (%t)\n", uint8(xor.ByteArray(x1, x2)[0]%2), xor.ByteArray(x1, x2)[0]%2 == byte(timeMod))
|
|
|
|
}
|
2018-04-22 18:54:16 +00:00
|
|
|
fmt.Printf(" check x2+x1 %% 3 eq o = %d (%t)\n", uint8(xor.ByteArray(x1, x2)[1]%3), uint8(xor.ByteArray(x1, x2)[1]%3) == c.key.MigrationCode())
|
|
|
|
}
|
2018-04-22 16:45:30 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
return nil
|
2018-04-22 17:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Rescue management
|
|
|
|
*
|
|
|
|
* @Y1<[]byte> First rescue parameter
|
|
|
|
* @Y2<[]byte> Second rescue parameter
|
|
|
|
*
|
|
|
|
* @return err<error> The error or NIL if not
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
func (c *T) rescue(y1 []byte, y2 []byte) error {
|
|
|
|
|
|
|
|
/* (1) Extract time mod */
|
2018-04-22 18:54:16 +00:00
|
|
|
timeMod := uint32(xor.ByteArray(y1, y2)[0] % 2)
|
2018-04-22 17:17:55 +00:00
|
|
|
|
|
|
|
/* (2) Try to guess time id from timeM */
|
2018-04-24 16:32:00 +00:00
|
|
|
timeID := timeid.Guess(c.ctx.Window(), timeMod)
|
|
|
|
timeIDBytes := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(timeIDBytes, timeID)
|
2018-04-22 17:17:55 +00:00
|
|
|
|
|
|
|
/* (3) Hash timeId */
|
2018-04-24 16:32:00 +00:00
|
|
|
hashedTimeID, err := scha.Hash(timeIDBytes, 1)
|
|
|
|
if err != nil { return err }
|
2018-04-22 17:17:55 +00:00
|
|
|
|
|
|
|
/* (4) Get the received hash */
|
2018-04-24 16:32:00 +00:00
|
|
|
receivedHash := xor.ByteArray(y1, hashedTimeID)
|
2018-04-22 17:17:55 +00:00
|
|
|
|
2018-04-23 16:09:31 +00:00
|
|
|
/* (5) Try to rescue the key */
|
2018-04-22 17:17:55 +00:00
|
|
|
err = c.key.Rescue(receivedHash)
|
2018-04-22 23:08:18 +00:00
|
|
|
if err != nil { return err }
|
|
|
|
|
2018-04-23 16:09:31 +00:00
|
|
|
/* (6) Store updated key */
|
2018-04-22 23:08:18 +00:00
|
|
|
c.updateConfig()
|
2018-04-22 17:17:55 +00:00
|
|
|
|
2018-04-22 23:08:18 +00:00
|
|
|
return nil
|
2018-04-22 17:17:55 +00:00
|
|
|
|
2018-04-22 18:54:16 +00:00
|
|
|
}
|