Class: RubyHome::SRP::Verifier

Inherits:
SRP::Verifier
  • Object
show all
Defined in:
lib/ruby_home/srp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group = 3072) ⇒ Verifier

Returns a new instance of Verifier.



103
104
105
106
107
# File 'lib/ruby_home/srp.rb', line 103

def initialize group=3072
  # select modulus (N) and generator (g)
  @N, @g = SRP.Ng group
  @k = SRP.calc_k(@N, @g)
end

Instance Attribute Details

#b=(value) ⇒ Object (writeonly)

Sets the attribute b

Parameters:

  • value

    the value to set the attribute b to.



101
102
103
# File 'lib/ruby_home/srp.rb', line 101

def b=(value)
  @b = value
end

#salt=(value) ⇒ Object (writeonly)

Sets the attribute salt

Parameters:

  • value

    the value to set the attribute salt to.



101
102
103
# File 'lib/ruby_home/srp.rb', line 101

def salt=(value)
  @salt = value
end

#uObject (readonly)

Returns the value of attribute u.



100
101
102
# File 'lib/ruby_home/srp.rb', line 100

def u
  @u
end

Instance Method Details

#generate_B(xverifier) ⇒ Object

generates challenge input verifier in hex



174
175
176
177
178
# File 'lib/ruby_home/srp.rb', line 174

def generate_B xverifier
  v = xverifier.to_i(16)
  @b ||= random_bignum
  @B = SRP.calc_B(@b, @k, v, @N, @g.hex).to_hex_string
end

#generate_userauth(username, password) ⇒ Object

Initial user creation for the persistance layer. Not part of the authentication process. Returns { <username>, <password verifier>, <salt> }



112
113
114
115
116
117
# File 'lib/ruby_home/srp.rb', line 112

def generate_userauth username, password
  @salt ||= random_salt
  x = SRP.calc_x(username, password, @salt)
  v = SRP.calc_v(x, @N, @g.hex)
  return {:username => username, :verifier => v.to_hex_string, :salt => @salt}
end

#get_challenge_and_proof(username, xverifier, xsalt) ⇒ Object

Authentication phase 1 - create challenge. Returns Hash with challenge for client and proof to be stored on server. Parameters should be given in hex.



122
123
124
125
126
127
128
# File 'lib/ruby_home/srp.rb', line 122

def get_challenge_and_proof username, xverifier, xsalt
  generate_B(xverifier)
  return {
    :challenge => {:B => @B, :salt => xsalt},
    :proof     => {:B => @B, :b => @b.to_hex_string, :I => username, :s => xsalt, :v => xverifier}
  }
end

#random_bignumObject



164
165
166
# File 'lib/ruby_home/srp.rb', line 164

def random_bignum
  SecureRandom.hex(32).hex
end

#random_saltObject



160
161
162
# File 'lib/ruby_home/srp.rb', line 160

def random_salt
  SecureRandom.hex(16)
end

#verify_session(proof, client_M) ⇒ Object

returns H_AMK on success, None on failure User -> Host: M = H(H(N) xor H(g), H(I), s, A, B, K) Host -> User: H(A, M, K)



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ruby_home/srp.rb', line 133

def verify_session proof, client_M
  @A = proof[:A]
  @B = proof[:B]
  @b = proof[:b].to_i(16)
  username = proof[:I]
  xsalt = proof[:s]
  v = proof[:v].to_i(16)

  @u = SRP.calc_u(@A, @B, @N)
  # SRP-6a safety check
  return false if @u == 0

  # calculate session key
  @S = SRP.calc_server_S(@A.to_i(16), @b, v, @u, @N).to_hex_string
  @K = SRP.sha512_hex(@S)

  # calculate match
  @M = SRP.calc_M(username, xsalt, @A, @B, @K, @N, @g).to_hex_string

  if @M == client_M
    # authentication succeeded
    @H_AMK = SRP.calc_H_AMK(@A, @M, @K, @N, @g).to_hex_string
    return @H_AMK
  end
  return false
end