[client.keyset] now secret size can be any but when 'Fetch()'-ing, it must be at least longer than the digest size (scha.HSIZE)

This commit is contained in:
xdrm-brackets 2018-04-21 17:39:28 +02:00
parent c9828e9807
commit 4eb7d6de25
1 changed files with 27 additions and 25 deletions

View File

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