summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2022-04-10 11:07:52 -0400
committersiddharth <s@ricketyspace.net>2022-04-10 11:31:08 -0400
commitac3632e554c576c4d7e24bca17d3f23abf664883 (patch)
tree6029940355e65b53638e2d9f9fe292f0dd52fe91
parentafc801186f2591555eb43c0138ac1951da02cea4 (diff)
lib: add srp ephemeral functions
-rw-r--r--lib/srp.go51
-rw-r--r--lib/srp_test.go66
2 files changed, 117 insertions, 0 deletions
diff --git a/lib/srp.go b/lib/srp.go
index 491565c..05b61ab 100644
--- a/lib/srp.go
+++ b/lib/srp.go
@@ -134,6 +134,42 @@ func NewSRPUser(n, g, k, ident, pass string) (*SRPUser, error) {
return user, nil
}
+func (u *SRPUser) EphemeralKeyGen() {
+ for {
+ u.b = big.NewInt(RandomInt(1, 10000000))
+ if u.b.Cmp(big.NewInt(0)) == 1 {
+ break
+ }
+ }
+}
+
+func (u *SRPUser) EphemeralKeyPub() (*big.Int, error) {
+ if u.k == nil || u.k.Cmp(big.NewInt(0)) != 1 {
+ return nil, CPError{"k is not initialized"}
+ }
+ if u.v == nil || u.v.Cmp(big.NewInt(0)) != 1 {
+ return nil, CPError{"v is not initialized"}
+ }
+ if u.g == nil || u.g.Cmp(big.NewInt(0)) != 1 {
+ return nil, CPError{"g is not initialized"}
+ }
+ if u.b == nil || u.b.Cmp(big.NewInt(0)) != 1 {
+ return nil, CPError{"b is not initialized"}
+ }
+
+ kv := new(big.Int)
+ kv.Mul(u.k, u.v)
+
+ gb := new(big.Int)
+ gb.Exp(u.g, u.b, u.n)
+
+ // pub is 'B'
+ pub := new(big.Int)
+ pub.Add(kv, gb)
+
+ return pub, nil
+}
+
func NewSRPClientSession(n, g, k, ident string) (*SRPClientSession, error) {
var ok bool
@@ -161,3 +197,18 @@ func NewSRPClientSession(n, g, k, ident string) (*SRPClientSession, error) {
return session, nil
}
+
+func (s *SRPClientSession) EphemeralKeyPub() (*big.Int, error) {
+ if s.g == nil || s.g.Cmp(big.NewInt(0)) != 1 {
+ return nil, CPError{"g is not initialized"}
+ }
+ if s.a == nil || s.a.Cmp(big.NewInt(0)) != 1 {
+ return nil, CPError{"a is not initialized"}
+ }
+
+ // pub is 'A'
+ pub := new(big.Int)
+ pub.Exp(s.g, s.a, s.n)
+
+ return pub, nil
+}
diff --git a/lib/srp_test.go b/lib/srp_test.go
index a31e658..3e3c8d6 100644
--- a/lib/srp_test.go
+++ b/lib/srp_test.go
@@ -68,6 +68,42 @@ func TestNewSRPUser(t *testing.T) {
}
}
+func TestSRPUserEphemeralKey(t *testing.T) {
+ n := StripSpaceChars(
+ `ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024
+ e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd
+ 3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec
+ 6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f
+ 24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361
+ c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552
+ bb9ed529077096966d670c354e4abc9804f1746c08ca237327fff
+ fffffffffffff`)
+ g := "2"
+ k := "3"
+ ident := "s@ricketyspace.net"
+ pass := "d59d6c93af0f37f272d924979"
+ user, err := NewSRPUser(n, g, k, ident, pass)
+ if err != nil {
+ t.Errorf("Error: %v\n", err)
+ return
+ }
+
+ user.EphemeralKeyGen()
+ if user.b.Cmp(big.NewInt(0)) != 1 {
+ t.Errorf("Error: b is <= 0")
+ return
+ }
+ pub, err := user.EphemeralKeyPub()
+ if err != nil {
+ t.Errorf("Error: %v\n", err)
+ return
+ }
+ if pub.Cmp(big.NewInt(0)) != 1 {
+ t.Errorf("Error: pub is <= 0")
+ return
+ }
+}
+
func TestNewSRPClientSession(t *testing.T) {
n := StripSpaceChars(
`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024
@@ -116,3 +152,33 @@ func TestNewSRPClientSession(t *testing.T) {
return
}
}
+
+func TestSRPClientSessionEphemeralKeyPub(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
+ }
+
+ pub, err := session.EphemeralKeyPub()
+ if err != nil {
+ t.Errorf("Error: %v\n", err)
+ return
+ }
+ if pub.Cmp(big.NewInt(0)) != 1 {
+ t.Errorf("Error: pub is <= 0")
+ return
+ }
+}