From b5c847758231af49055a2a83847913133f112300 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 21 Apr 2018 16:08:08 +0200 Subject: [PATCH] [lib.context] added 'minDepth' and 'depthThreshold' + [client.keyset] added context management --- .../schastsp/client/keyset/keyset.go | 37 +++++++++---------- .../schastsp/lib/context/context.go | 27 +++++++++++--- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/git.xdrm.io/schastsp/client/keyset/keyset.go b/src/git.xdrm.io/schastsp/client/keyset/keyset.go index 60f09af..0a128e8 100644 --- a/src/git.xdrm.io/schastsp/client/keyset/keyset.go +++ b/src/git.xdrm.io/schastsp/client/keyset/keyset.go @@ -1,6 +1,7 @@ package keyset import ( + "git.xdrm.io/schastsp/lib/context" "io" "encoding/binary" "errors" @@ -13,12 +14,12 @@ const a = 12 /* Attributes */ type Set struct { - min uint16 // min depth - 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 uint // consumption level + sec []byte // secret + consumption uint // consumption level // 0: none // 1: need to migrate // 2: waiting migration @@ -33,18 +34,18 @@ type Set struct { * @return outName outDesc * ---------------------------------------------------------*/ -func Create(min uint16, max uint16) (*Set, error) { +func Create(ctx *context.Context, max uint16) (*Set, error) { - /* (1) Fail if min >= max */ - if min >= max { - return nil, errors.New("Max depth is not greater than Min depth") + /* (1) Fail if min+thre >= max */ + if ctx.MinDepth()+ctx.DepthThreshold() >= max { + return nil, errors.New("Maximum depth must be greater than Min+threshold (from given Context)") } /* (2) Instanciate */ var instance = new(Set) /* (3) Set attributes */ - instance.min = min + instance.ctx = ctx instance.max = max /* (4) Generate values if secret */ @@ -76,7 +77,7 @@ func (s *Set) generate() { /* (2) Manage other attributes ---------------------------------------------------------*/ /* (1) Random depth pick init */ - var randMin, randMax uint16 = s.min + (s.max-s.min)/2, s.max + var randMin, randMax uint16 = s.ctx.MinDepth() + (s.max-s.ctx.MinDepth())/2, s.max /* (2) Select "random" depth */ s.depth = randMin + uint16(rand.Intn(int(randMax-randMin))) @@ -117,7 +118,7 @@ func (s *Set) Decrement() uint16 { s.depth-- /* (2) If near minDepth (10 far): set consumption to 1 */ - if s.depth <= s.min+10 { + if s.depth <= s.ctx.MinDepth()+s.ctx.DepthThreshold() { s.consumption = 1 } @@ -146,7 +147,7 @@ func (s *Set) Write(writer io.Writer) error { var err error; - /* (1) Copy hash size */ + /* (1) Copy secret size */ err = binary.Write(writer, binary.BigEndian, scha.HSIZE) if err != nil { return err } @@ -178,16 +179,12 @@ func (s *Set) Write(writer io.Writer) error { ---------------------------------------------------------*/ func (s *Set) Read(reader io.Reader) error { - var ( - err error - secretLength uint16 - ) + var err error + var secretLength uint16 /* (1) Read the secret size */ 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 */ if secretLength != scha.HSIZE { diff --git a/src/git.xdrm.io/schastsp/lib/context/context.go b/src/git.xdrm.io/schastsp/lib/context/context.go index d458af8..5cade7d 100644 --- a/src/git.xdrm.io/schastsp/lib/context/context.go +++ b/src/git.xdrm.io/schastsp/lib/context/context.go @@ -1,20 +1,37 @@ package context +import ( + "errors" +) + /* (1) Context class ---------------------------------------------------------*/ /* (1) Attributes */ type Context struct { - windowSize float64; // 'timeid' window size + win float64; // 'timeid' window size + dMin uint16; // minimum scha depth + dThreshold uint16; // scha depth threshold } /* (2) Constructor */ -func Create(winSize float64) *Context{ +func Create(win float64, dMin uint16, dThreshold uint16) (*Context, error) { var instance = new(Context); - instance.windowSize = winSize; + /* (1) Window size error */ + if win < 0 { return nil, errors.New("Window size must be positive and is negative") } + instance.win = win; - return instance + /* (2) Minimum Depth error */ + if dMin < 0x0f { return nil, errors.New("Minimum depth must be greater than 0x0f (decimal 15) for consistency issues") } + instance.dMin = dMin; + + /* (3) Depth Threshold error */ + instance.dThreshold = dThreshold; + + return instance, nil } /* (3) Getters */ -func (c Context) GetWindowSize() float64 { return c.windowSize } \ No newline at end of file +func (c Context) Window() float64 { return c.win } +func (c Context) MinDepth() uint16 { return c.dMin } +func (c Context) DepthThreshold() uint16 { return c.dThreshold } \ No newline at end of file