Class: SRP::Verifier

Inherits:
Object
  • Object
show all
Defined in:
lib/srp-rb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group = 1024, hash = 'sha-1') ⇒ Verifier

Returns a new instance of Verifier.



339
340
341
342
343
344
# File 'lib/srp-rb.rb', line 339

def initialize(group = 1024, hash = 'sha-1')
  # select modulus (N) and generator (g)
  @N, @g = SRP.Ng(group)
  @hash = hash
  @k = SRP.calc_k(@N, @g, @hash)
end

Instance Attribute Details

#AObject (readonly)

Returns the value of attribute A.



337
338
339
# File 'lib/srp-rb.rb', line 337

def A
  @A
end

#bObject (readonly)

Returns the value of attribute b.



337
338
339
# File 'lib/srp-rb.rb', line 337

def b
  @b
end

#BObject (readonly)

Returns the value of attribute B.



337
338
339
# File 'lib/srp-rb.rb', line 337

def B
  @B
end

#gObject (readonly)

Returns the value of attribute g.



337
338
339
# File 'lib/srp-rb.rb', line 337

def g
  @g
end

#H_AMKObject (readonly)

Returns the value of attribute H_AMK.



337
338
339
# File 'lib/srp-rb.rb', line 337

def H_AMK
  @H_AMK
end

#kObject (readonly)

Returns the value of attribute k.



337
338
339
# File 'lib/srp-rb.rb', line 337

def k
  @k
end

#KObject (readonly)

Returns the value of attribute K.



337
338
339
# File 'lib/srp-rb.rb', line 337

def K
  @K
end

#MObject (readonly)

Returns the value of attribute M.



337
338
339
# File 'lib/srp-rb.rb', line 337

def M
  @M
end

#NObject (readonly)

Returns the value of attribute N.



337
338
339
# File 'lib/srp-rb.rb', line 337

def N
  @N
end

#SObject (readonly)

Returns the value of attribute S.



337
338
339
# File 'lib/srp-rb.rb', line 337

def S
  @S
end

Instance Method Details

#generate_B(xverifier) ⇒ Object

generates challenge input verifier in hex



416
417
418
419
420
# File 'lib/srp-rb.rb', line 416

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

#generate_userauth(username, password) ⇒ Object

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



349
350
351
352
353
354
355
# File 'lib/srp-rb.rb', line 349

def generate_userauth username, password
  @salt ||= random_salt
  # warn "salt: #{@salt}"
  x = SRP.calc_x(username, password, @salt, @hash)
  v = SRP.calc_v(x, @N, @g)
  return {:username => username, :verifier => "%x" % v, :salt => @salt}
end

#get_challenge_and_proof(username, xverifier, xsalt, xaa) ⇒ 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.



360
361
362
363
364
365
366
367
368
369
370
# File 'lib/srp-rb.rb', line 360

def get_challenge_and_proof username, xverifier, xsalt, xaa
  # SRP-6a safety check
  return false if (xaa.to_i(16) % @N) == 0
  generate_B(xverifier)
  # warn "A: #{xaa}"
  return {
    :challenge    => {:B => @B, :salt => xsalt},
    :proof        => {:A => xaa, :B => @B, :b => "%x" % @b,
                      :I => username, :s => xsalt, :v => xverifier}
  }
end

#random_bignumObject



410
411
412
# File 'lib/srp-rb.rb', line 410

def random_bignum
  SRP.bigrand(32).hex
end

#random_saltObject



406
407
408
# File 'lib/srp-rb.rb', line 406

def random_salt
  "%x" % SRP.bigrand(16).hex
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)



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/srp-rb.rb', line 375

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

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

  # calculate session key
  @S = "%x" % SRP.calc_server_S(@A.to_i(16), @b, v, u, @N)

  @K = SRP.sha1_hex(@S, @hash)
  # warn "K: #{@K}"

  # calculate match
  @M = "%x" % SRP.calc_M1(@A, @B, @K, @N, @hash)
  
  if @M == client_M
    # authentication succeeded
    @H_AMK = "%x" % SRP.calc_M2(@A, @M, @K, @N, @hash)
    return @H_AMK
  end
  return false
end