diff --git a/src/git.xdrm.io/schastsp/client/keyset/keyset.go b/src/git.xdrm.io/schastsp/client/keyset/keyset.go index 4cf6119..3b8ac7c 100644 --- a/src/git.xdrm.io/schastsp/client/keyset/keyset.go +++ b/src/git.xdrm.io/schastsp/client/keyset/keyset.go @@ -10,22 +10,24 @@ import ( "time" ) -const a = 12 +const SecretSize = scha.HSIZE * 4; /* Attributes */ type Set struct { - ctx *context.Context // current context - depth uint16 // cur depth - max uint16 // max depth + ctx *context.Context // current context + depth uint16 // cur depth + max uint16 // max depth - sec []byte // secret - consumption uint8 // consumption level + sec []byte // secret + consumed bool // if secret is consumed // 0: none // 1: need to migrate // 2: waiting migration // 3: validated migration } + + /* (1) Creates a new KeySet * * @ctx Context constants @@ -52,6 +54,8 @@ func Create(ctx *context.Context) (*Set, error) { return instance, nil } + + /* (2) Generates a pseudo-random KeySet * ---------------------------------------------------------*/ @@ -63,10 +67,10 @@ func (s *Set) generate() { /* (1) Generate new secret ---------------------------------------------------------*/ /* (1) Reset current secret */ - s.sec = make([]byte, scha.HSIZE) + s.sec = make([]byte, SecretSize) /* (2) Generate each char. until same length as hash digest */ - for i := uint16(0); i < scha.HSIZE; i++ { + for i := uint16(0); i < SecretSize; i++ { s.sec[i] = byte(rand.Int() % 256) } @@ -79,10 +83,12 @@ func (s *Set) generate() { s.depth = randMin + uint16(rand.Intn(int(randMax-randMin))) /* (3) Reset comsumption level */ - s.consumption = 0 + s.consumed = false } + + /* (3) Get current hash * * @return digest<[]byte]> Current hash representing the set @@ -103,6 +109,8 @@ func (s Set) Hash() ([]byte, error) { } + + /* (4) Decrement depth * * @return remaining Remaining hashes before migration @@ -114,9 +122,7 @@ func (s *Set) Decrement() uint16 { s.depth-- /* (2) If near minDepth (10 far): set consumption to 1 */ - if s.depth <= s.ctx.MinDepth()+s.ctx.DepthThreshold() { - s.consumption = 1 - } + s.consumed = s.depth <= s.ctx.MinDepth()+s.ctx.DepthThreshold() /* (3) Return remaining attempts */ return s.depth - s.max @@ -124,13 +130,6 @@ func (s *Set) Decrement() uint16 { } -/* (5) Prepare a renewal -* -* @return level outDesc -* ----------------------------------------------------------*/ - - /* (5) Serialisation * @@ -153,7 +152,7 @@ func (s *Set) Store(writer io.Writer) error { var err error; /* (1) Copy secret size */ - err = binary.Write(writer, binary.BigEndian, scha.HSIZE) + err = binary.Write(writer, binary.BigEndian, uint16(len(s.sec))) if err != nil { return err } /* (2) Copy secret */ @@ -165,13 +164,15 @@ func (s *Set) Store(writer io.Writer) error { if err != nil { return err } /* (4) Copy migration level */ - err = binary.Write(writer, binary.BigEndian, s.consumption) + err = binary.Write(writer, binary.BigEndian, s.consumed) if err != nil { return err } return nil } + + /* (6) Builds a KeySet from its serial representation * * @serial String representation @@ -195,12 +196,13 @@ func (s *Set) Fetch(reader io.Reader) error { err = binary.Read(reader, binary.BigEndian, &secretLength) if err != nil { return err } - /* (2) Fail if secretLength different than digest size */ - if secretLength != scha.HSIZE { - return errors.New("Invalid secret size (must be the same as digest size)") + /* (2) Fail if secretLength lower than digest size */ + if secretLength < scha.HSIZE { + return errors.New("Invalid secret size (must be at least the same size as a digest)") } /* (3) Try to copy the secret */ + s.sec = make([]byte, secretLength); err = binary.Read(reader, binary.BigEndian, &s.sec) if err != nil { return err } @@ -214,7 +216,7 @@ func (s *Set) Fetch(reader io.Reader) error { if err != nil { return err } /* (6) Try to copy the consumption level */ - err = binary.Read(reader, binary.BigEndian, &s.consumption) + err = binary.Read(reader, binary.BigEndian, &s.consumed) if err != nil { return err } return nil