Class: SRP::Session

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/srp/session.rb

Constant Summary

Constants included from Util

Util::BIG_PRIME_N, Util::GENERATOR, Util::PRIME_N

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#bigrand, #hn_xor_hg, #modpow, #multiplier, #sha256_hex, #sha256_int, #sha256_str

Constructor Details

#initialize(user, aa = nil) ⇒ Session

params: user: user object that represents and account (username, salt, verifier) aa: SRPs A ephemeral value. encoded as a hex string.



9
10
11
12
# File 'lib/srp/session.rb', line 9

def initialize(user, aa=nil)
  @user = user
  aa ? initialize_server(aa) : initialize_client
end

Instance Attribute Details

#userObject

Returns the value of attribute user.



4
5
6
# File 'lib/srp/session.rb', line 4

def user
  @user
end

Instance Method Details

#aaObject



67
68
69
# File 'lib/srp/session.rb', line 67

def aa
  @aa ||= modpow(GENERATOR, @a).to_s(16) # A = g^a (mod N)
end

#authenticate(client_auth) ⇒ Object



30
31
32
33
34
35
# File 'lib/srp/session.rb', line 30

def authenticate(client_auth)
  if(client_auth == m)
    @authenticated = true
    return @user
  end
end

#authenticate!(client_auth) ⇒ Object



26
27
28
# File 'lib/srp/session.rb', line 26

def authenticate!(client_auth)
  authenticate(client_auth) || raise(SRP::WrongPassword)
end

#bbObject

B = g^b + k v (mod N)



72
73
74
# File 'lib/srp/session.rb', line 72

def bb
  @bb ||= calculate_bb.to_s(16)
end

#handshake(server) ⇒ Object

client -> server: I, A = g^a



15
16
17
18
19
# File 'lib/srp/session.rb', line 15

def handshake(server)
  bb = server.handshake(user.username, aa)
  validate_ephemeral(bb)
  @bb = bb
end

#internal_stateObject

for debugging use:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/srp/session.rb', line 53

def internal_state
  {
    username: @user.username,
    salt: @user.salt.to_s(16),
    verifier: @user.verifier.to_s(16),
    aa: aa,
    bb: bb,
    s: secret.to_s(16),
    k: k,
    m: m,
    m2: m2
  }
end

#to_hashObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/srp/session.rb', line 37

def to_hash
  if @authenticated
    { :M2 => m2 }
  else
    { :B => bb,
#         :b => @b.to_s(16),    # only use for debugging
      :salt => @user.salt.to_s(16)
    }
  end
end

#to_json(options = {}) ⇒ Object



48
49
50
# File 'lib/srp/session.rb', line 48

def to_json(options={})
  to_hash.to_json(options)
end

#validate(server) ⇒ Object

client -> server: M = H(H(N) xor H(g), H(I), s, A, B, K)



22
23
24
# File 'lib/srp/session.rb', line 22

def validate(server)
  server.validate(m)
end