From f613d9d03211efed74dcc9e5674f7eb8d9a94325 Mon Sep 17 00:00:00 2001 From: siddharth Date: Sun, 10 Apr 2022 12:30:20 -0400 Subject: lib: add srp scrambling parameter functions --- lib/srp.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/srp_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) (limited to 'lib') diff --git a/lib/srp.go b/lib/srp.go index 05b61ab..2303793 100644 --- a/lib/srp.go +++ b/lib/srp.go @@ -170,6 +170,35 @@ func (u *SRPUser) EphemeralKeyPub() (*big.Int, error) { return pub, nil } +func (u *SRPUser) SetScramblingParam(a *big.Int) error { + b, err := u.EphemeralKeyPub() + if err != nil { + return err + } + bb := b.Bytes() + ab := a.Bytes() + + // Make M=A+B + m := make([]byte, 0) + m = append(m, ab...) + m = append(m, bb...) + if len(m) != (len(ab) + len(bb)) { + return CPError{"length of m is incorrect"} + } + + // Hash M + u.h.Message(m) + h := u.h.Hash() + + // Set scrambling paramter u + u.u = new(big.Int) + u.u.SetBytes(h) + if u.u.Cmp(big.NewInt(0)) != 1 { + return CPError{"u is invalid"} + } + return nil +} + func NewSRPClientSession(n, g, k, ident string) (*SRPClientSession, error) { var ok bool @@ -212,3 +241,32 @@ func (s *SRPClientSession) EphemeralKeyPub() (*big.Int, error) { return pub, nil } + +func (s *SRPClientSession) SetScramblingParam(b *big.Int) error { + a, err := s.EphemeralKeyPub() + if err != nil { + return err + } + ab := a.Bytes() + bb := b.Bytes() + + // Make M=A+B + m := make([]byte, 0) + m = append(m, ab...) + m = append(m, bb...) + if len(m) != (len(ab) + len(bb)) { + return CPError{"length of m is incorrect"} + } + + // Hash M + s.h.Message(m) + h := s.h.Hash() + + // Set scrambling paramter u + s.u = new(big.Int) + s.u.SetBytes(h) + if s.u.Cmp(big.NewInt(0)) != 1 { + return CPError{"u is invalid"} + } + return nil +} diff --git a/lib/srp_test.go b/lib/srp_test.go index 3e3c8d6..a819b15 100644 --- a/lib/srp_test.go +++ b/lib/srp_test.go @@ -182,3 +182,69 @@ func TestSRPClientSessionEphemeralKeyPub(t *testing.T) { return } } + +func TestSRPScramblingParamter(t *testing.T) { + n := StripSpaceChars( + `ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024 + e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd + 3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec + 6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f + 24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361 + c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552 + bb9ed529077096966d670c354e4abc9804f1746c08ca237327fff + fffffffffffff`) + g := "2" + k := "3" + ident := "s@ricketyspace.net" + pass := "d59d6c93af0f37f272d924979" + + // Init srp server user. + user, err := NewSRPUser(n, g, k, ident, pass) + if err != nil { + t.Errorf("Error: %v\n", err) + return + } + + // Get server's pub for user. + user.EphemeralKeyGen() + pubB, err := user.EphemeralKeyPub() + if err != nil { + t.Errorf("Error: %v\n", err) + return + } + + // Init srp client session. + session, err := NewSRPClientSession(n, g, k, ident) + if err != nil { + t.Errorf("Error: %v\n", err) + return + } + + // Get client's pub for user. + pubA, err := session.EphemeralKeyPub() + if err != nil { + t.Errorf("Error: %v\n", err) + return + } + + // Compute server's scrambling parameter for user. + err = user.SetScramblingParam(pubA) + if err != nil { + t.Errorf("Error: %v\n", err) + return + } + + // Compute client's scrambling paramter for user. + err = session.SetScramblingParam(pubB) + if err != nil { + t.Errorf("Error: %v\n", err) + return + } + + // The server's and client's scrambling parameter must be the + // same. + if user.u.Cmp(session.u) != 0 { + t.Error("Error: scrambling parameter of server != client\n") + return + } +} -- cgit v1.2.3