From afc801186f2591555eb43c0138ac1951da02cea4 Mon Sep 17 00:00:00 2001 From: siddharth Date: Sat, 9 Apr 2022 20:26:52 -0400 Subject: lib: add NewSRPClientSession --- lib/srp.go | 30 ++++++++++++++++++++++++++++++ lib/srp_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) (limited to 'lib') diff --git a/lib/srp.go b/lib/srp.go index 20208a6..491565c 100644 --- a/lib/srp.go +++ b/lib/srp.go @@ -78,6 +78,8 @@ type SRPClientSession struct { // Multipier parameter. Client and server agree upon the value // of N. k *big.Int + // Hashing object for H() function. + h Sha256 // User's email address ident string // Scrambling parameter. @@ -131,3 +133,31 @@ func NewSRPUser(n, g, k, ident, pass string) (*SRPUser, error) { return user, nil } + +func NewSRPClientSession(n, g, k, ident string) (*SRPClientSession, error) { + var ok bool + + session := new(SRPClientSession) + session.n, ok = new(big.Int).SetString(StripSpaceChars(n), 16) + if !ok { + return nil, CPError{"n is invalid"} + } + session.g, ok = new(big.Int).SetString(StripSpaceChars(g), 16) + if !ok { + return nil, CPError{"g is invalid"} + } + session.k, ok = new(big.Int).SetString(StripSpaceChars(k), 16) + if !ok { + return nil, CPError{"k is invalid"} + } + session.ident = ident + + // Initialize hashing object. + session.h = Sha256{} + session.h.Init([]uint32{}) + + // Generate secret ephemeral value. + session.a = big.NewInt(RandomInt(1, 10000000)) + + return session, nil +} diff --git a/lib/srp_test.go b/lib/srp_test.go index d7e3b0d..a31e658 100644 --- a/lib/srp_test.go +++ b/lib/srp_test.go @@ -67,3 +67,52 @@ func TestNewSRPUser(t *testing.T) { return } } + +func TestNewSRPClientSession(t *testing.T) { + n := StripSpaceChars( + `ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024 + e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd + 3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec + 6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f + 24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361 + c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552 + bb9ed529077096966d670c354e4abc9804f1746c08ca237327fff + fffffffffffff`) + g := "2" + k := "3" + ident := "s@ricketyspace.net" + session, err := NewSRPClientSession(n, g, k, ident) + if err != nil { + t.Errorf("Error: %v\n", err) + return + } + + // Check n. + bigN, _ := new(big.Int).SetString(StripSpaceChars(n), 16) + if session.n.Cmp(bigN) != 0 { + t.Error("Error: n not set correctly\n") + return + } + // Check g. + bigG, _ := new(big.Int).SetString(StripSpaceChars(g), 16) + if session.g.Cmp(bigG) != 0 { + t.Error("Error: g not set correctly\n") + return + } + // Check k. + bigK, _ := new(big.Int).SetString(StripSpaceChars(k), 16) + if session.k.Cmp(bigK) != 0 { + t.Error("Error: k not set correctly\n") + return + } + // Check ident. + if session.ident != ident { + t.Error("Error: user not set correctly\n") + return + } + // Check a. + if session.a.Cmp(big.NewInt(1)) < 0 { + t.Error("Error: a not set correctly\n") + return + } +} -- cgit v1.2.3