Class: CryptoToolchain::SRP::Client

Inherits:
Object
  • Object
show all
Includes:
Framework
Defined in:
lib/crypto_toolchain/srp/client.rb

Direct Known Subclasses

SimpleClient

Constant Summary

Constants included from Framework

Framework::EVENT_WHITELIST

Instance Attribute Summary collapse

Attributes included from Framework

#email, #g, #k, #key, #n, #password, #privkey, #pubkey, #salt, #socket

Instance Method Summary collapse

Methods included from Framework

#error_received, #event_loop, #go!, #shutdown_received, #write_message

Constructor Details

#initialize(**kargs) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
# File 'lib/crypto_toolchain/srp/client.rb', line 9

def initialize(**kargs)
  provided_pubkey = kargs.delete(:pubkey)
  super(**kargs)
  @pubkey = provided_pubkey || g.modpow(privkey, n)
end

Instance Attribute Details

#authenticatedObject (readonly) Also known as: authenticated?

Returns the value of attribute authenticated.



6
7
8
# File 'lib/crypto_toolchain/srp/client.rb', line 6

def authenticated
  @authenticated
end

#server_pubkeyObject (readonly)

Returns the value of attribute server_pubkey.



6
7
8
# File 'lib/crypto_toolchain/srp/client.rb', line 6

def server_pubkey
  @server_pubkey
end

Instance Method Details

#authentication_success_receivedObject

Raises:



33
34
35
36
37
# File 'lib/crypto_toolchain/srp/client.rb', line 33

def authentication_success_received
  @authenticated = true
  write_message("shutdown")
  raise ShutdownSignal
end

#calculate_secretObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/crypto_toolchain/srp/client.rb', line 39

def calculate_secret
  return 0 if [0, n, n**2, n**3].include?(pubkey)

  xH = Digest::SHA256.hexdigest("#{salt}#{password}")
  x = xH.to_i(16)
  uH = Digest::SHA256.hexdigest("#{pubkey}#{server_pubkey}")
  u = uH.to_i(16)
  # S = (B - k * g**x)**(a + u * x) % N
  (server_pubkey - k * g.modpow(x, n)).modpow(privkey + u * x, n)
end

#hello_received(_salt, _server_pubkey) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/crypto_toolchain/srp/client.rb', line 24

def hello_received(_salt, _server_pubkey)
  @salt = _salt.to_i
  @server_pubkey = _server_pubkey.to_i
  secret = calculate_secret
  puts "Client generated secret #{secret}" if DEBUG
  @key = Digest::SHA256.hexdigest(secret.to_s)
  send_verify
end

#send_helloObject



15
16
17
# File 'lib/crypto_toolchain/srp/client.rb', line 15

def send_hello
  write_message("hello", email, pubkey)
end

#send_verifyObject



19
20
21
22
# File 'lib/crypto_toolchain/srp/client.rb', line 19

def send_verify
  hmac = OpenSSL::HMAC.hexdigest("SHA256", key.to_s, salt.to_s)
  write_message("verify", hmac)
end